View
215
Download
0
Tags:
Embed Size (px)
Citation preview
Constraint ProgrammingAn Appetizer
Christian [email protected]
Laboratory of Electronics and Computer SystemsInstitute of Microelectronics and Information Technology
KTH – Royal Institute of TechnologyStockholm, Sweden
Christian Schulte, IMIT, KTH 22003-10-02
Constraint Programming
Solving combinatorial problems
start with a first toy problem
Christian Schulte, IMIT, KTH 32003-10-02
Send More Money (SMM)
Find distinct digits for letters, such that
SEND+ MORE= MONEY
Christian Schulte, IMIT, KTH 42003-10-02
Constraint Model for SMM
Variables: S,E,N,D,M,O,R,Y {0,…,9}
Constraints:distinct(S,E,N,D,M,O,R,Y)
1000×S+100×E+10×N+D + 1000×M+100×O+10×R+E = 10000×M+1000×O+100×N+10×E+YS0 M0
Christian Schulte, IMIT, KTH 52003-10-02
Solving SMM
Find values for variables
such that
all constraints satisfied
Christian Schulte, IMIT, KTH 62003-10-02
Finding a Solution
Enumerate assignments: poor! Constraint programming
compute with possible values prune inconsistent values
constraint propagationsearch
branch: define search tree explore: explore search tree for solution
Overview
Christian Schulte, IMIT, KTH 82003-10-02
Overview
Constraint propagation Search Summary: Applications & Principles Application examples
resource scheduling instruction scheduling
Constraint Programming 2G1515
Constraint Propagation
Christian Schulte, IMIT, KTH 102003-10-02
Important Concepts
Constraint store Basic constraint Propagator Non-basic constraint Constraint propagation
Christian Schulte, IMIT, KTH 112003-10-02
Constraint Store
Stores basic constraints map variables to possible values
x{3,4,5} y{3,4,5}
Christian Schulte, IMIT, KTH 122003-10-02
Constraint Store
Stores basic constraints map variables to possible values
x{3,4,5} y{3,4,5}
finite domain constraints
Christian Schulte, IMIT, KTH 132003-10-02
Constraint Store
Stores basic constraints map variables to possible values
Domains: finite sets, real intervals, trees, …
x{3,4,5} y{3,4,5}
Christian Schulte, IMIT, KTH 142003-10-02
Propagators
Implement non-basic constraints
distinct(x1,…,xn)
x + 2×y = z
Christian Schulte, IMIT, KTH 152003-10-02
Propagators
Amplify store by constraint propagation
x{3,4,5} y{3,4,5}
xy y>3
Christian Schulte, IMIT, KTH 162003-10-02
Propagators
Amplify store by constraint propagation
x{3,4,5} y{3,4,5}
xy y>3
Christian Schulte, IMIT, KTH 172003-10-02
Propagators
Amplify store by constraint propagation
x{3,4,5} y{4,5}
xy y>3
Christian Schulte, IMIT, KTH 182003-10-02
Propagators
Amplify store by constraint propagation
x{3,4,5} y{4,5}
xy y>3
Christian Schulte, IMIT, KTH 192003-10-02
Propagators
Amplify store by constraint propagation
x{4,5} y{4,5}
xy y>3
Christian Schulte, IMIT, KTH 202003-10-02
Propagators
Amplify store by constraint propagation Disappear when done (entailed)
no more propagation possible
x{4,5} y{4,5}
xy y>3
Christian Schulte, IMIT, KTH 212003-10-02
Propagators
Amplify store by constraint propagation Disappear when done (entailed)
no more propagation possible
x{4,5} y{4,5}
xy
Christian Schulte, IMIT, KTH 222003-10-02
Computation Space
Store with connected propagators
x{4,5} y{4,5}
xy y>3
Christian Schulte, IMIT, KTH 232003-10-02
Propagation for SMM
Results in storeS=9 E{4,…,7} N{5,…,8} D{2,…,8} M=1 O=0 R{2,…,8} Y{2,…,8}
Propagation alone not sufficient! create simpler sub-problems branching
Search
Christian Schulte, IMIT, KTH 252003-10-02
Important Concepts
Branching Exploration Branching heuristics Best solution search
Christian Schulte, IMIT, KTH 262003-10-02
Branching
Yields spaces with additional constraints Enables further constraint propagation
x{4,5} y{4,5}
xy y>3
x{4} y{4}
xy y>3
x{5} y{4,5}
xy y>3
x=4 x4
Christian Schulte, IMIT, KTH 272003-10-02
Branching Strategy
Pick variable x with at least two values Pick value n from domain of x Branch with
x=n and xn
Part of model
Christian Schulte, IMIT, KTH 282003-10-02
Search
Iterate propagation and branching Orthogonal: branching exploration Nodes:
Unsolved Failed Succeeded
Christian Schulte, IMIT, KTH 292003-10-02
SMM: Solution
SEND+ MORE= MONEY
9567+ 1085= 10652
Christian Schulte, IMIT, KTH 302003-10-02
Heuristics for Branching
Which variable least possible values (first-fail) application dependent heuristic
Which value minimum, median, maximum
x=m or xm split with median m
x<m or xm Problem specific
Christian Schulte, IMIT, KTH 312003-10-02
SMM: Solution With First-fail
SEND+ MORE= MONEY
9567+ 1085= 10652
Christian Schulte, IMIT, KTH 322003-10-02
Send Most Money (SMM++)
Find distinct digits for letters, such that
and MONEY maximal
SEND+ MOST= MONEY
Christian Schulte, IMIT, KTH 332003-10-02
Best Solution Search
Naïve approach: compute all solutions choose best
Branch-and-bound approach: compute first solution add “betterness” constraint to open nodes next solution will be “better” prunes search space
Christian Schulte, IMIT, KTH 342003-10-02
Branch-and-bound Search
Find first solution
Christian Schulte, IMIT, KTH 352003-10-02
Branch-and-bound Search
Explore with additional constraint
Christian Schulte, IMIT, KTH 362003-10-02
Branch-and-bound Search
Explore with additional constraint
Christian Schulte, IMIT, KTH 372003-10-02
Branch-and-bound Search
Guarantees better solutions
Christian Schulte, IMIT, KTH 382003-10-02
Branch-and-bound Search
Guarantees better solutions
Christian Schulte, IMIT, KTH 392003-10-02
Branch-and-bound Search
Last solution best
Christian Schulte, IMIT, KTH 402003-10-02
Branch-and-bound Search
Proof of optimality
Christian Schulte, IMIT, KTH 412003-10-02
Modelling SMM++
Constraints and branching as before Order among solutions with constraints
so-far-best solution S,E,N,D,M,O,T,Y current node S,E,N,D,M,O,T,Y constraint added
10000×M+1000×O+100×N+10×E+Y<
10000×M+1000×O+100×N+10×E+Y
Christian Schulte, IMIT, KTH 422003-10-02
SMM++: Branch-and-bound
SEND+ MOST= MONEY
9782+ 1094= 10876
Christian Schulte, IMIT, KTH 432003-10-02
SMM++: All Solution Search
SEND+ MOST= MONEY
9782+ 1094= 10876
Summary
Christian Schulte, IMIT, KTH 452003-10-02
Summary
Modeling variables with domain constraints to state relations branching strategy solution ordering
Solving constraint propagation constraint branching search tree exploration
applications
principles
Christian Schulte, IMIT, KTH 462003-10-02
Application Areas Timetabling Scheduling Crew rostering Resource allocation Workflow planning and optimization Gate allocation at airports Sports-event scheduling Railroad: track allocation, train allocation, schedules Automatic composition of music Genome sequencing Frequency allocation …
Christian Schulte, IMIT, KTH 472003-10-02
Application Examples
Scheduling resources machines, personal, … constraint programming showcase
Instruction scheduling used in compilation for modern microprocessors with latencies
Christian Schulte, IMIT, KTH 482003-10-02
Principles
Models for constraint propagation properties and guarantees
Strong constraint propagation global constraints with strong algorithmic methods mantra: search kills, search kills, search k…
Branching strategies Exploration strategies
Christian Schulte, IMIT, KTH 492003-10-02
SMM: Strong Propagation
SEND+ MORE= MONEY
9567+ 1085= 10652
Christian Schulte, IMIT, KTH 502003-10-02
Used Techniques
Artificial intelligence
Operations research
Algorithms
Programming languages
Christian Schulte, IMIT, KTH 512003-10-02
Why Does CP Matter?
Middleware for combining smart algorithmic components
scheduling graphs flows …
plus essential extra constraints
Christian Schulte, IMIT, KTH 522003-10-02
Significance
Constraint programming identified as a strategic direction in computer science research
[ACM Computing Surveys, December 1996]
Applications are ubiquitous knowledgeable people are not!
Christian Schulte, IMIT, KTH 532003-10-02
Relation to Logic Programming
Logic programminghorn clauses + trees with unification
Constraint logic programminghorn clauses + constraint domain
Constraint programmingX + constraint domain with focus on constraints refined search language
Scheduling Resources
Modeling Propagation Strong propagation
Christian Schulte, IMIT, KTH 552003-10-02
Scheduling Resources: Given
Tasks duration resource
Precedence constraints determine order among two tasks
Resource constraints at most one task per resource
[disjunctive, non-preemptive scheduling]
Christian Schulte, IMIT, KTH 562003-10-02
Scheduling: Bridge Example
Christian Schulte, IMIT, KTH 572003-10-02
Scheduling: Solution
Start time for each task
All constraints satisfied
Earliest completion time minimal make-span
Christian Schulte, IMIT, KTH 582003-10-02
Scheduling: Model
Variable for start-time of task a
start(a) Precedence constraint: a before b
start(a) + dur(a) start(b)
Christian Schulte, IMIT, KTH 592003-10-02
Propagating Precedence
start(a){0,…,7}start(b){0,…,5}
a
b
a before b
Christian Schulte, IMIT, KTH 602003-10-02
Propagating Precedence
start(a){0,…,7}start(b){0,…,5}
a
b
a
b
a before b
start(a){0,…,2}start(b){3,…,5}
Christian Schulte, IMIT, KTH 612003-10-02
Scheduling: Model
Variable for start-time of task a
start(a) Precedence constraint: a before b
start(a) + dur(a) start(b) Resource constraint:
a before b
or
b before a
Christian Schulte, IMIT, KTH 622003-10-02
Scheduling: Model
Variable for start-time of task a
start(a) Precedence constraint: a before b
start(a) + dur(a) start(b) Resource constraint:
start(a) + dur(a) start(b)
or
b before a
Christian Schulte, IMIT, KTH 632003-10-02
Scheduling: Model
Variable for start-time of task a
start(a) Precedence constraint: a before b
start(a) + dur(a) start(b) Resource constraint:
start(a) + dur(a) start(b)
or
start(b) + dur(b) start(a)
Christian Schulte, IMIT, KTH 642003-10-02
Reified Constraints
Use control variable b{0,1}
c b=1 Propagate
c holds propagate b=1 c holds propagate b=0 b=1 holds propagate c b=0 holds propagate c
Christian Schulte, IMIT, KTH 652003-10-02
Reified Constraints
Use control variable b{0,1}
c b=1 Propagate
c holds propagate b=1 c holds propagate b=0 b=1 holds propagate c b=0 holds propagate c
not easy!
Christian Schulte, IMIT, KTH 662003-10-02
Reification for Disjunction
Reify each precedence
[start(a) + dur(a) start(b)] b0=1
and
[start(b) + dur(b) start(a)] b1=1
Model disjunction
b0 + b1 1
Christian Schulte, IMIT, KTH 672003-10-02
Model Is Too Naïve
Local view individual task pairs O(n2) propagators for n tasks
Global view all tasks on resource single propagator smarter algorithms possible
Christian Schulte, IMIT, KTH 682003-10-02
Edge Finding
Find ordering among tasks (“edges”) For each subset of tasks {a}B
assume: a before B
deduce information for a and B assume: B before a
deduce information for a and B join computed information can be done in O(n2)
Christian Schulte, IMIT, KTH 692003-10-02
Summary
Modeling easy but not always efficient constraint combinators (reification) global constraints smart heuristics
More on constraint-based schedulingBaptiste, Le Pape, Nuijten. Constraint-basedScheduling, Kluwer, 2001.
Instruction Scheduling
Christian Schulte, IMIT, KTH 712003-10-02
Instruction Scheduling
Optimized object code by compiler Minimum length instruction schedule
precedence latency resources
per basic block
Best paper CP 2001, Peter van Beek and Kent Wilken, Fast Optimal Scheduling for Single-issue Processors with Arbitrary Latencies, 2001.
Christian Schulte, IMIT, KTH 722003-10-02
Problem
r1 a r2 b
r1 r1 + r2 r3 c
r1 r1 + r3E
1 3
3
A B
D C
Christian Schulte, IMIT, KTH 732003-10-02
Problem
r1 a r2 b
r1 r1 + r2 r3 c
r1 r1 + r3E
1 3
3
A B
D C
instructions
Christian Schulte, IMIT, KTH 742003-10-02
Problem
r1 a r2 b
r1 r1 + r2 r3 c
r1 r1 + r3E
1 3
3
A B
D C
dependency with latency
Christian Schulte, IMIT, KTH 752003-10-02
Problem
r1 a r2 b
r1 r1 + r2 r3 c
r1 r1 + r3E
1 3
3
A B
D C
instructions i, j
Christian Schulte, IMIT, KTH 762003-10-02
Problem
r1 a r2 b
r1 r1 + r2 r3 c
r1 r1 + r3E
1 3
3
A B
D C
latency l(i, j)
Christian Schulte, IMIT, KTH 772003-10-02
Problem
r1 a r2 b
r1 r1 + r2 r3 c
r1 r1 + r3E
1 3
3
A B
D C
Find issue time s(i)1. ij s(i)s(j)2. s(i)+l(i,j) s(j)
Minimizemax s(i)
Christian Schulte, IMIT, KTH 782003-10-02
Problem Is also Model
r1 a r2 b
r1 r1 + r2 r3 c
r1 r1 + r3E
1 3
3
A B
D C
Find issue time s(i)1. ij s(i)s(j)2. s(i)+l(i,j) s(j)
Minimizemax s(i)
Christian Schulte, IMIT, KTH 792003-10-02
Model
All issue times must be distinct use single distinct constraint (as in SMM) is resource constraint or unit duration
Latency constraints precedence constraints (as before) difference: duration latency
Christian Schulte, IMIT, KTH 802003-10-02
Making It Work
Only propagate bounds information relevant for distinct
Add redundant constraints regions: additional structure in DAG successor and predecessor constraints
[special case of edge-finding]
Christian Schulte, IMIT, KTH 812003-10-02
Results
Tested with gcc SPEC95 FP Large basic blocks
up to 1000 instructions
Optimally solved less than 0.6% compile time increase limited static improvement (< 0.1%) better dynamic impact (loops)
Far better than ILP approach
2G1515
Christian Schulte, IMIT, KTH 832003-10-02
Course Overview 2G1515
As to be expected, no surprises:
applications
principles
pragmatics
limitations
Christian Schulte, IMIT, KTH 842003-10-02
Organization
12 lectures Assignments (practical + principles) Exam Material
to be developed as we go… with some textbook excerpts + research papers
Christian Schulte, IMIT, KTH 852003-10-02
More Info
Check my webpage frequently…
www.imit.kth.se/~schulte/