Optaplanner Modelling: Change size of a @PlanningEntityCollectionProperty Collection

Viewed 50

I currently have a PlanningSolution for scheduling multiple projects across several timeslots which is very similar to this example:

@PlanningSolution
class Schedule {
    @ProblemFactCollectionProperty
    lateinit var projects: List<Project>

    @ProblemFactCollectionProperty
    lateinit var timeslots: List<Timeslot>

    @ValueRangeProvider(id = "capacityRange")
    @ProblemFactCollectionProperty
    lateinit var timeslotBudgets: List<TimeslotBudget>

    @PlanningEntityCollectionProperty
    lateinit var budgetByProjectAndTimeslots: List<BudgetByProjectAndTimeslot>

    @PlanningScore
    var score: HardSoftScore? = null
}

With a planning entity

@PlanningEntity
class BudgetByProjectAndTimeslot {
    val timeslotBudgetAmount: Int get() = timeslotBudget?.amount ?: 0

    lateinit var project: Project
    lateinit var imeslot: Timeslot

    @PlanningVariable(valueRangeProviderRefs = ["capacityRange"])
    var timeslotBudget: TimeslotBudget? = null
}

An example solution, with each planned budget per project and timeslot as value in the table would look like:

Timeslot # |  1  |  2  |  3  |  4  |  5  |
Project 1  | 10  | 50  |  20 |  0  |  0  |
Project 2  | 40  |  0  |   0 |  0  |  0  |
Project 3  |  0  |  0  |  30 | 50  | 40  |

For a fixed number of Timeslots, this model does meet its intended purpose. But in the future, I would like to be able to extend the number of timeslots so every project can be fully scheduled. E.g. if Project 3 in the table above had an overall budget to be scheduled of 200, an amount of 80 couldn't be planned due to timeslot capacity restrictions.

Other relevant context to this question is that I currently try to minimize the deviation from 100% utilization of a timeslot's capacity. So in the example above, the solution is optimal for timeslot capacities of 50 from slots 1-4 and 40 for slot 5.

So my question is: How can I tell optaplanner to change the number of timeslots dynamically during planning, so all projects may be fully scheduled and I don't have more timeslots than the problem would require? Would I need to provide a range of timeslots and add a constraint to minimize the number of timeslots needed for fully scheduling all projects?

1 Answers

OptaPlanner allows you to modify your problem at runtime. See problem fact changes.

Note: at present (OptaPlanner 8.11), this is just a syntactic sugar for stopping the solver and loading a new solution with the new facts and entities.

Related