Working MCTS implementation

This is a basic working implementation of the MCTS algorithm. Though
currently the algorithm is slow compared with other implemenations, and
makes sub-optimal choices when playing tic-tac-toe. Therefore some
modifications are needed
This commit is contained in:
2025-06-23 13:46:04 -07:00
parent 197a46996a
commit 17884f4b90
18 changed files with 1416 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
//! Random play simulation policy
//!
//! Actions are chosen at random
use crate::state::GameState;
use crate::tree::node::{Node, RewardVal};
use rand::prelude::SliceRandom;
use std::collections::HashMap;
pub fn simulate<S: GameState>(node: &Node<S>) -> HashMap<S::Player, RewardVal> {
let mut state: S = node.state.clone();
while !state.is_terminal() {
let legal_actions = state.get_legal_actions();
let action = legal_actions.choose(&mut rand::thread_rng()).unwrap();
state = state.state_after_action(&action);
}
state.rewards_for_players()
}