use std::hash::Hash;
use std::marker::PhantomData;
use solverforge_core::score::Score;
use solverforge_core::{ConstraintRef, ImpactType};
use crate::constraint::if_exists::{ExistenceMode, IfExistsUniConstraint};
use super::collection_extract::CollectionExtract;
use super::filter::UniFilter;
pub struct IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>
where
Sc: Score,
{
mode: ExistenceMode,
extractor_a: EA,
extractor_b: EB,
key_a: KA,
key_b: KB,
filter_a: FA,
_phantom: PhantomData<(fn() -> S, fn() -> A, fn() -> B, fn() -> K, fn() -> Sc)>,
}
impl<S, A, B, K, EA, EB, KA, KB, FA, Sc> IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>
where
S: Send + Sync + 'static,
A: Clone + Send + Sync + 'static,
B: Clone + Send + Sync + 'static,
K: Eq + Hash + Clone + Send + Sync,
EA: CollectionExtract<S, Item = A>,
EB: Fn(&S) -> Vec<B> + Send + Sync,
KA: Fn(&A) -> K + Send + Sync,
KB: Fn(&B) -> K + Send + Sync,
FA: UniFilter<S, A>,
Sc: Score + 'static,
{
pub(crate) fn new(
mode: ExistenceMode,
extractor_a: EA,
extractor_b: EB,
key_a: KA,
key_b: KB,
filter_a: FA,
) -> Self {
Self {
mode,
extractor_a,
extractor_b,
key_a,
key_b,
filter_a,
_phantom: PhantomData,
}
}
pub fn penalize(
self,
weight: Sc,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, impl Fn(&A) -> Sc + Send + Sync, Sc>
where
Sc: Copy,
{
let is_hard = weight
.to_level_numbers()
.first()
.map(|&h| h != 0)
.unwrap_or(false);
IfExistsBuilder {
mode: self.mode,
extractor_a: self.extractor_a,
extractor_b: self.extractor_b,
key_a: self.key_a,
key_b: self.key_b,
filter_a: self.filter_a,
impact_type: ImpactType::Penalty,
weight: move |_: &A| weight,
is_hard,
_phantom: PhantomData,
}
}
pub fn penalize_with<W>(
self,
weight_fn: W,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
where
W: Fn(&A) -> Sc + Send + Sync,
{
IfExistsBuilder {
mode: self.mode,
extractor_a: self.extractor_a,
extractor_b: self.extractor_b,
key_a: self.key_a,
key_b: self.key_b,
filter_a: self.filter_a,
impact_type: ImpactType::Penalty,
weight: weight_fn,
is_hard: false,
_phantom: PhantomData,
}
}
pub fn penalize_hard_with<W>(
self,
weight_fn: W,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
where
W: Fn(&A) -> Sc + Send + Sync,
{
IfExistsBuilder {
mode: self.mode,
extractor_a: self.extractor_a,
extractor_b: self.extractor_b,
key_a: self.key_a,
key_b: self.key_b,
filter_a: self.filter_a,
impact_type: ImpactType::Penalty,
weight: weight_fn,
is_hard: true,
_phantom: PhantomData,
}
}
pub fn reward(
self,
weight: Sc,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, impl Fn(&A) -> Sc + Send + Sync, Sc>
where
Sc: Copy,
{
let is_hard = weight
.to_level_numbers()
.first()
.map(|&h| h != 0)
.unwrap_or(false);
IfExistsBuilder {
mode: self.mode,
extractor_a: self.extractor_a,
extractor_b: self.extractor_b,
key_a: self.key_a,
key_b: self.key_b,
filter_a: self.filter_a,
impact_type: ImpactType::Reward,
weight: move |_: &A| weight,
is_hard,
_phantom: PhantomData,
}
}
pub fn reward_with<W>(
self,
weight_fn: W,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
where
W: Fn(&A) -> Sc + Send + Sync,
{
IfExistsBuilder {
mode: self.mode,
extractor_a: self.extractor_a,
extractor_b: self.extractor_b,
key_a: self.key_a,
key_b: self.key_b,
filter_a: self.filter_a,
impact_type: ImpactType::Reward,
weight: weight_fn,
is_hard: false,
_phantom: PhantomData,
}
}
pub fn reward_hard_with<W>(
self,
weight_fn: W,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
where
W: Fn(&A) -> Sc + Send + Sync,
{
IfExistsBuilder {
mode: self.mode,
extractor_a: self.extractor_a,
extractor_b: self.extractor_b,
key_a: self.key_a,
key_b: self.key_b,
filter_a: self.filter_a,
impact_type: ImpactType::Reward,
weight: weight_fn,
is_hard: true,
_phantom: PhantomData,
}
}
pub fn penalize_hard(
self,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, impl Fn(&A) -> Sc + Send + Sync, Sc>
where
Sc: Copy,
{
self.penalize(Sc::one_hard())
}
pub fn penalize_soft(
self,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, impl Fn(&A) -> Sc + Send + Sync, Sc>
where
Sc: Copy,
{
self.penalize(Sc::one_soft())
}
pub fn reward_hard(
self,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, impl Fn(&A) -> Sc + Send + Sync, Sc>
where
Sc: Copy,
{
self.reward(Sc::one_hard())
}
pub fn reward_soft(
self,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, impl Fn(&A) -> Sc + Send + Sync, Sc>
where
Sc: Copy,
{
self.reward(Sc::one_soft())
}
}
impl<S, A, B, K, EA, EB, KA, KB, FA, Sc: Score> std::fmt::Debug
for IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("IfExistsStream")
.field("mode", &self.mode)
.finish()
}
}
pub struct IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
where
Sc: Score,
{
mode: ExistenceMode,
extractor_a: EA,
extractor_b: EB,
key_a: KA,
key_b: KB,
filter_a: FA,
impact_type: ImpactType,
weight: W,
is_hard: bool,
_phantom: PhantomData<(fn() -> S, fn() -> A, fn() -> B, fn() -> K, fn() -> Sc)>,
}
impl<S, A, B, K, EA, EB, KA, KB, FA, W, Sc> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
where
S: Send + Sync + 'static,
A: Clone + Send + Sync + 'static,
B: Clone + Send + Sync + 'static,
K: Eq + Hash + Clone + Send + Sync,
EA: CollectionExtract<S, Item = A>,
EB: Fn(&S) -> Vec<B> + Send + Sync,
KA: Fn(&A) -> K + Send + Sync,
KB: Fn(&B) -> K + Send + Sync,
FA: UniFilter<S, A>,
W: Fn(&A) -> Sc + Send + Sync,
Sc: Score + 'static,
{
pub fn named(
self,
name: &str,
) -> IfExistsUniConstraint<
S,
A,
B,
K,
EA,
EB,
KA,
KB,
impl Fn(&S, &A) -> bool + Send + Sync,
W,
Sc,
> {
let filter = self.filter_a;
let combined_filter = move |s: &S, a: &A| filter.test(s, a);
IfExistsUniConstraint::new(
ConstraintRef::new("", name),
self.impact_type,
self.mode,
self.extractor_a,
self.extractor_b,
self.key_a,
self.key_b,
combined_filter,
self.weight,
self.is_hard,
)
}
}
impl<S, A, B, K, EA, EB, KA, KB, FA, W, Sc: Score> std::fmt::Debug
for IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("IfExistsBuilder")
.field("mode", &self.mode)
.field("impact_type", &self.impact_type)
.finish()
}
}