Skip to content

Commit c97442b

Browse files
committed
documentation
1 parent af74a3f commit c97442b

File tree

12 files changed

+270
-228
lines changed

12 files changed

+270
-228
lines changed

LP05-Constraint-Logic-Programming-CLPFD/hw5q3.lp

Lines changed: 0 additions & 65 deletions
This file was deleted.

LP05-Constraint-Logic-Programming-CLPFD/hw5q4.lp

Lines changed: 0 additions & 112 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Problem 1: Program the N­Queen problem in CLP(FD). Follow the structure of the 8 queens problem whose code is given to you.
2+
3+
----------------------------------------------------------------------------------------------------------------------------------
4+
5+
Problem 2: Write a CLP(FD) program to solve all cryptarithmetic addition problems.
6+
7+
----------------------------------------------------------------------------------------------------------------------------------
8+
9+
Problem 3: Program the Zebra puzzle in CLP(FD).
10+
11+
Nationality: [Englishman, Spaniard, Japanese, Italian, Norwegian],
12+
Color: [Green, Red, Yellow, Blue, White],
13+
Occupation: [Painter, Diplomat, Violinist, Doctor, Sculptor],
14+
Animal: [Dog, Zebra, Fox, Snail, Horse],
15+
Drink: [Juice, Water, Tea, Coffee, Milk])
16+
17+
ZEBRA PROBLEM: RULES
18+
19+
1. The Englishman lives in the red house.
20+
2. The Spaniard owns a dog.
21+
3. The Japanese is a painter.
22+
4. The Italian drinks tea.
23+
5. The Norwegian lives in the first house.
24+
6. The owner of the green house drinks coffee.
25+
7. The green house is on the right of the white house.
26+
8. The sculptor breeds the snail.
27+
9. The diplomat lives in the yellow house.
28+
10. Milk is drunk in the middle house.
29+
11. The Norwegian's house is next to the blue one.
30+
12. Violinist drinks fruit juice.
31+
13. The fox is in the house next to that of the doctor.
32+
14. The horse is in the house next to that of the diplomat.
33+
34+
----------------------------------------------------------------------------------------------------------------------------------
35+
36+
Problem 4: Program the Sudoku Puzzle in CLF(FD). You should read input from the user which consists of a series of terms of the
37+
form: f(X,Y,Z). in a file called ”input”.
38+
39+
The term f(X,Y,Z) states that the square at position (X, Y) has value Z (Z ∈ 1..9). The input file is used to indicates the
40+
values given at the various squares in the puzzle. Your program should print the solution on the screen using write statements.
41+
42+
----------------------------------------------------------------------------------------------------------------------------------

LP05-Constraint-Logic-Programming-CLPFD/hw5q1.lp renamed to LP05-Constraint-Logic-Programming-CLPFD/src/hw5q1.lp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,58 @@
1010
% the list of numbers [1,2,3,..,N].
1111

1212
queens(N,Qs) :-
13-
range(1,N,Ns), permutation(Ns,Qs), safe(Qs).
13+
range(1,N,Ns), permutation(Ns,Qs), safe(Qs).
1414

1515
% The placement Qs is safe/ valid.
1616
safe([]).
1717
safe([Q|Qs]) :-
18-
safe(Qs), \+ attack(Q,Qs).
18+
safe(Qs), \+ attack(Q,Qs).
1919

2020
attack(X,Xs) :- attack(X,1,Xs).
2121
attack(X,N,[Y|_]) :-
22-
X #= Y+N; X #= Y-N.
22+
X #= Y+N; X #= Y-N.
2323
attack(X,N,[_|Ys]) :-
24-
N1 #= N+1, attack(X,N1,Ys).
24+
N1 #= N+1, attack(X,N1,Ys).
2525

2626
% permutation(L1,P): P is a permutation of L1
2727
permutation([],[]).
2828
permutation(Xs,[Z|Zs]) :-
29-
select(Z,Xs,Ys), permutation(Ys,Zs).
29+
select(Z,Xs,Ys), permutation(Ys,Zs).
3030

3131
% range(M,N,List): List is list of integers [M,N], M<N.
3232
range(N,N,[N]).
3333
range(M,N,[M|Ns]) :-
34-
M #< N, M1 #= M+1, range(M1,N,Ns).
34+
M #< N, M1 #= M+1, range(M1,N,Ns).
3535

