Skip to content

Commit

Permalink
Merge pull request #86 from kobanium/ver-9.0
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
kobanium authored May 13, 2018
2 parents 90d97a6 + 2105e7b commit 5f8310c
Showing 1 changed file with 44 additions and 65 deletions.
109 changes: 44 additions & 65 deletions src/UctSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1275,20 +1275,14 @@ RateComp( const void *a, const void *b )
static int
SelectMaxUcbChild( int current, int color )
{
child_node_t *uct_child = uct_node[current].child;
const int child_num = uct_node[current].child_num;
int max_child = 0;
const int sum = uct_node[current].move_count;
double p, max_value;
double ucb_value;
int max_index;
double max_rate;
double dynamic_parameter;
rate_order_t order[PURE_BOARD_MAX + 1];
int pos;
int width;
const double ucb_bonus_weight = bonus_weight * sqrt(bonus_equivalence / (sum + bonus_equivalence));

child_node_t *uct_child = uct_node[current].child;
int max_child = 0, pos, width;
double p, max_value, ucb_value, dynamic_parameter;
rate_order_t order[PURE_BOARD_MAX + 1];

// 128回ごとにOwnerとCriticalityでソートし直す
if ((sum & 0x7f) == 0 && sum != 0) {
int o_index[UCT_CHILD_MAX], c_index[UCT_CHILD_MAX];
Expand Down Expand Up @@ -1319,8 +1313,8 @@ SelectMaxUcbChild( int current, int color )
// Progressive Wideningの閾値を超えたら,
// レートが最大の手を読む候補を1手追加
if (sum > pw[uct_node[current].width]) {
max_index = -1;
max_rate = 0;
int max_index = -1;
double max_rate = 0.0;
for (int i = 0; i < child_num; i++) {
if (uct_child[i].flag == false) {
pos = uct_child[i].pos;
Expand Down Expand Up @@ -1375,11 +1369,11 @@ static void
Statistic( game_info_t *game, int winner )
{
const char *board = game->board;
int pos, color;

for (int i = 0; i < pure_board_max; i++) {
pos = onboard_pos[i];
color = board[pos];
const int pos = onboard_pos[i];
int color = board[pos];

if (color == S_EMPTY) color = territory[Pat3(game->pat, pos)];

std::atomic_fetch_add(&statistic[pos].colors[color], 1);
Expand All @@ -1397,12 +1391,13 @@ static void
UpdateNodeStatistic( game_info_t *game, int winner, statistic_t *node_statistic )
{
const char *board = game->board;
int pos, color;


for (int i = 0; i < pure_board_max; i++) {
pos = onboard_pos[i];
color = board[pos];
const int pos = onboard_pos[i];
int color = board[pos];

if (color == S_EMPTY) color = territory[Pat3(game->pat, pos)];

std::atomic_fetch_add(&node_statistic[pos].colors[color], 1);
if (color == winner) {
std::atomic_fetch_add(&node_statistic[pos].colors[0], 1);
Expand All @@ -1417,20 +1412,17 @@ UpdateNodeStatistic( game_info_t *game, int winner, statistic_t *node_statistic
static void
CalculateCriticalityIndex( uct_node_t *node, statistic_t *node_statistic, int color, int *index )
{
double win, lose;
const int other = FLIP_COLOR(color);
const int count = node->move_count;
const int child_num = node->child_num;
int pos;
const double win = (double)node->win / node->move_count;
const double lose = 1.0 - win;
double tmp;

win = (double)node->win / node->move_count;
lose = 1.0 - win;

index[0] = 0;

for (int i = 1; i < child_num; i++) {
pos = node->child[i].pos;
const int pos = node->child[i].pos;

tmp = ((double)node_statistic[pos].colors[0] / count) -
((((double)node_statistic[pos].colors[color] / count) * win)
Expand All @@ -1447,16 +1439,13 @@ CalculateCriticalityIndex( uct_node_t *node, statistic_t *node_statistic, int co
static void
CalculateCriticality( int color )
{
int pos;
double tmp;
const int other = FLIP_COLOR(color);
double win, lose;

win = (double)uct_node[current_root].win / uct_node[current_root].move_count;
lose = 1.0 - win;
const double win = (double)uct_node[current_root].win / uct_node[current_root].move_count;
const double lose = 1.0 - win;
double tmp;

for (int i = 0; i < pure_board_max; i++) {
pos = onboard_pos[i];
const int pos = onboard_pos[i];

tmp = ((float)statistic[pos].colors[0] / po_info.count) -
((((float)statistic[pos].colors[color] / po_info.count)*win)
Expand All @@ -1475,17 +1464,16 @@ CalculateCriticality( int color )
static void
CalculateOwnerIndex( uct_node_t *node, statistic_t *node_statistic, int color, int *index )
{
int pos;
const int count = node->move_count;
const int child_num = node->child_num;

index[0] = 0;

for (int i = 1; i < child_num; i++){
pos = node->child[i].pos;
const int pos = node->child[i].pos;
index[i] = (int)((double)node_statistic[pos].colors[color] * 10.0 / count + 0.5);
if (index[i] > OWNER_MAX - 1) index[i] = OWNER_MAX - 1;
if (index[i] < 0) index[pos] = 0;
if (index[i] < 0) index[i] = 0;
}
}

Expand All @@ -1496,13 +1484,11 @@ CalculateOwnerIndex( uct_node_t *node, statistic_t *node_statistic, int color, i
static void
CalculateOwner( int color, int count )
{
int pos;

for (int i = 0; i < pure_board_max; i++){
pos = onboard_pos[i];
const int pos = onboard_pos[i];
owner_index[pos] = (int)((double)statistic[pos].colors[color] * 10.0 / count + 0.5);
if (owner_index[pos] > OWNER_MAX - 1) owner_index[pos] = OWNER_MAX - 1;
if (owner_index[pos] < 0) owner_index[pos] = 0;
if (owner_index[pos] < 0) owner_index[pos] = 0;
}
}

Expand Down Expand Up @@ -1553,7 +1539,6 @@ CalculateNextPlayouts( game_info_t *game, int color, double best_wp, double fini
int
UctAnalyze( game_info_t *game, int color )
{
int pos;
thread *handle[THREAD_MAX];

// 探索情報をクリア
Expand All @@ -1568,10 +1553,6 @@ UctAnalyze( game_info_t *game, int color )

current_root = ExpandRoot(game, color);

for (int i = 0; i < pure_board_max; i++) {
pos = onboard_pos[i];
}

po_info.halt = 10000;

for (int i = 0; i < threads; i++) {
Expand All @@ -1581,20 +1562,18 @@ UctAnalyze( game_info_t *game, int color )
handle[i] = new std::thread(ParallelUctSearch, &t_arg[i]);
}


for (int i = 0; i < threads; i++) {
handle[i]->join();
delete handle[i];
}

int x, y, black = 0, white = 0;
double own;
int black = 0, white = 0;

for (y = board_start; y <= board_end; y++) {
for (x = board_start; x <= board_end; x++) {
pos = POS(x, y);
own = (double)statistic[pos].colors[S_BLACK] / uct_node[current_root].move_count;
if (own > 0.5) {
for (int y = board_start; y <= board_end; y++) {
for (int x = board_start; x <= board_end; x++) {
const int pos = POS(x, y);
const double ownership_value = (double)statistic[pos].colors[S_BLACK] / uct_node[current_root].move_count;
if (ownership_value > 0.5) {
black++;
} else {
white++;
Expand All @@ -1614,9 +1593,8 @@ UctAnalyze( game_info_t *game, int color )
void
OwnerCopy( int *dest )
{
int pos;
for (int i = 0; i < pure_board_max; i++) {
pos = onboard_pos[i];
const int pos = onboard_pos[i];
dest[pos] = (int)((double)uct_node[current_root].statistic[pos].colors[my_color] / uct_node[current_root].move_count * 100);
}
}
Expand All @@ -1628,9 +1606,8 @@ OwnerCopy( int *dest )
void
CopyCriticality( double *dest )
{
int pos;
for (int i = 0; i < pure_board_max; i++) {
pos = onboard_pos[i];
const int pos = onboard_pos[i];
dest[pos] = criticality[pos];
}
}
Expand Down Expand Up @@ -1670,8 +1647,7 @@ UctSearchGenmoveCleanUp( game_info_t *game, int color )
current_root = ExpandRoot(game, color);

if (uct_node[current_root].child_num <= 1) {
pos = PASS;
return pos;
return PASS;
}

for (int i = 0; i < pure_board_max; i++) {
Expand Down Expand Up @@ -1733,14 +1709,17 @@ UctSearchGenmoveCleanUp( game_info_t *game, int color )
}
}

if (count == 0) pos = PASS;
else pos = uct_child[select_index].pos;

if ((double)uct_child[select_index].win / uct_child[select_index].move_count < RESIGN_THRESHOLD) {
if (count == 0) {
pos = PASS;
} else {
pos = uct_child[select_index].pos;
}

if ((double)uct_child[select_index].win / uct_child[select_index].move_count < RESIGN_THRESHOLD) {
return PASS;
} else {
return pos;
}

return pos;
}


Expand Down

0 comments on commit 5f8310c

Please sign in to comment.