GroupedUniConstraint

Struct GroupedUniConstraint 

Source
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 type
  • A - Entity type
  • K - Group key type
  • E - Extractor function for entities
  • KF - Key function
  • C - Collector type
  • W - Weight function
  • Sc - 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,

Source

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 constraint
  • impact_type - Whether to penalize or reward
  • extractor - Function to get entity slice from solution
  • key_fn - Function to extract group key from entity
  • collector - Collector to aggregate entities per group
  • weight_fn - Function to compute score from collector result
  • is_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,

Source§

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

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

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, K, E, KF, C, W, Sc> Freeze for GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>
where E: Freeze, KF: Freeze, C: Freeze, W: Freeze,

§

impl<S, A, K, E, KF, C, W, Sc> RefUnwindSafe for GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>

§

impl<S, A, K, E, KF, C, W, Sc> Send for GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>
where E: Send, KF: Send, W: Send, S: Send, A: Send, K: Send, <C as UniCollector<A>>::Value: Send,

§

impl<S, A, K, E, KF, C, W, Sc> Sync for GroupedUniConstraint<S, A, K, E, KF, C, W, Sc>
where E: Sync, KF: Sync, W: Sync, S: Sync, A: Sync, K: Sync, <C as UniCollector<A>>::Value: Sync,

§

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>

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.