3636
% select(X,List,R): R #= a List with one removed X
3737
select(X,[X|T],T).
3838
select(X,[H|T],[H|R]) :- %X \= H,
39-
select(X,T,R).
39+
select(X,T,R).
4040

4141
%----------------------------------------------------------
4242
% n_queens(N,Queens), label(Queens) same as
4343
% queens(N,Queens), but USING CLPFD inbuilt functions.
4444

4545
n_queens(N, Qs) :-
46-
length(Qs, N),
47-
%in place of range and premutation.
48-
Qs ins 1..N,
49-
safe_queens(Qs).
46+
length(Qs, N),
47+
%in place of range and premutation.
48+
Qs ins 1..N,
49+
safe_queens(Qs).
5050

5151
safe_queens([]).
5252
safe_queens([Q|Qs]) :-
53-
safe_queens(Qs, Q, 1),
54-
safe_queens(Qs).
53+
safe_queens(Qs, Q, 1),
54+
safe_queens(Qs).
5555

5656
safe_queens([], _, _).
5757
safe_queens([Q|Qs], Q0, D0) :-
58-
%not in same row
59-
Q0 #\= Q,
60-
%diagonally safe
61-
abs(Q0 - Q) #\= D0,
62-
D1 #= D0 + 1,
63-
%recursive check
64-
safe_queens(Qs, Q0, D1).
58+
%not in same row
59+
Q0 #\= Q,
60+
%diagonally safe
61+
abs(Q0 - Q) #\= D0,
62+
D1 #= D0 + 1,
63+
%recursive check
64+
safe_queens(Qs, Q0, D1).
6565

6666
%----------------------- COMMANDS -------------------------
6767

LP05-Constraint-Logic-Programming-CLPFD/hw5q2.lp renamed to LP05-Constraint-Logic-Programming-CLPFD/src/hw5q2.lp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,43 @@
77
%-------------------------- Q2 ----------------------------
88
% Solves generic cryptarithmetic problems of any length for
99
% producing third list as sum of two lists.
10-
% E A T
11-
% + T H A T
12-
%-----------------
13-
% A P P L E
10+
% E A T
11+
% + T H A T
12+
%-------------
13+
% A P P L E
1414

1515
genCrypt([H1|T1] + [H2|T2] = [H3|T3]):-
16-
%combines all varibales
17-
append([H1|T1],[H2|T2],Temp),
18-
append(Temp,[H3|T3],Duplicates),
19-
20-
%produce a distinct set
21-
list_to_set(Duplicates,DistictSet),
22-
%setting up the domains
23-
DistictSet ins 0..9,
24-
%making all of them different
25-
all_distinct(DistictSet),
26-
27-
% trivial
28-
H1 #> 0, H2 #> 0, H3 #> 0,
29-
30-
%evaluating their values
31-
value([H1|T1],S1),
32-
value([H2|T2],S2),
33-
value([H3|T3],S3),
34-
S3 #= S1 + S2,
35-
36-
%printing them
37-
labeling([],DistictSet).
16+
%combines all varibales
17+
append([H1|T1],[H2|T2],Temp),
18+
append(Temp,[H3|T3],Duplicates),
19+
20+
%produce a distinct set
21+
list_to_set(Duplicates,DistictSet),
22+
%setting up the domains
23+
DistictSet ins 0..9,
24+
%making all of them different
25+
all_distinct(DistictSet),
26+
27+
% trivial
28+
H1 #> 0, H2 #> 0, H3 #> 0,
29+
30+
%evaluating their values
31+
value([H1|T1],S1),
32+
value([H2|T2],S2),
33+
value([H3|T3],S3),
34+
S3 #= S1 + S2,
35+
36+
%printing them
37+
labeling([],DistictSet).
3838

3939
% value([1,2,3],123). true.
4040
value([],0).
4141
value([H|T],V) :-
42-
length(T,L), Base #= 10^L,
43-
BaseValue #= Base * H,
44-
45-
V #= V1 + BaseValue,
46-
value(T,V1).
42+
length(T,L), Base #= 10^L,
43+
BaseValue #= Base * H,
44+
45+
V #= V1 + BaseValue,
46+
value(T,V1).
4747

4848
%----------------------- COMMANDS -------------------------
4949

0 commit comments

Comments
 (0)