Skip to content

Commit a649452

Browse files
authored
chore: improve score corruption error message (#822)
1 parent 21043f8 commit a649452

File tree

38 files changed

+429
-385
lines changed

38 files changed

+429
-385
lines changed

core/src/main/java/ai/timefold/solver/core/impl/constructionheuristic/DefaultConstructionHeuristicPhase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public String getPhaseTypeString() {
4141

4242
@Override
4343
public void solve(SolverScope<Solution_> solverScope) {
44-
var phaseScope = new ConstructionHeuristicPhaseScope<>(solverScope);
44+
var phaseScope = new ConstructionHeuristicPhaseScope<>(solverScope, phaseIndex);
4545
phaseStarted(phaseScope);
4646

4747
var solutionDescriptor = solverScope.getSolutionDescriptor();

core/src/main/java/ai/timefold/solver/core/impl/constructionheuristic/decider/ConstructionHeuristicDecider.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import ai.timefold.solver.core.impl.heuristic.move.Move;
1111
import ai.timefold.solver.core.impl.heuristic.move.NoChangeMove;
1212
import ai.timefold.solver.core.impl.heuristic.selector.move.generic.ChangeMove;
13+
import ai.timefold.solver.core.impl.phase.scope.SolverLifecyclePoint;
1314
import ai.timefold.solver.core.impl.score.director.InnerScoreDirector;
1415
import ai.timefold.solver.core.impl.solver.scope.SolverScope;
1516
import ai.timefold.solver.core.impl.solver.termination.Termination;
@@ -135,11 +136,11 @@ protected <Score_ extends Score<Score_>> void doMove(ConstructionHeuristicMoveSc
135136
});
136137
if (assertExpectedUndoMoveScore) {
137138
scoreDirector.assertExpectedUndoMoveScore(moveScope.getMove(),
138-
(Score_) moveScope.getStepScope().getPhaseScope().getLastCompletedStepScope().getScore());
139+
(Score_) moveScope.getStepScope().getPhaseScope().getLastCompletedStepScope().getScore(),
140+
SolverLifecyclePoint.of(moveScope));
139141
}
140142
logger.trace("{} Move index ({}), score ({}), move ({}).",
141-
logIndentation,
142-
moveScope.getMoveIndex(), moveScope.getScore(), moveScope.getMove());
143+
logIndentation, moveScope.getMoveIndex(), moveScope.getScore(), moveScope.getMove());
143144
}
144145

145146
}

