More global constraints Ulf Nilsson IDA, Linköping university

advertisement
More global constraints
Ulf Nilsson
IDA, Linköping university
ulfni@ida.liu.se
Encoding digraphs
X2
X3
X1
X5
X4
X1
X2
X3
X4
X5
in
in
in
in
in
5..5,
{1,5},
2..2,
{2,3},
4..4
Circuit/1
Find a Hamiltonian circuit in a digraph.
?- X1 in 5..5,
X1
X2 in {1,5},
X3 in 2..2,
X4 in {2,3},
X5 in 4..4,
circuit([X1,X2,X3,X4,X5]).
X1=5
X2=1
X3=2
X2
X4
X5
X4=3
X3
X5=4
Cycle/2 (CHIP only)
X1
graph(L) :X6
L = [X1,X2,X3,X4,X5,X6],
X1 :: [2,6], X2 :: [3,4],
X5
X3 :: [1], X4 :: [2,3],
X5 :: [2,6], X6 :: [2,5],
cycle(2, L), labeling(L).
?- graph(L).
L = [2,4,1,3,6,5]
X3
X2
X4
Among/5 (CHIP only)
Each 20-day period the staff must have 4 breaks.
Each break must be 2-4 days.
The staff must work no more than 3 days in a row.
There must be at least 10 days off.
group
Among/5
among([Nmin,Nmax,Smin,Smax,Dmin,Dmax,Tmin,Tmax],
[V1,...,Vn],
[C1,...,Cn],
[V1,...,Vi],
all)
Nmin - Nmax groups
Smin - Smax size of each group
Dmin - Dmax distance between groups
Tmin - Tmax total size of groups
V1,...,Vi are group elements
Among/5
?- [X1,...,X20] :: 0..1,
among([4, 4, 2, 4, 1, 3, 10, 20],
[X1,...,X20],
[0,...,0],
[0],
all).
Reducing search
Ulf Nilsson
IDA, Linköping university
ulfni@ida.liu.se
Outline
Constrain-and-generate
Disjunctive constraints
Reified constraints
Redundant constraints
Local search strategies
NP-complete problems
Characteristic features of CLP(FD)
problems:
The depth of the computation which leads to
a solution is often polynomially bounded
There may be exponentially many choices
Try to avoid as many choices as possible,
or delay choices as late as possible
Generate and test
nqueens(N, L) :intlist(1, N, Diag),
permutation(Diag, L),
safe(L).
safe([]).
safe([X|Xs]) :no_attack(X, Xs, 1),
safe(Xs).
...
Search tree
Constrain and generate
nqueens(N, L) :length(L, N),
safe(L),
labeling([], L).
safe([]).
safe([X|Xs]) :no_attack(X, Xs, 1),
safe(Xs).
...
Constrain and generate
Create variable
A constraint
store
Set up the constraints
Search
Another
constraint
store
Disjunctive problem
Some problems are disjunctive by nature
sequence(task(S1, D1), task(S2, D2)) :S1 + D1 #=< S2.
sequence(task(S1, D1), task(S2, D2)) :S2 + D2 #=< S1.
Disjunctive constraints
sequence(task(S1, D1), task(S2, D2)) :(S1 + D1 #=< S2) #\/ (S2 + D2 #=< S1).
Global constraints
Global constraints provide efficient
propagation algorithms for disjunctive
problems:
sequence(task(S1, D1), task(S2, D2)) :cumulative([S1, S2], [D1, D2], [1, 1], 1).
Avoiding disjunction
x
contact(X1, X2) :- X1+1 #= X2.
contact(X1, X2) :- X1 #= X2+1.
contact(X1, X2) :- abs(X1-X2) #= 1.
Reified constraints
Assume that we want precisely two
of C1, C2 and C3 to hold
two(C1, C2, C3) :- C1, C2, ~C3.
two(C1, C2, C3) :- C1, ~C2, C3.
two(C1, C2, C3) :- ~C1, C2, C3.
Reified constraints
The (reified) constraint:
Constraint #<=> B
holds when (1) B=1 and Constraint is
entailed by the store, or (2) B=0 and
~Constraint is entailed by the store.
Ask and tell constraints
A tell-constraint is added to the store,
(which may become inconsistent). E.g.
sat(~A*B).
Ask-constraints are used to check the
state of the store. They are not added to
the store. E.g. taut(~A*B, 1).
Reified constraints are generalized askconstraints!
Example
two(C1, C2, C3) :B1 in 0..1,
B2 in 0..1,
B3 in 0..1,
C1 #<=> B1,
C2 #<=> B2,
C3 #<=> B3,
B1 + B2 + B3 #= 2.
Example
sequence(tast(S1, D1), task(S2, D2)) :(S1 + D1 #=< S2) #<=> B1,
(S2 + D2 #=< S1) #<=> B2,
B1 + B2 #= 1.
or(C1, C2) :C1 #<=> B1,
C2 #<=> B2,
B1 + B2 #= 1.
Example
exactly([], 0).
exactly([C|Cs], N) :C #<=> B,
N #= B + M,
exactly(Cs, M)
Reified constraints IV
count(_, [], 0).
count(X, [Y|Ys], N) :(X #= Y) #<=> B,
N #= M+B,
count(X, Ys, M).
?- X in 1..2, X1 in 0..2,
X2 in 2..4, count(X, [X1,X2], 2).
X = X1 = X2 = 2
Redundant constraints
Constraints that do not contribute to the
solution(s), ...
...but may prune the search space,...
...or improve the propagation in case of
incomplete solvers.
Can also be used to prune symmetric
solutions.
Redundant constraint II
The constraint:
X #< Z
is redundant in the presence of
X #< Y , Y #< Z
More constraints may improve the
propagation, but may impose more work.
Example: Protein folding
A protein is a string of amino-acids (monomers):
H Hydrophobic
P Polar
For instance:
H P H P P HH P H P P H P HH P P H P H
2-D lattice model
P
H
P
H
H
P
P
H
H
P
H
H
H
H P
P
P
P
H P
Energy: -9
-1
-2
-3
-4
-6
-7
-8
-5
Constraint problem?
Constraints:
Self-avoidance;
Unit distance between adjacent monomers;
Optimisation problem:
Minimize energy induced by non-local
contacts between H-monomers;
Variables
We associate coordinates (Xi ,Yi) with each
monomer i;
We associate a boolean contact variable
Cij with each pair of non-adjacent Hmonomers;
Constraints
Adjacent monomers must be distance 1 apart:
DX in 0..1, DX #= abs(Xi - Xi+1),
DY in 0..1, DY #= abs(Yi - Yi+1),
DX + DY #= 1
Self avoidance (for all i and j s.t. i < j):
abs(Xi - Xj) + abs(Yi - Yj) #> 0
Non-local H-contacts
Modelling potential contacts between non-adjacent
H-monomers:
DX #= abs(Xi - Xj),
DY #= abs(Yi - Yj),
Cij in 0..1,
(DX + DY #= 1) #<=> Cij
Minimize sum of all (-1 * Cij).
Constrain and generate
fold(String) :length(String, N1), N is 2 * N1,
create_coordinates(String, Xs, Ys),
create_contacts(String, Xs, Ys, Contacts),
domain(Xs, 0, N),
domain(Ys, 0, N),
setup_initial(Xs, Ys, N1, N1),
setup_distance(Xs, Ys),
setup_different(Xs, Ys),
setup_energy(Contacts, Xs, Ys, Energy),
merge(Xs, Ys, Coord),
labeling([ff, minimize(Energy)], Coord).
Redundant constraints
There can be no contact between Hmonomers in even (or odd) positions.
Internal H-monomers can have at most 2
non-local H-contacts. H-monomers at the
ends can have at most three H-contacts.
Search strategies
Branch & bound
optimal
optimal
No pruning
Incomplete strategies
Local search
random or steepest descent
tabu search
Monte carlo search
simulated annealing
random walk
genetic algorithms
Local search
S, S’: solutions
N(S): solutions in the neighborhood of S
F: objective function
S1
S5
S0
S4
S2
S3
N(S0)
Local search scheme
1.
2.
3.
Select initial solution S
Let S be some solution in N(S)
Repeat 2 until some stop criterion
Random descent
1.
2.
3.
4.
Let S be initial solution
Select S’ in N(S) such that F(S’) > F(S)
Let S := S’
Repeat steps 2-3 until local optimum
Steepest descent
1.
2.
3.
4.
Let S be initial solution
Select S’ in N(S) such that:
(i) F(S’) > F(S)
(ii) F(S’) >= F(S’’) for all S’’ in N(S)
Let S := S’
Repeat steps 2-3 until local optimum
Avoiding local optima
Extend the neighborhood
Repeat the whole procedure starting from
alternative initial solutions (iterated
descent)
Make the neighborhood dependent on the
history; forbid certain moves (tabu
search)
Monte Carlo search
1.
2.
3.
4.
5.
Let S be an initial solution
Let S’ be a random solution in N(S)
Let S := S’ if F(S’) > F(S)
Otherwise let S := S’ with a probability
monotone in F(S’) - F(S)
Repeat 2-5 until some stop condition
Monte carlo methods
Simulated annealing: The probability of
bad moves is reduced as the search
proceeds;
Random walk: Let p be a low probability.
Make an improving move with probability
1-p; make a completely random move
with probability p.
Download