root/06/libkombilo-branches/hash_center/cpptest.cpp

Revision 249, 8.4 kB (checked in by ug, 2 years ago)

Merge -r 246:248 from libkombilo main branch.

Line 
1 #include <fstream>
2 #include <string>
3 #include "boost/filesystem/operations.hpp"
4 #include "search.h"
5
6 using namespace std;
7 using boost::filesystem::directory_iterator;
8
9 int main(int argc, char** argv) {
10   // ----------------- parse command line arguments ---------------------------------
11   int algos = ALGO_FINALPOS | ALGO_MOVELIST | ALGO_HASH_FULL | ALGO_HASH_CORNER | ALGO_HASH_CENTER;
12   bool process = false;
13   for(int i=1; i<argc; i++) {
14     if (!strcmp(argv[i], "-nh")) // disable hashing
15       algos = ALGO_FINALPOS | ALGO_MOVELIST;
16     if (!strcmp(argv[i], "-p")) process = true;
17   }
18
19   // ----------------- set up processing options -----------------------------------
20   ProcessOptions* p_op = new ProcessOptions;
21   p_op->algos = ALGO_FINALPOS | ALGO_MOVELIST | ALGO_HASH_FULL | ALGO_HASH_CORNER | ALGO_HASH_CENTER;
22
23   // ----------------- create GameList instance -----------------------------------
24   GameList gl("t1.db", "id", "[[PW]] - [[PB]] ([[winner]]), [[filename.]], ", p_op);
25   delete p_op;
26
27   // ---------------- process SGF games ---------------------------------------------
28   if (process) { // process sgf's. must be first argument
29     gl.start_processing();
30     directory_iterator end_itr;
31     string path = "/home/ug/go/kombilo/06/hash_center";
32     // string path = "/home/ug/go/gtl/reviews";
33     // string path = "/home/ug/go/gogod06/2001";
34     int counter = 0;
35     for(directory_iterator it(path); it != end_itr; ++it) {
36       string n = it->string();
37       if (n.substr(n.size()-4) == ".sgf") {
38         ifstream infile;
39         // printf("%s\n", n.c_str());
40         infile.open(it->native_file_string().c_str());
41
42         string sgf;
43         string line;
44         while (!infile.eof()) {
45           getline(infile, line);
46           sgf += line + "\n";
47         }
48         infile.close();
49         int flags = CHECK_FOR_DUPLICATES; // |OMIT_DUPLICATES;
50         if (gl.process(sgf.c_str(), path.c_str(), n.c_str(), "", flags))
51           if (gl.process_results() & IS_DUPLICATE) printf("is duplicate: %d\n", counter);
52         counter++;
53       }
54     }
55     gl.finalize_processing();
56     printf("Now %d games in db.\n", gl.size());
57   }
58   printf("%d games.\n", gl.size());
59
60   // ------------------- set up search pattern ----------------------------------------
61
62   // Pattern p(CENTER_PATTERN, 19, 2, 2, ".XXO", "D..F");
63
64   // Pattern p(CENTER_PATTERN, 19, 3, 3, ".X.XXXXOX", vector<MoveNC>());
65   // Pattern p(2,2,4,4, 19, 3, 3, ".X.XXXXOX", vector<MoveNC>()); // "fixed anchor"
66
67   // anchor varies only in small region of board: the first 4 entries
68   // (left, right, top, bottom) describe the rectangle which may contain the top left point of the pattern.
69   // The coordinates range from 0 to boardsize-1
70   // For example, CORNER_NW_PATTERN corresponds to (0,0,0,0)
71   // Pattern p(2,3,4,6, 19, 3, 3, ".X.XXXXOX", vector<MoveNC>());
72
73 //   Pattern p(CORNER_NW_PATTERN,19,8,7,
74 //                 "........"
75 //                 "........"
76 //                 "..X....."
77 //                 "...O...."
78 //                 "........"
79 //                 "........"
80 //                 "........"
81 //                 "........");
82 //   Pattern p(CENTER_PATTERN,19,5,4,
83 //             "...O."
84 //             "....."
85 //             ".XOO."
86 //             "..OXO");
87   Pattern p(CENTER_PATTERN, 19,5, 4,
88             "..XOO"
89             "...XX"
90             "....."
91             "..X..");
92
93   // Pattern p(CORNER_NW_PATTERN,19,7,7,".................X.....X......XO.....OO..........");
94   // Pattern p(CORNER_NW_PATTERN,19,7,7,".......................X.........................");
95
96   // gl.gisearch("pw = 'Hane Naoki'");
97   // Pattern p(CENTER_PATTERN, 19, 3, 5, ".X..OX.OX.OXOXO");
98   // vector<MoveNC> contList;
99   // contList.push_back(MoveNC(6,15,'X'));
100   // contList.push_back(MoveNC(6,13,'O'));
101   // contList.push_back(MoveNC(4,15,'X'));
102   // Pattern p(FULLBOARD_PATTERN, 19, 19, 19, ".....................O.O........OX......XO......X.OXX.XX...X,.OOXXX..OOOX.O....X.OXOOXOO..OX.......XOXXOXXOOXXOO....OX.XXXOOOXO.O.O...OXX..XOX..XO.X.XO...O.......X.....XO..O.,X....,.....XOO................X......X............X....O...........................................O.O...........O.....,.....X...........X.O.X............................................", contList);
103   // Pattern p(FULLBOARD_PATTERN, 19, 19, 19, "..O.O....X...XXXXX.OOXO....OXO.XXOOOXOXXXXOO.OOXO.OXO..O..X.X..OOX,X.XO.O.....XOOOXOXX..XO......X.XOXXX..XXXO........XOX..XXOOXO.OOO.....OOXOXOO.O...XX...X..OXXOO.XOX........O..OX.,..X..X.....X...OX...X..........O....XXXO...XO...X...OOOXOOXX...X....O..OX.O..OX..........OXX....OX..OO..O.OOOOX..O.OX..XX..OOXXXOX.XOOX..X....XXXXXOX...OX.......X.O.XO.............");
104
105   // Pattern p(FULLBOARD_PATTERN,19,19,19,"........................................................................O......................................................................................................................................................................................................................X.........................................................................");
106
107   // -------------------- set up search options ----------------------------------
108   SearchOptions so;
109   so.algos = algos;
110 //   so.fixedColor = true;
111   // so.trustHashFull = true;
112   // SearchOptions so(0,0,50); // use move limit
113   // so.searchInVariations = false;
114   // so.nextMove = 2;
115  
116   // -------------------- do pattern search --------------------------------------
117   gl.search(p, &so);
118
119   // ------------------- print some information about current list of games ------------
120   printf("num games: %d, num hits: %d\n", gl.size(), gl.numHits());
121   // vector<string> res = gl.currentEntriesAsStrings();
122   // for(vector<string>::iterator it = res.begin(); it != res.end(); it++)
123   //   printf("%s\n", it->c_str());
124   for(int i=0; i<gl.size(); i++) printf("%s\n", gl.currentEntryAsString(i).c_str());
125
126   // ------------------- print some statistics ------------------------------------------
127   printf("Search pattern:\n");
128   printf("%s\n", p.printPattern().c_str());
129   printf("Continuations:\n");
130   for(int y=0; y<p.sizeY; y++) {
131     for(int x=0; x<p.sizeX; x++) {
132       printf("%c", gl.lookupLabel(x,y));
133     }
134     printf("\n");
135   }
136   printf("\n");
137   printf("Statistics:\n");
138   printf("num hits: %d, num switched: %d, B wins: %d, W wins: %d\n", gl.num_hits, gl.num_switched, gl.Bwins, gl.Wwins);
139
140   printf("Continuation | Black      ( B wins / W wins ) | White      (B wins / W wins) |\n");
141   for(int y=0; y<p.sizeY; y++) {
142     for(int x=0; x<p.sizeX; x++) {
143       if (gl.lookupLabel(x,y) != '.') {
144         Continuation cont = gl.lookupContinuation(x,y);
145         printf("      %c      |   %3d[%3d] (    %3d /    %3d ) |   %3d[%3d] (   %3d /    %3d) | %1.1f /  %1.1f \n",
146             gl.lookupLabel(x,y), cont.B, cont.tB, cont.wB, cont.lB, cont.W, cont.tW, cont.wW, cont.lW,
147             cont.wW*100.0/cont.W, cont.wB*100.0/cont.B);
148       }
149     }
150   }
151
152   // ------------------- check for duplicates ---------------------------------
153   // gl.reset();
154   // int nd = gl.find_duplicates(19);
155   // printf("duplicates:\n");
156   // for(int i=0; i<nd; i++) {
157   //   // 1st method: retrieve_duplicates_VI
158   //   // vector<int> dupl_vector = gl.retrieve_duplicates_VI(i);
159   //   // for(vector<int>::iterator it = dupl_vector.begin(); it != dupl_vector.end(); it++) {
160   //   //   printf("%s%s\n", gl.currentEntryAsString(*it).c_str(), gl.getSignature(*it).c_str());
161   //   // }
162   //   
163   //   // 2nd method: retrieve_duplicates_PI
164   //   int * dupl_vector = gl.retrieve_duplicates_PI(i);
165   //   int j = 0;
166   //   while(dupl_vector[j] != -1) {
167   //     printf("%s%s\n", gl.currentEntryAsString(dupl_vector[j]).c_str(), gl.getSignature(dupl_vector[j]).c_str());
168   //     j++;
169   //   }
170   //   delete [] dupl_vector;
171
172   //   printf("--------------------------------------------------- \n");
173   // }
174
175   // ------------------- snapshot ---------------------------------------------
176
177   gl.delete_all_snapshots();
178   int handle = gl.snapshot();
179   printf("num games: %d, num hits: %d\n", gl.size(), gl.numHits());
180
181   gl.reset();
182   printf("num games: %d, num hits: %d\n", gl.size(), gl.numHits());
183
184   gl.restore(handle, true);
185   printf("num games: %d, num hits: %d\n", gl.size(), gl.numHits());
186
187   // ------------------- resetFormat ------------------------------------------
188   // printf("reset db\n");
189   // gl.resetFormat("pb");
190   // vector<string> res = gl.currentEntriesAsStrings(0, 40);
191   // for(vector<string>::iterator it = res.begin(); it != res.end(); it++)
192   //   printf("%s\n", it->c_str());
193 }
Note: See TracBrowser for help on using the browser.