solverforge_solver/scope/
phase.rs

1//! Phase-level scope.
2
3use std::time::Instant;
4
5use solverforge_core::domain::PlanningSolution;
6use solverforge_scoring::ScoreDirector;
7
8use super::SolverScope;
9
10/// Scope for a single phase of solving.
11pub struct PhaseScope<'a, S: PlanningSolution> {
12    /// Reference to the parent solver scope.
13    solver_scope: &'a mut SolverScope<S>,
14    /// Index of this phase (0-based).
15    phase_index: usize,
16    /// Score at the start of this phase.
17    starting_score: Option<S::Score>,
18    /// Number of steps in this phase.
19    step_count: u64,
20    /// When this phase started.
21    start_time: Instant,
22}
23
24impl<'a, S: PlanningSolution> PhaseScope<'a, S> {
25    /// Creates a new phase scope.
26    pub fn new(solver_scope: &'a mut SolverScope<S>, phase_index: usize) -> Self {
27        let starting_score = solver_scope.best_score().cloned();
28        Self {
29            solver_scope,
30            phase_index,
31            starting_score,
32            step_count: 0,
33            start_time: Instant::now(),
34        }
35    }
36
37    /// Returns the phase index.
38    pub fn phase_index(&self) -> usize {
39        self.phase_index
40    }
41
42    /// Returns the starting score for this phase.
43    pub fn starting_score(&self) -> Option<&S::Score> {
44        self.starting_score.as_ref()
45    }
46
47    /// Returns the elapsed time for this phase.
48    pub fn elapsed(&self) -> std::time::Duration {
49        self.start_time.elapsed()
50    }
51
52    /// Returns the step count for this phase.
53    pub fn step_count(&self) -> u64 {
54        self.step_count
55    }
56
57    /// Increments the phase step count.
58    pub fn increment_step_count(&mut self) -> u64 {
59        self.step_count += 1;
60        self.solver_scope.increment_step_count();
61        self.step_count
62    }
63
64    /// Returns a reference to the solver scope.
65    pub fn solver_scope(&self) -> &SolverScope<S> {
66        self.solver_scope
67    }
68
69    /// Returns a mutable reference to the solver scope.
70    pub fn solver_scope_mut(&mut self) -> &mut SolverScope<S> {
71        self.solver_scope
72    }
73
74    /// Convenience: returns the score director.
75    pub fn score_director(&self) -> &dyn ScoreDirector<S> {
76        self.solver_scope.score_director()
77    }
78
79    /// Convenience: returns a mutable score director.
80    pub fn score_director_mut(&mut self) -> &mut dyn ScoreDirector<S> {
81        self.solver_scope.score_director_mut()
82    }
83
84    /// Convenience: calculates the current score.
85    pub fn calculate_score(&mut self) -> S::Score {
86        self.solver_scope.calculate_score()
87    }
88
89    /// Convenience: updates best solution.
90    pub fn update_best_solution(&mut self) {
91        self.solver_scope.update_best_solution()
92    }
93}