From ffed7158f32512d1189fb7b7e06ad5d262998c97 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 29 Mar 2023 17:26:33 +0200 Subject: [PATCH] WIP: add mcts basics * expansion: ok --- TP2/includes/mybt.h | 24 +++++++++++++++--------- TP2/src/mybt.cpp | 35 +++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/TP2/includes/mybt.h b/TP2/includes/mybt.h index 0967b44..f0c7590 100644 --- a/TP2/includes/mybt.h +++ b/TP2/includes/mybt.h @@ -44,6 +44,15 @@ struct bt_move_t { #define MAX_LINES 10 #define MAX_COLS 10 +// Node MCTS +struct bt_node_t { + bt_node_t *parent; + std::vector children; + bt_move_t move; + int wins; + int nb_simulation; +}; + // rules reminder : // pieces moves from 1 square in diag and in front // pieces captures only in diag @@ -91,6 +100,12 @@ struct bt_t { std::string mkH2(); long long int mkH3(); + // MCTS + void mcts_selection(bt_node_t *); + void mcts_expansion(bt_node_t *); + void mcts_simulation(); + void mcts_back_propagation(); + // déclarées mais non définies double eval(); bt_move_t minimax(double _sec); @@ -109,13 +124,4 @@ struct bt_t { } }; -// Node MCTS -struct bt_node_t { - bt_node_t *parent; - std::vector children; - bt_move_t move; - int wins; - int nb_simulation; -}; - #endif /* MYBT_H */ diff --git a/TP2/src/mybt.cpp b/TP2/src/mybt.cpp index 9190273..5fc00ca 100644 --- a/TP2/src/mybt.cpp +++ b/TP2/src/mybt.cpp @@ -193,22 +193,45 @@ bt_move_t bt_t::get_rand_move() { return moves[r]; } -bt_move_t bt_t::get_mcts_move(double max_time) { +void bt_t::mcts_selection(bt_node_t *node) { (void)node; } + +void bt_t::mcts_expansion(bt_node_t *root) { update_moves(); + for (int i = 0; i < nb_moves; i++) { + // Child + bt_node_t *tmp = new bt_node_t; + tmp->move = moves[i]; + tmp->wins = 0; + tmp->parent = root; + tmp->nb_simulation = 0; + + // Add child to root + root->children.push_back(tmp); + } +} + +void bt_t::mcts_simulation(void) {} + +void bt_t::mcts_back_propagation(void) {} + +bt_move_t bt_t::get_mcts_move(double max_time) { // Init tree bt_node_t *tree = new bt_node_t(); - tree->children = {}; tree->parent = NULL; - tree->nb_simulation = 0; + tree->children = {}; tree->wins = 0; + tree->nb_simulation = 0; + mcts_expansion(tree); // Copy board bt_t b_copy = *this; - // MCTS + // Time constraint auto now = std::chrono::steady_clock::now(); std::chrono::duration elapsed{}; + + // MCTS while (elapsed.count() < max_time) { // Selection std::cerr << "selection\n"; @@ -227,14 +250,14 @@ bt_move_t bt_t::get_mcts_move(double max_time) { } // Select best move - bt_node_t *best_node = NULL; + /* bt_node_t *best_node = NULL; int best_score = -1; for (auto i : tree->children) { if (i->nb_simulation > best_score) { best_node = i; } } - // return best_node->move; + return best_node->move; */ return moves[(static_cast(rand())) % nb_moves]; // TMP }