Skip to main content

solverforge_solver/scope/
step.rs

1// Step-level scope.
2
3use solverforge_core::domain::PlanningSolution;
4use solverforge_scoring::Director;
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: Director<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: Director<S>, BestCb: BestSolutionCallback<S>>
28    StepScope<'t, 'a, 'b, S, D, BestCb>
29{
30    pub fn new(phase_scope: &'a mut PhaseScope<'t, 'b, S, D, BestCb>) -> Self {
31        let step_index = phase_scope.step_count();
32        Self {
33            phase_scope,
34            step_index,
35            step_score: None,
36        }
37    }
38
39    pub fn step_index(&self) -> u64 {
40        self.step_index
41    }
42
43    pub fn step_score(&self) -> Option<&S::Score> {
44        self.step_score.as_ref()
45    }
46
47    pub fn set_step_score(&mut self, score: S::Score) {
48        self.step_score = Some(score);
49    }
50
51    /// Marks this step as complete and increments counters.
52    pub fn complete(&mut self) {
53        self.phase_scope.increment_step_count();
54    }
55
56    pub fn phase_scope(&self) -> &PhaseScope<'t, 'b, S, D, BestCb> {
57        self.phase_scope
58    }
59
60    pub fn phase_scope_mut(&mut self) -> &mut PhaseScope<'t, 'b, S, D, BestCb> {
61        self.phase_scope
62    }
63
64    pub fn score_director(&self) -> &D {
65        self.phase_scope.score_director()
66    }
67
68    pub fn score_director_mut(&mut self) -> &mut D {
69        self.phase_scope.score_director_mut()
70    }
71
72    /// Calculates the current score.
73    pub fn calculate_score(&mut self) -> S::Score {
74        self.phase_scope.calculate_score()
75    }
76}