ComplementedConstraintStream

Struct ComplementedConstraintStream 

Source
pub struct ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, 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_scoring::stream::ConstraintFactory;
use solverforge_scoring::stream::collector::count;
use solverforge_scoring::api::constraint_set::IncrementalConstraint;
use solverforge_core::score::SimpleScore;

#[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, SimpleScore>::new()
    .for_each(|s: &Schedule| &s.shifts)
    .group_by(|shift: &Shift| shift.employee_id, count())
    .complement(
        |s: &Schedule| s.employees.as_slice(),
        |emp: &Employee| emp.id,
        |_emp: &Employee| 0usize,
    )
    .penalize_with(|count: &usize| SimpleScore::of(*count as i64))
    .as_constraint("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), SimpleScore::of(-2));

Implementations§

Source§

impl<S, A, B, K, EA, EB, KA, KB, C, D, Sc> ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, 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: 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 + 'static, C::Accumulator: Send + Sync, C::Result: Clone + Send + Sync, D: Fn(&B) -> C::Result + Send + Sync, Sc: Score + 'static,

Source

pub fn penalize_with<W>( self, weight_fn: W, ) -> ComplementedConstraintBuilder<S, A, B, K, EA, EB, KA, KB, C, D, W, Sc>
where W: Fn(&C::Result) -> Sc + Send + Sync,

Penalizes each complemented group with a weight based on the result.

Source

pub fn penalize_hard_with<W>( self, weight_fn: W, ) -> ComplementedConstraintBuilder<S, A, B, K, EA, EB, KA, KB, C, D, W, Sc>
where W: Fn(&C::Result) -> Sc + Send + Sync,

Penalizes each complemented group, explicitly marked as hard constraint.

Source

pub fn reward_with<W>( self, weight_fn: W, ) -> ComplementedConstraintBuilder<S, A, B, K, EA, EB, KA, KB, C, D, W, Sc>
where W: Fn(&C::Result) -> Sc + Send + Sync,

Rewards each complemented group with a weight based on the result.

Source

pub fn reward_hard_with<W>( self, weight_fn: W, ) -> ComplementedConstraintBuilder<S, A, B, K, EA, EB, KA, KB, C, D, W, Sc>
where W: Fn(&C::Result) -> Sc + Send + Sync,

Rewards each complemented group, explicitly marked as hard constraint.

Trait Implementations§

Source§

impl<S, A, B, K, EA, EB, KA, KB, C, D, Sc: Score> Debug for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, 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, D, Sc> Freeze for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, D, Sc>
where EA: Freeze, EB: Freeze, KA: Freeze, KB: Freeze, C: Freeze, D: Freeze,

§

impl<S, A, B, K, EA, EB, KA, KB, C, D, Sc> RefUnwindSafe for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, D, Sc>

§

impl<S, A, B, K, EA, EB, KA, KB, C, D, Sc> Send for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, D, Sc>
where EA: Send, EB: Send, KA: Send, KB: Send, C: Send, D: Send, S: Send, A: Send, B: Send, K: Send,

§

impl<S, A, B, K, EA, EB, KA, KB, C, D, Sc> Sync for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, D, Sc>
where EA: Sync, EB: Sync, KA: Sync, KB: Sync, C: Sync, D: Sync, S: Sync, A: Sync, B: Sync, K: Sync,

§

impl<S, A, B, K, EA, EB, KA, KB, C, D, Sc> Unpin for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, D, Sc>
where EA: Unpin, EB: Unpin, KA: Unpin, KB: Unpin, C: Unpin, D: Unpin, S: Unpin, A: Unpin, B: Unpin, K: Unpin, Sc: Unpin,

§

impl<S, A, B, K, EA, EB, KA, KB, C, D, Sc> UnwindSafe for ComplementedConstraintStream<S, A, B, K, EA, EB, KA, KB, C, D, Sc>

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.