4

strong textHello Optaplanner experts,

I am pretty new to OptaPlanner, so please pardon any naive or basic questions. I am using it to schedule a:

set of jobs, A, B and C, which can be completed by 5 resources, say Will, Jane, Roy, Tom, Jeff. 

Each of these jobs can be done by any of the 5 resources. There will be some algorithm to calculate who is the best fit based on soft constraints, but I am not there yet. For now, I just need to come up with multiple solutions, say

1. Will - A, Roy - B and Jeff - C
2. Roy - B, Tom - B and Jane - C
... and so on

Is there a way to do this in OptaPlanner 6.2.0 Final version? I only see a method for getting the best solution. I am sure I am missing something, just not sure what. Any pointers will be greatly appreciated.

Thank you for your time,

Alice

Alice
  • 390
  • 2
  • 5
  • 19

1 Answers1

3

There may be a better solution but I recommend adding a SolverEventListener to your solver via:

solver.addEventListener(new SolverEventListener<Solution>() {
    @Override
    public void bestSolutionChanged(BestSolutionChangedEvent<Solution> event) {
        // TODO Auto-generated method stub  
    }
});

The bestSolutionChanged method will be called every time a better solution is found. From here you will need to clone(I think, Geoffrey would know better) the solution and save it to a list(maybe keep the last 5 best solutions or something? I would check how long the cloning process takes since the method should return rather quickly.

  • 2
    That's indeed the recommended way :) In the past, people have also hacked `BestSolutionRecaller` to deliver this behaviour, but don't do that. In the future, I want to also fully support Pareto optimization out of the box, which will give you a list of *interesting* (as in sufficiently diverse) best solutions (see "pareto" in the docs). – Geoffrey De Smet Jun 22 '15 at 08:57
  • Thank you code4dc and @Geoffrey De Smet. I tried it out, but I am not getting more than 1 hit in the bestSolutionChanged method. I didnt get to spend much time on it today, but I will do more debugging and see what is the issue exactly. – Alice Jun 23 '15 at 00:50
  • Ah yes... now I remember why they hacked `BestSolutionRecaller`: because in the listener approach, `bestSolutionChanged()` is only called when the solution improves (so a strictly higher score), not when the same score is found... – Geoffrey De Smet Jun 23 '15 at 07:08
  • Thank you @Geoffrey De Smet.Yes, the score is same in both cases. So looks like my best solution is a hack. I looked in the optaplanner samples and found that BestSolutionRecaller is a optaplanner class called internally by the DefaultSolver. I couldn't find any samples of how to achieve this behavior. I know this is not recommended, but I was thinking of extending BestSolutionRecaller and overriding solvingStarted() and change something in the solverScope. Is that what you suggest? Sorry if this is a naive question, but want to be sure I am at least not messing too much with this approach. – Alice Jun 23 '15 at 16:57
  • the BestSolutionRecaller will be created in a Config class, probably SolverConfig IIRC, you probably want to overwrite its build method there, see the last chapter in the docs (the image) to understand that the Config classes vs the runtime classes. – Geoffrey De Smet Jun 24 '15 at 07:25