From d9a61023022a055103e6c51ad3e129615490be6c Mon Sep 17 00:00:00 2001 From: kobanium Date: Mon, 14 May 2018 00:27:05 +0900 Subject: [PATCH] Refactoring --- src/GoBoard.cpp | 62 ++++++-------- src/Pattern.cpp | 4 +- src/Simulation.cpp | 8 +- src/UctRating.cpp | 201 ++++++++++++++++++++------------------------- src/UctSearch.cpp | 68 +++++++-------- 5 files changed, 147 insertions(+), 196 deletions(-) diff --git a/src/GoBoard.cpp b/src/GoBoard.cpp index 71c4a1d..09e52fb 100644 --- a/src/GoBoard.cpp +++ b/src/GoBoard.cpp @@ -127,7 +127,7 @@ SetSuperKo( const bool flag ) void SetBoardSize( const int size ) { - int i, x, y; + int i; pure_board_size = size; pure_board_max = size * size; @@ -138,16 +138,16 @@ SetBoardSize( const int size ) board_end = (pure_board_size + OB_SIZE - 1); i = 0; - for (y = board_start; y <= board_end; y++) { - for (x = board_start; x <= board_end; x++) { + for (int y = board_start; y <= board_end; y++) { + for (int x = board_start; x <= board_end; x++) { onboard_pos[i++] = POS(x, y); board_x[POS(x, y)] = x; board_y[POS(x, y)] = y; } } - for (y = board_start; y <= board_end; y++) { - for (x = board_start; x <= (board_start + pure_board_size / 2); x++) { + for (int y = board_start; y <= board_end; y++) { + for (int x = board_start; x <= (board_start + pure_board_size / 2); x++) { border_dis_x[POS(x, y)] = x - (OB_SIZE - 1); border_dis_x[POS(board_end + OB_SIZE - x, y)] = x - (OB_SIZE - 1); border_dis_y[POS(y, x)] = x - (OB_SIZE - 1); @@ -155,8 +155,8 @@ SetBoardSize( const int size ) } } - for (y = 0; y < pure_board_size; y++) { - for (x = 0; x < pure_board_size; x++) { + for (int y = 0; y < pure_board_size; y++) { + for (int x = 0; x < pure_board_size; x++) { move_dis[x][y] = x + y + ((x > y) ? x : y); if (move_dis[x][y] >= MOVE_DISTANCE_MAX) move_dis[x][y] = MOVE_DISTANCE_MAX - 1; } @@ -164,8 +164,8 @@ SetBoardSize( const int size ) fill_n(board_pos_id, BOARD_MAX, 0); i = 1; - for (y = board_start; y <= (board_start + pure_board_size / 2); y++) { - for (x = board_start; x <= y; x++) { + for (int y = board_start; y <= (board_start + pure_board_size / 2); y++) { + for (int x = board_start; x <= y; x++) { board_pos_id[POS(x, y)] = i; board_pos_id[POS(board_end + OB_SIZE - x, y)] = i; board_pos_id[POS(y, x)] = i; @@ -179,8 +179,8 @@ SetBoardSize( const int size ) } first_move_candidates = 0; - for (y = board_start; y <= (board_start + board_end) / 2; y++) { - for (x = board_end + board_start - y; x <= board_end; x++) { + for (int y = board_start; y <= (board_start + board_end) / 2; y++) { + for (int x = board_end + board_start - y; x <= board_end; x++) { first_move_candidate[first_move_candidates++] = POS(x, y); } } @@ -738,18 +738,18 @@ IsFalseEyeConnection( const game_info_t *game, const int pos, const int color ) const string_t *string = game->string; const int *string_id = game->string_id; const char *board = game->board; + const int other = FLIP_COLOR(color); int checked_string[4] = { 0 }; int string_liberties[4] = { 0 }; int strings = 0; int id, lib, libs = 0, lib_sum = 0; int liberty[STRING_LIB_MAX]; int count; - bool checked; int neighbor4[4], neighbor; - bool already_checked; - int other = FLIP_COLOR(color); int player_id[4] = {0}; int player_ids = 0; + bool checked; + bool already_checked; // 欠け眼を構成する連のIDを取り出す GetNeighbor4(neighbor4, pos); @@ -915,14 +915,14 @@ IsSuicide( const game_info_t *game, const string_t *string, const int color, con const char *board = game->board; const int *string_id = game->string_id; const int other = FLIP_COLOR(color); - int neighbor4[4], i; + int neighbor4[4]; GetNeighbor4(neighbor4, pos); // 隣接するの石についての判定 // 隣接する石が相手でも、その石を含む連の呼吸点が1の時は合法手 // 隣接する石が自分で、その石を含む連の呼吸点が2以上の時は合法手 - for (i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { if (board[neighbor4[i]] == other && string[string_id[neighbor4[i]]].libs == 1) { return false; @@ -943,9 +943,9 @@ void PutStone( game_info_t *game, const int pos, const int color ) { const int *string_id = game->string_id; + const int other = FLIP_COLOR(color); char *board = game->board; string_t *string = game->string; - const int other = FLIP_COLOR(color); int connection = 0; int connect[4] = { 0 }; int prisoner = 0; @@ -1052,9 +1052,9 @@ void PoPutStone( game_info_t *game, const int pos, const int color ) { const int *string_id = game->string_id; + const int other = FLIP_COLOR(color); char *board = game->board; string_t *string = game->string; - const int other = FLIP_COLOR(color); int connection = 0; int connect[4] = { 0 }; int prisoner = 0; @@ -1144,9 +1144,9 @@ PoPutStone( game_info_t *game, const int pos, const int color ) static void MakeString( game_info_t *game, const int pos, const int color ) { + const char *board = game->board; string_t *string = game->string; string_t *new_string; - const char *board = game->board; int *string_id = game->string_id; int id = 1; int lib_add = 0; @@ -1239,13 +1239,13 @@ AddStone( game_info_t *game, const int pos, const int color, const int id ) // int color : 置いた石の色 // int id : 石を追加する先の連のID { + const int other = FLIP_COLOR(color); string_t *string = game->string; string_t *add_str; char *board = game->board; int *string_id = game->string_id; int lib_add = 0; - int other = FLIP_COLOR(color); - int neighbor, neighbor4[4], i; + int neighbor, neighbor4[4]; // IDを更新 string_id[pos] = id; @@ -1261,7 +1261,7 @@ AddStone( game_info_t *game, const int pos, const int color, const int id ) // 空点なら呼吸点を追加し // 敵の石があれば隣接する敵連の情報を更新 - for (i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { if (board[neighbor4[i]] == S_EMPTY) { lib_add = AddLiberty(add_str, neighbor4[i], lib_add); } else if (board[neighbor4[i]] == other) { @@ -1692,21 +1692,15 @@ RemoveNeighborString( string_t *string, const int id ) static void CheckBentFourInTheCorner( game_info_t *game ) { - char *board = game->board; const string_t *string = game->string; const int *string_id = game->string_id; const int *string_next = game->string_next; - int pos; - int i; - int id; - int neighbor; - int color; - int lib1, lib2; - int neighbor_lib1, neighbor_lib2; + char *board = game->board; + int pos, id, neighbor, color, lib1, lib2, neighbor_lib1, neighbor_lib2; // 四隅について隅のマガリ四目が存在するか確認し // 存在すれば地を訂正する - for (i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { id = string_id[corner[i]]; if (string[id].size == 3 && string[id].libs == 2 && @@ -1751,8 +1745,6 @@ CalculateScore( game_info_t *game ) // game_info_t *game : 盤面の情報を示すポインタ { const char *board = game->board; - int i; - int pos; int color; int scores[S_MAX] = { 0 }; @@ -1760,8 +1752,8 @@ CalculateScore( game_info_t *game ) CheckBentFourInTheCorner(game); // 地の数え上げ - for (i = 0; i < pure_board_max; i++) { - pos = onboard_pos[i]; + for (int i = 0; i < pure_board_max; i++) { + const int pos = onboard_pos[i]; color = board[pos]; if (color == S_EMPTY) color = territory[Pat3(game->pat, pos)]; scores[color]++; diff --git a/src/Pattern.cpp b/src/Pattern.cpp index f24163d..d463db0 100644 --- a/src/Pattern.cpp +++ b/src/Pattern.cpp @@ -122,11 +122,9 @@ static const unsigned long long large_mask[][3] = { void ClearPattern( pattern_t *pat ) { - int y; - memset(pat, 0, sizeof(pattern_t) * board_max); - for (y = board_start; y <= board_end; y++){ + for (int y = board_start; y <= board_end; y++){ // 1線 // 上 pat[POS(y, board_start)].list[MD_2] |= 0x0003003F; // 1 2 3 9 diff --git a/src/Simulation.cpp b/src/Simulation.cpp index c747b78..569ea26 100644 --- a/src/Simulation.cpp +++ b/src/Simulation.cpp @@ -16,13 +16,10 @@ using namespace std; void Simulation( game_info_t *game, int starting_color, std::mt19937_64 *mt ) { - int color = starting_color; - int pos = -1; - int length; - int pass_count; + int color = starting_color, pos = -1, pass_count; // シミュレーション打ち切り手数を設定 - length = MAX_MOVES - game->moves; + int length = MAX_MOVES - game->moves; if (length < 0) { return; } @@ -50,5 +47,4 @@ Simulation( game_info_t *game, int starting_color, std::mt19937_64 *mt ) // 手番の入れ替え color = FLIP_COLOR(color); } - } diff --git a/src/UctRating.cpp b/src/UctRating.cpp index fc67a77..746eaa2 100644 --- a/src/UctRating.cpp +++ b/src/UctRating.cpp @@ -104,15 +104,14 @@ static void InputLargePattern( const char *filename, latent_factor_t *lf, index_ void InitializeUctRating() { - int i; // γ読み込み InputUCTParameter(); - for (i = UCT_SAVE_CAPTURE_1_1; i <= UCT_SEMEAI_CAPTURE; i++) { + for (int i = UCT_SAVE_CAPTURE_1_1; i <= UCT_SEMEAI_CAPTURE; i++) { capture_mask |= uct_mask[i]; } - for (i = UCT_ATARI; i <= UCT_3POINT_C_ATARI_L_L; i++) { + for (int i = UCT_ATARI; i <= UCT_3POINT_C_ATARI_L_L; i++) { atari_mask |= uct_mask[i]; } } @@ -124,7 +123,7 @@ InitializeUctRating() void UctCheckFeaturesLib1( game_info_t *game, int color, int id, bool ladder, uct_features_t *uct_features ) { - string_t *string = game->string; + const string_t *string = game->string; int lib, neighbor; unsigned long long *tactical_features1 = uct_features->tactical_features1; @@ -195,10 +194,9 @@ UctCheckFeaturesLib1( game_info_t *game, int color, int id, bool ladder, uct_fea void UctCheckFeaturesLib2( game_info_t *game, int color, int id, uct_features_t *uct_features ) { - string_t *string = game->string; - int lib1, lib2, neighbor; + const string_t *string = game->string; + int lib1, lib2, neighbor, lib1_state, lib2_state; unsigned long long *tactical_features1 = uct_features->tactical_features1; - int lib1_state, lib2_state; // 呼吸点が2つになった連の呼吸点を取り出す lib1 = string[id].lib[0]; @@ -326,10 +324,9 @@ UctCheckFeaturesLib2( game_info_t *game, int color, int id, uct_features_t *uct_ void UctCheckFeaturesLib3( game_info_t *game, int color, int id, uct_features_t *uct_features ) { - string_t *string = game->string; - int lib1, lib2, lib3, neighbor; + const string_t *string = game->string; + int lib1, lib2, lib3, neighbor, lib1_state, lib2_state, lib3_state; unsigned long long *tactical_features1 = uct_features->tactical_features1; - int lib1_state, lib2_state, lib3_state; // 呼吸点が3つになった連の呼吸点を取り出す lib1 = string[id].lib[0]; @@ -501,18 +498,15 @@ UctCheckFeaturesLib3( game_info_t *game, int color, int id, uct_features_t *uct_ ////////////////// void UctCheckFeatures( game_info_t *game, int color, uct_features_t *uct_features ) -{ - string_t *string = game->string; - char *board = game->board; - int *string_id = game->string_id; - int previous_move = PASS; - int id; +{ + const char *board = game->board; + const string_t *string = game->string; + const int *string_id = game->string_id; + int previous_move = PASS, id; int check[4] = { 0 }; int checked = 0; - bool ladder; + bool ladder, already_checked; int neighbor4[4]; - int i, j; - bool already_checked; if (game->moves > 1) previous_move = game->record[game->moves - 1].pos; @@ -520,11 +514,11 @@ UctCheckFeatures( game_info_t *game, int color, uct_features_t *uct_features ) GetNeighbor4(neighbor4, previous_move); - for (i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { if (board[neighbor4[i]] == color) { id = string_id[neighbor4[i]]; already_checked = false; - for (j = 0; j < checked; j++) { + for (int j = 0; j < checked; j++) { if (check[j] == id) { already_checked = true; break; @@ -551,18 +545,17 @@ UctCheckFeatures( game_info_t *game, int color, uct_features_t *uct_features ) void UctCheckCaptureAfterKo( game_info_t *game, int color, uct_features_t *uct_features ) { - string_t *string = game->string; - char *board = game->board; - int *string_id = game->string_id; - int other = FLIP_COLOR(color); - int previous_move_2 = game->record[game->moves - 2].pos; - int id, lib, i; + const string_t *string = game->string; + const char *board = game->board; + const int *string_id = game->string_id; + const int other = FLIP_COLOR(color); + const int previous_move_2 = game->record[game->moves - 2].pos; + int id, lib, neighbor4[4]; unsigned long long *tactical_features1 = uct_features->tactical_features1; - int neighbor4[4]; GetNeighbor4(neighbor4, previous_move_2); - for (i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { if (board[neighbor4[i]] == other) { id = string_id[neighbor4[i]]; if (string[id].libs == 1) { @@ -590,7 +583,6 @@ UctCheckSelfAtari( game_info_t *game, int color, int pos, uct_features_t *uct_fe int already_num = 0; int id; int lib, count, libs = 0; - int i, j; int lib_candidate[PURE_BOARD_MAX]; bool checked; unsigned long long *tactical_features1 = uct_features->tactical_features1; @@ -599,7 +591,7 @@ UctCheckSelfAtari( game_info_t *game, int color, int pos, uct_features_t *uct_fe GetNeighbor4(neighbor4, pos); - for (i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { if (board[neighbor4[i]] == S_EMPTY) { lib_candidate[libs++] = neighbor4[i]; } @@ -609,11 +601,11 @@ UctCheckSelfAtari( game_info_t *game, int color, int pos, uct_features_t *uct_fe if (libs >= 2) return true; // 上下左右の確認 - for (i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { if (board[neighbor4[i]] == color) { id = string_id[neighbor4[i]]; already_checked = false; - for (j = 0; j < already_num; j++) { + for (int j = 0; j < already_num; j++) { if (already[j] == id) { already_checked = true; } @@ -626,7 +618,7 @@ UctCheckSelfAtari( game_info_t *game, int color, int pos, uct_features_t *uct_fe while (lib != LIBERTY_END) { if (lib != pos) { checked = false; - for (j = 0; j < libs; j++) { + for (int j = 0; j < libs; j++) { if (lib_candidate[j] == lib) { checked = true; break; @@ -678,20 +670,18 @@ UctCheckSelfAtari( game_info_t *game, int color, int pos, uct_features_t *uct_fe void UctCheckCapture( game_info_t *game, int color, int pos, uct_features_t *uct_features ) { - char *board = game->board; + const char *board = game->board; + const int other = FLIP_COLOR(color); string_t *string = game->string; int *string_id = game->string_id; - int other = FLIP_COLOR(color); bool check; - int neighbor; - int id; + int neighbor, id; unsigned long long *tactical_features1 = uct_features->tactical_features1; int neighbor4[4]; - int i; GetNeighbor4(neighbor4, pos); - for (i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { if (board[neighbor4[i]] == other) { if (string[string_id[neighbor4[i]]].libs == 1) { check = false; @@ -722,18 +712,16 @@ UctCheckCapture( game_info_t *game, int color, int pos, uct_features_t *uct_feat void UctCheckAtari( game_info_t *game, int color, int pos, uct_features_t *uct_features ) { - char *board = game->board; + const char *board = game->board; + const int other = FLIP_COLOR(color); string_t *string = game->string; - int other = FLIP_COLOR(color); int *string_id = game->string_id; - int id; - int size; + int id, size, neighbor4[4]; unsigned long long *tactical_features1 = uct_features->tactical_features1; - int neighbor4[4], i; GetNeighbor4(neighbor4, pos); - for (i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { if (board[neighbor4[i]] == other) { id = string_id[neighbor4[i]]; if (string[id].libs == 2) { @@ -758,10 +746,8 @@ UctCheckAtari( game_info_t *game, int color, int pos, uct_features_t *uct_featur void UctCheckKoConnection( game_info_t *game, uct_features_t *uct_features ) { - int ko_pos = game->ko_pos; - if (game->ko_move == game->moves - 2) { - uct_features->tactical_features1[ko_pos] |= uct_mask[UCT_KO_CONNECTION]; + uct_features->tactical_features1[game->ko_pos] |= uct_mask[UCT_KO_CONNECTION]; } } @@ -772,8 +758,8 @@ UctCheckKoConnection( game_info_t *game, uct_features_t *uct_features ) void UctCheckRemove2Stones( game_info_t *game, int color, uct_features_t *uct_features ) { - int i, rm1, rm2, connect; - int other = FLIP_COLOR(color); + const int other = FLIP_COLOR(color); + int i, connect; unsigned long long *tactical_features1 = uct_features->tactical_features1; int cross[4]; @@ -786,8 +772,8 @@ UctCheckRemove2Stones( game_info_t *game, int color, uct_features_t *uct_feature return; } - rm1 = game->capture_pos[other][0]; - rm2 = game->capture_pos[other][1]; + const int rm1 = game->capture_pos[other][0]; + const int rm2 = game->capture_pos[other][1]; if (rm1 - rm2 != 1 && rm2 - rm1 != 1 && @@ -824,17 +810,16 @@ UctCheckRemove2Stones( game_info_t *game, int color, uct_features_t *uct_feature void UctCheckRemove3Stones( game_info_t *game, int color, uct_features_t *uct_features ) { - int rm1, rm2, rm3; - int other = FLIP_COLOR(color); + const int other = FLIP_COLOR(color); unsigned long long *tactical_features1 = uct_features->tactical_features1; if (game->capture_num[other] != 3) { return; } - rm1 = game->capture_pos[other][0]; - rm2 = game->capture_pos[other][1]; - rm3 = game->capture_pos[other][2]; + const int rm1 = game->capture_pos[other][0]; + const int rm2 = game->capture_pos[other][1]; + const int rm3 = game->capture_pos[other][2]; if (DIS(rm1, rm2) == 2 && DIS(rm1, rm3) == 2){ tactical_features1[rm1] |= uct_mask[UCT_NAKADE_3]; @@ -852,8 +837,8 @@ UctCheckRemove3Stones( game_info_t *game, int color, uct_features_t *uct_feature void UctCheckKeimaTsukekoshi(game_info_t *game, int color, int pos, uct_features_t *uct_features) { - char *board = game->board; - int other = FLIP_COLOR(color); + const char *board = game->board; + const int other = FLIP_COLOR(color); unsigned long long *tactical_features1 = uct_features->tactical_features1; int keima_pos[8], opponent_pos[8]; @@ -1001,10 +986,10 @@ UctCheckDoubleKeima( game_info_t *game, int color, int pos, uct_features_t *uct_ // +O+++O+ // ++O+O++ // Oのうち自分と相手の石が1個ずつ以上ある時の特徴 - char *board = game->board; - int other = FLIP_COLOR(color); + const char *board = game->board; + const int other = FLIP_COLOR(color); int keima_pos[8]; - int i, p = 0, o = 0; + int player = 0, opponent = 0; unsigned long long *tactical_features1 = uct_features->tactical_features1; if (Pat3(game->pat, pos) != 0x0000) { @@ -1020,12 +1005,12 @@ UctCheckDoubleKeima( game_info_t *game, int color, int pos, uct_features_t *uct_ keima_pos[6] = 2 * board_size - 1; keima_pos[7] = 2 * board_size + 1; - for (i = 0; i < 8; i++) { - if (board[pos + keima_pos[i]] == color) p++; - if (board[pos + keima_pos[i]] == other) o++; + for (int i = 0; i < 8; i++) { + if (board[pos + keima_pos[i]] == color) player++; + if (board[pos + keima_pos[i]] == other) opponent++; } - if (p > 0 && o > 0){ + if (player > 0 && opponent > 0){ tactical_features1[pos] |= uct_mask[UCT_DOUBLE_KEIMA]; } @@ -1041,13 +1026,13 @@ UctCheckSnapBack( game_info_t *game, int color, int pos, uct_features_t *uct_fea const string_t *string = game->string; const int *string_id = game->string_id; const char *board = game->board; - int other = FLIP_COLOR(color); + const int other = FLIP_COLOR(color); unsigned long long *tactical_features1 = uct_features->tactical_features1; - int i, neighbor4[4]; + int neighbor4[4]; GetNeighbor4(neighbor4, pos); - for (i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { if (board[neighbor4[i]] == other) { int id = string_id[neighbor4[i]]; @@ -1076,10 +1061,10 @@ UctCheckSnapBack( game_info_t *game, int color, int pos, uct_features_t *uct_fea double CalculateLFRScore( game_info_t *game, int pos, int index[3], uct_features_t *uct_features ) { + const int moves = game->moves; pattern_t *pat = game->pat; int pm1 = PASS, pm2 = PASS; - int moves = game->moves; - int i, j, f, dis1 = -1, dis2 = -1; + int dis1 = -1, dis2 = -1; double score = weight_zero; double tmp_score; unsigned long long *tactical_features1 = uct_features->tactical_features1; @@ -1118,7 +1103,7 @@ CalculateLFRScore( game_info_t *game, int pos, int index[3], uct_features_t *uct md2 = md2_index[MD2(pat, pos)]; // 特徴を - for (i = 0; i < UCT_TACTICAL_FEATURE_MAX; i++) { + for (int i = 0; i < UCT_TACTICAL_FEATURE_MAX; i++) { if ((tactical_features1[pos] & uct_mask[i]) != 0) { all_feature[feature_num++] = &uct_tactical_features[i]; } @@ -1147,15 +1132,15 @@ CalculateLFRScore( game_info_t *game, int pos, int index[3], uct_features_t *uct } // wの足し算 - for (i = 0; i < feature_num; i++) { + for (int i = 0; i < feature_num; i++) { score += all_feature[i]->w; } // vの計算 - for (f = 0; f < LFR_DIMENSION; f++) { - for (i = 0; i < feature_num; i++) { + for (int f = 0; f < LFR_DIMENSION; f++) { + for (int i = 0; i < feature_num; i++) { tmp_score = 0.0; - for (j = i + 1; j < feature_num; j++) { + for (int j = i + 1; j < feature_num; j++) { tmp_score += all_feature[j]->v[f]; } score += tmp_score * all_feature[i]->v[f]; @@ -1172,12 +1157,9 @@ CalculateLFRScore( game_info_t *game, int pos, int index[3], uct_features_t *uct void AnalyzeUctRating( game_info_t *game, int color, double rate[] ) { - int i; - int pos; - int moves = game->moves; - float gamma; - pattern_hash_t hash_pat; + const int moves = game->moves; int pat_index[3]; + pattern_hash_t hash_pat; uct_features_t uct_features; memset(&uct_features, 0, sizeof(uct_features_t)); @@ -1190,8 +1172,8 @@ AnalyzeUctRating( game_info_t *game, int color, double rate[] ) UctCheckKoConnection(game, &uct_features); } - for (i = 0; i < pure_board_max; i++) { - pos = onboard_pos[i]; + for (int i = 0; i < pure_board_max; i++) { + const int pos = onboard_pos[i]; if (!game->candidates[pos] || !IsLegal(game, pos, color)) { rate[i] = 0; continue; @@ -1210,9 +1192,7 @@ AnalyzeUctRating( game_info_t *game, int color, double rate[] ) pat_index[1] = SearchIndex(md4_index, hash_pat.list[MD_4]); pat_index[2] = SearchIndex(md5_index, hash_pat.list[MD_5 + MD_MAX]); - gamma = CalculateLFRScore(game, pos, pat_index, &uct_features); - - rate[i] = gamma; + rate[i] = CalculateLFRScore(game, pos, pat_index, &uct_features); } } @@ -1294,7 +1274,6 @@ static void InputLatentFactor( const char *filename, latent_factor_t *lf, int n ) { FILE *fp; - int i, j; #if defined (_WIN32) errno_t err; @@ -1303,12 +1282,12 @@ InputLatentFactor( const char *filename, latent_factor_t *lf, int n ) if (err != 0) { cerr << "can not open -" << filename << "-" << endl; } - for (i = 0; i < n; i++) { + for (int i = 0; i < n; i++) { if (fscanf_s(fp, "%lf", &lf[i].w) == EOF) { cerr << "Read Error : " << filename << endl; exit(1); } - for (j = 0; j < LFR_DIMENSION; j++) { + for (int j = 0; j < LFR_DIMENSION; j++) { if (fscanf_s(fp, "%lf", &lf[i].v[j]) == EOF) { cerr << "Read Error : " << filename << endl; exit(1); @@ -1320,12 +1299,12 @@ InputLatentFactor( const char *filename, latent_factor_t *lf, int n ) if (fp == NULL) { cerr << "can not open -" << filename << "-" << endl; } - for (i = 0; i < n; i++) { + for (int i = 0; i < n; i++) { if (fscanf(fp, "%lf", &lf[i].w) == EOF) { cerr << "Read Error : " << filename << endl; exit(1); } - for (j = 0; j < LFR_DIMENSION; j++) { + for (int j = 0; j < LFR_DIMENSION; j++) { if (fscanf(fp, "%lf", &lf[i].v[j]) == EOF) { cerr << "Read Error : " << filename << endl; exit(1); @@ -1343,18 +1322,18 @@ static void InputPat3( const char *filename, latent_factor_t *lf ) { FILE *fp; - int i, idx = 0; + int idx = 0; double weight; - unsigned int pat3, pat3_transp16[16]; + unsigned int pat3_transp16[16]; - for (pat3 = 0; pat3 < (unsigned int)PAT3_MAX; pat3++) { + for (unsigned int pat3 = 0; pat3 < (unsigned int)PAT3_MAX; pat3++) { pat3_index[pat3] = -1; } - for (pat3 = 0; pat3 < (unsigned int)PAT3_MAX; pat3++) { + for (unsigned int pat3 = 0; pat3 < (unsigned int)PAT3_MAX; pat3++) { if (pat3_index[pat3] == -1) { Pat3Transpose16(pat3, pat3_transp16); - for (i = 0; i < 16; i++) { + for (int i = 0; i < 16; i++) { pat3_index[pat3_transp16[i]] = idx; } idx++; @@ -1369,14 +1348,14 @@ InputPat3( const char *filename, latent_factor_t *lf ) cerr << "can not open -" << filename << "-" << endl; exit(1); } - for (pat3 = 0; pat3 < (unsigned int)PAT3_MAX; pat3++) { + for (unsigned int pat3 = 0; pat3 < (unsigned int)PAT3_MAX; pat3++) { if (fscanf_s(fp, "%lf", &weight) == EOF) { cerr << "Read Error : " << filename << endl; exit(1); } idx = pat3_index[pat3]; lf[idx].w = weight; - for (i = 0; i < LFR_DIMENSION; i++) { + for (int i = 0; i < LFR_DIMENSION; i++) { if (fscanf_s(fp, "%lf", &lf[idx].v[i]) == EOF) { cerr << "Read Error : " << filename << endl; exit(1); @@ -1389,14 +1368,14 @@ InputPat3( const char *filename, latent_factor_t *lf ) cerr << "can not open -" << filename << "-" << endl; exit(1); } - for (pat3 = 0; pat3 < (unsigned int)PAT3_MAX; pat3++) { + for (unsigned int pat3 = 0; pat3 < (unsigned int)PAT3_MAX; pat3++) { if (fscanf(fp, "%lf", &weight) == EOF) { cerr << "Read Error : " << filename << endl; exit(1); } idx = pat3_index[pat3]; lf[idx].w = weight; - for (i = 0; i < LFR_DIMENSION; i++) { + for (int i = 0; i < LFR_DIMENSION; i++) { if (fscanf(fp, "%lf", &lf[idx].v[i]) == EOF) { cerr << "Read Error : " << filename << endl; exit(1); @@ -1413,19 +1392,18 @@ static void InputMD2( const char *filename, latent_factor_t *lf ) { FILE *fp; - int i; int index, idx = 0; double weight; - unsigned int md2, md2_transp16[16]; + unsigned int md2_transp16[16]; - for (md2 = 0; md2 < (unsigned int)MD2_MAX; md2++) { + for (unsigned int md2 = 0; md2 < (unsigned int)MD2_MAX; md2++) { md2_index[md2] = -1; } - for (md2 = 0; md2 < (unsigned int)MD2_MAX; md2++) { + for (unsigned int md2 = 0; md2 < (unsigned int)MD2_MAX; md2++) { if (md2_index[md2] == -1) { MD2Transpose16(md2, md2_transp16); - for (i = 0; i < 16; i++) { + for (int i = 0; i < 16; i++) { md2_index[md2_transp16[i]] = idx; } idx++; @@ -1442,7 +1420,7 @@ InputMD2( const char *filename, latent_factor_t *lf ) while (fscanf_s(fp, "%d%lf", &index, &weight) != EOF) { idx = md2_index[index]; lf[idx].w = weight; - for (i = 0; i < LFR_DIMENSION; i++) { + for (int i = 0; i < LFR_DIMENSION; i++) { if (fscanf_s(fp, "%lf", &lf[idx].v[i]) == EOF) { cerr << "Read Error : " << filename << endl; exit(1); @@ -1457,7 +1435,7 @@ InputMD2( const char *filename, latent_factor_t *lf ) while (fscanf(fp, "%d%lf", &index, &weight) != EOF) { idx = md2_index[index]; lf[idx].w = weight; - for (i = 0; i < LFR_DIMENSION; i++) { + for (int i = 0; i < LFR_DIMENSION; i++) { if (fscanf(fp, "%lf", &lf[idx].v[i]) == EOF) { cerr << "Read Error : " << filename << endl; exit(1); @@ -1473,12 +1451,11 @@ static void InputLargePattern( const char *filename, latent_factor_t *lf, index_hash_t *pat_index ) { FILE *fp; - int i; int index, idx = 0; unsigned long long hash; double weight; - for (i = 0; i < HASH_MAX; i++) { + for (int i = 0; i < HASH_MAX; i++) { pat_index[i].hash = 0; pat_index[i].index = -1; } @@ -1495,7 +1472,7 @@ InputLargePattern( const char *filename, latent_factor_t *lf, index_hash_t *pat_ pat_index[index].hash = hash; pat_index[index].index = idx; lf[idx].w = weight; - for (i = 0; i < LFR_DIMENSION; i++) { + for (int i = 0; i < LFR_DIMENSION; i++) { if (fscanf_s(fp, "%lf", &lf[idx].v[i]) == EOF) { cerr << "Read Error : " << filename << endl; exit(1); @@ -1513,7 +1490,7 @@ InputLargePattern( const char *filename, latent_factor_t *lf, index_hash_t *pat_ pat_index[index].hash = hash; pat_index[index].index = idx; lf[idx].w = weight; - for (i = 0; i < LFR_DIMENSION; i++) { + for (int i = 0; i < LFR_DIMENSION; i++) { if (fscanf(fp, "%lf", &lf[idx].v[i]) == EOF) { cerr << "Read Error : " << filename << endl; exit(1); diff --git a/src/UctSearch.cpp b/src/UctSearch.cpp index 8c0d5f3..3e03665 100644 --- a/src/UctSearch.cpp +++ b/src/UctSearch.cpp @@ -571,8 +571,6 @@ UctSearchGenmove( game_info_t *game, int color ) void UctSearchPondering( game_info_t *game, int color ) { - int pos; - if (!pondering_mode) { return ; } @@ -587,7 +585,7 @@ UctSearchPondering( game_info_t *game, int color ) po_info.count = 0; for (int i = 0; i < pure_board_max; i++) { - pos = onboard_pos[i]; + const int pos = onboard_pos[i]; owner[pos] = 50; owner_index[pos] = 5; candidates[pos] = true; @@ -644,13 +642,12 @@ InitializeCandidate( child_node_t *uct_child, int pos, bool ladder ) static int ExpandRoot( game_info_t *game, int color ) { + const int moves = game->moves; unsigned long long hash = game->move_hash; - unsigned int index = FindSameHashIndex(hash, color, game->moves); + unsigned int index = FindSameHashIndex(hash, color, moves); + int pos, child_num = 0, pm1 = PASS, pm2 = PASS; + bool ladder[BOARD_MAX] = { false }; child_node_t *uct_child; - int i, pos, child_num = 0; - bool ladder[BOARD_MAX] = { false }; - int pm1 = PASS, pm2 = PASS; - int moves = game->moves; // 直前の着手の座標を取り出す pm1 = game->record[moves - 1].pos; @@ -679,7 +676,7 @@ ExpandRoot( game_info_t *game, int color ) child_num = uct_node[index].child_num; - for (i = 0; i < child_num; i++) { + for (int i = 0; i < child_num; i++) { pos = uct_child[i].pos; uct_child[i].rate = 0.0; uct_child[i].flag = false; @@ -707,7 +704,7 @@ ExpandRoot( game_info_t *game, int color ) ClearUctHash(); // 空のインデックスを探す - index = SearchEmptyIndex(hash, color, game->moves); + index = SearchEmptyIndex(hash, color, moves); assert(index != uct_hash_size); @@ -728,8 +725,8 @@ ExpandRoot( game_info_t *game, int color ) child_num++; // 候補手の展開 - if (game->moves == 1) { - for (i = 0; i < first_move_candidates; i++) { + if (moves == 1) { + for (int i = 0; i < first_move_candidates; i++) { pos = first_move_candidate[i]; // 探索候補かつ合法手であれば探索対象にする if (candidates[pos] && IsLegal(game, pos, color)) { @@ -738,7 +735,7 @@ ExpandRoot( game_info_t *game, int color ) } } } else { - for (i = 0; i < pure_board_max; i++) { + for (int i = 0; i < pure_board_max; i++) { pos = onboard_pos[i]; // 探索候補かつ合法手であれば探索対象にする if (candidates[pos] && IsLegal(game, pos, color)) { @@ -771,22 +768,20 @@ ExpandRoot( game_info_t *game, int color ) static int ExpandNode( game_info_t *game, int color, int current ) { + const int moves = game->moves; unsigned long long hash = game->move_hash; - unsigned int index = FindSameHashIndex(hash, color, game->moves); - child_node_t *uct_child, *uct_sibling; - int i, pos, child_num = 0; + unsigned int index = FindSameHashIndex(hash, color, moves); + int child_num = 0, max_pos = PASS, sibling_num, pm1 = PASS, pm2 = PASS; double max_rate = 0.0; - int max_pos = PASS, sibling_num; - int pm1 = PASS, pm2 = PASS; - int moves = game->moves; - + child_node_t *uct_child, *uct_sibling; + // 合流先が検知できれば, それを返す if (index != uct_hash_size) { return index; } // 空のインデックスを探す - index = SearchEmptyIndex(hash, color, game->moves); + index = SearchEmptyIndex(hash, color, moves); assert(index != uct_hash_size); @@ -811,8 +806,8 @@ ExpandNode( game_info_t *game, int color, int current ) child_num++; // 候補手の展開 - for (i = 0; i < pure_board_max; i++) { - pos = onboard_pos[i]; + for (int i = 0; i < pure_board_max; i++) { + const int pos = onboard_pos[i]; // 探索候補でなければ除外 if (candidates[pos] && IsLegal(game, pos, color)) { InitializeCandidate(&uct_child[child_num], pos, false); @@ -835,7 +830,7 @@ ExpandNode( game_info_t *game, int color, int current ) // 兄弟ノードで一番レートの高い手を求める uct_sibling = uct_node[current].child; sibling_num = uct_node[current].child_num; - for (i = 0; i < sibling_num; i++) { + for (int i = 0; i < sibling_num; i++) { if (uct_sibling[i].pos != pm1) { if (uct_sibling[i].rate > max_rate) { max_rate = uct_sibling[i].rate; @@ -845,7 +840,7 @@ ExpandNode( game_info_t *game, int color, int current ) } // 兄弟ノードで一番レートの高い手を展開する - for (i = 0; i < child_num; i++) { + for (int i = 0; i < child_num; i++) { if (uct_child[i].pos == max_pos) { if (!uct_child[i].flag) { uct_child[i].open = true; @@ -865,16 +860,13 @@ ExpandNode( game_info_t *game, int color, int current ) static void RatingNode( game_info_t *game, int color, int index ) { - int child_num = uct_node[index].child_num; - int pos; - int moves = game->moves; - double score = 0.0; - int max_index; - double max_score; - pattern_hash_t hash_pat; + const int child_num = uct_node[index].child_num; + const int moves = game->moves; + int pos, max_index; int pat_index[3] = {0}; - double dynamic_parameter; + double score = 0.0, max_score, dynamic_parameter; bool self_atari_flag; + pattern_hash_t hash_pat; child_node_t *uct_child = uct_node[index].child; uct_features_t uct_features; @@ -963,9 +955,9 @@ RatingNode( game_info_t *game, int color, int index ) static bool InterruptionCheck( void ) { - int max = 0, second = 0; const int child_num = uct_node[current_root].child_num; const int rest = po_info.halt - po_info.count; + int max = 0, second = 0; child_node_t *uct_child = uct_node[current_root].child; if (mode != CONST_PLAYOUT_MODE && @@ -1625,12 +1617,8 @@ CopyStatistic( statistic_t *dest ) int UctSearchGenmoveCleanUp( game_info_t *game, int color ) { - int pos; - double finish_time; - int select_index; - int max_count; - double wp; - int count; + int pos, select_index, max_count, count; + double finish_time, wp; child_node_t *uct_child; thread *handle[THREAD_MAX];