2012 02-04 fosdem 2012 - drools planner

2,560 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,560
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
121
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

2012 02-04 fosdem 2012 - drools planner

  1. 1. Geoffrey De Smet Drools Planner: Planning optimization by example
  2. 2. Demo Drools Planner TSP example
  3. 3. Every organization has planning problems.
  4. 4. What is a planning problem? <ul><li>Complete goals
  5. 5. With limited resources
  6. 6. Under constraints </li></ul>
  7. 7. Hospital bed scheduling <ul><li>Assign each </li><ul><li>Patient </li></ul><li>To </li><ul><li>Bed </li></ul><li>Constraints </li><ul><li>Length of stay
  8. 8. Room requirements
  9. 9. Room preferences </li></ul></ul>http://www.flickr.com/photos/markhillary/2227726759/
  10. 10. Hospital nurse rostering <ul><li>Assign each </li><ul><li>Shift </li></ul><li>To </li><ul><li>Nurse </li></ul><li>Constraints </li><ul><li>Employment contract
  11. 11. Free time preferences
  12. 12. Skills </li></ul></ul>http://www.flickr.com/photos/glenpooh/709704564/
  13. 13. School timetabling <ul><li>Assign each </li><ul><li>Course </li></ul><li>To </li><ul><li>Room
  14. 14. Timeslot </li></ul><li>Constraints </li><ul><li>No simultaneous courses </li><ul><li>Per room
  15. 15. Per teacher
  16. 16. Per student </li></ul></ul></ul>http://www.flickr.com/photos/phelyan/2281095105/
  17. 17. Airline routing <ul><li>Assign each </li><ul><li>Flight </li></ul><li>To </li><ul><li>Airplane
  18. 18. Crew </li></ul><li>Constraints </li><ul><li>Airplane/crew depart from where they arrive
  19. 19. Minimize mileage </li></ul></ul>http://www.flickr.com/photos/yorickr/3674349657/
  20. 20. Bin packing in the cloud <ul><li>Assign each </li><ul><li>Process </li></ul><li>To </li><ul><li>Server </li></ul><li>Constraints </li><ul><li>Hardware requirements
  21. 21. Minimize server cost </li></ul></ul>http://www.flickr.com/photos/torkildr/3462607995/
  22. 22. Bin packing in the cloud <ul><li>Assign each </li><ul><li>Process </li></ul><li>To </li><ul><li>Server </li></ul><li>Constraints </li><ul><li>Hardware requirements
  23. 23. Minimize server cost </li></ul></ul>http://www.flickr.com/photos/torkildr/3462607995/
  24. 24. Which processes do we assign to this server?
  25. 25. How did we find that solution?
  26. 26. First fit by decreasing size
  27. 27. First fit by decreasing size
  28. 28. First fit by decreasing size
  29. 29. First fit by decreasing size
  30. 30. Another case
  31. 31. Try FFD again
  32. 32. Try FFD again
  33. 33. Try FFD again
  34. 34. Try FFD again
  35. 35. FFD failure
  36. 36. NP complete
  37. 37. NP complete <ul><li>No silver bullet known </li><ul><li>Holy grail of computer science </li><ul><li>P == NP </li></ul><li>Probably does not exist </li><ul><li>P != NP </li></ul></ul><li>Root problem of all planning problems </li></ul>http://www.flickr.com/photos/annguyenphotography/3267723713/
  38. 38. Multiple servers...
  39. 39. Multiple servers...
  40. 40. Multiple servers...
  41. 41. Multiple servers...
  42. 42. Multiple constraints...
  43. 43. Multiple constraints...
  44. 44. Multiple constraints...
  45. 45. Multiple constraints...
  46. 46. Organizations rarely optimize planning problems.
  47. 47. Reuse optimization algorithms?
  48. 48. <ul><li>Open source (ASL 2.0)
  49. 49. Regular releases
  50. 50. Reference manual
  51. 51. Examples </li></ul>
  52. 52. Given 2 solutions, which one is better?
  53. 53. Each Solution has 1 Score
  54. 54. Each Solution has 1 Score
  55. 55. Better score => better solution
  56. 56. Better score => better solution
  57. 57. Best score => best solution
  58. 58. Best score => best solution
  59. 59. Demo Drools Planner CloudBalance example
  60. 60. Domain model
  61. 61. Server
  62. 62. Server public class Server { private int cpuPower ; private int memory ; private int networkBandwidth ; private int cost ; // getters }
  63. 63. Process
  64. 64. Process
  65. 65. Process is a planning entity @PlanningEntity public class Process { private int requiredCpuPower ; private int requiredMemory ; private int requiredNetworkBandwidth ; ... // getters, clone, equals, hashcode }
  66. 66. Process has a planning variable @PlanningEntity public class Process { ... private Server server ; @PlanningVariable @ValueRange(type = FROM_SOLUTION_PROPERTY, solutionProperty = &quot;serverList&quot; ) public Server getServer() { return server ; } public void setServer(Server server) { ... } ... }
  67. 67. CloudBalance
  68. 68. Solution CloudBalance: problem facts public class CloudBalance implements Solution<HardAndSoftScore> { private List<Server> serverList ; public List<Server> getServerList() { return serverList ; } ... }
  69. 69. Solution CloudBalance: planning entities public class CloudBalance implements Solution<HardAndSoftScore> { ... private List<Process> processList ; @PlanningEntityCollectionProperty public List<Process> getProcessList() { return processList ; } ... }
  70. 70. Solution CloudBalance: getProblemFacts public class CloudBalance implements Solution<HardAndSoftScore> { ... // Used in score constraints public Collection<Object> getProblemFacts() { List<Object> facts = new ArrayList<Object>(); facts.addAll( serverList ); return facts; } ... }
  71. 71. Solution CloudBalance: score public class CloudBalance implements Solution<HardAndSoftScore> { ... private HardAndSoftScore score ; public HardAndSoftScore getScore() { ... } public void setScore(HardAndSoftScore score) { ... } ... }
  72. 72. Score constraints
  73. 73. Score calculation with Drools rule engine <ul><li>DRL </li><ul><li>Declarative
  74. 74. Like SQL, regular expressions </li></ul><li>Performance + scalability </li><ul><li>Indexing, ReteOO, ...
  75. 75. Incremental (delta based) score calculation </li><ul><li>Solution changes => only recalculate part of score </li></ul></ul></ul>
  76. 76. Soft constraint: server cost rule &quot;serverCost&quot; when // there is a server $s : Server( $c : cost) // there is a processes on that server exists Process(server == $s ) then // lower soft score by $c insertLogical( new IntConstraintOccurrence( &quot;serverCost&quot; , ConstraintType. NEGATIVE_SOFT , $c , $s )); end
  77. 77. Hard constraint: CPU power rule &quot;requiredCpuPowerTotal&quot; when // there is a server $s : Server( $cpu : cpuPower) // with too little cpu for its processes $total : Number(intValue > $cpu ) from accumulate ( Process(server == $s , $requiredCpu : requiredCpuPower), sum( $requiredCpu ) ) then // lower hard score by ($total.intValue() - $cpu) end
  78. 78. Solving it
  79. 79. Solver configuration by XML < solver > < solutionClass > ... CloudBalance</ solutionClass > < planningEntityClass > ... Process</> < scoreDrl > ... ScoreRules.drl</ scoreDrl > < scoreDefinition > < scoreDefinitionType >HARD_AND_SOFT</> </ scoreDefinition > <!-- optimization algorithms --> </ solver >
  80. 80. Solving XmlSolverFactory factory = new XmlSolverFactory( &quot; ... SolverConfig.xml&quot; ); Solver solver = factory .buildSolver(); solver.setPlanningProblem( cloudBalance ); solver.solve(); cloudBalance = ( CloudBalance) solver.getBestSolution();
  81. 81. Optimization algorithms
  82. 82. Brute Force
  83. 83. Brute Force config < solver > ... < bruteForce /> </ solver >
  84. 84. Brute force scalability 6 processes = 15 ms 9 processes = 1.5 seconds * 100
  85. 85. Brute force scalability 12 processes = 17 minutes 6 processes = 15 ms 9 processes = 1.5 seconds * 100 * 680
  86. 86. Plan 1200 processes with brute force?
  87. 87. First Fit
  88. 88. First Fit config < solver > ... < constructionHeuristic > < constructionHeuristicType >FIRST_FIT</> </ constructionHeuristic > </ solver >
  89. 89. First Fit scalability
  90. 90. First Fit results CPU: OK RAM: OK Network: OK Cost active servers: shown
  91. 91. First Fit Decreasing
  92. 92. First Fit Decreasing config < solver > ... < constructionHeuristic > < constructionHeuristicType >FIRST_FIT_DECREASING</> </ constructionHeuristic > </ solver >
  93. 93. DifficultyComparator public class ProcessDifficultyComparator implements Comparator<Process> { public int compare(Process a, Process b) { // Compare on requiredCpuPower * requiredMemory // * requiredNetworkBandwidth } } @PlanningEntity (difficultyComparatorClass = ProcessDifficultyComparator. class ) public class Process { ... }
  94. 94. First Fit Decreasing scalability
  95. 95. First Fit Decreasing results
  96. 96. Local Search
  97. 97. Local Search comes after Construction Heuristics < solver > ... < constructionHeuristic > < constructionHeuristicType >FIRST_FIT_DECREASING</> </ constructionHeuristic > < localSearch > ... < localSearch > </ solver >
  98. 98. Local Search needs to be terminated < solver > ... < termination > < maximumMinutesSpend >20</ maximumMinutesSpend > </ termination > ... </ solver >
  99. 99. Selecting moves < localSearch > < selector > < selector > < moveFactoryClass >GenericChangeMoveFactory</> </ selector > < selector > < moveFactoryClass >GenericSwapMoveFactory</> </ selector > </ selector > ... tabu search, simulated annealing or ... </ localSearch >
  100. 100. Hill climbing <ul><li>Select the best improving move
  101. 101. Gets stuck in local optima </li><ul><li>Not good </li></ul></ul>
  102. 102. Hill climbing < localSearch > < selector > ... </ selector > < forager > <!-- Untweaked standard value --> < minimalAcceptedSelection >1000</> </ forager > </ localSearch >
  103. 103. Tabu search <ul><li>Hill climbing ++
  104. 104. Select the best move </li><ul><li>Recently selected moves are taboo
  105. 105. Even if the best move is not improving </li></ul><li>Does not get stuck in local optima </li></ul>
  106. 106. Tabu Search < localSearch > < selector > ... </ selector > < acceptor > <!-- Untweaked standard value --> < propertyTabuSize >7</ propertyTabuSize > </ acceptor > < forager > <!-- Untweaked standard value --> < minimalAcceptedSelection >1000</> </ forager > </ localSearch >
  107. 107. Simulated annealing <ul><li>Randomly select moves </li><ul><li>Prefer improving moves </li><ul><li>Especially as time goes by </li></ul></ul><li>Does not get stuck in local optima </li></ul>
  108. 108. Simulated Annealing < localSearch > < selector > ... </ selector > < acceptor > <!-- Tweaked value --> < simulatedAnnealingStartingTemperature > 0hard/400soft</> </ acceptor > < forager > <!-- Untweaked standard value --> < minimalAcceptedSelection >4</> </ forager > </ localSearch >
  109. 109. Local Search results
  110. 110. Cost ($) reduction <ul><li>Compared to First Fit </li><ul><li>First Fit Decreasing </li><ul><li>49 480 $ = 4 % </li></ul><li>Tabu Search </li><ul><li>160 860 $ = 14 % </li></ul><li>Simulated annealing </li><ul><li>128 950 $ = 11 % </li></ul></ul><li>Few constraints here </li><ul><li>=> low ROI </li></ul></ul>
  111. 111. Organizations rarely optimize planning problems.
  112. 112. http://www.flickr.com/photos/techbirmingham/345897594/ Organizations waste resources.
  113. 113. Real-time planning
  114. 114. Real-time paradox <ul><li>First Fit Decreasing 1200 processes = 8 seconds
  115. 115. Time allowed: 100ms after last change </li></ul>
  116. 116. Demo Real-time planning CloudBalance example
  117. 117. Demo Real-time planning TSP example
  118. 118. Summary
  119. 119. Summary <ul><li>Drools Planner optimizes planning
  120. 120. Adding constraints is easy and scalable
  121. 121. Switching/combining algorithms is easy </li></ul>
  122. 122. Try an example!
  123. 123. Q&A <ul><li>Drools Planner homepage: </li><ul><li>http://www.jboss.org/drools/drools-planner </li></ul><li>Reference manual: </li><ul><li>http://www.jboss.org/drools/documentation </li></ul><li>Download this presentation: </li><ul><li>http://www.jboss.org/drools/presentations </li></ul><li>Twitter: @geoffreydesmet
  124. 124. Google+: Geoffrey De Smet </li></ul>

×