pub struct GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>where
C: UniCollector<A>,
Sc: Score,{ /* private fields */ }Expand description
Zero-erasure constraint that groups entities by key and scores based on collector results.
This enables incremental scoring for group-by operations:
- Tracks which entities belong to which group
- Maintains collector state per group
- Computes score deltas when entities are added/removed
All type parameters are concrete - no trait objects, no Arc allocations.
§Type Parameters
S- Solution typeA- Entity typeK- Group key typeE- Extractor function for entitiesKF- Key functionC- Collector typeW- Weight functionSc- Score type
§Example
use solverforge_scoring::constraint::grouped::GroupedUniConstraint;
use solverforge_scoring::stream::collector::count;
use solverforge_scoring::api::constraint_set::IncrementalConstraint;
use solverforge_core::{ConstraintRef, ImpactType};
use solverforge_core::score::SimpleScore;
#[derive(Clone, Hash, PartialEq, Eq)]
struct Shift { employee_id: usize }
#[derive(Clone)]
struct Solution { shifts: Vec<Shift> }
// Penalize based on squared workload per employee
let constraint = GroupedUniConstraint::new(
ConstraintRef::new("", "Balanced workload"),
ImpactType::Penalty,
|s: &Solution| &s.shifts,
|shift: &Shift| shift.employee_id,
count::<Shift>(),
|count: &usize| SimpleScore::of((*count * *count) as i64),
false,
);
let solution = Solution {
shifts: vec![
Shift { employee_id: 1 },
Shift { employee_id: 1 },
Shift { employee_id: 1 },
Shift { employee_id: 2 },
],
};
// Employee 1: 3 shifts -> 9 penalty
// Employee 2: 1 shift -> 1 penalty
// Total: -10
assert_eq!(constraint.evaluate(&solution), SimpleScore::of(-10));Implementations§
Source§impl<S, A, K, E, KF, C, W, Sc> GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>where
S: Send + Sync + 'static,
A: Clone + Send + Sync + 'static,
K: Clone + Eq + Hash + Send + Sync + 'static,
E: Fn(&S) -> &[A] + Send + Sync,
KF: Fn(&A) -> K + Send + Sync,
C: UniCollector<A> + Send + Sync + 'static,
C::Accumulator: Send + Sync,
C::Result: Send + Sync,
W: Fn(&C::Result) -> Sc + Send + Sync,
Sc: Score + 'static,
impl<S, A, K, E, KF, C, W, Sc> GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>where
S: Send + Sync + 'static,
A: Clone + Send + Sync + 'static,
K: Clone + Eq + Hash + Send + Sync + 'static,
E: Fn(&S) -> &[A] + Send + Sync,
KF: Fn(&A) -> K + Send + Sync,
C: UniCollector<A> + Send + Sync + 'static,
C::Accumulator: Send + Sync,
C::Result: Send + Sync,
W: Fn(&C::Result) -> Sc + Send + Sync,
Sc: Score + 'static,
Sourcepub fn new(
constraint_ref: ConstraintRef,
impact_type: ImpactType,
extractor: E,
key_fn: KF,
collector: C,
weight_fn: W,
is_hard: bool,
) -> Self
pub fn new( constraint_ref: ConstraintRef, impact_type: ImpactType, extractor: E, key_fn: KF, collector: C, weight_fn: W, is_hard: bool, ) -> Self
Creates a new zero-erasure grouped constraint.
§Arguments
constraint_ref- Identifier for this constraintimpact_type- Whether to penalize or rewardextractor- Function to get entity slice from solutionkey_fn- Function to extract group key from entitycollector- Collector to aggregate entities per groupweight_fn- Function to compute score from collector resultis_hard- Whether this is a hard constraint
Trait Implementations§
Source§impl<S, A, K, E, KF, C, W, Sc> Debug for GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>where
C: UniCollector<A>,
Sc: Score,
impl<S, A, K, E, KF, C, W, Sc> Debug for GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>where
C: UniCollector<A>,
Sc: Score,
Source§impl<S, A, K, E, KF, C, W, Sc> IncrementalConstraint<S, Sc> for GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>where
S: Send + Sync + 'static,
A: Clone + Send + Sync + 'static,
K: Clone + Eq + Hash + Send + Sync + 'static,
E: Fn(&S) -> &[A] + Send + Sync,
KF: Fn(&A) -> K + Send + Sync,
C: UniCollector<A> + Send + Sync + 'static,
C::Accumulator: Send + Sync,
C::Result: Send + Sync,
C::Value: Send + Sync,
W: Fn(&C::Result) -> Sc + Send + Sync,
Sc: Score + 'static,
impl<S, A, K, E, KF, C, W, Sc> IncrementalConstraint<S, Sc> for GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>where
S: Send + Sync + 'static,
A: Clone + Send + Sync + 'static,
K: Clone + Eq + Hash + Send + Sync + 'static,
E: Fn(&S) -> &[A] + Send + Sync,
KF: Fn(&A) -> K + Send + Sync,
C: UniCollector<A> + Send + Sync + 'static,
C::Accumulator: Send + Sync,
C::Result: Send + Sync,
C::Value: Send + Sync,
W: Fn(&C::Result) -> Sc + Send + Sync,
Sc: Score + 'static,
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, K, E, KF, C, W, Sc> Freeze for GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>
impl<S, A, K, E, KF, C, W, Sc> RefUnwindSafe for GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>where
E: RefUnwindSafe,
KF: RefUnwindSafe,
C: RefUnwindSafe,
W: RefUnwindSafe,
S: RefUnwindSafe,
A: RefUnwindSafe,
Sc: RefUnwindSafe,
K: RefUnwindSafe,
<C as UniCollector<A>>::Accumulator: RefUnwindSafe,
<C as UniCollector<A>>::Value: RefUnwindSafe,
impl<S, A, K, E, KF, C, W, Sc> Send for GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>
impl<S, A, K, E, KF, C, W, Sc> Sync for GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>
impl<S, A, K, E, KF, C, W, Sc> Unpin for GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>where
E: Unpin,
KF: Unpin,
C: Unpin,
W: Unpin,
S: Unpin,
A: Unpin,
Sc: Unpin,
K: Unpin,
<C as UniCollector<A>>::Accumulator: Unpin,
<C as UniCollector<A>>::Value: Unpin,
impl<S, A, K, E, KF, C, W, Sc> UnwindSafe for GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>where
E: UnwindSafe,
KF: UnwindSafe,
C: UnwindSafe,
W: UnwindSafe,
K: UnwindSafe,
<C as UniCollector<A>>::Accumulator: UnwindSafe,
<C as UniCollector<A>>::Value: UnwindSafe,
S: UnwindSafe,
A: 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