Skip to main content

solverforge_solver/scope/
phase.rs

1// Phase-level scope.
2
3use std::time::Duration;
4use std::time::Instant;
5
6use solverforge_core::domain::PlanningSolution;
7use solverforge_scoring::{Director, RecordingDirector};
8
9use super::solver::ProgressCallback;
10use super::SolverScope;
11use crate::stats::PhaseStats;
12
13/// Scope for a single phase of solving.
14///
15/// # Type Parameters
16/// * `'t` - Lifetime of the termination flag
17/// * `'a` - Lifetime of the solver scope reference
18/// * `S` - The planning solution type
19/// * `D` - The score director type
20/// * `BestCb` - The best-solution callback type
21pub struct PhaseScope<'t, 'a, S: PlanningSolution, D: Director<S>, BestCb = ()> {
22    // Reference to the parent solver scope.
23    solver_scope: &'a mut SolverScope<'t, S, D, BestCb>,
24    // Index of this phase (0-based).
25    phase_index: usize,
26    // Score at the start of this phase.
27    starting_score: Option<S::Score>,
28    // Number of steps in this phase.
29    step_count: u64,
30    // When this phase started.
31    start_time: Instant,
32    // Phase statistics.
33    stats: PhaseStats,
34}
35
36impl<'t, 'a, S: PlanningSolution, D: Director<S>, BestCb: ProgressCallback<S>>
37    PhaseScope<'t, 'a, S, D, BestCb>
38{
39    pub fn new(solver_scope: &'a mut SolverScope<'t, S, D, BestCb>, phase_index: usize) -> Self {
40        let starting_score = solver_scope.best_score().cloned();
41        Self {
42            solver_scope,
43            phase_index,
44            starting_score,
45            step_count: 0,
46            start_time: Instant::now(),
47            stats: PhaseStats::new(phase_index, "Unknown"),
48        }
49    }
50
51    pub fn with_phase_type(
52        solver_scope: &'a mut SolverScope<'t, S, D, BestCb>,
53        phase_index: usize,
54        phase_type: &'static str,
55    ) -> Self {
56        let starting_score = solver_scope.best_score().cloned();
57        Self {
58            solver_scope,
59            phase_index,
60            starting_score,
61            step_count: 0,
62            start_time: Instant::now(),
63            stats: PhaseStats::new(phase_index, phase_type),
64        }
65    }
66
67    pub fn phase_index(&self) -> usize {
68        self.phase_index
69    }
70
71    pub fn starting_score(&self) -> Option<&S::Score> {
72        self.starting_score.as_ref()
73    }
74
75    pub fn elapsed(&self) -> std::time::Duration {
76        self.start_time.elapsed()
77    }
78
79    pub fn step_count(&self) -> u64 {
80        self.step_count
81    }
82
83    /// Increments the phase step count.
84    pub fn increment_step_count(&mut self) -> u64 {
85        self.step_count += 1;
86        self.stats.record_step();
87        self.solver_scope.increment_step_count();
88        self.step_count
89    }
90
91    pub fn solver_scope(&self) -> &SolverScope<'t, S, D, BestCb> {
92        self.solver_scope
93    }
94
95    pub fn solver_scope_mut(&mut self) -> &mut SolverScope<'t, S, D, BestCb> {
96        self.solver_scope
97    }
98
99    pub fn score_director(&self) -> &D {
100        self.solver_scope.score_director()
101    }
102
103    pub(crate) fn score_director_mut(&mut self) -> &mut D {
104        self.solver_scope.score_director_mut()
105    }
106
107    pub fn trial<T, F>(&mut self, trial: F) -> T
108    where
109        F: FnOnce(&mut RecordingDirector<'_, S, D>) -> T,
110    {
111        self.solver_scope.trial(trial)
112    }
113
114    pub fn mutate<T, F>(&mut self, mutate: F) -> T
115    where
116        F: FnOnce(&mut D) -> T,
117    {
118        self.solver_scope.mutate(mutate)
119    }
120
121    /// Calculates the current score.
122    pub fn calculate_score(&mut self) -> S::Score {
123        self.stats.record_score_calculation();
124        self.solver_scope.calculate_score()
125    }
126
127    /// Updates best solution.
128    pub fn update_best_solution(&mut self) {
129        self.solver_scope.update_best_solution()
130    }
131
132    /// Publishes the current working solution when it ties the current best score.
133    pub(crate) fn promote_current_solution_on_score_tie(&mut self) {
134        self.solver_scope.promote_current_solution_on_score_tie()
135    }
136
137    pub fn stats(&self) -> &PhaseStats {
138        &self.stats
139    }
140
141    pub fn stats_mut(&mut self) -> &mut PhaseStats {
142        &mut self.stats
143    }
144
145    pub fn record_generated_batch(&mut self, count: u64, duration: Duration) {
146        self.stats.record_generated_batch(count, duration);
147        self.solver_scope
148            .stats_mut()
149            .record_generated_batch(count, duration);
150    }
151
152    pub fn record_generated_move(&mut self, duration: Duration) {
153        self.record_generated_batch(1, duration);
154    }
155
156    pub fn record_generation_time(&mut self, duration: Duration) {
157        self.stats.record_generation_time(duration);
158        self.solver_scope
159            .stats_mut()
160            .record_generation_time(duration);
161    }
162
163    pub fn record_evaluated_move(&mut self, duration: Duration) {
164        self.stats.record_evaluated_move(duration);
165        self.solver_scope
166            .stats_mut()
167            .record_evaluated_move(duration);
168    }
169
170    pub fn record_move_accepted(&mut self) {
171        self.stats.record_move_accepted();
172        self.solver_scope.stats_mut().record_move_accepted();
173    }
174
175    pub fn record_score_calculation(&mut self) {
176        self.stats.record_score_calculation();
177        self.solver_scope.stats_mut().record_score_calculation();
178    }
179}