5

Can we add/remove constraints in optaplanner dynamically using Java ? Is there any example. I want the user to be allowed to add or remove this constraints at runtime using some UI.

Below link says something about it using drools. can it be done using java ? how to dynamically add / remove constraints in optaplanner

Prashant
  • 51
  • 1
  • The answer there is quite clear I believe. You can use either Drools or the Java Score calculators but will have to create a solver configuration based on the constraints selected in the UI and then use that configuration to run the solver. Alternatively, you can enable or disable rules based on the constraints selected in the UI using some boolean flag. – k88 Jan 16 '20 at 03:18

1 Answers1

4

1) Usually non hard coded constraint suffice. For example, instead of having a constraint that says:

  • "when Ann is working on Friday, penalize"

there is:

  • A DayOfWeekDislike data class with an Employee and a DayOfWeek. ** The input data has an instance that with employee Ann and dayOfWeek Friday.
  • There is a constraints that says: "when there is a DayOfWeekDislike and there is a ShiftAssignment of that employee to such a DayOfWeek, penalize"

This approach suffices in most of the use cases to avoid dynamic constraints. Notice that you can add/remove DayOfWeekDislike data in real-time with Solver.addProblemFactChange().

2) Next, @ConstraintConfiguration can use @ConstraintWeight to disable/enable constraints in real time with Solver.addProblemFactChange(). Set a constraint weight to a zero score to disable it.

3) If both 1) and 2) fail, you really need dynamic constraints. I haven't seen any use case that needs to go this far yet, but we could support it. If you do need this approach, what's your use case?

Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
  • I have a java class which implements ConstraintProvider. We have implemented defineConstrqaints method. Suppose the backend code has defined 5 constraints (2 hard and 3 soft). We wanted to show a UI where we will specify constraint name to disable through UI. When the user clicks submit, the optaplanner engine executes but it must not execute the constraint entered by user.The business user has asked us to have such control to define constraints at runtime. – Prashant Jan 23 '20 at 11:15
  • 1
    Then 2) `@ConstaintWeight` is exactly what you need. See docs and some of the examples that use that (Conference scheduling for one). Besides enabling/disabling the constraint (by setting a `HardSoftScore.ZERO`), you can even let the business users weigh them in the UI. – Geoffrey De Smet Jan 23 '20 at 11:32
  • @GeoffreyDeSmet if you set the score to zero, does OP know not to bother executing the constraint? What is mean is, I wouldn't want an expensive join to be carried out to only set a score of zero. – Justin Phillips Sep 29 '21 at 14:11
  • 2
    In ConstraintStreams, OptaPlanner won't waste the time doing joins for constraints with a weight of zero. For scoreDRL (older), it's an open issue not that waste that time then too. – Geoffrey De Smet Sep 29 '21 at 14:26
  • 1
    Geoffrey, your final comment is worth explicitly mentioning in the documentation around @ConstraintWeights, it's just stopped me descending down a rabbit hole of dynamically building the Constraint[] returned by defineConstraints! – Alastair Jan 14 '22 at 09:14
  • We will :) https://issues.redhat.com/browse/PLANNER-2639 Thanks for the feedback! – Geoffrey De Smet Jan 14 '22 at 10:18