Skip to content

Commit 129c0c8

Browse files
committed
chore: add CH tests
1 parent 2a92ca1 commit 129c0c8

File tree

30 files changed

+1061
-36
lines changed

30 files changed

+1061
-36
lines changed

core/src/main/java/ai/timefold/solver/core/impl/heuristic/selector/common/decorator/FactorySelectionSorter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@
2424
public final class FactorySelectionSorter<Solution_, T> implements SelectionSorter<Solution_, T> {
2525

2626
private final SorterFactory<Solution_, T> selectionSorterFactory;
27-
private final Comparator<Comparable> comparator;
27+
private final Comparator<Comparable> appliedComparator;
2828

2929
public FactorySelectionSorter(SorterFactory<Solution_, T> selectionSorterFactory,
3030
SelectionSorterOrder selectionSorterOrder) {
3131
this.selectionSorterFactory = selectionSorterFactory;
3232
switch (selectionSorterOrder) {
3333
case ASCENDING:
34-
this.comparator = Comparator.naturalOrder();
34+
this.appliedComparator = Comparator.naturalOrder();
3535
break;
3636
case DESCENDING:
37-
this.comparator = Collections.reverseOrder();
37+
this.appliedComparator = Collections.reverseOrder();
3838
break;
3939
default:
4040
throw new IllegalStateException("The selectionSorterOrder (" + selectionSorterOrder
@@ -53,7 +53,7 @@ public void sort(ScoreDirector<Solution_> scoreDirector, List<T> selectionList)
5353
* of {@link PlanningEntity}, planningValue, {@link Move} or {@link Selector}
5454
*/
5555
public void sort(Solution_ solution, List<T> selectionList) {
56-
SortedMap<Comparable, T> selectionMap = new TreeMap<>(comparator);
56+
SortedMap<Comparable, T> selectionMap = new TreeMap<>(appliedComparator);
5757
for (T selection : selectionList) {
5858
Comparable difficultyWeight = selectionSorterFactory.createSorter(solution, selection);
5959
T previous = selectionMap.put(difficultyWeight, selection);
@@ -74,11 +74,11 @@ public boolean equals(Object other) {
7474
return false;
7575
FactorySelectionSorter<?, ?> that = (FactorySelectionSorter<?, ?>) other;
7676
return Objects.equals(selectionSorterFactory, that.selectionSorterFactory)
77-
&& Objects.equals(comparator, that.comparator);
77+
&& Objects.equals(appliedComparator, that.appliedComparator);
7878
}
7979

8080
@Override
8181
public int hashCode() {
82-
return Objects.hash(selectionSorterFactory, comparator);
82+
return Objects.hash(selectionSorterFactory, appliedComparator);
8383
}
8484
}

core/src/test/java/ai/timefold/solver/core/impl/constructionheuristic/DefaultConstructionHeuristicPhaseTest.java

Lines changed: 218 additions & 18 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
import org.jspecify.annotations.NonNull;
77

8-
public class OneValuePerEntityEasyScoreCalculator
8+
public class ListOneValuePerEntityEasyScoreCalculator
99
implements EasyScoreCalculator<TestdataListSortableSolution, HardSoftScore> {
1010

1111
@Override
12-
public @NonNull HardSoftScore calculateScore(@NonNull TestdataListSortableSolution testdataListSortableSolution) {
12+
public @NonNull HardSoftScore calculateScore(@NonNull TestdataListSortableSolution solution) {
1313
var softScore = 0;
1414
var hardScore = 0;
15-
for (var entity : testdataListSortableSolution.getEntityList()) {
15+
for (var entity : solution.getEntityList()) {
1616
if (entity.getValueList().size() == 1) {
1717
softScore -= 10;
1818
} else {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
import org.jspecify.annotations.NonNull;
77

8-
public class OneValuePerEntityFactoryEasyScoreCalculator
8+
public class ListOneValuePerEntityFactoryEasyScoreCalculator
99
implements EasyScoreCalculator<TestdataListFactorySortableSolution, HardSoftScore> {
1010

1111
@Override
1212
public @NonNull HardSoftScore
13-
calculateScore(@NonNull TestdataListFactorySortableSolution testdataListFactorySortableSolution) {
13+
calculateScore(@NonNull TestdataListFactorySortableSolution solution) {
1414
var softScore = 0;
1515
var hardScore = 0;
16-
for (var entity : testdataListFactorySortableSolution.getEntityList()) {
16+
for (var entity : solution.getEntityList()) {
1717
if (entity.getValueList().size() == 1) {
1818
softScore -= 10;
1919
} else {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
import org.jspecify.annotations.NonNull;
77

8-
public class OneValuePerEntityRangeEasyScoreCalculator
8+
public class ListOneValuePerEntityRangeEasyScoreCalculator
99
implements EasyScoreCalculator<TestdataListSortableEntityProvidingSolution, HardSoftScore> {
1010

1111
@Override
1212
public @NonNull HardSoftScore
13-
calculateScore(@NonNull TestdataListSortableEntityProvidingSolution testdataListSortableEntityProvidingSolution) {
13+
calculateScore(@NonNull TestdataListSortableEntityProvidingSolution solution) {
1414
var softScore = 0;
1515
var hardScore = 0;
16-
for (var entity : testdataListSortableEntityProvidingSolution.getEntityList()) {
16+
for (var entity : solution.getEntityList()) {
1717
if (entity.getValueList().size() == 1) {
1818
softScore -= 10;
1919
} else {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
import org.jspecify.annotations.NonNull;
77

8-
public class OneValuePerEntityRangeFactoryEasyScoreCalculator
8+
public class ListOneValuePerEntityRangeFactoryEasyScoreCalculator
99
implements EasyScoreCalculator<TestdataListFactorySortableEntityProvidingSolution, HardSoftScore> {
1010

1111
@Override
1212
public @NonNull HardSoftScore
1313
calculateScore(
14-
@NonNull TestdataListFactorySortableEntityProvidingSolution testdataListFactorySortableEntityProvidingSolution) {
14+
@NonNull TestdataListFactorySortableEntityProvidingSolution solution) {
1515
var softScore = 0;
1616
var hardScore = 0;
17-
for (var entity : testdataListFactorySortableEntityProvidingSolution.getEntityList()) {
17+
for (var entity : solution.getEntityList()) {
1818
if (entity.getValueList().size() == 1) {
1919
softScore -= 10;
2020
} else {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ai.timefold.solver.core.testdomain.sort.comparator;
2+
3+
import java.util.Objects;
4+
5+
import ai.timefold.solver.core.api.score.buildin.hardsoft.HardSoftScore;
6+
import ai.timefold.solver.core.api.score.calculator.EasyScoreCalculator;
7+
8+
import org.jspecify.annotations.NonNull;
9+
10+
public class OneValuePerEntityEasyScoreCalculator
11+
implements EasyScoreCalculator<TestdataSortableSolution, HardSoftScore> {
12+
13+
@Override
14+
public @NonNull HardSoftScore calculateScore(@NonNull TestdataSortableSolution solution) {
15+
var distinct = (int) solution.getEntityList().stream()
16+
.map(TestdataSortableEntity::getValue)
17+
.filter(Objects::nonNull)
18+
.distinct()
19+
.count();
20+
var assigned = solution.getEntityList().stream()
21+
.map(TestdataSortableEntity::getValue)
22+
.filter(Objects::nonNull)
23+
.count();
24+
var repeated = (int) (assigned - distinct);
25+
return HardSoftScore.of(-repeated, -distinct);
26+
}
27+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ai.timefold.solver.core.testdomain.sort.comparator;
2+
3+
import java.util.Comparator;
4+
5+
public class SortableEntityComparator implements Comparator<TestdataSortableEntity> {
6+
7+
@Override
8+
public int compare(TestdataSortableEntity e1, TestdataSortableEntity e2) {
9+
return e1.getDifficulty() - e2.getDifficulty();
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ai.timefold.solver.core.testdomain.sort.comparator;
2+
3+
import java.util.Comparator;
4+
5+
public class SortableValueComparator implements Comparator<TestdataSortableValue> {
6+
7+
@Override
8+
public int compare(TestdataSortableValue v1, TestdataSortableValue v2) {
9+
return v1.getStrength() - v2.getStrength();
10+
}
11+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ai.timefold.solver.core.testdomain.sort.comparator;
2+
3+
import ai.timefold.solver.core.api.domain.entity.PlanningEntity;
4+
import ai.timefold.solver.core.api.domain.variable.PlanningVariable;
5+
import ai.timefold.solver.core.testdomain.TestdataObject;
6+
7+
@PlanningEntity(difficultyComparatorClass = SortableEntityComparator.class)
8+
public class TestdataSortableEntity extends TestdataObject {
9+
10+
@PlanningVariable(valueRangeProviderRefs = "valueRange", strengthComparatorClass = SortableValueComparator.class)
11+
private TestdataSortableValue value;
12+
private int difficulty;
13+
14+
public TestdataSortableEntity() {
15+
}
16+
17+
public TestdataSortableEntity(String code, int difficulty) {
18+
super(code);
19+
this.difficulty = difficulty;
20+
}
21+
22+
public TestdataSortableValue getValue() {
23+
return value;
24+
}
25+
26+
public void setValue(TestdataSortableValue value) {
27+
this.value = value;
28+
}
29+
30+
public int getDifficulty() {
31+
return difficulty;
32+
}
33+
34+
public void setDifficulty(int difficulty) {
35+
this.difficulty = difficulty;
36+
}
37+
}

0 commit comments

Comments
 (0)