Can anyone find why I can\'t have any true answers with my \'go\' at this code? For example, I write go(7,3,l)
and I suppose that it should move 3 litres of water t
The first thing you need to correct it's basic syntax: variables must begin with uppercase, so for instance you must change this rule
pour(state(D1,D2,D3,n,l),move(D,C,r),state(D,C,D3,n,r)) :-
D is D1-n,
C is D2+n.
to
pour(state(D1,D2,D3,N,l),move(D,C,r),state(D,C,D3,N,r)) :-
D is D1-N,
C is D2+N.
and
newstate(state(D1,D2,D3,n,l),state(D11,D22,D33,n1,r)):-
carry(M,C),
M=<7,C=<3,
D22 is D2+n,
D11 is D1-n,
D3 is D33,
n1 is n,
D2=<7,D1=<10,
legal(D1,D2,D3).
to
newstate(state(D1,D2,D3,N,l),state(D11,D22,D33,N1,r)):-
carry(M,C),
M=<7,C=<3,
D22 is D2+N,
D11 is D1-N,
D3 is D33,
N1 is N,
D2=<7,D1=<10,
legal(D1,D2,D3).
You should realize that N1 is valorized just in the first newstate/2 procedure: then correct the other instances of that rule.
Then you should remove the useless dynamic assertions: think about your actual assert/retractall usage: what you need is to change the state moving 'water' between 'jugs' at any step: so you should assert/retract state/5 while looping in go, from an initial state (assert this at start program), to a final acceptable state, to be tested in go to stop the cycle.
But this style based on state change leads to very difficult debugging. Try instead to remove altogheter the dynamic,assert/retractall, and pass around the state in your cycle.
Expanding on @Mog's answer, I suggest to use iterative deepening if you want to find a shortest solution. The following is based on the code posted by @Mog (and thus solves the same problem that is slightly different from the one posted by OP). As we want to describe a list (of moves), DCG notation is convenient:
solution(Path) :- length(Path, _), phrase(path([0-3, 0-5, 8-8]), Path).
path(State) --> { equivalent(State, [0-3, 4-5, 4-8]) }.
path(State0) --> [From-To],
{ move(State0, State), State = [_-From, _-To, _] },
path(State).
equivalent(State1, State2) :- forall(member(X, State1), member(X, State2)).
move(State, [NewX-From, NewY-To|NewRest]) :-
select(X-From, State, Rest),
X \== 0,
select(Y-To, Rest, NewRest),
Fillable is To - Y,
ToFill is min(X, Fillable),
NewY is Y + ToFill,
NewX is X - ToFill.
Sample query:
?- solution(Ps).
Ps = [8-5, 5-3, 3-8, 5-3, 8-5, 5-3, 3-8] .
Well so as I said in my comment, I kinda wouldn't know how to help you with your current work since there are so many wrong things in it. I'd advise you to read a nice tutorial about Prolog (such as Learn Prolog now) so that you can grab the language basics. Here is a simple way to solve your problem if you're interested. If you don't want your problem to be spoiled, just don't read further : ] (the one I posted is about a 3/5/8 jugs and a 4/4 split).
go(Path) :-
solve([0-3, 0-5, 8-8], [], [], Temp),
reverse(Temp, Path).
solve(State, _Visited, Path, Path) :-
equivalent(State, [0-3, 4-5, 4-8]).
solve(State, Visited, Acc, Path) :-
move(State, NewState),
NewState = [_-From, _-To|_],
forall(member(Past, Visited), \+ equivalent(Past, NewState)),
solve(NewState, [NewState|Visited], [From-To|Acc], Path).
equivalent(State1, State2) :-
forall(member(X, State1), member(X, State2)).
move(State, [NewX-From, NewY-To|NewRest]) :-
select(X-From, State, Rest),
X \== 0,
select(Y-To, Rest, NewRest),
Fillable is To - Y,
ToFill is min(X, Fillable),
NewY is Y + ToFill,
NewX is X - ToFill.
If you need explanations about the code once you've read a little more about prolog, don't hesitate!