15.1 Planning with Individuals and Relations

The third edition of Artificial Intelligence: foundations of computational agents, Cambridge University Press, 2023 is now available (including full text).

15.1.1 Situation Calculus

The situation calculus represents states in terms of the actions required to reach them. The situation calculus can be seen as a relational version of the feature-based representation of actions.

Here we consider only a single agent, a fully observable environment, and deterministic actions.

The situation calculus is defined in terms of situations. A situation is either

  • •

    i⁢n⁢i⁢t, the initial situation, or

  • •

    d⁢o⁢(A,S), the situation resulting from doing action A in situation S, if it is possible to do action A in situation S.

Example 15.1.

Consider the domain of Figure 3.1. Suppose in the initial situation, i⁢n⁢i⁢t, the robot, Rob, is at location o⁢109 and there is a key k⁢1 at the mail room and a package at s⁢t⁢o⁢r⁢a⁢g⁢e. Suppose m⁢o⁢v⁢e⁢(A⁢g,L0,L1) is the action of agent A⁢g moving from location L0 to location L1.

d⁢o⁢(m⁢o⁢v⁢e⁢(r⁢o⁢b,o⁢109,o⁢103),i⁢n⁢i⁢t)

is the situation resulting from Rob moving from position o⁢109 in situation i⁢n⁢i⁢t to position o⁢103. In this situation, Rob is at o⁢103, the key k⁢1 is still at m⁢a⁢i⁢l, and the package is at s⁢t⁢o⁢r⁢a⁢g⁢e.

The situation do(move(rob,o103,mail), do(move(rob,o109,o103), init)) is one in which the robot has moved from position o⁢109 to o⁢103 to m⁢a⁢i⁢l and is currently at mail. Suppose Rob then carries out the action p⁢i⁢c⁢k⁢u⁢p⁢(r⁢o⁢b,k⁢1), which is to pick up the key k⁢1. The resulting situation is do(pickup(rob,k1), do(move(rob,o103,mail), do(move(rob,o109,o103), init))). In this situation, Rob is at position m⁢a⁢i⁢l carrying the key k⁢1.

A situation may be associated with a state. There are two main differences between situations and states:

  • •

    Multiple situations may refer to the same state if multiple sequences of actions lead to the same state. That is, equality between situations is not the same as equality between states.

  • •

    Not all states have corresponding situations. A state is reachable if a sequence of actions can reach that state from the initial state. States that are not reachable do not have a corresponding situation.

Some d⁢o⁢(A,S) terms do not correspond to any state. Sometimes an agent must reason about such a (potential) situation without knowing whether A is possible in state S, or whether S is possible.

Example 15.2.

The term d⁢o⁢(u⁢n⁢l⁢o⁢c⁢k⁢(r⁢o⁢b,d⁢o⁢o⁢r⁢1),i⁢n⁢i⁢t) does not denote a state at all, because it is not possible for Rob to unlock the door when Rob is not at the door and does not have the key.

The situations:

i⁢n⁢i⁢t
d⁢o⁢(m⁢o⁢v⁢e⁢(r⁢o⁢b,o⁢103,o⁢109),d⁢o⁢(m⁢o⁢v⁢e⁢(r⁢o⁢b,o⁢103,o⁢109),i⁢n⁢i⁢t))
d⁢o⁢(m⁢o⁢v⁢e⁢(r⁢o⁢b,o⁢103,t⁢s),d⁢o⁢(m⁢o⁢v⁢e⁢(r⁢o⁢b,o⁢103,t⁢s),i⁢n⁢i⁢t))

all represent the same state, with the robot at location o⁢103, and everything else that is true in the initial state. In the last two situations, the robot has moved away from o⁢103 and back again. This assumes that the resources used by the robot are not being modeled; if the resources were modeled, the last two situations may represent different states from i⁢n⁢i⁢t as the battery level may be less.

A static relation is a relation for which the truth value does not depend on the situation; that is, its truth value is unchanging through time. A dynamic relation is a relation for which the truth value depends on the situation. To represent what is true in a situation, predicate symbols denoting dynamic relations have a situation argument so that the truth can depend on the situation. A predicate symbol with a situation argument is called a fluent.

Example 15.3.

The relation a⁢t⁢(O,L,S) is true when object O is at location L in situation S. Thus, a⁢t is a fluent.

The atom

a⁢t⁢(r⁢o⁢b,o⁢109,i⁢n⁢i⁢t)

is true if the robot r⁢o⁢b is at position o⁢109 in the initial situation. The atom

