Optaplanner result is not reproducible - optaplanner

I have migrated from Optaplanner version 7.5 to 7.10. While migration I saw bulk(Output) is not the same. While running for another time I am not getting the same output. Can you share which are necessary steps I need to consider while migration? Can you help me out?
The Job schedule is different too. Screenshot attached below:
The primary difference is for Run 1 executes LS 11040 but Run 2 for the same time gets to reach LS 11600 plus with environment and other run parameters maintained without being changed.
Job Schedule diffference
log1_run
11:35:06.421 [main ] INFO Solving started: time spent (225), best score (-1722init/0hard/0medium/0soft), environment mode (REPRODUCIBLE), random (JDK with seed 0).
11:35:06.682 [main ] DEBUG CH step (0), time spent (487), score (-1721init/0hard/0medium/-900000soft), selected move count (543), picked move (TimeWindowedCustomer-1 {null -> Vehicle-393}).
11:35:06.756 [main ] DEBUG CH step (1), time spent (561), score (-1720init/0hard/0medium/-1560000soft), selected move count (544), picked move (TimeWindowedCustomer-2 {null -> TimeWindowedCustomer-7}).
11:35:07.229 [main ] DEBUG CH step (2), time spent (1034), score (-1719init/0hard/0medium/-2520000soft), selected move count (545), picked move (TimeWindowedCustomer-3 {null -> Vehicle-396}).
11:35:07.266 [main ] DEBUG CH step (3), time spent (1071), score (-1718init/0hard/0medium/-3360000soft), selected move count (546), picked move (TimeWindowedCustomer-4 {null -> Vehicle-328}).
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
11:37:06.194 [main ] DEBUG LS step (11039), time spent (119999), score (0hard/-45187medium/-1556640000soft), best score (0hard/-45154medium/-1549560000soft), accepted/selected move count (1/3), picked move (TimeWindowedCustomer-1 {Vehicle-220} <-tailChainSwap-> TimeWindowedCustomer-8 {Vehicle-78}).
11:37:06.200 [main ] DEBUG LS step (11040), time spent (120005), score (-54512774442hard/-30651medium/-1659540000soft), best score (0hard/-45154medium/-1549560000soft), accepted/selected move count (0/1), picked move (TimeWindowedCustomer-12 {TimeWindowedCustomer-4} <-tailChainSwap-> null {TimeWindowedCustomer-16}).
11:37:06.203 [main ] INFO Local Search phase (1) ended: time spent (120008), best score (0hard/-45154medium/-1549560000soft), score calculation speed (5349/sec), step total (11041).
11:37:06.203 [main ] INFO Solving ended: time spent (120008), best score (0hard/-45154medium/-1549560000soft), score calculation speed (22645/sec), phase total (2), environment mode (REPRODUCIBLE).
log2 _run
11:38:29.684 [main ] INFO Solving started: time spent (213), best score (-1722init/0hard/0medium/0soft), environment mode (REPRODUCIBLE), random (JDK with seed 0).
11:38:29.955 [main ] DEBUG CH step (0), time spent (485), score (-1721init/0hard/0medium/-900000soft), selected move count (543), picked move (TimeWindowedCustomer-97041025 {null -> Vehicle-393}).
11:38:30.027 [main ] DEBUG CH step (1), time spent (557), score (-1720init/0hard/0medium/-1560000soft), selected move count (544), picked move (TimeWindowedCustomer-1 {null -> TimeWindowedCustomer-6}).
11:38:30.075 [main ] DEBUG CH step (2), time spent (605), score (-1719init/0hard/0medium/-2520000soft), selected move count (545), picked move (TimeWindowedCustomer-2 {null -> Vehicle-396}).
11:38:30.114 [main ] DEBUG CH step (3), time spent (644), score (-1718init/0hard/0medium/-3360000soft), selected move count (546), picked move (TimeWindowedCustomer-3 {null -> Vehicle-328}).
-------------------------------------------
--------------------------------------------
11:40:25.467 [main ] DEBUG LS step (11039), time spent (116004), score (0hard/-45187medium/-1556640000soft), best score (0hard/-45154medium/-1549560000soft), accepted/selected move count (1/3), picked move (TimeWindowedCustomer-1 {Vehicle-220} <-tailChainSwap-> TimeWindowedCustomer-8 {Vehicle-78}).
11:40:25.470 [main ] DEBUG LS step (11040), time spent (116011), score (0hard/-45187medium/-1556640000soft), best score (0hard/-45154medium/-1549560000soft), accepted/selected move count (0/1), picked move (TimeWindowedCustomer-12 {TimeWindowedCustomer-4} <-tailChainSwap-> null {TimeWindowedCustomer-16}).
--------------------------------------------
--------------------------------------------
11:40:29.474 [main ] DEBUG LS step (11674), time spent (120004), score (-4200029hard/-45093medium/-1554780000soft), best score (0hard/-45064medium/-1558380000soft), accepted/selected move count (0/1), picked move (TimeWindowedCustomer-9 {TimeWindowedCustomer-5 -> TimeWindowedCustomer-2}).
11:40:29.477 [main ] INFO Local Search phase (1) ended: time spent (120007), best score (0hard/-45064medium/-1558380000soft), score calculation speed (5451/sec), step total (11675).
11:40:29.477 [main ] INFO Solving ended: time spent (120007), best score (0hard/-45064medium/-1558380000soft), score calculation speed (22762/sec), phase total (2), environment mode (REPRODUCIBLE).

