【工程】五子棋 AI
项目介绍
一个能够自己学习的1111五子棋AI。
使用 `121+2562+121神经网络,梯度下降算法为神经网络参数调整策略,搭建的五子棋AI模型,通过当前模型预测+dfs搜索` 得到更加精准优质的训练数据以训练得到更加精准的模型。
经过不断迭代得到的AI模型将拥有更强大的实力。
开源内容
请使用小熊猫c++打开代码(用于支持graphics.h可视化头文件)
#include<bits/stdc++.h>
#include<graphics.h>
#include<fstream>
#define double long double
using namespace std;
int a[18][18],allid;
inline string get(int ai=allid){
string r1="",r2="";
int t=ai;
while(t){
r1+=(t%10)+'0';
t/=10;
}
for(int i=0;i<=4;++i){
if(4-i>=r1.size())r2+='0';
else r2+=r1[4-i];
}
return "data"+r2+".txt";
}
const int Screen_Size_X=1000,Screen_Size_Y=600;//窗口的长宽
//第i排第j列;黑1白-1
inline int getx(int i,int j){return 300+(j-1)*40;}
inline int gety(int i,int j){return 100+(i-1)*40;}
inline void cp(int i,int j,int col){
if(col==-1){
setcolor(WHITE);
setfillcolor(WHITE);
}else{
setcolor(BLACK);
setfillcolor(BLACK);
}
fillcircle(getx(i,j),gety(i,j),13);
}
inline void board(){
setcolor(BLACK);
for(int i=1;i<=11;++i){
line(getx(i,1),gety(i,1),getx(i,11),gety(i,11));
line(getx(1,i),gety(1,i),getx(11,i),gety(11,i));
}
}
inline void chess(){
for(int i=1;i<=11;++i){
for(int j=1;j<=11;++j){
// cout<<a[i][j]<<' ';
if(a[i][j]!=0)cp(i,j,a[i][j]);
}
// cout<<'\n';
}
// cout<<'\n';
}
const int Height=256;//神经网络单层神经元个数
struct node{//神经元
double val[Height+1];//与下一层神经元的连接参数
double bias;//神经元偏置
}s[243+Height*2];
inline void loadout(string path){
ofstream fout(path.c_str());
for(int i=1;i<=242+Height*2;++i){
for(int j=1;j<=Height;++j)fout<<fixed<<setprecision(5)<<s[i].val[j]<<' ';
fout<<'\n'<<s[i].bias<<'\n';
}
}
inline void loadin(string path){
ifstream fin(path.c_str());
for(int i=1;i<=242+Height*2;++i){
for(int j=1;j<=Height;++j){
fin>>s[i].val[j];
if(s[i].val[j]!=s[i].val[j]){
cout<<"Nan";
while(1);
}
}
fin>>s[i].bias;
}
}
inline void init(){
//随机生成初始的神经网络
mt19937 rander(time(0));
normal_distribution<double> normal_dist(-0.042, 0.042);
for(int i=1;i<=121+Height;++i)
for(int j=1;j<=Height;++j)s[i].val[j]=normal_dist(rander);
for(int i=122+Height;i<=121+2*Height;++i)
for(int j=1;j<=121;++j)s[i].val[j]=normal_dist(rander);
for(int i=1;i<=242+Height*2;++i)s[i].bias=normal_dist(rander);
loadout(get());
}
double input[Height+1],output[Height+1];
inline void tipt(){
//turn in input
for(int i=1;i<=11;++i)
for(int j=1;j<=11;++j)input[(i-1)*11+j]=a[i][j]*1.0;
}
double tmpout[122],tmp2[Height+1],tmp1[Height+1];
inline void run(bool mem=false){
//以input作为输入运行神经网络返回到output之中
if(mem){
memset(tmp1,0,sizeof tmp1);
memset(tmp2,0,sizeof tmp2);
memset(tmpout,0,sizeof tmpout);
}
memset(output,0,sizeof output);
for(int i=1;i<=121;++i)
for(int j=1;j<=Height;++j)output[j]+=input[i]*s[i].val[j];
for(int i=1;i<=Height;++i)output[i]+=s[121+i].bias;
for(int i=1;i<=Height;++i)input[i]=tmp1[i]=output[i],output[i]=0;
for(int i=1;i<=Height;++i)
for(int j=1;j<=Height;++j)output[j]+=input[i]*s[i+121].val[j];
for(int i=1;i<=Height;++i)output[i]+=s[121+Height+i].bias;
for(int i=1;i<=Height;++i)input[i]=tmp2[i]=output[i],output[i]=0;
for(int i=1;i<=Height;++i)
for(int j=1;j<=121;++j)output[j]+=input[i]*s[i+Height+121].val[j];
for(int i=1;i<=121;++i)output[i]+=s[121+2*Height+i].bias;
if(mem)for(int i=1;i<=121;++i)tmpout[i]=output[i];
}
inline void pc(){
//根据output给出的输出计算打印当前局面每一个位置的权值
double mx=-1e9,mn=1e9;
for(int i=1;i<=121;++i){
mx=max(mx,output[i]);
mn=min(mn,output[i]);
}
for(int i=1;i<=11;++i)
for(int j=1;j<=11;++j){
double t=output[(i-1)*11+j];
int r,g,b;
if(abs(mx-mn)<1e-3)r=b=130,g=235;
else{
r=130-(120*(t-mn)/(mx-mn));
g=235-(125*(t-mn)/(mx-mn));
b=130-(120*(t-mn)/(mx-mn));
}
setcolor(EGERGB(r,g,b));
setfillcolor(EGERGB(r,g,b));
fillcircle(getx(i,j),gety(i,j),17);
}
}
int fx[]={0,1,0,1,-1};
int fy[]={0,0,1,1,1};
inline double pf(int w){//对当前局面进行规则性评分
int win=0,step=0;
for(int i=1;i<=11;++i){
for(int j=1;j<=11;++j){
if(a[i][j]){
++step;
for(int dir=1;dir<=4;++dir){
if(i+4*fx[dir]>11 or j+4*fy[dir]>11 or i+4*fx[dir]<1)continue;
for(int st=0;st<=4;++st){
if(a[i+st*fx[dir]][j+st*fy[dir]]!=a[i][j])break;
if(st==4)win=a[i][j];
}
if(win)break;
}
}
if(win)break;
}
if(win)break;
}
if(step==121)return -0.002;
return w*win*(1.0-0.005*step);
}
double LR=0.01;//learn rate
double err[122],del1[Height+1],del2[Height+1];
inline void study(int x,int y,double target){
//通过调整神经网络使得它在当前局面对(x,y)的判断更接近target
loadin(get());tipt();run(1);
for(int i=1;i<=Height;++i)
if(i<=121)err[i]=del1[i]=del2[i]=0.0;
else del1[i]=del2[i]=0;
err[(x-1)*11+y]=target-output[(x-1)*11+y];
for(int i=1;i<=121;++i)s[121+Height*2+i].bias+=LR*err[i];
for(int i=1;i<=Height;++i)
for(int j=1;j<=121;++j)
s[121+Height+i].val[j]+=LR*err[j]*tmp2[i];
for(int i=1;i<=Height;++i)
for(int j=1;j<=121;++j)
del2[i]+=err[j]*s[121+Height+i].val[j];
for(int i=1;i<=Height;++i)s[121+Height+i].bias+=LR*del2[i];
for(int i=1;i<=Height;++i)
for(int j=1;j<=Height;++j)
s[121+i].val[j]+=LR*del2[j]*tmp1[i];
for(int i=1;i<=Height;++i)
for(int j=1;j<=Height;++j)
del1[i]+=del2[j]*s[121+i].val[j];
for(int i=1;i<=Height;++i)s[121+i].bias+=LR*del1[i];
for(int i=1;i<=121;++i)
for(int j=1;j<=Height;++j)
s[i].val[j]+=LR*del1[j]*input[i];
loadout(get());
}
inline double dfs(int w,int step){//返回w方的最大获胜估值
cout<<step<<"in ";
double p=pf(w);
if(abs(p)>0.001)return p;
loadin(get());tipt();run();
cleardevice();
board();
pc();
int posx=11,posy=11;
double mx=-1e9;
for(int i=1;i<=11;++i)
for(int j=1;j<=11;++j)
if(!a[i][j] and output[(i-1)*11+j]>mx){
mx=output[(i-1)*11+j];
posx=i,posy=j;
}
a[posx][posy]=w;
chess();
double g=-dfs(-w,step+1);
study(posx,posy,g);
a[posx][posy]=0;//回溯
loadin(get());tipt();run();
mx=-1e9;
for(int i=1;i<=11;++i)
for(int j=1;j<=11;++j)
if(!a[i][j])mx=max(mx,output[(i-1)*11+j]);
cout<<step<<"out ";
return mx;
}
int main(){
init();
initgraph(Screen_Size_X,Screen_Size_Y);
setbkcolor(GRAY);
for(allid=1;allid<100000;++allid){
loadin(get(allid-1));
loadout(get());
cout<<allid<<'\n';
memset(a,0,sizeof a);
dfs(1,1);
cout<<'\n';
}
while(1);
closegraph();
return 0;
}