drools cylande chtijug 2010
TRANSCRIPT
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’ti JUG
Jboss Drools&
Drools Planner
21 janvier 2010
Ch’ti JUGCh’ti JUG
Ch’ti JUGCh’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 JUGCh’ti JUG
44
Effectifs : 430 collaborateurs dans le monde, 360 en France, 300 ressources basées à Roubaix
0
10
20
30
40
50
60
70
80
90
100
Effectifs
Experts Metiers
Directeur de Projet
Chef de Projet Métier
Chef de ProjetTechniques
Formateur et @learning
Directeurs de Produits
Développeurs
Recette et Qualification
Hot Line
Préparateurs etdéploiements
Ch’ti JUGCh’ti JUG Storeland pilote l’ensemble de votre supply chain étendue
Ch’ti JUGCh’ti JUG
Cylande a accompagné l’équipe de France
de Judo à Pékin
Le judo véhicule des valeurs
CYLANDE partenaire de la FFJ
Ch’ti JUGCh’ti JUG
Lauréat du Prix PME France CHINE ACFCI / CCIFC
Une croissance résolument tournée vers l’international
●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
Mark Proctor
Project Lead
9
Business Logic integration Platform
DroolsGuvnor
DroolsFusion
DroolsFlow
DroolsExpert
Introduction
Drools Expert
Learn by Example
12
D a t e d a t ed o u b l e a m o u n ti n t t y p el o n g a c c o u n t N o
C a s h f l o w
l o n g a c c o u n t N od o u b l e b a l a n c e
A c c o u n t
D a t e s t a r tD a t e e n d
A c c o u n t i n g P e r i o d
Classes
13
AccountaccountNo balance
1 0
increase balance for AccountPeriod Credits
select * from Account acc, Cashflow cf, AccountPeriod apwhere acc.accountNo == cf.accountNo and cf.type == CREDIT cf.date >= ap.start and cf.date <= ap.end
decrease balance for AccountPeriod Debits
select * from Account acc, Cashflow cf, AccountPeriod apwhere acc.accountNo == cf.accountNo and cf.type == DEBIT cf.date >= ap.start and cf.date <= ap.end
AccountingPeriodstart end
01-Jan-07 31-Mar-07
trigger : acc.balance += cf.amount
trigger : acc.balance -= cf.amount
Accountbalance
1 -25accountNo
Creating Views with Triggersdate amount type
12-Jan-07 100 CREDIT 12-Feb-07 200 DEBIT 118-May-07 50 CREDIT 19-Mar-07 75 CREDIT 1
accountNo
date amount type12-Jan-07 100 CREDIT9-Mar-07 75 CREDIT
CashFlow
date amount type2-Feb-07 200 DEBIT
CashFlow
14
What is a Rule
• rule “<name>” <attribute> <value> when <LHS> then <RHS>end
Quotes on Rule names are optional if the rule name has no spaces.
salience <int>agenda-group <string>no-loop <boolean>auto-focus <boolean>duration <long>....
RHS can be any valid java. Or MVEL. Other languages could be added.
15
Imperative vs Declarative
• public void helloMark(Person person) { if ( person.getName().equals( “mark” ) { System.out.println( “Hello Mark” ); }}
• rule “Hello Mark” when Person( name == “mark” ) then System.out.println( “Hello Mark” );end
LHS
RHS
specific passing of instances
Methods that must be called directly
Rules can never be called directly
Specific instances cannot be passed.
16
S h o w e r( t e m p e r a t u r e = = “ h o t ” )
P a t t e r n
F i e l d C o n s t r a i n t
R e s t r i c t i o n
E v a l u a t o rV a l u e
O b j e c t T y p e
F i e l d N a m e
What is a Pattern
17
rule “increase balance for AccountPeriod Credits” when ap : AccountPeriod() acc : Account( $accountNo : accountNo ) CashFlow( type == CREDIT, accountNo == $accountNo, date >= ap.start && <= ap.end, $ammount : ammount ) then acc.balance += $amount; end
select * from Account acc, Cashflow cf, AccountPeriod apwhere acc.accountNo == cf.accountNo and cf.type == CREDIT cf.date >= ap.start and cf.date <= ap.end
trigger : acc.balance += cf.amount
Pattern
Pattern Binding
field Binding
Literal Restriction
Variable Restriction
Multri Restriction - Variable Restriction
field Binding
Consequence (RHS)
Bringing it Together
18
AccountaccountNo balance
1 0
rule “increase balance for AccountPeriod Credits” when ap : AccountPeriod() acc : Account( $accountNo : accountNo ) CashFlow( type == CREDIT, accountNo == $accountNo, date >= ap.start && <= ap.end, $ammount : ammount ) then acc.balance += $amount; end
AccountingPeriodstart end
01-Jan-07 31-Mar-07
rule “decrease balance for AccountPeriod Debits” when ap : AccountPeriod() acc : Account( $accountNo : accountNo ) CashFlow( type == DEBIT, accountNo == $accountNo, date >= ap.start && <= ap.end, $ammount : ammount ) then acc.balance -= $amount; end
Rules as a “ view”
Accountbalance
1 -25accountNo
date amount type12-Jan-07 100 CREDIT 12-Feb-07 200 DEBIT 118-May-07 50 CREDIT 19-Mar-07 75 CREDIT 1
accountNo
date amount type12-Jan-07 100 CREDIT9-Mar-07 75 CREDIT
CashFlowdate amount type
2-Feb-07 200 DEBIT
CashFlow
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 )
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 )
21
Person(age > 30 && < 40 || hair == “black”)
Person(pets[’rover’].type == “dog”)
Person(pets[0].type == “dog”)
Person(age > 30 && < 40 || hair in (“black”, “brown”) )
Person(pets contain $rover )
Person( (age > 30 && < 40 && hair == “black”)
||
(age > 50 && hair == “grey”) )
More Pattern Examples
22
What is a Production Rule System
ProductionMemory
WorkingMemory
Inference Engine
Pattern Matcher
Agenda(rules) (facts)
insertupdateretract
Repository of inserted Java instances
Codification of the business knowledge
Rules can change
on the fly
23
A c c o u n tA c c o u n t i n g P e r i o dC a s h f l o w
v i e w1 v i e w2
m a i n v i e w
T a b l e s
V i e w s
V i e w
A c c o u n tA c c o u n t i n g P e r i o dC a s h f l o w
r u l e1 r u l e2
a g e n d a
O b j e c t T y p e s
R u l e s
a g e n d a
Production Rule SystemApproximated by SQL and Views
24
rule “Print blance for AccountPeriod” salience -50 when ap : AccountPeriod() acc : Account( ) then System.out.println( acc.accountNo + “ : “ acc.balance ); end
Salience
Agenda1 increase balance
arbitrary2 decrease balance3 increase balance4 print balance
Conflict Resolution with Salience
25
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
rule “Print blance for AccountPeriod” ruleflow-group “report” when ap : AccountPeriod() acc : Account( ) then System.out.println( acc.accountNo + “ : “ acc.balance ); end
ruleflow-group
RuleFlow
26
Two Phase System
Working Memory Action
retract
modifyinsert
Agenda Evaluation
Select Rule to Fire
exit
No RuleFound
Fire Rule
Determine possible rules to
fire
RuleFound
Conditional Elements
28
not Bus( color = “red” )
From CE for Expressions
exists Bus( color = “red” )
forall ( $bus : Bus( floors == 2 )
Bus( this == $bus, color == “red” ) )
forall ( $bus : Bus( color == “red” ) )
From CEfor Expressions
30
From CE for Expressions
rule “Find all the pets for a given owner”when $owner : Person( name == “mark” ) Pet( name == “rover” ) from $owner.pets
Using 'from' to reason over the nested list
31
'from' can work on any expression, not just a nested field on a bound variable.
From CE for Expressions
rule “Find People for given zip code”when $zipCode : ZipCode() Person( ) from $hbn.getNamedQuery(“Find People”) .setParameters( [ “zipCode” : $zipCode ] ) .list()Hibernate session
Collect CE
33
Collect CE
rule "accumulate"
when
$list : List( intValue > 100 )
from collect( Bus( color == "red" ) )
then
print "red buses “ + $list;
end
Accumulate CE
35
Accumulate CErule "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
36
Accumulate CErule "accumulate"
when
$sum : Number( intValue > 100 )
from accumulate( Bus( color == "red", $t : takings ) sum( $t ) )
then
print "sum is “ + $sum;
end
37
Accumulate CE
Patterns and CE's can be chained with 'from'
rule "collect"
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
TimersCalendars
39
Timers
rule “name” timer 1m30swhen $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” )
40
Timers
rule “name” timer ( cron: 0 0/15 * * * * )when $l : Light( status == “on” )then sendEmail( “turn the light off” )
Field Name Mandatory? Allowed Values Allowed Special CharactersSeconds YES 0-59 , - * /Minutes YES 0-59 , - * /Hours YES 0-23 , - * /Day of month YES 1-31 , - * ? / L WMonth YES 1-12 or JAN-DEC , - * /Day of week YES 1-7 or SUN-SAT , - * ? / L #Year NO empty, 1970-2099 , - * /
41
Calendarsrule "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
Truth MaintenanceInference
43
TMS and Inferencerule "Issue Child Bus Pass"
when
$p : Person( age < 16 )
then
insert(new ChildBusPass( $p ) );
end
rule "Issue Adult Bus Pass"
when
$p : Person( age >= 16 )
then
insert(new AdultBusPass( $p ) );
end
Couples the logic
What happens when the Child stops being 16?
44
TMS and Inference Bad
● Monolithic● Leaky● Brittle integrity - manual maintenance
45
TMS and Inference A rule “logically” inserts an object When the rule is no longer true, the object is retracted.
when
$p : Person( age < 16 )
then
logicalInsert( new IsChild( $p ) )
end
when
$p : Person( age >= 16 )
then
logicalInsert( new IsAdult( $p ) )
end
de-couples the logic
Maintains the truth by automatically retracting
46
TMS and Inferencerule "Issue Child Bus Pass"
when
$p : Person( )
IsChild( person =$p )
then
logicalInsert(new ChildBusPass( $p ) );
end
rule "Issue Adult Bus Pass"
when
$p : Person( age >= 16 )
IsAdult( person =$p )
then
logicalInsert(new AdultBusPass( $p ) );
end
The truth maintenance cascades
47
TMS and Inferencerule "Issue Child Bus Pass"
when
$p : Person( )
not( ChildBusPass( person == $p ) )
then
requestChildBusPass( $p );
End
The truth maintenance cascades
48
TMS and Inference Good
● De-couple knowledge responsibilities● Encapsulate knowledge● Provide semantic abstractions for those encapsulation● Integrity robustness – truth maintenance
Tooling
50
Guided Editor
51
Interactive Debugging
52
Decision Tables
53
DSLs
54
DSLs
55
Rule Flow
Drools Fusion
57
$c : Custumer( type == “VIP” )BuyOrderEvent( customer == $c )
session.insert( event ) ;
Rule engines do not scale for CEP. They have a single point of insertion and are single threaded, CEP has concurrent streams of events.
Single Point of entry
Patterns, evaluate facts sequentially in
a single thread.
Scalability
58
$c : Custumer( type == “VIP )BuyOrderEvent( customer == $c ) from entry-point “Home Broker Stream”
Scalability
EntryPoint entryPoint = session.getEntryPoint( “Home Broker Stream” );entryPoint.insert( event ) ;
So lets allow multiple named entry points for those streams
So now we can insert different
streams concurrently
Patterns can now optional specify their
entry-point.
When not specified uses the “default”
entry-point
59
All Fact life-cycles must be managed by the user, so retractions are manual.
declare StockTick @role( event )end
declare StockTick @role( event ) @timestamp( timestampAttr )
companySymbol : String stockPrice : double timestampAttr : longend
Automatic Life-Cycle Management
Just use the declare statement to declare a type as an event
and it will be retracted when it is no longer needed
The declare statement can also specify an internal model, that
external objects/xml/csv map on to. We support Smooks and
JAXB
60
$c : Custumer( type == “VIP )$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”
● coincides
● before
● after
● meets
● metby
● overlaps
● overlappedby
● during
● includes
● starts
● startedby
● finishes
● finishedby
Operators
Rule engines do not have rich enough set of temporal comparison operators BackAckEvent must occur
between 1s and 10s 'after' BuyOrderEvent
The Full set of Operators are supported
61
Operators
62
$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”
Operators
Existing Drools 'not' Conditional Elements can be used to detect
non-occurrence of events
63
Rule engines react to events happening now, there is no temporal understanding of changes over time.
That isn't much without the ability to deal with aggregations, rules engines suck.
Sliding time windows
StockTicker( symbol == “RHAT” ) over window:time( 5s )
StockTicker( symbol == “RHAT” ) over window:length( 1000 )
5s
1000 tickers
64
Aggregations
Rule Engines do not deal with aggregations
$n : Number( intValue > 100 ) from accumulate( $s : StockTicker( symbol == “RHAT” ) over window:time( 5s ), average( $s.price ) )
Over 5 seconds
Aggregate ticker price for RHAT over last 5
seconds
The pattern 'Number' reasons 'from' the accumulate result
$n : accumulate( $s : StockTicker( symbol == “RHAT” ) over window:time( 5s ), average( $s.price ) > 100 )
We can use some sugar to reduce verbosity
Drools Flow
66
Drools Flow
Integration● 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●
A workflow engine combining processes and rules
Truth MaintenanceInference
68
Rules and processes
loosely coupledtightly coupled
spec
ific
gene
ric
DecisionServices
ProcessRules
SC
OP
E
COUPLING
?
69
Business Logic Lifecycle
70
Example
71
RuleFlowGroup
Workflow can control my rules?
72
RuleFlowGroup
Rule Flow Group
73
RuleFlowGroup
74
Constraints
Java code constraint
75
Constraints
Rules can control my workflow?
LHS “when”Rule Constraint
76
Example
Business decisions are externalized using a decision service
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
77
Example
What if there is a lot of business logic like this?
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
rule Decision1 when // conditions then // actionsend
78
RulesEngine
Flow of Control
ProcessEngine
79
RulesEngine
Inversion of Control
ProcessEngine
Age
nda
80
Self monitoring and adaptivedeclare 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
81
Domain Specific Processes
82
Domain Specific Processes
83
Integrated debug and audit
Unified API
DefinitionsRuntime
Language
85
jBPMFile file = new File (“.....”); // file to XML process definition
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( IoUtils.FileToString( file ) );
ProcessInstance processInstance = new ProcessInstance(processDefinition);JessRete engine = new Rete();
FileReader file = new FileReader("myfile.clp");
Jesp parser = new Jesp(file, engine);
parser.parse(false);Esper
EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();
EPStatement countStmt = admin.createEPL( "...." );
countStmt.start();
Definitions
86
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() );
Definitions
87
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() );
Definitions
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 );
Unified Event Model
Ch’ti JUGCh’ti JUG
Mixins Interface Markers
92
Stateful KnowledgeSession
93
Knowledge Runtime
94
Coming in 5.1 BPMN2 (80% of Workflow) Integration
● OSGi ready● Spring● Camel
Seamless Remoting Simulation/Testing New Rete Algorithm “true modify”
95
Spring + camel
from("direct:test-with-session").to("drools:sm/ksession1?dataFormat=drools-xstream");
96
Questions?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. Goodbye. Joshua: Greetings, Professor
Falken.Stephen Falken: Hello, Joshua.Joshua: A strange game. The only winning move is not to play. How about a nice game of chess?
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’ti JUG
Q&A
9
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’ti JUG
Automated planningwith Drools Planner
Geoffrey De SmetDrools Planner lead
"Do more with less."
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’ti JUG N queens: an optimal solution
Score = 0
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’ti JUG N queens: demo
Not optimized!• Hello world
example
Not a realplanning problem• I can make an
optimal solutionfor any n queenswithout a computer
• See Wikipedia
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’ti JUG
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’ti JUG
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’ti JUG Hard and soft score
Solution A B C Hard constraints
• 11 hours rest 1 0 0 Soft constraints
• Fair night workassignment 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
Ch’ti JUGCh’ti JUG
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’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
Ch’ti JUGCh’ti JUG Soft constraints
2 exams in a row.
2 exams in a day.
Period spread: 2 exams that share studentsshould be a number of periods apart.
Mixed durations: 2 exams that share a roomshould not have different durations.
Front load: Large exams should be scheduledearlier 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
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’ti JUGBrute 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
Ch’ti JUGCh’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
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’ti JUG Needle in a haystack
How many possible solutions?• 1096 exams• 80 periods• 28 rooms
> atoms in the universeif 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
Ch’ti JUGCh’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
Ch’ti JUGCh’ti JUGA little over
10^3671
74443724674464882011383315953154621497427697455114051316288269134692843108344990310502102147434076562448130852404428098553211787226818492436455899991484967631419697684165817985739661390634926254859096857258977301840109249945418286726701389433250396830489437134122748296147216955996361597777271017137683780046154870127217758740223489170130893779085381647394360334935333289368078384002213161233225755719910067066354676237665251240673552315376749902467736827879981604429943150088424040897721698276067946148250230917492054728443158872165054373936157659332956136774730870081258025518405492389480888615900164269035398348299000380567467552410280857265893710574057117390411923324486282853392817922617168734507604739703552080299261320457186755798353796720329958815466662988845983738466048902038122152381226870228697167564520947170314014038670253281783219898668392349799158354071694433128608374231159613003286648446078922185727592075724811
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’ti JUGA little over
10^3671
6048135772412471854625105630495358121952017974176215221261550607694499282872000580072957918546796819172012885232741311107156500439895658139217642528073069419950416303276042981944782604076520149545429082567515199635531168668927010363569188258631683061394017239747010858770816458215631819437872729831119114113689168267734458648249288525981253268712682909721892541332433788104618254995718184937280503163787574781545179918774455713682720486085676323080374894817073654077307783490409626446740500738118392110173307114879831341215304834099815901166729699407017252645417836852601401021510814954906747082633216854492531462935276329826288243709434523924561625262847747165433198090950514642269855008208195099600705166755800356942782663732953126879621138033542807009649872210605061596144967082523007946872878429586274134471258439206305573503782097081716925686154420223798946020972887359043006100852387795351482973307623581925846555002793841
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’ti JUGA little over
10^3671
412819475399046707554915331636124476210270759983783881007403725028189106738399600287059413396296063538199837169373556801830583664641156130483672354172652266198330743819868438588044621805009480956563538464893798379308830824383808936545111608312964868056598674131595193654957707706822143338172833633019666638035983430262037019665125647894212392790462389810030266845803079031515302062019379538886948677023472435462645765005804746816166402399340231002187005109182016211164762492991719240503935116392473986075551679379460553477047460526845933176425584932086637889540004159744719173226633548555732700361980207696413126618655189183160162357390484834785168386038147341617149224158994590819150108545695234158875676738936645877760000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’ti JUGA little over
10^3671
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’ti JUGA little over
10^3671
0000000000000000000000000000000000000000000000000000000000000000000000000000
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
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’ti JUGImperfect 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
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’ti JUG Metaheuristic: local search
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’ti JUG
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’ti JUG Double time !=> double score
Softscore
Time (hours:minutes)
Examination test data 7
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’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 q2where 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
Ch’ti JUGCh’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
Ch’ti JUGCh’ti JUG Score rule isolation
rule "multipleQueensHorizontal" when $q1 : Queen($id : id, $y : y); $q2 : Queen(id > $id, y == $y); then ...endrule "multipleQueensAscendingDiagonal" when $q1 : Queen($id : id, $ascendingD : ascendingD); $q2 : Queen(id > $id, ascendingD == $ascendingD); then ...endrule "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
Ch’ti JUGCh’ti JUG
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’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
Ch’ti JUGCh’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
Ch’ti JUGCh’ti JUG
Q&A
66
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
Ch’ti JUGCh’ti JUG
Thanks for your attention!
67
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
Ch’ti JUGCh’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/
68
Copyright © Ch'ti JUG – License Creative Commons 2.0 France
Ch’ti JUGCh’ti JUG Cocktail
Merci pour votre attention Merci à Cylande pour son sponsoring
69