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 typeA- Primary entity type (scored)B- Secondary entity type (checked for existence)K- Join key typeEA- Extractor for A entitiesEB- Extractor for B entitiesKA- Key extractor for AKB- Key extractor for BFA- Filter on A entitiesW- Weight function for A entitiesSc- 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),
|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>
impl<S, A, B, K, EA, EB, KA, KB, FA, W, Sc> IfExistsUniConstraint<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
Sourcepub 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
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>
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§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(&A) -> bool + Send + Sync,
W: Fn(&A) -> Sc + Send + Sync,
Sc: Score,
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(&A) -> bool + Send + Sync,
W: Fn(&A) -> Sc + Send + Sync,
Sc: Score,
Source§fn match_count(&self, solution: &S) -> usize
fn match_count(&self, solution: &S) -> usize
Returns the number of matches for this constraint.
Source§fn initialize(&mut self, solution: &S) -> Sc
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
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
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 constraint_ref(&self) -> ConstraintRef
fn constraint_ref(&self) -> ConstraintRef
Returns the constraint reference (package + name). Read more
Source§fn get_matches(&self, _solution: &S) -> Vec<DetailedConstraintMatch<Sc>>
fn get_matches(&self, _solution: &S) -> Vec<DetailedConstraintMatch<Sc>>
Returns detailed matches with entity justifications. 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>
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>where
EA: RefUnwindSafe,
EB: RefUnwindSafe,
KA: RefUnwindSafe,
KB: RefUnwindSafe,
FA: RefUnwindSafe,
W: RefUnwindSafe,
S: RefUnwindSafe,
A: RefUnwindSafe,
B: RefUnwindSafe,
K: RefUnwindSafe,
Sc: RefUnwindSafe,
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>
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>
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>
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>where
EA: UnwindSafe,
EB: UnwindSafe,
KA: UnwindSafe,
KB: UnwindSafe,
FA: UnwindSafe,
W: UnwindSafe,
S: UnwindSafe,
A: UnwindSafe,
B: UnwindSafe,
K: UnwindSafe,
Sc: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more