Hanoi Tower(Towers of Hanoi) - prolog

I'm trying to do the Towers of Hanoi problem, what I have tried so far:
move(1,[H|T],B,C,A1,B1,C) :-
A1 = T,
B1 = [H|B].
move(N,A,B,C,A1,B1,C) :-
N>1,
M is N-1,
move(M,[H|T],C,B,A1,B1,C),
move(1,[H|T],B,_,A1,B1,C),
move(M,C,B,[H|T],A1,B1,C).
but this code does not work, I need get the result is looks like this:
?-move(3,[1,2,3],[],[],A1,B1,C).
and the results:
A1=[].
B1=[1,2,3]
C=[].
can someone help me fix my code up and can get the result like that? This is very important for me, I really need help.
this is what i did but with some problems:
move(N,[H|T],[],[],A1,B1,C) :-
N > 1,
M is N - 1,
move(N,[H|M],[H|_],[],A1,B1,C),
move(M,[_|M],[H|_],[H|_],A1,B1,C),
move(M,[_|M],[],[H|T],A1,B1,C),
move(M,[],[_|T],[H|T],A1,B1,C),
move(M,[H|_],[_|T],[H|T],A1,B1,C),
move(M,[H|_],[_|T],[],A1,B1,C),
move(M,[],[H|T],[],A1,B1,C).
move(N,[H|T],[],[]) :- write(A1), nl,
write(B1), nl,
write(C).

This is the instruction by instruction of solving towers of Hanoi problem.
move(1,X,Y,_) :-
write('Move top disk from '),
write(X),
write(' to '),
write(Y),
nl.
move(N,X,Y,Z) :-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).
Attack the problem in such a way: In the instruction by instruction solution we don't change the contents of X,Y or Z. But in your problem, you will eventually change the contents of them.
UPDATE:
Since it is declared that this is not a homework question, here is the full answer:
towersOfHanoi(N,A,B,C,A4,B4,C4) :- move(N,A,B,C,A4,B4,C4),!.
move(1,[H|T],B,C,A1,B1,C1) :- A1 = T,
B1 = [H|B],
C1 = C.
move(N,A,B,C,A4,B4,C4) :- N>1,
M is N-1,
move(M,A,C,B,A1,C1,B1),
move(1,A1,B1,C1,A2,B2,C2),
move(M,C2,B2,A2,C4,B4,A4).

Related

Why is my prolog program stucked in endless recursion

I have the following experimental code
s(a,b).
s(b,c).
s(c,b).
r(a).
r(c).
r(d).
p(X,Y) :- s(X,Y), not(r(Y)).
q(X,Y) :- q(Y,X), r(X).
q(X,Y) :- p(Y,X), s(X,Y).
t(X,Y) :- r(X), q(X,Y).
Querying for t(X,Y) will result in a endless recursion blowing up the stack. But I can actually think of X=c,Y=b being the solution because
t(c,b) <- r(c), q(c,b)
q(c,b) <- q(b,c), r(c)
q(b,c) <- p(c,b), s(b,c)
p(c,b) <- s(c,b), not(r(b))
Can someone explain to me, why prolog doesn't come to this solution and gets caught in an endless recursion around q(c,b) and q(b,c)
Many thanks!
In SWI-Prolog, you can solve the problem using tabled execution.
:- table q/2.
s(a,b).
s(b,c).
s(c,b).
r(a).
r(c).
r(d).
p(X,Y) :- s(X,Y), not(r(Y)).
q(X,Y) :- q(Y,X), r(X).
q(X,Y) :- p(Y,X), s(X,Y).
t(X,Y) :- r(X), q(X,Y).
Examples:
?- t(X,Y).
X = c,
Y = b ;
false.
?- q(X,Y).
X = c,
Y = b ;
X = b,
Y = c.

How do I see a detailed order (execution) for a Prolog query?

