bits1983
bits1983

Reputation: 5

Top N solution for a planning entity

I have a simple field mapping use case where I need to intelligently find the target field for an input source field based on multiple constraints. To elaborate more, there is only one source Field and there are say 100 target field.

@PlanningEntity
public class FieldMapping {

    @PlanningId
    private Long id;

    public FieldMapping()
    {

    }


    protected Field inputField;

    @PlanningVariable(valueRangeProviderRefs = {
            "targetFieldRange" })
    protected Field targetField; }  

@PlanningSolution
public class FieldMappingSolution {

    @ValueRangeProvider(id = "targetFieldRange")
    @ProblemFactCollectionProperty
    private List<PlanningRecommField> targetFields;

   @PlanningScore
    private HardSoftScore score;

    private SolverStatus solverStatus;

Currently Optaplanner is giving the best solution mapping. Can I have some API where I get the top N solutions.

Upvotes: 0

Views: 70

Answers (2)

Geoffrey De Smet
Geoffrey De Smet

Reputation: 27312

Caveat on Lukas's excellent answers: Listening to best solution events will not deliver a solution if it has the same score as the latest best solution at that time.

Maybe OptaPlanner should support a simple boolean in SolverConfig to have best solution events for solutions on new steps with a score the same as the current best solution. This may yield duplicate solutions, but the API would specify that clearly so the user can deal with it.


That being said, in most use cases, listening to new solutions with a score equal to the current best solution will be drinking from the firehose. Especially when LA/SA runs a while, you will likely get billions of solutions with the same score and trivial differences between them (for example: swap 2 math lessons of the same teacher and studentGroup).


Also, OptaPlanner doesn't support Pareto optimization out of the box, which is a more advanced form of what you're asking (with a lot more complexity - I've rarely seen it useful in practice). Some have hacked BestSolutionRecaller to enable it. Don't do that.

Upvotes: 2

No. But you can listen to best solutions and build up whatever statistics you like. That said, if two best solutions have the same score, the event will only be fired for the first one.

Upvotes: 2

Related Questions