pub struct ComplementedConstraintStream<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, Sc>where
Sc: Score,{ /* private fields */ }Expand description
Zero-erasure constraint stream with complemented groups.
ComplementedConstraintStream results from calling complement on a
GroupedConstraintStream. It ensures all keys from a complement source
are represented, using default values for missing keys.
The key function for A entities returns Option<K> to allow skipping
entities without valid keys (e.g., unassigned shifts).
§Type Parameters
S- Solution typeA- Original entity type (e.g., Shift)B- Complement entity type (e.g., Employee)K- Group key typeEA- Extractor for A entitiesFi- Filter on A entities, from the stream the group was built onEB- Extractor for B entities (complement source)KA- Key function for A (returnsOption<K>to allow filtering)KB- Key function for BC- Collector typeD- Default value functionSc- Score type
§Example
use solverforge_core::score::SoftScore;
use solverforge_scoring::api::constraint_set::IncrementalConstraint;
use solverforge_scoring::stream::collector::count;
use solverforge_scoring::stream::ConstraintFactory;
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
struct Employee {
id: usize,
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
struct Shift {
employee_id: usize,
}
#[derive(Clone)]
struct Schedule {
employees: Vec<Employee>,
shifts: Vec<Shift>,
}
// Count shifts per employee, including employees with 0 shifts
let constraint = ConstraintFactory::<Schedule, SoftScore>::new()
.for_each(Schedule::shifts())
.group_by(|shift: &Shift| shift.employee_id, count())
.complement(
Schedule::employees(),
|emp: &Employee| emp.id,
|_emp: &Employee| 0usize,
)
.penalize(|_employee_id: &usize, count: &usize| SoftScore::of(*count as i64))
.named("Shift count");
let schedule = Schedule {
employees: vec![Employee { id: 0 }, Employee { id: 1 }, Employee { id: 2 }],
shifts: vec![
Shift { employee_id: 0 },
Shift { employee_id: 0 },
// Employee 1 has 0 shifts, Employee 2 has 0 shifts
],
};
// Employee 0: 2, Employee 1: 0, Employee 2: 0 → Total: -2
assert_eq!(constraint.evaluate(&schedule), SoftScore::of(-2));Implementations§
Source§impl<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, Sc> ComplementedConstraintStream<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, Sc>where
S: Send + Sync + 'static,
A: Send + Sync + 'static,
B: Send + Sync + 'static,
K: Clone + Eq + Hash + Send + Sync + 'static,
EA: CollectionExtract<S, Item = A>,
Fi: UniFilter<S, A>,
EB: CollectionExtract<S, Item = B>,
KA: Fn(&A) -> Option<K> + Send + Sync,
KB: Fn(&B) -> K + Send + Sync,
C: for<'i> Collector<&'i A, Value = V, Result = R, Accumulator = Acc> + Send + Sync + 'static,
V: Send + Sync + 'static,
R: Send + Sync + 'static,
Acc: Accumulator<V, R> + Send + Sync + 'static,
D: Fn(&B) -> R + Send + Sync + 'static,
Sc: Score + 'static,
impl<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, Sc> ComplementedConstraintStream<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, Sc>where
S: Send + Sync + 'static,
A: Send + Sync + 'static,
B: Send + Sync + 'static,
K: Clone + Eq + Hash + Send + Sync + 'static,
EA: CollectionExtract<S, Item = A>,
Fi: UniFilter<S, A>,
EB: CollectionExtract<S, Item = B>,
KA: Fn(&A) -> Option<K> + Send + Sync,
KB: Fn(&B) -> K + Send + Sync,
C: for<'i> Collector<&'i A, Value = V, Result = R, Accumulator = Acc> + Send + Sync + 'static,
V: Send + Sync + 'static,
R: Send + Sync + 'static,
Acc: Accumulator<V, R> + Send + Sync + 'static,
D: Fn(&B) -> R + Send + Sync + 'static,
Sc: Score + 'static,
Sourcepub fn penalize<W>(
self,
weight: W,
) -> ComplementedConstraintBuilder<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, impl Fn(&K, &R) -> Sc + Send + Sync, Sc>
pub fn penalize<W>( self, weight: W, ) -> ComplementedConstraintBuilder<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, impl Fn(&K, &R) -> Sc + Send + Sync, Sc>
Penalizes each key by weight.
weight is a fixed score, or a closure over the matched (&key, &result) returning one.
A fixed weight with a non-zero hard level marks the constraint hard; closures
are soft unless wrapped in hard_weight.
Sourcepub fn reward<W>(
self,
weight: W,
) -> ComplementedConstraintBuilder<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, impl Fn(&K, &R) -> Sc + Send + Sync, Sc>
pub fn reward<W>( self, weight: W, ) -> ComplementedConstraintBuilder<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, impl Fn(&K, &R) -> Sc + Send + Sync, Sc>
Rewards each key by weight.
weight is a fixed score, or a closure over the matched (&key, &result) returning one.
A fixed weight with a non-zero hard level marks the constraint hard; closures
are soft unless wrapped in hard_weight.