some fixes

This commit is contained in:
Mylloon 2023-03-29 19:44:35 +02:00
parent 700b8cffef
commit ec89afcb7e
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -231,20 +231,18 @@ void bt_t::mcts_expansion(bt_node_t *root) {
}
bool bt_t::mcts_simulation(bt_node_t *node) {
bt_t b_copy = *this;
int me = (b_copy.turn % 2 == 0) ? WHITE : BLACK;
int me = (turn % 2 == 0) ? WHITE : BLACK;
// try to play my move
b_copy.play(node->move);
play(node->move);
// then play randomly 'til the game is over
while (b_copy.endgame() != EMPTY) {
b_copy.play(b_copy.get_rand_move());
while (endgame() != EMPTY) {
play(get_rand_move());
}
// if i won
return ((b_copy.turn % 2 == 0) ? WHITE : BLACK == me) ? true : false;
return ((turn % 2 == 0) ? WHITE : BLACK == me) ? true : false;
}
void bt_t::mcts_back_propagation(bt_node_t *simulated, bool won) {
@ -273,14 +271,17 @@ bt_move_t bt_t::get_mcts_move(double max_time) {
// MCTS
while (elapsed.count() < max_time) {
// Copy board
bt_t copy_b = *this;
// Selection
auto selected = mcts_selection(tree);
auto selected = copy_b.mcts_selection(tree);
// Expansion
mcts_expansion(selected);
copy_b.mcts_expansion(selected);
// Simulation
bool is_win = mcts_simulation(selected);
bool is_win = copy_b.mcts_simulation(selected);
// Update
mcts_back_propagation(selected, is_win);
@ -291,9 +292,10 @@ bt_move_t bt_t::get_mcts_move(double max_time) {
// Select best move
bt_node_t *best_node = nullptr;
int best_score = -1;
int best_score = 0;
for (auto i : tree->children) {
if (i->nb_simulation > best_score) {
best_score = i->nb_simulation;
best_node = i;
}
}