KIE Community
January 20, 2021

# OptaPlanner Shadow Variables for the Vehicle Routing Problem and Task Assignment

Learn how to simplify your OptaPlanner models with shadow variables and tackle otherwise near impossible constraints.

KieLive#22: OptaPlanner Shadow Variables for the Vehicle Routing Problem and Task Assignment

Some OptaPlanner use cases, such as the Vehicle Routing Problem (especially with Time Windows) and Task Assignment use shadow variables. A shadow variable is something that changes during planning, but unlike a genuine planning variable, doesn’t do so freely, it’s not decided by OptaPlanner directly. Instead, it’s value is deterministically defined by the values of the genuine planning variables. For example in VRP, the arrival time of a vehicle at each visit, is determined by the order of the visits of that vehicle and the departure time of the vehicle.

Geoffrey De Smet is the founder and lead of OptaPlanner (www.optaplanner.org), the open source AI constraint solver in Java that is used across the globe to automatically solve employee rostering, vehicle routing, task assignment, maintenance scheduling and other planning problems. He's an international speaker.

January 20, 2021

## Transcript

2. ### What is a shadow variable? What is a shadow variable?

(Genuine) planning variable Integer a with a value range of 1-10
3. ### What is a shadow variable? What is a shadow variable?

(Genuine) planning variable Integer a with a value range of 1-10 Shadow variable Integer b = a + 100
4. ### What is a shadow variable? What is a shadow variable?

(Genuine) planning variable Integer a with a value range of 1-10 Shadow variable Integer b = a + 100 Behavior: If a = 1 then b = 101
5. ### What is a shadow variable? What is a shadow variable?

(Genuine) planning variable Integer a with a value range of 1-10 Shadow variable Integer b = a + 100 Behavior: If a = 1 then b = 101 If a = 2 then b = 102
6. ### What is a shadow variable? What is a shadow variable?

(Genuine) planning variable Integer a with a value range of 1-10 Shadow variable Integer b = a + 100 Behavior: If a = 1 then b = 101 If a = 2 then b = 102 If a = 7 then b = 107
7. ### Why are shadow variables useful? Why are shadow variables useful?

int a; int getA() { return a; } // This is NOT a shadow variable, it's non-simple getter int getB() { return a + 100; }
8. None
9. None
10. None
11. None

13. None
14. None
15. None

are de-normalization information duplication

are de-normalization information duplication Shadow vars work incrementally Shadow variable corruption is possible!

are de-normalization information duplication Shadow vars work incrementally Shadow variable corruption is possible! Shadow var Room.getLessonCount() is probably not worth it.

23. ### Vehicle Routing Problem with Time Vehicle Routing Problem with Time

Windows (VRPTW) Windows (VRPTW) Swing demo!
24. ### VRPTW Customer visit properties VRPTW Customer visit properties Input: readyTime:

Time window start dueTime: Time window end serviceTime Change during planning:
25. ### VRPTW Customer visit properties VRPTW Customer visit properties Input: readyTime:

Time window start dueTime: Time window end serviceTime Change during planning: arrivalTime = previousDepartureTime + travelTim
26. ### VRPTW Customer visit properties VRPTW Customer visit properties Input: readyTime:

Time window start dueTime: Time window end serviceTime Change during planning: arrivalTime = previousDepartureTime + travelTim startTime = max(readyTime, arrivalTime
27. ### VRPTW Customer visit properties VRPTW Customer visit properties Input: readyTime:

Time window start dueTime: Time window end serviceTime Change during planning: arrivalTime = previousDepartureTime + travelTim startTime = max(readyTime, arrivalTime departureTime = startTime + serviceTim
28. ### VRPTW Customer visit properties VRPTW Customer visit properties Input: readyTime:

Time window start dueTime: Time window end serviceTime Change during planning: arrivalTime = previousDepartureTime + travelTim startTime = max(readyTime, arrivalTime departureTime = startTime + serviceTim Hard constraint: departureTime <= dueTime
29. None
30. None
31. None
32. None
33. None
34. None
35. ### VRPTW with lunches VRPTW with lunches Original: arrivalTime = previousDepartureTime

+ travelTim startTime = max(readyTime, arrivalTime
36. ### VRPTW with lunches VRPTW with lunches Original: arrivalTime = previousDepartureTime

+ travelTim startTime = max(readyTime, arrivalTime Extra logic to allow for lunch: If travelTime contains 12:00, add 1 hour to transitTime for lunch. Else serviceTime contains 12:00, add 1 hour to serviceTime for lunch.
37. ### VRPTW with lunches VRPTW with lunches Original: arrivalTime = previousDepartureTime

+ travelTim startTime = max(readyTime, arrivalTime Extra logic to allow for lunch: If travelTime contains 12:00, add 1 hour to transitTime for lunch. Else serviceTime contains 12:00, add 1 hour to serviceTime for lunch. All this logic ends up in the VariableListener.

39. None
40. None
41. None
42. None
43. None
44. None

47. None
48. None
49. None
50. None
51. None
52. None
53. None
54. None
55. None
56. None
57. None
58. None
59. None
60. None
61. None

63. ### Quick starts Quick starts github.com/kiegroup/optaplanner-quickstarts (https://github.com/kiegroup/optaplanner- quickstarts) \$ git clone

git@github.com:kiegroup/optaplanner-quickstarts.git ... \$ cd optaplanner-quickstarts \$ cd quarkus-school-timetabling \$ mvn quarkus:dev ...
64. ### @GeoffreyDeSmet Q & A Q & A Homepage Slides User

guide Feedback www.optaplanner.org (https://www.optapl www.optaplanner.org/learn/slides.html (https://www.optaplanner.org/learn/slides www.optaplanner.org/learn/documentatio (https://www.optaplanner.org/learn/docum @GeoffreyDeSmet (https://twitter.com/GeoffreyDeSmet)