2012 02-04fosdem2012-droolsplanner-120207031949-phpapp01

  • 87 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
87
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
2
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Drools Planner:Planning optimization by example Geoffrey De Smet
  • 2. DemoDrools PlannerTSP example 2
  • 3. Every organization has planning problems. 3
  • 4. What is a planning problem?  Complete goals  With limited resources  Under constraints 4
  • 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. Hospital nurse rostering  Assign each ● Shift  To ● Nurse  Constraintshttp://www.flickr.com/photos/glenpooh/709704564/ ● Employment contract ● Free time preferences ● Skills 6
  • 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. 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. Bin packing in the cloud  Assign each ● Process  To ● Server  Constraints ● Hardwarehttp://www.flickr.com/photos/torkildr/3462607995/ requirements ● Minimize server cost 9
  • 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. Which processes do we assignto this server? 11
  • 12. How did we find that solution? 12
  • 13. First fit by decreasing size 13
  • 14. First fit by decreasing size 14
  • 15. First fit by decreasing size 15
  • 16. First fit by decreasing size 16
  • 17. Another case 17
  • 18. Try FFD again 18
  • 19. Try FFD again 19
  • 20. Try FFD again 20
  • 21. Try FFD again 21
  • 22. FFD failure 22
  • 23. NP complete 23
  • 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. Multiple servers... 25
  • 26. Multiple servers... 26
  • 27. Multiple servers... 27
  • 28. Multiple servers... 28
  • 29. Multiple constraints... 29
  • 30. Multiple constraints... 30
  • 31. Multiple constraints... 31
  • 32. Multiple constraints... 32
  • 33. Organizations rarely optimize planning problems. 33
  • 34. Reuse optimization algorithms? 34
  • 35.  Open source (ASL 2.0) Regular releases Reference manual Examples 35
  • 36. Given 2 solutions, which one is better? 36
  • 37. Each Solution has 1 Score 37
  • 38. Each Solution has 1 Score 38
  • 39. Better score => better solution 39
  • 40. Better score => better solution 40
  • 41. Best score => best solution 41
  • 42. Best score => best solution 42
  • 43. Demo Drools PlannerCloudBalance example 43
  • 44. Domain model
  • 45. Server 45
  • 46. Serverpublic class Server { private int cpuPower; private int memory; private int networkBandwidth; private int cost; // getters} 46
  • 47. Process 47
  • 48. Process 48
  • 49. Process is a planning entity@PlanningEntitypublic class Process { private int requiredCpuPower; private int requiredMemory; private int requiredNetworkBandwidth; ... // getters, clone, equals, hashcode} 49
  • 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. CloudBalance 51
  • 52. Solution CloudBalance: problem factspublic class CloudBalance implements Solution<HardAndSoftScore> { private List<Server> serverList; public List<Server> getServerList() { return serverList; } ...} 52
  • 53. Solution CloudBalance: planning entitiespublic class CloudBalance implements Solution<HardAndSoftScore> { ... private List<Process> processList; @PlanningEntityCollectionProperty public List<Process> getProcessList() { return processList; } ...} 53
  • 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. Solution CloudBalance: scorepublic class CloudBalance implements Solution<HardAndSoftScore> { ... private HardAndSoftScore score; public HardAndSoftScore getScore() {...} public void setScore(HardAndSoftScore score) {...} ...} 55
  • 56. Score constraints
  • 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. 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. 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. Solving it
  • 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. SolvingXmlSolverFactory factory = new XmlSolverFactory( "...SolverConfig.xml");Solver solver = factory.buildSolver();solver.setPlanningProblem(cloudBalance);solver.solve();cloudBalance = (CloudBalance) solver.getBestSolution(); 62
  • 63. Optimization algorithms
  • 64. Brute Force 64
  • 65. Brute Force config<solver> ... <bruteForce /></solver> 65
  • 66. Brute force scalability 9 processes = 1.5 seconds * 100 6 processes = 15 ms 66
  • 67. Brute force scalability 12 processes = 17 minutes * 680 9 processes = 1.5 seconds 6 processes = 15 ms * 100 67
  • 68. Plan 1200 processeswith brute force? 68
  • 69. First Fit 69
  • 70. First Fit config<solver> ... <constructionHeuristic> <constructionHeuristicType>FIRST_FIT</> </constructionHeuristic></solver> 70
  • 71. First Fit scalability 71
  • 72. First Fit resultsCPU: OKRAM: OKNetwork: OKCost active servers: shown 72
  • 73. First Fit Decreasing 73
  • 74. First Fit Decreasing config<solver> ... <constructionHeuristic> <constructionHeuristicType>FIRST_FIT_DECREASING</> </constructionHeuristic></solver> 74
  • 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. First Fit Decreasing scalability 76
  • 77. First Fit Decreasing results 77
  • 78. Local Search 78
  • 79. Local Search comes after Construction Heuristics<solver> ... <constructionHeuristic> <constructionHeuristicType>FIRST_FIT_DECREASING</> </constructionHeuristic> <localSearch> ... <localSearch></solver> 79
  • 80. Local Search needs to be terminated<solver> ... <termination> <maximumMinutesSpend>20</maximumMinutesSpend> </termination> ...</solver> 80
  • 81. Selecting moves<localSearch> <selector> <selector> <moveFactoryClass>GenericChangeMoveFactory</> </selector> <selector> <moveFactoryClass>GenericSwapMoveFactory</> </selector> </selector> ... tabu search, simulated annealing or ...</localSearch> 81
  • 82. Hill climbing Select the best improving move Gets stuck in local optima ● Not good 82
  • 83. Hill climbing<localSearch> <selector>...</selector> <forager> <!-- Untweaked standard value --> <minimalAcceptedSelection>1000</> </forager></localSearch> 83
  • 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. Tabu Search<localSearch> <selector>...</selector> <acceptor> <!-- Untweaked standard value --> <propertyTabuSize>7</propertyTabuSize> </acceptor> <forager> <!-- Untweaked standard value --> <minimalAcceptedSelection>1000</> </forager></localSearch> 85
  • 86. Simulated annealing Randomly select moves ● Prefer improving moves ● Especially as time goes by Does not get stuck in local optima 86
  • 87. Simulated Annealing<localSearch> <selector>...</selector> <acceptor> <!-- Tweaked value --> <simulatedAnnealingStartingTemperature> 0hard/400soft</> </acceptor> <forager> <!-- Untweaked standard value --> <minimalAcceptedSelection>4</> </forager></localSearch> 87
  • 88. Local Search results 88
  • 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. Organizations rarely optimize planning problems. 90
  • 91. http://www.flickr.com/photos/techbirmingham/345897594/Organizations waste resources. 91
  • 92. Real-time planning
  • 93. Real-time paradox First Fit Decreasing 1200 processes = 8 seconds Time allowed: 100ms after last change 93
  • 94. Demo Real-time planningCloudBalance example 94
  • 95. DemoReal-time planning TSP example 95
  • 96. Summary
  • 97. Summary Drools Planner optimizes planning Adding constraints is easy and scalable Switching/combining algorithms is easy 97
  • 98. Try an example! 98
  • 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