Chapter 8

The planner Module

On this page

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:

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:

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.

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.

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.

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.1: A program for the Farmer’s problem using planner.

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 (Ri, Ci ), where Ri is a row number and Ci 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)]).

Figure 8.2: A program for the 15-puzzle