1

I have recently encountered a question written by Hector J. Levesque in his book Thinking as computation: question

I have written some code in response to this question:

bplan(L) :- tryplan([],L).
tryplan(L,L) :- plan(L).
tryplan(X,L) :- tryplan([_|X],L).
plan(L) :- initial_state(I), goal_state(G), reachable(I,L,G).
reachable(S,[],S).
reachable(S1,[M|L],S3) :- legal_move(S1,M,S2), reachable(S2,L,S3).
initial_state([yard,yard,yard,yard]).
goal_state([market, market, market, market]).
safe([LocFox, LocHen, LocGrain, LocCart]):- \+ danger([LocFox, LocHen, LocGrain, LocCart]).
danger([Loc, Loc, _, Loc1]):- \+ Loc = Loc1.
danger([_, Loc, Loc, Loc1]):- \+ Loc = Loc1.
legal_move([LocFox, LocHen, LocGrain, LocCart],go_alone(LocNew),[LocFox, LocHen, LocGrain, LocNew]) :- opposite(LocCart, LocNew), safe( [LocFox, LocHen, LocGrain, LocNew]).

However, it appears to me that something is missing. From my own interpretation, probably I missed the code for valid moves. Nevertheless, I struggled on creating predicates and clauses for the part, and is hoping to seek some hints / advice / answers to tackle my problem. That would be very much appreciated.

1 Answers1

0

Since you are seeking hints and not an answer see this other planner question and answer done in Prolog and also from a text book.

Prolog planning using retract and assert

The code does not define the predicates safe_state and legal_move but it should be possible to incorporate those.

The one thing this code has that you seem to be missing (looked at your code did not run it) is to search to incorporate Been_list so that the search does not go into an infinite loop.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136