mctrust 0.4.0

Universal search & planning toolkit — MCTS, bandit search, pluggable evaluators, tree reuse, DAG transpositions, root parallelism. Define an Environment, search handles the rest.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//! Integration tests for mctrust — end-to-end workflows.

use mctrust::*;
use std::sync::Arc;
use std::time::Duration;

// =============================================================================
// NumberLine Game (single-agent optimization)
// =============================================================================

#[derive(Clone, serde::Serialize, serde::Deserialize)]
struct NumberLine {
    value: i32,
    target: i32,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
enum NumberAction {
    Increment,
    Decrement,
}

impl Environment for NumberLine {
    type Action = NumberAction;

    fn legal_actions(&self) -> Vec<NumberAction> {
        vec![NumberAction::Increment, NumberAction::Decrement]
    }

    fn apply(&mut self, action: &NumberAction) {
        match action {
            NumberAction::Increment => self.value += 1,
            NumberAction::Decrement => self.value -= 1,
        }
    }

    fn evaluate(&self) -> Outcome {
        if self.value == self.target {
            Outcome::Success(Reward::WIN)
        } else if (self.value - self.target).abs() > 15 {
            Outcome::Failure
        } else {
            Outcome::Ongoing
        }
    }

    fn heuristic(&self) -> Heuristic {
        let dist = (self.value - self.target).abs() as f64;
        Heuristic::from_reward(Reward::new(1.0 - (dist / 15.0).min(1.0)))
    }

