Search code examples
optaplanner

OptaPlanner, force PlanningVariable to be filled in sequence of a range


Want to sequence a set of tasks with some rules. Each task has an index (the PlanningVariable) to indicate its sequence, its range is from 1 to n. One rule needs to minimize the sum of a shadow variable of all tasks, and it's meaningful only when the sum is calculated from index 1 consecutively to a larger number, say from 1 to 5. It's useless to sum say 2,4,8. Question: Is there any way to force optaplanner to assign 1 to a task and then 2 and then 3 ... to get potential solutions? no waste in this way.


Solution

  • Take a look at the task assigning example in optaplanner-examples (sources are in zip download in green button on optaplanner.org). See this video.

    It uses a CHAINED variable to assign tasks to a (linked) list basically. Then use a @CustomShadowVariable to calculate the index in that chain for each Task.

    In a future version of optaplanner, we'll support an Employee having a List<Task> and a Task having a @IndexShadowVariable, which will be a much simplier model. But meanwhile you'll have to work with the chained var approach.