cityAssignment.setNextCityAssignment(cityAssignment);
cityAssignment.setPreviousCityAssignment(cityAssignment);
cityAssignmentHandle = workingMemory.insert(cityAssignment);
} else {
Score bestScore = DefaultSimpleDoubleScore.valueOf(-Double.MAX_VALUE);
CityAssignment bestAfterCityAssignment = null;
FactHandle bestAfterCityAssignmentFactHandle = null;
CityAssignment bestBeforeCityAssignment = null;
FactHandle bestBeforeCityAssignmentFactHandle = null;
for (CityAssignment afterCityAssignment : assignedCityAssignmentList) {
CityAssignment beforeCityAssignment = afterCityAssignment.getNextCityAssignment();
FactHandle afterCityAssignmentFactHandle = workingMemory.getFactHandle(afterCityAssignment);
FactHandle beforeCityAssignmentFactHandle = workingMemory.getFactHandle(beforeCityAssignment);
// Do changes
afterCityAssignment.setNextCityAssignment(cityAssignment);
cityAssignment.setPreviousCityAssignment(afterCityAssignment);
cityAssignment.setNextCityAssignment(beforeCityAssignment);
beforeCityAssignment.setPreviousCityAssignment(cityAssignment);
if (cityAssignmentHandle == null) {
cityAssignmentHandle = workingMemory.insert(cityAssignment);
} else {
workingMemory.update(cityAssignmentHandle, cityAssignment);
}
workingMemory.update(afterCityAssignmentFactHandle, afterCityAssignment);
workingMemory.update(beforeCityAssignmentFactHandle, beforeCityAssignment);
// Calculate score
Score score = abstractSolverScope.calculateScoreFromWorkingMemory();
if (score.compareTo(bestScore) > 0) {
bestScore = score;
bestAfterCityAssignment = afterCityAssignment;
bestAfterCityAssignmentFactHandle = afterCityAssignmentFactHandle;
bestBeforeCityAssignment = beforeCityAssignment;
bestBeforeCityAssignmentFactHandle = beforeCityAssignmentFactHandle;
}
// Undo changes
afterCityAssignment.setNextCityAssignment(beforeCityAssignment);
beforeCityAssignment.setPreviousCityAssignment(afterCityAssignment);
workingMemory.update(afterCityAssignmentFactHandle, afterCityAssignment);
workingMemory.update(beforeCityAssignmentFactHandle, beforeCityAssignment);
}
if (bestAfterCityAssignment == null) {
throw new IllegalStateException("The bestAfterCityAssignment (" + bestAfterCityAssignment