solverforge_solver/phase/traits.rs
1// Phase trait definition.
2
3use std::fmt::Debug;
4
5use solverforge_core::domain::PlanningSolution;
6use solverforge_scoring::Director;
7
8use crate::scope::ProgressCallback;
9use crate::scope::SolverScope;
10use crate::stats::CandidateTracePhasePlan;
11
12/// A phase of the solving process.
13///
14/// Phases are executed in sequence by the solver. Each phase has its own
15/// strategy for exploring or constructing solutions.
16///
17/// # Type Parameters
18/// * `S` - The planning solution type
19/// * `D` - The score director type
20/// * `BestCb` - The best-solution callback type (default `()`)
21pub trait Phase<S: PlanningSolution, D: Director<S>, BestCb: ProgressCallback<S> = ()>:
22 Send + Debug
23{
24 /* Executes this phase.
25
26 The phase should inspect the current state through immutable accessors,
27 use typed move undo for speculative work, use `mutate(...)` for committed
28 arbitrary changes, and update the best solution when improvements are found.
29 */
30 fn solve(&mut self, solver_scope: &mut SolverScope<'_, S, D, BestCb>);
31
32 fn phase_type_name(&self) -> &'static str;
33
34 /// Runs once when the enclosing solver reaches its terminal boundary.
35 ///
36 /// The solver invokes this after it has attempted every configured
37 /// top-level phase and before it snapshots final statistics. It runs for
38 /// ordinary completion as well as cancellation or configured termination,
39 /// including phases whose [`Self::solve`] method was skipped. Implementors
40 /// can use it to publish terminal-only diagnostics; the default preserves
41 /// the existing phase lifecycle unchanged.
42 fn on_solver_terminal(&mut self, _solver_scope: &mut SolverScope<'_, S, D, BestCb>) {}
43
44 /// Returns the exact resolved-plan provenance this phase can prove.
45 ///
46 /// A phase that does not override this method is deliberately marked
47 /// opaque. Candidate traces must never synthesize a lookalike plan for
48 /// custom or foreign phases; a consumer can then reject that comparison
49 /// rather than accepting a misleading fallback.
50 fn candidate_trace_plan(&self) -> CandidateTracePhasePlan {
51 CandidateTracePhasePlan::opaque(self.phase_type_name())
52 }
53}
54
55// Unit type implements Phase as a no-op (empty phase list).
56impl<S: PlanningSolution, D: Director<S>, BestCb: ProgressCallback<S>> Phase<S, D, BestCb> for () {
57 fn solve(&mut self, _solver_scope: &mut SolverScope<'_, S, D, BestCb>) {
58 // No-op: empty phase list does nothing
59 }
60
61 fn phase_type_name(&self) -> &'static str {
62 "NoOp"
63 }
64
65 fn candidate_trace_plan(&self) -> CandidateTracePhasePlan {
66 CandidateTracePhasePlan::known(
67 "solverforge.phase.no_op",
68 std::iter::empty::<(String, String)>(),
69 Vec::new(),
70 )
71 }
72}