IfExistsUniConstraint

Struct IfExistsUniConstraint 

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

Zero-erasure uni-constraint with existence filtering.

Scores A entities based on whether a matching B entity exists (or doesn’t exist) in another collection. Unlike join, this produces a uni-constraint over A.

§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
  • KA - Key extractor for A
  • KB - Key extractor for B
  • FA - Filter on A entities
  • W - Weight function for A entities
  • Sc - Score type

§Example

use solverforge_scoring::constraint::if_exists::{IfExistsUniConstraint, ExistenceMode};
use solverforge_scoring::api::constraint_set::IncrementalConstraint;
use solverforge_core::{ConstraintRef, ImpactType};
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 = IfExistsUniConstraint::new(
    ConstraintRef::new("", "Vacation conflict"),
    ImpactType::Penalty,
    ExistenceMode::Exists,
    |s: &Schedule| s.shifts.as_slice(),
    |s: &Schedule| s.employees.iter().filter(|e| e.on_vacation).cloned().collect::<Vec<_>>(),
    |shift: &Shift| shift.employee_idx,
    |emp: &Employee| Some(emp.id),
    |_s: &Schedule, shift: &Shift| shift.employee_idx.is_some(),
    |_shift: &Shift| SimpleScore::of(1),
    false,
);

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

// Only shift 0 matches (assigned to employee on vacation)
assert_eq!(constraint.evaluate(&schedule), SimpleScore::of(-1));

Implementations§

Source§

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

Source

pub fn new( constraint_ref: ConstraintRef, impact_type: ImpactType, mode: ExistenceMode, extractor_a: EA, extractor_b: EB, key_a: KA, key_b: KB, filter_a: FA, weight: W, is_hard: bool, ) -> Self

Creates a new if_exists/if_not_exists constraint.

Trait Implementations§

Source§

impl<S, A, B, K, EA, EB, KA, KB, FA, W, Sc: Score> Debug for IfExistsUniConstraint<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>

Source§

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

Formats the value using the given formatter. Read more
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§

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

Full evaluation of this constraint. Read more
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. Read more
Source§

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

Called when an entity is inserted or its variable changes. Read more
Source§

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

Called when an entity is retracted or before its variable changes. Read more
Source§

fn reset(&mut self)

Resets internal state for a new solving session.
Source§

fn name(&self) -> &str

Returns the constraint name.
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). Read more
Source§

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

Returns detailed matches with entity justifications. Read more
Source§

fn weight(&self) -> Sc

Returns the constraint weight (score per match). Read more

Auto Trait Implementations§

§

impl<S, A, B, K, EA, EB, KA, KB, FA, W, Sc> Freeze for IfExistsUniConstraint<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
where EA: Freeze, EB: Freeze, KA: Freeze, KB: Freeze, FA: Freeze, W: Freeze,

§

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

§

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

§

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

§

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

§

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