Skip to main content

solverforge_solver/manager/
config.rs

1/* Configuration types for solver phases.
2
3This module contains enums for configuring different types of solver phases:
4
5- [`LocalSearchType`]: Algorithm selection for local search phases
6- [`ConstructionType`]: Algorithm selection for construction heuristic phases
7- [`PhaseConfig`]: Complete phase configuration
8*/
9
10/* Type of local search algorithm to use.
11
12Different local search algorithms have different characteristics:
13
14- [`HillClimbing`](Self::HillClimbing): Simple, fast, but can get stuck in local optima
15- [`TabuSearch`](Self::TabuSearch): Avoids revisiting recent states
16- [`SimulatedAnnealing`](Self::SimulatedAnnealing): Probabilistic acceptance of worse moves
17- [`LateAcceptance`](Self::LateAcceptance): Compares against historical scores
18
19# Examples
20
21```
22use solverforge_solver::manager::LocalSearchType;
23
24// Hill climbing - simplest approach
25let hill = LocalSearchType::HillClimbing;
26assert_eq!(hill, LocalSearchType::default());
27
28// Tabu search with memory of 10 recent moves
29let tabu = LocalSearchType::TabuSearch { tabu_size: 10 };
30
31// Simulated annealing with temperature decay
32let sa = LocalSearchType::SimulatedAnnealing {
33starting_temp: 1.0,
34decay_rate: 0.995,
35};
36
37// Late acceptance comparing to 100 steps ago
38let late = LocalSearchType::LateAcceptance { size: 100 };
39```
40*/
41#[derive(Debug, Clone, Copy, PartialEq, Default)]
42pub enum LocalSearchType {
43    /* Hill climbing: only accept improving moves.
44
45    This is the simplest local search strategy. It only accepts moves
46    that improve the score. Fast but can easily get stuck in local optima.
47
48    # Example
49
50    ```
51    use solverforge_solver::manager::LocalSearchType;
52
53    let acceptor = LocalSearchType::HillClimbing;
54    ```
55    */
56    #[default]
57    HillClimbing,
58
59    /* Tabu search: avoid recently visited states.
60
61    Maintains a list of recently made moves and forbids reversing them.
62    This helps escape local optima by forcing exploration.
63
64    # Example
65
66    ```
67    use solverforge_solver::manager::LocalSearchType;
68
69    // Keep last 7 moves in tabu list
70    let tabu = LocalSearchType::TabuSearch { tabu_size: 7 };
71    ```
72    */
73    TabuSearch {
74        // Size of the tabu list.
75        tabu_size: usize,
76    },
77
78    /* Simulated annealing: accept worse moves with decreasing probability.
79
80    Initially accepts worse moves with high probability, but as the
81    "temperature" decreases, it becomes more selective. Good for
82    escaping local optima early in the search.
83
84    # Example
85
86    ```
87    use solverforge_solver::manager::LocalSearchType;
88
89    // Start with temperature 1.0, decay by 0.1% per step
90    let sa = LocalSearchType::SimulatedAnnealing {
91    starting_temp: 1.0,
92    decay_rate: 0.999,
93    };
94    ```
95    */
96    SimulatedAnnealing {
97        // Initial temperature.
98        starting_temp: f64,
99        // Temperature decay rate per step.
100        decay_rate: f64,
101    },
102
103    /* Late acceptance: compare against score from N steps ago.
104
105    Accepts a move if the new score is better than the score from
106    N steps ago. Provides a balance between exploration and exploitation.
107
108    # Example
109
110    ```
111    use solverforge_solver::manager::LocalSearchType;
112
113    // Compare against score from 400 steps ago
114    let late = LocalSearchType::LateAcceptance { size: 400 };
115    ```
116    */
117    LateAcceptance {
118        // Number of steps to look back.
119        size: usize,
120    },
121
122    /* Value tabu search: forbid recently assigned values.
123
124    Remembers recently assigned values and forbids reassigning them.
125    Different from entity tabu in that it tracks the values themselves,
126    not the entity-variable combinations.
127
128    # Example
129
130    ```
131    use solverforge_solver::manager::LocalSearchType;
132
133    // Forbid last 5 assigned values
134    let value_tabu = LocalSearchType::ValueTabuSearch { value_tabu_size: 5 };
135    ```
136    */
137    ValueTabuSearch {
138        // Number of recent values to forbid.
139        value_tabu_size: usize,
140    },
141
142    /* Move tabu search: forbid recently made moves.
143
144    Remembers recently made moves (by hash) and forbids making the same
145    move again. Supports aspiration criterion: tabu moves can be accepted
146    if they lead to a new best solution.
147
148    # Example
149
150    ```
151    use solverforge_solver::manager::LocalSearchType;
152
153    // Forbid last 10 moves, with aspiration enabled
154    let move_tabu = LocalSearchType::MoveTabuSearch {
155    move_tabu_size: 10,
156    aspiration_enabled: true,
157    };
158    ```
159    */
160    MoveTabuSearch {
161        // Number of recent moves to forbid.
162        move_tabu_size: usize,
163        // Whether to allow tabu moves that reach new best score.
164        aspiration_enabled: bool,
165    },
166}
167
168/* Type of construction heuristic to use.
169
170Construction heuristics build an initial solution by assigning values
171to uninitialized planning variables. The type determines how values
172are selected:
173
174- [`FirstFit`](Self::FirstFit): Fast, takes first valid value
175- [`BestFit`](Self::BestFit): Slower, evaluates all options to find best
176
177# Examples
178
179```
180use solverforge_solver::manager::ConstructionType;
181
182// First fit - faster but may produce lower quality initial solution
183let fast = ConstructionType::FirstFit;
184assert_eq!(fast, ConstructionType::default());
185
186// Best fit - slower but produces better initial solution
187let best = ConstructionType::BestFit;
188```
189*/
190#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
191pub enum ConstructionType {
192    /* First fit: accept first valid assignment.
193
194    Assigns the first value that produces a feasible solution.
195    Fast but may not produce an optimal initial solution.
196
197    # Example
198
199    ```
200    use solverforge_solver::manager::ConstructionType;
201
202    let construction = ConstructionType::FirstFit;
203    ```
204    */
205    #[default]
206    FirstFit,
207
208    /* Best fit: evaluate all options, pick best score.
209
210    Evaluates all possible values and selects the one that produces
211    the best score. Slower but produces a higher quality initial solution.
212
213    # Example
214
215    ```
216    use solverforge_solver::manager::ConstructionType;
217
218    let construction = ConstructionType::BestFit;
219    ```
220    */
221    BestFit,
222}
223
224/* Configuration for a phase.
225
226This enum represents the configuration for different types of solver phases.
227Use this with the builder to configure your solving strategy.
228
229# Examples
230
231```
232use solverforge_solver::manager::{PhaseConfig, ConstructionType, LocalSearchType};
233
234// Construction phase configuration
235let construction = PhaseConfig::ConstructionHeuristic {
236construction_type: ConstructionType::BestFit,
237};
238
239// Local search phase with step limit
240let local_search = PhaseConfig::LocalSearch {
241search_type: LocalSearchType::TabuSearch { tabu_size: 7 },
242step_limit: Some(1000),
243};
244```
245*/
246#[derive(Debug, Clone)]
247pub enum PhaseConfig {
248    /* Construction heuristic phase.
249
250    Builds an initial solution by assigning values to uninitialized
251    planning variables.
252
253    # Example
254
255    ```
256    use solverforge_solver::manager::{PhaseConfig, ConstructionType};
257
258    let config = PhaseConfig::ConstructionHeuristic {
259    construction_type: ConstructionType::FirstFit,
260    };
261    ```
262    */
263    ConstructionHeuristic {
264        // Type of construction.
265        construction_type: ConstructionType,
266    },
267
268    /* Local search phase.
269
270    Improves an existing solution by exploring neighboring solutions.
271
272    # Example
273
274    ```
275    use solverforge_solver::manager::{PhaseConfig, LocalSearchType};
276
277    let config = PhaseConfig::LocalSearch {
278    search_type: LocalSearchType::HillClimbing,
279    step_limit: Some(500),
280    };
281    ```
282    */
283    LocalSearch {
284        // Type of local search.
285        search_type: LocalSearchType,
286        // Optional step limit for this phase.
287        step_limit: Option<u64>,
288    },
289}