Reproducibility is only guaranteed on each step index, not on time (although they are correlated). When using time spent termination, for example 2 minutes, there is no guarantee how much CPU cycles OptaPlanner will get and how and when the JVM will hotspot optimize. In some situations, it might actually get almost no CPU cycles. In other situations it gets plenty - in any case OptaPlanner uses what it gets, nothing goes to waste.
Looking at the step index level, the two runs are exactly the same (except for the time spent):
First run:
11:37:06.194 ...LS step (11039), time spent (119999), score (0hard/-45187medium/-1556640000soft), best score (0hard/-45154medium/-1549560000soft), accepted/selected move count (1/3), picked move (TimeWindowedCustomer-1 {Vehicle-220} <-tailChainSwap-> TimeWindowedCustomer-8 {Vehicle-78}).
Second run:
11:40:25.467 ...LS step (11039), time spent (116004), score (0hard/-45187medium/-1556640000soft), best score (0hard/-45154medium/-1549560000soft), accepted/selected move count (1/3), picked move (TimeWindowedCustomer-1 {Vehicle-220} <-tailChainSwap-> TimeWindowedCustomer-8 {Vehicle-78}).
To get the exact same result between 2 results, use a step limit termination instead.

Related

Partitioned Search in Optaplanner

I have been trying to test the new partitioned search function out from Optaplanner v7 release. I implemented a custom SolutionPartitioner according to the example provided in the documentation.
It seems working fine as I can see the scores are improving with each thread (no hard rule broken, soft scores are improving)... however at every merging/reduce step to the main thread it suddenly got hit by a large negative hard scores from a hard constraint which I can't figure out how does it developed and it's hard to debug.
This hard constraint is similar to the 'requiredCpuPowerTotal' rule in the CloudBalance example - simply checking if the allocation has exceeded one's capacity.
This is the output at the end from 2 threads:
[OptaPool-1-PartThread-2] INFO Local Search phase (1) ended: time
spent (120000), best score (0hard/-28317medium/4671205soft), score
calculation speed (1544/sec), step total (60272).
[OptaPool-1-PartThread-1] INFO Local Search phase (1) ended: time
spent (120000), best score (0hard/-16medium/3362676soft), score
calculation speed (1807/sec), step total (112408).
So I am expecting the final score in the main thread to be the sum of those two threads, i.e. 0hard/-28333medium/8033881soft.
But the actual result is very different:
[main] DEBUG PS step (229), time spent (120103), score
(-3458hard/-159medium/10503603soft), best score
(0hard/-7837556medium/0soft), picked move
(org.optaplanner.core.impl.partitionedsearch.scope.PartitionChangeMove#5fe73332).
[main] DEBUG PS step (230), time spent (120214), score
(-3452hard/-160medium/10511701soft), best score
(0hard/-7837556medium/0soft), picked move
(org.optaplanner.core.impl.partitionedsearch.scope.PartitionChangeMove#5dd30a7d).
[main] INFO Partitioned Search phase (0) ended: time spent (121216),
best score (0hard/-7837556medium/0soft), score calculation speed
(2/sec), step total (231), partCount (2), runnablePartThreadLimit
(2).
As you can see when the results from two threads reduced to the main thread, there is a -3452hard therefore the main thread has to pick the beginning score as the best score.
Any idea of how could is happen and how should I debug it? Thanks.

optaplanner incorrect cloud balance output

I am trying a simple Cloud balance example in the opta planner tutorial.
I have 2 computers and 4 process ( the same example which is explained in the opta planner documentation ).Each process needs certain amount of RAM and CPU .
The final solved output is incorrect which you can see from the logs below. Hard rules are violated.
Ideally Process 1 and 4 should fit in computer 1 .
Process 2 and 3 should fit in computer 2.
From the logs looks like HeuristicPhase happens but local search is not showing up in the logs.
Please guide me what I am dong wrong.
After building unsolvedCloudBalance Computers [ Computer [C1][7 CPU][6 RAM], Computer [C2][6 CPU][6 RAM]] Processes [ Process [P1][5 CPU][5 RAM], Process [P2][4 CPU][3 RAM], Process [P3][2 CPU][3 RAM], Process [P4][2 CPU][1 RAM]] Hard Soft Score null
01:35:57.099 [main] INFO o.o.core.impl.solver.DefaultSolver - Solving started: time spent (3), best score (4uninitialized/0hard/0soft), environment mode (REPRODUCIBLE), random (JDK with seed 0).
01:35:57.099 [main] INFO o.o.core.impl.solver.DefaultSolver - Solving started: time spent (3), best score (4uninitialized/0hard/0soft), environment mode (REPRODUCIBLE), random (JDK with seed 0).
01:35:57.104 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase - CH step (0), time spent (9), score (0hard/-800soft), selected move count (2), picked move ( Process [P1][5 CPU][5 RAM] {null -> Computer [C2][6 CPU][6 RAM]}).
01:35:57.104 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase - CH step (0), time spent (9), score (0hard/-800soft), selected move count (2), picked move ( Process [P1][5 CPU][5 RAM] {null -> Computer [C2][6 CPU][6 RAM]}).
01:35:57.104 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase - CH step (1), time spent (9), score (0hard/-1800soft), selected move count (2), picked move ( Process [P2][4 CPU][3 RAM] {null -> Computer [C1][7 CPU][6 RAM]}).
01:35:57.104 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase - CH step (1), time spent (9), score (0hard/-1800soft), selected move count (2), picked move ( Process [P2][4 CPU][3 RAM] {null -> Computer [C1][7 CPU][6 RAM]}).
01:35:57.104 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase - CH step (2), time spent (9), score (0hard/-1800soft), selected move count (1), picked move ( Process [P3][2 CPU][3 RAM] {null -> Computer [C1][7 CPU][6 RAM]}).
01:35:57.104 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase - CH step (2), time spent (9), score (0hard/-1800soft), selected move count (1), picked move ( Process [P3][2 CPU][3 RAM] {null -> Computer [C1][7 CPU][6 RAM]}).
01:35:57.105 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase - CH step (3), time spent (10), score (-1hard/-1800soft), selected move count (2), picked move ( Process [P4][2 CPU][1 RAM] {null -> Computer [C2][6 CPU][6 RAM]}).
01:35:57.105 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase - CH step (3), time spent (10), score (-1hard/-1800soft), selected move count (2), picked move ( Process [P4][2 CPU][1 RAM] {null -> Computer [C2][6 CPU][6 RAM]}).
01:35:57.106 [main] INFO o.o.c.i.c.DefaultConstructionHeuristicPhase - Construction Heuristic phase (0) ended: step total (4), time spent (11), best score (-1hard/-1800soft).
01:35:57.106 [main] INFO o.o.c.i.c.DefaultConstructionHeuristicPhase - Construction Heuristic phase (0) ended: step total (4), time spent (11), best score (-1hard/-1800soft).
01:36:07.095 [main] DEBUG o.o.c.i.l.DefaultLocalSearchPhase - LS step (0), time spent (10000), score (-2hard/-1800soft), best score (-1hard/-1800soft), accepted/selected move count (0/9152031), picked move ( Process [P4][2 CPU][1 RAM] { Computer [C2][6 CPU][6 RAM] -> Computer [C1][7 CPU][6 RAM]}).
01:36:07.095 [main] DEBUG o.o.c.i.l.DefaultLocalSearchPhase - LS step (0), time spent (10000), score (-2hard/-1800soft), best score (-1hard/-1800soft), accepted/selected move count (0/9152031), picked move ( Process [P4][2 CPU][1 RAM] { Computer [C2][6 CPU][6 RAM] -> Computer [C1][7 CPU][6 RAM]}).
01:36:07.096 [main] INFO o.o.c.i.l.DefaultLocalSearchPhase - Local Search phase (1) ended: step total (1), time spent (10001), best score (-1hard/-1800soft).
01:36:07.096 [main] INFO o.o.c.i.l.DefaultLocalSearchPhase - Local Search phase (1) ended: step total (1), time spent (10001), best score (-1hard/-1800soft).
01:36:07.096 [main] INFO o.o.core.impl.solver.DefaultSolver - Solving ended: time spent (10001), best score (-1hard/-1800soft), average calculate count per second (915112), environment mode (REPRODUCIBLE).
01:36:07.096 [main] INFO o.o.core.impl.solver.DefaultSolver - Solving ended: time spent (10001), best score (-1hard/-1800soft), average calculate count per second (915112), environment mode (REPRODUCIBLE).
time taken 10 sec
It worked as expected after I added difficultyComparatorClass to the #PlanningEntity annotation.

Optaplanner Best Score Null Pointer exception

14:46:27.239 [main] INFO o.o.core.impl.solver.DefaultSolver - Solving started: time spent (155), best score (3uninitialized/-3hard/-3soft), environment mode (REPRODUCIBLE), random (JDK with seed 0).
14:46:27.320 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase - CH step (0), time spent (237), score (0hard/-2soft), selected move count (3), picked move (Requests [id=4, res=null] {null -> Resources [Name=Doc2, Id=0002]}).
14:46:27.331 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase - CH step (1), time spent (248), score (3hard/-1soft), selected move count (3), picked move (Requests [id=5, res=null] {null -> Resources [Name=Doc2, Id=0002]}).
14:46:27.347 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase - CH step (2), time spent (264), score (5hard/0soft), selected move count (3), picked move (Requests [id=6, res=null] {null -> Resources [Name=Doc2, Id=0002]}).
14:46:27.348 [main] INFO o.o.c.i.c.DefaultConstructionHeuristicPhase - Construction Heuristic phase (0) ended: step total (3), time spent (264), best score (null).
Although the score is calculated in step 1 and 2 why i am getting Null Pointer Exception for Best Score.
ERROR MSG
Exception in thread "main" java.lang.NullPointerException
at org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore.compareTo(HardSoftScore.java:152)
at org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore.compareTo(HardSoftScore.java:30)
at org.optaplanner.core.impl.localsearch.decider.acceptor.lateacceptance.LateAcceptanceAcceptor.isAccepted(LateAcceptanceAcceptor.java:67)
at org.optaplanner.core.impl.localsearch.decider.LocalSearchDecider.processMove(LocalSearchDecider.java:166)
at org.optaplanner.core.impl.localsearch.decider.LocalSearchDecider.doMove(LocalSearchDecider.java:149)
at org.optaplanner.core.impl.localsearch.decider.LocalSearchDecider.decideNextStep(LocalSearchDecider.java:121)
at org.optaplanner.core.impl.localsearch.DefaultLocalSearchPhase.solve(DefaultLocalSearchPhase.java:72)
at org.optaplanner.core.impl.solver.DefaultSolver.runPhases(DefaultSolver.java:215)
at org.optaplanner.core.impl.solver.DefaultSolver.solve(DefaultSolver.java:176)
at app.resourceAllocation.main(resourceAllocation.java:44)
This bug was fixed in https://issues.jboss.org/browse/PLANNER-207 some years ago.

Optaplanner - local phase 0 step in total in 1 minute

I have set up a solver with two local search phases and it works fine. However, there was a time that the 2nd phase didn't make any move in about 1 minute, as the log shows below:
...
2016-05-07 14:14:55,847 [main] DEBUG LS step (10069), time spent (593822), score (0hard/-81medium/5395020soft), best score (0hard/-80medium/5393781soft), accepted/selected move count (5/48), picked move (CL [cID=1147576, id=27246 => SL [id=49, sID=E942648]] <=> CL [cID=1133912, id=14716 => SL [ id=7, sID=E942592]]).
2016-05-07 14:14:55,858 [main] DEBUG LS step (10070), time spent (593833), score (0hard/-81medium/5395390soft), best score (0hard/-80medium/5393781soft), accepted/selected move count (5/18), picked move (CL [cID=1142322, id=22533 => SL [ id=51, sID=E943251]] <=> CL [cID=1134362, id=14118 => SL [ id=49, sID=E942648]]).
2016-05-07 14:14:55,858 [main] INFO Local Search phase (1) ended: step total (10071), time spent (593833), best score (0hard/-80medium/5393781soft).
2016-05-07 14:16:05,042 [main] INFO Local Search phase (2) ended: step total (0), time spent (663017), best score (0hard/-80medium/5393781soft).
2016-05-07 14:16:05,042 [main] INFO Solving ended: time spent (663017), best score (0hard/-80medium/5393781soft), average calculate count per second (2771).
Before phase 1 ended, there wasn't any improvement in the last couple of steps. And phase 2 started but made 0 step in a minute. The solver then ended because it has reached the maximum time allowed.
I'm a bit surprised that phase 2 made no step at all. Is it simply because it didn't manage to find any better score?
If you don't see any moves in TRACE log (as suggested in comments), it could be because you're using a custom move list factory and that's taken too long to initialize.

Optaplanner - Local search phase terminated without picking a nextStep

I ran into a situation where only two valid moves that is allowed happens during the construction phase. This results in a situation where there is no valid moves to perform during local search phase. All the moves tried during the local search phase is not doable.
This results in NullPointerException at the end of local search phase.
Exception in thread "main" java.lang.NullPointerException
at org.optaplanner.core.impl.solver.scope.DefaultSolverScope.getBestScoreWithUninitializedPrefix(DefaultSolverScope.java:205)
at org.optaplanner.core.impl.solver.DefaultSolver.outerSolvingEnded(DefaultSolver.java:216)
at org.optaplanner.core.impl.solver.DefaultSolver.solve(DefaultSolver.java:161)
at com.example.jobplanner.Application.main(Application.java:328)
Here is the trace of the two valid moves that happens during the construction phase.
CH step (0), time spent (151), score (0hard/0soft), selected move count (1), picked move (com.example.jobplanner.domain.ServiceJobChip#981500 => com.example.jobplanner.domain.ShopFloorBay#9da471).
Update [fact 0:5:4216052:4216052:10:DEFAULT:NON_TRAIT:com.example.jobplanner.domain.ServiceJobChip#4054f4]
CH step (1), time spent (158), score (0hard/0soft), selected move count (1), picked move (com.example.jobplanner.domain.ServiceJobChip#4054f4 => com.example.jobplanner.domain.ShopFloorBay#15ac5d5).
Construction Heuristic phase (0) ended: step total (2), time spent (159), best score (null).
The model of the domain I am trying to optimize would be something like:
Each bay has --> set of tasks that can be performed
Job -> collection of tasks to be performed
Task --> can be scheduled to bays which can perform the task

Resources