Search code examples
optaplanner

OptaPlanner return the best score but not its associated solution


I'm working on a problem like CloudBalancing with OptaPlanner 7.33.0.

The returned PlanningSolution object contains the best calculated score but the solution does not match. I debugged all solutions with a very small dataset and the returned solution seems to be the latest that it doesn't have the returned score.

Example :

  • 1: The solution with the best score has a score of 0hard/-1197medium/4soft
  • 2: The returned solution has a score of 0hard/-1198medium/2soft
  • The solver return a PlanningSolution object with the score of the solution 1 but the content of solution 2

This is my planning solution class

@PlanningSolution
public class GroupSolution
{
    @PlanningEntityCollectionProperty
    private List<Request> request;

    @ValueRangeProvider(id = "groupRange")
    @ProblemFactCollectionProperty
    private List<Group> proposedGroups;

    @PlanningScore
    private HardMediumSoftScore score;

    // Others planning fact that I use in my EasyScoreCalculator
}

Request class

@PlanningEntity
public class Request
{
    @PlanningVariable(valueRangeProviderRefs = {
        "groupRange"
    }, nullable = false)
    private Group group;

    // Other properties
}

Configuration

<?xml version="1.0" encoding="UTF-8"?>
<solver>
  <scanAnnotatedClasses>
    <packageInclude>my.package.domain</packageInclude>
  </scanAnnotatedClasses>

  <!-- Score configuration -->
  <scoreDirectorFactory>
    <initializingScoreTrend>ONLY_DOWN</initializingScoreTrend>
    <easyScoreCalculatorClass>my.package.MyEasyScoreCalculator</easyScoreCalculatorClass>
  </scoreDirectorFactory>

  <constructionHeuristic />

  <termination>
    <minutesSpentLimit>5</minutesSpentLimit>
  </termination>
</solver>

I'm using an EasyScoreCalculator with 3 levels of scoring and I use them like this :

  • hard : I only set it to 0 or -1. if -1 I don't calc others scores as -1 means the solution is not applicable
  • medium : The price, example if the solution cost 100 eur, my medium score will be -100
  • soft : If 2 price are same, I use this score to prefer a solution with more empty group (biggest number of request per group). This score is never negative

I will continue to investigate on this but if you have any suggestion to get the correct result you can tell me.

But the main question is, do you know if it's a normal OptaPlanner behavior or not? If it's normal in which case he will return a non matching score and solution?


Solution

  • 1) Sounds like score corruption. Given that FAST_ASSERT "fixes it" (it doesn't), run it with <environmentMode>NON_INTRUSIVE_FULL_ASSERT</environmentMode>, if it throws an exception, it might point you to the real problem, because it detects the issue earlier.

    2) Could also be due to planning entity cloning corruption. That's harder to detect. Do you use a custom solution cloner? Are there any domain classes that have references to the planning entity class(es) or planning solution (so references Request or GroupSolution)? Any classes that have a Collection or Map with Request or GroupSolution instances?