Skip to main content

PlanningSolution

Trait PlanningSolution 

Source
pub trait PlanningSolution:
    Clone
    + Send
    + Sync
    + 'static {
    type Score: Score;

    const HAS_CONSTRAINT_WEIGHTS: bool = false;

    // 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 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§

Source

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.

Required Associated Types§

Source

type Score: Score

The score type used to evaluate this solution.

Required Methods§

Source

fn score(&self) -> Option<Self::Score>

Returns the current score of this solution, if calculated.

Returns None if the solution has not been scored yet.

Source

fn set_score(&mut self, score: Option<Self::Score>)

Stores the score; None marks the solution as unscored.

Provided Methods§

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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".

Implementors§