Information Technology Reference
In-Depth Information
Figure 9.3.
A general planner in Prolog
plan.pl
% This general planner needs the predicates below to be defined:
% - legal_move(BeforeState,Move,AfterState)
% - initial_state(State)
% - goal_state(State)
% plan(L): L is a list of moves from the initial state to a goal state.
plan(L) :- initial_state(I), goal_state(G), reachable(I,L,G).
% reachable(S1,L,S2): S2 is reachable from S1 using moves L.
reachable(S,[],S).
reachable(S1,[M|L],S3) :- legal_move(S1,M,S2), reachable(S2,L,S3).
Figure 9.4.
The three-coins problem in Prolog
coins.pl
% The three-coins problem formulated for the general planner.
initial_state([h,h,t]).
goal_state([h,h,h]).
goal_state([t,t,t]).
% The three possible moves. Each changes one of the coins.
legal_move([X,Y,Z],flip_left,[X1,Y,Z]) :- opposite(X,X1).
legal_move([X,Y,Z],flip_middle,[X,Y1,Z]) :- opposite(Y,Y1).
legal_move([X,Y,Z],flip_right,[X,Y,Z1]) :- opposite(Z,Z1).
opposite(h,t).
% Flipping a head gives a tail.
opposite(t,h).
% Flipping a tail gives a head.
This is what is called a general planner. For each specific planning problem to be
solved, one must provide clauses that define the initial_state , goal_state , and
legal_move predicates for that problem.
9.2.2 Solving the three-coins problem
To solve a planning problem like the three-coins problem, the first thing to determine
is how to represent the states of the problem symbolically. In this case, a state is
determined by which sides of the coins are showing. So a state of the problem can be
represented using a list with three elements [ x , y , z ] , where the x , y , and z are either
h (for heads) or t (for tails). (There are eight states in total.)
The full program based on this representation is shown in figure 9.4. Note the
single initial state and the two possible goal states. The next three clauses define
the legal_move predicate. There are three possible moves: turning over the left, the
 
Search WWH ::




Custom Search