Search code examples
javaoptaplanner

Can I use multiple @PlanningEntityCollectionProperty in OptaPlanner?


I would like to allocate an Employee to either a Reserve period or Shift period.

@PlanningSolution
public class RosterSolution implements Solution<HardSoftScore> {

  @PlanningEntityCollectionProperty 
  private List<Shift> shift = new ArrayList<Shift>();

  @PlanningEntityCollectionProperty 
  private List<Reserve> reserve = new ArrayList<Reserve>();
  ....



}

Each Shift and Reserve entity has a single @PlanningVariable of

@PlanningVariable(valueRangeProviderRefs={"employees"}, nullable= true)
    private Employee emp;

Is it possible to use multiple @PlanningEntityCollectionProperty annotations in a solution? I haven't seen any examples of its usage. The documentation under @PlanningSolution states 'Each planning solution must have at least 1 PlanningEntityCollectionProperty or PlanningEntityProperty property', but I'm not sure how to implement it.

I'm trying to avoid the single list of planning entities that use type coding, unless I can 'replace type code with state/strategy pattern' (if that is possible).


Solution

  • Yes, you can. The example CoachShuttleGatheringSolution and VehicleRoutingSolution do it.

    It's useful when you have multiple planning entity classes (some of which can be shadow entity classes). But don't go there unless you really need to.