libkombilo
0.7
|
00001 00104 #ifndef _ALGOS_H_ 00105 #define _ALGOS_H_ 00106 00107 #include <vector> 00108 #include <stack> 00109 #include <fstream> 00110 #include <stdint.h> 00111 #include <sqlite3.h> 00112 #include "boost/unordered_map.hpp" 00113 00114 #include "abstractboard.h" 00115 #include "sgfparser.h" 00116 #include "pattern.h" 00117 00118 00119 00120 class GameList; 00121 class SearchOptions; 00122 00123 00125 class Algorithm { 00126 public: 00127 Algorithm(int bsize); 00128 virtual ~Algorithm(); 00129 00130 virtual void initialize_process(); 00131 virtual void newgame_process(int game_id); 00132 virtual void AB_process(int x, int y); 00133 virtual void AW_process(int x, int y); 00134 virtual void AE_process(int x, int y, char removed); 00135 virtual void endOfNode_process(); 00136 virtual void move_process(Move m); 00137 virtual void pass_process(); 00138 virtual void branchpoint_process(); 00139 virtual void endOfVariation_process(); 00140 virtual void endgame_process(bool commit=true); 00141 virtual void finalize_process(); 00142 00143 virtual SnapshotVector get_data(); 00144 00145 virtual int search(PatternList& patternList, GameList& gl, SearchOptions& options); 00146 00147 int gid; 00148 int boardsize; 00149 }; 00150 00151 00152 // -------------------------------------------------------------------------------------------------- 00153 00155 class Algo_signature : public Algorithm { 00156 public: 00157 Algo_signature(int bsize, SnapshotVector DATA); 00158 ~Algo_signature(); 00159 void initialize_process(); 00160 void newgame_process(int game_id); 00161 void AB_process(int x, int y); 00162 void AW_process(int x, int y); 00163 void AE_process(int x, int y, char removed); 00164 void endOfNode_process(); 00165 void move_process(Move m); 00166 void pass_process(); 00167 void branchpoint_process(); 00168 void endOfVariation_process(); 00169 void endgame_process(bool commit=true); 00170 void finalize_process(); 00171 00172 int counter; 00173 char* signature; 00174 char* get_current_signature(); 00175 std::vector<int> search_signature(char* sig); 00176 00177 SnapshotVector get_data(); 00178 boost::unordered_multimap<string, int> data; 00179 private: 00180 bool main_variation; 00181 }; 00182 00183 00203 class Algo_finalpos : public Algorithm { 00204 public: 00205 Algo_finalpos(int bsize, SnapshotVector DATA); 00206 ~Algo_finalpos(); 00207 void initialize_process(); 00208 void newgame_process(int game_id); 00209 void AB_process(int x, int y); 00210 void AW_process(int x, int y); 00211 void AE_process(int x, int y, char removed); 00212 void endOfNode_process(); 00213 void move_process(Move m); 00214 void pass_process(); 00215 void branchpoint_process(); 00216 void endOfVariation_process(); 00217 void endgame_process(bool commit=true); 00218 void finalize_process(); 00219 hashtype get_current_fphash(); 00220 hashtype get_fphash(int index); 00221 00222 char* fp; 00223 int fpIndex; 00224 00225 SnapshotVector get_data(); 00226 std::vector<pair<int, char* > > data; 00227 00228 int search(PatternList& patternList, GameList& gl, SearchOptions& options); 00229 }; 00230 00231 00232 // ------------------------------------------------------------------------------------------------------- 00233 00234 00235 // in x-coord: 00236 const int ENDOFNODE = 128; 00237 const int BRANCHPOINT = 64; 00238 const int ENDOFVARIATION = 32; 00239 00240 // in y-coord 00241 const int REMOVE = 128; 00242 const int BLACK = 64; 00243 const int WHITE = 32; 00244 00245 00246 class MovelistCand { 00247 public: 00248 int orientation; 00249 Pattern* p; 00250 char* dicts; 00251 ExtendedMoveNumber dictsF; 00252 bool dictsFound; 00253 ExtendedMoveNumber dictsFI; 00254 bool node_changes_relevant_region; 00255 bool dictsFoundInitial; 00256 bool dictsDR; 00257 int dictsNO; 00258 std::vector<MoveNC> contList; 00259 int contListIndex; 00260 p_cc Xinterv; 00261 p_cc Yinterv; 00262 char mx; 00263 char my; 00264 00265 MovelistCand(Pattern* P, int ORIENTATION, char* DICTS, int NO, char X, char Y); 00266 ~MovelistCand(); 00267 char dictsget(char x, char y); 00268 void dictsset(char x, char y, char d); 00269 bool in_relevant_region(char x, char y); 00270 char contlistgetX(int i); 00271 char contlistgetY(int i); 00272 char contlistgetCO(int i); 00273 }; 00274 00275 class VecMC : public std::vector<MovelistCand* > { 00276 public: 00277 VecMC(); 00278 ~VecMC(); 00279 VecMC* deepcopy(ExtendedMoveNumber& COUNTER, int CANDSSIZE); 00280 ExtendedMoveNumber counter; 00281 int candssize; 00282 }; 00283 00284 00285 00286 00299 class Algo_movelist : public Algorithm { 00300 public: 00301 Algo_movelist(int bsize, SnapshotVector DATA); 00302 ~Algo_movelist(); 00303 void initialize_process(); 00304 void newgame_process(int game_id); 00305 void AB_process(int x, int y); 00306 void AW_process(int x, int y); 00307 void AE_process(int x, int y, char removed); 00308 void endOfNode_process(); 00309 void move_process(Move m); 00310 void pass_process(); 00311 void branchpoint_process(); 00312 void endOfVariation_process(); 00313 void endgame_process(bool commit=true); 00314 void finalize_process(); 00315 int search(PatternList& patternList, GameList& gl, SearchOptions& options); 00316 00317 std::vector<char> movelist; 00318 char* fpC; 00319 std::map<int, char* > data1; 00320 std::map<int, char* > data2; 00321 std::map<int, int> data1l; 00322 SnapshotVector get_data(); 00323 }; 00324 00325 00326 // -------------------------------------------------------------------------------------------------------- 00327 00328 00329 00330 class HashhitF { // hashing hit for full board search 00331 public: 00332 int gameid; 00333 char orientation; 00334 MoveNC* cont; 00335 ExtendedMoveNumber* emn; 00336 00337 HashhitF(); 00338 HashhitF(int GAMEID, char ORIENTATION, ExtendedMoveNumber& EMN, MoveNC* CONT); 00339 HashhitF(int GAMEID, char ORIENTATION, char* blob); 00340 HashhitF(const HashhitF& HHF); 00341 ~HashhitF(); 00342 HashhitF& operator=(const HashhitF& HHF); 00343 00344 char* export_blob(); 00345 }; 00346 00347 typedef vector<HashhitF* >* vpsip; 00348 00349 class HashhitCS { // hashing hit for corner/side pattern search 00350 public: 00351 int gameid; 00352 int position; 00353 bool cs; 00354 HashhitCS(int GAMEID, int POSITION, bool CS); 00355 bool operator==(const HashhitCS& hhc); 00356 }; 00357 00358 class HashVarInfo { 00359 public: 00360 hashtype chc; 00361 std::vector<std::pair<hashtype, ExtendedMoveNumber> > * lfc; 00362 ExtendedMoveNumber* moveNumber; 00363 int numStones; 00364 00365 HashVarInfo(hashtype CHC, std::vector<std::pair<hashtype, ExtendedMoveNumber> > * LFC, ExtendedMoveNumber* MOVENUMBER, int NUMSTONES); 00366 }; 00367 00369 class Algo_hash_full : public Algorithm { 00381 public: 00382 Algo_hash_full(int bsize, SnapshotVector DATA, const string OS_DATA_NAME, int MAXNUMSTONES = 50); 00383 ~Algo_hash_full(); 00384 void initialize_process(); 00385 void newgame_process(int game_id); 00386 void AB_process(int x, int y); 00387 void AW_process(int x, int y); 00388 void AE_process(int x, int y, char removed); 00389 void endOfNode_process(); 00390 void move_process(Move m); 00391 void pass_process(); 00392 void branchpoint_process(); 00393 void endOfVariation_process(); 00394 void endgame_process(bool commit=true); 00395 void finalize_process(); 00396 int search(PatternList& patternList, GameList& gl, SearchOptions& options); 00397 00398 void process_lfc(int x, int y, char color); 00399 hashtype compute_hashkey(Pattern& pattern); 00400 int maxNumStones; 00401 int numStones; 00402 00403 vector<pair<hashtype, int> > data; 00404 boost::unordered_multimap<hashtype, HashhitF> data_p; 00405 SnapshotVector get_data(); 00406 fstream os_data; 00407 00408 private: 00409 hashtype currentHashCode; 00410 ExtendedMoveNumber* moveNumber; 00411 std::vector<std::pair<hashtype, ExtendedMoveNumber> > *lfc; // hash code + move number, still *l*ooking *f*or *c*ontinuation 00412 std::stack<HashVarInfo>* branchpoints; 00413 boost::unordered_multimap<hashtype, HashhitF> hash_vector; 00414 void get_HHF(int ptr, vpsip results, int orientation); 00415 }; 00416 00417 00418 // -------------------------------------------------------------------------------------------- 00419 00420 00421 class HashInstance { 00422 // When processing sgf games, Algo_hash maintains a list of HashInstance's - 00423 // those are regions on the board for which hash codes are put into the 00424 // database 00425 00426 public: 00427 HashInstance(char X, char Y, char SIZEX, char SIZEY, int BOARDSIZE); 00428 ~HashInstance(); 00429 bool inRelevantRegion(char X, char Y); 00430 00431 char xx; // position on the board 00432 char yy; 00433 int pos; 00434 int boardsize; 00435 char sizeX; // size of the pattern 00436 char sizeY; 00437 bool changed; 00438 00439 void initialize(); 00440 void finalize(); 00441 void addB(char x, char y); 00442 void removeB(char x, char y); 00443 void addW(char x, char y); 00444 void removeW(char x, char y); 00445 void bppush(); 00446 void bppop(); 00447 std::pair<hashtype,int> cHC(); // returns min(currentHashCode) and corresp. index 00448 hashtype* currentHashCode; // array of 8 hashtype values (to automatically symmetrize hash codes) 00449 std::stack<std::pair<hashtype*,int> >* branchpoints; 00450 int numStones; 00451 }; 00452 00454 class Algo_hash : public Algorithm { 00455 // This class should not be used by the "end-user" (see Algo_hash_corner and 00456 // Algo_hash_sides instead) 00457 00458 public: 00459 Algo_hash(int bsize, SnapshotVector DATA, string OS_DATA_NAME, int MAXNUMSTONES); 00460 virtual ~Algo_hash(); 00461 virtual void initialize_process(); 00462 virtual void newgame_process(int game_id); 00463 virtual void AB_process(int x, int y); 00464 virtual void AW_process(int x, int y); 00465 virtual void AE_process(int x, int y, char removed); 00466 virtual void endOfNode_process(); 00467 virtual void move_process(Move m); 00468 virtual void pass_process(); 00469 virtual void branchpoint_process(); 00470 virtual void endOfVariation_process(); 00471 virtual void endgame_process(bool commit=true); 00472 virtual void finalize_process(); 00473 00480 virtual int search(PatternList& patternList, GameList& gl, SearchOptions& options); 00481 00482 friend class GameList; 00483 friend class Algo_hash_full; 00484 friend class HashInstance; 00485 00486 protected: 00487 00488 int maxNumStones; 00489 SnapshotVector get_data(); //< Used to read data from disk into \c data 00490 00493 virtual void get_HHCS(int ptr, vector<HashhitCS* >* results, bool cs); 00494 00495 virtual std::pair<hashtype,std::vector<int> > compute_hashkey(PatternList& pl, int CS); 00496 static const hashtype hashCodes[]; 00497 std::vector<HashInstance>* hi; 00498 std::vector<std::pair<hashtype, int> > hash_vector; 00499 00500 vector<pair<hashtype, int> > data; 00501 boost::unordered_multimap<hashtype, pair<int,int> > data_p; 00502 fstream os_data; 00503 }; 00504 00506 class Algo_hash_corner : public Algo_hash { 00507 public: 00508 Algo_hash_corner(int bsize, SnapshotVector DATA, string OS_DATA_NAME, int SIZE=7, int MAXNUMSTONES = 20); 00509 00515 std::pair<hashtype,std::vector<int> > compute_hashkey(PatternList& pl, int CS); 00516 00518 int size; 00519 }; 00520 00521 00522 // --------------------------------------------------------------------------------------------------------------- 00523 00524 00525 typedef Algorithm* algo_p; 00526 00527 00528 #endif 00529