![]() |
Cinnamon
1.0
chess engine
|
00001 #include "IterativeDeeping.h" 00002 00003 IterativeDeeping::IterativeDeeping() { 00004 mutex1 =new Mutex(); 00005 openBook=NULL; 00006 ponderEnabled=true; 00007 followBook=true; 00008 setUseBook(true); 00009 } 00010 00011 IterativeDeeping::~IterativeDeeping() { 00012 delete mutex1; 00013 if(openBook) 00014 delete openBook; 00015 } 00016 00017 void IterativeDeeping::enablePonder(bool b) { 00018 ponderEnabled=b; 00019 } 00020 00021 void IterativeDeeping::setFollowBook(bool b) { 00022 followBook=b; 00023 } 00024 void IterativeDeeping::lockMutex(bool b) { 00025 mutex1->lockMutex(b); 00026 } 00027 00028 bool IterativeDeeping::getPonderEnabled() { 00029 return ponderEnabled; 00030 } 00031 00032 void IterativeDeeping::clearMovesPath() { 00033 followBook=true; 00034 Search::clearMovesPath(); 00035 } 00036 00037 bool IterativeDeeping::getUseBook() { 00038 return useBook; 00039 } 00040 00041 void IterativeDeeping::setUseBook(bool b) { 00042 useBook = b; 00043 bool valid = true; 00044 if(b && openBook==NULL) { 00045 openBook= new OpenBook(); 00046 valid = useBook = openBook->load(); 00047 } 00048 if((!b && openBook) || !valid) { 00049 delete openBook; 00050 openBook=NULL; 00051 useBook = false; 00052 } 00053 } 00054 00055 void IterativeDeeping::run() { 00056 lockMutex(true); 00057 _Tmove resultMove; 00058 memset(&resultMove,0,sizeof(resultMove)); 00059 struct timeb start1; 00060 struct timeb end1; 00061 _TpvLine line; 00062 int val=0, tmp; 00063 string pvv; 00064 _Tmove move2; 00065 int TimeTaken; 00066 setRunning(2); 00067 int mply = 0; 00068 startClock(); 00069 clearKillerHeuristic(); 00070 clearAge(); 00071 ftime(&start1); 00072 if(useBook && followBook) { 00073 ASSERT(openBook); 00074 string obMove=openBook->search(getSide(),getMovesPath()); 00075 if(!obMove.size()) { 00076 followBook=false; 00077 } else { 00078 _Tmove move; 00079 getMoveFromSan(obMove,&move); 00080 makemove(&move); 00081 cout << "bestmove " << obMove<<endl << flush; 00082 pushMovesPath((char)decodeBoard(obMove.substr(0,2))); 00083 pushMovesPath(decodeBoard(obMove.substr(2,2))); 00084 lockMutex(false); 00085 return; 00086 } 00087 } 00088 string ponderMove=""; 00089 while (getRunning()) { 00090 if (mply == MAX_PLY-3) 00091 break; 00092 init(); 00093 ++mply; 00094 setMainPly(mply); 00095 00096 if (mply == 1) { 00097 memset(&line, 0, sizeof(_TpvLine)); 00098 val = search( mply, -_INFINITE, _INFINITE, &line); 00099 } else { 00100 memset(&line, 0,sizeof(_TpvLine)); 00101 00102 tmp = search( mply, val - valWINDOW, val + valWINDOW, &line); 00103 if (tmp <= val - valWINDOW || tmp >= val + valWINDOW) { 00104 memset(&line,0, sizeof(_TpvLine)); 00105 00106 tmp = search( mply, val - valWINDOW*2, val + valWINDOW*2, &line); 00107 } 00108 if (tmp <= val - valWINDOW*2 || tmp >= val + valWINDOW*2) { 00109 memset(&line, 0, sizeof(_TpvLine)); 00110 00111 tmp = search( mply, val - valWINDOW*4, val + valWINDOW*4, &line); 00112 } 00113 if (tmp <= val - valWINDOW*4 || tmp >= val + valWINDOW*4) { 00114 memset(&line, 0, sizeof(_TpvLine)); 00115 00116 tmp = search( mply, -_INFINITE, _INFINITE, &line); 00117 } 00118 val = tmp; 00119 } 00120 if ( !getRunning()) { 00121 break; 00122 } 00123 u64 totMoves = 0; 00124 if (mply == 2 ) 00125 setRunning(1); 00126 00127 memcpy(&move2, line.argmove, sizeof(_Tmove)); 00128 pvv.clear(); 00129 string pvvTmp; 00130 00131 for (int t = 0; t < line.cmove; t++) { 00132 pvvTmp.clear(); 00133 pvvTmp+= decodeBoardinv(line.argmove[t].type, line.argmove[t].from, getSide()); 00134 if (pvvTmp.length() != 4) { 00135 pvvTmp+= decodeBoardinv(line.argmove[t].type, line.argmove[t].to, getSide()); 00136 } 00137 pvv+= pvvTmp; 00138 if(t==1)ponderMove=pvvTmp; 00139 pvv+= " "; 00140 }; 00141 00142 memcpy(&resultMove, &move2, sizeof(_Tmove)); 00143 incKillerHeuristic(resultMove.from, resultMove.to, 0x800); 00144 ftime(&end1); 00145 TimeTaken = _time::diffTime(end1, start1); 00146 totMoves += getTotMoves(); 00147 assert(pvv.length()); 00148 int sc=resultMove.score / 100;; 00149 if(resultMove.score >_INFINITE-100)sc=0x7fffffff; 00150 #ifdef DEBUG_MODE 00151 int totStoreHash = nRecordHashA + nRecordHashB + nRecordHashE+1; 00152 int percStoreHashA = nRecordHashA * 100 / totStoreHash; 00153 int percStoreHashB = nRecordHashB * 100 / totStoreHash; 00154 int percStoreHashE = nRecordHashE * 100 / totStoreHash; 00155 00156 int totCutHash = n_cut_hashA + n_cut_hashB + n_cut_hashE+1; 00157 int percCutHashA = n_cut_hashA * 100 / totCutHash; 00158 int percCutHashB = n_cut_hashB * 100 / totCutHash; 00159 int percCutHashE = n_cut_hashE * 100 / totCutHash; 00160 00161 cout << endl<<"info string ply: " << mply<<endl; 00162 cout << "info string tot moves: " << totMoves << endl; 00163 cout << "info string hash stored "<<totStoreHash * 100 / cumulativeMovesCount<<"% (alpha="<<percStoreHashA<<"% beta="<<percStoreHashB<<"% exact="<<percStoreHashE<<"%)"<<endl; 00164 cout << "info string cut hash "<<totCutHash * 100 / cumulativeMovesCount<<"% (alpha="<<percCutHashA<<"% beta="<<percCutHashB<<"% exact="<<percCutHashE<<"%)"<<endl; 00165 u64 nps = 0; 00166 if (TimeTaken) 00167 nps = totMoves * 1000 / TimeTaken; 00168 if (nCutAB) { 00169 betaEfficencyCumulative += betaEfficency / totGen * 10; 00170 cout << "info string beta efficency: " << (int)betaEfficencyCumulative << "%" << endl; 00171 betaEfficency = totGen = 0.0; 00172 } 00173 cout << "info string millsec: " << TimeTaken << " (" << nps/1000 << "k nodes per seconds) "<<endl; 00174 cout << "info string alphaBeta cut: " << nCutAB << endl; 00175 cout << "info string lazy eval cut: " << LazyEvalCuts << endl; 00176 cout << "info string futility pruning cut: "<< nCutFp << endl; 00177 cout << "info string razor cut: " << nCutRazor << endl; 00178 cout << "info string null move cut: " << nNullMoveCut <<endl; 00179 #endif 00180 00181 cout << "info score cp " << sc << " depth " << (int) mply << " nodes " << totMoves << " time " << TimeTaken 00182 << " pv " << pvv << endl << flush; 00183 } 00184 00185 resultMove.capturedPiece = (resultMove.side ^ 1)==WHITE?getPieceAt<WHITE>( POW2[resultMove.to]):getPieceAt<BLACK>( POW2[resultMove.to]); 00186 string bestmove= decodeBoardinv(resultMove.type, resultMove.from, resultMove.side); 00187 if (!(resultMove.type & (KING_SIDE_CASTLE_MOVE_MASK | QUEEN_SIDE_CASTLE_MOVE_MASK))) { 00188 bestmove+= decodeBoardinv(resultMove.type, resultMove.to, resultMove.side); 00189 if (resultMove.promotionPiece != 0xFF) 00190 bestmove+=tolower(FEN_PIECE[resultMove.promotionPiece]); 00191 } 00192 00193 cout << "bestmove " << bestmove; 00194 if(ponderEnabled && ponderMove.size()) { 00195 cout<< " ponder "<<ponderMove; 00196 } 00197 cout<<endl << flush; 00198 pushMovesPath(decodeBoard(bestmove.substr(0,2))); 00199 pushMovesPath(decodeBoard(bestmove.substr(2,2))); 00200 lockMutex(false); 00201 }