Constraint Streams
Build constraints with forEach, filter, join, groupBy, and more
This section covers the constraint streams API for defining constraints.
Constraints are defined as pipelines of stream operations:
use solverforge_core::{Collector, StreamComponent, WasmFunction};
use indexmap::IndexMap;
let mut constraints = IndexMap::new();
constraints.insert(
"requiredSkill".to_string(),
vec![
StreamComponent::for_each("Shift"),
StreamComponent::filter(WasmFunction::new("skillMismatch")),
StreamComponent::penalize("1hard/0soft"),
],
);
Every constraint follows this pattern:
Source → [Filter/Join/Group] → Scoring
for_each, for_each_unique_pair)Penalties and rewards use score format strings:
| Format | Score Type | Example |
|---|---|---|
"1hard" | Hard constraint | "1hard/0soft" |
"1soft" | Soft constraint | "0hard/1soft" |
"1hard/5soft" | Combined | Hard and soft |
"1" | Simple | Single dimension |
// Penalize unassigned shifts
StreamComponent::for_each("Shift"),
StreamComponent::filter(WasmFunction::new("hasNoEmployee")),
StreamComponent::penalize("1hard/0soft"),
// Penalize overlapping shifts for same employee
StreamComponent::for_each("Shift"),
StreamComponent::join_with_joiners("Shift", vec![
Joiner::equal(WasmFunction::new("get_Shift_employee"))
]),
StreamComponent::filter(WasmFunction::new("shiftsOverlap")),
StreamComponent::penalize("1hard/0soft"),
// Balance shift assignments across employees
StreamComponent::for_each("Shift"),
StreamComponent::group_by(
vec![WasmFunction::new("get_Shift_employee")],
vec![Collector::count()]
),
StreamComponent::complement("Employee"),
StreamComponent::group_by(
vec![],
vec![Collector::load_balance_with_load(
WasmFunction::new("pick1"),
WasmFunction::new("pick2")
)]
),
StreamComponent::penalize_with_weigher("0hard/1soft", WasmFunction::new("scaleByFloat")),
Build constraints with forEach, filter, join, groupBy, and more
Efficiently match entities with equal, lessThan, overlapping, and filtering joiners
Aggregate values with count, sum, average, min, max, toList, toSet, and loadBalance
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.