CSCE421/821, Fall 2015 www.cse.unl.edu/~choueiry/F15-421-821
Questions : Piazza
Berthe Y. Choueiry (Shu-we-ri)
AVH 360
Foundations of Constraint Processing, CSCE421/821
Overview 1 1
Motivating example, application areas
CSP: Definition, representation
Some simple modeling examples
More on definition and formal characterization
Basic solving techniques
(Implementing backtrack search)
Advanced solving techniques
Issues & research directions
Foundations of Constraint Processing, CSCE421/821
Overview 1 2
• Context : You are a senior in college
• Problem : You need to register in 4 courses for the Spring semester
• Possibilities : Many courses offered in Math, CSE, EE, CBA, etc.
• Constraints : restrict the choices you can make
– Unary : Courses have prerequisites you have/don't have
Courses/instructors you like/dislike
– Binary : Courses are scheduled at the same time
– n-ary : In CE: 4 courses from 5 tracks such as at least 3 tracks are covered
• You have choices, but are restricted by constraints
– Make the right decisions!!
Foundations of Constraint Processing, CSCE421/821
Overview 1 3
• Given
– A set of variables: 4 courses at UNL
– For each variable, a set of choices (values)
– A set of constraints that restrict the combinations of values the variables can take at the same time
• Questions
– Does a solution exist? (classical decision problem)
– How two or more solutions differ? How to change specific choices without perturbing the solution?
– If there is no solution, what are the sources of conflicts? Which constraints should be retracted?
– etc.
Foundations of Constraint Processing, CSCE421/821
Overview 1 4
Adapted from E.C. Freuder
• Radio resource management (RRM)
• Databases (computing joins, view updates)
• Temporal and spatial reasoning
• Planning, scheduling, resource allocation
• Design and configuration
• Graphics, visualization, interfaces
• Hardware verification and software engineering
• HC Interaction and decision support
• Molecular biology
• Robotics, machine vision and computational linguistics
• Transportation
• Qualitative and diagnostic reasoning
Foundations of Constraint Processing, CSCE421/821
Overview 1 5
Borrowed from Milano&Lombardi
• Netherlands have one of most dense railroad networks in the world, with 5,500 trains per day
• In 2009, the entire timetable has been redesigned from scratch (via OR techniques and Constraint
Programming)
• More robust schedule
• More effective
• Profit increase: ~$75M
Foundations of Constraint Processing, CSCE421/821
Overview 1 6
Borrowed from Milano&Lombardi
• The port of Singapore is one of the world ’s largest container hubs
• 200 shipping lines with connections to 600 ports in 123 countries
• Yard locations and loading plans must be assigned under a large number of operational and safety requirements
• The Yard planning system, based on CP is assisting the job
Foundations of Constraint Processing, CSCE421/821
Overview 1 7
• is about ...
– Solving a decision problem…
– … While allowing the user to state arbitrary constraints in an expressive way and
– Providing concise and high-level feedback about alternatives and conflicts
– CP = Model + Constraint Reasoning + Search
• We will learn what these are
• Related areas:
– AI, OR, Algorithmic, DB, TCS, Prog. Languages, etc.
Foundations of Constraint Processing, CSCE421/821
Overview 1 8
• Flexibility & expressiveness of representations
• Interactivity relax users can constraints reinforce
Foundations of Constraint Processing, CSCE421/821
Overview 1 9
Motivating example, application areas
CSP: Definition, representation
Some simple modeling examples
More on definition and formal characterization
Basic solving techniques
Foundations of Constraint Processing, CSCE421/821
Overview 1 10
• General template of any computational problem
– Given :
• Example: a set of objects, their relations, etc.
– Query/Question :
• Example: Find x such that the condition y is satisfied
• How about the Constraint Satisfaction
Problem?
Foundations of Constraint Processing, CSCE421/821
Overview 1 11
• Given
– A set of variables
– A set of variable domains (domain values)
– A set of constraints,
• Query : can we find a value for each variable such that all constraints are satisfied?
Foundations of Constraint Processing, CSCE421/821
Overview 1 12
Different Queries Yield Different Problems
• Find a solution decision problem
• Find number of/all solutions counting problem
• Find a set of constraints that can be removed so that a solution exists optimization problem
• Etc.
Foundations of Constraint Processing, CSCE421/821
Overview 1 13
• x
• Domains:
– Restricted to {0,1}: Boolean CSPs
– Finite (discrete), enumeration works
– Continuous, sophisticated algebraic techniques are needed
• Consistency techniques on domain bounds
Foundations of Constraint Processing, CSCE421/821
Overview 1 14
• Given
• Find a consistent assignment for variables
Constraint Network (graph, hypergraph)
• Variable node (vertex)
• Domain node label
• Constraint arc (edge) between nodes
Foundations of Constraint Processing, CSCE421/821
Overview 1 15
Graph
• Vertices: variables
• Edges: binary constraints
V1
{1, 2, 3, 4} v1 < v2
{ 3, 6, 7 } V2 v1+v3 < 9
V3
{ 3, 4, 9 } v2 < v3 v2 > v4
{ 3, 5, 7 }
V4
V1
Hypergraph
• Vertices: variables
• Hyperedges: constraints v1+v3 < 9
{ 1, 2, 3, 4 } v1 < v2 v2 < v3
{ 3, 4, 9 } v1+v2+V4 < 10
V3
V2
{ 3, 6, 7 } v2 > v4
{ 3, 5, 7 } V4
Foundations of Constraint Processing, CSCE421/821
Overview 1 16
• A constraint C is defined by
− A scope , the set of variables on which the constraint applies
Notation: SCOPE( C ) , scope ( C ), scp ( C )
− A relation , a subset of the Cartesian product of the domains of the variables in the scope of the constraint
Notation: RELATION( C ) , rel ( C )
• Arity , cardinality of the constraint ’s scope
− Unary, binary, ternary,…, global
− Universal constraint
Foundations of Constraint Processing, CSCE421/821
Overview 1 17
• Extension , all tuples are enumerated
− As a list of allowed tuples (supports, positive table)
− As a list of forbidden tuples (conflicts, no-goods)
• Intension , given by a set builder
− When it is not practical or possible to list all tuples
− Define types/templates of common constraints to be used repeatedly
− Examples: linear constraints, All-Diff (mutex), Atmost,
TSP-constraint, cycle-constraint, etc.
Foundations of Constraint Processing, CSCE421/821
Overview 1 18
• Predicate function
• Set of tuples (list or table)
• Binary matrix (bit-matrix)
• Constrained Decision Diagrams
( [Cheng & Yap, AAAI 05] )
• etc.
Foundations of Constraint Processing, CSCE421/821
Overview 1 19
Motivating example, application areas
CSP: Definition, representation
Some simple modeling examples
More on definition and formal characterization
Basic solving techniques
Foundations of Constraint Processing, CSCE421/821
Overview 1 20
B
A < B
[ 5.... 18]
A
[ 1.... 10 ]
B < C
2 < C - A < 5
[ 4.... 15]
C
• Give one solution: …….
• Satisfaction, yes/no: decision problem
Foundations of Constraint Processing, CSCE421/821
Overview 1 21
Using 3 colors (R, G, & B), color the US map such that no two adjacent states have the same color
• Variables?
• Domains?
• Constraints?
Foundations of Constraint Processing, CSCE421/821
Overview 1 22
Using 3 colors (R, G, & B), color the US map such that no two adjacent states have the same color
WY
CO
{ red, green, blue }
NE
KS
{ red, green, blue }
AR
UT
AZ
NM
{ red, green, blue }
OK
LA
TX
Foundations of Constraint Processing, CSCE421/821
Overview 1 23
{ a, b, c }
{ a, b } { a, c, d }
{ b, c, d }
What is the CSP formulation?
Foundations of Constraint Processing, CSCE421/821
Overview 1 24
{ a, b, c }
{ a, b } { a, c, d }
{ b, c, d }
T2
T3
T7
Interval Order
{ R1, R3 }
T1
{ R1, R3 }
{ R1, R3 } { R1, R3, R4 }
T4
{ R1, R2, R3 }
T5
T6
{ R2, R4 }
{ R2, R4 }
Foundations of Constraint Processing, CSCE421/821
Overview 1
T2
Constraint Graph
T1
{ R1, R3 }
{ R1, R3 }
T4
{ R1, R2, R3 }
T6
{ R2, R4 }
{ R1, R2, R3 }
{ R2, R4 }
T3
{ R1, R3 } T5
T7
25
Cryptarithmetic puzzles
• D
X1
• D
F
= D
=D
X2
T
=D
U
= D
=D
X3
W
= {0,1}
=D
R
=D
O
=[0,9]
F T U W R O
+
T
T
W O
W
F O U
O
R
• O+O = R+10X1
• X1+W+W = U+10X2
(a)
• X2+ T+T = O + 10X3
• X3=F
• Alldiff({F,T,U,W,R,O})
X
3
X
2
(b)
X
1
Foundations of Constraint Processing, CSCE421/821
Overview 1 26
Train, elevator, car, etc.
Given:
• Components and their attributes (variables)
• Domain covered by each characteristic (values)
• Relations among the components (constraints)
• A set of required functionalities (more constraints)
Find: a product configuration i.e., an acceptable combination of components that realizes the required functionalities
Foundations of Constraint Processing, CSCE421/821
Overview 1 27
Given:
• Four musicians: Fred, Ike, Mike, and Sal, play bass, drums, guitar and keyboard, not necessarily in that order.
• They have 4 successful songs, ‘Blue Sky,’ ‘Happy Song,’
‘Gentle Rhythm,’ and ‘Nice Melody.’
• Ike and Mike are, in one order or the other, the composer of
‘Nice Melody’ and the keyboardist.
• etc ...
Query: Who plays which instrument and who composed which song?
Foundations of Constraint Processing, CSCE421/821
Overview 1 28
Formulation 1:
• Variables : Bass, Drums, Guitar, Keyboard, Blue Sky, Happy Song
Gentle Rhythm and Nice Melody.
• Domains : Fred, Ike, Mike, Sal
• Constraints : …
Formulation 2:
• Variables: Fred's-instrument, Ike'sinstrument, …,
Fred's-song, Ikes's-song, Mike ’s-song, …, etc.
• Domains:
{ bass, drums, guitar, keyboard }
{ Blue Sky, Happy Song, Gentle Rhythm, Nice Melody}
• Constraints : …
Foundations of Constraint Processing, CSCE421/821
Overview 1 29
• Example I: algebraic constraints
V1
{1, 2, 3, 4} v1 < v2
{ 3, 6, 7 }
V2 v1+v3 < 9 v2 < v3 v2 > v4
V3
{ 3, 4, 9 } { 3, 5, 7 }
V4
• Example II: B
A
A < B
[ 5.... 18]
(algebraic) constraints
[ 1.... 10 ]
B < C of bounded difference
2 < C - A < 5
[ 4.... 15]
C
• Example III & IV: coloring, mutual exclusion, difference constraints
Constraint Graph
NE
T1
WY KS { R1, R3 }
T4
{ red, green, blue }
{ R1, R2, R3 }
T6
CO
AR
{ R2, R4 }
{ red, green, blue }
UT { R1, R3 }
AZ
NM
{ red, green, blue }
OK
LA
T3
{ R1, R3 }
{ R1, R2, R3 }
{ R2, R4 }
T7
T5
TX
• Example V & VI: elements of C must be made explicit
Foundations of Constraint Processing, CSCE421/821
Overview 1 30
• Example VII: Databases
– Join operation in relational DB is a CSP
– View materialization is a CSP
• Example VIII : Interactive systems
– Data-flow constraints
– Spreadsheets
– Graphical layout systems and animation
– Graphical user interfaces
• Example IX: Molecular biology (bioinformatics)
– Threading, etc
Foundations of Constraint Processing, CSCE421/821
Overview 1 31
Motivating example, application areas
CSP: Definition, representation
Some simple modeling examples
More on definition and formal characterization
Basic solving techniques
Foundations of Constraint Processing, CSCE421/821
Overview 1 32
Macrostructure G(P):
- constraint graph for binary constraints
- constraint network for non-binary constraints
V2
V1 a, c a, b
b, c V3
(V1, a ) (V1, b)
Micro-structure
(P):
(V2, a ) (V2, c) (V3, b ) (V3, c)
(V1, a ) (V1, b) no goods
Co-microstructure co-
(P) :
(V2, a ) (V2, c) (V3, b ) (V3, c)
Foundations of Constraint Processing, CSCE421/821
Overview 1 33
• Decision problem
• In general, NP-complete by reduction from 3SAT
Foundations of Constraint Processing, CSCE421/821
Overview 1 34
1. Show that
1 is in NP
2. Given a problem
1 in NP , show that an known
NP -complete problem
2 can be efficiently reduced to
– Select a known NP -complete problem
SAT)
1
2
(e.g.,
– Construct a transformation f from
2 to
1
– Prove that f is a polynomial transformation
(Check Chapter 3 of Garey & Johnson)
Foundations of Constraint Processing, CSCE421/821
Overview 1 35
Given a sentence :
– Sentence : conjunction of clauses
( c
1
Ú Ø c
4
Ú c
5
Ú c
6
) ( c
2
– Clause : disjunction of literals
Ú Ø c
3
( c
2
Ú Ø c
3
)
– Literal : a term or its negation c
1
,
Ø c
6
Ø c
4
– Term : Boolean variable c
1
=
1
Û Ø c
1
=
0
Question : Find an assignment of truth values to the
Boolean variables such the sentence is satisfied.
Foundations of Constraint Processing, CSCE421/821
Overview 1 36
• Verifying that an assignment for all variables is a solution
– Provided constraints can be checked in polynomial time
• Reduction from 3SAT to CSP
– Many such reductions exist in the literature
(perhaps 7 of them)
Foundations of Constraint Processing, CSCE421/821
Overview 1 37
Example: CSP into SAT ( proves nothing, just an exercise )
Notation: variable-value pair = vvp
• vvp
term
– V
1
= {a, b, c, d} yields x
1
= (V
1
, a), x
2
= (V
1
, b), x
3
= (V
1
, c), x
4
– V
2
= {a, b, c} yields x
5
= (V
2
, a), x
6
= (V
2, b), x
7
• The vvp ’s of a variable disjunction of terms
– V
1
= {a, b, c, d} yields
• (Optional) At most one VVP per variable
–
Way 1:
(
( x
1
Ù
Ø x
1
Ø x
2
Ù Ø x
2
Ù Ø x
3
Ù x
3
Ù
Ù x
1
Ø x
4
Ø x
4
Ú x
) (
) (
2
Ø x
1
Ø x
1
Ú
Ù
Ù x x
2
3
Ù
Ø x
2
Ú
Ø x
3
Ù x
4
Ù
Ø x
3
= (V
2
,c).
Ø x
4
Ù x
4
)
)
Ú
= (V
1
, d),
– Way 2:
Foundations of Constraint Processing, CSCE421/821
Overview 1 38
Constraint: C
V
1
V
2
=
{( a , a ), ( a , b ), ( b , c ), ( c , b ), ( d , a )}
1.
Way 1: Each inconsistent tuple
one disjunctive clause
–
Ø x
Ú Ø x
2.
Way 2: a) Consistent tuple
conjunction of terms x
1
Ù x
5 b) Each constraint
disjunction of these conjunctions
( x
1
(
Ù x
5
) ( x
1
) (
Ù x
6
) (
) x
2
Ù x
7
)
Ú x
Ù x
Ú x
Ù x
3 6 4 5
transform into conjunctive normal form (CNF)
Question : find a truth assignment of the Boolean variables such that the sentence is satisfied
Foundations of Constraint Processing, CSCE421/821
Overview 1 39
Motivating example, application areas
CSP: Definition, representation
Some simple modeling examples
More on definition and formal characterization
Basic solving techniques
• Modeling and consistency checking
• Constructive, systematic search
• Iterative improvement, local search
Foundations of Constraint Processing, CSCE421/821
Overview 1 40
1. Constructive, systematic
2. Iterative repair, local search
Foundations of Constraint Processing, CSCE421/821
Overview 1 41
Consider:
• Importance of modeling /formulation:
– To control the size of the search space
• Preprocessing
– A.k.a. constraint filtering/propagation, consistency checking
– reduces size of search space
Foundations of Constraint Processing, CSCE421/821
Overview 1 42
• N -queen : formulation 1
– 4 Rows
– Domains?
– Size of CSP?
4 Column positions
4
´
4
´
4
´
4
=
4
4 =
2
8
V
1
V
2
V
3
V
4
1 2 3 4
• N -queens : formulation 2
– Variables?
16 Cells
– Domains?
– Size of CSP?
{0,1}
2
16
V
11
V
12
V
13
V
14
V
21
V
22
V
23
V
24
V
31
V
32
V
33
V
34
V
41
V
42
V
43
V
44
Foundations of Constraint Processing, CSCE421/821
Overview 1 43
Arc-consistency
A
A < B
B
13
14
[ 5.... 18]
2
[ 1.... 10 ]
B < C
2 < C - A < 5
6 14
[ 4.... 15] C
1- B: [ 5 .. 14 ]
C: [ 6 .. 15 ]
2- A: [ 2 .. 10 ]
C: [ 6 .. 14 ]
3- B: [ 5 .. 13 ]
Foundations of Constraint Processing, CSCE421/821
Overview 1 44
Arc-consistency: every combination of two adjacent variables
3-consistency, k -consistency ( k
n )
Constraint filtering, constraint checking, etc..
Eliminate non-acceptable tuples prior to search
Warning : arc-consistency does not solve the problem
A
B
A
B
( A
=
2 )
Ù
( B
=
3 ) still is not a solution!
Foundations of Constraint Processing, CSCE421/821
Overview 1 45
Starting from a root node
Consider all values for a variable V
1
For every value for V
1
etc..
, consider all values for V
S
2
Var 1 v1 v2 v3 v4
Var 2
For n variables, each of domain size d
Maximum depth? fixed!
Maximum number of paths? size of search space, size of CSP
Foundations of Constraint Processing, CSCE421/821
Overview 1 46
• Systematic search generates d n possibilities
• Are all possibilities acceptable?
S
Var 1 v1 v2 v3 v4
S
Var 1 v1 v2 v3 v4
Var 2
Var 2
Expand a partial solution only when it is consistent
This yields early pruning of inconsistent paths
Foundations of Constraint Processing, CSCE421/821
Overview 1 47
Systematic search: Chronological backtracking
What if only one solution is needed?
S
Var 1 v1 v2 v3 v4
Var 1 v1 v2
S
Var 2
• Depth-first search & Chronological backtracking
• DFS : Soundness? Completeness?
Foundations of Constraint Processing, CSCE421/821
Overview 1 48
Systematic search: Intelligent backtracking
What if the reason for failure was higher up in the tree?
Backtrack to source of conflict !!
S
S
Var 1 v1 v2
Var 1 v1 v2
Backjumping, conflict-directed backjumping, etc.
Foundations of Constraint Processing, CSCE421/821
Overview 1 49
Ordering heuristics
• Which variable to expand first?
Exp
Sol :
: V
1
,
{( V
1
V
2
=
, D
V
1 c ),
=
( V
2
{ a ,
= b , c , a )} d }, and
D
V
2
{( V
1
=
{
= a , b c ),
}
( V
2 s s
= b )}
V1 a b c d
V2 a b
• Heuristics:
V2 a b a b a b V1 a b c d
– most constrained variable first (reduce branching factor)
– most promising value first (find quickly first solution)
Strategies for variable ordering value ordering could be static
í dynamic
Foundations of Constraint Processing, CSCE421/821
Overview 1 50
• Search tree with only backtrack search?
Root node
Q Q
Q Q 1
Q Q
Q
1
2 3 4
1 2 3 4 1 2 3 4
1 2 3 4
1
2
2 3 4
1
1 2 3
Solution!
26 nodes visited.
Foundations of Constraint Processing, CSCE421/821
Overview 1 51
Forward checking
Search Tree with domains filter by Forward Check
Root node
Q Q
Q Q
Q Q
Q
1
3 4
Domain
2
Wipe Out
V
3
Domain
Wipe Out
V
4
2
4
1
3
Solution!
8 nodes visited.
Foundations of Constraint Processing, CSCE421/821
Overview 1 52
• General purpose methods for
1.
Variable, value ordering
2.
Improving backtracking: intelligent backtracking avoids repeating failure
3.
Look-ahead techniques: propagate constraints as variables are instantiated
Foundations of Constraint Processing, CSCE421/821
Overview 1 53
• K-way branching
– One branch for every value in the domain
• Binary branching
– One branch for the first value
– One branch for all the remaining values in the domain
– Used in commercial constraint solvers: ILOG, Eclipse
• Have different behaviors (e.g., WRT value ordering heuristics [Smith, IJCAI 95] )
Foundations of Constraint Processing, CSCE421/821
Overview 1 54
• Constructive, systematic, exhaustive
– In general sound and complete
• Back-checking: expands nodes consistent with past
• Backtracking: Chronological vs. intelligent
• Ordering heuristics:
– Static
– Dynamic variable
– Dynamic variable-value pairs
• Looking ahead:
– Partial look-ahead
• Forward checking (FC)
• Directional arc-consistency (DAC)
– Full (a.k.a. Maintaining Arc-consistency or MAC)
Foundations of Constraint Processing, CSCE421/821
Overview 1 55
(NP-complete)
1.
Modeling : abstraction and reformulation
2.
Preprocessing techniques :
• eliminate non-acceptable tuples prior to search
3.
Systematic search :
• potentially d n paths of fixed lengths
• chronological backtracking
• intelligent backtracking
• variable/value ordering heuristics
4.
Search ‘hybrids:’
• Mixing consistency checking with search: look-ahead strategies
Foundations of Constraint Processing, CSCE421/821
Overview 1 56
• Iterative repair, local search: modifies a global but inconsistent solution to decrease the number of violated constraints
• Examples :
Hill climbing, taboo search, simulated annealing, GSAT, WalkSAT, Genetic
Algorithms, Swarm intelligence, etc.
• Features : Incomplete & not sound
– Advantage: anytime algorithm
– Shortcoming: cannot tell that no solution exists
Foundations of Constraint Processing, CSCE421/821
Overview 1 57
• We have seen
Motivating example, application areas
CSP: Definition, representation
Some simple modeling examples
More on definition and formal characterization
Basic solving techniques
• We will move to
(Implementing backtrack search)
Advanced solving techniques
Issues & research directions
Foundations of Constraint Processing, CSCE421/821
Overview 1 58
CP = Modeling + Inference + Search
• Variables
• Domains
• Constraints
• Query
• Enforcing consistency
(filtering) and
• Propagation
(E.g., AC, PC, etc.)
• Systematic (chronological backtracking, with/without intelligence, etc.) or
• Non-systematic (hill climbing, taboo, etc.)
Foundations of Constraint Processing, CSCE421/821
Overview 1 59