Let's say I have this Prolog program:
loves(vincent, mia).
loves(marcellus, mia).
jealous(A, B) :- loves(A, C), loves(B, C).
With query jealous(A,B). I'm very new to Prolog and I'd like to know how is it possible to see the exact order the program will be running and taking its ways for this query? I have tried using trace, jealous(A,B). command but it has only given me that:
Isn't there any more detailed solution for that? :/
Have you seen the Prolog Visualizer?
When you get to the page be sure to click on the icons in the upper right to learn more.
Enjoy.
Screenshot after step 10 of 49.
Screenshot for example given after all steps.
The Prolog Visualizer uses a slightly nonstandard way to enter a query by ending the query with a question mark (?), e.g.
jealous(A,B)?
If you do not post a query in the input area on the left you will receive an error, e.g.
The input for the Prolog Visualizer for your example is
loves(vincent, mia).
loves(marcellus, mia).
jealous(A, B) :- loves(A, C), loves(B, C).
jealous(A,B)?
When the Prolog Visualizer completes your example, notice the four results in green on the right
If you are using SWI-Prolog and after you understand syntactic unification, backtracking and write more advanced code you will find this of use:
Overview of the SWI Prolog Graphical Debugger
For other useful Prolog references see: Useful Prolog references
If the Prolog system has callable_property/2 and sys_rule/3, then one can code
a smart "unify" port as follows, showing most general unifiers (mgu's`):
:- op(1200, fx, ?-).
% solve(+Goal, +Assoc, +Integer, -Assoc)
solve(true, L, _, L) :- !.
solve((A, B), L, P, R) :- !, solve(A, L, P, H), solve(B, H, P, R).
solve(H, L, P, R) :- functor(H, F, A), sys_rule(F/A, J, B),
callable_property(J, sys_variable_names(N)),
number_codes(P, U), atom_codes(V, [0'_|U]), shift(N, V, W),
append(L, W, M), H = J, reverse(M, Z), triage(M, Z, I, K),
offset(P), write_term(I, [variable_names(Z)]), nl,
O is P+1, solve(B, K, O, R).
% triage(+Assoc, +Assoc, -Assoc, -Assoc)
triage([V=T|L], M, R, [V=T|S]) :- var(T), once((member(W=U, M), U==T)), W==V, !,
triage(L, M, R, S).
triage([V=T|L], M, [V=T|R], S) :-
triage(L, M, R, S).
triage([], _, [], []).
% shift(+Assoc, +Atom, -Assoc)
shift([V=T|L], N, [W=T|R]) :-
atom_concat(V, N, W),
shift(L, N, R).
shift([], _, []).
% offset(+Integer)
offset(1) :- !.
offset(N) :- write('\t'), M is N-1, offset(M).
% ?- Goal
(?- G) :-
callable_property(G, sys_variable_names(N)),
shift(N, '_0', M),
solve(G, M, 1, _).
Its not necessary to modify mgu's retrospectively, since a solution to a
Prolog query is the sequential composition of mgu's. Here is an example run:
?- ?- jealous(A,B).
[A_0 = X_1, B_0 = Y_1]
[H_1 = mia, X_1 = vincent]
[Y_1 = vincent]
A = vincent,
B = vincent ;
[Y_1 = marcellus]
A = vincent,
B = marcellus ;
Etc..
This is a preview of Jekejeke Prolog 1.5.0 the new
predicate sys_rule/3, its inspired by the new
predicate rule/2 of SWI-Prolog, but keeps the
clause/2 argument of head and body and uses a predicate
indicator.

what is a good way to link exclusive statements in prolog?

We were asked to write a menu based calculator on an exam where we were to return control to the menu once the desired calculation had been performed.
I wrote the following code as my solution and while the professor deemed it as correct I still think there must be a better way to return control to the menu after the the first clause to table(A,N) returns false.
Please note that I've redacted quite a bit of my original code that was irrelevant to my question.
menu :-
write('Enter a choice: '),
read(C),
choice(C).
choice(1) :-
table(5).
table(N) :-
A is 1,
start(A,N).
table(A,N) :-
K is A*N,
write(K),
nl,
A1 is A+1,
A1=<10,
table(A1, N)
;
menu.
I'm very new to prolog so the question might not be appropriately worded. Please let me know if that's the case.
You can use repeat/0 to loop forever.
menu :-
repeat, % add this line
write('Enter a choice: '),
read(C),
choice(C).
choice(1) :-
table(5).
table(N) :-
A is 1,
start(A,N).
table(A,N) :-
K is A*N,
write(K),
nl,
A1 is A+1,
A1=<10,
table(A1, N).
% ; % delete this line
% menu. % delete this line

Symbolic & Numeric Calculation

I'm a Computer Science student, on last semester we learned to program in Prolog. Now I'm trying to have fun with it.
I'm trying to build a program that given a symbolic/mathematical input it return the result.
example:
? solve(2+3+Z+K+5+Z+1, R).
R = 11+2*Z+K or R = 11+Z+K+Z
This is the snippet (for + operation)
solve(X, R) :-
eval(X, R).
eval(X, X) :- var(X),!.
eval(X, X) :- number(X), !.
eval(+(X, Y), R) :-
eval(X, A),
eval(Y, B),
add(A, B, R), !.
add(A, B, R) :-
number(A),
number(B),
!,
R is A + B.
add(A, B, A+B) :-
var(A); var(B),!.
add(A+X, B, R+X) :-
number(A),
number(B),
var(X),
!,
R is A + B.
add(X+A, B, R+X) :-
number(A),
number(B),
var(X),
!,
R is A + B.
I have some issue when numbers are separated by many variables, example:
? solve(5+Z+5+4+K+Z+6, R).
FALSE.
or, (not form source code above), if numbers are separated by many variables, they are not "processed", example:
? solve(5+Z+K+7, R).
R = 5+Z+K+7.
Thanks for help, any suggestion or reference will be appreciate.
This is a bit more involved than what your code is doing at the moment. At the end, you seem to want to have a solver for symbolic equations, is that so? So for example, if you type into Wolfram Alfa your equation, 2+3+Z+K+5+Z+1 = R, you get the answer K+2 Z+11 = R.
Similar functionality is provided for example by metafont:
$ mf
This is METAFONT, Version 2.7182818 (TeX Live 2014) (preloaded base=mf)
**\relax
*tracingequations:=tracingonline:=1;
*2+3+a+b+5+a+1=r;
## a=0.5r-0.5b-5.5
*x^2+3=0;
## x^2=-3
... and I guess by every program as Matlab, Mathematica, etc.
In Prolog, for integers, you get something very similar for free if you use library(clpfd):
?- use_module(library(clpfd)).
true.
?- 2 + 3 + Z + K + Z + 1 #= R.
2*Z+K+ -1*R#= -6.
If you want to program this yourself, you should probably start with deciding how you want to represent your answers: as you see, the three programs demonstrated here choose different approaches. From there, you can either try to see how to get there yourself (see the comment by #lurker), or try to figure out how others have implemented it.

Prolog Programming

I have made two programs in Prolog for the nqueens puzzle using hill climbing and beam search algorithms.
Unfortunately I do not have the experience to check whether the programs are correct and I am in dead end.
I would appreciate if someone could help me out on that.
Unfortunately the program in hill climbing is incorrect. :(
The program in beam search is:
queens(N, Qs) :-
range(1, N, Ns),
queens(Ns, [], Qs).
range(N, N, [N]) :- !.
range(M, N, [M|Ns]) :-
M < N,
M1 is M+1,
range(M1, N, Ns).
queens([], Qs, Qs).
queens(UnplacedQs, SafeQs, Qs) :-
select(UnplacedQs, UnplacedQs1,Q),
not_attack(SafeQs, Q),
queens(UnplacedQs1, [Q|SafeQs], Qs).
not_attack(Xs, X) :-
not_attack(Xs, X, 1).
not_attack([], _, _) :- !.
not_attack([Y|Ys], X, N) :-
X =\= Y+N,
X =\= Y-N,
N1 is N+1,
not_attack(Ys, X, N1).
select([X|Xs], Xs, X).
select([Y|Ys], [Y|Zs], X) :- select(Ys, Zs, X).
I would like to mention this problem is a typical constraint satisfaction problem and can be efficiency solved using the CSP module of SWI-Prolog. Here is the full algorithm:
:- use_module(library(clpfd)).
queens(N, L) :-
N #> 0,
length(L, N),
L ins 1..N,
all_different(L),
applyConstraintOnDescDiag(L),
applyConstraintOnAscDiag(L),
label(L).
applyConstraintOnDescDiag([]) :- !.
applyConstraintOnDescDiag([H|T]) :-
insertConstraintOnDescDiag(H, T, 1),
applyConstraintOnDescDiag(T).
insertConstraintOnDescDiag(_, [], _) :- !.
insertConstraintOnDescDiag(X, [H|T], N) :-
H #\= X + N,
M is N + 1,
insertConstraintOnDescDiag(X, T, M).
applyConstraintOnAscDiag([]) :- !.
applyConstraintOnAscDiag([H|T]) :-
insertConstraintOnAscDiag(H, T, 1),
applyConstraintOnAscDiag(T).
insertConstraintOnAscDiag(_, [], _) :- !.
insertConstraintOnAscDiag(X, [H|T], N) :-
H #\= X - N,
M is N + 1,
insertConstraintOnAscDiag(X, T, M).
N is the number of queens or the size of the board (), and , where , being the position of the queen on the line .
Let's details each part of the algorithm above to understand what happens.
:- use_module(library(clpfd)).
It indicates to SWI-Prolog to load the module containing the predicates for constraint satisfaction problems.
queens(N, L) :-
N #> 0,
length(L, N),
L ins 1..N,
all_different(L),
applyConstraintOnDescDiag(L),
applyConstraintOnAscDiag(L),
label(L).
The queens predicate is the entry point of the algorithm and checks if the terms are properly formatted (number range, length of the list). It checks if the queens are on different lines as well.
applyConstraintOnDescDiag([]) :- !.
applyConstraintOnDescDiag([H|T]) :-
insertConstraintOnDescDiag(H, T, 1),
applyConstraintOnDescDiag(T).
insertConstraintOnDescDiag(_, [], _) :- !.
insertConstraintOnDescDiag(X, [H|T], N) :-
H #\= X + N,
M is N + 1,
insertConstraintOnDescDiag(X, T, M).
It checks if there is a queen on the descendant diagonal of the current queen that is iterated.
applyConstraintOnAscDiag([]) :- !.
applyConstraintOnAscDiag([H|T]) :-
insertConstraintOnAscDiag(H, T, 1),
applyConstraintOnAscDiag(T).
insertConstraintOnAscDiag(_, [], _) :- !.
insertConstraintOnAscDiag(X, [H|T], N) :-
H #\= X - N,
M is N + 1,
insertConstraintOnAscDiag(X, T, M).
Same as previous, but it checks if there is a queen on the ascendant diagonal.
Finally, the results can be found by calling the predicate queens/2, such as:
?- findall(X, queens(4, X), L).
L = [[2, 4, 1, 3], [3, 1, 4, 2]]
If I read your code correctly, the algorithm you're trying to implement is a simple depth-first search rather than beam search. That's ok, because it should be (I don't see how beam search will be effective for this problem and it can be hard to program).
I'm not going to debug this code for you, but I will give you a suggestion: build the chess board bottom-up with
queens(0, []).
queens(N, [Q|Qs]) :-
M is N-1,
queens(M, Qs),
between(1, N, Q),
safe(Q, Qs).
where safe(Q,Qs) is true iff none of Qs attack Q. safe/2 is then the conjunction of a simple memberchk/2 check (see SWI-Prolog manual) and your not_attack/2 predicate, which on first sight seems to be correct.
A quick check on Google has found a few candidates for you to compare with your code and find what to change.
My favoured solution for sheer clarity would be the second of the ones linked to above:
% This program finds a solution to the 8 queens problem. That is, the problem of placing 8
% queens on an 8x8 chessboard so that no two queens attack each other. The prototype
% board is passed in as a list with the rows instantiated from 1 to 8, and a corresponding
% variable for each column. The Prolog program instantiates those column variables as it
% finds the solution.
% Programmed by Ron Danielson, from an idea by Ivan Bratko.
% 2/17/00
queens([]). % when place queen in empty list, solution found
queens([ Row/Col | Rest]) :- % otherwise, for each row
queens(Rest), % place a queen in each higher numbered row
member(Col, [1,2,3,4,5,6,7,8]), % pick one of the possible column positions
safe( Row/Col, Rest). % and see if that is a safe position
% if not, fail back and try another column, until
% the columns are all tried, when fail back to
% previous row
safe(Anything, []). % the empty board is always safe
safe(Row/Col, [Row1/Col1 | Rest]) :- % see if attack the queen in next row down
Col =\= Col1, % same column?
Col1 - Col =\= Row1 - Row, % check diagonal
Col1 - Col =\= Row - Row1,
safe(Row/Col, Rest). % no attack on next row, try the rest of board
member(X, [X | Tail]). % member will pick successive column values
member(X, [Head | Tail]) :-
member(X, Tail).
board([1/C1, 2/C2, 3/C3, 4/C4, 5/C5, 6/C6, 7/C7, 8/C8]). % prototype board
The final link, however, solves it in three different ways so you can compare against three known solutions.

Resources