Drools Cylande Chtijug 2010

986 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
986
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
27
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Drools Cylande Chtijug 2010

  1. 1. Ch’ti JUG Jboss Drools & Drools Planner 21 janvier 2010 Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  2. 2. Ch’ti JUG
  3. 3. Ch’ti JUG ● Editeur de logiciels exclusivement dédiés aux enseignes du Retail ● Création en 1986 ● 35 M€ de CA en 2008 (+20 %/an en moyenne depuis 5 ans) ● 34 % à l’International ● 5 sites en France dont le siège à Roubaix. (Paris, Belfort, Antibes, Vannes) ● 5 filiales hors hexagone : Shanghai, Portugal, Espagne, Tunisie, Pologne en cours ● Une expérience éprouvée dans 60 pays
  4. 4. Ch’ti JUG Effectifs : 430 collaborateurs dans le monde, 360 en France, 300 ressources basées à Roubaix 10 0 E pe Me rs x rts tie 90 D cte de P t ire ur roje 80 C f d P t Mé r he e roje tie 70 C fd P t he e roje 60 T chnique e s F a ur e @le rning orm te t a 50 D cte de P ire urs roduits 40 30 D ve é loppeurs 20 R ce e Q lifica e tte t ua tion 10 H L ot ine 0 P pa te e ré ra urs t E c ffe tifs déploie e m nts 4
  5. 5. Ch’ti JUG Storeland pilote l’ensemble de votre supply chain étendue
  6. 6. Ch’ti JUG CYLANDE partenaire de la FFJ Le judo véhicule des valeurs Cylande a accompagné l’équipe de France de Judo à Pékin
  7. 7. Ch’ti JUG Une croissance résolument tournée vers l’international Lauréat du Prix PME France CHINE ACFCI / CCIFC
  8. 8. Mark Proctor Project Lead ● The SkyNet funding bill is passed. ● The system goes online on August 4th, 1997. ● Human decisions are removed from strategic defense. ● SkyNet begins to learn at a geometric rate. ● It becomes self-aware at 2:14am Eastern time, August 29th ● In a panic, they try to pull the plug. ● And, Skynet fights back
  9. 9. Introduction Drools Drools Drools Drools Expert Flow Fusion Guvnor Business Logic integration Platform 9
  10. 10. Drools Expert
  11. 11. Learn by Example
  12. 12. Classes C a s h f lo w A c c o u n t D a t e d a t e lo n g a c c o u n t N o d o u b l e a m o u n t d o u b le b a la n c e in t t y p e lo n g a c c o u n t N o A c c o u n t in g P e r io d D a t e s t a r t D a t e e n d 12
  13. 13. Creating Views with Triggers date amount type accountNo AccountingPeriod 12-Jan-07 100 CREDIT 1 start end 2-Feb-07 200 DEBIT 1 01-Jan-07 31-Mar-07 18-May-07 50 CREDIT 1 9-Mar-07 75 CREDIT 1 Account accountNo balance 1 0 increase balance for AccountPeriod Credits decrease balance for AccountPeriod Debits select * from Account acc, select * from Account acc, Cashflow cf, AccountPeriod ap Cashflow cf, AccountPeriod ap where acc.accountNo == cf.accountNo where acc.accountNo == cf.accountNo and and cf.type == CREDIT cf.type == DEBIT cf.date >= ap.start and cf.date >= ap.start and trigger : acc.balance += cf.date <= ap.end cf.date <= ap.end trigger : acc.balance -= cf.amount cf.amount CashFlow CashFlow date amount type date amount type 12-Jan-07 100 CREDIT 2-Feb-07 200 DEBIT 9-Mar-07 75 CREDIT Account accountNo balance 1 -25 13
  14. 14. What is a Rule Quotes on Rule names salience <int> are optional if the rule agenda-group <string> name has no spaces. no-loop <boolean> auto-focus <boolean> duration <long> • rule “<name>” .... <attribute> <value> when <LHS> then RHS can be any valid <RHS> java. Or MVEL. Other end languages could be added. 14
  15. 15. Imperative vs Declarative Methods that must specific passing of be called directly instances • public void helloMark(Person person) { Rules can never if ( person.getName().equals( “mark” ) { be called directly System.out.println( “Hello Mark” ); Specific instances } cannot be passed. } LHS • rule “Hello Mark” when Person( name == “mark” ) then System.out.println( “Hello Mark” ); RHS end 15
  16. 16. What is a Pattern P a t t e r n O b j e c t T y p Fe i e l d C o n s t r a i n t F i e l d N a R m e es t r i c t i o n E v a l Vu a lt uo er S h o( t ew m e r p = e “= r ha )ot u t 16
  17. 17. Bringing it Together select * from Account acc, Cashflow cf, AccountPeriod ap where acc.accountNo == cf.accountNo and cf.type == CREDIT Pattern triggercf.date >= += : acc.balance Pattern Binding ap.start and cf.amount cf.date <= ap.end field Binding rule “increase balance for AccountPeriod Credits” when Variable Restriction ap : AccountPeriod() acc : Account( $accountNo : accountNo ) Literal Restriction CashFlow( type == CREDIT, accountNo == $accountNo, date >= ap.start && <= ap.end, Multri Restriction - $ammount : ammount ) Restriction Variable then acc.balance += $amount; field Binding end Consequence (RHS) 17
  18. 18. Rules as a “ view” date amount type accountNo AccountingPeriod 12-Jan-07 100 CREDIT 1 start end 2-Feb-07 200 DEBIT 1 01-Jan-07 31-Mar-07 18-May-07 50 CREDIT 1 9-Mar-07 75 CREDIT 1 Account accountNo balance 1 0 rule “increase balance for AccountPeriodrule “decrease balance for AccountPeriod Credits” Debits” when when ap : AccountPeriod() ap : AccountPeriod() acc : Account( $accountNo : accountNo ) acc : Account( $accountNo : accountNo ) CashFlow( type == CREDIT, CashFlow( type == DEBIT, accountNo == $accountNo, accountNo == $accountNo, date >= ap.start && <= date >= ap.start && <= ap.end, ap.end, CashFlow $ammount : ammount ) CashFlow $ammount : ammount ) then date amount type then date amount type acc.balance += $amount; CREDIT 12-Jan-07 100 acc.balance -= $amount;200 DEBIT 2-Feb-07 end 9-Mar-07 75 CREDIT end Account accountNo balance 1 -25 18
  19. 19. Patterns in more details CashFlow( type == “credit” ) $ap : AccountPeriod() CashFlow( date >= $ap.start ) $ap : AccountPeriod() CashFlow( date >= $ap.start && <= $ap.end ) $ap : AccountPeriod() CashFlow( type == “credit”, date >= $ap.start && <= $ap.end ) 19
  20. 20. More Pattern Examples Person( $age : age ) Person( age == ( $age + 1 ) ) Person( $age : age ) Person( eval( age == $age + 1 ) ) Person( $age1 : age ) Person( $age2 : age ) eval( $age2 == $age1 + 1 ) 20
  21. 21. More Pattern Examples Person(age > 30 && < 40 || hair == “black”) Person(age > 30 && < 40 || hair in (“black”, “brown”) ) Person( (age > 30 && < 40 && hair == “black”) || (age > 50 && hair == “grey”) ) Person(pets contain $rover ) Person(pets[’rover’].type == “dog”) Person(pets[0].type == “dog”) 21
  22. 22. What is a Production Rule System Codification of the business Repository of knowledge inserted Java instances Inference Engine Production Pattern Working Memory Matcher Memory (rules) (facts) Agenda insert Rules can update change retract on the fly 22
  23. 23. Production Rule System Approximated by SQL and Views T a b le s A c c o u nAt c c o u n t i n g C P a e s r h i o f l do w O b je c t T A y c p c eo su nAt c c o u n t i n g C P a e s r h i o f l do w V ie w s v i e1 w v i e2 w V ie w m a in v ie w R u le s r u1 le r u2 le a g e n d a a g e n d a 23
  24. 24. Conflict Resolution with Salience Salience rule “Print blance for AccountPeriod” salience -50 when ap : AccountPeriod() acc : Account( ) then System.out.println( acc.accountNo + “ : “ acc.balance ); end Agenda 1 increase balance 2 decrease balance arbitrary 3 increase balance 4 print balance 24
  25. 25. RuleFlow rule “increase balance for AccountPeriod Credits” ruleflow-group “calculation” when ap : AccountPeriod() acc : Account( $accountNo : accountNo ) CashFlow( type == CREDIT, accountNo == $accountNo, date >= ap.start && <= ap.end, $ammount : ammount ) then acc.balance += $amount; end ruleflow-group rule “Print blance for AccountPeriod” ruleflow-group “report” when ap : AccountPeriod() acc : Account( ) then System.out.println( acc.accountNo + “ : “ acc.balance ); end 25
  26. 26. Two Phase System Determine possible rules to fire Agenda Evaluation Working Memory Action Rule insert modify Found Select Fire Rule Rule to Fire retract No Rule Found exit 26
  27. 27. Conditional Elements
  28. 28. From CE for Expressions not Bus( color = “red” ) exists Bus( color = “red” ) forall ( $bus : Bus( color == “red” ) ) forall ( $bus : Bus( floors == 2 ) Bus( this == $bus, color == “red” ) ) 28
  29. 29. From CE for Expressions
  30. 30. From CE for Expressions Using 'from' to reason over the nested list rule “Find all the pets for a given owner” when $owner : Person( name == “mark” ) Pet( name == “rover” ) from $owner.pets 30
  31. 31. From CE for Expressions 'from' can work on any expression, not just a nested field on a bound variable. rule “Find People for given zip code” when $zipCode : ZipCode() Person( ) from $hbn.getNamedQuery(“Find People”) .setParameters( [ “zipCode” : $zipCode ] ) Hibernate session .list() 31
  32. 32. Collect CE
  33. 33. Collect CE rule "accumulate" when $list : List( intValue > 100 ) from collect( Bus( color == "red" ) ) then print "red buses “ + $list; end 33
  34. 34. Accumulate CE
  35. 35. Accumulate CE rule "accumulate" when $sum : Number( intValue > 100 ) from accumulate( Bus( color == "red", $t : takings ) init( sum = 0 ), action( sum += $t ), result( sum ) ) then print "sum is “ + $sum; end 35
  36. 36. Accumulate CE rule "accumulate" when $sum : Number( intValue > 100 ) from accumulate( Bus( color == "red", $t : takings ) sum( $t ) ) then print "sum is “ + $sum; end 36
  37. 37. rule "collect" Accumulate CE when $zipCode : ZipCode() $sum : Number( intValue > 100 ) from accumulate( Bus( color == "red", $t : takings ) from $hbn.getNamedQuery(“ Find Buses” ) .setParameters( [ “ zipCode” : $zipCode ] ) .list(), sum( $t ) ) then print "sum is “ + $sum; end 37
  38. 38. Timers Calendars
  39. 39. Timers rule “name” timer 1m30s when $l : Light( status == “on” ) then SendEmail( “turn the light off” ) rule “name” timer (int: 0 1m30) when $l : Light( status == “on” ) then SendEmail( “turn the light off” ) 39
  40. 40. Timers Field Name Mandatory? Allowed Values Allowed Special Characters Seconds YES 0-59 ,-*/ Minutes YES 0-59 ,-*/ Hours YES 0-23 ,-*/ Day of month YES 1-31 ,-*?/LW Month YES 1-12 or JAN-DEC ,-*/ Day of week YES 1-7 or SUN-SAT ,-*?/L# Year NO empty, 1970-2099 ,-*/ rule “name” timer ( cron: 0 0/15 * * * * ) when $l : Light( status == “on” ) then sendEmail( “turn the light off” ) 40
  41. 41. Calendars rule "weekdays are high priority" calendars "weekday" timer (int:0 1h) when Alarm() then send( "priority high - we have an alarm” ); end rule "weekend are low priority" calendars "weekend" timer (int:0 4h) when Alarm() then send( "priority low - we have an alarm” ); end 41
  42. 42. Truth Maintenance Inference
  43. 43. TMS and Inference rule "Issue Child Bus Pass" Couples the logic when $p : Person( age < 16 ) then insert(new ChildBusPass( $p ) ); end What happens when the Child rule "Issue Adult Bus Pass" stops being 16? when $p : Person( age >= 16 ) then insert(new AdultBusPass( $p ) ); end 43
  44. 44. TMS and Inference  Bad ● Monolithic ● Leaky ● Brittle integrity - manual maintenance 44
  45. 45. TMS and Inference  A rule “ logically” inserts an object  When the rule is no longer true, the object is retracted. when de-couples the logic $p : Person( age < 16 ) then logicalInsert( new IsChild( $p ) ) end Maintains the truth by when automatically retracting $p : Person( age >= 16 ) then logicalInsert( new IsAdult( $p ) ) end 45
  46. 46. TMS and Inference rule "Issue Child Bus Pass" when $p : Person( ) IsChild( person =$p ) then logicalInsert(new ChildBusPass( $p ) ); end rule "Issue Adult Bus Pass" The truth maintenance cascades when $p : Person( age >= 16 ) IsAdult( person =$p ) then logicalInsert(new AdultBusPass( $p ) ); end 46
  47. 47. TMS and Inference rule "Issue Child Bus Pass" when $p : Person( ) not( ChildBusPass( person == $p ) ) then requestChildBusPass( $p ); The truth maintenance cascades End 47
  48. 48. TMS and Inference  Good ● De-couple knowledge responsibilities ● Encapsulate knowledge ● Provide semantic abstractions for those encapsulation ● Integrity robustness – truth maintenance 48
  49. 49. Tooling
  50. 50. Guided Editor 50
  51. 51. Interactive Debugging 51
  52. 52. Decision Tables 52
  53. 53. DSLs 53
  54. 54. DSLs 54
  55. 55. Rule Flow 55
  56. 56. Drools Fusion
  57. 57. Scalability Rule engines do not scale for CEP. They have a single point of insertion and are single threaded, CEP has concurrent streams of events. session.insert( event ) $c : Custumer( type == “VIP” ) ; BuyOrderEvent( customer == $c ) Single Point of entry Patterns, evaluate facts sequentially in a single thread. 57
  58. 58. Scalability So lets allow multiple named entry points for those streams EntryPoint entryPoint = session.getEntryPoint( “Home Broker Stream” ); entryPoint.insert( event ) ; So now we can When not specified insert different uses the “default” streams entry-point concurrently $c : Custumer( type == “VIP ) BuyOrderEvent( customer == $c ) from entry-point “Home Broker Stream” Patterns can now optional specify their entry-point. 58
  59. 59. Automatic Life-Cycle Management All Fact life-cycles must be managed by the user, so retractions are manual. declare Just use the declare statement StockTick to declare a type as an event @role( event ) and it will be retracted when it is end no longer needed declare StockTick @role( event ) @timestamp( timestampAtt The declare statement can also r) specify an internal model, that external objects/xml/csv map on companySymbol : String to. We support Smooks and stockPrice : double JAXB timestampAttr : long end 59
  60. 60. Operators Rule engines do not have rich enough set of temporal comparison operators BackAckEvent must occur between 1s and 10s 'after' $c : Custumer( type == “VIP ) BuyOrderEvent $oe : BuyOrderEvent( customer == $c ) from entry-point “Home Broker Stream” BuyAckEvent( relatedEvent == $oe.id, this after[1s, 10s] $oe ) from entry-point “Stock Trader Stream” The Full set of Operators are supported ● coincides ● overlaps ● starts ● before ● overlappedby ● startedby ● after ● during ● finishes ● meets ● includes ● finishedby ● metby 60
  61. 61. Operators 61
  62. 62. Operators $c : Custumer( type == “VIP ) $oe : BuyOrderEvent( customer == $c ) from entry-point “Home Broker Stream” not BuyAckEvent( relatedEvent == $oe.id, this after[1s, 10s] $oe ) from entry-point “Stock Trader Stream” Existing Drools 'not' Conditional Elements can be used to detect non-occurrence of events 62
  63. 63. Sliding time windows Rule engines react to events happening now, there is no temporal understanding of changes over time. StockTicker( symbol == “RHAT” ) over window:time( 5s ) 5s StockTicker( symbol == “RHAT” ) over window:length( 1000 ) 1000 tickers That isn't much without the ability to deal with aggregations, rules engines suck. 63
  64. 64. Aggregations Rule Engines do not deal with Over 5 seconds aggregations $n : Number( intValue > 100 ) from accumulate( $s : StockTicker( symbol == “RHAT” ) over window:time( 5s ), average( $s.price ) ) Aggregate ticker price The pattern 'Number' for RHAT over last 5 reasons 'from' the seconds accumulate result $n : accumulate( $s : StockTicker( symbol == “RHAT” ) over window:time( 5s ), average( $s.price ) > 100 ) We can use some sugar to reduce verbosity 64
  65. 65. Drools Flow
  66. 66. Drools Flow A workflow engine combining processes and Integration rules ● From loose coupling (decision services) ● To advance integration (process rules)  Unification ● Rules and processes are different types of business knowledge assets ● Infrastructure ● Timers/Schedulers ● Testing ● Communication/Services ● Tooling ● IDE ● repository, management ● Auditing 66
  67. 67. Truth Maintenance Inference
  68. 68. generic Rules and processes Decision ? Services SCOPE Process specific Rules tightly coupled COUPLING loosely coupled 68
  69. 69. Business Logic Lifecycle 69
  70. 70. Example 70
  71. 71. RuleFlowGroup Workflow can control my rules? 71
  72. 72. RuleFlowGroup Rule Flow Group 72
  73. 73. RuleFlowGroup 73
  74. 74. Constraints Java code constraint 74
  75. 75. Constraints Rules can control my workflow? LHS “when” Rule Constraint 75
  76. 76. Example  Business decisions are externalized using a decision service rule rule Decision1 when rule Decision1 Decision1 // when when conditions // then // conditions conditions // actions then end // actions then end // actions 76 end
  77. 77. Example  What if there is a lot of business logic like this? rule Decision1 rule Decision1 when rule Decision1 rule Decision1 when rule Decision1 rule Decision1 rule Decision1 when rule Decision1 // when rule Decision1 rule Decision1 // when rule Decision1 when when rule Decision1 rule Decision1 rule Decision1 // when conditions when// when rule Decision1 conditions // when // rule Decision1 when when // when rule rule then // conditions rule then // rule conditions conditions rule conditions // // when // when // conditions // Decision1 conditions then //// actions then conditions conditions //// actions then then Decision1 conditions conditions conditions Decision1 conditions Decision1 Decision1 then when end then then // actions end // actions conditions then when // actions then thenthen conditions when when // actions when // actions then end actions // actions end actions // actions // actions end // // actions then // // // then // end actions end end actions end // end // // actions end end // conditions conditions // end end end endconditions conditions conditions then then then then then // // // // // actions actions actions actions actions end end end 77 end end
  78. 78. Flow of Control Process Engine Rules Engine 78
  79. 79. Inversion of Control Process Engine Agenda Rules Engine 79
  80. 80. Self monitoring and adaptive declare ProcessStartedEvent @role( event ) end rule "Number of process instances above threshold" when Number( nbProcesses : intValue > 1000 ) from accumulate( e: ProcessStartedEvent( processInstance.processId == "com.sample.order.OrderProcess" ) over window:size(1h), count(e) ) then System.err.println( "WARNING: Nb of order processes in the last hour > 1000: " + nbProcesses ); end 80
  81. 81. Domain Specific Processes 81
  82. 82. Domain Specific Processes 82
  83. 83. Integrated debug and audit 83
  84. 84. Unified API Definitions Runtime Language
  85. 85. Definitions jBPM File file = new File (“.....”); // file to XML process definition ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( IoUtils.FileToString( file ) ); ProcessInstance processInstance = Jess new ProcessInstance(processDefinition); Rete engine = new Rete(); FileReader file = new FileReader("myfile.clp"); Jesp parser = new Jesp(file, engine); Esper parser.parse(false); EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider(); EPStatement countStmt = admin.createEPL( "...." ); countStmt.start(); 85
  86. 86. Definitions Drools Flow KnowledegBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBulider(); kbuilder.addResource( ResourceFactory.newClassPathResource( “myflow.drf”, ResourceType.DRF ); If ( kbuilder.hasErrors() ) { log.error( kbuilder.hasErrors().toString() ); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages( kbase.getKnowledgePackages() ); 86
  87. 87. Definitions Drools Expert KnowledegBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBulider(); kbuilder.addResource( ResourceFactory.newClassPathResource( “myrules.drl”, ResourceType.DRL ); If ( kbuilder.hasErrors() ) { log.error( kbuilder.hasErrors().toString() ); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages( kbase.getKnowledgePackages() ); 87
  88. 88. Drools Integration Deployment Descriptors <change-set> <add> <resource source='classpath:myapp/data/myflow.drf' type='DRF' /> <resource source='http:myapp/data/myrules.drl' type='DRL' /> <resource source='classpath:data/IntegrationExampleTest.xls' type="DTABLE"> <decisiontable-conf input-type="XLS" worksheet-name="Tables_2" /> </resource> <add> </change-set> KnowledegBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBulider(); kbuilder.addResource( ResourceFactory.newFileResource( “changeset.xml”, ResourceType.ChangeSet ); 88
  89. 89. Unified Event Model
  90. 90. Ch’ti JUG
  91. 91. Mixins Interface Markers
  92. 92. Stateful Knowledge Session 92
  93. 93. Knowledge Runtime 93
  94. 94. Coming in 5.1  BPMN2 (80% of Workflow)  Integration ● OSGi ready ● Spring ● Camel  Seamless Remoting  Simulation/Testing  New Rete Algorithm “ true modify” 94
  95. 95. Spring + camel from("direct:test-with-session"). to("drools:sm/ksession1?dataFormat=drools- xstream"); 95
  96. 96. Questions? • Dave Bowman: All right, HAL; I'll go in through the emergency airlock. • HAL: Without your space helmet, Dave, you're going to find that rather difficult. • Dave Bowman: HAL, I won't argue with you anymore! Open the doors! • HAL: Dave, this conversation can serve no purpose anymore. Joshua: Greetings, Professor Falken. Goodbye. Stephen Falken: Hello, Joshua. Joshua: A strange game. The only winning move is not to play. How about a nice game of chess? 96
  97. 97. Ch’ti JUG Q&A Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  98. 98. Ch’ti JUG Automated planning with Drools Planner Geoffrey De Smet Drools Planner lead "Do more with less." Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  99. 99. Ch’ti JUG Agenda  Use cases of automated planning • N queens • Bin packaging • Employee shift rostering • Examination timetabling  Find the best solution • With Drools Planner  Calculate the score of a solution • With Drools Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  100. 100. Ch’ti JUG Agenda  Use cases of automated planning • N queens • Bin packaging • Employee shift rostering • Examination timetabling  Find the best solution • With Drools Planner  Calculate the score of a solution • With Drools Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  101. 101. Ch’ti JUG N Queens: use case  Place n queens on a n-sized chess board  No 2 queens can attack each other Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  102. 102. Ch’ti JUG N queens: partially solved  Score -1 for every 2 queens that can attack each other Score = -2 Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  103. 103. Ch’ti JUG N queens: an optimal solution Score = 0 Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  104. 104. Ch’ti JUG N queens: demo  Not optimized! • Hello world example  Not a real planning problem • I can make an optimal solution for any n queens without a computer • See Wikipedia Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  105. 105. Ch’ti JUG Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  106. 106. Ch’ti JUG NP complete  Yellow item goes in first (or last) • Why? • Not the largest size • Not the largest side • So why?  NP complete • A given solution can be verified fast • No efficient way to find a solution • Is there even a solution? Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  107. 107. Ch’ti JUG Real world bin packaging  Not just 5 items • 1000+ items  Not just 1 container • 100+ containers • Different container types  More constraints... • Distribute weight evenly • Not all fireworks in the same container • ... Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  108. 108. Ch’ti JUG Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  109. 109. Ch’ti JUG Hard constraints  Hard constraints must be fulfilled  For example: • Ensure continuous service • At least 1 emergency nurse at any given time • Labor laws • Every 24 hours: at least 11 hours rest • Every 7 days: at least 35 hours rest • No shifts during approved vacation Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  110. 110. Ch’ti JUG Soft constraints  Soft constraints should be fulfilled as much as possible • Only after the hard constraints are fulfilled  Each soft constraint is weighted  For example: • Fair night work assignment: weight 5 • Forward rotation: weight 10 • Nurse preferences: weight 1 • Ann dislikes Saturday night shifts • Beth dislikes Wednesday afternoon shifts Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  111. 111. Ch’ti JUG Hard and soft score  Solution A B C  Hard constraints • 11 hours rest 1 0 0  Soft constraints • Fair night work assignment 0 1000 1 • Weight 5 • Nurse preferences 0 0 4000 • Weight 1  Total score -1H/0S 0H/-5000S 0H/-4005S •A<B<C • C is the best solution Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  112. 112. Ch’ti JUG Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  113. 113. Ch’ti JUG Hard constraints  Exam conflict: 2 exams that share students should not occur in the same period.  Room capacity: A room's seating capacity should suffice at all times.  Period duration: A period's duration should suffice for all of its exams.  Period related hard constraints should be fulfilled: • Coincidence: 2 exams should use the same period (but possibly another room). • Exclusion: 2 exams should not use the same period. • After: 1 exam should occur in a period after another exam's period.  Room related hard constraints should be fulfilled: • Exclusive: 1 exam should not have to share its room with any other exam. Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  114. 114. Ch’ti JUG Soft constraints  2 exams in a row.  2 exams in a day.  Period spread: 2 exams that share students should be a number of periods apart.  Mixed durations: 2 exams that share a room should not have different durations.  Front load: Large exams should be scheduled earlier in the schedule.  Period penalty: Some periods have a penalty when used.  Room penalty: Some rooms have a penalty when used. Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  115. 115. Ch’ti JUG Examination demo  International timetabling competition 2007 • Finished 4th (back then)  7 minutes • CPU depended  Real word test data  14 constraints • 7 hard constraints • 7 soft constraints Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  116. 116. Ch’ti JUG Other use cases  Vehicle routing • Freight routing  Scheduling • Course, meeting, conference scheduling • Appointment and resource scheduling • Sport scheduling  Storage organizing  Machine queue planning  ... Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  117. 117. Ch’ti JUG Why use Drools Planner?  Open source • ASL (business-friendly)  Maven-ready (JBoss repository)  Documentation • Reference manual • Examples  JBoss Drools community support • User mailing list, issue tracking, … • Blog & twitter (#droolsplanner) Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  118. 118. Ch’ti JUG Agenda  Use cases of automated planning • N queens • Bin packaging • Employee shift rostering • Examination timetabling  Find the best solution • With Drools Planner  Calculate the score of a solution • With Drools Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  119. 119. Ch’ti JUG Brute force for (periodOfExam1 : periodList) { exam1.setPeriod(periodOfExam1); for (roomOfExam1 : roomList) { exam1.setRoom(roomOfExam1); for (periodOfExam2 : periodList) { exam2.setPeriod(periodOfExam2); for (roomOfExam2 : roomList) { exam2.setRoom(roomOfExam2); ... for (periodOfExamN : periodList) { examN.setPeriod(periodOfExamN); for (roomOfExamN : roomList) { examN.setRoom(roomOfExamN); Score score = calculateScore(solution); cloneIfScoreIsBetter(solution, score); } } } }…} } Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  120. 120. Ch’ti JUG Needle in a haystack  How many possible solutions? • 1096 exams • 80 periods • 28 rooms  > habitants in Lille per km²? Source: wikipedia • 6 483 hab./km² Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  121. 121. Ch’ti JUG Needle in a haystack  How many possible solutions? • 1096 exams • 80 periods • 28 rooms  > humans? • 7.000.000.000 humans Source: NASA (wikipedia) Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  122. 122. Ch’ti JUG Needle in a haystack  How many possible solutions? • 1096 exams • 80 periods • 28 rooms  > minimum atoms in the observable universe? • 10^80 atoms Source: NASA and ESA (wikipedia) Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  123. 123. Ch’ti JUG Needle in a haystack  How many possible solutions? • 1096 exams • 80 periods • 28 rooms  > atoms in the universe if every atom is a universe of atoms? • (10^80)^80 = 10^6400 Source: NASA and ESA (wikipedia) Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  124. 124. Ch’ti JUG Do the math  1 exam • 80 periods and 28 rooms • 80 * 28 = 2240 ways to schedule 1 exam  2 exams • 2240 * 2240 = 5.017.600  3 exams • 2240 * 2240 * 2240 = 11.239.424.000  1096 exams • 2240 * 2240 * … * 2240 • 2240^1096 = a little over 10^3671 Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  125. 125. Ch’ti JUG A little over 10^3671  74443724674464882011383315953154621497427697455114051 31628826913469284310834499031050210214743407656244813 08524044280985532117872268184924364558999914849676314 19697684165817985739661390634926254859096857258977301 84010924994541828672670138943325039683048943713412274 82961472169559963615977772710171376837800461548701272 17758740223489170130893779085381647394360334935333289 36807838400221316123322575571991006706635467623766525 12406735523153767499024677368278799816044299431500884 24040897721698276067946148250230917492054728443158872 16505437393615765933295613677473087008125802551840549 23894808886159001642690353983482990003805674675524102 80857265893710574057117390411923324486282853392817922 61716873450760473970355208029926132045718675579835379 67203299588154666629888459837384660489020381221523812 26870228697167564520947170314014038670253281783219898 66839234979915835407169443312860837423115961300328664 8446078922185727592075724811 Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  126. 126. Ch’ti JUG A little over 10^3671  60481357724124718546251056304953581219520179741762152 21261550607694499282872000580072957918546796819172012 88523274131110715650043989565813921764252807306941995 04163032760429819447826040765201495454290825675151996 35531168668927010363569188258631683061394017239747010 85877081645821563181943787272983111911411368916826773 44586482492885259812532687126829097218925413324337881 04618254995718184937280503163787574781545179918774455 71368272048608567632308037489481707365407730778349040 96264467405007381183921101733071148798313412153048340 99815901166729699407017252645417836852601401021510814 95490674708263321685449253146293527632982628824370943 45239245616252628477471654331980909505146422698550082 08195099600705166755800356942782663732953126879621138 03354280700964987221060506159614496708252300794687287 84295862741344712584392063055735037820970817169256861 54420223798946020972887359043006100852387795351482973 307623581925846555002793841 Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  127. 127. Ch’ti JUG A little over 10^3671  41281947539904670755491533163612447621027075998378388 10074037250281891067383996002870594133962960635381998 37169373556801830583664641156130483672354172652266198 33074381986843858804462180500948095656353846489379837 93088308243838089365451116083129648680565986741315951 93654957707706822143338172833633019666638035983430262 03701966512564789421239279046238981003026684580307903 15153020620193795388869486770234724354626457650058047 46816166402399340231002187005109182016211164762492991 71924050393511639247398607555167937946055347704746052 68459331764255849320866378895400041597447191732266335 48555732700361980207696413126618655189183160162357390 48483478516838603814734161714922415899459081915010854 56952341588756767389366458777600000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000 Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  128. 128. Ch’ti JUG A little over 10^3671  00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000 Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  129. 129. Ch’ti JUG A little over 10^3671  00000000000000000000000000000000000000000000000 00000000000000000000000000000  The search space is big! • Compare with WWW size • 22 020 000 000 pages  Each possible solution • 1096+ exams scheduled into • 80 periods • 28 rooms • Still need to calculate the score Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  130. 130. Ch’ti JUG Throw hardware at it?  If 10^9 scores calculated per ms • Not possible today! • 31.579.200.000 ms in 1 year • < 10^11 ms in 1 year • 10^9 * 10^11 scores per year • = 10^20 scores per year  How many years? 10^3671 / 10^20 • = 10^3651 years  CPU 1000 times faster • It becomes 10^3648 years Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  131. 131. Ch’ti JUG A dose of reality  Find the optimal solution? • Of a real world planning problem?  Not in our lifetimes!  Who cares? • Beat the human planner(s) (=easy) • Spend less resources • Save more money • Save the environment • Make more people happy • Never ending competition Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  132. 132. Ch’ti JUG Smarter brute force?  Eliminate subtrees • Branch and bound • Still too many for loops • Still takes forever for (periodOfExam2 : periodList) { exam2.setPeriod(periodOfExam2); if (exam1.shareStudentWith(exam2) && periodOfExam1.equals(periodOfExam2)) { continue; // bug: best solution might break a hard constraint } ... Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  133. 133. Ch’ti JUG Imperfect algorithms (mimic a human)  Deterministic • First in, first assigned, never changed • Easy to implement • Drools Planner score support • Fixed time (for example 18 seconds)  Metaheuristic • Move things around • Start from result of deterministic algorithm • Drools Planner implementations • More time = better score Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  134. 134. Ch’ti JUG Deterministic: N queens  Demo  Not feasible • Not optimal  Good initialization • Jump 10 meter into the pool Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  135. 135. Ch’ti JUG Deterministic: examination List<Exam> sortedExamList = sortExamsOnDifficulty(examList); for (exam : sortedExamList) { // Determine best remaining spot Score bestScoreOfExam = - INFINITY; for (period : periodList) { exam.setPeriod(period); for (room : roomList) { exam.setRoom(room); Score score = calculateScore(solution); if (score > bestScoreOfExam) { bestScoreOfExam = score; ... store bestPeriod, bestRoom } } } … assign exam to bestPeriod, bestRoom } Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  136. 136. Ch’ti JUG Metaheuristic algorithms  Local search: 1st , 2nd , 3rd and 4th in ITC 2007 • Simple local search (Hill climbing) • Tabu search • Local search ++ • Simulated annealing • Great deluge • ...  Genetic algorithms: 5th in ITC 2007  Ant colony optimization  ... Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  137. 137. Ch’ti JUG Move things around  Move = from solution A to solution B • Change the row of 1 queen • Give 2 queens each others rows • ... Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  138. 138. Ch’ti JUG All moves from one solution  Number of moves < number of solutions • N queens • n*n < n^n • 4 queens • 16 < 256 • 8 queens • 64 < 16777216 • 64 queens • 4096 < 10^116 Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  139. 139. Ch’ti JUG Metaheuristic: local search Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  140. 140. Ch’ti JUG Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  141. 141. Ch’ti JUG Local optima  1) Deterministic StartingSolutionInitializer  2) Simple local search  3) Stuck in local optimum! Source: Wikipedia Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  142. 142. Ch’ti JUG Tabu search = local search++  Solution tabu (high tabu size) • Been there, no need to go there again  Move tabu (low tabu size) • Done that recently, no need to do that again  Property tabu (low tabu size) • Changed that recently, no need to change that again Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  143. 143. Ch’ti JUG Drool planner configuration <selector> <selector> <moveFactoryClass>...PeriodChangeMoveFactory</...> <relativeSelection>0.002</relativeSelection> </selector> ... <selector> <moveFactoryClass>...ExamSwitchMoveFactory</...> <relativeSelection>0.002</relativeSelection> </selector> </selector> <accepter> <completeSolutionTabuSize>1000</completeSolutionTabuSize> <completeMoveTabuSize>7</completeMoveTabuSize> </accepter> <forager> <foragerType>MAX_SCORE_OF_ALL</foragerType> </forager> Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  144. 144. Ch’ti JUG Termination  Synchronous (configured) • Max timeMillis/seconds/minutes/hours spend • Score attained • Max step count • Max unimproved step count  Asynchronous (from another thread) • planner.terminateEarly(); Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  145. 145. Ch’ti JUG Double time !=> double score Examination test data 7 Soft score Time (hours:minutes) Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  146. 146. Ch’ti JUG Benchmarker utility  Battle of different planner configurations • Different algorithms (tabu search, ...) • Different moves • Different settings  On multiple datasets  Results are ranked: • Best one wins  Coming soon: • Graph: best score over time Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  147. 147. Ch’ti JUG Agenda  Use cases of automated planning • N queens • Bin packaging • Employee shift rostering • Examination timetabling  Find the best solution • With Drools Planner  Calculate the score of a solution • With Drools Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  148. 148. Ch’ti JUG JAVA vs SQL vs DRL for (q1 : queenList) { for (q2 : queenList) { if (q1.getId() < q2.getId() && q1.getY() == q2.getY()) { ... } } } select * from Queen q1, Queen q2 where q1.id < q2.id and q1.y = q2.y; rule "multipleQueensHorizontal" when $q1 : Queen($id : id, $y : y); $q2 : Queen(id > $id, y == $y); Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  149. 149. Ch’ti JUG N queens: score rule rule "multipleQueensHorizontal" when $q1 : Queen($id : id, $y : y); $q2 : Queen(id > $id, y == $y); then insertLogical(new IntConstraintOccurrence( "multipleQueensHorizontal", ConstraintType.NEGATIVE_HARD, 1, $q1, $q2)); end Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  150. 150. Ch’ti JUG Score rule isolation rule "multipleQueensHorizontal" when $q1 : Queen($id : id, $y : y); $q2 : Queen(id > $id, y == $y); then ... end rule "multipleQueensAscendingDiagonal" when $q1 : Queen($id : id, $ascendingD : ascendingD); $q2 : Queen(id > $id, ascendingD == $ascendingD); then ... end rule "multipleQueensDescendingDiagonal" when $q1 : Queen($id : id, $descendingD : descendingD); $q2 : Queen(id > $id, descendingD == $descendingD); then ... end Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  151. 151. Ch’ti JUG Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  152. 152. Ch’ti JUG Examination: period spread  2 exams that share students should be a number of periods apart rule "periodSpread" when $iw : InstitutionalWeighting(periodSpreadPenality != 0); // For any 2 conflicting exams in the same period ... $topicConflict : TopicConflict($leftT : leftTopic, $rightT : rightTopic); $leftExam : Exam(topic == $leftT, $leftPeriod : period); $rightExam : Exam(topic == $rightT, $rightPeriod : period); // … which are in within the periodSpread eval(Math.abs($leftPeriod.getPeriodIndex() - $rightPeriod.getPeriodIndex()) < ($iw.getPeriodSpreadLength() + 1)); then insertLogical(new IntConstraintOccurrence(... NEGATIVE_SOFT, $topicConflict.getStudentSize() * $iw.getPeriodSpreadPenality(), $leftExam, $rightExam)); end Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  153. 153. Ch’ti JUG Summary  Drools Planner solves planning problems  Adding constraints is easy and scalable  Switching/combining algorithms is easy Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  154. 154. Ch’ti JUG Q&A  Home page • http://www.jboss.org/drools/drools-planner.html  Reference manual • http://www.jboss.org/drools/documentation.html  Blog • http://blog.athico.com/search/label/planner  Twitter • #droolsplanner Copyright © Ch'ti JUG – License Creative Commons 2.0 France 66
  155. 155. Ch’ti JUG Thanks for your attention!  Home page • http://www.jboss.org/drools/drools-planner.html  Reference manual • http://www.jboss.org/drools/documentation.html  Blog • http://blog.athico.com/search/label/planner  Twitter • #droolsplanner Copyright © Ch'ti JUG – License Creative Commons 2.0 France 67
  156. 156. Ch’ti JUG Licence  Les photos et logos appartiennent à leurs auteurs respectifs  Le contenu de la présentation est sous licence Creative Commons 2.0 France • Contrat Paternité • Pas d'Utilisation Commerciale • Partage des Conditions Initiales à l'Identique  http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Copyright © Ch'ti JUG – License Creative Commons 2.0 France
  157. 157. Ch’ti JUG Cocktail  Merci pour votre attention  Merci à Cylande pour son sponsoring Copyright © Ch'ti JUG – License Creative Commons 2.0 France

×