Optaplanner should consider only positive scores

200 views Asked by At

I have a use case where I want to assign a salesperson to a list of appointments. Now, these salespeople have to travel from one point to another to reach the appointment location. I am using Optaplanner to schedule a list of salespersons for a bunch of appointments.

I have multiple constraints. Currently, I have implemented two constraints.
1. Sales rep work timing constraint.
2. A sales rep can accommodate at most one appointment at the same time.

Currently, when I ran these constraints individually it works fine and assigns correctly. However, when I add all the constraints and run the program, it is assigning some sales rep incorrectly. Even though some constraints are penalized but still OP selects the incorrect ones maybe it finds the best score among the penalized values.

Is there a way to completely reject a solution if we find any constraint(among multiples) violated?

For example, always choose a positive score and reject if a score has at least one negative value.
(5hard/3medium/2soft) > (1hard/0medium/0soft) but reject (6hard/-1medium/0soft)

Reject: 100hard/0medium/-1soft
Even if anyone score is negative then reject and only accept a score if it has all positive values. 

2

There are 2 answers

10
Geoffrey De Smet On

Reject: 100hard/0medium/-1soft

That 100hard should probably be -100hard.

OptaPlanner will always output the solution with the highest score - that is the best solution. So solution A with score 100hard is better than solution B with score 0hard, which is better than solution C with score -100hard.

I suspect you might have implemented a hard constraint that rewards instead of penalizes, unintentionally.

can I reject a solution if a negative score is encountered?

Practically yes (even if the answer is no internally). By default, OptaPlanner will assign all variables and output the best solution (= solution with the highest score) encountered during it's run. If it has encountered any solution with no hard constraints broken, the best solution it outputs will have no hard constraints broken.

But what if it didn't encounter any solution with no hard constriants broken?

  • Either keep searching: Use a feasibleScore termination. See docs. This might run forever if a dataset is overconstrained.
  • Or don't assign all variables. This is called overconstrained planning. See docs.
6
Lukáš Petrovický On

Scores in OptaPlanner are compared based on their level, from left to right, hardest to softest. If a score level has a higher numeric value, it is considered better. If the numeric values for that level are equal, a softer level is considered.

In your example, the following is true:

(6hard/-1medium/0soft) > (5hard/3medium/2soft) > (1hard/0medium/0soft)

Hard score 6 is better than hard score 5, and hard score 5 is better than hard score 1. If the solution represented by the first score is not in fact better than the second score, then you need to redefine your constraint weights to tell OptaPlanner that.

The score is a measure of solution quality. Better score means better solution. There is no working around that fact, it is something you have to accept.