Skip to main content

entrenar/search/mcts/
mod.rs

1//! Monte Carlo Tree Search implementation for code generation.
2//!
3//! # Overview
4//!
5//! MCTS is a heuristic search algorithm that builds a search tree iteratively
6//! through four phases: Selection, Expansion, Simulation, and Backpropagation.
7//!
8//! For code generation:
9//! - **State**: Partial AST (Abstract Syntax Tree)
10//! - **Action**: Transform rules (e.g., add statement, wrap in loop)
11//! - **Reward**: Compilation success (binary: 0 or 1)
12//!
13//! # Example
14//!
15//! ```rust
16//! use entrenar::search::{MctsSearch, MctsConfig, State, Action};
17//!
18//! // Define your state and action spaces
19//! #[derive(Clone, PartialEq, Eq, Hash)]
20//! struct CodeState {
21//!     ast_tokens: Vec<String>,
22//! }
23//!
24//! impl State for CodeState {
25//!     fn is_terminal(&self) -> bool {
26//!         self.ast_tokens.iter().any(|t| t == "EOF")
27//!     }
28//! }
29//!
30//! // Create MCTS searcher with default config
31//! let config = MctsConfig::default();
32//! // let mcts = MctsSearch::new(initial_state, action_space, config);
33//! ```
34
35mod config;
36mod node;
37mod search;
38mod traits;
39mod tree;
40
41// Re-export all public types
42pub use config::MctsConfig;
43pub use node::{Node, NodeId, NodeStats};
44pub use search::{MctsResult, MctsSearch, MctsStats};
45pub use traits::{Action, ActionSpace, PolicyNetwork, State, StateSpace};
46pub use tree::SearchTree;
47
48/// Type alias for reward values (typically 0.0 to 1.0)
49pub type Reward = f64;