Changeset 242
- Timestamp:
- 03/18/07 23:37:15 (1 year ago)
- Files:
-
- 06/libkombilo-branches/hash_center/cpptest.cpp (modified) (3 diffs)
- 06/libkombilo-branches/hash_center/process.py (modified) (1 diff)
- 06/libkombilo-branches/hash_center/search.cpp (modified) (39 diffs)
- 06/libkombilo-branches/hash_center/search.h (modified) (7 diffs)
- 06/libkombilo-branches/hash_center/test_hash_center.py (added)
- 06/libkombilo-branches/hash_center/test_hash_center2.py (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
06/libkombilo-branches/hash_center/cpptest.cpp
r241 r242 31 31 // string path = "/home/ug/go/kombilo/06/hash_center"; 32 32 // string path = "/home/ug/go/gtl/reviews"; 33 string path = "/home/ug/go/gogod06/200 0";33 string path = "/home/ug/go/gogod06/2001"; 34 34 int counter = 0; 35 35 for(directory_iterator it(path); it != end_itr; ++it) { … … 71 71 // Pattern p(2,3,4,6, 19, 3, 3, ".X.XXXXOX", vector<MoveNC>()); 72 72 73 // Pattern p(CORNER_NW_PATTERN,19,8, 8,73 // Pattern p(CORNER_NW_PATTERN,19,8,7, 74 74 // "........" 75 75 // "........" 76 // "...X...." 77 // "........" 76 // "..X....." 78 77 // "...O...." 79 78 // "........" 80 79 // "........" 80 // "........" 81 81 // "........"); 82 Pattern p(CORNER_NW_PATTERN,19,7,8, 83 "......." 84 "......." 85 "...O..." 86 "......." 87 "......." 88 "......." 89 "......." 90 "......."); 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 91 93 // Pattern p(CORNER_NW_PATTERN,19,7,7,".................X.....X......XO.....OO.........."); 92 94 // Pattern p(CORNER_NW_PATTERN,19,7,7,".......................X........................."); … … 120 122 // for(vector<string>::iterator it = res.begin(); it != res.end(); it++) 121 123 // printf("%s\n", it->c_str()); 122 //for(int i=0; i<gl.size(); i++) printf("%s\n", gl.currentEntryAsString(i).c_str());124 for(int i=0; i<gl.size(); i++) printf("%s\n", gl.currentEntryAsString(i).c_str()); 123 125 124 126 // ------------------- print some statistics ------------------------------------------ 06/libkombilo-branches/hash_center/process.py
r241 r242 31 31 counter = 0 32 32 for filename in filenames: 33 # print filename33 print counter 34 34 try: 35 35 file = open(filename) 06/libkombilo-branches/hash_center/search.cpp
r241 r242 418 418 } 419 419 420 Pattern &Pattern::apply_flip(int f, bool CS) {420 Pattern Pattern::apply_flip(int f, bool CS) { 421 421 int newSizeX = max(Pattern::flipsX(f,0,0,sizeX,sizeY), 422 422 Pattern::flipsX(f,sizeX,sizeY,sizeX,sizeY)); … … 468 468 } 469 469 470 Pattern * pNew =new Pattern(newLeft, newRight, newTop, newBottom, boardsize,471 newSizeX, newSizeY, newInitialPos, newContList);472 473 pNew ->flip = f;474 if (CS) pNew ->colorSwitch = 1;475 else pNew ->colorSwitch = 0;470 Pattern pNew(newLeft, newRight, newTop, newBottom, boardsize, 471 newSizeX, newSizeY, newInitialPos, newContList); 472 473 pNew.flip = f; 474 if (CS) pNew.colorSwitch = 1; 475 else pNew.colorSwitch = 0; 476 476 delete [] newInitialPos; 477 return *pNew;477 return pNew; 478 478 } 479 479 … … 602 602 } 603 603 if (foundNewPattern) { 604 printf("new p: %s\n", pNew.printPattern().c_str());604 // printf("new p: %s\n", pNew.printPattern().c_str()); 605 605 flipTable[f] = data.size(); 606 606 data.push_back(pNew); … … 1054 1054 int rc = sqlite3_exec(db, sql, 0, 0, 0); 1055 1055 if (rc != SQLITE_OK) { 1056 printf("error %d\n", rc);1056 // printf("error %d\n", rc); 1057 1057 throw DBError(); 1058 1058 } … … 1723 1723 numOfSwitched += label[2]; 1724 1724 result->push_back(new Hit(new ExtendedMoveNumber((*it)->dictsF), label)); 1725 // printf("hit from %d %d %d\n", (*it)->mx, (*it)->my, (*it)->orientation); 1725 1726 } 1726 1727 … … 2190 2191 2191 2192 HashhitF::HashhitF() { 2192 printf("oops\n");2193 // printf("oops\n"); 2193 2194 cont = 0; 2194 2195 emn = 0; … … 2575 2576 } 2576 2577 2577 HashQuery::HashQuery(std::string SQL, Pattern* p0, int OFF0X, int OFF0Y, int FLIP0, hashtype HASHCODE0, Pattern* p1, int FLIP1) { 2578 HashQuery::HashQuery(std::string SQL, PatternList* PL0, int OFF0X, int OFF0Y, int FLIP0, hashtype HASHCODE0, 2579 PatternList* PL1, int OFF1X, int OFF1Y, int FLIP1) { 2578 2580 sql = SQL; 2579 if (p0) { 2580 printf("use \n%s\n", p0->printPattern().c_str()); 2581 pl0 = new PatternList(*p0,1,0); 2582 printf("pl size %d\n", pl0->size()); 2583 offset0 = OFF0X + p0->sizeX * OFF0Y; 2584 delete p0; 2581 if (PL0) { 2582 pl0 = PL0; 2583 // printf("pl size %d\n", pl0->size()); 2584 offset0 = OFF0X + pl0->data[0].sizeX * OFF0Y; 2585 2585 } else { 2586 2586 pl0 = 0; 2587 2587 offset0 = 0; 2588 2588 } 2589 if (p1) { 2590 pl1 = new PatternList(*p1,1,0); 2591 delete p1; 2589 if (PL1) { 2590 pl1 = PL1; 2592 2591 } else { 2593 2592 pl1 = 0; 2593 offset1 = OFF1X + pl1->data[0].sizeX * OFF1Y; 2594 2594 } 2595 2595 flip0 = FLIP0; … … 2812 2812 // printf("leave algo_hash::initialize_processing\n"); 2813 2813 } 2814 2814 2815 2815 void Algo_hash::newgame_process(int game_id) { 2816 2816 gid = game_id; … … 2821 2821 void Algo_hash::AB_process(int x, int y) { 2822 2822 for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 2823 (*it)->add B(x,y);2823 (*it)->add('B', x, y); 2824 2824 } 2825 2825 2826 2826 void Algo_hash::AW_process(int x, int y) { 2827 2827 for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 2828 (*it)->add W(x,y);2829 } 2828 (*it)->add('W', x, y); 2829 } 2830 2830 2831 2831 void Algo_hash::AE_process(int x, int y, char removed) { 2832 if (removed == 'B') { 2833 for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) (*it)->removeB(x,y); 2834 } else { 2835 for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) (*it)->removeW(x,y); 2836 } 2832 for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) (*it)->remove(removed, x, y); 2837 2833 } 2838 2834 … … 2853 2849 2854 2850 void Algo_hash::move_process(Move m) throw(DBError) { 2855 if (m.color == 'B') { 2856 for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 2857 (*it)->addB(m.x, m.y); 2858 if (m.captures) { 2859 for(vector<p_cc>::iterator cap_it = m.captures->begin(); cap_it != m.captures->end(); cap_it++) { 2860 for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 2861 (*it)->removeW(cap_it->first, cap_it->second); 2862 } 2863 } 2864 } else { 2865 for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 2866 (*it)->addW(m.x, m.y); 2867 if (m.captures) { 2868 for(vector<p_cc>::iterator cap_it = m.captures->begin(); cap_it != m.captures->end(); cap_it++) { 2869 for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 2870 (*it)->removeB(cap_it->first, cap_it->second); 2871 } 2851 for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 2852 (*it)->add(m.color, m.x, m.y); 2853 if (m.captures) { 2854 for(vector<p_cc>::iterator cap_it = m.captures->begin(); cap_it != m.captures->end(); cap_it++) { 2855 for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 2856 (*it)->remove(invertColor(m.color), cap_it->first, cap_it->second); 2872 2857 } 2873 2858 } … … 2945 2930 vector<int>* result = new vector<int>; 2946 2931 bool used[8]; 2947 printf("pft "); for(int i=0; i<8; i++) printf("%d, ", pattern_ft[i]); printf("\n");2948 printf("sft ");2949 for(int i=0; i<8; i++)2950 printf("%d, ", subpattern_ft[i]);2951 printf("\n");2932 // printf("pft "); for(int i=0; i<8; i++) printf("%d, ", pattern_ft[i]); printf("\n"); 2933 // printf("sft "); 2934 // for(int i=0; i<8; i++) 2935 // printf("%d, ", subpattern_ft[i]); 2936 // printf("\n"); 2952 2937 { for(int i=0; i<8; i++) used[i] = false; 2953 2938 } … … 2975 2960 if (gl.start_sorted() == 0) { 2976 2961 // query database 2977 printf("sql query: %s\n", query.sql.c_str());2962 // printf("sql query: %s\n", query.sql.c_str()); 2978 2963 sqlite3_stmt* selStmt = 0; 2979 2964 int rc = sqlite3_prepare(db, query.sql.c_str(), -1, &selStmt, 0); … … 2987 2972 rc = sqlite3_finalize(selStmt); 2988 2973 if (rc != SQLITE_OK) printf("SQLITE errorB %d\n", rc); 2989 printf("%d results\n", results->data.size());2974 // printf("%d results\n", results->data.size()); 2990 2975 2991 2976 // communicate results of query to database … … 3012 2997 if ((*resultIT)->cs) { 3013 2998 subpattern_flipTable = query.pl1->flipTable; 3014 fl = query.flip1;2999 fl = Pattern::PatternInvFlip(query.flip1); 3015 3000 subp_sizeX = query.pl1->pattern.sizeX; 3016 3001 subp_sizeY = query.pl1->pattern.sizeY; 3017 3002 } else { 3018 3003 subpattern_flipTable = query.pl0->flipTable; 3019 fl = query.flip0;3004 fl = Pattern::PatternInvFlip(query.flip0); 3020 3005 subp_sizeX = query.pl0->pattern.sizeX; 3021 3006 subp_sizeY = query.pl0->pattern.sizeY; … … 3037 3022 if (!(*resultIT)->cs) index = patternList.flipTable[*flip]; 3038 3023 else index = patternList.flipTable[*flip + 8]; 3039 // printf(" make cand %d, %d, %d\n", pos_left, pos_top, index);3024 // printf("%d %d %d %d make cand %d, %d, %d\n", pos, ori, fl, *flip, pos_left, pos_top, index); 3040 3025 candidates->push_back(new Candidate(pos_left, pos_top, index)); 3041 3026 } … … 3090 3075 char p = pattern->finalPos[i + pattern->sizeX*j]; 3091 3076 if (p == 'x' || p == 'o' || p == '*') { 3092 printf("c_h 0a\n");3077 // printf("c_h 0a\n"); 3093 3078 return CompleteHashKey(); 3094 3079 } … … 3127 3112 3128 3113 HashQuery Algo_hash_corner::searchQuery(PatternList& patternList) { 3129 printf("Enter Algo_hash_corner::search\n");3114 // printf("Enter Algo_hash_corner::searchQuery\n"); 3130 3115 if (patternList.data[0].sizeX < size ||\ 3131 3116 patternList.data[0].sizeY < size ||\ … … 3134 3119 (patternList.data[0].left != 0 && patternList.data[0].left != boardsize-size) ||\ 3135 3120 (patternList.data[0].top != 0 && patternList.data[0].top != boardsize-size)) { 3136 printf("c_h 0\n");3121 // printf("c_h 0\n"); 3137 3122 return HashQuery(); 3138 3123 } … … 3145 3130 3146 3131 Pattern subp = pattern->subpattern(offsetX, offsetY, size, size); 3147 PatternList sub_pl(subp, 0, 0);3148 CompleteHashKey chk0 = compute_hashkey( sub_pl, 0);3132 PatternList* sub_pl = new PatternList(subp, 0, 0); 3133 CompleteHashKey chk0 = compute_hashkey(*sub_pl, 0); 3149 3134 if (chk0.hashCode == NOT_HASHABLE) { 3150 printf("0 NH\n");3135 // printf("0 NH\n"); 3151 3136 return HashQuery(); 3152 3137 } 3153 int fl = patternList.data[chk0.index].flip; 3154 3155 char buf[100]; 3156 sprintf(buf, "select data,hash from algo_hash_%d_%s where hash ", 3157 boardsize, dbnameext.c_str()); 3138 int fl = sub_pl->data[chk0.index].flip; 3139 3140 char buf[200]; 3141 sprintf(buf, "select data,hash from algo_hash_%d_%s where hash ", boardsize, dbnameext.c_str()); 3158 3142 string sql = buf; 3159 Pattern* p0 = new Pattern(sub_pl.data[0]); 3160 3143 3144 PatternList* sub_pl1; 3161 3145 int fl2 = fl; 3162 Pattern* p1 = 0;3163 3146 if (patternList.data[patternList.size()-1].colorSwitch) { 3164 CompleteHashKey chk1 = compute_hashkey( sub_pl, 1);3147 CompleteHashKey chk1 = compute_hashkey(*sub_pl, 1); 3165 3148 // printf("HC2 %lld\n", hashCode2); 3166 3149 if (chk1.hashCode == NOT_HASHABLE) { 3167 printf("1 NH\n");3150 // printf("1 NH\n"); 3168 3151 return HashQuery(); 3169 3152 } … … 3173 3156 sprintf(buf, "in ('%lld', '%lld')", chk0.hashCode, chk1.hashCode); 3174 3157 sql += buf; 3175 p1 = new Pattern(sub_pl.data[sub_pl.size()/2]);3158 sub_pl1 = new PatternList(sub_pl->data[sub_pl->size()/2], 1, 0); 3176 3159 } else { 3177 3160 sprintf(buf, "= '%lld'", chk0.hashCode); … … 3182 3165 sql += buf; 3183 3166 } 3184 return HashQuery(sql, p0, offsetX, offsetY, fl, chk0.hashCode, p1, fl2);3167 return HashQuery(sql, sub_pl, offsetX, offsetY, fl, chk0.hashCode, sub_pl1, offsetX, offsetY, fl2); 3185 3168 } 3186 3169 … … 3205 3188 3206 3189 3207 Algo_hash_center::Algo_hash_center(int bsize) : Algo_hash(bsize, "CENTER", 16) { 3190 Algo_hash_center::Algo_hash_center(int bsize, int L_BOUND, int U_BOUND) : Algo_hash(bsize, "CENTER", 16) { 3191 l_bound = L_BOUND; 3192 u_bound = U_BOUND; 3208 3193 // read "frequency" table from kombilo.db 3209 3194 sqlite3* kombilodb = 0; … … 3225 3210 rc = sqlite3_close(kombilodb); 3226 3211 if (rc != SQLITE_OK) printf("SQLITE errorD %d\n", rc); 3227 } 3228 3212 sizeX = 4; 3213 sizeY = 4; 3214 } 3215 3229 3216 void Algo_hash_center::endOfNode_process() { 3230 3217 for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) { … … 3233 3220 pair<hashtype,int> phi = (*it)->cHC(); 3234 3221 map<hashtype, int>::iterator freq_it = frequency.find(phi.first); 3235 if (freq_it != frequency.end() && freq_it->second > 100 && freq_it->second < 1000)3222 if (freq_it != frequency.end() && freq_it->second > l_bound && freq_it->second < u_bound) 3236 3223 if (insert_hash(phi.first, phi.second) != 0) throw DBError(); 3237 3224 } … … 3242 3229 Algo_hash::initialize_process(DB); 3243 3230 hi = new vector<HashInstance* >; 3244 for(int i=0; i<boardsize-3; i++) // FIXME: boundaries 3245 for(int j=0; j<boardsize-3; j++) 3246 hi->push_back(new HashInstanceCTR(i, j, 4, 4, boardsize)); 3247 } 3248 3249 hashtype center_hashkey(char* p, int sizeX, int start) { 3250 // computes the hash key of the 4x4 pattern in p 3251 hashtype currentHashCode[8]; 3252 for(int f=0; f<8; f++) { 3253 for(int x=0; x<4; x++) { 3254 for(int y=0; y<4; y++) { 3255 char x1 = Pattern::flipsX(f, x, y, 3, 3); 3256 char y1 = Pattern::flipsY(f, x, y, 3, 3); 3257 char c = p[start + x1*sizeX + y1]; 3258 // stone at position (x1, y1) at bits 4*x1+yy1 4*x1+y1+1 3259 int p = 2*(4*x1+y1); 3260 if (c=='X') 3261 currentHashCode[f] |= 1 << p; // set to 01 = black 3262 else if (c == 'W') 3263 currentHashCode[f] |= 1 << (p+1); // set to 10 = white 3264 else if (c != '.') return NOT_HASHABLE; 3231 for(int i=0; i<boardsize-sizeX+1; i++) // FIXME: boundaries 3232 for(int j=0; j<boardsize-sizeY+1; j++) 3233 hi->push_back(new HashInstanceCTR(i, j, sizeX, sizeY, boardsize)); 3234 } 3235 3236 HashQuery Algo_hash_center::searchQuery(PatternList& patternList) { 3237 // printf("Enter Algo_hash_center::search\n"); 3238 Pattern* pattern = &(patternList.data[0]); 3239 int offsetX = 0; 3240 int offsetY = 0; 3241 int fl = 0; 3242 int freq = 1000000; 3243 hashtype hashCode = NOT_HASHABLE; 3244 PatternList* sub_pl = 0; 3245 PatternList* pl0 = 0; 3246 for(int oX = 0; oX <= pattern->sizeX - sizeX; oX++) { 3247 for(int oY = 0; oY <= pattern->sizeY - sizeY; oY++) { 3248 Pattern* subp = &pattern->subpattern(oX, oY, sizeX, sizeY); 3249 sub_pl = new PatternList(*subp, 1, 0); 3250 CompleteHashKey hc = HashInstanceCTR::center_hashkey(*sub_pl, 0); 3251 map<hashtype, int>::iterator f_it = frequency.find(hc.hashCode); 3252 if (hc.hashCode != NOT_HASHABLE && f_it != frequency.end() && f_it->second > l_bound && f_it->second < u_bound && f_it->second < freq) { 3253 hashCode = hc.hashCode; 3254 freq = f_it->second; 3255 offsetX = oX; 3256 offsetY = oY; 3257 if (pl0) delete pl0; 3258 pl0 = sub_pl; 3259 fl = sub_pl->data[hc.index].flip; 3260 } else { 3261 delete sub_pl; 3265 3262 } 3266 } 3267 } 3268 hashtype minCHC = currentHashCode[0]; 3269 for(int f=1; f<8; f++) { 3270 if (currentHashCode[f] < minCHC) { 3271 minCHC = currentHashCode[f]; 3272 } 3273 } 3274 return minCHC; 3275 } 3276 3277 CompleteHashKey Algo_hash_center::compute_hashkey(PatternList& pl, int CS) { 3278 if (pl.pattern.sizeX < 4 || pl.pattern.sizeY < 4 || 3279 (pl.pattern.sizeX < 5 && (pl.pattern.left == 0 || pl.pattern.right == boardsize-1)) || 3280 (pl.pattern.sizeY < 5 && (pl.pattern.top == 0 || pl.pattern.bottom == boardsize-1))) 3281 return CompleteHashKey(); // NOT_HASHABLE 3282 3283 // go through all center 4x4 sub-patterns, and compute the corresp. hash key 3284 3285 int X1 = 0; 3286 int Y1 = 0; 3287 int X2 = pl.pattern.sizeX - 4; 3288 int Y2 = pl.pattern.sizeY - 4; 3289 3290 // if (pl.pattern.left == 0) X1++; 3291 // if (pl.pattern.top == 0) Y1++; 3292 // if (pl.pattern.right == boardsize - pl.pattern.sizeX) X2--; 3293 // if (pl.pattern.bottom == boardsize - pl.pattern.sizeY) Y2--; 3294 3295 // 3296 return CompleteHashKey(); // NOT_HASHABLE 3297 } 3298 3299 HashQuery Algo_hash_center::searchQuery(PatternList& patternList) { 3300 return HashQuery(); 3263 delete subp; 3264 } 3265 } 3266 3267 if (hashCode == NOT_HASHABLE) { 3268 return HashQuery(); 3269 } 3270 3271 char buf[200]; 3272 sprintf(buf, "select data,hash from algo_hash_%d_%s where hash ", boardsize, dbnameext.c_str()); 3273 string sql = buf; 3274 3275 int offsetX1 = 0; 3276 int offsetY1 = 0; 3277 freq = 1000000; 3278 hashtype hashCode1 = NOT_HASHABLE; 3279 PatternList* sub_pl1; 3280 PatternList* pl1 = 0; 3281 3282 int fl1 = fl; 3283 if (patternList.data[patternList.size()-1].colorSwitch) { 3284 for(int oX = 0; oX <= pattern->sizeX - sizeX; oX++) { 3285 for(int oY = 0; oY <= pattern->sizeY - sizeY; oY++) { 3286 Pattern* subp = &patternList.data[patternList.size()/2].subpattern(oX, oY, sizeX, sizeY); 3287 sub_pl1 = new PatternList(*subp, 0, 0); 3288 CompleteHashKey hc = HashInstanceCTR::center_hashkey(*sub_pl1, 0); 3289 map<hashtype, int>::iterator f_it = frequency.find(hc.hashCode); 3290 if (hc.hashCode != NOT_HASHABLE && f_it != frequency.end() && f_it->second > l_bound && f_it->second < u_bound && f_it->second < freq) { 3291 hashCode1 = hc.hashCode; 3292 freq = f_it->second; 3293 offsetX1 = oX; 3294 offsetY1 = oY; 3295 if (pl1) delete pl1; 3296 pl1 = sub_pl1; 3297 fl1 = sub_pl1->data[hc.index].flip; 3298 } else { 3299 delete sub_pl1; 3300 } 3301 delete subp; 3302 } 3303 } 3304 3305 if (hashCode1 == NOT_HASHABLE) { 3306 // printf("1 NH\n"); 3307 return HashQuery(); 3308 } 3309 3310 if (hashCode != hashCode1) { 3311 sprintf(buf, "in ('%lld', '%lld')", hashCode, hashCode1); 3312 sql += buf; 3313 } else { 3314 sprintf(buf, "= '%lld'", hashCode); 3315 sql += buf; 3316 } 3317 } else { 3318 sprintf(buf, "= '%lld'", hashCode); 3319 sql += buf; 3320 } 3321 return HashQuery(sql, pl0, offsetX, offsetY, fl, hashCode, pl1, offsetX1, offsetY1, fl1); 3301 3322 } 3302 3323 … … 3347 3368 } 3348 3369 3349 void HashInstance::addB(char x, char y) { printf("ouch\n"); } 3350 3351 void HashInstance::addW(char x, char y) { } 3352 3353 void HashInstance::removeB(char x, char y) { } 3354 3355 void HashInstance::removeW(char x, char y) { } 3370 void HashInstance::add(char co, char x, char y) { printf("ouch\n"); } 3371 3372 void HashInstance::remove(char co, char x, char y) { } 3356 3373 3357 3374 pair<hashtype,int> HashInstance::cHC() { … … 3387 3404 } 3388 3405 3389 void HashInstanceCO::add B(char x, char y) {3406 void HashInstanceCO::add(char co, char x, char y) { 3390 3407 if (inRelevantRegion(x,y)) { 3391 3408 changed = true; 3392 3409 for(int i=0; i<8; i++) { 3393 currentHashCode[i] += Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 3410 if (co == 'X' || co =='B') 3411 currentHashCode[i] += Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 3412 else if (co == 'O' || co == 'W') 3413 currentHashCode[i] -= Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 3414 else printf("ouch\n"); // FIXME 3394 3415 } 3395 3416 numStones++; … … 3397 3418 } 3398 3419 3399 void HashInstanceCO:: addW(char x, char y) {3420 void HashInstanceCO::remove(char co, char x, char y) { 3400 3421 if (inRelevantRegion(x,y)) { 3401 3422 changed = true; 3402 3423 for(int i=0; i<8; i++) { 3403 currentHashCode[i] -= Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 3424 if (co == 'X' || co =='B') 3425 currentHashCode[i] -= Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 3426 else if (co == 'O' || co == 'W') 3427 currentHashCode[i] += Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 3428 else printf("ouch\n"); // FIXME 3404 3429 } 3405 3430 numStones++; … … 3407 3432 } 3408 3433 3409 void HashInstanceCO::removeB(char x, char y) { 3434 HashInstanceCTR::HashInstanceCTR(char X, char Y, char SIZEX, char SIZEY, int BOARDSIZE) : HashInstance(X, Y, SIZEX, SIZEY, BOARDSIZE) { 3435 if (sizeX != 4 || sizeY != 4) printf("error\n"); // FIXME 3436 } 3437 3438 HashInstanceCTR::~HashInstanceCTR() { 3439 finalize(); 3440 } 3441 3442 void HashInstanceCTR::add(char co, char x, char y) { 3410 3443 if (inRelevantRegion(x,y)) { 3411 3444 changed = true; 3412 3445 for(int i=0; i<8; i++) { 3413 currentHashCode[i] -= Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 3414 } 3415 numStones++; 3416 } 3417 } 3418 3419 void HashInstanceCO::removeW(char x, char y) { 3420 if (inRelevantRegion(x,y)) { 3421 changed = true; 3422 for(int i=0; i<8; i++) { 3423 currentHashCode[i] += Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 3424 } 3425 numStones++; 3426 } 3427 } 3428 3429 3430 HashInstanceCTR::HashInstanceCTR(char X, char Y, char SIZEX, char SIZEY, int BOARDSIZE) : HashInstance(X, Y, SIZEX, SIZEY, BOARDSIZE) { 3431 } 3432 3433 HashInstanceCTR::~HashInstanceCTR() { 3434 finalize(); 3435 } 3436 3437 void HashInstanceCTR::addB(char x, char y) { 3438 if (inRelevantRegion(x,y)) { 3439 changed = true; 3440 for(int i=0; i<8; i++) { 3446 // char x1 = Pattern::flipsX(Pattern::PatternInvFlip(i), x-xx, y-yy, 3, 3); 3447 // char y1 = Pattern::flipsY(Pattern::PatternInvFlip(i), x-xx, y-yy, 3, 3); 3441 3448 char x1 = Pattern::flipsX(i, x-xx, y-yy, 3, 3); 3442 3449 char y1 = Pattern::flipsY(i, x-xx, y-yy, 3, 3); … … 3444 3451 int p = 2*(4*x1+y1); 3445 3452 currentHashCode[i] ^= ((currentHashCode[i] >> p)%4) << p; // set to 00 3446 currentHashCode[i] |= 1 << p; // set to 01 = black 3447 } 3448 } 3449 } 3450 3451 void HashInstanceCTR::addW(char x, char y) { 3453 if (co == 'X' || co == 'B') 3454 currentHashCode[i] |= 1 << p; // set to 01 = black 3455 else if (co == 'O' || co == 'W') 3456 currentHashCode[i] |= 1 << (p+1); // set to 10 = white 3457 else printf("ouch\n"); // FIXME 3458 } 3459 } 3460 } 3461 3462 void HashInstanceCTR::remove(char co, char x, char y) { 3452 3463 if (inRelevantRegion(x,y)) { 3453 3464 changed = true; 3454 3465 for(int i=0; i<8; i++) { 3466 // char x1 = Pattern::flipsX(Pattern::PatternInvFlip(i), x-xx, y-yy, 3, 3); 3467 // char y1 = Pattern::flipsY(Pattern::PatternInvFlip(i), x-xx, y-yy, 3, 3); 3455 3468 char x1 = Pattern::flipsX(i, x-xx, y-yy, 3, 3); 3456 3469 char y1 = Pattern::flipsY(i, x-xx, y-yy, 3, 3); … … 3458 3471 int p = 2*(4*x1+y1); 3459 3472 currentHashCode[i] ^= ((currentHashCode[i] >> p)%4) << p; // set to 00 3460 currentHashCode[i] |= 1 << (p+1); // set to 10 = white 3461 } 3462 } 3463 } 3464 3465 void HashInstanceCTR::removeB(char x, char y) { 3466 if (inRelevantRegion(x,y)) { 3467 changed = true; 3468 for(int i=0; i<8; i++) { 3469 char x1 = Pattern::flipsX(i, x-xx, y-yy, 3, 3); 3470 char y1 = Pattern::flipsY(i, x-xx, y-yy, 3, 3); 3471 // stone at position (x1, y1) at bits 4*x1+yy1 4*x1+y1+1 3472 int p = 2*(4*x1+y1); 3473 currentHashCode[i] ^= ((currentHashCode[i] >> p)%4) << p; // set to 00 3474 } 3475 } 3476 } 3477 3478 void HashInstanceCTR::removeW(char x, char y) { 3479 if (inRelevantRegion(x,y)) { 3480 changed = true; 3481 for(int i=0; i<8; i++) { 3482 char x1 = Pattern::flipsX(i, x-xx, y-yy, 3, 3); 3483 char y1 = Pattern::flipsY(i, x-xx, y-yy, 3, 3); 3484 // stone at position (x1, y1) at bits 4*x1+yy1 4*x1+y1+1 3485 int p = 2*(4*x1+y1); 3486 currentHashCode[i] ^= ((currentHashCode[i] >> p)%4) << p; // set to 00 3487 } 3488 } 3489 } 3490 3473 } 3474 } 3475 } 3476 3477 CompleteHashKey HashInstanceCTR::center_hashkey(PatternList& pl, int CS) { 3478 // computes the hash key of the (sizeX x sizeY) pattern in p 3479 3480 hashtype hk = NOT_HASHABLE; 3481 int f = 0; 3482 // printf("before loop %d %d\n", pl.size(), CS); 3483 for(int pCtr=0; pCtr<pl.size(); pCtr++) { 3484 if (CS == pl.data[pCtr].colorSwitch) { 3485 // printf("loop %d\n", pCtr); 3486 hashtype hashkey = 0; 3487 3488 Pattern *pattern = & pl.data[pCtr]; 3489 3490 for(int i=0; i<4; i++) { 3491 for(int j=0; j<4; j++) { 3492 char c = pattern->finalPos[i + pattern->sizeX*j]; 3493 int p = 2*(4*i+j); 3494 if (c=='X') 3495 hashkey |= 1 << p; // set to 01 = black 3496 else if (c == 'O') 3497 hashkey |= 1 << (p+1); // set to 10 = white 3498 else if (c == 'x' || c == 'o' || c == '*') return CompleteHashKey(); 3499 } 3500 } 3501 if (hk==NOT_HASHABLE || hashkey<hk) { 3502 hk = hashkey; 3503 f = pCtr; 3504 } 3505 } 3506 } 3507 return CompleteHashKey(hk, f); 3508 } 3491 3509 3492 3510 // UIntervals::UIntervals(intervs = []) { … … 4285 4303 // algo_ps[algo_hash_side] = new Algo_hash_side(boardsize, 6, 4, p_op->algo_hash_side_maxNumStones); 4286 4304 if (p_op->algos & ALGO_HASH_CENTER) { 4287 algo_ps[algo_hash_center+20*ctr] = new Algo_hash_center(bs );4305 algo_ps[algo_hash_center+20*ctr] = new Algo_hash_center(bs, 100, 1000); 4288 4306 algo_dbs[algo_hash_center+20*ctr] = algo_db2; 4289 4307 } … … 4315 4333 rc = sqlite3_exec(db, sql.c_str(), insertEntry, this, 0); 4316 4334 if (rc != SQLITE_OK && rc != SQLITE_ERROR) { 4317 printf("sql error %d\n", rc);4335 // printf("sql error %d\n", rc); 4318 4336 throw DBError(); 4319 4337 } … … 4421 4439 4422 4440 void GameList::makeCurrentCandidate(vector<Candidate* > * candidates) { 4423 printf("GameList::makeCurrentCandidate\n");4441 // printf("GameList::makeCurrentCandidate\n"); 4424 4442 GameListEntry* gle = (*all)[(*oldList)[current].second]; 4425 4443 if (gle->candidates) delete gle->candidates; 4426 4444 gle->candidates = candidates; 4427 for(vector<Candidate* >::iterator it = candidates->begin(); it != candidates->end(); it++) {4428 printf("x: %d, y: %d, flip: %d\n", (*it)->x, (*it)->y, (*it)->orientation);4429 }4445 // for(vector<Candidate* >::iterator it = candidates->begin(); it != candidates->end(); it++) { 4446 // printf("x: %d, y: %d, flip: %d\n", (*it)->x, (*it)->y, (*it)->orientation); 4447 // } 4430 4448 currentList->push_back((*oldList)[current]); 4431 4449 } … … 4828 4846 sizeX = pattern.sizeX; // need this in lookupLabel 4829 4847 PatternList pl(pattern, searchOptions->fixedColor, searchOptions->nextMove); 4830 printf("created pl, size: %d\n", pl.size());4848 // printf("created pl, size: %d\n", pl.size()); 4831 4849 // for(int i=0; i< pl.size(); i++) printf("%s\n", pl.data[i].printPattern().c_str()); 4832 4850 … … 4873 4891 if (hash_result == 0) { 4874 4892 if (searchOptions->algos & ALGO_MOVELIST && algo_ps[algo_movelist+20*bs_index]) 4875 printf("movelist 0\n");4893 // printf("movelist 0\n"); 4876 4894 algo_ps[algo_movelist+20*bs_index]->search(pl, *this, *searchOptions); 4877 4895 } … … 4879 4897 4880 4898 if (hash_result == -1) { 4881 if (searchOptions->algos & ALGO_FINALPOS && algo_ps[algo_finalpos+20*bs_index]) 4882 algo_ps[algo_finalpos+20*bs_index]->search(pl, *this, *searchOptions); 4883 printf("movelist 1\n"); 4884 if (searchOptions->algos & ALGO_MOVELIST && algo_ps[algo_movelist+20*bs_index]) 4885 algo_ps[algo_movelist+20*bs_index]->search(pl, *this, *searchOptions); 4899 // USE CENTER HASHING? 4900 if ((searchOptions->algos & ALGO_HASH_CENTER) && algo_ps[algo_hash_center+20*bs_index]) { 4901 hash_result = ((Algo_hash_center*)algo_ps[algo_hash_center+20*bs_index])->search(pl, *this, *searchOptions, algo_dbs[algo_hash_center+20*bs_index]); 4902 if (hash_result == 0) { 4903 // printf("movelist after center hash\n"); 4904 if (searchOptions->algos & ALGO_MOVELIST && algo_ps[algo_movelist+20*bs_index]) 4905 algo_ps[algo_movelist+20*bs_index]->search(pl, *this, *searchOptions); 4906 } 4907 } 4908 if (hash_result == -1) { 4909 if (searchOptions->algos & ALGO_FINALPOS && algo_ps[algo_finalpos+20*bs_index]) 4910 algo_ps[algo_finalpos+20*bs_index]->search(pl, *this, *searchOptions); 4911 // printf("movelist\n"); 4912 if (searchOptions->algos & ALGO_MOVELIST && algo_ps[algo_movelist+20*bs_index]) 4913 algo_ps[algo_movelist+20*bs_index]->search(pl, *this, *searchOptions); 4914 } 4886 4915 } 4887 4916 } … … 5069 5098 int pos = 0; 5070 5099 while (root) { 5071 printf("current: %d\n", current);5100 // printf("current: %d\n", current); 5072 5101 current++; 5073 5102 int return_val = 0; 5074 if (algo_db2 && !(current%5000)) {5075 int rc = sqlite3_exec(algo_db2, "commit", 0, 0, 0);5076 if (rc) {5077 sqlite3_close(algo_db2);5078 algo_db2 = 0;5079 throw DBError();5080 }5081 rc = sqlite3_exec(algo_db2, "begin transaction", 0, 0, 0);5082 if (rc) {5083 sqlite3_close(algo_db2);5084 algo_db2 = 0;5085 throw DBError();5086 }5087 }5103 // if (algo_db2 && !(current%5000)) { 5104 // int rc = sqlite3_exec(algo_db2, "commit", 0, 0, 0); 5105 // if (rc) { 5106 // sqlite3_close(algo_db2); 5107 // algo_db2 = 0; 5108 // throw DBError(); 5109 // } 5110 // rc = sqlite3_exec(algo_db2, "begin transaction", 0, 0, 0); 5111 // if (rc) { 5112 // sqlite3_close(algo_db2); 5113 // algo_db2 = 0; 5114 // throw DBError(); 5115 // } 5116 // } 5088 5117 vector<string>* rootNodeProperties = parseRootNode(root, SGFtags); 5089 5118 // for(vector<string>::iterator rnp = rootNodeProperties->begin(); rnp != rootNodeProperties->end(); rnp++) … … 5193 5222 int rc = sqlite3_prepare(db, sql_ins_rnp.c_str(), -1, &ppStmt, 0); 5194 5223 if (rc != SQLITE_OK || ppStmt==0) { 5195 printf("db error %d\n", rc);5224 // printf("db error %d\n", rc); 5196 5225 throw DBError(); // FIXME: catch busy error, (and/or throw DBError?) 5197 5226 } 06/libkombilo-branches/hash_center/search.h
r241 r242 121 121 Pattern& copy(const Pattern& p); 122 122 123 Pattern &apply_flip(int f, bool CS = false);123 Pattern apply_flip(int f, bool CS = false); 124 124 Pattern& subpattern(int offsetX, int offsetY, int sizeX, int sizeY); 125 125 char getInitial(int i, int j); … … 450 450 virtual void initialize(); 451 451 virtual void finalize(); 452 virtual void addB(char x, char y); 453 virtual void removeB(char x, char y); 454 virtual void addW(char x, char y); 455 virtual void removeW(char x, char y); 452 virtual void add(char co, char x, char y); 453 virtual void remove(char co, char x, char y); 456 454 virtual void bppush(); 457 455 virtual void bppop(); … … 467 465 virtual ~HashInstanceCO(); 468 466 469 void add B(char x, char y);470 void remove B(char x, char y);471 void addW(char x, char y);472 void removeW(char x, char y); 473 };467 void add(char co, char x, char y); 468 void remove(char co, char x, char y); 469 }; 470 471 class CompleteHashKey; 474 472 475 473 class HashInstanceCTR : public HashInstance { … … 478 476 virtual ~HashInstanceCTR(); 479 477 480 void add B(char x, char y);481 void remove B(char x, char y);482 void addW(char x, char y); 483 void removeW(char x, char y);478 void add(char co, char x, char y); 479 void remove(char co, char x, char y); 480 481 static CompleteHashKey center_hashkey(PatternList& pl, int CS); 484 482 }; 485 483 … … 505 503 int flip1; 506 504 int offset0; 505 int offset1; 507 506 hashtype hashCode0; 508 507 509 508 HashQuery(); 510 HashQuery(std::string SQL, Pattern* p0, int OFF0X, int OFF0Y, int FLIP0, hashtype HASHCODE0, Pattern* p1, int FLIP1); 509 HashQuery(std::string SQL, 510 PatternList* PL0, int OFF0X, int OFF0Y, int FLIP0, hashtype HASHCODE0, 511 PatternList* PL1, int OFF1X, int OFF1Y, int FLIP1); 511 512 ~HashQuery(); 512 513 int get_offX(); … … 583 584 class Algo_hash_center : public Algo_hash { 584 585 public: 585 Algo_hash_center(int bsize );586 Algo_hash_center(int bsize, int L_BOUND, int U_BOUND); 586 587 void initialize_process(sqlite3* DB) throw(DBError); 587 588 void endOfNode_process(); 588 CompleteHashKey compute_hashkey(PatternList& pl, int CS);589 589 HashQuery searchQuery(PatternList& patternList); 590 590 … … 592 592 private: 593 593 std::map<hashtype, int> frequency; 594 int sizeX; 595 int sizeY; 596 int l_bound; 597 int u_bound; 594 598 }; 595 599
