Hi! I got stuck with this problem. When I trace it(trace! in iprolog), I notice that it doesn't loop correctly for Di values.
And I have 2 different implementations, i)when Do<Di. ii)when Do>=Di. But when Do>=Di, it doesn't perform the expected action(append..), yet go back to distance predicate.
Please point out my mistakes and help.
%getIntentions1([goal(X,Y,S)|O],at(A,B),[goal(X1,Y1,S1)|I],Intentions1).
getIntentions1([],at(A,B),L,L):-!.
getIntentions1(L,at(A,B),[],L).
getIntentions1([goal(X,Y,S)|O],at(A,B),[goal(X1,Y1,S1)|I],[goal(X,Y,S)|NI]):-
distance((A,B),(X,Y),Do),distance((A,B),(X1,Y1),Di),
Do<=Di,!,
append([goal(X1,Y1,S1)],I,L2),
getIntentions1(O,at(A,B),L2,NI).
getIntentions1(at(A,B),[goal(X,Y,S)|O],[goal(X1,Y1,S1)|I],[goal(X1,Y1,S1)|NI]):-
distance((A,B),(X,Y),Do),distance((A,B),(X1,Y1),Di),
Do>=Di,!,
append([goal(X,Y,S)],O,L1),
getIntentions1(L1,at(A,B),I,NI).
%finding manhantan distance
distance((X,Y), (X1,Y1), D) :-
dif(X, X1, Dx),
dif(Y, Y1, Dy),
D is Dx + Dy.
% D is |A - B|
dif(A, B, D) :-
D is A - B, D >= 0, !.
dif(A, B, D) :-
D is B - A.
%TEST
%getIntentions1([goal(3,4,5),goal(3,2,5),goal(6,7,2)],at(3,3),[goal(4,5,6),goal(5,6,7),goal(6,6,8)],Intentions1)?
getIntentions1(Options, Beliefs, Intentions, Intentions1) which has four arguments: a list of options each of the form goal(X,Y,S), a list of beliefs (here containing one term of the form at(X,Y)), the current list of intentions each of the form goal(X,Y,S), and computes a list which contains the new options inserted into the current list of intentions in order of the Manhattan distance of the goal from the agent's current location. If an option is the same distance from the agent as an existing goal, the new goal should be placed later in the list.
And I have 2 different implementations, i)when Do<Di. ii)when Do>=Di. But when Do>=Di, it doesn't perform the expected action(append..), yet go back to distance predicate.
Please point out my mistakes and help.
%getIntentions1([goal(X,Y,S)|O],at(A,B),[goal(X1,Y1,S1)|I],Intentions1).
getIntentions1([],at(A,B),L,L):-!.
getIntentions1(L,at(A,B),[],L).
getIntentions1([goal(X,Y,S)|O],at(A,B),[goal(X1,Y1,S1)|I],[goal(X,Y,S)|NI]):-
distance((A,B),(X,Y),Do),distance((A,B),(X1,Y1),Di),
Do<=Di,!,
append([goal(X1,Y1,S1)],I,L2),
getIntentions1(O,at(A,B),L2,NI).
getIntentions1(at(A,B),[goal(X,Y,S)|O],[goal(X1,Y1,S1)|I],[goal(X1,Y1,S1)|NI]):-
distance((A,B),(X,Y),Do),distance((A,B),(X1,Y1),Di),
Do>=Di,!,
append([goal(X,Y,S)],O,L1),
getIntentions1(L1,at(A,B),I,NI).
%finding manhantan distance
distance((X,Y), (X1,Y1), D) :-
dif(X, X1, Dx),
dif(Y, Y1, Dy),
D is Dx + Dy.
% D is |A - B|
dif(A, B, D) :-
D is A - B, D >= 0, !.
dif(A, B, D) :-
D is B - A.
%TEST
%getIntentions1([goal(3,4,5),goal(3,2,5),goal(6,7,2)],at(3,3),[goal(4,5,6),goal(5,6,7),goal(6,6,8)],Intentions1)?
getIntentions1(Options, Beliefs, Intentions, Intentions1) which has four arguments: a list of options each of the form goal(X,Y,S), a list of beliefs (here containing one term of the form at(X,Y)), the current list of intentions each of the form goal(X,Y,S), and computes a list which contains the new options inserted into the current list of intentions in order of the Manhattan distance of the goal from the agent's current location. If an option is the same distance from the agent as an existing goal, the new goal should be placed later in the list.