17884f4b90
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
68 lines
2.3 KiB
Rust
68 lines
2.3 KiB
Rust
use crate::policy::backprop::BackpropagationPolicy;
|
|
use crate::policy::decision::DecisionPolicy;
|
|
use crate::policy::selection::SelectionPolicy;
|
|
use crate::policy::simulation::SimulationPolicy;
|
|
use crate::state::GameState;
|
|
use std::time::Duration;
|
|
|
|
/// Configuration for the MCTS algorithm
|
|
#[derive(Debug)]
|
|
pub struct MCTSConfig<S: GameState> {
|
|
/// The maximum number of iterations to run when searching
|
|
///
|
|
/// The search will stop after the given number of iterations, even if there
|
|
/// is search time has not exceeded `max_time`.
|
|
pub max_iterations: usize,
|
|
|
|
/// The maximum time to run the search
|
|
///
|
|
/// If set, the search will stop after this duration even if the maximum
|
|
/// iterations hasn't been reached.
|
|
pub max_time: Option<Duration>,
|
|
|
|
/// The size to initially allocate for the search tree
|
|
///
|
|
/// This pre-allocates memory for the search tree which ensures contiguous
|
|
/// memory and improves performance by preventing the resizing of tree
|
|
/// as we explore.
|
|
pub tree_size_allocation: usize,
|
|
|
|
/// The selection policy
|
|
///
|
|
/// This dictates the path through which the game tree is searched. As such
|
|
/// the policy has a large impact on the overall aglorthm exeuction
|
|
pub selection_policy: SelectionPolicy<S>,
|
|
|
|
/// The simulation policy
|
|
///
|
|
/// This dictates the game siluation when expanding and evaluating the
|
|
/// search tree. Random is generally a good default.
|
|
pub simulation_policy: SimulationPolicy<S>,
|
|
|
|
/// The backpropagation policy
|
|
///
|
|
/// This dictates how the results of the simulation playouts are propagated
|
|
/// back up the tree.
|
|
pub backprop_policy: BackpropagationPolicy<S>,
|
|
|
|
/// The decision policy
|
|
///
|
|
/// This dictates how the MCTS algorithm determines its final decision
|
|
/// after iterating through the search tree
|
|
pub decision_policy: DecisionPolicy,
|
|
}
|
|
|
|
impl<S: GameState> Default for MCTSConfig<S> {
|
|
fn default() -> Self {
|
|
MCTSConfig {
|
|
max_iterations: 10_000,
|
|
max_time: None,
|
|
tree_size_allocation: 10_000,
|
|
selection_policy: SelectionPolicy::UCB1Tuned(1.414),
|
|
simulation_policy: SimulationPolicy::Random,
|
|
backprop_policy: BackpropagationPolicy::Standard,
|
|
decision_policy: DecisionPolicy::MostVisits,
|
|
}
|
|
}
|
|
}
|