pub trait PlanningSolution:
Clone
+ Send
+ Sync
+ 'static {
type Score: Score;
const HAS_CONSTRAINT_WEIGHTS: bool = false;
const HAS_CASCADING_SHADOWS: bool = false;
Show 18 methods
// Required methods
fn score(&self) -> Option<Self::Score>;
fn set_score(&mut self, score: Option<Self::Score>);
// Provided methods
fn update_entity_shadows(
&mut self,
_descriptor_index: usize,
_entity_index: usize,
) { ... }
fn update_all_shadows(&mut self) { ... }
fn constraint_weights(&self) -> Option<&ConstraintWeights> { ... }
fn list_shadow_elements(
&self,
_descriptor_index: usize,
_entity_index: usize,
) -> Option<(usize, &[usize])> { ... }
fn clear_list_shadows(&mut self, _element: usize) { ... }
fn list_shadow_source(_collection: &str) -> Option<usize> { ... }
fn cascading_element_count(&self) -> usize { ... }
fn cascading_assigned(&self, _element: usize) -> bool { ... }
fn cascading_next(&self, _element: usize) -> Option<usize> { ... }
fn cascading_dependencies(
&self,
_element: usize,
_dependencies: &mut Vec<usize>,
) { ... }
fn cascading_peers(&self, _element: usize, _peers: &mut Vec<usize>) { ... }
fn list_structure_unchanged(
&self,
_element: usize,
_owner_descriptor: usize,
_owner_index: usize,
_index: usize,
_previous: Option<usize>,
_next: Option<usize>,
) -> bool { ... }
fn cascading_unset(&self, _element: usize) -> bool { ... }
fn update_cascading_shadow<W: FnOnce(&Self)>(
&mut self,
_element: usize,
_computable: bool,
_before_write: W,
) -> bool { ... }
fn update_list_structure_shadows(
&mut self,
descriptor_index: usize,
entity_index: usize,
) { ... }
fn is_initialized(&self) -> bool { ... }
}Expand description
Marker trait for planning solutions.
A planning solution represents both the problem definition and the (potentially partial) solution. It contains:
- Problem facts: Immutable input data
- Planning entities: Things to be optimized
- Score: The quality of the current solution
§Example
use solverforge_core::{PlanningSolution, score::SoftScore};
#[derive(Clone)]
struct NQueens {
n: usize,
rows: Vec<Option<usize>>,
score: Option<SoftScore>,
}
impl PlanningSolution for NQueens {
type Score = SoftScore;
fn score(&self) -> Option<Self::Score> {
self.score
}
fn set_score(&mut self, score: Option<Self::Score>) {
self.score = score;
}
}For complex solutions, use the #[derive(PlanningSolution)] macro from solverforge-macros.
§Thread Safety
Planning solutions must be Send + Sync to support multi-threaded solving.
Provided Associated Constants§
Sourceconst HAS_CONSTRAINT_WEIGHTS: bool = false
const HAS_CONSTRAINT_WEIGHTS: bool = false
Whether this solution type carries runtime constraint weights.
Whether a value’s weights apply is decided by the score director’s weight
mode, a type chosen when the director is built; solves pick the weighted
mode only when this constant is true and some multiplier differs from
1. #[planning_solution] sets it to true exactly when a field is
marked #[constraint_weights], and then also routes the solve through the
weighted entrypoint. A manual implementation that overrides
constraint_weights must set it to true as well, or the weights are
never applied.
Sourceconst HAS_CASCADING_SHADOWS: bool = false
const HAS_CASCADING_SHADOWS: bool = false
Whether the list shadows include a cascading shadow: an element value computed from the element’s previous element in its list and from the elements it declares it depends on, which may sit in other owners’ lists.
#[shadow_variable_updates(cascading_field = ...)] sets it to true
and generates the cascading_* hooks below. When it is false
(the default) a score director compiles no cascading code into its
variable-change path.
Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn update_entity_shadows(
&mut self,
_descriptor_index: usize,
_entity_index: usize,
)
fn update_entity_shadows( &mut self, _descriptor_index: usize, _entity_index: usize, )
Updates shadow variables for the descriptor/entity pair.
Default implementation is a no-op. Solutions with derived state can override this to keep cached values synchronized during solving.
Sourcefn update_all_shadows(&mut self)
fn update_all_shadows(&mut self)
Updates all shadow variables for the full solution.
Default implementation is a no-op. Solutions with derived state can override this to refresh all cached values before initialization.
Sourcefn constraint_weights(&self) -> Option<&ConstraintWeights>
fn constraint_weights(&self) -> Option<&ConstraintWeights>
Returns the runtime constraint weight multipliers carried by this solution.
Default implementation returns None: every constraint keeps its coded
weight. #[planning_solution] generates this accessor for the field marked
#[constraint_weights]. Weighted-mode score directors bind these
multipliers on construction and on every (re)initialization, so a running
solve must reset its director after changing them.
Sourcefn list_shadow_elements(
&self,
_descriptor_index: usize,
_entity_index: usize,
) -> Option<(usize, &[usize])>
fn list_shadow_elements( &self, _descriptor_index: usize, _entity_index: usize, ) -> Option<(usize, &[usize])>
Returns the elements whose list shadows
update_entity_shadows(descriptor_index, entity_index) writes, with the
change source their stream rows are reported under.
A score director retracts these rows before the shadow write and inserts them after it, so streams that read element shadows stay incremental. Default implementation reports no list shadows.
Sourcefn clear_list_shadows(&mut self, _element: usize)
fn clear_list_shadows(&mut self, _element: usize)
Clears the list shadows of an element that no list holds any more.
Default implementation is a no-op.
Sourcefn list_shadow_source(_collection: &str) -> Option<usize>
fn list_shadow_source(_collection: &str) -> Option<usize>
Returns the change source under which rows of collection are reported
when they carry list shadows, or None when the collection holds none.
Sourcefn cascading_element_count(&self) -> usize
fn cascading_element_count(&self) -> usize
Number of elements in the collection that carries the cascading shadow.
Sourcefn cascading_assigned(&self, _element: usize) -> bool
fn cascading_assigned(&self, _element: usize) -> bool
Whether element is held by a list.
Sourcefn cascading_next(&self, _element: usize) -> Option<usize>
fn cascading_next(&self, _element: usize) -> Option<usize>
The element after element in its list, if any.
Sourcefn cascading_dependencies(
&self,
_element: usize,
_dependencies: &mut Vec<usize>,
)
fn cascading_dependencies( &self, _element: usize, _dependencies: &mut Vec<usize>, )
Appends the elements element’s cascading value depends on, besides
its previous element in its list, to dependencies. Dependencies are
problem facts: moves never change them.
Sourcefn cascading_peers(&self, _element: usize, _peers: &mut Vec<usize>)
fn cascading_peers(&self, _element: usize, _peers: &mut Vec<usize>)
Appends the peers of element to peers: elements computed together
with it, joined transitively into a joint group. A member’s value is
computed after the previous elements and dependencies of every member
hold their final values, so it may read them; it must not read
another member’s cascading value. Peers are problem facts: moves
never change them.
Sourcefn list_structure_unchanged(
&self,
_element: usize,
_owner_descriptor: usize,
_owner_index: usize,
_index: usize,
_previous: Option<usize>,
_next: Option<usize>,
) -> bool
fn list_structure_unchanged( &self, _element: usize, _owner_descriptor: usize, _owner_index: usize, _index: usize, _previous: Option<usize>, _next: Option<usize>, ) -> bool
Whether element’s list structure shadows already describe the place
owner_descriptor/owner_index, position index, between previous
and next: its owner, previous and next shadows (and index shadow,
when declared) hold those values.
A score director with a cascading shadow asks this before rewriting an
owner’s structure and keeps the stream row of an element that answers
true and whose cascading value does not change. false (the default)
rescores the element. #[shadow_variable_updates(cascading_field = ...)] generates it from the configured structure fields.
Sourcefn cascading_unset(&self, _element: usize) -> bool
fn cascading_unset(&self, _element: usize) -> bool
Whether element’s cascading value is unset.
Sourcefn update_cascading_shadow<W: FnOnce(&Self)>(
&mut self,
_element: usize,
_computable: bool,
_before_write: W,
) -> bool
fn update_cascading_shadow<W: FnOnce(&Self)>( &mut self, _element: usize, _computable: bool, _before_write: W, ) -> bool
Recomputes element’s cascading value, or unsets it when
computable is false (the element is in no list, or on a
dependency cycle). When the value changes, calls before_write with
the solution still holding the old value, then writes the new one and
returns true.
Sourcefn update_list_structure_shadows(
&mut self,
descriptor_index: usize,
entity_index: usize,
)
fn update_list_structure_shadows( &mut self, descriptor_index: usize, entity_index: usize, )
Updates the list shadows of one owner that describe list structure
(inverse, index, previous, next) without the cascading shadow, which
the caller propagates afterwards. Defaults to update_entity_shadows.
Sourcefn is_initialized(&self) -> bool
fn is_initialized(&self) -> bool
Returns true if this solution is fully initialized.
A solution is initialized when all planning variables have been assigned.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".