2012 02-04fosdem2012-droolsplanner-120207031949-phpapp01

Embed Size (px)

Citation preview

  • 1. Drools Planner:Planning optimization by exampleGeoffrey 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 preferences5 6. Hospital nurse rostering Assign each Shift To Nurse Constraintshttp://www.flickr.com/photos/glenpooh/709704564/ Employmentcontract Free timepreferences Skills6 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 cost9 10. Bin packing in the cloud Assign each Process To Server Constraints Hardwarehttp://www.flickr.com/photos/torkildr/3462607995/requirements Minimize server cost10 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 again18 19. Try FFD again19 20. Try FFD again20 21. Try FFD again21 22. FFD failure22 23. NP complete23 24. NP complete No silver bullet known Holy grail of computer science P == NP Probably does notexist P != NP Root problem ofhttp://www.flickr.com/photos/annguyenphotography/3267723713/all planningproblems24 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 optimizeplanning problems.33 34. Reuse optimization algorithms? 34 35. Open source (ASL 2.0) Regular releases Reference manual Examples35 36. Given 2 solutions, which one is better?36 37. Each Solution has 1 Score37 38. Each Solution has 1 Score38 39. Better score => better solution39 40. Better score => better solution40 41. Best score => best solution41 42. Best score => best solution42 43. DemoDrools 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. Process47 48. Process48 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 CloudBalanceimplements Solution {private List serverList;public List getServerList() {return serverList;}...}52 53. Solution CloudBalance:planning entitiespublic class CloudBalanceimplements Solution {...private List processList;@PlanningEntityCollectionPropertypublic List getProcessList() {return processList;}...}53 54. Solution CloudBalance:getProblemFactspublic class CloudBalanceimplements Solution {...// Used in score constraintspublic Collection getProblemFacts() {List facts = new ArrayList();facts.addAll(serverList);return facts;}...}54 55. Solution CloudBalance:scorepublic class CloudBalance implements Solution {...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 ofscore57 58. Soft constraint:server costrule "serverCost" when// there is a server$s : Server($c : cost)// there is a processes on that serverexists Process(server == $s) then// lower soft score by $cinsertLogical(new IntConstraintOccurrence("serverCost", ConstraintType.NEGATIVE_SOFT,$c,$s));end58 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)end59 60. Solving it 61. Solver configuration by XML...CloudBalance...Process>...ScoreRules.drlHARD_AND_SOFT> 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 Force64 65. Brute Force config... 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 seconds6 processes = 15 ms * 100 67 68. Plan 1200 processeswith brute force?68 69. First Fit69 70. First Fit config...FIRST_FIT> 70 71. First Fit scalability71 72. First Fit resultsCPU: OKRAM: OKNetwork: OKCost active servers: shown 72 73. First Fit Decreasing 73 74. First Fit Decreasing config...FIRST_FIT_DECREASING>74 75. DifficultyComparatorpublic class ProcessDifficultyComparatorimplements Comparator {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 afterConstruction Heuristics...FIRST_FIT_DECREASING> ...79 80. Local Search needs to beterminated...20... 80 81. Selecting movesGenericChangeMoveFactory> GenericSwapMoveFactory> ... tabu search, simulated annealing or ... 81 82. Hill climbing Select the best improving move Gets stuck in local optima Not good 82 83. Hill climbing...1000> 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 optima84 85. Tabu Search...71000>85 86. Simulated annealing Randomly select moves Prefer improving moves Especially as time goes by Does not get stuck in local optima86 87. Simulated Annealing... 0hard/400soft>4> 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 optimizeplanning 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 change93 94. DemoReal-time planningCloudBalance example 94 95. DemoReal-time planningTSP example 95 96. Summary 97. Summary Drools Planner optimizes planning Adding constraints is easy and scalable Switching/combining algorithms is easy97 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