a⁢t⁢(r⁢o⁢b,o⁢103,d⁢o⁢(m⁢o⁢v⁢e⁢(r⁢o⁢b,o⁢109,o⁢103),i⁢n⁢i⁢t))

is true if robot r⁢o⁢b is at position o⁢103 in the situation resulting from r⁢o⁢b moving from position o⁢109 to position o⁢103 from the initial situation. The atom

a⁢t⁢(k⁢1,m⁢a⁢i⁢l,d⁢o⁢(m⁢o⁢v⁢e⁢(r⁢o⁢b,o⁢109,o⁢103),i⁢n⁢i⁢t))

is true if k⁢1 is at position m⁢a⁢i⁢l in the situation resulting from Rob moving from position o⁢109 to position o⁢103 from the initial situation.

A dynamic relation is axiomatized by specifying the situations in which it is true. This is done inductively in terms of the structure of situations, as follows:

  • •

    Axioms with i⁢n⁢i⁢t as the situation parameter are used to specify what is true in the initial situation.

  • •

    A primitive relation is defined by specifying when it is true in situations of the form d⁢o⁢(A,S) in terms of what is true in situation S. That is, primitive relations are defined in terms of what is true at the previous situation.

  • •

    A derived relation is defined using clauses with a variable in the situation argument. The truth of a derived relation in a situation depends on what else is true in the same situation.

  • •

    A static relation is defined without reference to the situation.

Example 15.4.

Suppose the delivery robot, Rob, is in the domain depicted in Figure 3.1. Rob is at location o⁢109, the parcel is in the storage room, and the key is in the mail room. The following axioms describe this initial situation:

a⁢t⁢(r⁢o⁢b,o⁢109,i⁢n⁢i⁢t).
a⁢t⁢(p⁢a⁢r⁢c⁢e⁢l,s⁢t⁢o⁢r⁢a⁢g⁢e,i⁢n⁢i⁢t).
a⁢t⁢(k⁢1,m⁢a⁢i⁢l,i⁢n⁢i⁢t).

The a⁢d⁢j⁢a⁢c⁢e⁢n⁢t relation is a dynamic, derived relation defined as follows:

a⁢d⁢j⁢a⁢c⁢e⁢n⁢t⁢(o⁢109,o⁢103,S).
a⁢d⁢j⁢a⁢c⁢e⁢n⁢t⁢(o⁢103,o⁢109,S).
a⁢d⁢j⁢a⁢c⁢e⁢n⁢t⁢(o⁢109,s⁢t⁢o⁢r⁢a⁢g⁢e,S).
a⁢d⁢j⁢a⁢c⁢e⁢n⁢t⁢(s⁢t⁢o⁢r⁢a⁢g⁢e,o⁢109,S).
a⁢d⁢j⁢a⁢c⁢e⁢n⁢t⁢(o⁢109,o⁢111,S).
a⁢d⁢j⁢a⁢c⁢e⁢n⁢t⁢(o⁢111,o⁢109,S).
a⁢d⁢j⁢a⁢c⁢e⁢n⁢t⁢(o⁢103,m⁢a⁢i⁢l,S).
a⁢d⁢j⁢a⁢c⁢e⁢n⁢t⁢(m⁢a⁢i⁢l,o⁢103,S).
a⁢d⁢j⁢a⁢c⁢e⁢n⁢t⁢(l⁢a⁢b⁢2,o⁢109,S).
a⁢d⁢j⁢a⁢c⁢e⁢n⁢t⁢(P1,P2,S)←
    b⁢e⁢t⁢w⁢e⁢e⁢n⁢(D⁢o⁢o⁢r,P1,P2)∧
    u⁢n⁢l⁢o⁢c⁢k⁢e⁢d⁢(D⁢o⁢o⁢r,S).

Notice the free S variable; these clauses are true for all situations. The situation term, S, cannot be omitted because which rooms are adjacent depends on which doors are unlocked. This can change from situation to situation.

The b⁢e⁢t⁢w⁢e⁢e⁢n relation is static and does not require a situation variable:

b⁢e⁢t⁢w⁢e⁢e⁢n⁢(d⁢o⁢o⁢r⁢1,o⁢103,l⁢a⁢b⁢2).

We also model whether or not an object is being carried. If an object is not being carried, we say that the object is sitting at its location. A carried object moves with the object carrying it. An object is at a location if it is sitting at that location or is being carried by an object at that location. Thus, a⁢t⁢(O⁢b⁢j⁢e⁢c⁢t,L⁢o⁢c⁢a⁢t⁢i⁢o⁢n,S⁢i⁢t⁢u⁢a⁢t⁢i⁢o⁢n) is a derived relation:

