:- module(_,_).
%% * CLP: a first comparison with classic Logic Programming
%! \begin{focus}
q(X, Y, Z) :- Z = f(X, Y).
%! \end{focus}... is fully reversible:?- q(3, 4, Z).
?- q(X, Y, f(3,4)).
?- q(X, Y, Z).
p(X, Y, Z) :- Z is X + Y.... is not reversible (as we know)! This works:
?- p(3, 4, Z).... but this does not:
?- p(X, 4, 7).
:- module(_,_). :- use_package(clpr). % You can also try: :- use_package(clpq). p(X, Y, Z) :- Z .=. X + Y.With constraints, arithmetic is reversible:
?- p(3, 4, Z).
?- p(X, 4, 7).
?- p(X, Y, 7).Note that in this last case we get a constraint back.
:- module(_,_).
%% * CLP and search space reduction
%% Generate and test vs. constrain and generate
%! \begin{focus}
p(11). p(3). p(7). p(16). p(15). p(14).
%! \end{focus} solution(X, Y, Z) :-
p(X),
p(Y),
p(Z),
test(X, Y, Z).
test(X, Y, Z) :- Y is X + 1, Z is Y + 1.Try (asking for more solutions):?- solution(X, Y, Z).458 steps (and 475 steps for all solutions, i.e., to say no at the end).
:- module(_,_). :- use_package(clpr). % You can also try: :- use_package(clpq). p(11). p(3). p(7). p(16). p(15). p(14).
solution(X, Y, Z) :-
p(X),
p(Y),
p(Z),
test(X, Y, Z).
test(X, Y, Z) :- Y .=. X + 1, Z .=. Y + 1.Try (asking for more solutions):?- solution(X, Y, Z).Still 458 steps (and 475 steps for all solutions, i.e., to say no at the end).
:- module(_,_). :- use_package(clpr). % You can also try: :- use_package(clpq). p(11). p(3). p(7). p(16). p(15). p(14).
solution(X, Y, Z) :-
test(X, Y, Z),
p(X),
p(Y),
p(Z).
test(X, Y, Z) :- Y .=. X + 1, Z .=. Y + 1.Try (asking for more solutions):?- solution(X, Y, Z).In 11 steps (and also 11 steps for all solutions, i.e., to say no at the end).
solution_prolog(X, Y, Z) :-
test_prolog(X, Y, Z),
p(X),
p(Y),
p(Z).
test_prolog(X, Y, Z) :- Y is X + 1, Z is Y + 1.?- solution_prolog(X, Y, Z).Does not work!
solution_gen_test_prolog(X, Y, Z) :-
q(X),
q(Y),
q(Z),
test_prolog(X, Y, Z).?- time(solution_gen_test_prolog(X, Y, Z)).Takes much longer!
solution_gen_test_clp(X, Y, Z) :-
q(X),
q(Y),
q(Z),
test(X, Y, Z).?- time(solution_gen_test_clp(X, Y, Z)).Actually, takes longer than generate and test in standard Prolog (constraint solving does have overhead).
solution_cons_gen_clp(X, Y, Z) :-
test(X, Y, Z),
q(X),
q(Y),
q(Z).?- time(solution_cons_gen_clp(X, Y, Z)).Much better!
%! \begin{focus}
solution_gen_and_early_test_prolog(X, Y, Z) :-
q(X),
Y is X + 1,
q(Y),
Z is Y + 1,
q(Z).
%! \end{focus}
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(11). q(3). q(7). q(16). q(15). q(14).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).
q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0). q(0).?- time(solution_gen_and_early_test_prolog(X, Y, Z)).Works quite well! But can be harder / more work than constrain and generate in realistic cases.
(x₁,x₂, …, xn) . (y₁, y₂, …, yn) = x₁ . y₁ + x₂ . y₂ + … + xn . yn
:- module(_,_).
:- use_package(clpr).
% You can also try: :- use_package(clpq).
prod([], [], Result) :-
Result .=. 0.
prod([X|Xs], [Y|Ys], Result) :-
Result .=. X * Y + Rest,
prod(Xs, Ys, Rest). ?- prod([2, 3], [4, 5], K).
?- prod([2, 3], [4, Y2], 23).
?- prod([2, 7, 3], [Vx, Vy, Vz], 0).
?- prod([Y,2,3],[4,X,6],32).
?- prod([3, 1], [X, Y], 5), prod([1, 8], [X, Y], 3).
:- use_package(clpr).
% You can also try: :- use_package(clpq).
%! \begin{focus}
system([], _Vars, []).
system([Co|Coefs], Vars, [Ind|Indeps]) :-
prod(Co, Vars, Ind),
system(Coefs, Vars, Indeps).
%! \end{focus}
prod([], [], Result) :-
Result .=. 0.
prod([X|Xs], [Y|Ys], Result) :-
Result .=. X * Y + Rest,
prod(Xs, Ys, Rest). ?- system([[3, 1],[1, 8]], [X, Y], [5, 3]).Note that we are using both Herbrand terms and constraints.
?- use_package(clpr).
?- sin(X) .=. cos(X).
?- X*X + 2*X + 1 .=. 0.Reason: no general solving technique is known. CLP(R) solves only linear (dis)equations.
?- X .=. cos(sin(Y)), Y .=. 2+Y*3.
?- X + Y .=<. 4, Y .>=. 4, X .>=. 0.
:- module(_,_).
% -------------------------------------------------------------------
% Fibonacci revisited
%! \begin{focus}
fib(0, 0).
fib(1, 1).
fib(N, F) :-
N > 1,
N1 is N - 1,
N2 is N - 2,
fib(N1, F1),
fib(N2, F2),
F is F1 + F2.
%! \end{focus} ?- fib(11, F).
?- fib(N, 89).(Gives error.)
?- fib(N, F).(Also gives error, at third solution.)
:- module(_,_).
:- use_package(clpr).
% You can also try: :- use_package(clpq).
fib(N, N) :- N .=. 0.
fib(N, N) :- N .=. 1.
fib(N, R) :-
N .>. 1, F1 .>=. 0, F2 .>=. 0,
N1 .=. N - 1, N2 .=. N - 2,
fib(N1, F1),
fib(N2, F2),
R .=. F1 + F2. ?- fib(11, F).
?- fib(N, 89).Both works...
?- fib(N, F).Also works! (ask for more solutions)
:- module(_,_).
:- use_package(clpr).
% Or, we can use clpq for exact calculations:
% :- use_package(clpq).
mg(P, T, I, B, MP):-
T .=. 1,
B + MP .=. P * (1 + I).
mg(P, T, I, B, MP):-
T .>. 1,
P1 .=. P * (1 + I) - MP,
T1 .=. T - 1,
mg(P1, T1, I, B, MP). ?- mg(1000, 30, 0.03, 0, MP).
?- mg(1000, 30, 0.03, 0, MP).
?- mg(P, 30, 0.03, 0, 20).
?- mg(1000, 30, 0.03, B, 0).
?- mg(1000, 30, 0.03, B, -50).
:- module(_,_).
:- use_package(clpr).
% Or, we can use clpq for exact calculations
% :- use_package(clpq).
mg(P, T, I, B, MP):-
T .=. 1,
B + MP .=. P * (1 + I).
mg(P, T, I, B, MP):-
T .>. 1,
P1 .=. P * (1 + I) - MP,
T1 .=. T - 1,
mg(P1, T1, I, B, MP).
%! \begin{focus}
%! \end{focus} Do some exercises simulating a population: exercise! ▶
:- module(_,_).
%----------------------------------------
% RLC Analog Circuits
%! \begin{focus}
:- use_package(clpr).
c_add(c(Re1,Im1), c(Re2,Im2), c(Re12,Im12)) :-
Re12 .=. Re1+Re2,
Im12 .=. Im1+Im2.
%! \end{focus} c_mult(c(Re1, Im1), c(Re2, Im2), c(Re3, Im3)) :-
Re3 .=. Re1 * Re2 - Im1 * Im2,
Im3 .=. Re1 * Im2 + Re2 * Im1. Try:
?- c_add(c(1,2), c(3,4), X).
?- c_mult(c(1,2), c(3,4), X).Thanks to the constraints, c_mult/3 also divides:
?- c_mult(c(1,2), X, c(-5,10)).
:- module(_,_).
:- use_package(clpr).
%! \begin{focus}
circuit(series(N1, N2), V, I, W) :-
c_add(V1, V2, V),
circuit(N1, V1, I, W),
circuit(N2, V2, I, W).
%! \end{focus} %! \begin{focus}
circuit(parallel(N1, N2), V, I, W) :-
c_add(I1, I2, I),
circuit(N1, V, I1, W),
circuit(N2, V, I2, W).
%! \end{focus}
circuit(resistor(R), V, I, _W) :-
c_mult(I, c(R, 0), V).
circuit(inductor(L), V, I, W) :-
Im .=. W * L,
c_mult(I, c(0, Im), V).
circuit(capacitor(C), V, I, W) :-
Im .=. -1 / (W * C),
c_mult(I, c(0, Im), V).
c_add(c(Re1,Im1), c(Re2,Im2), c(Re12,Im12)) :-
Re12 .=. Re1+Re2,
Im12 .=. Im1+Im2.
c_mult(c(Re1, Im1), c(Re2, Im2), c(Re3, Im3)) :-
Re3 .=. Re1 * Re2 - Im1 * Im2,
Im3 .=. Re1 * Im2 + Re2 * Im1. ?- circuit(series(resistor(10), resistor(20)), V, c(1,0), 50).
?- circuit(parallel(resistor(10), resistor(10)), c(10,0), I, 50).
:- module(_,_).
:- use_package(clpr).
%! \begin{focus}
circuit(resistor(R), V, I, _W) :-
c_mult(I, c(R, 0), V).
%! \end{focus} circuit(inductor(L), V, I, W) :-
Im .=. W * L,
c_mult(I, c(0, Im), V). %! \begin{focus}
circuit(capacitor(C), V, I, W) :-
Im .=. -1 / (W * C),
c_mult(I, c(0, Im), V).
%! \end{focus}
circuit(series(N1, N2), V, I, W) :-
c_add(V1, V2, V),
circuit(N1, V1, I, W),
circuit(N2, V2, I, W).
circuit(parallel(N1, N2), V, I, W) :-
c_add(I1, I2, I),
circuit(N1, V, I1, W),
circuit(N2, V, I2, W).
c_add(c(Re1,Im1), c(Re2,Im2), c(Re12,Im12)) :-
Re12 .=. Re1+Re2,
Im12 .=. Im1+Im2.
c_mult(c(Re1, Im1), c(Re2, Im2), c(Re3, Im3)) :-
Re3 .=. Re1 * Re2 - Im1 * Im2,
Im3 .=. Re1 * Im2 + Re2 * Im1. ?- circuit(resistor(10), V, c(2,0), 50).
?- circuit(inductor(0.1), V, c(1,0), 50).
?- circuit(capacitor(0.01), V, c(1,0), 50).
Calculating the C and R values needed in the given circuit to get the given voltage and current at the given frequency:
?- circuit(parallel(inductor(0.073), series(capacitor(C), resistor(R))), c(4.5, 0), c(0.65, 0), 2400).Or even looking for circuits that do it:
?- circuit(C, c(4.5, 0), c(0.65, 0), 2400).
:- module(_,_).
:- use_package(clpr).
c_add(c(Re1,Im1), c(Re2,Im2), c(Re12,Im12)) :-
Re12 .=. Re1+Re2,
Im12 .=. Im1+Im2.
c_mult(c(Re1, Im1), c(Re2, Im2), c(Re3, Im3)) :-
Re3 .=. Re1 * Re2 - Im1 * Im2,
Im3 .=. Re1 * Im2 + Re2 * Im1.
circuit(resistor(R), V, I, _W) :-
c_mult(I, c(R, 0), V).
circuit(inductor(L), V, I, W) :-
Im .=. W * L,
c_mult(I, c(0, Im), V).
circuit(capacitor(C), V, I, W) :-
Im .=. -1 / (W * C),
c_mult(I, c(0, Im), V).
circuit(parallel(N1, N2), V, I, W) :-
c_add(I1, I2, I),
circuit(N1, V, I1, W),
circuit(N2, V, I2, W).
circuit(series(N1, N2), V, I, W) :-
c_add(V1, V2, V),
circuit(N1, V1, I, W),
circuit(N2, V2, I, W).
%! \begin{focus}
%! \end{focus}
is represented as [2, 4, 1, 3]
%----------------------------------------
% The N Queens Problem in standard Prolog
% - We represent the state as a list holding in which column the queen
% is placed for each row. E.g., for 4 queens:
%
% -----------------
% | | Q | | |
% -----------------
% | | | | Q |
% -----------------
% | Q | | | |
% -----------------
% | | | Q | |
% -----------------
%
% is represented as `[2, 4, 1, 3]`.
% - General idea: from a partial solution, non-deterministically
% select a new queen, check safety of new queen against those
% already placed, if OK add new queen to partial solution and
% continue, otherwise backtrack to the selection and choose
% another possible queen.
:- module(_,_).
%! \begin{focus}
queens(N, Qs) :-
queens_list(N, Ns), % E.g., Ns=[4,3,2,1]
queens_(Ns, [], Qs).
queens_([], Qs, Qs). % All queens placed!
queens_(Unplaced, Placed, Qs) :-
selectq(Unplaced, Q, NewUnplaced), % E.g., Q=4, NewU=[3,2,1]
no_attack(Placed, Q, 1), % Fail if Q attacked
queens_(NewUnplaced, [Q|Placed], Qs). % OK -> Choose next Q
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).
selectq([X|Ys], X, Ys). % Just select/3, renamed not to clash
selectq([Y|Ys], X, [Y|Zs]) :-
selectq(Ys, X, Zs).
queens_list(0, []).
queens_list(N, [N|Ns]) :-
N > 0, N1 is N - 1, queens_list(N1, Ns).
%! \end{focus} ?- queens_list(4,L).
?- selectq([4,3,2,1], Q, NewUnplaced).Try different solutions.
?- queens(4,L).
?- queens(20,L).
?- queens(27,L).
?- queens(30,L).Takes a long time...
%----------------------------------------
% N Queens in CLP(R)
:- module(_,_).
%! \begin{focus}
:- use_package(clpr).
queens(N, Qs) :-
constrain_values(N, N, Qs),
place_queens(N, Qs).
constrain_values(0, _N, []).
constrain_values(I, N, [X|Xs]) :-
I .>. 0,
X .>. 0, X .=<. N, % All queens between 0 and N
I1 .=. I - 1,
constrain_values(I1, N, Xs), no_attack(Xs, X, 1).
no_attack([], _Queen, _Nb).
no_attack([Y|Ys], Queen, Nb) :-
Queen .<>. Y + Nb,
Queen .<>. Y - Nb,
Nb1 .=. Nb + 1,
no_attack(Ys, Queen, Nb1).
place_queens(0, _).
place_queens(N, Q) :-
N .>. 0,
memberq(N, Q),
N1 .=. N - 1,
place_queens(N1, Q).
memberq(X, [X|_]).
memberq(X, [_|Xs]) :- memberq(X, Xs).
%! \end{focus} ?- queens(4,Qs).
?- queens(20,Qs).
:- module(_,_).
:- use_package(clpq).
queens(N, Qs) :-
constrain_values(N, N, Qs),
place_queens(N, Qs).
constrain_values(0, _N, []).
constrain_values(I, N, [X|Xs]) :-
I .>. 0,
X .>. 0, X .=<. N, % All queens between 0 and N
I1 .=. I - 1,
constrain_values(I1, N, Xs), no_attack(Xs, X, 1).
no_attack([], _Queen, _Nb).
no_attack([Y|Ys], Queen, Nb) :-
Queen .<>. Y + Nb,
Queen .<>. Y - Nb,
Nb1 .=. Nb + 1,
no_attack(Ys, Queen, Nb1).
place_queens(0, _).
place_queens(N, Q) :-
N .>. 0,
memberq(N, Q),
N1 .=. N - 1,
place_queens(N1, Q).
memberq(X, [X|_]).
memberq(X, [_|Xs]) :- memberq(X, Xs).
%! \begin{focus}
%! \end{focus} ?- queens(N, L).
?- constrain_values(4, 4, Qs).
?- memberq(4, [A, B, C, D]).
?- place_queens(4, [A, B, C, D]).
:- module(_,_).
:- use_package(clpr).
queens(N, Qs) :-
constrain_values(N, N, Qs),
place_queens(N, Qs).
constrain_values(0, _N, []).
constrain_values(I, N, [X|Xs]) :-
I .>. 0,
X .>. 0, X .=<. N, % All queens between 0 and N
I1 .=. I - 1,
constrain_values(I1, N, Xs), no_attack(Xs, X, 1).
no_attack([], _Queen, _Nb).
no_attack([Y|Ys], Queen, Nb) :-
Queen .<>. Y + Nb,
Queen .<>. Y - Nb,
Nb1 .=. Nb + 1,
no_attack(Ys, Queen, Nb1).
place_queens(0, _).
place_queens(N, Q) :-
N .>. 0,
memberq(N, Q),
N1 .=. N - 1,
place_queens(N1, Q).
memberq(X, [X|_]).
memberq(X, [_|Xs]) :- memberq(X, Xs).
%! \begin{focus}
%! \end{focus} ?- constrain_values(4, 4, Qs), Qs = [3,1|OQs].
?- constrain_values(4, 4, Qs), Qs = [3,2|OQs].
:- module(_,_).
:- use_package(clpr).
queens(N, Qs) :-
constrain_values(N, N, Qs),
place_queens(N, Qs).
constrain_values(0, _N, []).
constrain_values(I, N, [X|Xs]) :-
I .>. 0,
X .>. 0, X .=<. N, % All queens between 0 and N
I1 .=. I - 1,
constrain_values(I1, N, Xs), no_attack(Xs, X, 1).
no_attack([], _Queen, _Nb).
no_attack([Y|Ys], Queen, Nb) :-
Queen .<>. Y + Nb,
Queen .<>. Y - Nb,
Nb1 .=. Nb + 1,
no_attack(Ys, Queen, Nb1).
place_queens(0, _).
place_queens(N, Q) :-
N .>. 0,
memberq(N, Q),
N1 .=. N - 1,
place_queens(N1, Q).
memberq(X, [X|_]).
memberq(X, [_|Xs]) :- memberq(X, Xs).
%! \begin{focus}
%! \end{focus} 
| Can be represented as, e.g., | E :: [-123, -10..4, 10] | [Eclipse notation] |
| or as | E in -123 \/ (-10..4) \/ 10 | [Ciao notation] |
?- use_package(clpfd).
?- X #= A + B, A in 1..3, B in 3..7.→ The respective minimums and maximums are added
?- X #= A - B, A in 1..3, B in 3..7.→ The min value of X is the min value of A minus the max value of B
?- X #= A - B, A in 1..3, B in 3..7, X #>= 0.
?- use_package(clpfd).
?- domain([X, Y, Z],1,1000), X*X+Y*Y #= Z*Z, X #>= Y.
?- domain([X, Y, Z],1,1000), X*X+Y*Y #= Z*Z, X #>= Y, labeling([],[X,Y,Z]).
?- domain([X, Y, Z],1,100), X*X+Y*Y #= Z*Z, X #>= Y, minimize(labeling([],[X,Y,Z]), Y).This can be used to minimize or maximize a solution
S E N D + M O R E _________ M O N E Y
:- module(_,_).
:- use_package(clpfd).
% -------------------------------------------------------------------
% The classic SEND MORE MONEY example
smm_no_labeling(Vars) :-
Vars = [S,E,N,D,M,O,R,Y],
domain(Vars, 0, 9), % All digits 0..9
0 #< S, 0 #< M, % No leftmost zeros
all_different(Vars), % All digits different
% And the arithmetic constraints:
S*1000 + E*100 + N*10 + D +
M*1000 + O*100 + R*10 + E #=
M*10000 + O*1000 + N*100 + E*10 + Y.Try:?- smm_no_labeling([S,E,N,D,M,O,R,Y]).→ We need labeling!
smm(Vars) :-
Vars = [S,E,N,D,M,O,R,Y],
domain(Vars, 0, 9),
0 #< S, 0 #< M,
all_different(Vars),
S*1000 + E*100 + N*10 + D +
M*1000 + O*100 + R*10 + E #=
M*10000 + O*1000 + N*100 + E*10 + Y,
labeling([], Vars),
pp_smm(Vars).Try:?- smm([S,E,N,D,M,O,R,Y]).
pp_smm([S,E,N,D,M,O,R,Y]) :-
format("~n ~w ~w ~w ~w~n", [S,E,N,D]),
format( " + ~w ~w ~w ~w~n", [M,O,R,E]),
format( " ---------~n", []),
format( " ~w ~w ~w ~w ~w~n",[M,O,N,E,Y]). Solve an earlier puzzle again, now with finite domains: exercise! ▶

