IncrementalConstraint

Trait IncrementalConstraint 

Source
pub trait IncrementalConstraint<S, Sc>: Send + Sync
where Sc: Score,
{ // Required methods fn evaluate(&self, solution: &S) -> Sc; fn match_count(&self, solution: &S) -> usize; fn initialize(&mut self, solution: &S) -> Sc; fn on_insert(&mut self, solution: &S, entity_index: usize) -> Sc; fn on_retract(&mut self, solution: &S, entity_index: usize) -> Sc; fn reset(&mut self); fn name(&self) -> &str; // Provided methods fn is_hard(&self) -> bool { ... } fn constraint_ref(&self) -> ConstraintRef { ... } fn get_matches(&self, _solution: &S) -> Vec<DetailedConstraintMatch<Sc>> { ... } fn weight(&self) -> Sc { ... } }
Expand description

A single constraint with incremental scoring capability.

Unlike the trait-object Constraint trait, IncrementalConstraint is designed for monomorphized code paths where the concrete type is known.

§Incremental Protocol

The incremental methods allow delta-based score updates:

  1. Call initialize once to populate internal state
  2. Before changing an entity’s variable: call on_retract with old state
  3. After changing the variable: call on_insert with new state
  4. Score delta = insert_delta - retract_delta

This avoids full re-evaluation on every move.

Required Methods§

Source

fn evaluate(&self, solution: &S) -> Sc

Full evaluation of this constraint.

Iterates all entities and computes the total score impact. Use this for initial scoring; use on_insert/on_retract for deltas.

Source

fn match_count(&self, solution: &S) -> usize

Returns the number of matches for this constraint.

Source

fn initialize(&mut self, solution: &S) -> Sc

Initializes internal state by inserting all entities.

Must be called before using incremental methods (on_insert/on_retract). Returns the total score from initialization.

Source

fn on_insert(&mut self, solution: &S, entity_index: usize) -> Sc

Called when an entity is inserted or its variable changes.

Returns the score delta from this insertion.

Source

fn on_retract(&mut self, solution: &S, entity_index: usize) -> Sc

Called when an entity is retracted or before its variable changes.

Returns the score delta (negative) from this retraction.

Source

fn reset(&mut self)

Resets internal state for a new solving session.

Source

fn name(&self) -> &str

Returns the constraint name.

Provided Methods§

Source

fn is_hard(&self) -> bool

Returns true if this is a hard constraint.

Source

fn constraint_ref(&self) -> ConstraintRef

Returns the constraint reference (package + name).

Default implementation constructs from name().

Source

fn get_matches(&self, _solution: &S) -> Vec<DetailedConstraintMatch<Sc>>

Returns detailed matches with entity justifications.

The default implementation returns an empty vector. Constraints should override this to provide detailed match information including the entities involved in each constraint violation.

This enables score explanation features without requiring all constraints to implement detailed tracking.

Source

fn weight(&self) -> Sc

Returns the constraint weight (score per match).

Used for score explanation. Default returns zero.

Implementors§

Source§

impl<S, A, B, C, K, CK, EA, EB, KA, KB, Flatten, CKeyFn, ALookup, F, W, Sc> IncrementalConstraint<S, Sc> for FlattenedBiConstraint<S, A, B, C, K, CK, EA, EB, KA, KB, Flatten, CKeyFn, ALookup, F, W, Sc>
where S: Send + Sync + 'static, A: Clone + Send + Sync + 'static, B: Clone + Send + Sync + 'static, C: Clone + Send + Sync + 'static, K: Eq + Hash + Clone + Send + Sync, CK: Eq + Hash + Clone + Send + Sync, EA: Fn(&S) -> &[A] + Send + Sync, EB: Fn(&S) -> &[B] + Send + Sync, KA: Fn(&A) -> K + Send + Sync, KB: Fn(&B) -> K + Send + Sync, Flatten: Fn(&B) -> &[C] + Send + Sync, CKeyFn: Fn(&C) -> CK + Send + Sync, ALookup: Fn(&A) -> CK + Send + Sync, F: Fn(&S, &A, &C) -> bool + Send + Sync, W: Fn(&A, &C) -> Sc + Send + Sync, Sc: Score,

Source§

impl<S, A, B, K, EA, EB, KA, KB, C, D, W, Sc> IncrementalConstraint<S, Sc> for ComplementedGroupConstraint<S, A, B, K, EA, EB, KA, KB, C, D, W, Sc>
where S: Send + Sync + 'static, A: Clone + Send + Sync + 'static, B: Clone + Send + Sync + 'static, K: Clone + Eq + Hash + Send + Sync, EA: Fn(&S) -> &[A] + Send + Sync, EB: Fn(&S) -> &[B] + Send + Sync, KA: Fn(&A) -> Option<K> + Send + Sync, KB: Fn(&B) -> K + Send + Sync, C: UniCollector<A> + Send + Sync, <C as UniCollector<A>>::Accumulator: Send + Sync, <C as UniCollector<A>>::Result: Clone + Send + Sync, <C as UniCollector<A>>::Value: Send + Sync, D: Fn(&B) -> <C as UniCollector<A>>::Result + Send + Sync, W: Fn(&<C as UniCollector<A>>::Result) -> Sc + Send + Sync, Sc: Score,

Source§

impl<S, A, B, K, EA, EB, KA, KB, F, W, Sc> IncrementalConstraint<S, Sc> for IncrementalCrossBiConstraint<S, A, B, K, EA, EB, KA, KB, F, W, Sc>
where S: Send + Sync + 'static, A: Clone + Debug + Send + Sync + 'static, B: Clone + Debug + Send + Sync + 'static, K: Eq + Hash + Clone + Send + Sync, EA: Fn(&S) -> &[A] + Send + Sync, EB: Fn(&S) -> &[B] + Send + Sync, KA: Fn(&A) -> K + Send + Sync, KB: Fn(&B) -> K + Send + Sync, F: Fn(&S, &A, &B) -> bool + Send + Sync, W: Fn(&A, &B) -> Sc + Send + Sync, Sc: Score,

Source§

impl<S, A, B, K, EA, EB, KA, KB, FA, W, Sc> IncrementalConstraint<S, Sc> for IfExistsUniConstraint<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
where S: Send + Sync + 'static, A: Clone + Send + Sync + 'static, B: Clone + Send + Sync + 'static, K: Eq + Hash + Clone + Send + Sync, EA: Fn(&S) -> &[A] + Send + Sync, EB: Fn(&S) -> Vec<B> + Send + Sync, KA: Fn(&A) -> K + Send + Sync, KB: Fn(&B) -> K + Send + Sync, FA: Fn(&S, &A) -> bool + Send + Sync, W: Fn(&A) -> Sc + Send + Sync, Sc: Score,

Source§

impl<S, A, E, F, W, Sc> IncrementalConstraint<S, Sc> for IncrementalUniConstraint<S, A, E, F, W, Sc>
where S: Send + Sync + 'static, A: Clone + Debug + Send + Sync + 'static, E: Fn(&S) -> &[A] + Send + Sync, F: Fn(&S, &A) -> bool + Send + Sync, W: Fn(&A) -> Sc + Send + Sync, Sc: Score,

Source§

impl<S, A, K, E, F, KF, Sc> IncrementalConstraint<S, Sc> for BalanceConstraint<S, A, K, E, F, KF, Sc>
where S: Send + Sync + 'static, A: Clone + Send + Sync + 'static, K: Clone + Eq + Hash + Send + Sync + 'static, E: Fn(&S) -> &[A] + Send + Sync, F: UniFilter<S, A>, KF: Fn(&A) -> Option<K> + Send + Sync, Sc: Score + 'static,

Source§

impl<S, A, K, E, KE, F, W, Sc> IncrementalConstraint<S, Sc> for IncrementalPentaConstraint<S, A, K, E, KE, F, W, Sc>
where S: Send + Sync + 'static, A: Clone + Debug + Send + Sync + 'static, K: Eq + Hash + Clone + Send + Sync, E: Fn(&S) -> &[A] + Send + Sync, KE: Fn(&A) -> K + Send + Sync, F: Fn(&S, &A, &A, &A, &A, &A) -> bool + Send + Sync, W: Fn(&A, &A, &A, &A, &A) -> Sc + Send + Sync, Sc: Score,

Source§

impl<S, A, K, E, KE, F, W, Sc> IncrementalConstraint<S, Sc> for IncrementalQuadConstraint<S, A, K, E, KE, F, W, Sc>
where S: Send + Sync + 'static, A: Clone + Debug + Send + Sync + 'static, K: Eq + Hash + Clone + Send + Sync, E: Fn(&S) -> &[A] + Send + Sync, KE: Fn(&A) -> K + Send + Sync, F: Fn(&S, &A, &A, &A, &A) -> bool + Send + Sync, W: Fn(&A, &A, &A, &A) -> Sc + Send + Sync, Sc: Score,

Source§

impl<S, A, K, E, KE, F, W, Sc> IncrementalConstraint<S, Sc> for IncrementalTriConstraint<S, A, K, E, KE, F, W, Sc>
where S: Send + Sync + 'static, A: Clone + Debug + Send + Sync + 'static, K: Eq + Hash + Clone + Send + Sync, E: Fn(&S) -> &[A] + Send + Sync, KE: Fn(&A) -> K + Send + Sync, F: Fn(&S, &A, &A, &A) -> bool + Send + Sync, W: Fn(&A, &A, &A) -> Sc + Send + Sync, Sc: Score,

Source§

impl<S, A, K, E, KE, F, W, Sc> IncrementalConstraint<S, Sc> for IncrementalBiConstraint<S, A, K, E, KE, F, W, Sc>
where S: Send + Sync + 'static, A: Clone + Debug + Send + Sync + 'static, K: Eq + Hash + Clone + Send + Sync, E: Fn(&S) -> &[A] + Send + Sync, KE: Fn(&A) -> K + Send + Sync, F: Fn(&S, &A, &A) -> bool + Send + Sync, W: Fn(&A, &A) -> Sc + Send + Sync, Sc: Score,

Source§

impl<S, A, K, E, KF, C, W, Sc> IncrementalConstraint<S, Sc> for GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>
where S: Send + Sync + 'static, A: Clone + Send + Sync + 'static, K: Clone + Eq + Hash + Send + Sync + 'static, E: Fn(&S) -> &[A] + Send + Sync, KF: Fn(&A) -> K + Send + Sync, C: UniCollector<A> + Send + Sync + 'static, <C as UniCollector<A>>::Accumulator: Send + Sync, <C as UniCollector<A>>::Result: Send + Sync, <C as UniCollector<A>>::Value: Send + Sync, W: Fn(&<C as UniCollector<A>>::Result) -> Sc + Send + Sync, Sc: Score + 'static,