IfExistsStream

Struct IfExistsStream 

Source
pub struct IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>
where Sc: Score,
{ /* private fields */ }
Expand description

Zero-erasure stream for building if_exists/if_not_exists constraints.

Created by UniConstraintStream::if_exists() or if_not_exists(). Filters A entities based on whether a matching B exists.

§Type Parameters

  • S - Solution type
  • A - Primary entity type (scored)
  • B - Secondary entity type (checked for existence)
  • K - Join key type
  • EA - Extractor for A entities
  • EB - Extractor for B entities (returns Vec for filtering)
  • KA - Key extractor for A
  • KB - Key extractor for B
  • FA - Filter on A entities
  • Sc - Score type

§Example

use solverforge_scoring::stream::ConstraintFactory;
use solverforge_scoring::stream::joiner::equal_bi;
use solverforge_scoring::api::constraint_set::IncrementalConstraint;
use solverforge_core::score::SimpleScore;

#[derive(Clone)]
struct Shift { id: usize, employee_idx: Option<usize> }

#[derive(Clone)]
struct Employee { id: usize, on_vacation: bool }

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

// Penalize shifts assigned to employees who are on vacation
let constraint = ConstraintFactory::<Schedule, SimpleScore>::new()
    .for_each(|s: &Schedule| s.shifts.as_slice())
    .filter(|shift: &Shift| shift.employee_idx.is_some())
    .if_exists_filtered(
        |s: &Schedule| s.employees.iter().filter(|e| e.on_vacation).cloned().collect(),
        equal_bi(
            |shift: &Shift| shift.employee_idx,
            |emp: &Employee| Some(emp.id),
        ),
    )
    .penalize(SimpleScore::of(1))
    .as_constraint("Vacation conflict");

let schedule = Schedule {
    shifts: vec![
        Shift { id: 0, employee_idx: Some(0) },
        Shift { id: 1, employee_idx: Some(1) },
        Shift { id: 2, employee_idx: None },
    ],
    employees: vec![
        Employee { id: 0, on_vacation: true },
        Employee { id: 1, on_vacation: false },
    ],
};

// Shift 0 is assigned to employee 0 who is on vacation
assert_eq!(constraint.evaluate(&schedule), SimpleScore::of(-1));

Implementations§

Source§

impl<S, A, B, K, EA, EB, KA, KB, FA, Sc> IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, 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: UniFilter<S, A>, Sc: Score + 'static,

Source

pub fn penalize( self, weight: Sc, ) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, impl Fn(&A) -> Sc + Send + Sync, Sc>
where Sc: Copy,

Penalizes each matching entity with a fixed weight.

Source

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

Penalizes each matching entity with a dynamic weight.

Source

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

Penalizes each matching entity with a dynamic weight, explicitly marked as hard.

Source

pub fn reward( self, weight: Sc, ) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, impl Fn(&A) -> Sc + Send + Sync, Sc>
where Sc: Copy,

Rewards each matching entity with a fixed weight.

Source

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

Rewards each matching entity with a dynamic weight.

Source

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

Rewards each matching entity with a dynamic weight, explicitly marked as hard.

Trait Implementations§

Source§

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

§

impl<S, A, B, K, EA, EB, KA, KB, FA, Sc> RefUnwindSafe for IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>

§

impl<S, A, B, K, EA, EB, KA, KB, FA, Sc> Send for IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>
where EA: Send, EB: Send, KA: Send, KB: Send, FA: Send, S: Send, A: Send, B: Send, K: Send,

§

impl<S, A, B, K, EA, EB, KA, KB, FA, Sc> Sync for IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>
where EA: Sync, EB: Sync, KA: Sync, KB: Sync, FA: Sync, S: Sync, A: Sync, B: Sync, K: Sync,

§

impl<S, A, B, K, EA, EB, KA, KB, FA, Sc> Unpin for IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>
where EA: Unpin, EB: Unpin, KA: Unpin, KB: Unpin, FA: Unpin, S: Unpin, A: Unpin, B: Unpin, K: Unpin, Sc: Unpin,

§

impl<S, A, B, K, EA, EB, KA, KB, FA, Sc> UnwindSafe for IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, 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.