:- module(_,_).
:- use_package(clpfd).
% -------------------------------------------------------------------
% A project management problem (1)
% The job dependencies and task lengths
% are given by this graph:
%
% 0*G
% ^ ^
% / |
% / |
% 4*E 1*F
% ^^ ^^
% | \ | \
% | \ | \
% 1*B 2*C 3*D
% ^ ^ ^
% \ | /
% \ | /
% 0*A
%
% Plus, the whole job should be finished in 10
% time units or less.
%! \begin{focus}
pn1(A,B,C,D,E,F,G) :-
domain([A,B,C,D,E,F,G], 0, 10),
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 + 1.
%! \end{focus} ?- use_package(clpfd).
?- pn1(A,B,C,D,E,F,G).
:- module(_,_).
:- use_package(clpfd).
pn1(A,B,C,D,E,F,G) :-
domain([A,B,C,D,E,F,G], 0, 10),
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 + 1.
%! \begin{focus}
%! \end{focus}Queries to the constraints of the previous example, pn1(A,B,C,D,E,F,G):?- use_package(clpfd).
?- pn1(A,B,C,D,E,F,G).→ Note the slack of the variables. Some additional constraints must be respected as well, but are not shown by default.
?- minimize(pn1(A,B,C,D,E,F,G),G).→ Variables without slack represent critical tasks (the critical path); there is some slack elsewhere.
And we can minimize more variables:
?- minimize(pn1(A,B,C,D,E,F,G),G), minimize(true, F), minimize(true,B).

