reimplement time constraint

This commit is contained in:
Mylloon 2023-04-11 16:06:29 +02:00
parent 07a38b5c2b
commit 92cd90dd2f
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 12 additions and 4 deletions

View file

@ -1,6 +1,7 @@
#ifndef MYBT_H
#define MYBT_H
#include <chrono>
#include <random>
#define WHITE 0
@ -88,7 +89,7 @@ struct bt_t {
bool black_can_move_forward(int _line, int _col);
bool black_can_move_left(int _line, int _col);
bt_move_t get_mcts_move(int);
bt_move_t get_mcts_move(double);
bt_move_t get_rand_move();
bool can_play(bt_move_t _m);
void play(bt_move_t _m);

View file

@ -58,7 +58,7 @@ void genmove() {
printf("= \n\n");
return;
}
bt_move_t m = B.get_mcts_move(100000);
bt_move_t m = B.get_mcts_move(1);
B.play(m);
if (verbose) {
m.print(stderr, white_turn, B.nbl);

View file

@ -283,7 +283,11 @@ void print_vec(bt_t board, std::vector<bt_node_t *> v) {
}
}
bt_move_t bt_t::get_mcts_move(int max_it) {
bt_move_t bt_t::get_mcts_move(double max_time) {
// Time constraint
auto now = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed{};
// Init tree
bt_node_t *tree = new bt_node_t();
tree->parent = nullptr;
@ -295,7 +299,7 @@ bt_move_t bt_t::get_mcts_move(int max_it) {
mcts_expansion(tree);
// MCTS
for (int it = 0; it < max_it; ++it) {
while (elapsed.count() < max_time) {
// Copy board
bt_t copy_b = *this;
@ -310,6 +314,9 @@ bt_move_t bt_t::get_mcts_move(int max_it) {
// Update
mcts_back_propagation(result, is_win);
// Update elapsed time
elapsed = std::chrono::steady_clock::now() - now;
}
// Select best move