next_inactive up previous







Computational Logic
Constraint Logic Programming

Constraints

A General View

A comparison with LP (I)

A Comparison with LP (II)

A Comparison with LP (III)

A Comparison with LP (IV)

Example of Search Space Reduction

Example of Search Space Reduction


Generate-and-test Search Tree

\psfig{figure=/home/logalg/public_html/slides/Figs/new_prolog_tree.eps,width=0.9\textwidth}

Example of Search Space Reduction


Constrain-and-generate Search Tree

\psfig{figure=/home/logalg/public_html/slides/Figs/clpr_tree.eps,width=0.8\textwidth}

Constraint Domains


Domains (I)


Domains (II)

Domains (III)

CLP(${\cal X}$) Programs


A case study: CLP($\Re$)

Linear Equations (CLP($\Re$))

Systems of Linear Equations (CLP($\Re$))


Non-linear Equations (CLP($\Re$))


Fibonaci Revisited (Prolog)


Fibonaci Revisited (CLP($\Re$))


Analog RLC circuits (CLP($\Re$))

Analog RLC circuits (CLP($\Re$))

Analog RLC circuits (CLP($\Re$))

Analog RLC circuits (CLP($\Re$))

Each basic component can be modeled as a separate unit:

Analog RLC circuits (CLP($\Re$))


The N Queens Problem

The N Queens Problem (Prolog)


queens(N, Qs) :- queens_list(N, Ns), queens(Ns, [], Qs).
queens([], Qs, Qs).
queens(Unplaced, Placed, Qs) :-
    select(Unplaced, Q, NewUnplaced), no_attack(Placed, Q, 1),
    queens(NewUnplaced, [Q|Placed], Qs).
no_attack([], _Queen, _Nb).
no_attack([Y|Ys], Queen, Nb) :-
    Queen =\= Y + Nb, Queen =\= Y - Nb, Nb1 is Nb + 1,
    no_attack(Ys, Queen, Nb1).
select([X|Ys], X, Ys).
select([Y|Ys], X, [Y|Zs]) :- select(Ys, X, Zs).
queens_list(0, []).
queens_list(N, [N|Ns]) :- N > 0, N1 is N - 1, queens_list(N1, Ns).

The N Queens Problem (Prolog)

\psfig{figure=/home/logalg/public_html/slides/Figs/queens.eps,height=0.85\textheight}

The N Queens Problem (CLP($\Re$))


queens(N, Qs) :- constrain_values(N, N, Qs), place_queens(N, Qs).
constrain_values(0, _N, []).
constrain_values(N, Range, [X|Xs]) :-
        N > 0, X > 0, X <= Range, 
        constrain_values(N - 1, Range, Xs), no_attack(Xs, X, 1).
no_attack([], _Queen, _Nb).
no_attack([Y|Ys], Queen, Nb) :-
        abs(Queen - (Y + Nb)) > 0,  % Queen =\= Y + Nb
        abs(Queen - (Y - Nb)) > 0,  % Queen =\= Y - Nb
        no_attack(Ys, Queen, Nb + 1).
place_queens(0, _).
place_queens(N, Q) :- N > 0, member(N, Q), place_queens(N - 1, Q).

member(X, [X|_]).
member(X, [_|Xs]) :- member(X, Xs).

The N Queens Problem (CLP($\Re$))


The N Queens Problem (CLP($\Re$))

\psfig{figure=/home/logalg/public_html/slides/Figs/queens_clp.eps,height=0.57\textwidth}

The N Queens Problem (CLP($\Re$))

The N Queens Problem (CLP($\Re$))

Finite Domains (I)

Finite Domains (II)

 $$
Example:
?- X #= A + B, A in 1..3, B in 3..7.
X in 4..10, A in 1..3, B in 3..7
 $$
The respective minimums and maximums are added
 $$
There is no unique solution
?- X #= A - B, A in 1..3, B in 3..7.
X in -6..0, A in 1..3, B in 3..7
 $$
The minimum value of X is the minimum value of A minus the maximum value of B
 $$
(Similar for the maximum values)
 $$
