0

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. 

user101
  • 139
  • 10

2 Answers2

1

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.

Lukáš Petrovický
  • 3,945
  • 1
  • 11
  • 20
  • Does that mean there is no way I can reject a solution if a negative score is encountered? – user101 Dec 10 '22 at 15:23
  • 2
    There is no way how you get to reject any kind of score, that's the solver's job. The job of the developer is to give the solver a scoring function that accurately describes solution quality. Your way of telling the solver what is right and what is wrong is to make sure that the wrong things have a worse score than the right things. – Lukáš Petrovický Dec 10 '22 at 15:52
  • I have multiple constraints and I am using HardMediumSoftScore. I think whatever scoring function I provide, it may choose a solution that will not be feasible for my use case(most of the time). Becuase I cannot afford to break any constraint and I prefer to leave the appointment unassigned then assigning an incorrect sales rep – user101 Dec 10 '22 at 18:37
  • If I can just tweak the OP library to ignore all negative scores and only choose the best score among the positive ones then I can rebuild the OP library and use it for my use case correctly – user101 Dec 10 '22 at 18:40
  • Are you aware where in the library(Optaplanner) are we choosing the best score, so that I can apply my tweak in the code? – user101 Dec 10 '22 at 18:42
  • 1
    I am sorry, but you are on the wrong path. I strongly suggest you abandon this idea. – Lukáš Petrovický Dec 10 '22 at 18:48
0

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.
Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
  • I have implemented overconstrained planning where if there are no feasible solutions (no hard constraint are broken) then don't assign any sales rep to any appointment i.e. there is no way we can assign anybody to any appointment – user101 Dec 11 '22 at 11:01
  • But the algorithm should never assign a sales rep if even a single hard constraint is broken that makes the entire solution unusable – user101 Dec 11 '22 at 11:02
  • It will do that if your "assign all sales reps" is a medium constraint (so any hard constraint overrules it). – Geoffrey De Smet Dec 11 '22 at 12:02
  • And of course, no sales rep is assigned with `@PlaningPin pinned=true` that breaks hard constraints (regardless if the unfluence of the unpinned sales rep assignments). – Geoffrey De Smet Dec 11 '22 at 12:03
  • Check your constraints. Run ScoreManager.explainScore() on a best solution. Understand that seeing 100hard is impossible if you only have negative hard constraints. – Geoffrey De Smet Dec 11 '22 at 12:08
  • 100hard is just a hypothetical example I have given in the post. I meant that even if my score is even 100hard/0medium/-1soft , I wanted to reject that solution because it has -1soft(a constraint is broken) – user101 Dec 11 '22 at 13:06
  • Currently, I have implemented two constraints 1. Sales rep work hours check 2. Only one appointment per sales rep at any given time. For each of these constraint, I have a penalized method and a reward method – user101 Dec 11 '22 at 13:08
  • The reason why I have two methods for each constraint is because when I had only penalized method with overconstrainted planning enabled then 0hard/0medium/0soft(initial solution with no sales rep assigned) was always coming as the best solution – user101 Dec 11 '22 at 13:14
  • Hence, in order to improve the best score. I implement a reward method for improving the best score. – user101 Dec 11 '22 at 13:15
  • If -1hard+101hard is worse than 0hard, use BendableScore to get 4 score levels and make it -1veryHard/101hard so it prefers 0veryHard/0hard. – Geoffrey De Smet Dec 12 '22 at 08:47