pumpkin_core/branching/brancher.rs
1use enum_map::Enum;
2
3#[cfg(doc)]
4use crate::basic_types::Random;
5use crate::basic_types::SolutionReference;
6#[cfg(doc)]
7use crate::branching;
8#[cfg(doc)]
9use crate::branching::branchers::dynamic_brancher::DynamicBrancher;
10#[cfg(doc)]
11use crate::branching::value_selection::ValueSelector;
12#[cfg(doc)]
13use crate::branching::variable_selection::VariableSelector;
14use crate::branching::SelectionContext;
15#[cfg(doc)]
16use crate::create_statistics_struct;
17use crate::engine::predicates::predicate::Predicate;
18use crate::engine::variables::DomainId;
19#[cfg(doc)]
20use crate::results::solution_iterator::SolutionIterator;
21use crate::statistics::StatisticLogger;
22#[cfg(doc)]
23use crate::Solver;
24
25/// A trait for definining a branching strategy (oftentimes utilising a [`VariableSelector`] and a
26/// [`ValueSelector`]).
27///
28/// In general, implementations of this trait define how the search of the solver proceeds (i.e. it
29/// controls how the solver determines which part of the search space to explore). It is required
30/// that the resulting decision creates a smaller domain for at least 1 of the variables (and more
31/// domains can be affected due to subsequent inference). See [`branching`] for
32/// example usages.
33///
34/// If the [`Brancher`] (or any component thereof) is implemented incorrectly then the
35/// behaviour of the solver is undefined.
36pub trait Brancher {
37 /// Logs statistics of the brancher using the provided [`StatisticLogger`].
38 ///
39 /// It is recommended to create a struct through the [`create_statistics_struct!`] macro!
40 fn log_statistics(&self, _statistic_logger: StatisticLogger) {}
41
42 /// Returns the next decision concerning a single variable and value; it returns the
43 /// [`Predicate`] corresponding to this decision (or [`None`] if all variables under
44 /// consideration are assigned).
45 ///
46 /// Note that this method **cannot** perform the assignment of the decision, it should only
47 /// return a suggestion in the form of a [`Predicate`]; the [`SelectionContext`] is
48 /// only mutable to account for the usage of random generators (e.g. see [`Random`]).
49 fn next_decision(&mut self, context: &mut SelectionContext) -> Option<Predicate>;
50
51 /// A function which is called after a conflict has been found and processed but (currently)
52 /// does not provide any additional information.
53 ///
54 /// To receive information about this event, use [`BrancherEvent::Conflict`] in
55 /// [`Self::subscribe_to_events`]
56 fn on_conflict(&mut self) {}
57
58 /// A function which is called whenever a backtrack occurs in the [`Solver`].
59 ///
60 /// To receive information about this event, use [`BrancherEvent::Backtrack`] in
61 /// [`Self::subscribe_to_events`]
62 fn on_backtrack(&mut self) {}
63
64 /// This method is called when a solution is found; this will either be called when a new
65 /// incumbent solution is found (i.e. a solution with a better objective value than previously
66 /// known) or when a new solution is found when iterating over solutions using
67 /// [`SolutionIterator`].
68 ///
69 /// To receive information about this event, use [`BrancherEvent::Solution`] in
70 /// [`Self::subscribe_to_events`]
71 fn on_solution(&mut self, _solution: SolutionReference) {}
72
73 /// A function which is called after a [`DomainId`] is unassigned during backtracking (i.e. when
74 /// it was fixed but is no longer), specifically, it provides `variable` which is the
75 /// [`DomainId`] which has been reset and `value` which is the value to which the variable was
76 /// previously fixed. This method could thus be called multiple times in a single
77 /// backtracking operation by the solver.
78 ///
79 /// To receive information about this event, use [`BrancherEvent::UnassignInteger`] in
80 /// [`Self::subscribe_to_events`]
81 fn on_unassign_integer(&mut self, _variable: DomainId, _value: i32) {}
82
83 /// A function which is called when a [`Predicate`] appears in a conflict during conflict
84 /// analysis.
85 ///
86 /// To receive information about this event, use
87 /// [`BrancherEvent::AppearanceInConflictPredicate`] in [`Self::subscribe_to_events`]
88 fn on_appearance_in_conflict_predicate(&mut self, _predicate: Predicate) {}
89
90 /// This method is called whenever a restart is performed.
91 /// To receive information about this event, use [`BrancherEvent::Restart`] in
92 /// [`Self::subscribe_to_events`]
93 fn on_restart(&mut self) {}
94
95 /// Called after backtracking.
96 /// Used to reset internal data structures to account for the backtrack.
97 ///
98 /// To receive information about this event, use [`BrancherEvent::Synchronise`] in
99 /// [`Self::subscribe_to_events`]
100 fn synchronise(&mut self, _context: &mut SelectionContext) {}
101
102 /// This method returns whether a restart is *currently* pointless for the [`Brancher`].
103 ///
104 /// For example, if a [`Brancher`] is using a static search strategy then a restart is
105 /// pointless; however, if a [`Brancher`] is using a variable selector which
106 /// changes throughout the search process then restarting is not pointless.
107 ///
108 /// Note that even if the [`Brancher`] has indicated that a restart is pointless, it could be
109 /// that the restart is still performed (e.g. if this [`Brancher`] is a subcomponent of another
110 /// [`Brancher`] and it is not the only `is_restart_pointless` response which is taken into
111 /// account).
112 fn is_restart_pointless(&mut self) -> bool {
113 true
114 }
115
116 /// Indicates which [`BrancherEvent`] are relevant for this particular [`Brancher`].
117 ///
118 /// This can be used by [`Brancher::subscribe_to_events`] to determine upon which
119 /// events which [`VariableSelector`] should be called.
120 fn subscribe_to_events(&self) -> Vec<BrancherEvent>;
121}
122
123/// The events which can occur for a [`Brancher`]. Used for returning which events are relevant in
124/// [`Brancher::subscribe_to_events`], [`VariableSelector::subscribe_to_events`],
125/// and [`ValueSelector::subscribe_to_events`].
126#[derive(Debug, Clone, Copy, Enum, Hash, PartialEq, Eq)]
127pub enum BrancherEvent {
128 /// Event for when a conflict is detected
129 Conflict,
130 /// Event for when a backtrack is performed
131 Backtrack,
132 /// Event for when a solution has been found
133 Solution,
134 /// Event for when an integer variable has become unassigned
135 UnassignInteger,
136 /// Event for when a predicate appears during conflict analysis
137 AppearanceInConflictPredicate,
138 /// Event for when a restart occurs
139 Restart,
140 /// Event which is called with the new state after a backtrack has occurred
141 Synchronise,
142}