Drools Planner:Planning optimization     by example    Geoffrey De Smet
DemoDrools PlannerTSP example                 2
Every organization has planning problems.                                            3
What is a planning problem?             Complete goals             With limited resources             Under constraints...
Hospital bed scheduling                                                          Assign each                             ...
Hospital nurse rostering                                                      Assign each                                ...
School timetabling                                                      Assign each                                      ...
Airline routing                                                      Assign each                                         ...
Bin packing in the cloud                                                       Assign each                               ...
Bin packing in the cloud                                                       Assign each                               ...
Which processes do we assignto this server?                           11
How did we find that solution?                             12
First fit by decreasing size                               13
First fit by decreasing size                               14
First fit by decreasing size                               15
First fit by decreasing size                               16
Another case               17
Try FFD again                18
Try FFD again                19
Try FFD again                20
Try FFD again                21
FFD failure              22
NP complete              23
NP complete                                                                  No silver bullet known                      ...
Multiple servers...                      25
Multiple servers...                      26
Multiple servers...                      27
Multiple servers...                      28
Multiple constraints...                          29
Multiple constraints...                          30
Multiple constraints...                          31
Multiple constraints...                          32
Organizations rarely optimize    planning problems.                                33
Reuse optimization algorithms?                                 34
   Open source (ASL 2.0)   Regular releases   Reference manual   Examples                            35
Given 2 solutions, which one is better?                                          36
Each Solution has 1 Score                            37
Each Solution has 1 Score                            38
Better score => better solution                              39
Better score => better solution                              40
Best score => best solution                              41
Best score => best solution                              42
Demo    Drools PlannerCloudBalance example                       43
Domain model
Server         45
Serverpublic class Server {    private int cpuPower;    private int memory;    private int networkBandwidth;    private in...
Process          47
Process          48
Process is a planning entity@PlanningEntitypublic class Process {    private int requiredCpuPower;    private int required...
Process has a               planning variable@PlanningEntitypublic class Process {    ...    private Server server;    @Pl...
CloudBalance               51
Solution CloudBalance:              problem factspublic class CloudBalance        implements Solution<HardAndSoftScore> { ...
Solution CloudBalance:              planning entitiespublic class CloudBalance        implements Solution<HardAndSoftScore...
Solution CloudBalance:              getProblemFactspublic class CloudBalance        implements Solution<HardAndSoftScore> ...
Solution CloudBalance:            scorepublic class CloudBalance     implements Solution<HardAndSoftScore> {    ...    pri...
Score constraints
Score calculation                   with Drools rule engine   DRL       ●   Declarative       ●   Like SQL, regular expre...
Soft constraint:                server costrule "serverCost" when      // there is a server      $s : Server($c : cost)   ...
Hard constraint:               CPU powerrule "requiredCpuPowerTotal" when      // there is a server      $s : Server($cpu ...
Solving it
Solver configuration by XML<solver>  <solutionClass>...CloudBalance</solutionClass>  <planningEntityClass>...Process</>  <...
SolvingXmlSolverFactory factory = new XmlSolverFactory(   "...SolverConfig.xml");Solver solver = factory.buildSolver();sol...
Optimization algorithms
Brute Force              64
Brute Force config<solver>  ...  <bruteForce /></solver>                                 65
Brute force scalability                     9 processes = 1.5 seconds                             * 100       6 processes ...
Brute force scalability                         12 processes = 17 minutes                                       * 680     ...
Plan 1200 processeswith brute force?                      68
First Fit            69
First Fit config<solver>  ...  <constructionHeuristic>   <constructionHeuristicType>FIRST_FIT</>  </constructionHeuristic>...
First Fit scalability                        71
First Fit resultsCPU: OKRAM: OKNetwork: OKCost active servers: shown                             72
First Fit Decreasing                       73
First Fit Decreasing config<solver>  ...  <constructionHeuristic>   <constructionHeuristicType>FIRST_FIT_DECREASING</>  </...
DifficultyComparatorpublic class ProcessDifficultyComparator        implements Comparator<Process> {    public int compare...
First Fit Decreasing scalability                               76
First Fit Decreasing results                               77
Local Search               78
Local Search comes after            Construction Heuristics<solver>  ...  <constructionHeuristic>   <constructionHeuristic...
Local Search needs to be            terminated<solver>  ...  <termination>   <maximumMinutesSpend>20</maximumMinutesSpend>...
Selecting moves<localSearch>  <selector>   <selector>     <moveFactoryClass>GenericChangeMoveFactory</>   </selector>   <s...
Hill climbing   Select the best improving move   Gets stuck in local optima       ●   Not good                          ...
Hill climbing<localSearch>  <selector>...</selector>  <forager>   <!-- Untweaked standard value -->   <minimalAcceptedSele...
Tabu search   Hill climbing ++   Select the best move       ●   Recently selected moves are taboo       ●   Even if the ...
Tabu Search<localSearch>  <selector>...</selector>  <acceptor>   <!-- Untweaked standard value -->   <propertyTabuSize>7</...
Simulated annealing   Randomly select moves       ●   Prefer improving moves               ●   Especially as time goes by...
Simulated Annealing<localSearch>  <selector>...</selector>  <acceptor>   <!-- Tweaked value -->   <simulatedAnnealingStart...
Local Search results                       88
Cost ($) reduction              Compared to First Fit                  ●   First Fit Decreasing                          ...
Organizations rarely optimize    planning problems.                                90
http://www.flickr.com/photos/techbirmingham/345897594/Organizations waste resources.                                      ...
Real-time planning
Real-time paradox   First Fit Decreasing 1200 processes = 8 seconds   Time allowed: 100ms after last change             ...
Demo  Real-time planningCloudBalance example                       94
DemoReal-time planning  TSP example                     95
Summary
Summary   Drools Planner optimizes planning   Adding constraints is easy and scalable   Switching/combining algorithms ...
Try an example!                  98
Q&A   Drools Planner homepage:        ●   http://www.jboss.org/drools/drools-planner   Reference manual:        ● http:/...
Upcoming SlideShare
Loading in...5
×

2012 02-04fosdem2012-droolsplanner-120207031949-phpapp01

110

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
110
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

2012 02-04fosdem2012-droolsplanner-120207031949-phpapp01

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

    Clipping is a handy way to collect important slides you want to go back to later.

×