Putting more constraints:
?- X #= A - B, A in 1..3, B in 3..7, X #>= 0.
A = 3, B = 3, X = 0

Finite Domains (III)

Some useful primitives in finite domains:

 $$
fd_min(X, T): the term T is the minimum value in the domain of the variable X
 $$
This can be used to minimize (c.f., maximize) a solution
?- X #= A - B, A in 1..3, B in 3..7, fd_min(X, X).
A = 1, B = 7, X = -6

 $$
domain(Variables, Min, Max): A shorthand for several in constraints

 $$
labeling(Options, VarList):
?- X*X+Y*Y#=Z*Z, X#>=Y, domain([X, Y, Z],1,1000),labeling([],[X,Y,Z]).
X = 4, Y = 3, Z = 5
X = 8, Y = 6, Z = 10
X = 12, Y = 5, Z = 13
...

A Project Management Problem (I)

A Project Management Problem (II)

 $$
Query:
?- pn1(A,B,C,D,E,F,G).
A in 0..4, B in 0..5, C in 0..4,
D in 0..6, E in 2..6, F in 3..9, G in 6..10,

 $$
Note the slack of the variables
 $$
Some additional constraints must be respected as well, but are not shown by default
 $$
Minimize the total project time:
?- pn1(A,B,C,D,E,F,G), fd_min(G, G).
A = 0, B in 0..1, C = 0, D in 0..2,
E = 2, F in 3..5, G = 6
 $$
Variables without slack represent critical tasks

A Project Management Problem (III)

 $$
An alternative setting:
\psfig{figure=/home/logalg/public_html/slides/Figs/prec_net_2.eps,width=0.36\textheight}

 $$
We can accelerate task F at some cost

pn2(A, B, C, D, E, F, G, X) :-
A #>= 0, G #=< 10,
B #>= A, C #>= A, D #>= A,
E #>= B + 1, E #>= C + 2,
F #>= C + 2, F #>= D + 3,
G #>= E + 4, G #>= F + X.
 $$
We do not want to accelerate it more than needed!
?- pn2(A, B, C, D, E, F, G, X),
fd_min(G,G), fd_max(X, X).
A = 0, B in 0..1, C = 0, D = 0,
E = 2, F = 3, G = 6, X = 3

A Project Management Problem (IV)

A Project Management Problem (V)


 $$
Constraints describing the net:
pn3(A,B,C,D,E,F,G,X,Y) :-
A #>= 0, G #=< 10,
X #>= 2, Y #>= 2, X + Y #= 6,
B #>= A, C #>= A, D #>= A,
E #>= B + X, E #>= C + 2,
F #>= C + 2, F #>= D + Y,
G #>= E + 4, G #>= F + 1.
 $$
Query: ?- pn3(A,B,C,D,E,F,G,X,Y), fd_min(G,G).
A = 0, B = 0, C = 0, D in 0..1, E = 2, F in 4..5, X = 2, Y = 4, G = 6
 $$
I.e., we must devote more resources to task B
 $$
All tasks but F and D are critical now
 $$
Sometimes, fd_min/2 not enough to provide best solution (pending constraints):
pn3(A,B,C,D,E,F,G,X,Y),
labeling([ff, minimize(G)], [A,B,C,D,E,F,G,X,Y]).

The N-Queens Problem Using Finite Domains (in SICStus Prolog)


CLP(${\cal FT}$) (a.k.a. Logic Programming)

CLP(${\cal WE}$)

CLP((${\cal WE, Q}$))

Summarizing

Complex Constraints


Other Primitives

Implementation Issues: Satisfiability

Implementation Issues: Backtracking in CLP(${\cal X}$)


\psfig{figure=/home/logalg/public_html/slides/Figs/timestamping.eps,width=0.8\textwidth}

Implementation Issues: Extensibility

Attributed Variables Example: Freeze


Programming Tips


Some Real Systems (I)


Some Real Systems (II)


Some Real Systems (III)



next_inactive up previous
Last modification: Wed Nov 22 23:38:08 CET 2006 <webmaster@clip.dia.fi.upm.es>[CLIP] [FIM] [UNM]