Ch’ti JUG




             Jboss Drools
                  &
            Drools Planner

            21 janvier 2010
Ch’ti JUG
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
Ch’ti JUG   Effectifs : 430 collaborateurs dans le monde, 360 en France, 300 ressources
            basées à Roubaix

               10
                0                                            Experts Metiers

                 90                                          Directeur de Projet

                 80                                          C de Projet Métier
                                                              hef
                 70
                                                             C de Projet
                                                              hef
                 60                                          Techniques
                                                             Formateur et @learning
                 50
                                                             Directeursde Produits
                 40
                 30                                          Développeurs

                 20                                          Recette et Qualification

                 10                                          H L
                                                              ot ine

                  0                                          Préparateurset
                                  Effectifs                  déploiem ents
                                                                                        4
Ch’ti JUG
      Storeland pilote l’ensemble de votre supply chain étendue
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
Ch’ti JUG

 Une croissance résolument tournée vers l’international
Lauréat du Prix PME France CHINE ACFCI / CCIFC
Ch’ti JUG


            Automated planning
            with Drools Planner
                Geoffrey De Smet
               Drools Planner lead


               "Do more with less."
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
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
Ch’ti JUG             N Queens: use case
             Place n queens on a n-sized chess board
             No 2 queens can attack each other
Ch’ti JUG        N queens: partially solved
             Score -1 for every 2 queens that can attack
              each other




                               Score = -2
Ch’ti JUG   N queens: an optimal solution




                       Score = 0
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
Ch’ti JUG
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?
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
              • ...
Ch’ti JUG
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
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
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
Ch’ti JUG
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.
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.
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
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
             ...
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)
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
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);

            } } } }…} }
Ch’ti JUG                   Needle in a haystack
                                 How many possible
                                  solutions?
                                  • 1096 exams
                                  • 80 periods
                                  • 28 rooms
                                 > habitants in Lille per km²?
                                  • 6 483 hab./km²
            Source: wikipedia
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)
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)
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)
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
Ch’ti JUG
                                  A little over
                                   10^3671
               744437246744648820113833159531546214974276974551140513162882691
                3469284310834499031050210214743407656244813085240442809855321
                17872268184924364558999914849676314196976841658179857396613906
                3492625485909685725897730184010924994541828672670138943325039
                683048943713412274829614721695599636159777727101713768378004615
                48701272177587402234891701308937790853816473943603349353332893
                68078384002213161233225755719910067066354676237665251240673552
                3153767499024677368278799816044299431500884240408977216982760
                67946148250230917492054728443158872165054373936157659332956136
                774730870081258025518405492389480888615900164269035398348299
                0003805674675524102808572658937105740571173904119233244862828
                53392817922617168734507604739703552080299261320457186755798353
                7967203299588154666629888459837384660489020381221523812268702
                28697167564520947170314014038670253281783219898668392349799158
                35407169443312860837423115961300328664844607892218572759207572
                4811
Ch’ti JUG
                                   A little over
                                    10^3671
               604813577241247185462510563049535812195201797417621522126155060
                76944992828720005800729579185467968191720128852327413111071565
                0043989565813921764252807306941995041630327604298194478260407
                65201495454290825675151996355311686689270103635691882586316830
                6139401723974701085877081645821563181943787272983111911411368916
                8267734458648249288525981253268712682909721892541332433788104
                618254995718184937280503163787574781545179918774455713682720486
                0856763230803748948170736540773077834904096264467405007381183
                921101733071148798313412153048340998159011667296994070172526454
                17836852601401021510814954906747082633216854492531462935276329
                8262882437094345239245616252628477471654331980909505146422698
                5500820819509960070516675580035694278266373295312687962113803
                354280700964987221060506159614496708252300794687287842958627
                4134471258439206305573503782097081716925686154420223798946020
                9728873590430061008523877953514829733076235819258465550027938
                41
