Skip to main content

solverforge_solver/scope/
step.rs

1//! Step-level scope.
2
3use solverforge_core::domain::PlanningSolution;
4use solverforge_scoring::ScoreDirector;
5
6use super::solver::BestSolutionCallback;
7use super::PhaseScope;
8
9/// Scope for a single step within a phase.
10///
11/// # Type Parameters
12/// * `'t` - Lifetime of the termination flag
13/// * `'a` - Lifetime of the phase scope reference
14/// * `'b` - Lifetime of the solver scope reference
15/// * `S` - The planning solution type
16/// * `D` - The score director type
17/// * `BestCb` - The best-solution callback type
18pub struct StepScope<'t, 'a, 'b, S: PlanningSolution, D: ScoreDirector<S>, BestCb = ()> {
19    // Reference to the parent phase scope.
20    phase_scope: &'a mut PhaseScope<'t, 'b, S, D, BestCb>,
21    // Index of this step within the phase (0-based).
22    step_index: u64,
23    // Score after this step.
24    step_score: Option<S::Score>,
25}
26
27impl<'t, 'a, 'b, S: PlanningSolution, D: ScoreDirector<S>, BestCb: BestSolutionCallback<S>>
28    StepScope<'t, 'a, 'b, S, D, BestCb>
29{
30    /// Creates a new step scope.
31    pub fn new(phase_scope: &'a mut PhaseScope<'t, 'b, S, D, BestCb>) -> Self {
32        let step_index = phase_scope.step_count();
33        Self {
34            phase_scope,
35            step_index,
36            step_score: None,
37        }
38    }
39
40    /// Returns the step index within the phase.
41    pub fn step_index(&self) -> u64 {
42        self.step_index
43    }
44
45    /// Returns the step score.
46    pub fn step_score(&self) -> Option<&S::Score> {
47        self.step_score.as_ref()
48    }
49
50    /// Sets the step score.
51    pub fn set_step_score(&mut self, score: S::Score) {
52        self.step_score = Some(score);
53    }
54
55    /// Marks this step as complete and increments counters.
56    pub fn complete(&mut self) {
57        self.phase_scope.increment_step_count();
58    }
59
60    /// Returns a reference to the phase scope.
61    pub fn phase_scope(&self) -> &PhaseScope<'t, 'b, S, D, BestCb> {
62        self.phase_scope
63    }
64
65    /// Returns a mutable reference to the phase scope.
66    pub fn phase_scope_mut(&mut self) -> &mut PhaseScope<'t, 'b, S, D, BestCb> {
67        self.phase_scope
68    }
69
70    /// Returns a reference to the score director.
71    pub fn score_director(&self) -> &D {
72        self.phase_scope.score_director()
73    }
74
75    /// Returns a mutable reference to the score director.
76    pub fn score_director_mut(&mut self) -> &mut D {
77        self.phase_scope.score_director_mut()
78    }
79
80    /// Calculates the current score.
81    pub fn calculate_score(&mut self) -> S::Score {
82        self.phase_scope.calculate_score()
83    }
84}