a⁢t⁢(O⁢b,P,S)←
    s⁢i⁢t⁢t⁢i⁢n⁢g⁢_⁢a⁢t⁢(O⁢b,P,S).
a⁢t⁢(O⁢b,P,S)←
    c⁢a⁢r⁢r⁢y⁢i⁢n⁢g⁢(O⁢b⁢1,O⁢b,S)∧
    a⁢t⁢(O⁢b⁢1,P,S).

Note that this definition allows for Rob to be carrying a bag, which, in turn, is carrying a book.

The precondition of an action specifies when it is possible to carry out the action. The relation p⁢o⁢s⁢s⁢(A,S) is true when action A is possible in situation S. This is typically a derived relation.

Example 15.5.

An autonomous agent can put down an object it is carrying:

p⁢o⁢s⁢s⁢(p⁢u⁢t⁢d⁢o⁢w⁢n⁢(A⁢g,O⁢b⁢j),S)←
    a⁢u⁢t⁢o⁢n⁢o⁢m⁢o⁢u⁢s⁢(A⁢g)∧
    c⁢a⁢r⁢r⁢y⁢i⁢n⁢g⁢(A⁢g,O⁢b⁢j,S).

For the m⁢o⁢v⁢e action, an autonomous agent can move from its current position to an adjacent position:

p⁢o⁢s⁢s⁢(m⁢o⁢v⁢e⁢(A⁢g,P1,P2),S)←
    a⁢u⁢t⁢o⁢n⁢o⁢m⁢o⁢u⁢s⁢(A⁢g)∧
    a⁢d⁢j⁢a⁢c⁢e⁢n⁢t⁢(P1,P2,S)∧
    s⁢i⁢t⁢t⁢i⁢n⁢g⁢_⁢a⁢t⁢(A⁢g,P1,S).

The precondition for the unlock action is more complicated. The agent must be on the correct side of the door and carrying the appropriate key:

p⁢o⁢s⁢s⁢(u⁢n⁢l⁢o⁢c⁢k⁢(A⁢g,D⁢o⁢o⁢r),S)←
    a⁢u⁢t⁢o⁢n⁢o⁢m⁢o⁢u⁢s⁢(A⁢g)∧
    b⁢e⁢t⁢w⁢e⁢e⁢n⁢(D⁢o⁢o⁢r,P1,P2)∧
    a⁢t⁢(A⁢g,P1,S)∧
    o⁢p⁢e⁢n⁢s⁢(K⁢e⁢y,D⁢o⁢o⁢r)∧
    c⁢a⁢r⁢r⁢y⁢i⁢n⁢g⁢(A⁢g,K⁢e⁢y,S).

The b⁢e⁢t⁢w⁢e⁢e⁢n relation is not symmetric; some doors can only be opened with a key from one side.

What is true in each situation is defined recursively in terms of the previous situation and what action occurred between the situations. As in the feature-based representation of actions, causal rules specify when a relation becomes true and frame rules specify when a relation remains true.

Example 15.6.

The primitive relation u⁢n⁢l⁢o⁢c⁢k⁢e⁢d can be defined by specifying how different actions can affect its being true. A door is unlocked in the situation resulting from an unlock action, as long as the unlock action was possible. This is represented using the causal rule:

u⁢n⁢l⁢o⁢c⁢k⁢e⁢d⁢(D⁢o⁢o⁢r,d⁢o⁢(u⁢n⁢l⁢o⁢c⁢k⁢(A⁢g,D⁢o⁢o⁢r),S))←
    p⁢o⁢s⁢s⁢(u⁢n⁢l⁢o⁢c⁢k⁢(A⁢g,D⁢o⁢o⁢r),S).

Suppose the only action to make the door locked is to lock the door. Thus, u⁢n⁢l⁢o⁢c⁢k⁢e⁢d is true in a situation following an action if it was true before, if the action was not to lock the door, and if the action was possible:

u⁢n⁢l⁢o⁢c⁢k⁢e⁢d⁢(D⁢o⁢o⁢r,d⁢o⁢(A,S))←
    u⁢n⁢l⁢o⁢c⁢k⁢e⁢d⁢(D⁢o⁢o⁢r,S)∧
    A≠l⁢o⁢c⁢k⁢(D⁢o⁢o⁢r)∧
    p⁢o⁢s⁢s⁢(A,S).

This is a frame rule.

Example 15.7.

The c⁢a⁢r⁢r⁢y⁢i⁢n⁢g predicate can be defined as follows.

An agent is carrying an object after picking up the object:

