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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0
//! [`VariationGenerator`] trait for parameter variation strategies.
//!
//! Implement this trait to plug a custom search strategy into [`ExperimentEngine`].
//! Three built-in implementations are provided:
//!
//! - [`GridStep`] — systematic sweep through all discrete grid points.
//! - [`Random`] — uniform random sampling within parameter bounds.
//! - [`Neighborhood`] — perturbation around the current best configuration.
//!
//! [`ExperimentEngine`]: crate::ExperimentEngine
//! [`GridStep`]: crate::GridStep
//! [`Random`]: crate::Random
//! [`Neighborhood`]: crate::Neighborhood
use HashSet;
use ConfigSnapshot;
use Variation;
/// Maximum number of retry attempts before a generator gives up on a rejection-sampling
/// loop (the space is considered effectively exhausted). Shared by [`Random`] and
/// [`Neighborhood`], both of which resample until an unvisited variation is found.
///
/// [`Random`]: crate::Random
/// [`Neighborhood`]: crate::Neighborhood
pub const MAX_RETRIES: usize = 1000;
/// A strategy for generating parameter variations one at a time.
///
/// Each call to [`VariationGenerator::next`] must produce a variation that changes
/// exactly one parameter from the baseline. The caller ([`ExperimentEngine`]) is
/// responsible for tracking visited variations and passing them back via `visited`.
///
/// Implementations hold mutable state (position cursor, RNG seed) and must be
/// both `Send` and `Sync` so that [`ExperimentEngine`] can be used with
/// `tokio::spawn`. The engine loop accesses the generator exclusively via `&mut self`,
/// so no concurrent access occurs in practice.
///
/// # Implementing a Custom Generator
///
/// ```rust
/// use std::collections::HashSet;
/// use zeph_experiments::{ConfigSnapshot, ParameterKind, Variation, VariationValue, VariationGenerator};
///
/// /// Always suggests temperature = 0.5, then exhausts.
/// struct FixedSuggestion;
///
/// impl VariationGenerator for FixedSuggestion {
/// fn next(&mut self, _baseline: &ConfigSnapshot, visited: &HashSet<Variation>) -> Option<Variation> {
/// let v = Variation {
/// parameter: ParameterKind::Temperature,
/// value: VariationValue::from(0.5_f64),
/// };
/// if visited.contains(&v) { None } else { Some(v) }
/// }
///
/// fn name(&self) -> &'static str { "fixed" }
/// }
///
/// fn main() {
/// let mut variation_gen = FixedSuggestion;
/// let baseline = ConfigSnapshot::default();
/// let mut visited = HashSet::new();
/// let first = variation_gen.next(&baseline, &visited).unwrap();
/// visited.insert(first);
/// assert!(variation_gen.next(&baseline, &visited).is_none());
/// }
/// ```
///
/// [`ExperimentEngine`]: crate::ExperimentEngine