Compare commits
8 Commits
6a33818238
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 3676838082 | |||
| 6aa9002e92 | |||
| 76051cd76b | |||
| a7102a0e44 | |||
| a2711f341f | |||
| 48c93fa197 | |||
| 854772a63d | |||
| 44ef9ebdd8 |
Generated
+161
-40
@@ -3,34 +3,113 @@
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.9.1"
|
||||
name = "anstyle"
|
||||
version = "1.0.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
|
||||
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b4388bee8683e3d04af747c73422af53102d2bd24d9eadb6cbc100baef4b43f8"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.1"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
"terminal_size",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
|
||||
|
||||
[[package]]
|
||||
name = "condtype"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf0a07a401f374238ab8e2f11a104d2851bf9ce711ec69804834de8af45c7af"
|
||||
|
||||
[[package]]
|
||||
name = "divan"
|
||||
version = "0.1.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a405457ec78b8fe08b0e32b4a3570ab5dff6dd16eb9e76a5ee0a9d9cbd898933"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"clap",
|
||||
"condtype",
|
||||
"divan-macros",
|
||||
"libc",
|
||||
"regex-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "divan-macros"
|
||||
version = "0.1.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9556bc800956545d6420a640173e5ba7dfa82f38d3ea5a167eb555bc69ac3323"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.3.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.3.3"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
|
||||
checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"r-efi",
|
||||
"wasi",
|
||||
"wasip2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.174"
|
||||
version = "0.2.186"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
|
||||
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
@@ -43,18 +122,18 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.95"
|
||||
version = "1.0.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
|
||||
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.40"
|
||||
version = "1.0.46"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
|
||||
checksum = "dfbc457d0c7a0759a614551b11a6409e5951f6c7537be1f1b7682b9ae9230368"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
@@ -67,9 +146,9 @@ checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.9.1"
|
||||
version = "0.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97"
|
||||
checksum = "44c5af06bb1b7d3216d91932aed5265164bf384dc89cd6ba05cf59a35f5f76ea"
|
||||
dependencies = [
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
@@ -87,26 +166,46 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.9.3"
|
||||
version = "0.9.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
|
||||
checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-lite"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cab834c73d247e67f4fae452806d17d3c7501756d98c8808d7c9c7aa7d18f973"
|
||||
|
||||
[[package]]
|
||||
name = "rustic_mcts"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"divan",
|
||||
"rand",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.103"
|
||||
name = "rustix"
|
||||
version = "1.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e4307e30089d6fd6aff212f2da3a1f9e32f3223b1f010fb09b7c95f90f3ca1e8"
|
||||
checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.118"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b9ae57f904213ebb649ce6895b8a66c66f0203b9319718f69a5612a065b1422"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@@ -114,19 +213,29 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "2.0.12"
|
||||
name = "terminal_size"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
|
||||
checksum = "230a1b821ccbd75b185820a1f1ff7b14d21da1e442e22c0863ea5f08771a8874"
|
||||
dependencies = [
|
||||
"rustix",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "2.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "2.0.12"
|
||||
version = "2.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
|
||||
checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@@ -135,42 +244,54 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.18"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
|
||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.14.2+wasi-0.2.4"
|
||||
name = "wasip2"
|
||||
version = "1.0.4+wasi-0.2.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
|
||||
checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487"
|
||||
dependencies = [
|
||||
"wit-bindgen-rt",
|
||||
"wit-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen-rt"
|
||||
version = "0.39.0"
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen"
|
||||
version = "0.57.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e"
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.8.26"
|
||||
version = "0.8.53"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
|
||||
checksum = "75726053136156d419e285b9b7eddaaea9e3fea6ce32eed44a89901f0bd98de1"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.8.26"
|
||||
version = "0.8.53"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
|
||||
checksum = "4714fd92cf900833d49538023a9b3915155210801d1c1169eba513b2addefd71"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
||||
@@ -14,3 +14,10 @@ categories = ["algorithms", "data-structures"]
|
||||
[dependencies]
|
||||
rand = "~0.9"
|
||||
thiserror = "~2.0"
|
||||
|
||||
[dev-dependencies]
|
||||
divan = "~0.1"
|
||||
|
||||
[[bench]]
|
||||
name = "e2e"
|
||||
harness = false
|
||||
|
||||
+187
@@ -0,0 +1,187 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rustic_mcts::policy::backprop::BackpropagationPolicy;
|
||||
use rustic_mcts::policy::decision::DecisionPolicy;
|
||||
use rustic_mcts::policy::selection::SelectionPolicy;
|
||||
use rustic_mcts::policy::simulation::SimulationPolicy;
|
||||
use rustic_mcts::{Action, GameState, MCTSConfig, RewardVal, MCTS};
|
||||
|
||||
fn main() {
|
||||
// Run registered benchmarks.
|
||||
divan::main();
|
||||
}
|
||||
|
||||
#[divan::bench]
|
||||
fn tic_tac_toe() {
|
||||
// Set up a new game
|
||||
let mut game = TicTacToe::new();
|
||||
|
||||
// Create MCTS configuration
|
||||
let config = 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,
|
||||
};
|
||||
|
||||
// Main game loop
|
||||
while !game.is_terminal() {
|
||||
// Create a new MCTS search
|
||||
let mut mcts = MCTS::new(game.clone(), &config);
|
||||
|
||||
// Find the best move
|
||||
match mcts.search() {
|
||||
Ok(action) => {
|
||||
// Apply the AI's move
|
||||
game = game.state_after_action(&action);
|
||||
}
|
||||
Err(_) => {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Players in Tic-Tac-Toe
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
enum Player {
|
||||
X,
|
||||
O,
|
||||
}
|
||||
|
||||
impl rustic_mcts::Player for Player {}
|
||||
|
||||
/// Tic-Tac-Toe move
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
struct Move {
|
||||
/// Board position index (0-8)
|
||||
index: usize,
|
||||
}
|
||||
|
||||
impl Action for Move {}
|
||||
|
||||
/// Tic-Tac-Toe game state
|
||||
#[derive(Debug, Clone)]
|
||||
struct TicTacToe {
|
||||
/// Board representation (None = empty, Some(Player) = occupied)
|
||||
board: [Option<Player>; 9],
|
||||
|
||||
/// Current player's turn
|
||||
current_player: Player,
|
||||
|
||||
/// Number of moves played so far
|
||||
moves_played: usize,
|
||||
}
|
||||
|
||||
impl TicTacToe {
|
||||
/// Creates a new empty Tic-Tac-Toe board
|
||||
fn new() -> Self {
|
||||
TicTacToe {
|
||||
board: [None; 9],
|
||||
current_player: Player::X,
|
||||
moves_played: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the winner of the game, if any
|
||||
fn get_winner(&self) -> Option<Player> {
|
||||
// Check rows
|
||||
for row in 0..3 {
|
||||
let i = row * 3;
|
||||
if self.board[i].is_some()
|
||||
&& self.board[i] == self.board[i + 1]
|
||||
&& self.board[i] == self.board[i + 2]
|
||||
{
|
||||
return self.board[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Check columns
|
||||
for col in 0..3 {
|
||||
if self.board[col].is_some()
|
||||
&& self.board[col] == self.board[col + 3]
|
||||
&& self.board[col] == self.board[col + 6]
|
||||
{
|
||||
return self.board[col];
|
||||
}
|
||||
}
|
||||
|
||||
// Check diagonals
|
||||
if self.board[0].is_some()
|
||||
&& self.board[0] == self.board[4]
|
||||
&& self.board[0] == self.board[8]
|
||||
{
|
||||
return self.board[0];
|
||||
}
|
||||
if self.board[2].is_some()
|
||||
&& self.board[2] == self.board[4]
|
||||
&& self.board[2] == self.board[6]
|
||||
{
|
||||
return self.board[2];
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn reward_for_player(&self, player: &Player) -> RewardVal {
|
||||
if let Some(winner) = self.get_winner() {
|
||||
if winner == *player {
|
||||
return 1.0; // Win
|
||||
} else {
|
||||
return 0.0; // Loss
|
||||
}
|
||||
}
|
||||
|
||||
// Draw
|
||||
0.5
|
||||
}
|
||||
}
|
||||
|
||||
impl GameState for TicTacToe {
|
||||
type Action = Move;
|
||||
type Player = Player;
|
||||
|
||||
fn get_legal_actions(&self) -> Vec<Self::Action> {
|
||||
let mut actions = Vec::new();
|
||||
for i in 0..9 {
|
||||
if self.board[i].is_none() {
|
||||
actions.push(Move { index: i });
|
||||
}
|
||||
}
|
||||
actions
|
||||
}
|
||||
|
||||
fn state_after_action(&self, action: &Self::Action) -> Self {
|
||||
let mut new_state = self.clone();
|
||||
|
||||
// Make the move
|
||||
new_state.board[action.index] = Some(self.current_player);
|
||||
new_state.moves_played = self.moves_played + 1;
|
||||
|
||||
// Switch player
|
||||
new_state.current_player = match self.current_player {
|
||||
Player::X => Player::O,
|
||||
Player::O => Player::X,
|
||||
};
|
||||
|
||||
new_state
|
||||
}
|
||||
|
||||
fn is_terminal(&self) -> bool {
|
||||
self.get_winner().is_some() || self.moves_played == 9
|
||||
}
|
||||
|
||||
fn rewards_for_players(&self) -> HashMap<Self::Player, RewardVal> {
|
||||
HashMap::from_iter(vec![
|
||||
(Player::X, self.reward_for_player(&Player::X)),
|
||||
(Player::O, self.reward_for_player(&Player::O)),
|
||||
])
|
||||
}
|
||||
|
||||
fn get_current_player(&self) -> &Self::Player {
|
||||
&self.current_player
|
||||
}
|
||||
}
|
||||
@@ -40,7 +40,6 @@ fn main() {
|
||||
// Find the best move
|
||||
match mcts.search() {
|
||||
Ok(action) => {
|
||||
mcts.print_tree();
|
||||
println!(
|
||||
"AI chooses: {} (row {}, col {})",
|
||||
action.index,
|
||||
@@ -85,11 +84,7 @@ struct Move {
|
||||
index: usize,
|
||||
}
|
||||
|
||||
impl Action for Move {
|
||||
fn id(&self) -> usize {
|
||||
self.index
|
||||
}
|
||||
}
|
||||
impl Action for Move {}
|
||||
|
||||
/// Tic-Tac-Toe game state
|
||||
#[derive(Clone)]
|
||||
@@ -153,6 +148,19 @@ impl TicTacToe {
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn reward_for_player(&self, player: &Player) -> RewardVal {
|
||||
if let Some(winner) = self.get_winner() {
|
||||
if winner == *player {
|
||||
return 1.0; // Win
|
||||
} else {
|
||||
return 0.0; // Loss
|
||||
}
|
||||
}
|
||||
|
||||
// Draw
|
||||
0.5
|
||||
}
|
||||
}
|
||||
|
||||
impl GameState for TicTacToe {
|
||||
@@ -189,19 +197,6 @@ impl GameState for TicTacToe {
|
||||
self.get_winner().is_some() || self.moves_played == 9
|
||||
}
|
||||
|
||||
fn reward_for_player(&self, player: &Self::Player) -> RewardVal {
|
||||
if let Some(winner) = self.get_winner() {
|
||||
if winner == *player {
|
||||
return 1.0; // Win
|
||||
} else {
|
||||
return 0.0; // Loss
|
||||
}
|
||||
}
|
||||
|
||||
// Draw
|
||||
0.5
|
||||
}
|
||||
|
||||
fn rewards_for_players(&self) -> HashMap<Self::Player, RewardVal> {
|
||||
HashMap::from_iter(vec![
|
||||
(Player::X, self.reward_for_player(&Player::X)),
|
||||
|
||||
+14
-18
@@ -119,11 +119,7 @@ struct Move {
|
||||
index: usize,
|
||||
}
|
||||
|
||||
impl Action for Move {
|
||||
fn id(&self) -> usize {
|
||||
self.index
|
||||
}
|
||||
}
|
||||
impl Action for Move {}
|
||||
|
||||
/// Tic-Tac-Toe game state
|
||||
#[derive(Clone)]
|
||||
@@ -195,6 +191,19 @@ impl TicTacToe {
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn reward_for_player(&self, player: &Player) -> RewardVal {
|
||||
if let Some(winner) = self.get_winner() {
|
||||
if winner == *player {
|
||||
return 1.0; // Win
|
||||
} else {
|
||||
return 0.0; // Loss
|
||||
}
|
||||
}
|
||||
|
||||
// Draw
|
||||
0.5
|
||||
}
|
||||
}
|
||||
|
||||
impl GameState for TicTacToe {
|
||||
@@ -231,19 +240,6 @@ impl GameState for TicTacToe {
|
||||
self.get_winner().is_some() || self.moves_played == 9
|
||||
}
|
||||
|
||||
fn reward_for_player(&self, player: &Self::Player) -> RewardVal {
|
||||
if let Some(winner) = self.get_winner() {
|
||||
if winner == *player {
|
||||
return 1.0; // Win
|
||||
} else {
|
||||
return 0.0; // Loss
|
||||
}
|
||||
}
|
||||
|
||||
// Draw
|
||||
0.5
|
||||
}
|
||||
|
||||
fn rewards_for_players(&self) -> HashMap<Self::Player, RewardVal> {
|
||||
HashMap::from_iter(vec![
|
||||
(Player::X, self.reward_for_player(&Player::X)),
|
||||
|
||||
+9
-2
@@ -75,8 +75,15 @@ impl<'conf, S: GameState + std::fmt::Debug> MCTS<'conf, S> {
|
||||
if !selected_node.state.is_terminal() {
|
||||
self.expand(selected_id);
|
||||
let children: &Vec<usize> = &self.arena.get_node(selected_id).children;
|
||||
let random_child: usize = *children.choose(&mut rand::rng()).unwrap();
|
||||
selected_id = random_child;
|
||||
match children.choose(&mut rand::rng()) {
|
||||
Some(&random_child) => {
|
||||
selected_id = random_child;
|
||||
}
|
||||
None => {
|
||||
// We ran out of nodes
|
||||
return Err(MCTSError::NonTerminalGame);
|
||||
}
|
||||
}
|
||||
}
|
||||
let rewards = self.simulate(selected_id);
|
||||
self.backprop(selected_id, &rewards);
|
||||
|
||||
@@ -36,15 +36,15 @@ pub fn select_best_child<S: GameState>(
|
||||
exploration_constant,
|
||||
parent_visits,
|
||||
node_a.visits,
|
||||
node_a.rewards(player),
|
||||
node_a.reward_average(player),
|
||||
node_a.reward_variance(player),
|
||||
);
|
||||
let ucb_b = ucb1_tuned_value(
|
||||
exploration_constant,
|
||||
parent_visits,
|
||||
node_b.visits,
|
||||
node_b.rewards(player),
|
||||
node_b.reward_average(player),
|
||||
node_b.reward_variance(player),
|
||||
);
|
||||
ucb_a.partial_cmp(&ucb_b).unwrap()
|
||||
})
|
||||
@@ -57,41 +57,31 @@ pub fn ucb1_tuned_value(
|
||||
exploration_constant: f64,
|
||||
parent_visits: u64,
|
||||
child_visits: u64,
|
||||
child_rewards: Option<&Vec<RewardVal>>,
|
||||
reward_avg: RewardVal,
|
||||
reward_variance: f64,
|
||||
) -> RewardVal {
|
||||
match child_rewards {
|
||||
None => {
|
||||
RewardVal::INFINITY // Always explore nodes that have never been visited
|
||||
}
|
||||
Some(child_rewards) => {
|
||||
if child_visits == 0 {
|
||||
RewardVal::INFINITY // Always explore nodes that have never been visited
|
||||
} else {
|
||||
let parent_visits: RewardVal = parent_visits as RewardVal;
|
||||
let child_visits: RewardVal = child_visits as RewardVal;
|
||||
if child_visits == 0 {
|
||||
RewardVal::INFINITY // Always explore nodes that have never been visited
|
||||
} else {
|
||||
let parent_visits: RewardVal = parent_visits as RewardVal;
|
||||
let child_visits: RewardVal = child_visits as RewardVal;
|
||||
|
||||
// N: number of visits to the parent node
|
||||
// n: number of visits to the child node
|
||||
// x_i: reward of the ith visit to the child node
|
||||
// X: average reward of the child
|
||||
// C: exploration constant
|
||||
//
|
||||
// UCB1-Tuned = X + C * sqrt(Ln(parent_visits) / child_visits * min(1/4, V_n)
|
||||
// V(n) = sum(x_i^2)/n - X^2 + sqrt(2*ln(N)/n)
|
||||
let exploitation = reward_avg;
|
||||
let mut variance = (child_rewards.iter().map(|&x| x * x).sum::<RewardVal>()
|
||||
/ child_visits)
|
||||
- (reward_avg * reward_avg)
|
||||
+ (2.0 * parent_visits.ln() / child_visits).sqrt();
|
||||
if variance > 0.25 {
|
||||
variance = 0.25;
|
||||
}
|
||||
let exploration =
|
||||
exploration_constant * (parent_visits.ln() / child_visits * variance).sqrt();
|
||||
|
||||
exploitation + exploration
|
||||
}
|
||||
// N: number of visits to the parent node
|
||||
// n: number of visits to the child node
|
||||
// x_i: reward of the ith visit to the child node
|
||||
// X: average reward of the child
|
||||
// C: exploration constant
|
||||
//
|
||||
// UCB1-Tuned = X + C * sqrt(Ln(parent_visits) / child_visits * min(1/4, V_n)
|
||||
// V(n) = sum(x_i^2)/n - X^2 + sqrt(2*ln(N)/n)
|
||||
let exploitation = reward_avg;
|
||||
let mut variance = reward_variance + (2.0 * parent_visits.ln() / child_visits).sqrt();
|
||||
if variance > 0.25 {
|
||||
variance = 0.25;
|
||||
}
|
||||
let exploration =
|
||||
exploration_constant * (parent_visits.ln() / child_visits * variance).sqrt();
|
||||
|
||||
exploitation + exploration
|
||||
}
|
||||
}
|
||||
|
||||
+5
-11
@@ -40,11 +40,11 @@ pub trait GameState: Clone + Debug {
|
||||
/// instead should modify a copy of the state and return that.
|
||||
fn state_after_action(&self, action: &Self::Action) -> Self;
|
||||
|
||||
/// Returns the reward from the perspective of the given player for the game state
|
||||
/// Returns the rewards for all players from their perspective for the game state
|
||||
///
|
||||
/// This evaluates the current state from the perspective of the given player, and
|
||||
/// returns the reward indicating how good of a result the given state is for the
|
||||
/// player.
|
||||
/// This evaluates the current state from the perspective of each player, and
|
||||
/// returns a HashMap mapping each player to the result of this evaluation, which
|
||||
/// we call the reward.
|
||||
///
|
||||
/// This is used in the MCTS backpropagation and simulation phases to evaluate
|
||||
/// the value of a given node in the search tree.
|
||||
@@ -55,9 +55,6 @@ pub trait GameState: Clone + Debug {
|
||||
/// - 0.0 => a loss for the player
|
||||
///
|
||||
/// Other values can be used for relative wins or losses
|
||||
fn reward_for_player(&self, player: &Self::Player) -> RewardVal;
|
||||
|
||||
/// Returns the rewards for all players at the current state
|
||||
fn rewards_for_players(&self) -> HashMap<Self::Player, RewardVal>;
|
||||
|
||||
/// Returns the player whose turn it is for the game state
|
||||
@@ -72,10 +69,7 @@ pub trait GameState: Clone + Debug {
|
||||
///
|
||||
/// An action is dependent upon the specific game being defined, and includes
|
||||
/// things like moves, attacks, and other decisions.
|
||||
pub trait Action: Clone + Debug {
|
||||
/// Returns a uniqie identifier for this action
|
||||
fn id(&self) -> usize;
|
||||
}
|
||||
pub trait Action: Clone + Debug {}
|
||||
|
||||
/// Trait used for players participating in a game
|
||||
pub trait Player: Clone + Debug + PartialEq + Eq + Hash {}
|
||||
|
||||
+12
-8
@@ -69,10 +69,10 @@ impl<S: GameState> Node<S> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rewards(&self, player: &S::Player) -> Option<&Vec<RewardVal>> {
|
||||
pub fn reward_variance(&self, player: &S::Player) -> f64 {
|
||||
match self.player_view.get(player) {
|
||||
Some(pv) => Some(&pv.rewards),
|
||||
None => None,
|
||||
Some(pv) => pv.weighted_variance / self.visits as f64,
|
||||
None => 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,9 +82,10 @@ impl<S: GameState> Node<S> {
|
||||
|
||||
pub fn record_player_reward(&mut self, player: S::Player, reward: RewardVal) {
|
||||
let pv = self.player_view.entry(player).or_default();
|
||||
pv.rewards.push(reward);
|
||||
let prev_reward_average = pv.reward_average;
|
||||
pv.reward_sum += reward;
|
||||
pv.reward_average = pv.reward_sum / pv.rewards.len() as f64;
|
||||
pv.reward_average = pv.reward_sum / self.visits as f64;
|
||||
pv.weighted_variance += (reward - prev_reward_average) * (reward - pv.reward_average);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,8 +99,11 @@ pub struct PlayerRewardView {
|
||||
/// The average reward from simulations through this node, often called the node value
|
||||
pub reward_average: RewardVal,
|
||||
|
||||
/// The rewards we have gotten so far for simulations through this node
|
||||
pub rewards: Vec<RewardVal>,
|
||||
/// The weighted variance from simulations through this node
|
||||
///
|
||||
/// This is used to calculate online sample variance.
|
||||
/// See Donald E. Knuth. Seminumerical Algorithms, volume 2 of The Art of Computer Programming, chapter 4.2.2, page 232
|
||||
pub weighted_variance: f64,
|
||||
}
|
||||
|
||||
impl Default for PlayerRewardView {
|
||||
@@ -107,7 +111,7 @@ impl Default for PlayerRewardView {
|
||||
PlayerRewardView {
|
||||
reward_sum: 0.0,
|
||||
reward_average: 0.0,
|
||||
rewards: Vec::new(),
|
||||
weighted_variance: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user