add time constraint

This commit is contained in:
Mylloon 2023-03-29 16:01:13 +02:00
parent f2b585958a
commit f18a070a7a
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 12 additions and 8 deletions

View file

@ -1,6 +1,7 @@
#ifndef MYBT_H
#define MYBT_H
#include <chrono>
#include <random>
#define WHITE 0
@ -79,7 +80,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();
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();
bt_move_t m = B.get_mcts_move(1);
B.play(m);
if (verbose) {
m.print(stderr, white_turn, B.nbl);

View file

@ -1,4 +1,5 @@
#include "../includes/mybt.h"
#include <iostream>
void bt_t::init(int _nbl, int _nbc) {
if (_nbl > MAX_LINES || _nbc > MAX_COLS) {
@ -192,30 +193,32 @@ bt_move_t bt_t::get_rand_move() {
return moves[r];
}
bt_move_t bt_t::get_mcts_move() {
bt_move_t bt_t::get_mcts_move(double max_time) {
update_moves();
// Init tree
bt_node_t *tree = new bt_node_t;
bt_node_t *tree = new bt_node_t();
tree->children = {};
tree->parent = NULL;
tree->nb_simulation = 0;
tree->wins = 0;
// MCTS
while (true) {
//
auto delta = std::chrono::duration<double>();
while (delta.count() < max_time) {
std::cerr << delta.count() << "\n";
}
// Select best move
bt_node_t *best_node;
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<int>(rand())) % nb_moves]; // TMP
}
bool bt_t::can_play(bt_move_t _m) {