Skip to main content

ComplementedConstraintStream

Struct ComplementedConstraintStream 

Source
pub struct ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc>
where Sc: Score,
{ /* private fields */ }
Expand description

Zero-erasure constraint stream with complemented groups.

ComplementedConstraintStream results from calling complement on a GroupedConstraintStream. It ensures all keys from a complement source are represented, using default values for missing keys.

The key function for A entities returns Option<K> to allow skipping entities without valid keys (e.g., unassigned shifts).

§Type Parameters

  • S - Solution type
  • A - Original entity type (e.g., Shift)
  • B - Complement entity type (e.g., Employee)
  • K - Group key type
  • EA - Extractor for A entities
  • EB - Extractor for B entities (complement source)
  • KA - Key function for A (returns Option<K> to allow filtering)
  • KB - Key function for B
  • C - Collector type
  • D - Default value function
  • Sc - Score type

§Example

use solverforge_core::score::SoftScore;
use solverforge_scoring::api::constraint_set::IncrementalConstraint;
use solverforge_scoring::stream::collector::count;
use solverforge_scoring::stream::ConstraintFactory;

#[derive(Clone, Hash, PartialEq, Eq)]
struct Employee {
    id: usize,
}

#[derive(Clone, Hash, PartialEq, Eq)]
struct Shift {
    employee_id: usize,
}

#[derive(Clone)]
struct Schedule {
    employees: Vec<Employee>,
    shifts: Vec<Shift>,
}

// Count shifts per employee, including employees with 0 shifts
let constraint = ConstraintFactory::<Schedule, SoftScore>::new()
    .for_each(Schedule::shifts())
    .group_by(|shift: &Shift| shift.employee_id, count())
    .complement(
        Schedule::employees(),
        |emp: &Employee| emp.id,
        |_emp: &Employee| 0usize,
    )
    .penalize(|_employee_id: &usize, count: &usize| SoftScore::of(*count as i64))
    .named("Shift count");

let schedule = Schedule {
    employees: vec![Employee { id: 0 }, Employee { id: 1 }, Employee { id: 2 }],
    shifts: vec![
        Shift { employee_id: 0 },
        Shift { employee_id: 0 },
        // Employee 1 has 0 shifts, Employee 2 has 0 shifts
    ],
};

// Employee 0: 2, Employee 1: 0, Employee 2: 0 → Total: -2
assert_eq!(constraint.evaluate(&schedule), SoftScore::of(-2));

Implementations§

Source§

impl<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc> ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc>
where S: Send + Sync + 'static, A: Clone + Send + Sync + 'static, B: Clone + Send + Sync + 'static, K: Clone + Eq + Hash + Send + Sync + 'static, EA: CollectionExtract<S, Item = A>, EB: CollectionExtract<S, Item = B>, KA: Fn(&A) -> Option<K> + Send + Sync, KB: Fn(&B) -> K + Send + Sync, C: for<'i> Collector<&'i A, Value = V, Result = R, Accumulator = Acc> + Send + Sync + 'static, V: Send + Sync + 'static, R: Send + Sync + 'static, Acc: Accumulator<V, R> + Send + Sync + 'static, D: Fn(&B) -> R + Send + Sync + 'static, Sc: Score + 'static,

Source

pub fn penalize<W>( self, weight: W, ) -> ComplementedConstraintBuilder<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, impl Fn(&K, &R) -> Sc + Send + Sync, Sc>
where W: for<'w> ConstraintWeight<(&'w K, &'w R), Sc> + Send + Sync,

Penalizes each key by weight.

weight is a fixed score, or a closure over the matched (&key, &result) returning one. A fixed weight with a non-zero hard level marks the constraint hard; closures are soft unless wrapped in hard_weight.

Source

pub fn reward<W>( self, weight: W, ) -> ComplementedConstraintBuilder<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, impl Fn(&K, &R) -> Sc + Send + Sync, Sc>
where W: for<'w> ConstraintWeight<(&'w K, &'w R), Sc> + Send + Sync,

Rewards each key by weight.

weight is a fixed score, or a closure over the matched (&key, &result) returning one. A fixed weight with a non-zero hard level marks the constraint hard; closures are soft unless wrapped in hard_weight.

Trait Implementations§

Source§

impl<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc: Score> Debug for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc> Forget for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc>

§

impl<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc> Freeze for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc>
where EA: Freeze, EB: Freeze, KA: Freeze, KB: Freeze, C: Freeze, D: Freeze, PhantomData<(fn() -> S, fn() -> A, fn() -> B, fn() -> K, fn() -> V, fn() -> R, fn() -> Acc, fn() -> Sc)>: Freeze,

§

impl<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc> RefUnwindSafe for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc>
where EA: RefUnwindSafe, EB: RefUnwindSafe, KA: RefUnwindSafe, KB: RefUnwindSafe, C: RefUnwindSafe, D: RefUnwindSafe, PhantomData<(fn() -> S, fn() -> A, fn() -> B, fn() -> K, fn() -> V, fn() -> R, fn() -> Acc, fn() -> Sc)>: RefUnwindSafe,

§

impl<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc> Send for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc>
where EA: Send, EB: Send, KA: Send, KB: Send, C: Send, D: Send, PhantomData<(fn() -> S, fn() -> A, fn() -> B, fn() -> K, fn() -> V, fn() -> R, fn() -> Acc, fn() -> Sc)>: Send,

§

impl<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc> Sync for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc>
where EA: Sync, EB: Sync, KA: Sync, KB: Sync, C: Sync, D: Sync, PhantomData<(fn() -> S, fn() -> A, fn() -> B, fn() -> K, fn() -> V, fn() -> R, fn() -> Acc, fn() -> Sc)>: Sync,

§

impl<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc> Unpin for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc>
where EA: Unpin, EB: Unpin, KA: Unpin, KB: Unpin, C: Unpin, D: Unpin, PhantomData<(fn() -> S, fn() -> A, fn() -> B, fn() -> K, fn() -> V, fn() -> R, fn() -> Acc, fn() -> Sc)>: Unpin,

§

impl<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc> UnsafeUnpin for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc>
where EA: UnsafeUnpin, EB: UnsafeUnpin, KA: UnsafeUnpin, KB: UnsafeUnpin, C: UnsafeUnpin, D: UnsafeUnpin, PhantomData<(fn() -> S, fn() -> A, fn() -> B, fn() -> K, fn() -> V, fn() -> R, fn() -> Acc, fn() -> Sc)>: UnsafeUnpin,

§

impl<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc> UnwindSafe for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, V, R, Acc, D, Sc>
where EA: UnwindSafe, EB: UnwindSafe, KA: UnwindSafe, KB: UnwindSafe, C: UnwindSafe, D: UnwindSafe, PhantomData<(fn() -> S, fn() -> A, fn() -> B, fn() -> K, fn() -> V, fn() -> R, fn() -> Acc, fn() -> Sc)>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.