Ch’ti JUG
                                  A little over
                                   10^3671
               41281947539904670755491533163612447621027075998378388100740372
                5028189106738399600287059413396296063538199837169373556801830
                5836646411561304836723541726522661983307438198684385880446218
                050094809565635384648937983793088308243838089365451116083129
                64868056598674131595193654957707706822143338172833633019666638
                035983430262037019665125647894212392790462389810030266845803
                0790315153020620193795388869486770234724354626457650058047468
                161664023993402310021870051091820162111647624929917192405039351
                1639247398607555167937946055347704746052684593317642558493208
                66378895400041597447191732266335485557327003619802076964131266
                186551891831601623573904848347851683860381473416171492241589945
                908191501085456952341588756767389366458777600000000000000000
                0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                00000
Ch’ti JUG
                                A little over
                                 10^3671
               0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                0000000000000000000000000000000000000000000000000000000
                00000000
Ch’ti JUG
                                   A little over
                                    10^3671
               0000000000000000000000000000000000000000000000000
                000000000000000000000000000


             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
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
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
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
                 }
                 ...
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
Ch’ti JUG   Deterministic: N queens
                   Demo
                   Not feasible
                    • Not optimal
                   Good initialization
                    • Jump 10 meter into the pool
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
            }
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
             ...
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
              • ...
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
Ch’ti JUG   Metaheuristic: local search
Ch’ti JUG
Ch’ti JUG                 Local optima
             1) Deterministic StartingSolutionInitializer
             2) Simple local search
             3) Stuck in local optimum!




                                 Source: Wikipedia
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
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>
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();
Ch’ti JUG     Double time !=> double score
                        Examination test data 7




            Soft
            score




                          Time (hours:minutes)
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
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
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);
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
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
Ch’ti JUG
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
Ch’ti JUG                  Summary
             Drools Planner solves planning problems
             Adding constraints is easy and scalable
             Switching/combining algorithms is easy
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



                                                                  64
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



                                                                  65
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/
Ch’ti JUG                   Cocktail


             Merci pour votre attention
             Merci à Cylande pour son sponsoring

