Skip to content

Commit

Permalink
Rename MctsStrategy -> SearchConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasmarsh committed Feb 19, 2024
1 parent ecf0d6f commit 8e4913b
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 96 deletions.
4 changes: 2 additions & 2 deletions benches/ttt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};

use mcts::games::ttt;
use mcts::strategies::mcts::util;
use mcts::strategies::mcts::MctsStrategy;
use mcts::strategies::mcts::SearchConfig;
use mcts::strategies::mcts::TreeSearch;
use mcts::strategies::Search;

Expand All @@ -14,7 +14,7 @@ fn ponder(c: &mut Criterion) {
group.bench_with_input(BenchmarkId::from_parameter(n), &n, |b, &n| {
b.iter(|| {
let mut ts =
TicTacToeTS::default().strategy(MctsStrategy::default().max_iterations(n));
TicTacToeTS::default().strategy(SearchConfig::default().max_iterations(n));

ts.choose_action(&ttt::HashedPosition::new());
});
Expand Down
6 changes: 3 additions & 3 deletions demo/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use mcts::strategies::mcts::meta::QuasiBestFirst;
use mcts::strategies::mcts::select;
use mcts::strategies::mcts::simulate;
use mcts::strategies::mcts::util;
use mcts::strategies::mcts::MctsStrategy;
use mcts::strategies::mcts::SearchConfig;
use mcts::strategies::mcts::TreeSearch;

use mcts::games::druid::Druid;
Expand Down Expand Up @@ -49,8 +49,8 @@ pub fn debug(book: &OpeningBook<Move>) {

fn make_mcts() -> TreeSearch<Druid, util::Ucb1Mast> {
TreeSearch::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(MAX_ITER)
.max_playout_depth(PLAYOUT_DEPTH)
.max_time(Duration::from_secs(MAX_TIME_SECS))
Expand Down
50 changes: 25 additions & 25 deletions demo/druid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use mcts::games::druid::{Druid, State};
use mcts::strategies::mcts::select::SelectStrategy;
use mcts::strategies::mcts::simulate;
use mcts::strategies::mcts::util;
use mcts::strategies::mcts::MctsStrategy;
use mcts::strategies::mcts::SearchConfig;
use mcts::strategies::mcts::TreeSearch;
use mcts::strategies::mcts::{backprop, select, Strategy};
use mcts::util::Verbosity;
Expand All @@ -26,8 +26,8 @@ fn main() {
assert_eq!(Duration::default(), Duration::from_secs(0));

let grave: TreeSearch<Druid, util::McGrave> = TreeSearch::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(MAX_ITER)
.max_playout_depth(PLAYOUT_DEPTH)
.max_time(Duration::from_secs(MAX_TIME_SECS))
Expand All @@ -41,8 +41,8 @@ fn main() {
.verbose(VERBOSE);

let mut brave: TreeSearch<Druid, util::McBrave> = TreeSearch::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(MAX_ITER)
.max_playout_depth(PLAYOUT_DEPTH)
.max_time(Duration::from_secs(MAX_TIME_SECS))
Expand All @@ -52,8 +52,8 @@ fn main() {
.verbose(VERBOSE);

let mut ucb1_grave: TreeSearch<Druid, util::Ucb1Grave> = TreeSearch::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(MAX_ITER)
.max_playout_depth(PLAYOUT_DEPTH)
.max_time(Duration::from_secs(MAX_TIME_SECS))
Expand All @@ -75,8 +75,8 @@ fn main() {
// 'threshold': 211,
// })
let mut ucb1_grave_mast: TreeSearch<Druid, util::Ucb1GraveMast> = TreeSearch::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(MAX_ITER)
.max_playout_depth(PLAYOUT_DEPTH)
.max_time(Duration::from_secs(MAX_TIME_SECS))
Expand All @@ -92,8 +92,8 @@ fn main() {
.verbose(VERBOSE);

let mut amaf: TreeSearch<Druid, util::ScalarAmaf> = TreeSearch::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(MAX_ITER)
.max_playout_depth(PLAYOUT_DEPTH)
.max_time(Duration::from_secs(MAX_TIME_SECS))
Expand All @@ -106,8 +106,8 @@ fn main() {
.verbose(VERBOSE);

let mut amaf_mast: TreeSearch<Druid, util::ScalarAmafMast> = TreeSearch::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(MAX_ITER)
.max_playout_depth(PLAYOUT_DEPTH)
.max_time(Duration::from_secs(MAX_TIME_SECS))
Expand All @@ -121,8 +121,8 @@ fn main() {
.verbose(VERBOSE);

let mut uct: TreeSearch<Druid, util::Ucb1> = TreeSearch::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(MAX_ITER)
.max_playout_depth(PLAYOUT_DEPTH)
.max_time(Duration::from_secs(MAX_TIME_SECS))
Expand All @@ -134,8 +134,8 @@ fn main() {
.verbose(VERBOSE);

let mut uct_mast_low: TreeSearch<Druid, util::Ucb1Mast> = TreeSearch::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(MAX_ITER)
.max_playout_depth(PLAYOUT_DEPTH)
.max_time(Duration::from_secs(MAX_TIME_SECS))
Expand All @@ -148,8 +148,8 @@ fn main() {
.verbose(VERBOSE);

let mut uct_mast_high: TreeSearch<Druid, util::Ucb1Mast> = TreeSearch::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(MAX_ITER)
.max_playout_depth(PLAYOUT_DEPTH)
.max_time(Duration::from_secs(MAX_TIME_SECS))
Expand All @@ -162,8 +162,8 @@ fn main() {
.verbose(VERBOSE);

let mut tuned: TreeSearch<Druid, util::Ucb1Tuned> = TreeSearch::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(MAX_ITER)
.max_playout_depth(PLAYOUT_DEPTH)
.max_time(Duration::from_secs(MAX_TIME_SECS))
Expand All @@ -175,8 +175,8 @@ fn main() {
.verbose(VERBOSE);

let meta: TreeSearch<Druid, util::MetaMcts> = TreeSearch::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(MAX_ITER)
.max_playout_depth(PLAYOUT_DEPTH)
.max_time(Duration::from_secs(MAX_TIME_SECS))
Expand All @@ -185,8 +185,8 @@ fn main() {
exploration_constant: C_TUNED,
})
.simulate(simulate::MetaMcts {
inner: TreeSearch::default().strategy(
MctsStrategy::default()
inner: TreeSearch::default().config(
SearchConfig::default()
.max_iterations(3)
.max_playout_depth(PLAYOUT_DEPTH)
.max_time(Duration::default())
Expand Down
10 changes: 5 additions & 5 deletions demo/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use mcts::games::druid::State;
use mcts::strategies::mcts::select;
use mcts::strategies::mcts::simulate;
use mcts::strategies::mcts::util;
use mcts::strategies::mcts::MctsStrategy;
use mcts::strategies::mcts::SearchConfig;
use mcts::strategies::mcts::TreeSearch;
use mcts::util::round_robin_multiple;
use mcts::util::AnySearch;
Expand Down Expand Up @@ -70,8 +70,8 @@ fn calc_cost(results: Vec<mcts::util::Result>) -> f64 {

fn make_opponent(seed: u64) -> TS<util::Ucb1> {
TS::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(MAX_ITER)
.max_playout_depth(PLAYOUT_DEPTH)
.max_time(Duration::from_secs(MAX_TIME_SECS))
Expand All @@ -86,8 +86,8 @@ fn make_opponent(seed: u64) -> TS<util::Ucb1> {

fn make_candidate(args: Args) -> TS<util::Ucb1GraveMast> {
TS::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(MAX_ITER)
.max_playout_depth(PLAYOUT_DEPTH)
.max_time(Duration::from_secs(MAX_TIME_SECS))
Expand Down
23 changes: 11 additions & 12 deletions demo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use mcts::games::nim;
use mcts::games::ttt;
use mcts::strategies::flat_mc::FlatMonteCarloStrategy;
use mcts::strategies::mcts::util;
use mcts::strategies::mcts::MctsStrategy;
use mcts::strategies::mcts::SearchConfig;
use mcts::strategies::mcts::TreeSearch;
use mcts::strategies::random::Random;
use mcts::strategies::Search;
Expand Down Expand Up @@ -40,16 +40,16 @@ fn expansion_test() {
type TS = TreeSearch<ttt::BiddingTicTacToe, util::Ucb1>;

let expand5 = TS::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(10000)
.expand_threshold(5),
)
.name("expand5");

let expand0 = TS::default()
.strategy(
MctsStrategy::default()
.config(
SearchConfig::default()
.max_iterations(10000)
.expand_threshold(0),
)
Expand Down Expand Up @@ -93,8 +93,7 @@ struct BattleConfig {
fn battle_nim_mcts(config: &BattleConfig) {
for samples in &config.samples_per_move {
println!("samples per move: {}", samples);
let mut mcts = NimMCTS::default();
mcts.strategy.max_iterations = *samples;
let mut mcts = NimMCTS::default().config(SearchConfig::default().max_iterations(*samples));
let mut flat_mc = NimFlatMC::new().set_samples_per_move(*samples as u32);

let mut fst = Vec::with_capacity(100);
Expand Down Expand Up @@ -123,7 +122,7 @@ fn battle_ttt(config: &BattleConfig) {
println!("samples per move: {}", samples);
let mut flat_mc = TttFlatMC::new().set_samples_per_move(*samples as u32);
let mut mcts = TttMCTS::default();
mcts.strategy.max_iterations = *samples;
mcts.config.max_iterations = *samples;

let mut fst = Vec::with_capacity(100);
let mut snd = Vec::with_capacity(100);
Expand All @@ -149,8 +148,8 @@ fn battle_ttt(config: &BattleConfig) {
fn demo_mcts() {
let mut mcts = TttMCTS::default();
mcts.verbose = true;
mcts.strategy.max_time = Duration::from_secs(5);
mcts.strategy.max_iterations = usize::MAX;
mcts.config.max_time = Duration::from_secs(5);
mcts.config.max_iterations = usize::MAX;
let mut random: Random<TicTacToe> = Random::new();
let mut player = ttt::Piece::X;

Expand All @@ -176,8 +175,8 @@ fn demo_mcts() {
fn demo_nim() {
let mut mcts = NimMCTS::default();
mcts.verbose = true;
mcts.strategy.max_time = Duration::from_secs(5);
mcts.strategy.max_iterations = usize::MAX;
mcts.config.max_time = Duration::from_secs(5);
mcts.config.max_iterations = usize::MAX;
let mut random: Random<Nim> = Random::new();
let mut player = nim::Player::Black;

Expand Down
2 changes: 1 addition & 1 deletion src/games/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mod tests {
#[test]
pub fn test_unit() {
let mut search: TreeSearch<UnitGame, util::Ucb1> = TreeSearch::default();
search.strategy.max_iterations = 10;
search.config.max_iterations = 10;
let state = Unit::default();
search.choose_action(&state);
#[allow(clippy::unit_arg)]
Expand Down
4 changes: 2 additions & 2 deletions src/strategies/mcts/meta.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::index;
use super::MctsStrategy;
use super::SearchConfig;
use super::Strategy;
use super::TreeSearch;
use crate::game::Action;
Expand Down Expand Up @@ -183,7 +183,7 @@ impl<G, S> QuasiBestFirst<G, S>
where
G: Game,
S: Strategy<G>,
MctsStrategy<G, S>: Default,
SearchConfig<G, S>: Default,
{
pub fn new(book: OpeningBook<G::A>, search: TreeSearch<G, S>, rng: SmallRng) -> Self {
// The default value here is 0.5, but the Chaslot paper noted the difficulty
Expand Down
Loading

0 comments on commit 8e4913b

Please sign in to comment.