core/src/main/java/ai/timefold/solver/core/impl/constructionheuristic/scope/ConstructionHeuristicMoveScope.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,16 @@
77
/**
88
* @param <Solution_> the solution type, the class with the {@link PlanningSolution} annotation
99
*/
10-
public class ConstructionHeuristicMoveScope<Solution_> extends AbstractMoveScope<Solution_> {
11-
12-
private final ConstructionHeuristicStepScope<Solution_> stepScope;
10+
public final class ConstructionHeuristicMoveScope<Solution_> extends AbstractMoveScope<Solution_> {
1311

1412
public ConstructionHeuristicMoveScope(ConstructionHeuristicStepScope<Solution_> stepScope,
1513
int moveIndex, Move<Solution_> move) {
16-
super(moveIndex, move);
17-
this.stepScope = stepScope;
14+
super(stepScope, moveIndex, move);
1815
}
1916

2017
@Override
2118
public ConstructionHeuristicStepScope<Solution_> getStepScope() {
22-
return stepScope;
19+
return (ConstructionHeuristicStepScope<Solution_>) super.getStepScope();
2320
}
2421

25-
// ************************************************************************
26-
// Calculated methods
27-
// ************************************************************************
28-
2922
}

core/src/main/java/ai/timefold/solver/core/impl/constructionheuristic/scope/ConstructionHeuristicPhaseScope.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
/**
88
* @param <Solution_> the solution type, the class with the {@link PlanningSolution} annotation
99
*/
10-
public class ConstructionHeuristicPhaseScope<Solution_> extends AbstractPhaseScope<Solution_> {
10+
public final class ConstructionHeuristicPhaseScope<Solution_> extends AbstractPhaseScope<Solution_> {
1111

1212
private ConstructionHeuristicStepScope<Solution_> lastCompletedStepScope;
1313

14-
public ConstructionHeuristicPhaseScope(SolverScope<Solution_> solverScope) {
15-
super(solverScope, false);
14+
public ConstructionHeuristicPhaseScope(SolverScope<Solution_> solverScope, int phaseIndex) {
15+
super(solverScope, phaseIndex, false);
1616
lastCompletedStepScope = new ConstructionHeuristicStepScope<>(this, -1);
1717
}
1818

core/src/main/java/ai/timefold/solver/core/impl/constructionheuristic/scope/ConstructionHeuristicStepScope.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* @param <Solution_> the solution type, the class with the {@link PlanningSolution} annotation
99
*/
10-
public class ConstructionHeuristicStepScope<Solution_> extends AbstractStepScope<Solution_> {
10+
public final class ConstructionHeuristicStepScope<Solution_> extends AbstractStepScope<Solution_> {
1111

1212
private final ConstructionHeuristicPhaseScope<Solution_> phaseScope;
1313

core/src/main/java/ai/timefold/solver/core/impl/exhaustivesearch/DefaultExhaustiveSearchPhase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public String getPhaseTypeString() {
5959
@Override
6060
public void solve(SolverScope<Solution_> solverScope) {
6161
SortedSet<ExhaustiveSearchNode> expandableNodeQueue = new TreeSet<>(nodeComparator);
62-
ExhaustiveSearchPhaseScope<Solution_> phaseScope = new ExhaustiveSearchPhaseScope<>(solverScope);
62+
ExhaustiveSearchPhaseScope<Solution_> phaseScope = new ExhaustiveSearchPhaseScope<>(solverScope, phaseIndex);
6363
phaseScope.setExpandableNodeQueue(expandableNodeQueue);
6464
phaseStarted(phaseScope);
6565

core/src/main/java/ai/timefold/solver/core/impl/exhaustivesearch/decider/ExhaustiveSearchDecider.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import ai.timefold.solver.core.impl.heuristic.move.Move;
1111
import ai.timefold.solver.core.impl.heuristic.selector.entity.mimic.ManualEntityMimicRecorder;
1212
import ai.timefold.solver.core.impl.heuristic.selector.move.MoveSelector;
13+
import ai.timefold.solver.core.impl.phase.scope.SolverLifecyclePoint;
1314
import ai.timefold.solver.core.impl.score.director.InnerScoreDirector;
1415
import ai.timefold.solver.core.impl.solver.recaller.BestSolutionRecaller;
1516
import ai.timefold.solver.core.impl.solver.scope.SolverScope;
@@ -132,15 +133,15 @@ private <Score_ extends Score<Score_>> void doMove(ExhaustiveSearchStepScope<Sol
132133
moveNode.setUndoMove(undoMove);
133134
processMove(stepScope, moveNode);
134135
undoMove.doMoveOnly(scoreDirector);
136+
var executionPoint = SolverLifecyclePoint.of(stepScope, moveNode.getTreeId());
135137
if (assertExpectedUndoMoveScore) {
136138
// In BRUTE_FORCE a stepScore can be null because it was not calculated
137139
if (stepScope.getStartingStepScore() != null) {
138-
scoreDirector.assertExpectedUndoMoveScore(move, (Score_) stepScope.getStartingStepScore());
140+
scoreDirector.assertExpectedUndoMoveScore(move, (Score_) stepScope.getStartingStepScore(), executionPoint);
139141
}
140142
}
141143
LOGGER.trace("{} Move treeId ({}), score ({}), expandable ({}), move ({}).",
142-
logIndentation,
143-
moveNode.getTreeId(), moveNode.getScore(), moveNode.isExpandable(), moveNode.getMove());
144+
logIndentation, executionPoint.treeId(), moveNode.getScore(), moveNode.isExpandable(), moveNode.getMove());
144145
}
145146

146147
private <Score_ extends Score<Score_>> void processMove(ExhaustiveSearchStepScope<Solution_> stepScope,

core/src/main/java/ai/timefold/solver/core/impl/exhaustivesearch/scope/ExhaustiveSearchPhaseScope.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
/**
1414
* @param <Solution_> the solution type, the class with the {@link PlanningSolution} annotation
1515
*/
16-
public class ExhaustiveSearchPhaseScope<Solution_> extends AbstractPhaseScope<Solution_> {
16+
public final class ExhaustiveSearchPhaseScope<Solution_> extends AbstractPhaseScope<Solution_> {
1717

1818
private List<ExhaustiveSearchLayer> layerList;
1919
private SortedSet<ExhaustiveSearchNode> expandableNodeQueue;
2020
private Score bestPessimisticBound;
2121

2222
private ExhaustiveSearchStepScope<Solution_> lastCompletedStepScope;
2323

24-
public ExhaustiveSearchPhaseScope(SolverScope<Solution_> solverScope) {
25-
super(solverScope, false);
24+
public ExhaustiveSearchPhaseScope(SolverScope<Solution_> solverScope, int phaseIndex) {
25+
super(solverScope, phaseIndex, false);
2626
lastCompletedStepScope = new ExhaustiveSearchStepScope<>(this, -1);
2727
}
2828

core/src/main/java/ai/timefold/solver/core/impl/exhaustivesearch/scope/ExhaustiveSearchStepScope.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* @param <Solution_> the solution type, the class with the {@link PlanningSolution} annotation
1010
*/
11-
public class ExhaustiveSearchStepScope<Solution_> extends AbstractStepScope<Solution_> {
11+
public final class ExhaustiveSearchStepScope<Solution_> extends AbstractStepScope<Solution_> {
1212

1313
private final ExhaustiveSearchPhaseScope<Solution_> phaseScope;
1414

core/src/main/java/ai/timefold/solver/core/impl/localsearch/DefaultLocalSearchPhase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public String getPhaseTypeString() {
5555

5656
@Override
5757
public void solve(SolverScope<Solution_> solverScope) {
58-
LocalSearchPhaseScope<Solution_> phaseScope = new LocalSearchPhaseScope<>(solverScope);
58+
LocalSearchPhaseScope<Solution_> phaseScope = new LocalSearchPhaseScope<>(solverScope, phaseIndex);
5959
phaseStarted(phaseScope);
6060

6161
if (solverScope.isMetricEnabled(SolverMetric.MOVE_COUNT_PER_STEP)) {

0 commit comments

Comments
 (0)