Title: CLP Principles
1CLP Principles
- H. Simonis
- COSYTEC SA
- 4, rue Jean Rostand
- F-91893 Orsay Cedex
- helmut_at_cosytec.fr
2Outline
- Background on CLP
- Simple Examples
- Global Constraints
- Search
3What is common among
- the production of Mirage 2000 fighter aircraft
- the personnel planning for the guards in all
French jails - the production of Belgian chocolates
- the selection of the music programme of a Pop
music radio station - the design of advanced signal processing chips
- the print engine controller in Xerox copiers
They all use constraint programming to solve
their problem
4Constraint Programming - in a nutshell
- Declarative description of problems with
- Variables which range over (finite) sets of
values - Constraints over subsets of variables which
restrict possible value combinations - A solution is a value assignment which satisfies
all constraints - Constraint propagation/reasoning
- Removing inconsistent values for variables
- Detect failure if constraint can not be satisfied
- Interaction of constraints via shared variables
- Incomplete
- Search
- User controlled assignment of values to variables
- Each step triggers constraint propagation
- Different domains require/allow different methods
5Techniques behind CLP
Predicate logic Unification Non-determinism
Logic Programming
Constraint propagation Consistency checking Demons
CLP tools
Operations Research
Artificial Intelligence
Simplex Linear programming Integer
programming Implicit enumeration Branch
bound Flow algorithms Scheduling methods
Graph theory Combinatorics Spatial data
structures Equation solving methods
Mathematics
6Example Problem
- Solve the cryptarithmetic puzzle
- Each character represents a digit
- Different characters have different values
- Numbers do not start with 0
SEND MORE ------ MONEY
7Example Model
- top-
- S, E, N, D, M, O, R, Y 0..9,
- alldifferent(S, E, N, D, M, O, R, Y),
- S \ 0, M \ 0,
- 1000 S 100E 10N D
- 1000M 100O 10R E
- 10000 M 1000O 100N 10E Y,
- labeling(S, E, N, D, M, O, R, Y).
Variable definition
Constraints between variables
Search routine
8Constraint reasoning
- Simplification (each variable occurs once)
- 1000S in 1..9 91E in 0..9 D in 0..9
10R in 0..9 9000M in 1..9 900O in
0..9 90N in 0..9 Y in 0..9 - Evaluation lhs/rhs
- lhs in 1000..9918
- rhs in 9000..89919
- Merging of sides
- constraint in 9000..9918
9Reasoning
- Consequence
- M 1
- S 9
- O in 0..1
- Propagation of alldifferent
- O 0
10Reasoning
- Re-evaluation of equality
- 10009 91E in 2..8 D in 2..8 10R in
2..8 90001 9000 90N in 2..8 Y in
2..8 - lhs in 9204..9816, rhs in 9182..9728, eq in
9204..9728 - N \ 2, E \ 8
- Re-evaluation, ...
- Continuing the process gives
- M 1, S 9, O 0, E in 4..7, N in 5..8, D in
2..8, R in 2..8, Y in 2..8
11Starting labeling
- First variable is E, first value is 4
- Propagation on equality gives
- 10009 914 D in 2..8 10R in 2..8
90001 9000 90N in 5..8 Y in 2..8 - results in N 5, D 8, R 8, Y 2
- Propagation on alldifferent fails
- Backtracking to last choice
12First alternative
- Next value for E is 5
- Propagation on equality gives
- 10009 915 D in 2..8 10R in 2..8
90001 9000 90N in 5..8 Y in 2..8 - results in N 6, R 8
- Constraint propagation (alldifferent equality)
gives - D 7, Y 2
13Points to remember
- Even small problems create complex propagation
chains - The same constraint can be woken several times in
the same propagation loop - The order in which constraints are woken has an
influence on the speed - Propagation continues until no further
information is obtained - Search (under user control) required to find
ground solution - Basic structure of finite domain program always
the same - define variables
- define constraints
- user defined search
- typical pick variable and find value
14Example 2 The N-queens problem
- Place queens on a NxN chessboard so that they do
not attack each other - The classical constraints example
- Not a hard problem possible to construct generic
solutions - Used here show the impact of the search routine
15Model
- run(N)-
- length(L, N),
- L 1..N,
- create_dif(L, 1, N, L1, L2),
- alldifferent(L),
- alldifferent(L1),
- alldifferent(L2),
- labeling(L).
- create_dif(, N, M, , ).
- create_dif(HT, N, M, HNR, HMS)-
- N1 is N1,
- M1 is M-1,
- create_dif(T, N1, M1, R, S).
16Naïve search
- labeling().
- labeling(XY) -
- indomain(X),
- labeling(Y).
17Results
first hard instance 22
18Searchtree (N22)
19First fail
- label().
- label(XY) -
- delete(Var,XY,Rest,0,first_fail),
- indomain(Var),
- label(Rest).
20Results
First hard instance 80
21Searchtree (N80)
22Heuristic reordering
- run(N)-
- length(L, N),
- L 1..N,
- create_dif(L, 1, N, L1, L2),
- alldifferent(L),
- alldifferent(L1),
- alldifferent(L2),
- reorder(L, LL),
- label(LL).
- reorder(L, L1)-
- front_rear(L, L, , F, R),
- merge_it(F, R, L1).
- front_rear(R, , F, F, R).
- front_rear(R, _, F, F, R).
- front_rear(HT, _, _Q, F, Fend, Rear)-
- front_rear(T, Q, HF, Fend, Rear).
- merge_it(, , ).
- merge_it(, A, A).
- merge_it(AA1, BB1, A, BC1)-
- merge_it(A1, B1, C1).
- label().
- label(XY) -
- delete(Var, XY, Rest, 0, first_fail),
- indomain(Var, middle),
- label(Rest).
23Results
Exceptional hard instances 108, 168
24Searchtree (N108)
25Credit based partial search
- label(L)-
- length(L,K),
- Credit is KK,
- credit(L, Credit, K, choose, choice, 5,
part(1,2)). - choose(Term, LTerm, RTerm)-
- delete(Term, LTerm, RTerm, 0,first_fail).
- choice(X)-
- indomain(X,middle).
26Results
runs up to several thousand queens with less than
10 backtracking steps
27Searchtree (N108)
28Global Constraints
29Need for global constraints
1
X
X in 2,3
Y
2
Y in 2,3
?
U in 1,2,3,4
Z
3
Z in 1,3
U
4
local reasoning, no action
global reasoning, detect implications by
bi-partite matching
30Global constraints
- Work on sets of variables
- Global conditions, not local constraints
- Semantic methods
- Operations Research
- Spatial algorithms
- Graph theory
- Network flows
- Building blocks (high-level constraint
primitives) - Multi-purpose
- As general as possible
- Usable with other constraints
- Very strong propagation
- Acceptable algorithmic complexity
31Constraint morphology
precedence
diffn
cumulative
sequence
cycle
case
among
alldifferent
setup
disjunctive
prod/cons
permutation
\
gt, distance
atmost, atleast
circuit
element
Different
Dependency
Tour
Resource
Order
32The Cumulative global constraint
- Cumulative constraint
- Resource limits over periods of time
- Upper/lower limits
- Soft/hard limits
- Gradual constraint relaxation
- Application
- Resource restrictive scheduling, producer
consumer constraints, disjunctive schedule,
manpower constraints, overtime
33Cumulative
- Methods
- obligatory parts
- task intervals
- available space
- many more (25000 lines of C code)
- Concepts
- one constraint may be used for different purposes
34The Diffn global constraint
- Diffn constraint
- non overlapping areas on n-dimensional rectangles
- distances between rectangles
- limit use of areas
- relaxation
- Application
- layout, packing, resource assignment, setup,
distribution planning, time-tabling
35Diffn
- Methods
- obligatory parts
- region intervals
- max flow on assignment
- available space
- many others (32000 lines of C code)
- Concepts
36The Cycle global constraint
- Cycle constraint
- Finds cycles in directed graphs with minimal cost
- Assign resources, find compatible start dates
- Applications
- Tour planning, personnel rotation, distribution
problems, production sequencing
37Cycle
- Methods
- connected components
- bi-partite matching
- non-oriented graph concepts
- shortest/longest paths
- micro-rules
- many others (38000 lines of C code)
- Concepts
38The Among global constraint
- Among constraint
- How often do values occur in (sub)sequences
- based on counting arguments
- interaction between sequences
- Applications
- production sequencing, time tabling, coloring
problems, set covering
39The Precedence global constraint
- Precedence constraint
- Combine resource constraints and precedence
networks - Reasoning on latency (position in network)
- Co-operation between multiple resources
- Applications
- resource restricted scheduling, channel routing,
frequency allocation
40The Sequence global constraint
sequence
lt 40 hours
2 days off
after day with more than 10 hours, next day must
have less than 8 hours
- Sequence constraint
- constraints on pattern inside sequences
- combinatorial pattern matching
- counting arguments
- Applications
- Time tabling, personnel assignment,
- work rules, scheduling with daily working time
limits
41The power of global constraints
plan
schedule
- Multi-functional tools
- Building blocks
assign
42Controlling Search
43Search tree visualization
- Generation of search tree representation at
run-time - Shows parent child relation, failed sub-trees,
success nodes - In examples here
- leaf failure nodes suppressed
- failure trees not collapsed
- Interface a set of simple meta-call predicates
- Supported by work in DISCIPL Esprit project
- Declarative debugging
- Visualization of constraint programming results
- Understand behaviour of different strategies
44Search tree tool
Menubar
Panel
Tree view
Other Views, here domain state
Info Area
45Search strategies
- How to find values for variables
- Central to application of strategies/heuristics
- Chronological backtracking
- explores full search tree
- complete
- often stuck in one part of tree
- Partial search meta-heuristics
- different search methods
- not complete
- polynomial complexity
- used to explore different parts of search tree in
systematic fashion - uses normal variable and value selection criteria
46Credit based search
- Systematic search at top of tree
- Limited amount of backtracking credit
- typical N, N2, N3
- Distribute credit to children in different ways
- preference on first child
- equal credit to all children
- If credit runs out, perform deterministic search
- Allow small amount of local search to overcome
problems
47Example of tree search
credit(X1..X10, 8, 10, my_delete,
my_indomain, 4, part(1,2)),
48Credit Example