pub struct BiConstraintStream<S, A, K, E, KE, F, Sc>where
Sc: Score,{ /* private fields */ }Expand description
Same-collection constraint stream over tuples of A entities whose keys are
equal.
Each unordered combination of distinct entities from extractor E with equal
keys KE appears once, in increasing index order, and is kept when filter F
accepts it.
Implementations§
Source§impl<S, A, K, E, KE, Sc> BiConstraintStream<S, A, K, E, KE, TrueFilter, Sc>
impl<S, A, K, E, KE, Sc> BiConstraintStream<S, A, K, E, KE, TrueFilter, Sc>
Sourcepub fn new_self_join(extractor: E, key_extractor: KE) -> Self
pub fn new_self_join(extractor: E, key_extractor: KE) -> Self
Creates a stream over every equal-key tuple, with no filter.
Source§impl<S, A, K, E, KE, F, Sc> BiConstraintStream<S, A, K, E, KE, F, Sc>
impl<S, A, K, E, KE, F, Sc> BiConstraintStream<S, A, K, E, KE, F, Sc>
Sourcepub fn new_self_join_with_filter(
extractor: E,
key_extractor: KE,
filter: F,
) -> Self
pub fn new_self_join_with_filter( extractor: E, key_extractor: KE, filter: F, ) -> Self
Creates a stream over every equal-key tuple accepted by filter.
Sourcepub fn filter<P>(
self,
predicate: P,
) -> BiConstraintStream<S, A, K, E, KE, AndBiFilter<F, FnBiFilter<impl Fn(&S, &A, &A, usize, usize) -> bool + Send + Sync>>, Sc>
pub fn filter<P>( self, predicate: P, ) -> BiConstraintStream<S, A, K, E, KE, AndBiFilter<F, FnBiFilter<impl Fn(&S, &A, &A, usize, usize) -> bool + Send + Sync>>, Sc>
Keeps only tuples for which predicate returns true, after the existing filter.
Sourcepub fn group_by<GK, GF, Co, V, R, Acc>(
self,
key: GF,
collector: Co,
) -> JoinedGroupedStream<S, FilterNode<SelfJoinNode<S, A, CollectionNode<S, E>, K, KE, 2>, impl Fn(&S, &[Leaf<'_, A>; 2]) -> bool + Send + Sync>, impl Fn(&[Leaf<'_, A>; 2]) -> GK + Send + Sync, TupleCollector<Co, V, R, Acc>, GK, V, R, Acc, Sc>where
E: 'static,
KE: 'static,
F: 'static,
K: 'static,
GK: Eq + Hash + Clone + Send + Sync + 'static,
GF: Fn(&A, &A) -> GK + Send + Sync + 'static,
Co: for<'i> Collector<(&'i A, &'i A), Value = V, Result = R, Accumulator = Acc> + 'static,
V: Send + Sync + 'static,
R: Send + Sync + 'static,
Acc: Accumulator<V, R> + 'static,
for<'i> TupleCollector<Co, V, R, Acc>: Collector<[Leaf<'i, A>; 2], Value = V, Result = R, Accumulator = Acc>,
pub fn group_by<GK, GF, Co, V, R, Acc>(
self,
key: GF,
collector: Co,
) -> JoinedGroupedStream<S, FilterNode<SelfJoinNode<S, A, CollectionNode<S, E>, K, KE, 2>, impl Fn(&S, &[Leaf<'_, A>; 2]) -> bool + Send + Sync>, impl Fn(&[Leaf<'_, A>; 2]) -> GK + Send + Sync, TupleCollector<Co, V, R, Acc>, GK, V, R, Acc, Sc>where
E: 'static,
KE: 'static,
F: 'static,
K: 'static,
GK: Eq + Hash + Clone + Send + Sync + 'static,
GF: Fn(&A, &A) -> GK + Send + Sync + 'static,
Co: for<'i> Collector<(&'i A, &'i A), Value = V, Result = R, Accumulator = Acc> + 'static,
V: Send + Sync + 'static,
R: Send + Sync + 'static,
Acc: Accumulator<V, R> + 'static,
for<'i> TupleCollector<Co, V, R, Acc>: Collector<[Leaf<'i, A>; 2], Value = V, Result = R, Accumulator = Acc>,
Groups each matching tuple, preserving upstream filters.
Sourcepub fn penalize<W>(
self,
weight: W,
) -> BiConstraintBuilder<S, A, K, E, KE, F, impl Fn(&A, &A) -> Sc + Send + Sync, Sc>
pub fn penalize<W>( self, weight: W, ) -> BiConstraintBuilder<S, A, K, E, KE, F, impl Fn(&A, &A) -> Sc + Send + Sync, Sc>
Penalizes each match by weight.
weight is a fixed score, or a closure over the matched entities 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,
) -> BiConstraintBuilder<S, A, K, E, KE, F, impl Fn(&A, &A) -> Sc + Send + Sync, Sc>
pub fn reward<W>( self, weight: W, ) -> BiConstraintBuilder<S, A, K, E, KE, F, impl Fn(&A, &A) -> Sc + Send + Sync, Sc>
Rewards each match by weight.
weight is a fixed score, or a closure over the matched entities returning one.
A fixed weight with a non-zero hard level marks the constraint hard; closures
are soft unless wrapped in hard_weight.
Source§impl<S, A, K, E, KE, F, Sc> BiConstraintStream<S, A, K, E, KE, F, Sc>
join method - transitions to TriConstraintStream
impl<S, A, K, E, KE, F, Sc> BiConstraintStream<S, A, K, E, KE, F, Sc>
join method - transitions to TriConstraintStream
Sourcepub fn join<J>(
self,
joiner: J,
) -> TriConstraintStream<S, A, K, E, KE, impl TriFilter<S, A, A, A>, Sc>where
J: Joiner<A, A> + 'static,
F: 'static,
pub fn join<J>(
self,
joiner: J,
) -> TriConstraintStream<S, A, K, E, KE, impl TriFilter<S, A, A, A>, Sc>where
J: Joiner<A, A> + 'static,
F: 'static,
Joins this stream with a third element to create triples.
§Example
use solverforge_core::score::SoftScore;
use solverforge_scoring::api::constraint_set::IncrementalConstraint;
use solverforge_scoring::stream::joiner::equal;
use solverforge_scoring::stream::ConstraintFactory;
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
struct Task {
team: u32,
}
#[derive(Clone)]
struct Solution {
tasks: Vec<Task>,
}
// Penalize when three tasks are on the same team
let constraint = ConstraintFactory::<Solution, SoftScore>::new()
.for_each(Solution::tasks())
.join(equal(|t: &Task| t.team))
.join(equal(|t: &Task| t.team))
.penalize(SoftScore::of(1))
.named("Team clustering");
let solution = Solution {
tasks: vec![
Task { team: 1 },
Task { team: 1 },
Task { team: 1 },
Task { team: 2 },
],
};
// One triple on team 1: (0, 1, 2) = -1 penalty
assert_eq!(constraint.evaluate(&solution), SoftScore::of(-1));