Changeset 233

Show
Ignore:
Timestamp:
03/02/07 16:16:43 (1 year ago)
Author:
ug
Message:

Basic functions for hashing of center patterns (no search capability yet).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • 06/libkombilo-branches/hash_center/cpptest.cpp

    r230 r233  
    1919  // ----------------- set up processing options ----------------------------------- 
    2020  ProcessOptions* p_op = new ProcessOptions; 
    21   p_op->algos = ALGO_FINALPOS | ALGO_MOVELIST
     21  p_op->algos = ALGO_FINALPOS | ALGO_MOVELIST | ALGO_HASH_CENTER
    2222 
    2323  // ----------------- create GameList instance ----------------------------------- 
    24   GameList gl("t1.db", "id", "[[PW]] - [[PB]] ([[winner]]), [[filename.]], ", 0); 
     24  GameList gl("t1.db", "id", "[[PW]] - [[PB]] ([[winner]]), [[filename.]], ", p_op); 
    2525  delete p_op; 
    2626 
     
    2929    gl.start_processing(); 
    3030    directory_iterator end_itr; 
    31     string path = "/home/ug/go/kombilo/06/libkombilo"; 
     31    // string path = "/home/ug/go/kombilo/06/center_hashes/libkombilo"; 
    3232    // string path = "/home/ug/go/gtl/reviews"; 
    33     // string path = "/home/ug/go/gogod06/2000"; 
     33    string path = "/home/ug/go/gogod06/2000"; 
    3434    for(directory_iterator it(path); it != end_itr; ++it) { 
    3535      string n = it->string(); 
  • 06/libkombilo-branches/hash_center/process.py

    r219 r233  
    2121    try: 
    2222        pop = ProcessOptions() 
    23         # pop.algos = 0 
     23        pop.algos = ALGO_FINALPOS | ALGO_MOVELIST | ALGO_HASH_CENTER 
    2424        # pop.rootNodeTags = 'PW,PB,RE,DT' 
    2525        pop.sgfInDB = False 
     
    4646 
    4747# filelist = glob.glob('./*.sgf') 
    48 filelist = glob.glob('/home/ug/go/gogod06/*/*.sgf') 
     48# filelist = glob.glob('/home/ug/go/gogod06/*/*.sgf') 
     49filelist = glob.glob('/home/ug/go/KGS/*/*.sgf') 
    4950# filelist = glob.glob('/home/ug/go/KGS2005/*.sgf') 
    5051 
  • 06/libkombilo-branches/hash_center/search.cpp

    r230 r233  
    24162416 
    24172417Algo_hash::~Algo_hash() { 
    2418   if (hi) delete hi; 
     2418  if (hi) { 
     2419    for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 
     2420      delete *it; 
     2421    delete hi; 
     2422    hi = 0; 
     2423  } 
    24192424} 
    24202425 
     
    24542459void Algo_hash::newgame_process(int game_id) { 
    24552460  gid = game_id; 
    2456   for(vector<HashInstance>::iterator it = hi->begin(); it != hi->end(); it++) 
    2457     it->initialize(); 
     2461  for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 
     2462    (*it)->initialize(); 
    24582463} 
    24592464 
    24602465void Algo_hash::AB_process(int x, int y) { 
    2461   for(vector<HashInstance>::iterator it = hi->begin(); it != hi->end(); it++) 
    2462     it->addB(x,y); 
     2466  for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 
     2467    (*it)->addB(x,y); 
    24632468} 
    24642469 
    24652470void Algo_hash::AW_process(int x, int y) { 
    2466   for(vector<HashInstance>::iterator it = hi->begin(); it != hi->end(); it++) 
    2467     it->addW(x,y); 
     2471  for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 
     2472    (*it)->addW(x,y); 
    24682473}                  
    24692474 
    24702475void Algo_hash::AE_process(int x, int y, char removed) { 
    24712476  if (removed == 'B') { 
    2472     for(vector<HashInstance>::iterator it = hi->begin(); it != hi->end(); it++) it->removeB(x,y); 
     2477    for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) (*it)->removeB(x,y); 
    24732478  } else {  
    2474     for(vector<HashInstance>::iterator it = hi->begin(); it != hi->end(); it++) it->removeW(x,y); 
     2479    for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) (*it)->removeW(x,y); 
    24752480  } 
    24762481} 
    24772482 
    24782483void Algo_hash::endOfNode_process() { 
    2479   for(vector<HashInstance>::iterator it = hi->begin(); it != hi->end(); it++) { 
    2480     if (it->numStones <= maxNumStones && it->changed) { 
    2481       it->changed = false; 
    2482       pair<hashtype,int> phi = it->cHC(); 
     2484  for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) { 
     2485    if ((*it)->numStones <= maxNumStones && (*it)->changed) { 
     2486      (*it)->changed = false; 
     2487      pair<hashtype,int> phi = (*it)->cHC(); 
    24832488      if (insert_hash(phi.first, phi.second) != 0) throw DBError(); 
    24842489    } 
     
    24922497void Algo_hash::move_process(Move m) throw(DBError) { 
    24932498  if (m.color == 'B') { 
    2494     for(vector<HashInstance>::iterator it = hi->begin(); it != hi->end(); it++) 
    2495       it->addB(m.x, m.y); 
     2499    for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 
     2500      (*it)->addB(m.x, m.y); 
    24962501    if (m.captures) { 
    24972502      for(vector<p_cc>::iterator cap_it = m.captures->begin(); cap_it != m.captures->end(); cap_it++) { 
    2498         for(vector<HashInstance>::iterator it = hi->begin(); it != hi->end(); it++) 
    2499           it->removeW(cap_it->first, cap_it->second); 
     2503        for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 
     2504          (*it)->removeW(cap_it->first, cap_it->second); 
    25002505      } 
    25012506    } 
    25022507  } else { 
    2503     for(vector<HashInstance>::iterator it = hi->begin(); it != hi->end(); it++) 
    2504       it->addW(m.x, m.y); 
     2508    for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 
     2509      (*it)->addW(m.x, m.y); 
    25052510    if (m.captures) { 
    25062511      for(vector<p_cc>::iterator cap_it = m.captures->begin(); cap_it != m.captures->end(); cap_it++) { 
    2507         for(vector<HashInstance>::iterator it = hi->begin(); it != hi->end(); it++) 
    2508           it->removeB(cap_it->first, cap_it->second); 
     2512        for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 
     2513          (*it)->removeB(cap_it->first, cap_it->second); 
    25092514      } 
    25102515    } 
     
    25132518 
    25142519void Algo_hash::branchpoint_process() { 
    2515   for(vector<HashInstance>::iterator it = hi->begin(); it != hi->end(); it++) 
    2516     it->bppush(); 
     2520  for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 
     2521    (*it)->bppush(); 
    25172522} 
    25182523 
    25192524void Algo_hash::endOfVariation_process() { 
    2520   for(vector<HashInstance>::iterator it = hi->begin(); it != hi->end(); it++) 
    2521     it->bppop(); 
     2525  for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 
     2526    (*it)->bppop(); 
    25222527} 
    25232528 
    25242529void Algo_hash::endgame_process() { 
    2525   for(vector<HashInstance>::iterator it = hi->begin(); it != hi->end(); it++) 
    2526     it->finalize(); 
     2530  for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 
     2531    (*it)->finalize(); 
    25272532} 
    25282533  
     
    26432648  sprintf(buf, "%d", size); 
    26442649  dbnameext += buf; 
    2645   hi = new vector<HashInstance>; 
    2646   hi->push_back(HashInstance(0,0,size,size,boardsize)); 
    2647   hi->push_back(HashInstance(0,bsize-size,size,size,boardsize)); 
    2648   hi->push_back(HashInstance(bsize-size,0,size,size,boardsize)); 
    2649   hi->push_back(HashInstance(bsize-size, bsize-size, size, size,boardsize)); 
     2650  hi = new vector<HashInstance* >; 
     2651  hi->push_back(new HashInstanceCO(0,0,size,size,boardsize)); 
     2652  hi->push_back(new HashInstanceCO(0,bsize-size,size,size,boardsize)); 
     2653  hi->push_back(new HashInstanceCO(bsize-size,0,size,size,boardsize)); 
     2654  hi->push_back(new HashInstanceCO(bsize-size, bsize-size, size, size,boardsize)); 
    26502655} 
    26512656 
     
    27122717// } 
    27132718 
     2719 
     2720Algo_hash_center::Algo_hash_center(int bsize) : Algo_hash(bsize, "CENTER", 16) { 
     2721  hi = new vector<HashInstance* >; 
     2722  for(int i=1; i<bsize-4; i++) 
     2723    for(int j=1; j<bsize-4; j++) 
     2724      hi->push_back(new HashInstanceCTR(i, j, 4, 4, boardsize)); 
     2725} 
     2726         
     2727void Algo_hash_center::endOfNode_process() { 
     2728  for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) { 
     2729    if ((*it)->numStones <= maxNumStones && (*it)->changed) { 
     2730      (*it)->changed = false; 
     2731      pair<hashtype,int> phi = (*it)->cHC(); 
     2732      if (phi.first in table of relevant hash codes)  
     2733        if (insert_hash(phi.first, phi.second) != 0) throw DBError(); 
     2734    } 
     2735  } 
     2736} 
     2737 
     2738 
     2739pair<hashtype,int> Algo_hash_center::compute_hashkey(PatternList& pl, int CS) { 
     2740  return make_pair(NOT_HASHABLE,0); 
     2741} 
     2742 
     2743 
     2744 
    27142745HashInstance::HashInstance(char X, char Y, char SIZEX, char SIZEY, int BOARDSIZE) { 
    27152746  boardsize = BOARDSIZE; 
     
    27572788} 
    27582789 
    2759 void HashInstance::addB(char x, char y) { 
    2760   if (inRelevantRegion(x,y)) { 
    2761     changed = true; 
    2762     for(int i=0; i<8; i++) { 
    2763       currentHashCode[i] += Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 
    2764     } 
    2765     numStones++; 
    2766   } 
    2767 
    2768  
    2769 void HashInstance::addW(char x, char y) { 
    2770   if (inRelevantRegion(x,y)) { 
    2771     changed = true; 
    2772     for(int i=0; i<8; i++) { 
    2773       currentHashCode[i] -= Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 
    2774     } 
    2775     numStones++; 
    2776   } 
    2777 
    2778  
    2779 void HashInstance::removeB(char x, char y) { 
    2780   if (inRelevantRegion(x,y)) { 
    2781     changed = true; 
    2782     for(int i=0; i<8; i++) { 
    2783       currentHashCode[i] -= Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 
    2784     } 
    2785     numStones++; 
    2786   } 
    2787 
    2788  
    2789 void HashInstance::removeW(char x, char y) { 
    2790   if (inRelevantRegion(x,y)) { 
    2791     changed = true; 
    2792     for(int i=0; i<8; i++) { 
    2793       currentHashCode[i] += Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 
    2794     } 
    2795     numStones++; 
    2796   } 
    2797 
     2790void HashInstance::addB(char x, char y) { printf("ouch\n"); } 
     2791 
     2792void HashInstance::addW(char x, char y) { } 
     2793 
     2794void HashInstance::removeB(char x, char y) { } 
     2795 
     2796void HashInstance::removeW(char x, char y) { } 
    27982797 
    27992798pair<hashtype,int> HashInstance::cHC() { 
     
    28202819  numStones = branchpoints->top().second; 
    28212820  branchpoints->pop(); 
     2821} 
     2822 
     2823 
     2824HashInstanceCO::HashInstanceCO(char X, char Y, char SIZEX, char SIZEY, int BOARDSIZE) : HashInstance(X, Y, SIZEX, SIZEY, BOARDSIZE) { 
     2825} 
     2826 
     2827HashInstanceCO::~HashInstanceCO() { 
     2828  finalize(); 
     2829} 
     2830 
     2831void HashInstanceCO::addB(char x, char y) { 
     2832  if (inRelevantRegion(x,y)) { 
     2833    changed = true; 
     2834    for(int i=0; i<8; i++) { 
     2835      currentHashCode[i] += Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 
     2836    } 
     2837    numStones++; 
     2838  } 
     2839} 
     2840 
     2841void HashInstanceCO::addW(char x, char y) { 
     2842  if (inRelevantRegion(x,y)) { 
     2843    changed = true; 
     2844    for(int i=0; i<8; i++) { 
     2845      currentHashCode[i] -= Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 
     2846    } 
     2847    numStones++; 
     2848  } 
     2849} 
     2850 
     2851void HashInstanceCO::removeB(char x, char y) { 
     2852  if (inRelevantRegion(x,y)) { 
     2853    changed = true; 
     2854    for(int i=0; i<8; i++) { 
     2855      currentHashCode[i] -= Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 
     2856    } 
     2857    numStones++; 
     2858  } 
     2859} 
     2860 
     2861void HashInstanceCO::removeW(char x, char y) { 
     2862  if (inRelevantRegion(x,y)) { 
     2863    changed = true; 
     2864    for(int i=0; i<8; i++) { 
     2865      currentHashCode[i] += Algo_hash::hashCodes[Pattern::flipsX(i,x,y,boardsize-1, boardsize-1) + boardsize*Pattern::flipsY(i,x,y,boardsize-1, boardsize-1)]; 
     2866    } 
     2867    numStones++; 
     2868  } 
     2869} 
     2870 
     2871 
     2872HashInstanceCTR::HashInstanceCTR(char X, char Y, char SIZEX, char SIZEY, int BOARDSIZE) : HashInstance(X, Y, SIZEX, SIZEY, BOARDSIZE) { 
     2873} 
     2874 
     2875HashInstanceCTR::~HashInstanceCTR() { 
     2876  finalize(); 
     2877} 
     2878 
     2879void HashInstanceCTR::addB(char x, char y) { 
     2880  if (inRelevantRegion(x,y)) { 
     2881    changed = true; 
     2882    for(int i=0; i<8; i++) { 
     2883      char x1 = Pattern::flipsX(i, x-xx, y-yy, 3, 3); 
     2884      char y1 = Pattern::flipsY(i, x-xx, y-yy, 3, 3); 
     2885      // stone at position (x1, y1) at bits 4*x1+yy1 4*x1+y1+1 
     2886      int p = 2*(4*x1+y1); 
     2887      currentHashCode[i] ^= ((currentHashCode[i] >> p)%4) << p; // set to 00 
     2888      currentHashCode[i] |= 1 << p; // set to 01 = black 
     2889    } 
     2890  } 
     2891} 
     2892 
     2893void HashInstanceCTR::addW(char x, char y) { 
     2894  if (inRelevantRegion(x,y)) { 
     2895    changed = true; 
     2896    for(int i=0; i<8; i++) { 
     2897      char x1 = Pattern::flipsX(i, x-xx, y-yy, 3, 3); 
     2898      char y1 = Pattern::flipsY(i, x-xx, y-yy, 3, 3); 
     2899      // stone at position (x1, y1) at bits 4*x1+yy1 4*x1+y1+1 
     2900      int p = 2*(4*x1+y1); 
     2901      currentHashCode[i] ^= ((currentHashCode[i] >> p)%4) << p; // set to 00 
     2902      currentHashCode[i] |= 1 << p+1; // set to 10 = white 
     2903    } 
     2904  } 
     2905} 
     2906 
     2907void HashInstanceCTR::removeB(char x, char y) { 
     2908  if (inRelevantRegion(x,y)) { 
     2909    changed = true; 
     2910    for(int i=0; i<8; i++) { 
     2911      char x1 = Pattern::flipsX(i, x-xx, y-yy, 3, 3); 
     2912      char y1 = Pattern::flipsY(i, x-xx, y-yy, 3, 3); 
     2913      // stone at position (x1, y1) at bits 4*x1+yy1 4*x1+y1+1 
     2914      int p = 2*(4*x1+y1); 
     2915      currentHashCode[i] ^= ((currentHashCode[i] >> p)%4) << p; // set to 00 
     2916    } 
     2917  } 
     2918} 
     2919 
     2920void HashInstanceCTR::removeW(char x, char y) { 
     2921  if (inRelevantRegion(x,y)) { 
     2922    changed = true; 
     2923    for(int i=0; i<8; i++) { 
     2924      char x1 = Pattern::flipsX(i, x-xx, y-yy, 3, 3); 
     2925      char y1 = Pattern::flipsY(i, x-xx, y-yy, 3, 3); 
     2926      // stone at position (x1, y1) at bits 4*x1+yy1 4*x1+y1+1 
     2927      int p = 2*(4*x1+y1); 
     2928      currentHashCode[i] ^= ((currentHashCode[i] >> p)%4) << p; // set to 00 
     2929    } 
     2930  } 
    28222931} 
    28232932 
     
    36173726  // if (algos & ALGO_HASH_SIDE)  
    36183727  //   algo_ps[algo_hash_side] = new Algo_hash_side(boardsize, 6, 4, p_op->algo_hash_side_maxNumStones); 
     3728  if (p_op->algos & ALGO_HASH_CENTER) { 
     3729    algo_ps[algo_hash_center+20*ctr] = new Algo_hash_center(bs); 
     3730    algo_dbs[algo_hash_center+20*ctr] = algo_db2; 
     3731  } 
    36193732} 
    36203733 
  • 06/libkombilo-branches/hash_center/search.h

    r230 r233  
    396396  public: 
    397397    HashInstance(char X, char Y, char SIZEX, char SIZEY, int BOARDSIZE); 
    398     ~HashInstance(); 
     398    virtual ~HashInstance(); 
    399399    bool inRelevantRegion(char X, char Y); 
    400400 
     
    407407    bool changed; 
    408408 
    409     void initialize(); 
    410     void finalize(); 
     409    virtual void initialize(); 
     410    virtual void finalize(); 
     411    virtual void addB(char x, char y); 
     412    virtual void removeB(char x, char y); 
     413    virtual void addW(char x, char y); 
     414    virtual void removeW(char x, char y); 
     415    virtual void bppush(); 
     416    virtual void bppop(); 
     417    virtual std::pair<hashtype,int> cHC();  // returns current hash code and corresp. index 
     418    hashtype* currentHashCode; // array of 8 hashtype values (to automatically symmetrize hash codes) 
     419    std::stack<std::pair<hashtype*,int> >* branchpoints; 
     420    int numStones; 
     421}; 
     422 
     423class HashInstanceCO : public HashInstance { 
     424  public: 
     425    HashInstanceCO(char X, char Y, char SIZEX, char SIZEY, int BOARDSIZE); 
     426    virtual ~HashInstanceCO(); 
     427 
    411428    void addB(char x, char y); 
    412429    void removeB(char x, char y); 
    413430    void addW(char x, char y); 
    414431    void removeW(char x, char y); 
    415     void bppush(); 
    416     void bppop(); 
    417     std::pair<hashtype,int> cHC();  // returns min(currentHashCode) and corresp. index 
    418     hashtype* currentHashCode; // array of 8 hashtype values (to automatically symmetrize hash codes) 
    419     std::stack<std::pair<hashtype*,int> >* branchpoints; 
    420     int numStones; 
     432}; 
     433 
     434class HashInstanceCTR : public HashInstance { 
     435  public: 
     436    HashInstanceCTR(char X, char Y, char SIZEX, char SIZEY, int BOARDSIZE); 
     437    virtual ~HashInstanceCTR(); 
     438 
     439    void addB(char x, char y); 
     440    void removeB(char x, char y); 
     441    void addW(char x, char y); 
     442    void removeW(char x, char y); 
    421443}; 
    422444 
     
    445467    virtual int insert_hash(hashtype hashCod, int pos); 
    446468    virtual std::pair<hashtype,int> compute_hashkey(PatternList& pl, int CS); 
    447     std::vector<HashInstance>* hi; 
     469    std::vector<HashInstance* >* hi; 
    448470    std::string dbnameext; 
    449471    int maxNumStones; 
     
    465487//     int sizeY; 
    466488// }; 
     489 
     490class Algo_hash_center : public Algo_hash { 
     491  public: 
     492    Algo_hash_center(int bsize); 
     493    void initialize_process(sqlite3* DB) throw(DBError); 
     494    void endOfNode_process(); 
     495    void finalize_process(); 
     496    std::pair<hashtype,int> compute_hashkey(PatternList& pl, int CS); 
     497 
     498    std::map<hashtype, int> data; 
     499}; 
    467500 
    468501// class UIntervals {