Chapter 8
The planner Module
The planner module provides several predicates for solving planning problems. Given an initial state, a final state, and a set of possible actions, a planning problem is to find a plan that transforms the initial state to the final state. In order to use the planner module to solve a planning problem, users have to provide the condition for the final states and the state transition diagram through the following global predicates:
- final(): This predicate succeeds if is a final state.
-
final(, , ): A final state can be reached from by the action sequence in with . If this predicate is given, then there is no need to define final/1. If this predicate is not given, then the system assumes the following definition:
final(S, Plan, Cost) => Plan = [], Cost = 0, final(S). - action(, , , ): This predicate encodes the state transition diagram of the planning problem. The state can be transformed into by performing . The cost of is . If the plan’s length matters, then should be 1.
A state is normally a ground term. As all states are tabled during search, it is of paramount importance to find a good representation for states such that terms among states can be shared as much as possible.
In addition to the two required predicates given above, users can optionally provide the following global procedures to assist Picat in searching for plans:
- heuristic() = : This function returns an estimation of the resource amount needed to transform into a goal state. This estimation is said to be admissible if it never exceeds the real cost. All heuristic estimations must be admissible in order for the planner to find optimal plans. This function is used by the planner to check the heuristic estimation before each state expansion.
-
sequence(, ): This predicate binds to a viable action form based on the current partial plan . Note that the actions in list are in reverse order, with the most recent action occurring first in the list, and the first action occurring last in the list. The planner calls sequence/2 to find an action for expanding the current state before calling action/4. For example,
sequence([move(R,X,Y)|_], Action) ?=> Action = $move(R, Y, _). sequence([move(R,_,_)|_], Action) ?=> Action = $jump(R). sequence([move(R,_,_)|_], Action) => Action = $wait(R). sequence(_, _) => true.These sequence rules ban robots from moving in an interleaving fashion; a robot must continue to move until it takes the action jump or wait before another robot can start moving. The last rule sequence(_, _) => true is necessary; it permits any action to be taken if the partial plan is empty, or if the most recent action in the partial plan is not move.
8.1 Depth-Bounded Search
Depth-bounded search amounts to exploring the search space, taking into account the current available resource amount. A new state is only explored if the available resource amount is non-negative. When depth-bounded search is used, the function current_resource() can be used to retrieve the current resource amount. If the heuristic estimate of the cost to travel from the current state to the final state is greater than the available resource amount, then the current state fails.
- plan(, , , ): This predicate, if it succeeds, binds to a plan that can transform state to a final state that satisfies the condition given by final/1 or final/3. is the cost of , which cannot exceed , which is a given non-negative integer.
- plan(, , ): If the second argument is an integer, then this predicate is the same as the plan/4 predicate, except that the plan’s cost is not returned.
- plan(, , ): If the second argument is a variable, then this predicate is the same as the plan/4 predicate, except that the limit is assumed to be 268435455.
- plan(, ): This predicate is the same as the plan/4 predicate, except that the limit is assumed to be 268435455, and that the plan’s cost is not returned.
- best_plan(, , , ): This predicate finds an optimal plan by using the following algorithm: It first calls plan/4 to find a plan of 0 cost. If no plan is found, then it sets the cost limit to 1 or doubles the current limit. Once a plan is found, the algorithm uses binary search to find an optimal plan.
- best_plan(, , ): If the second argument is an integer, then this predicate is the same as the best_plan/4 predicate, except that the plan’s cost is not returned.
- best_plan(, , ): If the second argument is a variable, then this predicate is the same as the best_plan/4 predicate, except that the limit is assumed to be 268435455.
- best_plan(, ): This predicate is the same as the best_plan/4 predicate, except that the limit is assumed to be 268435455, and that the plan’s cost is not returned.
- best_plan_bb(, , , ): This predicate, if it succeeds, binds to an approximated optimal plan that can transform state to a final state. is the cost of , which cannot exceed , which is a given non-negative integer. The branch-and-bound algorithm is used to find an optimal plan.
- best_plan_bb(, , ): If the second argument is an integer, then this predicate is the same as the best_plan_bb/4 predicate, except that the plan’s cost is not returned.
- best_plan_bb(, , ): If the second argument is a variable, then this predicate is the same as the best_plan_bb/4 predicate, except that the limit is assumed to be 268435455.
- best_plan_bb(, ): This predicate is the same as the best_plan_bb/4 predicate, except that the limit is assumed to be 268435455, and that the plan’s cost is not returned.
- best_plan_bin(,
,
,
):
This predicate, if it succeeds, binds
to an approximated optimal plan that can transform state to a final state. is the cost of , which cannot exceed , which is a given non-negative integer. The branch-and-bound algorithm is used with binary search to find an optimal plan. - best_plan_bin(, , ): If the second argument is an integer, then this predicate is the same as the best_plan_bin/4 predicate, except that the plan’s cost is not returned.
- best_plan_bin(, , ): If the second argument is a variable, then this predicate is the same as the best_plan_bin/4 predicate, except that the limit is assumed to be 268435455.
- best_plan_bin(, ): This predicate is the same as the best_plan_bin/4 predicate, except that the limit is assumed to be 268435455, and that the plan’s cost is not returned.
- current_resource()=: This function returns the current available resource amount of the current node. If the current execution path was not initiated by one of the calls that performs resource-bounded search, then 268435455 is returned. This function can be used to check the heuristics. If the heuristic estimate of the cost to travel from the current state to a final state is greater than the available resource amount, then the current state can be failed.
- current_plan()=: This function returns the current plan that has transformed the initial state to the current state. If the current execution path was not initiated by one of the calls that performs resource-bounded search, then [] is returned.
- current_resource_plan_cost(, , ): This predicate retrieves the attributes of the current node in the search tree, including the resource amount, the path to the node, and its cost.
- is_tabled_state(): This predicate succeeds if the state has been explored before and has been tabled.
If depth-bounded search is used, then the table area must be cleared by calling the
initialize_table predicate before each subsequent call to plan or best_plan.
8.2 Depth-Unbounded Search
In contrast to depth-bounded search, depth-unbounded search does not take into account the available resource amount. A new state can be explored even if no resource is available for the exploration. The advantage of depth-unbounded search is that failed states are never re-explored.
- best_plan_nondet(,
,
,
):
This predicate is the same as
best_plan(, , , ), except that it allows multiple best plans to be returned through backtracking. - best_plan_nondet(, , ): If the second argument is an integer, then this predicate is the same as the best_plan_nondet/4 predicate, except that the plan’s cost is not returned.
- best_plan_nondet(, , ): If the second argument is a variable, then this predicate is the same as the best_plan_nondet/4 predicate, except that the limit is assumed to be 268435455.
- best_plan_nondet(, ): This predicate is the same as the best_plan(, ) predicate, except that it is nondeterministic.
- plan_unbounded(, , , ): This predicate, if it succeeds, binds to a plan that can transform state to a final state. is the cost of , which cannot exceed , which is a given non-negative integer.
- plan_unbounded(, , ): If the second argument is an integer, then this predicate is the same as the plan_unbounded/4 predicate, except that the plan’s cost is not returned.
- plan_unbounded(, , ): If the second argument is a variable, then this predicate is the same as the plan_unbounded/4 predicate, except that the limit is assumed to be 268435455.
- plan_unbounded(, ): This predicate is the same as the above predicate, except that the cost is not returned.
- best_plan_unbounded(, , , ): This predicate, if it succeeds, binds to an optimal plan that can transform state to a final state. is the cost of , which cannot exceed , which is a given non-negative integer.
- best_plan_unbounded(, , ): If the second argument is an integer, then this predicate is the same as the best_plan_unbounded/4 predicate, except that the plan’s cost is not returned.
- best_plan_unbounded(, , ): If the second argument is a variable, then this predicate is the same as the best_plan_unbounded/4 predicate, except that the limit is assumed to be 268435455.
- best_plan_unbounded(, ): This predicate is the same as the above predicate, except that the cost is not returned.
8.3 Examples
The program shown in Figure 8.1 solves the Farmer’s problem by using the planner module.
import planner.
go =>
S0 = [s,s,s,s],
best_plan(S0, Plan),
writeln(Plan).
final([n,n,n,n]) => true.
action([F,F,G,C], S1, Action, ActionCost) ?=>
Action = farmer_wolf,
ActionCost = 1,
opposite(F, F1),
S1 = [F1,F1,G,C],
not unsafe(S1).
action([F,W,F,C], S1, Action, ActionCost) ?=>
Action = farmer_goat,
ActionCost = 1,
opposite(F, F1),
S1 = [F1,W,F1,C],
not unsafe(S1).
action([F,W,G,F], S1, Action, ActionCost) ?=>
Action = farmer_cabbage,
ActionCost = 1,
opposite(F, F1),
S1 = [F1,W,G,F1],
not unsafe(S1).
action([F,W,G,C], S1, Action, ActionCost) =>
Action = farmer_alone,
ActionCost = 1,
opposite(F, F1),
S1 = [F1,W,G,C],
not unsafe(S1).
index (+, -) (-, +)
opposite(n, s).
opposite(s, n).
unsafe([F,W,G,_C]), W == G, F !== W => true.
unsafe([F,_W,G,C]), G == C, F !== G => true.
Figure 8.2 gives a program for the 15-puzzle problem. A state is represented as a list of sixteen locations, each of which takes the form (, ), where is a row number and is a column number. The first element in the list gives the position of the empty square, and the remaining elements in the list give the positions of the numbered tiles from 1 to 15. The function heuristic(Tiles) returns the Manhattan distance between the current state and the final state.
import planner.
main =>
InitS = [(1,2),(2,2),(4,4),(1,3),
(1,1),(3,2),(1,4),(2,4),
(4,2),(3,1),(3,3),(2,3),
(2,1),(4,1),(4,3),(3,4)],
best_plan(InitS, Plan),
foreach (Action in Plan)
println(Action)
end.
final(State) => State = [(1,1),(1,2),(1,3),(1,4),
(2,1),(2,2),(2,3),(2,4),
(3,1),(3,2),(3,3),(3,4),
(4,1),(4,2),(4,3),(4,4)].
action([P0@(R0,C0)|Tiles], NextS, Action, Cost) =>
Cost = 1,
(R1 = R0-1, R1 >= 1, C1 = C0, Action = up;
R1 = R0+1, R1 =< 4, C1 = C0, Action = down;
R1 = R0, C1 = C0-1, C1 >= 1, Action = left;
R1 = R0, C1 = C0+1, C1 =< 4, Action = right),
P1 = (R1, C1),
slide(P0, P1, Tiles, NTiles),
NextS = [P1|NTiles].
% slide the tile at P1 to the empty square at P0
slide(P0, P1, [P1|Tiles], NTiles) =>
NTiles = [P0|Tiles].
slide(P0, P1, [Tile|Tiles], NTiles) =>
NTiles = [Tile|NTilesR],
slide(P0, P1, Tiles, NTilesR).
heuristic([_|Tiles]) = Dist =>
final([_|FTiles]),
Dist = sum([abs(R-FR)+abs(C-FC) :
{(R,C),(FR,FC)} in zip(Tiles,FTiles)]).