forEachUniquePair : How is the pair constructed ? #2130
Replies: 2 comments 3 replies
-
|
Hello @CrommVardek! Pairs are considered equal if they contain the same elements, so (A,B) and (B,A) would be considered equal and therefore only one of them would be produced. Constraint streams respect the order of entities given to it in the planning solution. Which means your ordering issue likely comes from there - are you sure you're not using |
Beta Was this translation helpful? Give feedback.
-
|
Not where it matters, no. So it means that with But in my unit test, it looks like this : @ParameterizedTest
@MethodSource("groupsNotSaturdaySundayShifts")
public void ShiftsTooClose_TwoShiftsWithin3daysForANotEmployeeInSaturdaySundayGroupsShouldPenalize(Group group) {
Employee Employee = EmployeeRosterObjectsGeneratorForTests.getEmployee(10, 3, group, anchorWeComplet);
var shiftDate = EmployeeRosterObjectsGeneratorForTests.getshiftDate(LocalDate.of(2025, 10, 4));
Shift da = EmployeeRosterObjectsGeneratorForTests.getShift(true, true, Employee, shiftDate);
var shiftDate2 = EmployeeRosterObjectsGeneratorForTests.getshiftDate(LocalDate.of(2025, 10, 5));
Shift da2 = EmployeeRosterObjectsGeneratorForTests.getShift(true, true, Employee, shiftDate2);
var EmployeeRoster = new EmployeeRosterBuilderForTestCases()
.withEmployee(Employee)
.withshiftDate(shiftDate)
.withShift(da)
.withshiftDate(shiftDate2)
.withShift(da2)
.getResult();
constraintVerifier.verifyThat(EmployeeRosteringConstraintProviderV2::ShiftsTooCloseV2)
.givenSolution(EmployeeRoster).penalizes();
}"with" methods in the builder just add the entity to a list. So the order is always the same. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I've been modifying some of our constraints trying to optimize the performances behind it.
Without going into too much details about the domain (I've changed it to match your doc as close as possible), I moved from a foreach + groupBy with filters and then sorting into a forEachUniquePair.
However, since I modified constraint stream logic, unit test on this constraint does not pass, more importantly, sometimes the test pass, sometimes not, I'm starting to believe it is due to a misunderstanding of how forEachUniquePair is constructed.
Let's say we have a Set of 4 shifts : (A, B, C, D). How is the the produced result of forEachUniquePair on Shifts will look like ?
Option 1 :
(A, B), (A, C), (A, D), (B, A), (B, C), (B, D), (C, A), ...
Or option 2 :
(A, B), (A, C), (A, D), (B, C), (B, D), (C, D)
Basically my question is, does the combination takes into account the "order" or not ? Because I understood it as option 1, but maybe I'm wrong...
I've used it like that :
.forEachUniquePair(
Shift.class,
equal(Shift::getEmployee),
lessThan(Shift::getShiftDateDayIndex)
)
So, in my mind, let's say all shifts are assigned to the same employee on different dates : the pair A, B that might not pass the condition lessThan(Shift::getShiftDateDayIndex) is different from pair B, A that pass the condition and can be further evaluated.
Before the constraint was written like that :
New way :
I think my question boils down to : Are the pair produced ordered or not ?
Beta Was this translation helpful? Give feedback.
All reactions