Drools Planner Chtijug 2010

  • 1.
    Ch’ti JUG Jboss Drools & Drools Planner 21 janvier 2010
  • 2.
  • 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.
    Ch’ti JUG Effectifs : 430 collaborateurs dans le monde, 360 en France, 300 ressources basées à Roubaix 10 0 Experts Metiers 90 Directeur de Projet 80 C de Projet Métier hef 70 C de Projet hef 60 Techniques Formateur et @learning 50 Directeursde Produits 40 30 Développeurs 20 Recette et Qualification 10 H L ot ine 0 Préparateurset Effectifs déploiem ents 4
  • 5.
    Ch’ti JUG Storeland pilote l’ensemble de votre supply chain étendue
  • 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.
    Ch’ti JUG Unecroissance résolument tournée vers l’international Lauréat du Prix PME France CHINE ACFCI / CCIFC
  • 8.
    Ch’ti JUG Automated planning with Drools Planner Geoffrey De Smet Drools Planner lead "Do more with less."
  • 9.
    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
  • 10.
    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
  • 11.
    Ch’ti JUG N Queens: use case  Place n queens on a n-sized chess board  No 2 queens can attack each other
  • 12.
    Ch’ti JUG N queens: partially solved  Score -1 for every 2 queens that can attack each other Score = -2
  • 13.
    Ch’ti JUG N queens: an optimal solution Score = 0
  • 14.
    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
  • 15.
  • 16.
    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?
  • 17.
    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 • ...
  • 18.
  • 19.
    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
  • 20.
    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
  • 21.
    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
  • 22.
  • 23.
    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.
  • 24.
    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.
  • 25.
    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
  • 26.
    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  ...
  • 27.
    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)
  • 28.
    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
  • 29.
    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); } } } }…} }
  • 30.
    Ch’ti JUG Needle in a haystack  How many possible solutions? • 1096 exams • 80 periods • 28 rooms  > habitants in Lille per km²? • 6 483 hab./km² Source: wikipedia
  • 31.
    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)
  • 32.
    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)
  • 33.
    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)
  • 34.
    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
  • 35.
    Ch’ti JUG A little over 10^3671  744437246744648820113833159531546214974276974551140513162882691 3469284310834499031050210214743407656244813085240442809855321 17872268184924364558999914849676314196976841658179857396613906 3492625485909685725897730184010924994541828672670138943325039 683048943713412274829614721695599636159777727101713768378004615 48701272177587402234891701308937790853816473943603349353332893 68078384002213161233225755719910067066354676237665251240673552 3153767499024677368278799816044299431500884240408977216982760 67946148250230917492054728443158872165054373936157659332956136 774730870081258025518405492389480888615900164269035398348299 0003805674675524102808572658937105740571173904119233244862828 53392817922617168734507604739703552080299261320457186755798353 7967203299588154666629888459837384660489020381221523812268702 28697167564520947170314014038670253281783219898668392349799158 35407169443312860837423115961300328664844607892218572759207572 4811
  • 36.
    Ch’ti JUG A little over 10^3671  604813577241247185462510563049535812195201797417621522126155060 76944992828720005800729579185467968191720128852327413111071565 0043989565813921764252807306941995041630327604298194478260407 65201495454290825675151996355311686689270103635691882586316830 6139401723974701085877081645821563181943787272983111911411368916 8267734458648249288525981253268712682909721892541332433788104 618254995718184937280503163787574781545179918774455713682720486 0856763230803748948170736540773077834904096264467405007381183 921101733071148798313412153048340998159011667296994070172526454 17836852601401021510814954906747082633216854492531462935276329 8262882437094345239245616252628477471654331980909505146422698 5500820819509960070516675580035694278266373295312687962113803 354280700964987221060506159614496708252300794687287842958627 4134471258439206305573503782097081716925686154420223798946020 9728873590430061008523877953514829733076235819258465550027938 41
  • 37.
    Ch’ti JUG A little over 10^3671  41281947539904670755491533163612447621027075998378388100740372 5028189106738399600287059413396296063538199837169373556801830 5836646411561304836723541726522661983307438198684385880446218 050094809565635384648937983793088308243838089365451116083129 64868056598674131595193654957707706822143338172833633019666638 035983430262037019665125647894212392790462389810030266845803 0790315153020620193795388869486770234724354626457650058047468 161664023993402310021870051091820162111647624929917192405039351 1639247398607555167937946055347704746052684593317642558493208 66378895400041597447191732266335485557327003619802076964131266 186551891831601623573904848347851683860381473416171492241589945 908191501085456952341588756767389366458777600000000000000000 0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 00000
  • 38.
    Ch’ti JUG A little over 10^3671  0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000 00000000
  • 39.
    Ch’ti JUG A little over 10^3671  0000000000000000000000000000000000000000000000000 000000000000000000000000000  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
  • 40.
    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
  • 41.
    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
  • 42.
    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 } ...
  • 43.
    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
  • 44.
    Ch’ti JUG Deterministic: N queens  Demo  Not feasible • Not optimal  Good initialization • Jump 10 meter into the pool
  • 45.
    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 }
  • 46.
    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  ...
  • 47.
    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 • ...
  • 48.
    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
  • 49.
    Ch’ti JUG Metaheuristic: local search
  • 50.
  • 51.
    Ch’ti JUG Local optima  1) Deterministic StartingSolutionInitializer  2) Simple local search  3) Stuck in local optimum! Source: Wikipedia
  • 52.
    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
  • 53.
    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>
  • 54.
    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();
  • 55.
    Ch’ti JUG Double time !=> double score Examination test data 7 Soft score Time (hours:minutes)
  • 56.
    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
  • 57.
    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
  • 58.
    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);
  • 59.
    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
  • 60.
    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
  • 61.
  • 62.
    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
  • 63.
    Ch’ti JUG Summary  Drools Planner solves planning problems  Adding constraints is easy and scalable  Switching/combining algorithms is easy
  • 64.
    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 64
  • 65.
    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 65
  • 66.
    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/
  • 67.
    Ch’ti JUG Cocktail  Merci pour votre attention  Merci à Cylande pour son sponsoring