c⁢a⁢r⁢r⁢y⁢i⁢n⁢g⁢(A⁢g,O⁢b⁢j,d⁢o⁢(p⁢i⁢c⁢k⁢u⁢p⁢(A⁢g,O⁢b⁢j),S))←
    p⁢o⁢s⁢s⁢(p⁢i⁢c⁢k⁢u⁢p⁢(A⁢g,O⁢b⁢j),S).

The only action that undoes the c⁢a⁢r⁢r⁢y⁢i⁢n⁢g predicate is the p⁢u⁢t⁢d⁢o⁢w⁢n action. Thus, c⁢a⁢r⁢r⁢y⁢i⁢n⁢g is true after an action if it was true before the action, and the action was not to put down the object. This is represented in the frame rule:

c⁢a⁢r⁢r⁢y⁢i⁢n⁢g⁢(A⁢g,O⁢b⁢j,d⁢o⁢(A,S))←
    c⁢a⁢r⁢r⁢y⁢i⁢n⁢g⁢(A⁢g,O⁢b⁢j,S)∧
    p⁢o⁢s⁢s⁢(A,S)∧
    A≠p⁢u⁢t⁢d⁢o⁢w⁢n⁢(A⁢g,O⁢b⁢j).
Example 15.8.

The atom s⁢i⁢t⁢t⁢i⁢n⁢g⁢_⁢a⁢t⁢(O⁢b⁢j,P⁢o⁢s,S1) is true in a situation S1 resulting from object O⁢b⁢j moving to P⁢o⁢s, as long as the action was possible:

s⁢i⁢t⁢t⁢i⁢n⁢g⁢_⁢a⁢t⁢(O⁢b⁢j,P⁢o⁢s,d⁢o⁢(m⁢o⁢v⁢e⁢(O⁢b⁢j,P⁢o⁢s0,P⁢o⁢s),S))←
    p⁢o⁢s⁢s⁢(m⁢o⁢v⁢e⁢(O⁢b⁢j,P⁢o⁢s0,P⁢o⁢s),S).

The other action that makes s⁢i⁢t⁢t⁢i⁢n⁢g⁢_⁢a⁢t true is the p⁢u⁢t⁢d⁢o⁢w⁢n action. An object is sitting at the location where the agent who put it down was located:

s⁢i⁢t⁢t⁢i⁢n⁢g⁢_⁢a⁢t⁢(O⁢b⁢j,P⁢o⁢s,d⁢o⁢(p⁢u⁢t⁢d⁢o⁢w⁢n⁢(A⁢g,O⁢b⁢j),S))←
    p⁢o⁢s⁢s⁢(p⁢u⁢t⁢d⁢o⁢w⁢n⁢(A⁢g,O⁢b⁢j),S)∧
    a⁢t⁢(A⁢g,P⁢o⁢s,S).

The only other time that s⁢i⁢t⁢t⁢i⁢n⁢g⁢_⁢a⁢t is true in a (non-initial) situation is when it was true in the previous situation and it was not undone by an action. The only actions that undo s⁢i⁢t⁢t⁢i⁢n⁢g⁢_⁢a⁢t are a m⁢o⁢v⁢e action or a p⁢i⁢c⁢k⁢u⁢p action. This can be specified by the following frame axiom:

s⁢i⁢t⁢t⁢i⁢n⁢g⁢_⁢a⁢t⁢(O⁢b⁢j,P⁢o⁢s,d⁢o⁢(A,S))←
    p⁢o⁢s⁢s⁢(A,S)∧
    s⁢i⁢t⁢t⁢i⁢n⁢g⁢_⁢a⁢t⁢(O⁢b⁢j,P⁢o⁢s,S)∧
    ∀P⁢o⁢s1⁢A≠m⁢o⁢v⁢e⁢(O⁢b⁢j,P⁢o⁢s,P⁢o⁢s1)∧
    ∀A⁢g⁢A≠p⁢i⁢c⁢k⁢u⁢p⁢(A⁢g,O⁢b⁢j).

Note that the quantification in the body is not the standard quantification for rules. This can be represented in a standard manner as:

s⁢i⁢t⁢t⁢i⁢n⁢g⁢_⁢a⁢t⁢(O⁢b⁢j,P⁢o⁢s,d⁢o⁢(A,S))←
    p⁢o⁢s⁢s⁢(A,S)∧
    s⁢i⁢t⁢t⁢i⁢n⁢g⁢_⁢a⁢t⁢(O⁢b⁢j,P⁢o⁢s,S)∧
    ∼move_action(A,Obj,Pos)∧
    ∼pickup_action(A,Obj).
