Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ where
let b_changed = self
.b_source
.assert_localizes(descriptor_index, &self.constraint_ref.name);
let mut total = Sc::zero();

if !a_changed && !b_changed {
return total;
}

let entities_a = self.extractor_a.extract(solution);
let entities_b = self.extractor_b.extract(solution);
let mut total = Sc::zero();
if a_changed {
total = total + self.insert_a(solution, entities_a, entities_b, entity_index);
}
Expand All @@ -108,6 +113,11 @@ where
.b_source
.assert_localizes(descriptor_index, &self.constraint_ref.name);
let mut total = Sc::zero();

if !a_changed && !b_changed {
return total;
}

if a_changed {
total = total + self.retract_a(entity_index);
}
Expand Down
77 changes: 76 additions & 1 deletion crates/solverforge-scoring/src/constraint/tests/cross_bi_incr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};

use crate::api::constraint_set::IncrementalConstraint;
use crate::constraint::IncrementalCrossBiConstraint;
use crate::stream::collection_extract::{source, ChangeSource};
use crate::stream::collection_extract::{source, ChangeSource, CollectionExtract};
use crate::stream::joiner::equal_bi;
use crate::stream::ConstraintFactory;
use solverforge_core::score::{Score, SoftScore};
Expand All @@ -24,6 +29,42 @@ struct Schedule {
employees: Vec<Employee>,
}

#[derive(Clone)]
struct CountingShiftExtract {
calls: Arc<AtomicUsize>,
}

impl CollectionExtract<Schedule> for CountingShiftExtract {
type Item = Shift;

fn extract<'s>(&self, schedule: &'s Schedule) -> &'s [Self::Item] {
self.calls.fetch_add(1, Ordering::Relaxed);
schedule.shifts.as_slice()
}

fn change_source(&self) -> ChangeSource {
ChangeSource::Descriptor(0)
}
}

#[derive(Clone)]
struct CountingEmployeeExtract {
calls: Arc<AtomicUsize>,
}

impl CollectionExtract<Schedule> for CountingEmployeeExtract {
type Item = Employee;

fn extract<'s>(&self, schedule: &'s Schedule) -> &'s [Self::Item] {
self.calls.fetch_add(1, Ordering::Relaxed);
schedule.employees.as_slice()
}

fn change_source(&self) -> ChangeSource {
ChangeSource::Descriptor(1)
}
}

fn create_unavailable_employee_constraint() -> impl IncrementalConstraint<Schedule, SoftScore> {
IncrementalCrossBiConstraint::new(
ConstraintRef::new("", "Unavailable employee"),
Expand Down Expand Up @@ -65,6 +106,40 @@ fn sample_schedule() -> Schedule {
}
}

#[test]
fn cross_bi_unrelated_insert_skips_extractors() {
let shift_extract_calls = Arc::new(AtomicUsize::new(0));
let employee_extract_calls = Arc::new(AtomicUsize::new(0));
let mut constraint = IncrementalCrossBiConstraint::new(
ConstraintRef::new("", "Unavailable employee"),
ImpactType::Penalty,
CountingShiftExtract {
calls: Arc::clone(&shift_extract_calls),
},
CountingEmployeeExtract {
calls: Arc::clone(&employee_extract_calls),
},
|shift: &Shift| shift.employee_id,
|employee: &Employee| Some(employee.id),
|_schedule: &Schedule, shift: &Shift, employee: &Employee| {
shift.employee_id.is_some() && employee.unavailable_days.contains(&shift.day)
},
|_schedule: &Schedule, _shift_idx: usize, _employee_idx: usize| SoftScore::of(1),
false,
);
let schedule = sample_schedule();

assert_eq!(constraint.initialize(&schedule), SoftScore::of(-1));
shift_extract_calls.store(0, Ordering::Relaxed);
employee_extract_calls.store(0, Ordering::Relaxed);

let delta = constraint.on_insert(&schedule, 0, 2);

assert_eq!(delta, SoftScore::zero());
assert_eq!(shift_extract_calls.load(Ordering::Relaxed), 0);
assert_eq!(employee_extract_calls.load(Ordering::Relaxed), 0);
}

#[test]
fn test_cross_bi_evaluate_works_without_initialize() {
let constraint = create_unavailable_employee_constraint();
Expand Down