Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define rep(i, a, b) for(int i = (a); i < int(b); ++i)
- #define rrep(i, a, b) for(int i = (a) - 1; i >= int(b); --i)
- #define trav(it, v) for(typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
- #define all(v) (v).begin(), (v).end()
- typedef double fl;
- typedef long long ll;
- typedef pair<int, int> pii;
- typedef vector<int> vi;
- typedef vector<pii> vpi;
- typedef pair<ll, pair<ll, ll> > hashType;
- clock_t startTime;
- bool aborted;
- int statesDone;
- int checkTime;
- int DX[8]={-1,-1,-1,0,1,1,1,0}, DY[8]={-1,0,1,1,1,0,-1,-1};
- int bufdis[2][10][10];
- int bufdis2[2][10][10];
- int bufneigh[10][10];
- float squareScore[10][10];
- int bufx[100];
- int bufy[100];
- float coeff;
- bool lateGame;
- struct Bot{
- bool useAlphaBeta;
- bool useIterativeDeepening;
- bool useMobilityEval;
- bool useWeirdMobility;
- bool useDistanceEval;
- bool useExtraDistanceEval;
- bool useTranspositionTable;
- bool useKiller;
- bool useHistory;
- bool useFullHistory;
- bool useLateGameEval;
- bool updateCoeff;
- //bool restrictMoves;
- string name;
- float eFilled0;
- float eFilled1;
- float eFilled2;
- float eFilled3;
- float eScore1_1;
- float eScore1_2;
- float eScore1_3;
- float eScore1_4;
- float eScore2_1;
- float eScore2_2;
- float eScore2_3;
- float eScore2_4;
- float eScore2_5;
- float eScore2_6;
- float eCoeffMax;
- float eMobilityWeight;
- float eMobilityFactor;
- float timeLimit;
- Bot(){}
- Bot(string _name){
- timeLimit=0.85;
- name = _name;
- useAlphaBeta = true;
- useIterativeDeepening = true;
- useWeirdMobility = false;
- useMobilityEval = true;
- useDistanceEval = false;
- useExtraDistanceEval = false;
- useTranspositionTable = false;
- useLateGameEval = false;
- useKiller = false;
- useHistory = false;
- updateCoeff = true;
- /*eFilled0=1;
- eFilled1=0.5;
- eFilled2=0.3;
- eFilled3=0.1;
- eScore1_1=60;
- eScore1_2=80;
- eScore1_3=90;
- eScore1_4=100;
- eScore2_1=24;
- eScore2_2=40;
- eScore2_3=60;
- eScore2_4=80;
- eScore2_5=90;
- eScore2_6=100;
- eCoeffMax=70;*/
- eFilled0=0.98;
- eFilled1=0.51;
- eFilled2=0.29;
- eFilled3=0.09;
- eScore1_1=62;
- eScore1_2=82;
- eScore1_3=90;
- eScore1_4=96;
- eScore2_1=20;
- eScore2_2=40;
- eScore2_3=60;
- eScore2_4=80;
- eScore2_5=90;
- eScore2_6=100;
- eCoeffMax=72;
- eMobilityWeight=1000;
- eMobilityFactor=0.1;
- //restrictMoves = false;
- }
- void setFinal(){
- eFilled0=0.86;
- eFilled1=0.5;
- eFilled2=0.38;
- eFilled3=0.15;
- eScore1_1=65;
- eScore1_2=72;
- eScore1_3=77;
- eScore1_4=82;
- eScore2_1=14;
- eScore2_2=26;
- eScore2_3=38;
- eScore2_4=52;
- eScore2_5=75;
- eScore2_6=105;
- eCoeffMax=62;
- eMobilityWeight=1000;
- eMobilityFactor=0.095;
- }
- void load(string fileName){
- FILE* fin = fopen(fileName.c_str(), "r");
- fscanf(fin, "%f", &eFilled0);
- fscanf(fin, "%f", &eFilled1);
- fscanf(fin, "%f", &eFilled2);
- fscanf(fin, "%f", &eFilled3);
- fscanf(fin, "%f", &eScore1_1);
- fscanf(fin, "%f", &eScore1_2);
- fscanf(fin, "%f", &eScore1_3);
- fscanf(fin, "%f", &eScore1_4);
- fscanf(fin, "%f", &eScore2_1);
- fscanf(fin, "%f", &eScore2_2);
- fscanf(fin, "%f", &eScore2_3);
- fscanf(fin, "%f", &eScore2_4);
- fscanf(fin, "%f", &eScore2_5);
- fscanf(fin, "%f", &eScore2_6);
- fscanf(fin, "%f", &eCoeffMax);
- fscanf(fin, "%f", &eMobilityWeight);
- fscanf(fin, "%f", &eMobilityFactor);
- fclose(fin);
- }
- void save(string fileName){
- FILE* fout = fopen(fileName.c_str(), "w");
- fprintf(fout, "%f\n", eFilled0);
- fprintf(fout, "%f\n", eFilled1);
- fprintf(fout, "%f\n", eFilled2);
- fprintf(fout, "%f\n", eFilled3);
- fprintf(fout, "%f\n", eScore1_1);
- fprintf(fout, "%f\n", eScore1_2);
- fprintf(fout, "%f\n", eScore1_3);
- fprintf(fout, "%f\n", eScore1_4);
- fprintf(fout, "%f\n", eScore2_1);
- fprintf(fout, "%f\n", eScore2_2);
- fprintf(fout, "%f\n", eScore2_3);
- fprintf(fout, "%f\n", eScore2_4);
- fprintf(fout, "%f\n", eScore2_5);
- fprintf(fout, "%f\n", eScore2_6);
- fprintf(fout, "%f\n", eCoeffMax);
- fprintf(fout, "%f\n", eMobilityWeight);
- fprintf(fout, "%f\n", eMobilityFactor);
- fclose(fout);
- }
- void printParams(){
- cout << eFilled0 << " = " << eFilled0 << endl;
- cout << eFilled1 << " = " << eFilled1 << endl;
- cout << eFilled2 << " = " << eFilled2 << endl;
- cout << eFilled3 << " = " << eFilled3 << endl;
- cout << eScore1_1 << " = " << eScore1_1 << endl;
- cout << eScore1_2 << " = " << eScore1_2 << endl;
- cout << eScore1_3 << " = " << eScore1_3 << endl;
- cout << eScore1_4 << " = " << eScore1_4 << endl;
- cout << eScore2_1 << " = " << eScore2_1 << endl;
- cout << eScore2_2 << " = " << eScore2_2 << endl;
- cout << eScore2_3 << " = " << eScore2_3 << endl;
- cout << eScore2_4 << " = " << eScore2_4 << endl;
- cout << eScore2_5 << " = " << eScore2_5 << endl;
- cout << eScore2_6 << " = " << eScore2_6 << endl;
- cout << eCoeffMax << " = " << eCoeffMax << endl;
- }
- } bot;
- void printPos(int x, int y){
- printf("%c%d", 'a'+y, x+1);
- }
- struct Move{
- int fx, fy;
- int tx, ty;
- int ax, ay;
- Move(){
- }
- Move(int _fx, int _fy, int _tx, int _ty, int _ax, int _ay){
- fx=_fx;
- fy=_fy;
- tx=_tx;
- ty=_ty;
- ax=_ax;
- ay=_ay;
- }
- bool operator<(const Move &other) const{
- if(fx != other.fx)
- return fx < other.fx;
- if(tx != other.tx)
- return tx < other.tx;
- if(ax != other.ax)
- return ax < other.ax;
- if(fy != other.fy)
- return fy < other.fy;
- if(ty != other.ty)
- return ty < other.ty;
- if(ay != other.ay)
- return ay < other.ay;
- return 0;
- }
- bool operator==(const Move &other) const{
- if(fx != other.fx)
- return 0;
- if(fy != other.fy)
- return 0;
- if(tx != other.tx)
- return 0;
- if(ty != other.ty)
- return 0;
- if(ax != other.ax)
- return 0;
- if(ay != other.ay)
- return 0;
- return 1;
- }
- void print(){
- printPos(fx, fy);
- printf(" - ");
- printPos(tx, ty);
- printf(" : ");
- printPos(ax, ay);
- printf("\n");
- }
- void printUgly(){
- printf("%d %d\n%d %d\n%d %d\n", fx, fy, tx, ty, ax, ay);
- }
- };
- map<hashType, int> lowerBound[125];
- map<hashType, int> upperBound[125];
- map<hashType, int> scoreMap;
- struct State{
- char f[10][10];
- //char region[10][10];
- int turn;
- //int regionN;
- //vector<int> regionSize;
- //vector<int> amazonN[2];
- hashType toHash(){
- ll h1=0, h2=0, h3=0;
- int k=0;
- rep(i,0,6){
- rep(j,0,10){
- if(f[i][j] != 0)
- h1 |= (1LL<<k);
- ++k;
- }
- }
- k=0;
- rep(i,6,10){
- rep(j,0,10){
- if(f[i][j] != 0)
- h2 |= (1LL<<k);
- ++k;
- }
- }
- ll mul=1;
- rep(i,0,10)
- rep(j,0,10){
- if(f[i][j] == 1){
- h3 |= mul*(10*i+j);
- mul *= 100;
- }
- }
- rep(i,0,10)
- rep(j,0,10){
- if(f[i][j] == 2){
- h3 |= mul*(10*i+j);
- mul *= 100;
- }
- }
- return make_pair(h1, make_pair(h2, h3));
- }
- State performMove(Move move){
- State ret=*this;
- ret.f[move.tx][move.ty]=ret.f[move.fx][move.fy];
- ret.f[move.fx][move.fy]=0;
- ret.f[move.ax][move.ay]=-1;
- ret.turn^=1;
- return ret;
- }
- bool lineFree(int fx, int fy, int tx, int ty, int ix, int iy){
- int dx, dy;
- if(tx > fx)dx=1;
- else if(tx < fx)dx=-1;
- else dx=0;
- if(ty > fy)dy=1;
- else if(ty < fy)dy=-1;
- else dy=0;
- fx += dx;
- fy += dy;
- while(fx != tx && fy != ty){
- if(f[fx][fy] != 0 && !(fx == ix && fy == iy))
- return 0;
- fx += dx;
- fy += dy;
- }
- return 1;
- }
- bool okMove(Move move){
- if(move.fx < 0 || move.fy < 0 || move.fx >= 10 || move.fy >= 10)
- return 0;
- if(f[move.fx][move.fy]-1 != turn)
- return 0;
- if(f[move.tx][move.ty] != 0)
- return 0;
- if(!lineFree(move.fx, move.fy, move.tx, move.ty, -1, -1))
- return 0;
- if(!lineFree(move.tx, move.ty, move.ax, move.ay, move.fx, move.fy))
- return 0;
- if(f[move.ax][move.ay] != 0 && !(move.ax == move.fx && move.ay == move.fy))
- return 0;
- return 1;
- }
- void print(int indent){
- rep(i,0,10){
- rep(j,0,indent)
- printf(" ");
- rep(j,0,10){
- if(f[i][j] == 0)
- printf(".");
- else if(f[i][j] == -1)
- printf("#");
- else
- printf("%c", '0'+f[i][j]);
- }
- printf("\n");
- }
- printf("\n");
- }
- void print(){
- print(0);
- }
- int eval(){
- hashType h;
- if(bot.useTranspositionTable){
- h=toHash();
- if(scoreMap.count(h)){
- return scoreMap[h];
- }
- }
- int ret=0;
- if(!bot.useDistanceEval && bot.useMobilityEval){
- rep(i,0,10)
- rep(j,0,10){
- if(f[i][j] <= 0)
- continue;
- int mobility=0;
- rep(k,0,8){
- int x=i+DX[k];
- int y=j+DY[k];
- if(x < 0 || y < 0 || x >= 10 || y >= 10)
- continue;
- if(f[x][y] == 0)
- mobility++;
- }
- if(f[i][j]-1 == turn)
- ret += mobility;
- else
- ret -= mobility;
- }
- }
- rep(i,0,10)
- rep(j,0,10){
- if(f[i][j] == -1)
- continue;
- bufneigh[i][j]=0;
- rep(k,0,8){
- int tx=i+DX[k];
- int ty=j+DY[k];
- if(tx < 0 || tx >= 10)
- continue;
- if(ty < 0 || ty >= 10)
- continue;
- if(f[tx][ty] != 0)
- continue;
- bufneigh[i][j]++;
- }
- }
- rep(i,0,10)
- rep(j,0,10){
- squareScore[i][j]=1;
- }
- if(lateGame && bot.useLateGameEval){
- rep(i,0,10)
- rep(j,0,10){
- if(f[i][j] == -1)
- continue;
- squareScore[i][j]=1+bufneigh[i][j]*0.005;
- /*if(bufneigh[i][j] <= 3){
- bool ok=0;
- rep(k,0,8){
- int tx=i+DX[k];
- int ty=j+DY[k];
- if(tx < 0 || tx >= 10)
- continue;
- if(ty < 0 || ty >= 10)
- continue;
- if(f[tx][ty] == -1)
- continue;
- if(bufneigh[tx][ty] > 1){
- ok=1;
- break;
- }
- }
- if(!ok)
- squareScore[i][j]=0.5;
- }*/
- //squareScore[i][j]=1;
- }
- }
- if(bot.useDistanceEval){
- //print();
- for(int player=0; player <= 1; ++player){
- int qfront=0;
- int qend=0;
- rep(i,0,10)
- rep(j,0,10){
- bufdis[player][i][j]=120;
- bufdis2[player][i][j]=120;
- if(f[i][j] == player+1){
- bufx[qend]=i;
- bufy[qend]=j;
- bufdis[player][i][j]=0;
- ++qend;
- }
- }
- while(qfront < qend){
- int i=bufx[qfront];
- int j=bufy[qfront];
- int D=bufdis[player][i][j];
- ++qfront;
- for(int dir=0; dir < 8; ++dir)
- for(int len=1; true; ++len){
- int tx=i+DX[dir]*len;
- int ty=j+DY[dir]*len;
- if(tx < 0 || tx >= 10)
- break;
- if(ty < 0 || ty >= 10)
- break;
- if(f[tx][ty] != 0)
- break;
- if(bufdis[player][tx][ty] <= D+1)
- continue;
- bufdis[player][tx][ty]=D+1;
- bufx[qend]=tx;
- bufy[qend]=ty;
- ++qend;
- }
- }
- qfront=0;
- qend=0;
- rep(i,0,10)
- rep(j,0,10){
- if(f[i][j] == player+1){
- bufx[qend]=i;
- bufy[qend]=j;
- bufdis2[player][i][j]=0;
- ++qend;
- }
- }
- while(qfront < qend){
- int i=bufx[qfront];
- int j=bufy[qfront];
- int D=bufdis2[player][i][j];
- ++qfront;
- for(int dir=0; dir < 8; ++dir){
- int tx=i+DX[dir];
- int ty=j+DY[dir];
- if(tx < 0 || tx >= 10)
- continue;
- if(ty < 0 || ty >= 10)
- continue;
- if(f[tx][ty] != 0)
- continue;
- if(bufdis2[player][tx][ty] <= D+1)
- continue;
- bufdis2[player][tx][ty]=D+1;
- bufx[qend]=tx;
- bufy[qend]=ty;
- ++qend;
- }
- }
- }
- float ret1=0, ret2=0;
- if(bot.updateCoeff)
- coeff=0;
- rep(i,0,10)
- rep(j,0,10){
- int diff=bufdis[turn][i][j]-bufdis[!turn][i][j];
- if(bot.updateCoeff){
- if(diff == 0){
- if(bufdis[turn][i][j] < 10)
- coeff += bot.eFilled0;
- continue;
- }
- if(abs(diff) == 1)
- coeff += bot.eFilled1;
- else if(abs(diff) == 2)
- coeff += bot.eFilled2;
- else if(abs(diff) <= 5){
- coeff += bot.eFilled3;
- }
- }
- if(diff < 0){
- if(diff == -1)
- ret1 += bot.eScore1_1*squareScore[i][j];
- else if(diff == -2)
- ret1 += bot.eScore1_2*squareScore[i][j];
- else if(diff == -3)
- ret1 += bot.eScore1_3*squareScore[i][j];
- else
- ret1 += bot.eScore1_4*squareScore[i][j];
- continue;
- }
- if(diff > 0){
- if(diff == 1)
- ret1 -= bot.eScore1_1*squareScore[i][j];
- else if(diff == 2)
- ret1 -= bot.eScore1_2*squareScore[i][j];
- else if(diff == 3)
- ret1 -= bot.eScore1_3*squareScore[i][j];
- else
- ret1 -= bot.eScore1_4*squareScore[i][j];
- continue;
- }
- }
- if(!bot.useExtraDistanceEval){
- return ret1;
- }
- rep(i,0,10)
- rep(j,0,10){
- int diff=bufdis2[turn][i][j]-bufdis2[!turn][i][j];
- if(diff == 0)
- continue;
- int adiff=abs(diff);
- int change;
- switch(adiff){
- case 1:change=bot.eScore2_1;break;
- case 2:change=bot.eScore2_2;break;
- case 3:change=bot.eScore2_3;break;
- case 4:change=bot.eScore2_4;break;
- case 5:change=bot.eScore2_5;break;
- default:change=bot.eScore2_6;break;
- }
- if(diff < 0){
- ret2 += change*squareScore[i][j];
- continue;
- }
- if(diff > 0){
- ret2 -= change*squareScore[i][j];
- continue;
- }
- }
- if(bot.updateCoeff)
- coeff /= bot.eCoeffMax;
- if(bot.useMobilityEval){
- //print();
- rep(i,0,10)
- rep(j,0,10){
- if(f[i][j] <= 0)
- continue;
- int p=f[i][j];
- float m=0;
- for(int dir=0; dir < 8; ++dir){
- float mult=1;
- for(int len=1; true; ++len){
- int tx=i+DX[dir]*len;
- int ty=j+DY[dir]*len;
- if(tx < 0 || tx >= 10)
- break;
- if(ty < 0 || ty >= 10)
- break;
- if(f[tx][ty] != 0)
- break;
- if(bufdis2[2-p][tx][ty] > 50)
- break;
- m += mult*bufneigh[tx][ty];
- mult*=0.5;
- }
- }
- float change=bot.eMobilityWeight*coeff*(1-exp(-m*bot.eMobilityFactor));
- if(bot.useWeirdMobility)
- change=5000*coeff/(5+bot.eMobilityFactor);
- if(p-1 == turn)
- ret += change;
- else
- ret -= change;
- }
- }
- ret += ret2*coeff+ret1*(1-coeff);
- }
- /*if(!regionN)
- regionify();
- rep(i,0,regionN){
- if(amazonN[0][i] == amazonN[1][i])
- continue;
- if(!amazonN[turn][i]){
- ret -= regionSize[i];
- continue;
- }
- if(!amazonN[!turn][i]){
- ret += regionSize[i];
- continue;
- }
- ret += regionSize[i]*(amazonN[turn][i]-amazonN[!turn][i]);
- }*/
- if(bot.useTranspositionTable){
- scoreMap[h]=ret;
- }
- return ret;
- }
- bool operator<(const State &other) const{
- return 0;
- }
- /*void regionify(){
- queue<pair<int, int> > q;
- rep(i,0,10)
- rep(j,0,10)
- region[i][j]=-1;
- regionN=0;
- rep(i,0,10)
- rep(j,0,10){
- if(f[i][j] == -1 || region[i][j] != -1)
- continue;
- q.push(make_pair(i, j));
- while(!q.empty()){
- int x=q.front().first;
- int y=q.front().second;
- q.pop();
- rep(d,0,8){
- int nx=x+DX[d];
- int ny=y+DY[d];
- if(nx < 0 || ny < 0 || nx >= 10 || ny >= 10)
- continue;
- if(f[nx][ny] == -1 || region[nx][ny] != -1)
- continue;
- region[nx][ny]=regionN;
- q.push(make_pair(nx, ny));
- }
- }
- ++regionN;
- }
- regionSize=vector<int>(regionN);
- amazonN[0]=vector<int>(regionN);
- amazonN[1]=vector<int>(regionN);
- rep(i,0,10)
- rep(j,0,10){
- if(region[i][j] != -1){
- ++regionSize[region[i][j]];
- if(f[i][j] != 0)
- ++amazonN[f[i][j]-1][region[i][j]];
- }
- }
- }*/
- };
- /*unordered_map<ll, int> lowerBound[70];
- unordered_map<ll, int> upperBound[70];
- unordered_map<ll, int> scoreMap[70];*/
- State parseState(){
- State ret;
- for(int i=0; i < 10; ++i)
- for(int j=0; j < 10; ++j){
- int f;
- scanf("%d", &f);
- ret.f[i][j]=f;
- }
- scanf("%d", &ret.turn);
- ret.turn -= 1;
- return ret;
- }
- bool doFullSearch=false;
- int statesChecked;
- int startD=0;
- bool printStuff;
- Move killerMoves[125];
- map<Move, int> historyScore;
- int alphabeta(State s, int d, int alpha, int beta){
- ++statesChecked;
- if(aborted)
- return 0;
- //s.regionify();
- statesDone += 100;
- if(statesDone > checkTime && startD){
- checkTime += 10000;
- if(!doFullSearch && (float(clock()-startTime))/CLOCKS_PER_SEC > bot.timeLimit){
- aborted=1;
- return 0;
- }
- }
- if(!d){
- return s.eval();
- }
- int sc=-1000000000;
- bool existsDisputed=false;
- //s.print();
- vector<pair<int, pair<State, Move> > > v;
- Move killerMove = killerMoves[d];
- if(bot.useKiller){
- if(s.okMove(killerMove)){
- State newState=s.performMove(killerMove);
- v.push_back(make_pair(-1500000000, make_pair(newState, killerMove)));
- }
- }
- rep(fx,0,10)
- rep(fy,0,10){
- if(s.f[fx][fy]-1 != s.turn)
- continue;
- for(int dir=0; dir < 8; ++dir)
- for(int len=1; true; ++len){
- int tx=fx+DX[dir]*len;
- int ty=fy+DY[dir]*len;
- if(tx < 0 || tx >= 10)
- break;
- if(ty < 0 || ty >= 10)
- break;
- if(s.f[tx][ty] != 0)
- break;
- for(int adir=0; adir < 8; ++adir)
- for(int alen=1; true; ++alen){
- int ax=tx+DX[adir]*alen;
- int ay=ty+DY[adir]*alen;
- Move move(fx, fy, tx, ty, ax, ay);
- if(ax < 0 || ax >= 10)
- break;
- if(ay < 0 || ay >= 10)
- break;
- if(s.f[ax][ay] != 0 && !(ax == fx && ay == fy))
- break;
- if(move == killerMove)
- continue;
- //if(!s.okMove(move))
- // assert(0);
- /*if(bot.restrictMoves){
- if(s.amazonN[!s.turn][s.region[fx][fy]]){
- if(!existsDisputed){
- existsDisputed=true;
- v.clear();
- }
- }
- else{
- if(existsDisputed)
- continue;
- }
- }*/
- State newState=s.performMove(move);
- if(bot.useIterativeDeepening){
- if(d > 1 && !bot.useFullHistory)
- v.push_back(make_pair(newState.eval(), make_pair(newState, move)));
- else if(bot.useHistory)
- v.push_back(make_pair(-historyScore[move], make_pair(newState, move)));
- else
- v.push_back(make_pair(0, make_pair(newState, move)));
- }
- else{
- v.push_back(make_pair(0, make_pair(newState, move)));
- }
- }
- }
- }
- if((d > 1 || bot.useHistory) && bot.useIterativeDeepening)sort(v.begin(), v.end());
- rep(i,0,v.size()){
- int score;
- score=-alphabeta(v[i].second.first, d-1, -beta, -alpha);
- if(score > alpha){
- //scoreMap[d][sLL]=score;
- alpha=score;
- }
- if(score > sc){
- sc=score;
- }
- if(sc >= beta && bot.useAlphaBeta){
- killerMoves[d]=v[i].second.second;
- if(bot.useHistory)
- historyScore[v[i].second.second] += d*d;
- return sc;
- }
- }
- if(v.empty())
- sc=-1000000000;
- return sc;
- }
- Move findMove(State s){
- historyScore.clear();
- statesChecked=0;
- Move bestMove(-1,-1,-1,-1,-1,-1);
- Move lastBestMove=bestMove;
- bool lastBestMoveExists=0;
- bool anyMove=false;
- int bestScore=-1000000001;
- int numMoves=0;
- rep(i,0,10)
- rep(j,0,10){
- numMoves += (s.f[i][j] == -1);
- }
- int randSize;
- if(numMoves < 6)
- randSize=30;
- else if(numMoves < 10)
- randSize=10;
- else if(numMoves < 20)
- randSize=5;
- else if(numMoves < 40)
- randSize=1;
- else
- randSize=1;
- lateGame = (numMoves > 50);
- vector<pair<int, Move> > perm;
- srand(time(0));
- rep(fx,0,10)
- rep(fy,0,10){
- if(s.f[fx][fy]-1 != s.turn)
- continue;
- for(int dir=0; dir < 8; ++dir)
- for(int len=1; true; ++len){
- int tx=fx+DX[dir]*len;
- int ty=fy+DY[dir]*len;
- if(tx < 0 || tx >= 10)
- break;
- if(ty < 0 || ty >= 10)
- break;
- if(s.f[tx][ty] != 0)
- break;
- for(int adir=0; adir < 8; ++adir)
- for(int alen=1; true; ++alen){
- int ax=tx+DX[adir]*alen;
- int ay=ty+DY[adir]*alen;
- Move move(fx, fy, tx, ty, ax, ay);
- if(ax < 0 || ax >= 10)
- break;
- if(ay < 0 || ay >= 10)
- break;
- if(s.f[ax][ay] != 0 && !(ax == fx && ay == fy))
- break;
- if(!s.okMove(move))
- assert(0);
- State newState=s.performMove(move);
- if(bot.useIterativeDeepening){
- int add=rand()%randSize;
- perm.push_back(make_pair(newState.eval()+add, move));
- }
- else
- perm.push_back(make_pair(0, move));
- }
- }
- }
- if(bot.useIterativeDeepening)
- sort(all(perm));
- else
- random_shuffle(all(perm));
- vector<int> scores;
- bool updateCoeff= bot.updateCoeff;
- if(!updateCoeff){
- bot.updateCoeff=true;
- s.eval();
- bot.updateCoeff=false;
- }
- rep(d,0,105){
- startD=d;
- bestScore=-1000000001;
- bestMove=lastBestMove;
- if(lastBestMove.fx != -1){
- Move move=lastBestMove;
- State newState=s.performMove(move);
- int score;
- int add=rand()%randSize;
- score=-alphabeta(newState, d, -1000000000, 1000000000)+add;
- if(score > bestScore){
- bestScore = score;
- bestMove=move;
- #ifdef DEBUG_PRINT
- printf("d = %d, bestScore = %d : ", d, bestScore);
- bestMove.print();
- #endif
- }
- }
- rep(i,0,perm.size()){
- Move move=perm[i].second;
- if(d && lastBestMove == move)
- continue;
- State newState=s.performMove(move);
- int score;
- score=-alphabeta(newState, d, -1000000000, -bestScore);
- if(score > bestScore && !aborted){
- int add=rand()%randSize;
- score += add;
- bestScore = score;
- bestMove=move;
- anyMove=true;
- #ifdef DEBUG_PRINT
- printf("d = %d, bestScore = %d : ", d, bestScore);
- bestMove.print();
- //newState.print();
- #endif
- }
- }
- if(!anyMove)
- return Move(-2,-2,-2,-2,-2,-2);
- lastBestMove=bestMove;
- if(aborted){
- break;
- }
- scores.push_back(bestScore);
- }
- #ifdef DEBUG_PRINT
- cout << bot.name << " ";
- printf("%d (%d)\n", scores.back(), scores.size());
- printf("States checked: %d\n", statesChecked);
- #endif
- bestMove=lastBestMove;
- assert(s.okMove(bestMove));
- return bestMove;
- }
- vector<Bot> bots;
- pair<int, int> playGames(){
- vector<int> totScore(bots.size(), 0);
- vector<vector<int> > stats(bots.size(), vector<int>(bots.size(), -1));
- for(int b1=0; b1 < bots.size(); ++b1)
- for(int b2=0; b2 < bots.size(); ++b2){
- if(b1 == b2)
- continue;
- string fileName = "game_" + bots[b1].name + "_" + bots[b2].name + ".txt";
- FILE *file;
- if(file = fopen(fileName.c_str(), "r")){
- int result;
- fscanf(file, "%d", &result);
- fclose(file);
- stats[b1][b2]=result;
- continue;
- }
- }
- for(int b1=0; b1 < bots.size(); ++b1)
- for(int b2=0; b2 < bots.size(); ++b2){
- if(b1 == b2)
- continue;
- string fileName = "game_" + bots[b1].name + "_" + bots[b2].name + ".txt";
- FILE *file;
- if(file = fopen(fileName.c_str(), "r")){
- int result;
- fscanf(file, "%d", &result);
- fclose(file);
- stats[b1][b2]=result;
- continue;
- }
- file = fopen(fileName.c_str(), "w");
- State s;
- rep(i,0,10)
- rep(j,0,10)
- s.f[i][j]=0;
- s.f[3][0]=2;
- s.f[0][3]=2;
- s.f[0][6]=2;
- s.f[3][9]=2;
- s.f[6][0]=1;
- s.f[9][3]=1;
- s.f[9][6]=1;
- s.f[6][9]=1;
- s.turn=0;
- int turn=0;
- while(true){
- scoreMap.clear();
- startTime=clock();
- statesDone=0;
- aborted=0;
- checkTime=0;
- if(turn == 0)
- bot=bots[b1];
- else
- bot=bots[b2];
- Move bestMove = findMove(s);
- if(bestMove.fx == -2){
- stats[b1][b2]=s.turn;
- fprintf(file, "%d", stats[b1][b2]);
- fclose(file);
- break;
- }
- s=s.performMove(bestMove);
- turn=!turn;
- #ifdef DEBUG_PRINT
- s.print();
- #endif
- bestMove.print();
- //getch();
- }
- #ifdef DEBUG_PRINT
- s.print();
- #endif
- rep(a,0,bots.size()){
- totScore[a]=0;
- printf("%s", bots[a].name.c_str());
- rep(k,bots[a].name.size(), 14)printf(" ");
- int sum=0;
- rep(b,0,bots.size()){
- if(stats[a][b] == -1)printf(" ");
- else{
- printf("%d", stats[a][b]);
- sum += stats[a][b];
- }
- if(stats[b][a] == -1)printf(" ");
- else{
- printf("%d", !stats[b][a]);
- sum += !stats[b][a];
- }
- }
- totScore[a]=sum;
- printf(" %d\n", sum);
- }
- }
- return make_pair(totScore[bots.size()-2], totScore[bots.size()-1]);
- }
- int main(){
- startTime = clock();
- /*Bot bot1("mob");
- bot1.useAlphaBeta=false;
- bot1.useIterativeDeepening=false;
- bot1.useMobilityEval=true;
- bots.push_back(bot1);
- Bot bot2("default");
- bots.push_back(bot2);*/
- /*Bot bot3("Ni");
- bot3.useIterativeDeepening=false;
- bot3.useMobilityEval=false;
- bot3.useDistanceEval=true;
- bots.push_back(bot3);
- Bot bot4("NiRm");
- bot4.useIterativeDeepening=false;
- bot4.useMobilityEval=false;
- bot4.useDistanceEval=true;
- bots.push_back(bot4);*/
- /*Bot bot5("MbDi");
- bot5.useMobilityEval=false;
- bot5.useDistanceEval=true;
- bots.push_back(bot5);*/
- /*Bot bot6("NiEf");
- bot6.useIterativeDeepening=false;
- bot6.useMobilityEval=false;
- bot6.useDistanceEval=true;
- bot6.useExtraDistanceEval=true;
- bots.push_back(bot6);
- Bot bot7("Ef");
- bot7.useMobilityEval=false;
- bot7.useDistanceEval=true;
- bot7.useExtraDistanceEval=true;
- bots.push_back(bot7);*/
- /*Bot bot8("NiEfMo");
- bot8.useIterativeDeepening=false;
- bot8.useMobilityEval=true;
- bot8.useDistanceEval=true;
- bot8.useExtraDistanceEval=true;
- bots.push_back(bot8);*/
- Bot bot9("EfMo");
- bot9.useMobilityEval=true;
- bot9.useDistanceEval=true;
- bot9.useExtraDistanceEval=true;
- bots.push_back(bot9);
- /*Bot bot10("NiEfMoK");
- bot10.useIterativeDeepening=false;
- bot10.useMobilityEval=true;
- bot10.useDistanceEval=true;
- bot10.useExtraDistanceEval=true;
- bot10.useKiller=true;
- bots.push_back(bot10);*/
- Bot bot11("EfMoK");
- bot11.useMobilityEval=true;
- bot11.useDistanceEval=true;
- bot11.useExtraDistanceEval=true;
- bot11.useKiller=true;
- bots.push_back(bot11);
- Bot bot12("EfMoHi");
- bot12.useMobilityEval=true;
- bot12.useDistanceEval=true;
- bot12.useExtraDistanceEval=true;
- bot12.useHistory=true;
- bots.push_back(bot12);
- /*Bot bot13("NiEfMoTr");
- bot13.useIterativeDeepening=false;
- bot13.useMobilityEval=true;
- bot13.useDistanceEval=true;
- bot13.useExtraDistanceEval=true;
- bot13.useTranspositionTable=true;
- bots.push_back(bot13);*/
- Bot bot14("EfMoTr");
- bot14.useMobilityEval=true;
- bot14.useDistanceEval=true;
- bot14.useExtraDistanceEval=true;
- bot14.useTranspositionTable=true;
- bots.push_back(bot14);
- /*Bot bot15("NiEfMoKTr");
- bot15.useIterativeDeepening=false;
- bot15.useMobilityEval=true;
- bot15.useDistanceEval=true;
- bot15.useExtraDistanceEval=true;
- bot15.useKiller=true;
- bot15.useTranspositionTable=true;
- bots.push_back(bot15);*/
- Bot bot16("EfMoKTr");
- bot16.useMobilityEval=true;
- bot16.useDistanceEval=true;
- bot16.useExtraDistanceEval=true;
- bot16.useKiller=true;
- bot16.useTranspositionTable=true;
- bots.push_back(bot16);
- Bot bot17("EfMoHiTr");
- bot17.useMobilityEval=true;
- bot17.useDistanceEval=true;
- bot17.useExtraDistanceEval=true;
- bot17.useHistory=true;
- bot17.useTranspositionTable=true;
- bots.push_back(bot17);
- Bot bot18("EfMoFHi");
- bot18.useMobilityEval=true;
- bot18.useDistanceEval=true;
- bot18.useExtraDistanceEval=true;
- bot18.useHistory=true;
- bot18.useFullHistory=true;
- bots.push_back(bot18);
- Bot bot19("EfMoFHiTr");
- bot19.useMobilityEval=true;
- bot19.useDistanceEval=true;
- bot19.useExtraDistanceEval=true;
- bot19.useHistory=true;
- bot19.useFullHistory=true;
- bot19.useTranspositionTable=true;
- bots.push_back(bot19);
- Bot botFinal=bot16;
- botFinal.name="Final";
- botFinal.setFinal();
- bots.push_back(botFinal);
- Bot botFinal2=botFinal;
- botFinal2.name="Final2";
- bots.push_back(botFinal2);
- Bot botFinal3=botFinal2;
- botFinal3.name="Final3";
- botFinal3.updateCoeff=false;
- bots.push_back(botFinal3);
- Bot botFinal4=botFinal3;
- botFinal4.name="Final3_50_";
- botFinal4.updateCoeff=false;
- botFinal4.eCoeffMax=50;
- bots.push_back(botFinal4);
- Bot botFinal5=botFinal3;
- botFinal5.name="Final3_55_";
- botFinal5.updateCoeff=false;
- botFinal5.eCoeffMax=55;
- bots.push_back(botFinal5);
- Bot botFinal6=botFinal3;
- botFinal6.name="Final3_57_";
- botFinal6.updateCoeff=false;
- botFinal6.eCoeffMax=57;
- bots.push_back(botFinal6);
- Bot botFinal9=botFinal3;
- botFinal9.name="Final3_58_";
- botFinal9.updateCoeff=false;
- botFinal9.eCoeffMax=58;
- bots.push_back(botFinal9);
- Bot botFinal10=botFinal3;
- botFinal10.name="Final3_60_";
- botFinal10.updateCoeff=false;
- botFinal10.eCoeffMax=60;
- bots.push_back(botFinal10);
- Bot botFinal7=botFinal3;
- botFinal7.name="Final3_61_";
- botFinal7.updateCoeff=false;
- botFinal7.eCoeffMax=61;
- bots.push_back(botFinal7);
- Bot botFinal8=botFinal3;
- botFinal8.name="Final3_63_";
- botFinal8.updateCoeff=false;
- botFinal8.eCoeffMax=63;
- bots.push_back(botFinal8);
- Bot botFinalEG=botFinal;
- botFinalEG.name="FinalEG";
- botFinalEG.useLateGameEval=true;
- bots.push_back(botFinalEG);
- Bot botFinal3EG=botFinal3;
- botFinal3EG.name="Final3EG";
- botFinal3EG.useLateGameEval=true;
- bots.push_back(botFinal3EG);
- bot=botFinal3EG;
- /*State start = parseState();
- while(true){
- getch();
- State s;
- rep(i,0,5)
- rep(j,0,5)
- s.f[i][j]=0;
- //start=s;
- int movesDone=0;
- int prevScore=0;
- while(true){
- rep(i,0,70){
- scoreMap[i].clear();
- }
- startTime=clock();
- statesDone=0;
- checkTime=100000;
- aborted=0;
- Move bestMove = findMove(start);
- if(bestMove.mx == -2)
- break;
- bestMove.print();
- start=start.performMove(bestMove);
- start.print();
- if(movesDone > 45 && abs(abs(newScore)-abs(prevScore)) > 2000){
- assert(0);
- }
- prevScore=newScore;
- ++movesDone;
- getch();
- }
- }*/
- State start = parseState();
- Move bestMove = findMove(start);
- bestMove.printUgly();
- return 0;
- playGames();
- return 0;
- playGames();
- Bot improveBot=botFinal3;
- improveBot.load("improve.bot");
- int botN=10;
- float alpha=0.04;
- while(true){
- alpha *= 0.993;
- float* changeVal;
- srand(time(0));
- int r=rand()%17;
- switch(r){
- case 0: changeVal=&improveBot.eFilled0;break;
- case 1: changeVal=&improveBot.eFilled1;break;
- case 2: changeVal=&improveBot.eFilled2;break;
- case 3: changeVal=&improveBot.eFilled3;break;
- case 4: changeVal=&improveBot.eScore1_1;break;
- case 5: changeVal=&improveBot.eScore1_2;break;
- case 6: changeVal=&improveBot.eScore1_3;break;
- case 7: changeVal=&improveBot.eScore1_4;break;
- case 8: changeVal=&improveBot.eScore2_1;break;
- case 9: changeVal=&improveBot.eScore2_2;break;
- case 10: changeVal=&improveBot.eScore2_3;break;
- case 11: changeVal=&improveBot.eScore2_4;break;
- case 12: changeVal=&improveBot.eScore2_5;break;
- case 13: changeVal=&improveBot.eScore2_6;break;
- case 14: changeVal=&improveBot.eMobilityWeight;break;
- case 15: changeVal=&improveBot.eMobilityFactor;break;
- default: changeVal=&improveBot.eCoeffMax;break;
- }
- float initVal=*changeVal;
- *changeVal=initVal*(1-alpha);
- Bot botL=improveBot;
- *changeVal=initVal*(1+alpha);
- Bot botH=improveBot;
- stringstream ss;
- string name=improveBot.name;
- while(name.back() >= '0' && name.back() <= '9')
- name.pop_back();
- ss << name << botN;
- botL.name=ss.str();
- botL.name.push_back('L');
- botH.name=ss.str();
- botH.name.push_back('H');
- *changeVal=initVal;
- bots.push_back(botL);
- bots.push_back(botH);
- pair<int, int> result = playGames();
- float w1=result.first;
- float w2=result.second;
- bots.pop_back();
- bots.pop_back();
- //changeVal=initVal*(1+alpha*(w2-w1)/(1+w2+w1));
- Bot newBot;
- if(w2 > w1)
- newBot=botH;
- else if(w2 < w1)
- newBot=botL;
- else
- newBot=improveBot;
- //Bot newBot=improveBot;
- *changeVal=initVal;
- newBot.name=ss.str();
- newBot.printParams();
- if(rand()%10 == 0)
- bots.push_back(newBot);
- improveBot=newBot;
- improveBot.save("improve.bot");
- ++botN;
- }
- }
Add Comment
Please, Sign In to add comment