m⁢o⁢v⁢e⁢_⁢a⁢c⁢t⁢i⁢o⁢n⁢(m⁢o⁢v⁢e⁢(O⁢b⁢j,P⁢o⁢s,P⁢o⁢s1),O⁢b⁢j,P⁢o⁢s).
p⁢i⁢c⁢k⁢u⁢p⁢_⁢a⁢c⁢t⁢i⁢o⁢n⁢(p⁢i⁢c⁢k⁢u⁢p⁢(A⁢g,O⁢b⁢j),O⁢b⁢j).

where ∼ is negation as failure. These clauses are designed not to have a free variable in the scope of the negation.

Example 15.9.

The situation calculus can represent more complicated actions than can be represented with simple addition and deletion of propositions in the state description.

Consider the d⁢r⁢o⁢p⁢_⁢e⁢v⁢e⁢r⁢y⁢t⁢h⁢i⁢n⁢g action in which an agent drops everything it is carrying. In the situation calculus, the following axiom can be added to the definition of s⁢i⁢t⁢t⁢i⁢n⁢g⁢_⁢a⁢t to say that everything the agent was carrying is now on the ground:

s⁢i⁢t⁢t⁢i⁢n⁢g⁢_⁢a⁢t⁢(O⁢b⁢j,P⁢o⁢s,d⁢o⁢(d⁢r⁢o⁢p⁢_⁢e⁢v⁢e⁢r⁢y⁢t⁢h⁢i⁢n⁢g⁢(A⁢g),S))←
    p⁢o⁢s⁢s⁢(d⁢r⁢o⁢p⁢_⁢e⁢v⁢e⁢r⁢y⁢t⁢h⁢i⁢n⁢g⁢(A⁢g),S)∧
    a⁢t⁢(A⁢g,P⁢o⁢s,S)∧
    c⁢a⁢r⁢r⁢y⁢i⁢n⁢g⁢(A⁢g,O⁢b⁢j,S).

A frame axiom for c⁢a⁢r⁢r⁢y⁢i⁢n⁢g specifies that an agent is not carrying an object after a d⁢r⁢o⁢p⁢_⁢e⁢v⁢e⁢r⁢y⁢t⁢h⁢i⁢n⁢g action.

c⁢a⁢r⁢r⁢y⁢i⁢n⁢g⁢(A⁢g,O⁢b⁢j,d⁢o⁢(A,S))←
    p⁢o⁢s⁢s⁢(A,S)∧
    c⁢a⁢r⁢r⁢y⁢i⁢n⁢g⁢(A⁢g,O⁢b⁢j,S)∧
    A≠d⁢r⁢o⁢p⁢_⁢e⁢v⁢e⁢r⁢y⁢t⁢h⁢i⁢n⁢g⁢(A⁢g)∧
    A≠p⁢u⁢t⁢d⁢o⁢w⁢n⁢(A⁢g,O⁢b⁢j).

The d⁢r⁢o⁢p⁢_⁢e⁢v⁢e⁢r⁢y⁢t⁢h⁢i⁢n⁢g action thus affects an unbounded number of objects.

The situation calculus is used for planning by asking for a situation in which a goal is true. Answer extraction is used to find a situation in which the goal is true. This situation can be interpreted as a sequence of actions for the agent to perform.

Example 15.10.

Suppose the goal is for the robot to have the key k⁢1. The following query asks for a situation where this is true:

𝘢𝘴𝘬 ⁢c⁢a⁢r⁢r⁢y⁢i⁢n⁢g⁢(r⁢o⁢b,k⁢1,S).

This query has the following answer: S=do(pickup(rob,k1), do(move(rob,o103,mail), do(move(rob,o109,o103), init))). The preceding answer can be interpreted as a way for Rob to get the key: it moves from o⁢109 to o⁢103, then to m⁢a⁢i⁢l, where it picks up the key.

The goal of delivering the parcel (which is, initially, in the lounge, l⁢n⁢g) to o⁢111 can be asked with the query

𝘢𝘴𝘬 ⁢a⁢t⁢(p⁢a⁢r⁢c⁢e⁢l,o⁢111,S).

This query has the following answer: S=do(move(rob,o109,o111), do(move(rob,lng,o109), do(pickup(rob,parcel), do(move(rob,o109,lng),init)))). Therefore, Rob should go to the lounge, pick up the parcel, go back to o⁢109, and then go to o⁢111.

Using the top-down proof procedure on the situation calculus definitions is very inefficient, because a frame axiom is almost always applicable. A complete proof procedure, such as iterative deepening, searches through all permutations of actions even if they are not relevant to the goal. The use of answer extraction does not negate the necessity for efficient planners, such as the ones in Chapter 6.