mctrust
Production-grade Monte Carlo Tree Search for any sequential decision problem. Portfolio optimization, security scan planning, hyperparameter tuning, game AI, molecular design, compiler optimization, robot path planning — anything with a state space to explore. Implement the Environment trait and the search handles the rest.
UCT, PUCT, Gumbel MuZero, Thompson Sampling, RAVE, pluggable evaluators, multi-agent negamax, tree reuse, DAG transpositions, root parallelism, progressive widening, node & time budgets, tree visualization, checkpoint/restore.
Quick Start
use ;
let state = Optimizer ;
let config = builder.iterations.build;
let mut search = new;
let best = search.run; // Some(Action::Increase)
Two Search Engines
Environment-based (TreeSearch)
Full tree-search MCTS. Define an Environment with actions, transitions, and terminal conditions. The engine handles selection, expansion, simulation, and backpropagation.
Use for: optimization, planning, scheduling, game AI, theorem proving.
Bandit-based (BanditSearch)
Two-level MCTS for explore/exploit problems with a flat set of options grouped by category. Uses UCT+RAVE to decide which option to evaluate next.
Use for: fuzzing, A/B testing, hyperparameter search, scan strategy optimization.
use ;
let mut search = new;
for category in 0..5u64
while let Some = search.next_arm
Tree Policies
| Policy | Use Case | Exploration Constant? |
|---|---|---|
Uct |
General purpose | Yes |
Puct |
Neural-network-guided search (AlphaZero-style) | Yes |
Gumbel |
Hyperparameter-free search (MuZero-style) | No |
ThompsonSampling |
Stochastic exploration | Temperature only |
use ;
// Gumbel MuZero: same quality in ~16x fewer simulations, zero tuning
let config = builder
.iterations
.tree_policy
.build;
Pluggable Evaluator
Replace random rollouts with neural networks, domain heuristics, or any custom evaluation:
use ;
// search.with_evaluator(Arc::new(ValueNetwork { ... }));
Multi-Agent Environments
For adversarial or multi-agent environments, implement current_player() and num_players():
The engine automatically applies negamax-style reward flipping during backpropagation.
Search Control
Time Budgets
use Duration;
let config = builder
.iterations // high ceiling
.time_budget // but stop after 50ms
.build;
Arbitrary Stop Conditions (run_until)
search.run_until;
Node Budget (Memory Safety)
search.with_max_nodes; // never allocate more than 1M nodes
search.run;
Stepped Iteration
for _ in 0..100
if search.best_root_reward.unwrap_or > 0.9
Tree Reuse (Warm Start Between Decisions)
After choosing an action, re-root the tree at the chosen child's subtree instead of discarding everything. Preserves all accumulated statistics — the next search starts with a massive head start.
loop
No other Rust MCTS crate supports tree reuse.
DAG Transposition Tables
When your environment has multiple paths to the same state, enable DAG mode to merge duplicate nodes:
search.enable_dag;
search.run;
Requires implementing state_hash() on your Environment.
Root Parallelism
Lock-free linear scaling across CPU cores:
let best = search.run_parallel;
Each thread runs an independent search tree; results are merged at the root.
Diagnostics & Visualization
// Best line of actions through the tree:
let pv = search.principal_variation; // Vec<Action>
// Full intermediate state sequence along the PV:
let pv_states = search.principal_variation_states; // Vec<Environment>
// Per-action visit counts and rewards at root:
let stats = search.root_stats; // Vec<(Action, NodeStats)>
// Best action's average reward:
let reward = search.best_root_reward; // Option<f64>
// Graphviz DOT export for visual inspection:
let dot = search.export_dot;
write?;
// Then: dot -Tsvg tree.dot -o tree.svg
TOML Configuration
= 50000
= 1.41
= 100
[]
= "gumbel"
= 16
= 50.0
[]
= true
= 300.0
let config = from_toml_str?;
let config = from_toml_file?;
Feature Flags
| Feature | Default | Description |
|---|---|---|
std |
✅ | Enables rand/std — disable for #![no_std] targets |
parallel |
✅ | Root-parallel MCTS via Rayon |
dag |
✅ | Transposition tables via hashbrown |
toml |
✅ | TOML config parsing |
Use Cases
MCTS generalizes to any sequential decision problem:
- Finance — portfolio rebalancing, options hedging, order execution strategies
- Security — scan strategy optimization, fuzzing prioritization, attack path planning
- Operations — scheduling, resource allocation, supply chain optimization
- Science — molecular design, protein folding, experiment design
- Engineering — compiler optimization, chip placement, network routing
- Games — board games, real-time strategy, puzzle solving
The Environment trait is fully generic. If you can define states, actions, and outcomes, mctrust can search it.
Contributing
Pull requests welcome. Open a PR if you find a bug, a better API, or a rough edge.
License
MIT. Copyright 2026 CORUM COLLECTIVE LLC.