    fn max_depth(&self) -> Option<usize> {
        Some(15)
    }
}

fn numberline_search_config(iterations: usize) -> SearchConfig {
    SearchConfig::builder()
        .iterations(iterations)
        .max_depth(15)
        .heuristic_weight(1.0)
        .rave(RaveConfig {
            enabled: false,
            bias: 300.0,
        })
        .build()
}

#[test]
fn integration_numberline_positive_target() {
    let game = NumberLine {
        value: 0,
        target: 5,
    };
    let config = numberline_search_config(2_000);
    let mut search = TreeSearch::with_seed(game, config, 42);
    let best = search.run();
    assert_eq!(best, Some(NumberAction::Increment));
}

#[test]
fn integration_numberline_negative_target() {
    let game = NumberLine {
        value: 0,
        target: -5,
    };
    let config = numberline_search_config(2_000);
    let mut search = TreeSearch::with_seed(game, config, 42);
    let best = search.run();
    assert_eq!(best, Some(NumberAction::Decrement));
}

#[test]
fn integration_numberline_tree_reuse() {
    let game = NumberLine {
        value: 0,
        target: 5,
    };
    let config = numberline_search_config(1_000);
    let mut search = TreeSearch::with_seed(game, config, 42);
    search.run();

    let pv = search.principal_variation();
    assert!(!pv.is_empty());

    let first = pv[0].clone();
    let tree_size_before = search.tree_size();
    assert!(search.advance_to_action(&first));

    assert!(search.tree_size() <= tree_size_before);
    let states = search.principal_variation_states();
    assert_eq!(
        states[0].value,
        if first == NumberAction::Increment {
            1
        } else {
            -1
        }
    );
}

#[test]
fn integration_numberline_principal_variation_states() {
    let game = NumberLine {
        value: 0,
        target: 5,
    };
    let config = numberline_search_config(1_000);
    let mut search = TreeSearch::with_seed(game, config, 42);
    search.run();

    let states = search.principal_variation_states();
    assert!(states.len() >= 2);
    assert_eq!(states[0].value, 0);
}

#[test]
fn integration_numberline_with_evaluator() {
    struct NumberLineEval;
    impl Evaluator<NumberLine> for NumberLineEval {
        fn evaluate(&self, env: &NumberLine) -> Reward {
            let dist = (env.value - env.target).abs() as f64;
            Reward::new(1.0 - (dist / 15.0).min(1.0))
        }
    }

    let game = NumberLine {
        value: 0,
        target: 5,
    };
    let config = SearchConfig::builder().iterations(500).build();
    let mut search = TreeSearch::with_seed(game, config, 42);
    search.with_evaluator(Arc::new(NumberLineEval));
    let best = search.run();
    assert_eq!(best, Some(NumberAction::Increment));
}

// =============================================================================
// Bandit Hyperparameter Search Simulation
// =============================================================================

#[test]
fn integration_bandit_hyperparameter_search() {
    let mut search = BanditSearch::new_seeded(BanditConfig::builder().max_pulls(100).build(), 42);

    // 10 configurations across 3 model families
    for i in 0..30u64 {
        search.add_arm(i, (i / 10) as u32);
    }

    while let Some(arm) = search.next_arm() {
        // Simulate validation accuracy: family 0 = 0.7, family 1 = 0.8, family 2 = 0.6
        let reward = if arm < 10 {
            0.7
        } else if arm < 20 {
            0.8
        } else {
            0.6
        };
        search.observe(arm, reward);
    }

    let stats = search.group_stats();
    assert_eq!(stats.len(), 3);

    // Family 1 should have the highest average reward
    let best_family = stats
        .iter()
        .max_by(|a, b| a.average_reward.partial_cmp(&b.average_reward).unwrap())
        .unwrap();
    assert_eq!(best_family.group_id, 1);
}

#[test]
fn integration_bandit_with_signals() {
    let mut weights = std::collections::HashMap::new();
    weights.insert("accuracy".to_string(), 1.0);
    weights.insert("latency_ms".to_string(), -0.01);

    let mut search = BanditSearch::new_seeded(
        BanditConfig::builder()
            .scalarizer(Scalarizer {
                signal_weights: weights,
                default_weight: 0.0,
            })
            .build(),
        42,
    );

    for i in 0..5u64 {
        search.add_arm(i, 0);
    }

    for i in 0..5u64 {
        let arm = search.next_arm().unwrap();
        let accuracy = 0.8 + (i as f64) * 0.02;
        let latency = 100.0 - (i as f64) * 5.0;
        search.observe_with_signals(arm, &[("accuracy", accuracy), ("latency_ms", latency)]);
    }

    let stats = search.group_stats();
    assert_eq!(stats[0].visits, 5);
}

#[test]
fn integration_bandit_reweight_online() {
    let mut search = BanditSearch::new_seeded(BanditConfig::default(), 42);
    search.add_arm(0, 0);
    search.add_arm(1, 0);

    // First pull with default scalarizer
    let arm1 = search.next_arm().unwrap();
    search.observe_with_signals(arm1, &[("feature_a", 1.0)]);

    // Reweight online
    search.reweight_signals(&[("feature_a", 2.0), ("feature_b", -1.0)]);

    let arm2 = search.next_arm().unwrap();
    search.observe_with_signals(arm2, &[("feature_a", 1.0), ("feature_b", 1.0)]);

    let stats = search.group_stats();
    assert_eq!(stats[0].visits, 2);
}

// =============================================================================
// TreeSearch Policy Integration Tests
// =============================================================================

#[test]
fn integration_treesearch_uct_policy() {
    let game = NumberLine {
        value: 0,
        target: 3,
    };
    let config = SearchConfig::builder()
        .iterations(1_000)
        .max_depth(15)
        .heuristic_weight(1.0)
        .rave(RaveConfig {
            enabled: false,
            bias: 300.0,
        })
        .tree_policy(TreePolicy::Uct)
        .build();
    let mut search = TreeSearch::with_seed(game, config, 42);
    let best = search.run();
    assert_eq!(best, Some(NumberAction::Increment));
}

#[test]
fn integration_treesearch_puct_policy() {
    #[derive(Clone)]
    struct PriorNumberLine {
        value: i32,
        target: i32,
    }

    #[derive(Clone, Debug, PartialEq, Eq, Hash)]
    enum PriorAction {
        Increment,
        Decrement,
    }

    impl Environment for PriorNumberLine {
        type Action = PriorAction;
        fn legal_actions(&self) -> Vec<PriorAction> {
            vec![PriorAction::Increment, PriorAction::Decrement]
        }
        fn apply(&mut self, action: &PriorAction) {
            match action {
                PriorAction::Increment => self.value += 1,
                PriorAction::Decrement => self.value -= 1,
            }
        }
        fn evaluate(&self) -> Outcome {
            if self.value == self.target {
                Outcome::Success(Reward::WIN)
            } else if (self.value - self.target).abs() > 10 {
                Outcome::Failure
            } else {
                Outcome::Ongoing
            }
        }
        fn action_priors(&self, actions: &[PriorAction]) -> Option<Vec<f64>> {
            Some(
                actions
                    .iter()
                    .map(|a| match a {
                        PriorAction::Increment => 0.9,
                        PriorAction::Decrement => 0.1,
                    })
                    .collect(),
            )
        }
    }

    let game = PriorNumberLine {
        value: 0,
        target: 3,
    };
    let config = SearchConfig::builder()
        .iterations(1_000)
        .tree_policy(TreePolicy::Puct { prior_weight: 2.0 })
        .build();
    let mut search = TreeSearch::with_seed(game, config, 42);
    let best = search.run();
    assert_eq!(best, Some(PriorAction::Increment));
}

#[test]
fn integration_treesearch_thompson_policy() {
    let game = NumberLine {
        value: 0,
        target: 3,
    };
    let config = SearchConfig::builder()
        .iterations(1_000)
        .tree_policy(TreePolicy::ThompsonSampling { temperature: 0.5 })
        .build();
    let mut search = TreeSearch::with_seed(game, config, 42);
    let best = search.run();
    assert_eq!(best, Some(NumberAction::Increment));
}

#[test]
fn integration_treesearch_gumbel_policy() {
    let game = NumberLine {
        value: 0,
        target: 3,
    };
    let config = SearchConfig::builder()
        .iterations(1_000)
        .tree_policy(TreePolicy::Gumbel {
            sampled_actions: 16,
            max_completions_coeff: 50.0,
        })
        .build();
    let mut search = TreeSearch::with_seed(game, config, 42);
    let best = search.run();
    assert_eq!(best, Some(NumberAction::Increment));
}

// =============================================================================
// DOT Export Integration
// =============================================================================

#[test]
fn integration_dot_export_basic() {
    let game = NumberLine {
        value: 0,
        target: 3,
    };
    let config = SearchConfig::builder().iterations(50).build();
    let mut search = TreeSearch::with_seed(game, config, 42);
    search.run();

    let dot = search.export_dot(3);
    assert!(dot.starts_with("digraph mctrust {"));
    assert!(dot.contains("n0"));
    assert!(dot.ends_with("}\n"));
}

#[test]
fn integration_dot_export_depth_zero() {
    let game = NumberLine {
        value: 0,
        target: 3,
    };
    let config = SearchConfig::builder().iterations(50).build();
    let mut search = TreeSearch::with_seed(game, config, 42);
    search.run();

    let dot = search.export_dot(0);
    assert!(dot.starts_with("digraph mctrust {"));
    assert!(dot.ends_with("}\n"));
}

// =============================================================================
// Checkpoint / Restore Integration
// =============================================================================

#[test]
fn integration_checkpoint_restore_treesearch() {
    let game = NumberLine {
        value: 0,
        target: 3,
    };
    let config = SearchConfig::builder().iterations(100).build();
    let mut search = TreeSearch::with_seed(game, config, 42);
    search.run();

    let size_before = search.tree_size();
    let sims_before = search.total_simulations();
    let cp = search.checkpoint();

    let restored: TreeSearch<NumberLine> = TreeSearch::restore(cp);
    assert_eq!(restored.tree_size(), size_before);
    assert_eq!(restored.total_simulations(), sims_before);
}

#[test]
fn integration_checkpoint_restore_bandit() {
    let mut search = BanditSearch::new_seeded(BanditConfig::default(), 42);
    for i in 0..20u64 {
        search.add_arm(i, (i / 5) as u32);
    }
    for _ in 0..10 {
        if let Some(arm) = search.next_arm() {
            search.observe(arm, 0.5);
        }
    }

    let cp = search.checkpoint();
    let restored = BanditSearch::restore(cp);
    assert_eq!(restored.total_pulls(), 10);

    let stats = restored.group_stats();
    assert_eq!(stats.len(), 4);
}

// =============================================================================
// Progressive Widening Integration
// =============================================================================

#[test]
fn integration_progressive_widening_limits_expansion() {
    #[derive(Clone)]
    struct WideEnv(i32);
    #[derive(Clone, Debug, PartialEq, Eq, Hash)]
    struct WideAction(i32);

    impl Environment for WideEnv {
        type Action = WideAction;
        fn legal_actions(&self) -> Vec<WideAction> {
            (0..100).map(WideAction).collect()
        }
        fn apply(&mut self, action: &WideAction) {
            self.0 += action.0;
        }
        fn evaluate(&self) -> Outcome {
            if self.0.abs() > 50 {
                Outcome::Terminal(Reward::WIN)
            } else {
                Outcome::Ongoing
            }
        }
    }

    let config = SearchConfig::builder()
        .iterations(50)
        .progressive_widening(ProgressiveWideningConfig {
            minimum_children: 1,
            coefficient: 1.0,
            exponent: 0.5,
        })
        .build();
    let mut search = TreeSearch::with_seed(WideEnv(0), config, 42);
    search.run();

    // Root should not have all 100 children expanded
    let stats = search.root_stats();
    assert!(stats.len() < 100);
}

// =============================================================================
// Time Budget Integration
// =============================================================================

#[test]
fn integration_time_budget_stops_early() {
    let game = NumberLine {
        value: 0,
        target: 3,
    };
    let mut config = SearchConfig::builder().iterations(10_000_000).build();
    config.time_budget = Some(Duration::from_millis(10));
    let mut search = TreeSearch::with_seed(game, config, 42);
    search.run();

    assert!(search.total_simulations() > 0);
    assert!(search.total_simulations() < 10_000_000);
}

// =============================================================================
// run_until Integration
// =============================================================================

#[test]
fn integration_run_until_predicate() {
    let game = NumberLine {
        value: 0,
        target: 3,
    };
    let config = SearchConfig::builder().iterations(100_000).build();
    let mut search = TreeSearch::with_seed(game, config, 42);

    search.run_until(|s| s.total_simulations() >= 250);
    assert!(search.total_simulations() >= 250);
}

#[test]
fn integration_run_until_reward_threshold() {
    let game = NumberLine {
        value: 0,
        target: 3,
    };
    let config = SearchConfig::builder().iterations(100_000).build();
    let mut search = TreeSearch::with_seed(game, config, 42);

    search
        .run_until(|s| s.best_root_reward().unwrap_or(0.0) > 0.5 || s.total_simulations() >= 5000);
    assert!(search.total_simulations() >= 1);
}