libkombilo  0.8
/home/ug/devel/kombilo-py-dev/kombilo/libkombilo/algos.h
Go to the documentation of this file.
1 
104 #ifndef _ALGOS_H_
105 #define _ALGOS_H_
106 
107 #include <vector>
108 #include <stack>
109 #include <fstream>
110 #include "sqlite3.h"
111 #include "pstdint.h"
112 #include <boost/unordered_map.hpp>
113 
114 #include "abstractboard.h"
115 #include "sgfparser.h"
116 #include "pattern.h"
117 
118 
119 
120 class GameList;
121 class SearchOptions;
122 
123 
125 class Algorithm {
126  public:
127  Algorithm(int bsize);
128  virtual ~Algorithm();
129 
130  virtual void initialize_process();
131  virtual void newgame_process(int game_id);
132  virtual void AB_process(int x, int y);
133  virtual void AW_process(int x, int y);
134  virtual void AE_process(int x, int y, char removed);
135  virtual void endOfNode_process();
136  virtual void move_process(Move m);
137  virtual void pass_process();
138  virtual void branchpoint_process();
139  virtual void endOfVariation_process();
140  virtual void endgame_process(bool commit=true);
141  virtual void finalize_process();
142 
143  virtual SnapshotVector get_data();
144 
145  virtual int search(PatternList& patternList, GameList& gl, SearchOptions& options);
146 
147  int gid;
148  int boardsize;
149 };
150 
151 
152 // --------------------------------------------------------------------------------------------------
153 
155 class Algo_signature : public Algorithm {
156  public:
157  Algo_signature(int bsize, SnapshotVector DATA);
158  ~Algo_signature();
159  void initialize_process();
160  void newgame_process(int game_id);
161  void AB_process(int x, int y);
162  void AW_process(int x, int y);
163  void AE_process(int x, int y, char removed);
164  void endOfNode_process();
165  void move_process(Move m);
166  void pass_process();
167  void branchpoint_process();
168  void endOfVariation_process();
169  void endgame_process(bool commit=true);
170  void finalize_process();
171 
172  int counter;
173  char* signature;
174  char* get_current_signature();
175  std::vector<int> search_signature(char* sig);
176 
178  boost::unordered_multimap<string, int> data;
179  private:
180  bool main_variation;
181 };
182 
183 
203 class Algo_finalpos : public Algorithm {
204  public:
205  Algo_finalpos(int bsize, SnapshotVector DATA);
206  ~Algo_finalpos();
207  void initialize_process();
208  void newgame_process(int game_id);
209  void AB_process(int x, int y);
210  void AW_process(int x, int y);
211  void AE_process(int x, int y, char removed);
212  void endOfNode_process();
213  void move_process(Move m);
214  void pass_process();
215  void branchpoint_process();
216  void endOfVariation_process();
217  void endgame_process(bool commit=true);
218  void finalize_process();
219  hashtype get_current_fphash();
220  hashtype get_fphash(int index);
221 
222  char* fp;
223  int fpIndex;
224 
226  std::vector<pair<int, char* > > data;
227 
228  int search(PatternList& patternList, GameList& gl, SearchOptions& options);
229 };
230 
231 
232 // -------------------------------------------------------------------------------------------------------
233 
234 
235 // in x-coord:
236 const int ENDOFNODE = 128;
237 const int BRANCHPOINT = 64;
238 const int ENDOFVARIATION = 32;
239 
240 // in y-coord
241 const int REMOVE = 128;
242 const int BLACK = 64;
243 const int WHITE = 32;
244 
245 
247  public:
248  int orientation;
249  Pattern* p;
250  char* dicts;
251  ExtendedMoveNumber dictsF;
252  bool dictsFound;
253  ExtendedMoveNumber dictsFI;
254  bool node_changes_relevant_region;
255  bool dictsFoundInitial;
256  bool dictsDR;
257  int dictsNO;
258  std::vector<MoveNC> contList;
259  int contListIndex;
260  p_cc Xinterv;
261  p_cc Yinterv;
262  char mx;
263  char my;
264 
265  MovelistCand(Pattern* P, int ORIENTATION, char* DICTS, int NO, char X, char Y);
266  ~MovelistCand();
267  char dictsget(char x, char y);
268  void dictsset(char x, char y, char d);
269  bool in_relevant_region(char x, char y);
270  char contlistgetX(int i);
271  char contlistgetY(int i);
272  char contlistgetCO(int i);
273 };
274 
275 class VecMC : public std::vector<MovelistCand* > {
276  public:
277  VecMC();
278  ~VecMC();
279  VecMC* deepcopy(ExtendedMoveNumber& COUNTER, int CANDSSIZE);
280  ExtendedMoveNumber counter;
281  int candssize;
282 };
283 
284 
285 
286 
299 class Algo_movelist : public Algorithm {
300  public:
301  Algo_movelist(int bsize, SnapshotVector DATA);
302  ~Algo_movelist();
303  void initialize_process();
304  void newgame_process(int game_id);
305  void AB_process(int x, int y);
306  void AW_process(int x, int y);
307  void AE_process(int x, int y, char removed);
308  void endOfNode_process();
309  void move_process(Move m);
310  void pass_process();
311  void branchpoint_process();
312  void endOfVariation_process();
313  void endgame_process(bool commit=true);
314  void finalize_process();
315  int search(PatternList& patternList, GameList& gl, SearchOptions& options);
316 
317  std::vector<char> movelist;
318  char* fpC;
319  std::map<int, char* > data1;
320  std::map<int, char* > data2;
321  std::map<int, int> data1l;
323 };
324 
325 
326 // --------------------------------------------------------------------------------------------------------
327 
328 
329 
330 class HashhitF { // hashing hit for full board search
331  public:
332  int gameid;
333  char orientation;
334  MoveNC* cont;
335  ExtendedMoveNumber* emn;
336 
337  HashhitF();
338  HashhitF(int GAMEID, char ORIENTATION, ExtendedMoveNumber& EMN, MoveNC* CONT);
339  HashhitF(int GAMEID, char ORIENTATION, char* blob);
340  HashhitF(const HashhitF& HHF);
341  ~HashhitF();
342  HashhitF& operator=(const HashhitF& HHF);
343 
344  char* export_blob();
345 };
346 
347 typedef vector<HashhitF* >* vpsip;
348 
349 class HashhitCS { // hashing hit for corner/side pattern search
350  public:
351  int gameid;
352  int position;
353  bool cs;
354  HashhitCS(int GAMEID, int POSITION, bool CS);
355  bool operator==(const HashhitCS& hhc);
356 };
357 
358 class HashVarInfo {
359  public:
360  hashtype chc;
361  std::vector<std::pair<hashtype, ExtendedMoveNumber> > * lfc;
362  ExtendedMoveNumber* moveNumber;
363  int numStones;
364 
365  HashVarInfo(hashtype CHC, std::vector<std::pair<hashtype, ExtendedMoveNumber> > * LFC, ExtendedMoveNumber* MOVENUMBER, int NUMSTONES);
366 };
367 
369 class Algo_hash_full : public Algorithm {
381  public:
382  Algo_hash_full(int bsize, SnapshotVector DATA, const string OS_DATA_NAME, int MAXNUMSTONES = 50);
383  ~Algo_hash_full();
384  void initialize_process();
385  void newgame_process(int game_id);
386  void AB_process(int x, int y);
387  void AW_process(int x, int y);
388  void AE_process(int x, int y, char removed);
389  void endOfNode_process();
390  void move_process(Move m);
391  void pass_process();
392  void branchpoint_process();
393  void endOfVariation_process();
394  void endgame_process(bool commit=true);
395  void finalize_process();
396  int search(PatternList& patternList, GameList& gl, SearchOptions& options);
397 
398  void process_lfc(int x, int y, char color);
399  hashtype compute_hashkey(Pattern& pattern);
400  int maxNumStones;
401  int numStones;
402 
403  vector<pair<hashtype, int> > data;
404  boost::unordered_multimap<hashtype, HashhitF> data_p;
406  fstream os_data;
407 
408  private:
409  hashtype currentHashCode;
410  ExtendedMoveNumber* moveNumber;
411  std::vector<std::pair<hashtype, ExtendedMoveNumber> > *lfc; // hash code + move number, still *l*ooking *f*or *c*ontinuation
412  std::stack<HashVarInfo>* branchpoints;
413  boost::unordered_multimap<hashtype, HashhitF> hash_vector;
414  void get_HHF(int ptr, vpsip results, int orientation);
415 };
416 
417 
418 // --------------------------------------------------------------------------------------------
419 
420 
422  // When processing sgf games, Algo_hash maintains a list of HashInstance's -
423  // those are regions on the board for which hash codes are put into the
424  // database
425 
426  public:
427  HashInstance(char X, char Y, char SIZEX, char SIZEY, int BOARDSIZE);
428  ~HashInstance();
429  bool inRelevantRegion(char X, char Y);
430 
431  char xx; // position on the board
432  char yy;
433  int pos;
434  int boardsize;
435  char sizeX; // size of the pattern
436  char sizeY;
437  bool changed;
438 
439  void initialize();
440  void finalize();
441  void addB(char x, char y);
442  void removeB(char x, char y);
443  void addW(char x, char y);
444  void removeW(char x, char y);
445  void bppush();
446  void bppop();
447  std::pair<hashtype,int> cHC(); // returns min(currentHashCode) and corresp. index
448  hashtype* currentHashCode; // array of 8 hashtype values (to automatically symmetrize hash codes)
449  std::stack<std::pair<hashtype*,int> >* branchpoints;
450  int numStones;
451 };
452 
454 class Algo_hash : public Algorithm {
455  // This class should not be used by the "end-user" (see Algo_hash_corner and
456  // Algo_hash_sides instead)
457 
458  public:
459  Algo_hash(int bsize, SnapshotVector DATA, string OS_DATA_NAME, int MAXNUMSTONES);
460  virtual ~Algo_hash();
461  virtual void initialize_process();
462  virtual void newgame_process(int game_id);
463  virtual void AB_process(int x, int y);
464  virtual void AW_process(int x, int y);
465  virtual void AE_process(int x, int y, char removed);
466  virtual void endOfNode_process();
467  virtual void move_process(Move m);
468  virtual void pass_process();
469  virtual void branchpoint_process();
470  virtual void endOfVariation_process();
471  virtual void endgame_process(bool commit=true);
472  virtual void finalize_process();
473 
480  virtual int search(PatternList& patternList, GameList& gl, SearchOptions& options);
481 
482  friend class GameList;
483  friend class Algo_hash_full;
484  friend class HashInstance;
485 
486  protected:
487 
488  int maxNumStones;
489  SnapshotVector get_data(); //< Used to read data from disk into \c data
490 
493  virtual void get_HHCS(int ptr, vector<HashhitCS* >* results, bool cs);
494 
495  virtual std::pair<hashtype,std::vector<int> > compute_hashkey(PatternList& pl, int CS);
496  static const hashtype hashCodes[];
497  std::vector<HashInstance>* hi;
498  std::vector<std::pair<hashtype, int> > hash_vector;
499 
500  vector<pair<hashtype, int> > data;
501  boost::unordered_multimap<hashtype, pair<int,int> > data_p;
502  fstream os_data;
503 };
504 
506 class Algo_hash_corner : public Algo_hash {
507  public:
508  Algo_hash_corner(int bsize, SnapshotVector DATA, string OS_DATA_NAME, int SIZE=7, int MAXNUMSTONES = 20);
509 
515  std::pair<hashtype,std::vector<int> > compute_hashkey(PatternList& pl, int CS);
516 
518  int size;
519 };
520 
521 
522 // ---------------------------------------------------------------------------------------------------------------
523 
524 
525 typedef Algorithm* algo_p;
526 
527 
528 #endif
529 
Definition: pattern.h:86
Definition: algos.h:125
Definition: abstractboard.h:49
Definition: algos.h:421
int gid
store the game id during processing
Definition: algos.h:147
Definition: algos.h:299
virtual void pass_process()
Called during processing, for each pass.
Definition: algos.cpp:74
Definition: pattern.h:189
Definition: search.h:97
virtual void endOfNode_process()
Called during processing, after fully processing a node (which might contain several AB...
Definition: algos.cpp:72
int boardsize
board size
Definition: algos.h:148
Definition: algos.h:203
virtual void endOfVariation_process()
Called during processing, when reaching the end of variation ("jump back to most recent branchpoint")...
Definition: algos.cpp:76
virtual void AW_process(int x, int y)
Called during processing, for each AW SGF tag.
Definition: algos.cpp:70
Definition: pattern.h:293
virtual void AB_process(int x, int y)
Called during processing, for each AB SGF tag.
Definition: algos.cpp:69
Definition: sgfparser.h:42
The GameList class is the main interface to the libkombilo functionality.
Definition: search.h:187
Definition: abstractboard.h:61
This algorithm computes the symmetrized Dyer signature of each game.
Definition: algos.h:155
virtual void branchpoint_process()
Called during processing, for each node where a variation starts.
Definition: algos.cpp:75
Definition: algos.h:246
virtual int search(PatternList &patternList, GameList &gl, SearchOptions &options)
pattern search
Definition: algos.cpp:80
virtual void endgame_process(bool commit=true)
Called during processing, when the end of the game is reached.
Definition: algos.cpp:77
int size
size of the region used for hashing (the region is a square with side-length size, located at a corner of the board)
Definition: algos.h:518
Definition: algos.h:330
virtual SnapshotVector get_data()
Extract the relevant data from file at Kombilo startup.
Definition: algos.cpp:79
Base class for hashing for general type patterns (currently works only for corner patterns)...
Definition: algos.h:454
Definition: algos.h:349
virtual void AE_process(int x, int y, char removed)
Called during processing, for each AE SGF tag.
Definition: algos.cpp:71
virtual void move_process(Move m)
Called during processing, for each move (B, W tags)
Definition: algos.cpp:73
Definition: algos.h:275
Definition: algos.h:358
Hashing for full board patterns.
Definition: algos.h:369
virtual void initialize_process()
Called by GameList::start_processing.
Definition: algos.cpp:67
Hashing for corner patterns.
Definition: algos.h:506
virtual void newgame_process(int game_id)
Called when a new game is about to be GameList::process&#39;ed.
Definition: algos.cpp:68
virtual void finalize_process()
Called by GameList::finalize_processing.
Definition: algos.cpp:78