:- module(_,_).
:- use_package(clpfd).
% -------------------------------------------------------------------
% A project management problem (2)
% The job dependencies and task lengths
% are now given by this graph, where we
% assume we can accelerate or slow down task F
% (we make the length of F a variable X )
%
% 0*G
% ^ ^
% / |
% / |
% 4*E X*F
% ^^ ^^
% | \ | \
% | \ | \
% 1*B 2*C 3*D
% ^ ^ ^
% \ | /
% \ | /
% 0*A
%
% Still, the whole job should be finished in 10
% time units or less.
%! \begin{focus}
pn2(A, B, C, D, E, F, G, X) :-
domain([A,B,C,D,E,F,G,X], 0, 10),
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.
%! \end{focus} ?- use_package(clpfd).
?- pn2(A, B, C, D, E, F, G, X).
?- minimize(pn2(A,B,C,D,E,F,G,X), G), maximize(true,X).
:- module(_,_).
:- use_package(clpfd).
% -------------------------------------------------------------------
% A project management problem (3)
% The job dependencies and task lengths
% are now given by this graph, where we
% now have two independent tasks B and D
% whose lengths X and Y are not fixed:
%
% 0*G
% ^ ^
% / |
% / |
% 4*E 1*F
% ^^ ^^
% | \ | \
% | \ | \
% X*B 2*C Y*D
% ^ ^ ^
% \ | /
% \ | /
% 0*A
%
% Still, the whole job should be finished in 10
% time units or less.
%
% But we can finish any of B or D in 2 time units at best
% -> X #>=2, Y #>=2
%
% Plus, some shared resource disallows finishing both
% tasks in 2 time units: they will instead take 6 time units
% -> X #>=2, Y #>=2, X + Y #= 6
%! \begin{focus}
pn3(A,B,C,D,E,F,G,X,Y) :-
domain([A,B,C,D,E,F,G,X,Y], 0, 10),
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.
%! \end{focus} ?- use_package(clpfd).
?- minimize(pn3(A,B,C,D,E,F,G,X,Y), G).→ We must devote more resources to task B.
We can use labeling for D and F:
?- minimize(pn3(A,B,C,D,E,F,G,X,Y),G), labeling([],[D,F]).Also:
?- pn3(A,B,C,D,E,F,G,X,Y), minimize(labeling([],[A,B,C,D,E,F,G,X,Y]),G).
?- minimize(pn3(A,B,C,D,E,F,G,X,Y),G), labeling([],[D,F]).
----------------- | | Q | | | ----------------- | | | | Q | ----------------- | Q | | | | ----------------- | | | Q | | -----------------is represented as [2, 4, 1, 3].
:- module(_,_).
:- use_package(clpfd).
% -------------------------------------------------------------------
% N Queens in CLP(fd)
queens_fd(N, Qs, Type) :- % Type is the labeling strategy
constrain_values(N, N, Qs),
all_different(Qs), % built-in constraint
labeling(Type,Qs).
constrain_values(0, _N, []).
constrain_values(N, Range, [X|Xs]) :-
N > 0, N1 is N - 1, X in 1 .. Range,
constrain_values(N1, Range, Xs), no_attack(Xs, X, 1).
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).Queries:?- time(queens_fd(20, Q, [ff])).
?- time(queens_pl(20,Q)).
?- time(queens_fd(100, Q, [ff])).
?- time(queens_pl(100,Q)).(Takes a long time...)
fd_all(N,L) :- findall(X, queens_fd(N,X,[ff]),S), length(S,L). pl_all(N,L) :- findall(X, queens_pl(N,X),S), length(S,L).
?- time(pl_all(11, L)).
?- time(fd_all(11, L)).
?- time(pl_all(12, L)).
?- time(fd_all(12, L)).
?- time(pl_all(13, L)).
?- time(fd_all(13, L)).→ CLP(fd) (at least this program) not so good for all solutions!
queens_pl(N, Qs) :-
queens_pl_list(N, Ns),
queens_pl_(Ns, [], Qs).
queens_pl_([], Qs, Qs).
queens_pl_(Unplaced, Placed, Qs) :-
selectq(Unplaced, Q, NewUnplaced),
pl_no_attack(Placed, Q, 1),
queens_pl_(NewUnplaced, [Q|Placed], Qs).
pl_no_attack([], _Queen, _Nb).
pl_no_attack([Y|Ys], Queen, Nb) :-
Queen =\= Y + Nb, Queen =\= Y - Nb, Nb1 is Nb + 1,
pl_no_attack(Ys, Queen, Nb1).
selectq([X|Ys], X, Ys).
selectq([Y|Ys], X, [Y|Zs]) :- selectq(Ys, X, Zs).
queens_pl_list(0, []).
queens_pl_list(N, [N|Ns]) :- N > 0, N1 is N - 1, queens_pl_list(N1, Ns). :- module(_,_).
iso(Tree, Tree).
iso(t(R, I1, D1), t(R, I2, D2)) :-
iso(I1, D2),
iso(D1, I2).Try:?- iso(t(a, b, t(X, Y, Z)), t(a, t(u, v, W), L)).
?- "123".Z = Z."231", Z::0. ?- "123".Z = Z."231", Z::3. no no ?- "123".Z = Z."231", Z::1. ?- "123".Z = Z."231", Z::4. Z = "1" Z = "1231" ?- "123".Z = Z."231", Z::2. no
seq(<Y, X>). abs(Y, Y) :- Y >= 0. seq(<Y1 - X, Y, X>.U) :- abs(Y,-Y) :- Y < 0. seq(<Y, X>.U) abs(Y, Y1).
?- seq(U.V.W), U::2, V::7, W::2, U#W. fail
freeze( X, Goal) :- attach_attribute( V, frozen(V,Goal)), X = V. verify_attribute( frozen(Var,Goal), Value) :- detach_attribute( Var), Var = Value, call(Goal). combine_attributes( frozen(V1,G1), frozen(V2,G2)) :- detach_attribute( V1), detach_attribute( V2), V1 = V2, attach_attribute( V1, frozen(V1,(G1,G2))).
max(X,Y,X) :- X .>. Y. ?- max(X, Y, Z). max(X,Y,Y) :- X .=<. Y. Z .=. X, Y .<. X ;with
max(X,Y,X) :- X .>. Y, !. ?- max(X, Y, Z). max(X,Y,Y) :- X .=<. Y. Z .=. X, Y .<. X