Ticket #145: genmove.c.patch

File genmove.c.patch, 11.0 kB (added by draqo, 2 years ago)
  • gnugo/engine/genmove.c

    RCS file: /sources/gnugo/gnugo/engine/genmove.c,v
    retrieving revision 1.116
    diff -u -r1.116 genmove.c
     
    7373void 
    7474reset_engine() 
    7575{ 
     76  static int last_level = -1; 
     77  int act_level; 
     78 
    7679  /* To improve the reproducability of games, we restart the random 
    7780   * number generator with the same seed for each move. Thus we don't 
    7881   * have to know how many previous moves have been played, nor 
     
    98101  clear_break_in_list(); 
    99102 
    100103  /* Set up depth values (see comments there for details). */ 
    101   set_depth_values(get_level(), 0); 
     104  act_level = get_level(); 
     105  if (last_level != act_level) 
     106  { 
     107        set_depth_values(act_level, 0); 
     108        last_level = act_level; 
     109  } 
    102110} 
    103111 
    104112/* 
     
    117125void 
    118126examine_position(int how_much, int aftermath_play) 
    119127{ 
     128#ifndef GG_TURN_OFF_TRACES 
    120129  int save_verbose = verbose; 
     130#endif 
    121131 
    122132  purge_persistent_caches(); 
    123133   
     134#ifndef GG_TURN_OFF_TRACES 
    124135  /* Don't print reading traces during make_worms and make_dragons unless  
    125136   * the user really wants it (verbose == 3).  
    126137   */ 
    127138  if (verbose == 1 || verbose == 2) 
    128139    --verbose; 
     140#endif 
    129141 
    130142  if (NEEDS_UPDATE(worms_examined)) { 
     143 
     144#ifndef GG_TURN_OFF_TRACES 
    131145    start_timer(0); 
     146#endif 
     147 
    132148    make_worms(); 
     149 
     150#ifndef GG_TURN_OFF_TRACES 
    133151    time_report(0, "  make worms", NO_MOVE, 1.0); 
     152#endif 
    134153  } 
    135154 
    136155  if (how_much == EXAMINE_WORMS) { 
     156 
     157#ifndef GG_TURN_OFF_TRACES 
    137158    verbose = save_verbose; 
     159#endif 
     160 
    138161    gg_assert(test_gray_border() < 0); 
    139162    return; 
    140163  } 
     
    142165  if (stones_on_board(BLACK | WHITE) != 0) { 
    143166    if (NEEDS_UPDATE(initial_influence_examined)) 
    144167      compute_worm_influence(); 
     168 
    145169    if (how_much == EXAMINE_INITIAL_INFLUENCE) { 
     170 
     171#ifndef GG_TURN_OFF_TRACES 
    146172      verbose = save_verbose; 
     173#endif 
     174 
    147175      gg_assert(test_gray_border() < 0); 
    148176      return; 
    149177    } 
     
    151179    if (how_much == EXAMINE_DRAGONS_WITHOUT_OWL) { 
    152180      if (NEEDS_UPDATE(dragons_examined_without_owl)) 
    153181        make_dragons(1); 
     182 
     183#ifndef GG_TURN_OFF_TRACES 
    154184      verbose = save_verbose; 
     185#endif 
     186 
    155187      gg_assert(test_gray_border() < 0); 
    156188      return; 
    157189    } 
     
    163195      dragons_examined_without_owl = position_number; 
    164196    } 
    165197    if (how_much == EXAMINE_DRAGONS) { 
     198 
     199#ifndef GG_TURN_OFF_TRACES 
    166200      verbose = save_verbose; 
     201#endif 
     202 
    167203      gg_assert(test_gray_border() < 0); 
    168204      return; 
    169205    } 
     
    173209           || how_much == EXAMINE_ALL) { 
    174210    initialize_dragon_data(); 
    175211    compute_scores(chinese_rules || aftermath_play); 
     212 
     213#ifndef GG_TURN_OFF_TRACES 
    176214    verbose = save_verbose; 
     215#endif 
     216 
    177217    gg_assert(test_gray_border() < 0); 
    178218    return; 
    179219  } 
    180220   
     221#ifndef GG_TURN_OFF_TRACES 
    181222  verbose = save_verbose; 
     223#endif 
    182224 
    183225  if (NEEDS_UPDATE(initial_influence2_examined)) { 
    184226    compute_dragon_influence(); 
     
    197239    return; 
    198240  } 
    199241 
     242#ifndef GG_TURN_OFF_TRACES 
    200243  if (printworms) 
    201244    show_dragons(); 
     245#endif 
    202246} 
    203247 
    204248 
     
    283327/* This function collects move reasons can be generated immediately from 
    284328 * the data gathered in the examine_position() phase. 
    285329 */ 
    286 void 
     330inline void 
    287331collect_move_reasons(int color) 
    288332{ 
    289333  worm_reasons(color); 
     
    306350           int allowed_moves[BOARDMAX], float *value, int *resign) 
    307351{ 
    308352  float average_score, pessimistic_score, optimistic_score; 
    309   int save_verbose; 
    310   int save_depth; 
    311353  int move; 
    312354  float dummy_value; 
    313355  int use_thrashing_dragon_heuristics = 0; 
    314356 
     357#ifndef GG_TURN_OFF_TRACES 
     358  int save_verbose; 
     359#endif 
     360 
     361#ifndef GG_TURN_OFF_ASSERTS 
     362  int save_depth; 
     363#endif 
     364 
    315365  if (!value) 
    316366    value = &dummy_value; 
    317367 
     368#ifndef GG_TURN_OFF_TRACES 
    318369  start_timer(0); 
     370#endif 
     371 
     372#ifndef GG_TURN_OFF_STATS 
    319373  clearstats(); 
     374#endif 
    320375 
    321376  /* Usually we would not recommend resignation. */ 
    322377  if (resign) 
     
    332387  /* Prepare pattern matcher and reading code. */ 
    333388  reset_engine(); 
    334389 
     390#ifndef GG_TURN_OFF_ASSERTS 
    335391  /* Store the depth value so we can check that it hasn't changed when 
    336392   * we leave this function. 
    337393   */ 
    338394  save_depth = depth; 
     395#endif 
    339396 
    340397  /* If in mirror mode, try to find a mirror move. */ 
    341398  if (play_mirror_go 
     
    348405  } 
    349406 
    350407  /* Find out information about the worms and dragons. */ 
     408#ifndef GG_TURN_OFF_TRACES 
    351409  start_timer(1); 
     410#endif 
     411 
    352412  examine_position(EXAMINE_ALL, 0); 
     413 
     414#ifndef GG_TURN_OFF_TRACES 
    353415  time_report(1, "examine position", NO_MOVE, 1.0); 
     416#endif 
    354417 
    355418 
    356419  /* The score will be used to determine when we are safely 
     
    374437    average_score = -(white_score + black_score)/2.0; 
    375438  choose_strategy(color, average_score, game_status(color)); 
    376439 
     440#ifndef GG_TURN_OFF_TRACES 
    377441  if (printboard) { 
    378442    if (printboard == 1) 
    379443      fprintf(stderr, "\n          dragon_status display:\n\n"); 
     
    387451      showboard(4); 
    388452    } 
    389453  } 
     454#endif 
    390455   
    391456  gg_assert(stackp == 0); 
    392457   
     
    396461 
    397462   
    398463  /* Pick up moves that we know of already. */ 
     464#ifndef GG_TURN_OFF_TRACES 
    399465  save_verbose = verbose; 
    400466  if (verbose > 0) 
    401467    verbose--; 
     468#endif 
     469 
    402470  collect_move_reasons(color); 
     471 
     472#ifndef GG_TURN_OFF_TRACES 
    403473  verbose = save_verbose; 
    404474  time_report(1, "generate move reasons", NO_MOVE, 1.0); 
     475#endif 
    405476   
    406477  /* Try to find empty corner moves. */ 
    407478  fuseki(color); 
     
    420491   
    421492  /* The general pattern database. */ 
    422493  shapes(color); 
     494 
     495#ifndef GG_TURN_OFF_TRACES 
    423496  time_report(1, "shapes", NO_MOVE, 1.0); 
     497#endif 
     498 
    424499  gg_assert(stackp == 0); 
    425500 
    426501  /* Look for combination attacks and defenses against them. */ 
    427502  combinations(color); 
     503 
     504#ifndef GG_TURN_OFF_TRACES 
    428505  time_report(1, "combinations", NO_MOVE, 1.0); 
     506#endif 
     507 
    429508  gg_assert(stackp == 0); 
    430509 
    431510  /* Review the move reasons and estimate move values. */ 
     
    433512                          pure_threat_value, pessimistic_score, allowed_moves, 
    434513                          use_thrashing_dragon_heuristics)) 
    435514    TRACE("Move generation likes %1m with value %f\n", move, *value); 
     515 
    436516  gg_assert(stackp == 0); 
     517 
     518#ifndef GG_TURN_OFF_TRACES 
    437519  time_report(1, "review move reasons", NO_MOVE, 1.0); 
     520#endif 
    438521 
    439522 
    440523  /* If the move value is 6 or lower, we look for endgame patterns too. */ 
     
    447530                            use_thrashing_dragon_heuristics)) 
    448531      TRACE("Move generation likes %1m with value %f\n", move, *value); 
    449532    gg_assert(stackp == 0); 
     533 
     534#ifndef GG_TURN_OFF_TRACES 
    450535    time_report(1, "endgame", NO_MOVE, 1.0); 
     536#endif 
    451537  } 
    452538   
    453539  /* If no move found yet, revisit any semeai and change the 
     
    465551              move, *value);  
    466552      } 
    467553    } 
    468     time_report(1, "move reasons with revised semeai status", 
    469                 NO_MOVE, 1.0); 
     554 
     555#ifndef GG_TURN_OFF_TRACES 
     556    time_report(1, "move reasons with revised semeai status", NO_MOVE, 1.0); 
     557#endif 
    470558  } 
    471559 
    472560  /* If still no move, fill a remaining liberty. This should pick up 
     
    478566      *value = 1.0; 
    479567      TRACE("Filling a liberty at %1m\n", move); 
    480568      record_top_move(move, *value); 
    481       move_considered(move, *value); 
     569 
     570#ifndef GG_TURN_OFF_TRACES 
     571          move_considered(move, *value); 
    482572      time_report(1, "fill liberty", NO_MOVE, 1.0); 
     573#endif 
    483574    } 
    484575    else 
    485576      move = PASS_MOVE; 
     
    492583  if (move == PASS_MOVE) { 
    493584    if (play_out_aftermath  
    494585        || capture_all_dead  
    495         || (!doing_scoring && thrashing_dragon && pessimistic_score > 15.0)) 
     586        || (!doing_scoring && thrashing_dragon && pessimistic_score > 1.0)) 
    496587      move = aftermath_genmove(color, 0, allowed_moves); 
    497588       
    498589    /* If we're instructed to capture all dead opponent stones, generate 
     
    506597      *value = 1.0; 
    507598      TRACE("Aftermath move at %1m\n", move); 
    508599      record_top_move(move, *value); 
    509       move_considered(move, *value); 
     600 
     601#ifndef GG_TURN_OFF_TRACES 
     602          move_considered(move, *value); 
    510603      time_report(1, "aftermath_genmove", NO_MOVE, 1.0); 
     604#endif 
    511605    } 
    512606  } 
    513607 
     
    534628    *resign = 1; 
    535629  } 
    536630   
     631#ifndef GG_TURN_OFF_STATS 
    537632  /* If statistics is turned on, this is the place to show it. */ 
    538633  if (showstatistics) 
    539634    showstats(); 
     635#endif 
    540636 
     637#ifndef GG_TURN_OFF_TRACES 
    541638  if (showtime) { 
    542639    double spent = time_report(0, "TIME to generate move at ", move, 1.0); 
    543640    total_time += spent; 
     
    547644      slowest_movenum = movenum + 1; 
    548645    } 
    549646  } 
     647#endif 
    550648 
    551649  /* Some consistency checks to verify that things are properly 
    552650   * restored and/or have not been corrupted. 
    553651   */ 
    554652  gg_assert(stackp == 0); 
    555653  gg_assert(test_gray_border() < 0); 
     654 
     655#ifndef GG_TURN_OFF_ASSERTS 
    556656  gg_assert(depth == save_depth); 
     657#endif 
    557658 
    558659  return move; 
    559660} 
     
    585686  int found_one = 0; 
    586687  int other = OTHER_COLOR(color); 
    587688 
    588   if (stones_on_board(BLACK | WHITE) == 0) 
    589     return 0; 
    590  
    591689  if (doing_scoring) 
    592690    return 0; 
    593691   
    594692  gg_assert(dragon2 != NULL); 
    595693 
    596   for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
    597     if (ON_BOARD(pos) 
    598         && dragon[pos].color == other 
     694  scan_board(pos, 
     695    if (dragon[pos].color == other 
    599696        && DRAGON2(pos).semeais 
    600697        && dragon[pos].status == DEAD) { 
    601698      found_one = 1; 
     
    604701        TRACE("revise_semeai: changed status of dragon %1m from DEAD to UNKNOWN\n", 
    605702              pos); 
    606703    } 
    607   } 
     704  ) 
    608705   
    609706  return found_one; 
    610707} 
     
    622719  int pos; 
    623720  signed char safe_stones[BOARDMAX]; 
    624721  float strength[BOARDMAX]; 
     722  int other_color; 
    625723 
    626724  /* Trust the owl code's opinion if we are behind. */ 
    627725  if (our_score < advantage) 
     
    632730      || dragon[thrashing_dragon].status != DEAD) 
    633731    return 0; 
    634732   
    635   for (pos = BOARDMIN; pos < BOARDMAX; pos++) 
    636     if (ON_BOARD(pos) && thrashing_stone[pos] 
     733  scan_board(pos, 
     734    if (thrashing_stone[pos] 
    637735        && worm[pos].unconditional_status != DEAD) { 
    638736      dragon[pos].status = UNKNOWN; 
    639737      DRAGON2(pos).safety = ALIVE; 
    640738    } 
     739  ) 
    641740 
    642   set_strength_data(OTHER_COLOR(color), safe_stones, strength); 
    643   compute_influence(OTHER_COLOR(color), safe_stones, strength, 
     741  other_color = OTHER_COLOR(color); 
     742  set_strength_data(other_color, safe_stones, strength); 
     743  compute_influence(other_color, safe_stones, strength, 
    644744                    OPPOSITE_INFLUENCE(color), 
    645745                    NO_MOVE, "revised thrashing dragon"); 
    646746  compute_refined_dragon_weaknesses(); 
     
    670770    } 
    671771  } 
    672772  else { 
    673     for (mirror_move = BOARDMIN; mirror_move < BOARDMAX; mirror_move++) { 
    674       if (ON_BOARD(mirror_move) 
    675           && test_symmetry_after_move(mirror_move, color, 0)) { 
     773    scan_board(mirror_move, 
     774      if (test_symmetry_after_move(mirror_move, color, 0)) { 
    676775        *move = mirror_move; 
    677776        return 1; 
    678777      } 
    679     } 
     778        ) 
    680779  } 
    681780   
    682781  return 0; 
     
    701800                    NO_MOVE, "White territory estimate"); 
    702801  black_score = influence_score(&move_influence, use_chinese_rules); 
    703802 
     803#ifndef GG_TURN_OFF_TRACES 
    704804  if (verbose || showscore) { 
    705805    if (white_score == black_score) 
    706806      gprintf("Score estimate: %s %f\n", 
     
    711811              white_score > 0 ? "W " : "B ", gg_abs(white_score)); 
    712812    fflush(stderr); 
    713813  } 
     814#endif 
    714815} 
    715816 
    716817 
     
    749850   * it looks like all our dragons are dead and the generated move 
    750851   * is a pass. 
    751852   */ 
    752   if (board_size > 2 && move == PASS_MOVE && !lively_dragon_exists(color)) 
     853  if (move == PASS_MOVE && !lively_dragon_exists(color)) 
    753854    return 1; 
    754855   
    755856  if (move == PASS_MOVE