prolog. contents 1.prolog: the robot blocks world. 2.prolog: the monkey and bananas problem. 3.what...

119
PROLOG

Upload: haden-cowan

Post on 15-Jan-2016

222 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

PROLOG

Page 2: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Contents1. PROLOG: The Robot Blocks World.2. PROLOG: The Monkey and Bananas problem.3. What is Planning?4. Planning vs. Problem Solving5. STRIPS and Shakey6. Planning in Prolog7. Operators8. The Frame Problem9. Representing a plan10. Means Ends Analysis

Page 3: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

PROLOG PROGRAM PROLOG PROGRAM FOR BLOCKS WORLDFOR BLOCKS WORLD• Consider a robot that manipulates blocks on table

• Robot can see blocks by a camera mounted on ceiling

• Robot wants to know:1. blocks’ coordinates, 2. whether a block is graspable (nothing on top), 3. etc.

Page 4: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

5 4 3 21

PROLOG PROGRAM FOR PROLOG PROGRAM FOR ROBOT’S WORLDROBOT’S WORLD

1 2 3 4 5 6

a

b

dc

% see( Block, X, Y)

see( a, 2, 5).

see( d, 5, 5).

see( e, 5, 2).

% on( Block, BlockOrTable)

on( a, b).

on( b, c).

on( c, table).

on( d, table).

on( e, table).

e

X

Y

Page 5: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

INTERACTION WITH ROBOT PROGRAMStart Prolog interpreter?- [robot]. % Load file robot.plFile robot consulted?- see( a, X, Y). % Where do you see block aX = 2Y = 5?- see( Block, _, _). % Which block(s) do you see?Block = a; % More answers?Block = d;Block = e;no

Page 6: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

INTERACTION, CTD.

?- see( B1, _, Y), see( B2, _, Y). % Blocks at same Y?

% Prolog’s answers may surprise!

% Perhaps this was intended:?- see( B1, _, Y), see( B2, _, Y), B1 \== B2.

Page 7: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

B

B1

Z1

Z

on( B, B1)

Z is Z1 + 1Z is Z1 + 1z( B1, Z1)

z( B, Z) :-

,

,

.

z coordinate of a block z coordinate of a block that is not on tablethat is not on table

Page 8: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

EXTRACT X, Y, Z COORD.

% z( Block, Z): z-coord. of Block

z( B, 0) :- on( B, table).

z( B, Z) :- on( B, B0), z( B0, Z0), Z is Z0 + 1.

B

B0on

Z0

Z is Z0 + 1

Page 9: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Prolog tends to keep results in symbolic form

% An attempt at shortening this

z( B, Z0 + 1) :- on( B, B0), z( B0, Z0).

?- z( a, Za).

Za = 0+1+1 % Prolog constructs a formula!

Page 10: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Prolog can easily construct general formula

z( B, Z0 + height( B)) :- % Add hight of block B on( B, B0), z( B0, Z0).

?- z( a, Za).

Za = 0 + height(c) + height( b)

Page 11: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

% xy( Block, X, Y): X, Y coord. of Block

xy( B, X, Y) :- see( B, X, Y).

xy( B, X, Y) :- on( B0, B), xy( B0, X, Y). % Blocks in stack same xy-coord.

Page 12: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

?- z( c, Z).Z = 0

?- z( a, Z).Z = 2

% Trace proof tree of this execution

Page 13: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

RELATION ABOVE% above( Block1, Block2): Block1 above Block2 in the same stack

above( B1, B2) :- on( B1, B2).

above( B1, B2) :- on( B1, B), above( B, B2).

?- above( a, c). % Trace proof tree for this

Page 14: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

DECLARATIVE vs PROCEDURAL MEANINGDECLARATIVE vs PROCEDURAL MEANING

• A & B is logically equal to B & A

• Declarative meaning of Prolog program = logical meaning

• Order of goals in clauses does not affect declarative meaning

• Procedural meaning of Prolog = algorithm for searching for proof

• Order of goals and clauses does affect procedural meaning

Page 15: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

A VARIANT OF ABOVE

above2( B1, B2) :- above2( B, B2), on( B1, B).

above2( B1, B2) :- on( B1, B2).

?- above( a, c). % Trace to see what happens

Page 16: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

TRY SIMPLE THINGS FIRST• Why above2 fails procedurally?• above2 always tries complicated things first• This is a bad idea

• A principle that often works: Try simple things first

• Do we always have to study procedural details in Prolog? No, usually not!

• In fact, quite the opposite: we shouldn’t!• Programmer encouraged to think declaratively - this is the point of a declarative language!

Page 17: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Prolog Prolog ProgrammingProgramming

Page 18: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Introduction

• PROgramming in LOGic• Emphasis on what rather than how

Basic Machine

Logic Machine

Problem in Declarative Form

Page 19: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Prolog’s strong and weak points

• Assists thinking in terms of objects and entities

• Not good for number crunching

• Useful applications of Prolog in– Expert Systems (Knowledge Representation and

Inferencing)– Natural Language Processing– Relational Databases

Page 20: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

A Typical Prolog program

Compute_length ([],0).Compute_length ([Head|Tail], Length):-

Compute_length (Tail,Tail_length),Length is Tail_length+1.

High level explanation:The length of a list is 1 plus the length of the tail of the list, obtained by removing the first element of the list.

This is a declarative description of the computation.

Page 21: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Planning Planning and and

PrologProlog

Page 22: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Planning• To solve this problem the monkey needed to devise a plan, a

sequence of actions that would allow him to reach the desired goal.

• Planning is a topic of traditional interest in Artificial Intelligence as it is an important part of many different AI applications, such as robotics and intelligent agents.

• To be able to plan, a system needs to be able to reason about the individual and cumulative effects of a series of actions. – This is a skill that is only observed in a few animal species and only mastered by

humans.

• The planning problems we will be discussing today are mostly Toy-World problems but they can be scaled up to real-world problems such as a robot negotiating a space.

Page 23: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Planning vs. Problem Solving• Planning and problem solving (Search) are considered as

different approaches even though they can often be applied to the same problem.

• Basic problem solving (as discussed in the Search lectures) searches a state-space of possible actions, starting from an initial state and following any path that it believes will lead it the goal state.

• Planning is distinct from this in three key ways:1. Planning “opens up” the representation of states, goals and actions so

that the planner can deduce direct connections between states and actions.

2. The planner does not have to solve the problem in order (from initial to goal state) it can suggest actions to solve any sub-goals at anytime.

3. Planners assume that most parts of the world are independent so they can be stripped apart and solved individually (turning the problem into practically sized chunks).

Page 24: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Shakey

• Shakey.ram

Page 25: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Planning using STRIPSPlanning using STRIPS• The “classical” approach most planners use today is derived from the

STRIPS language.

• STRIPS was devised by SRI in the early 1970s to control a robot called Shakey.

• Shakey’s task was to negotiate a series of rooms, move boxes, and grab objects.

• The STRIPS language was used to derive plans that would control Shakey’s movements so that he could achieve his goals.

• The STRIPS language is very simple but expressive language that lends itself to efficient planning algorithms.

• The representation we will use in Prolog is derived from the original STRIPS representation.

Page 26: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

STRIPSSTRIPS1. Stanford Research Institute Problem Solver (1970s)

1. Planning system for a robotics project : SHAKEY (by Nilsson et.al.)

2. Knowledge Representation : First Order Logic.

3. Algorithm : Forward chaining on rules.

4. Any search procedure : Finds a path from start to goal.1. Forward Chaining : Data-driven inferencing2. Backward Chaining : Goal-driven

Page 27: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Forward & Backward Chaining1. Rule : man(x) mortal(x)2. Data : man(Shakespeare)3. To prove : mortal(Shakespeare)

4. Forward Chaining:5. man(Shakespeare) matches LHS of Rule.6. X = Shakespeare

1. mortal( Shakespeare) added7. Forward Chaining used by design expert systems

8. Backward Chaining: uses RHS matching9. - Used by diagnostic expert systems

Page 28: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Triangular Table

• For n operations in the plan, there are :• (n+1) rows : 1 n+1• (n+1) columns : 0 n

• At the end of the ith row, place the ith component of the plan. • The row entries for the ith step contain the pre-conditions for the

ith operation.• The column entries for the jth column contain the add list for the

rule on the top.• The <i,j> th cell (where 1 ≤ i ≤ n+1 and 0≤ j ≤ n) contain the pre-

conditions for the ith operation that are added by the jth operation.• The first column indicates the starting state and the last row

indicates the goal state.

Page 29: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

STRIPS Representation• Planning can be considered as a logical inference problem:

– a plan is inferred from facts and logical relationships.

• STRIPS represented planning problems as a series of state descriptions and operators expressed in first-order predicate logic.

State descriptions represent the state of the world at three points during the plan:– Initial state, the state of the world at the start of the problem;– Current state, and– Goal state, the state of the world we want to get to.

Operators are actions that can be applied to change the state of the world.– Each operator has outcomes i.e. how it affects the world.– Each operator can only be applied in certain circumstances. – These are the preconditions of the operator.

Page 30: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

30

Planning•the subfield of planning combines rule-based reasoning with search

•a planner seeks to find a sequence of actions to accomplish some task– e.g., consider a simple blocks world

Page 31: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

31

Planning as state space search•can describe the environment (state) using predicates:

gripping(Block) clear(Block)on(BlockTop, BlockBottom) onTable(Block)

start state:gripping() onTable(A)

onTable(B) clear(B)on(C, A) clear(C)

goal state:gripping() onTable(C)

on(B, C) on(A,B)clear(A)

Page 32: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

32

Planning as state space search•similarly, can describe actions using predicates & rules:

pickup(B): if clear(B) & onTable(B) & gripping() gripping(B)putdown(B): if gripping(B) onTable(B) & clear(B) & gripping()stack(T, B): if gripping(T) & clear(B) on(T, B) & clear(T) & gripping()unstack(T, B): if gripping() & on(T, B) & clear(T) clear(B) & gripping(T)

in theory, we could define the entire state space using such rules generate a plan (sequence of actions) in the same way we solved flights, jugs, …

in practice, this gets very messy the frame problem refers to the fact that you not only have to define everything that

is changed by an action, but also everything that ISN'T changed

e.g., when you pickup a block, all other blocks retain same properties

as complexity increases, keeping track of what changes and doesn't change after each action becomes difficult

dividing a problem into pieces is tricky, since solution to one might undo other

Page 33: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

33

STRIPS approach•STRIPS (STanford Research Institute Planning System)

– approach introduced by Fikes & Nilsson at SRI, 1971– was used to control a robot that moved around a building, took out trash– has served as the basis of many planning systems since then

idea: for each action, associate 3 sets preconditions for the action, additions & deletions as a result of the action

pickup(B)P: {clear(B), onTable(B), gripping()} A: {gripping(B)} D: {onTable(B), gripping()}

putdown(B)P: {gripping(B)} A: {onTable(B), clear(B), gripping()} D: {gripping(B)}

stack(T, B)P: {gripping(T), clear(B)} A: {on(T, B), clear(T), gripping()} D: {clear(B), gripping(T)}

unstack(T, B)P: {gripping(), on(T, B), clear(T)} A: {clear(B), gripping(T)} D: {on(T, B), gripping()}

Page 34: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

34

STRIPS example:

•start state: { gripping(), onTable(A), onTable(B), clear(B), on(C, A), clear(C) }

•unstack(C, A): { onTable(A), onTable(B), clear(B), clear(A), gripping(C) }

•putdown(C):{ onTable(A), onTable(B), clear(B), clear(A), onTable(C), clear(C), gripping() }

•pickup(B): { onTable(A), clear(A), onTable(C), clear(C), gripping(B) }

•stack(B, C): { onTable(A), clear(A), onTable(C), on(B, C), clear(B), gripping() }

•pickup(A): {onTable(C), on(B, C), clear(B), gripping(A) }

•stack(A, B): {onTable(C), on(B, C), on(A, B), clear(A), gripping() }

Page 35: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Planning in Prolog• As STRIPS uses a logic based representation of states it lends itself well to

being implemented in Prolog.

• To show the development of a planning system we will implement the Monkey and Bananas problem in Prolog using STRIPS.

• When beginning to produce a planner there are certain representation considerations that need to be made:– How do we represent the state of the world?– How do we represent operators?– Does our representation make it easy to:

• check preconditions;• alter the state of the world after performing actions; and• recognise the goal state?

Page 36: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Representing the Representing the World in Monkey World in Monkey

and Banana Problemand Banana Problem

Page 37: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Representing the World in Representing the World in Monkey and Banana ProblemMonkey and Banana Problem• In the M&B problem we have:

– objects: a monkey, a box, the bananas, and a floor.– locations: we’ll call them a, b, and c.– relations of objects to locations. For example:

• the monkey is at location a;• the monkey is on the floor;• the bananas are hanging;• the box is in the same location as the bananas.

• To represent these relations we need to choose appropriate predicates and arguments:

• at(monkey,a).• on(monkey,floor).• status(bananas,hanging).• at(box,X), at(bananas,X).

Page 38: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Monkey & Bananas1. A hungry monkey is in a room.

2. Suspended from the roof, just out of his reach, is a bunch of bananas.

3. In the corner of the room is a box.

4. The monkey desperately wants the bananas but he can’t reach them.

5. What shall he do?

Page 39: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Monkey & Bananas (2)

1. After several unsuccessful attempts to reach the bananas, the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

1. The hungry monkey is now a happy monkey.

Page 40: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Initial and Goal State• Once we have decided on appropriate state predicates we need to represent

the Initial and Goal states.• Initial State:Initial State:

on(monkey, floor),on(box, floor),at(monkey, a),at(box, b),at(bananas, c),status(bananas, hanging).

• Goal State:Goal State:on(monkey, box),on(box, floor),at(monkey, c),at(box, c),at(bananas, c),status(bananas, grabbed).

• Only this last state can be known withoutknowing the details of the Plan (i.e. how we’re going to get there).

Page 41: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Representing Operators• STRIPS operators are defined as:

– NAME: How we refer to the operator e.g. go(Agent, From, To).

– PRECONDITIONS: What states need to hold for the operator to be applied. e.g. [at(Agent, From)].

– ADD LIST: What new states are added to the world as a result of applying the operator e.g. [at(Agent, To)].

– DELETE LIST: What old states are removed from the world as a result of applying the operator. e.g. [at(Agent, From)].

• We will specify operators within a Prolog predicate opn/4:

opn( go(Agent,From,To),

[at(Agent, From)],

[at(Agent, To)],

[at(Agent, From)] ).

NamePreconditionsAdd ListDelete List

Page 42: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

The Frame Problem• When representing operators we make the assumption that the

only effects our operator has on the world are those specified by the add and delete lists.

• In real-world planning this is a hard assumption to make as we can never be absolutely certain of the extent of the effects of an action.– This is known in AI as the Frame Problem.

• Real-World systems, such as Shakey, are notoriously difficult to plan for because of this problem. – Plans must constantly adapt based on incoming sensory information about the

new state of the world otherwise the operator preconditions will no longer apply.

• The planning domains we will be working in our Toy-Worlds so we can assume that our framing assumptions are accurate.

Page 43: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

All OperatorsOperator Preconditions Delete List Add List

go(X,Y) at(monkey,X) at(monkey,X) at(monkey,Y)

on(monkey, floor)

push(B,X,Y) at(monkey,X) at(monkey,X) at(monkey,Y)

at(B,X) at(B,X) at(B,Y)

on(monkey,floor)

on(B,floor)

climb_on(B) at(monkey,X) on(monkey,floor) on(monkey,B)

at(B,X)

on(monkey,floor)

on(B,floor)

grab(B) on(monkey,box) status(B,hanging) status(B,grabbed)

at(box,X)

at(B,X)

status(B,hanging)

Page 44: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Finding a solution

1. Look at the state of the world:• Is it the goal state? If so, the list of operators so far is the plan to be

applied.• If not, go to Step 2.

2. Pick an operator:• Check that it has not already been applied (to stop looping).• Check that the preconditions are satisfied.If either of these checks fails, backtrack to get another operator.

3. Apply the operator:1. Make changes to the world: delete from and add to the world state.2. Add operator to the list of operators already applied.3. Go to Step 1.

Page 45: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Finding a solution in Prolog• The main work of generating a plan is done by the solve/4

predicate.

% First check if the Goal states are a subset of the current state.solve(State, Goal, Plan, Plan):-

is_subset(Goal, State)

solve(State, Goal, Sofar, Plan):-

opn(Op, Precons, Delete, Add), % get first operator \+ member(Op, Sofar), % check for looping is_subset(Precons, State), % check preconditions hold delete_list(Delete, State, Remainder), % delete old states append(Add, Remainder, NewState), % add new states solve(NewState, Goal, [Op|Sofar], Plan).% recurse

• On first glance this seems very similar to a normal depth-first search algorithm (unifies with first possible move and pursues it)

Page 46: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Why use operators?• In fact, solve/4 is performing depth-first search through the space

of possible actions but because actions are represented as operators instead of predicate definitions the result is significantly different:

– A range of different actions can be selected using the same predicate opn/4.

– The effect an action has on the world is made explicit. – This allows actions to be chosen based on the preconditions of sub-goals:

directing our search towards the goal rather than searching blindly.

– Representing the state of the world as a list allows it to be dynamically modified without the need for asserting and retracting facts from the database.

• solve/4 tries multiple operators when forced to backtrack due to failure. • Database manipulation does not revert back to the original state during

backtracking so we couldn’t use it to generate a plan in this manner.

Page 47: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Representing Representing the planthe plan

Page 48: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Representing the plan• solve/4 is a linear planner: it starts at the initial state and tries to

find a series of operators that have the cumulative effect of adding the goal state to the world.

• We can represent its behaviour as a flow-chart.

• When an operator is applied the information in its preconditions is used to instantiate as many of its variables as possible.

• Uninstantiated variables are carried forward to be filled in later.

on(monkey,floor),on(box,floor),at(monkey,a), at(box,b),at(bananas,c),status(bananas,hanging)

go(a,X)Add: at(monkey,X)Delete: at(monkey,a)

Initial State

Operator to be applied

Effect of operator on world state

Page 49: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

• solve/4 chooses the push operator this time as it is the next operator after go/2 stored in the database and go(a,X) is now stored in the SoFar list so go(X,Y) can’t be applied again.

• The preconditions of push/3 require the monkey to be in the same location as the box so the variable location, X, from the last move inherits the value b.

Representing the plan (2)on(monkey,floor),on(box,floor),at(monkey,a),at(box,b),at(bananas,c),status(bananas,hanging)

on(monkey,floor),on(box,floor),at(monkey,b),at(box,b),at(bananas,c),status(bananas,hanging)

go(a,b)

push(box,b,Y)

Add: at(monkey,b)Delete: at(monkey,a)

Add: at(monkey,Y), at(box,Y)Delete: at(monkey,b), at(box,b)

monkey’s location

is changed

Page 50: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Representing the plan (3)on(monkey,floor),on(box,floor),at(monkey,a),at(box,b),at(bananas,c),status(bananas,hanging)

on(monkey,floor),on(box,floor),at(monkey,b),at(box,b),at(bananas,c),status(bananas,hanging)

go(a,b)

push(box,b,Y)

Add: at(monkey,b)Delete: at(monkey,a)

Add: at(monkey,Y), at(box,Y)Delete: at(monkey,b), at(box,b)

on(monkey,floor),on(box,floor),at(monkey,Y),at(box,Y),at(bananas,c),status(bananas,hanging)

climbon(monkey) Add: on(monkey,monkey)Delete: on(monkey,floor)

• The operator only specifies that the monkey must climb on something in the same location; not that it must be something other than itself!

• This instantiation fails once it tries to satisfy the preconditions for the grab/1 operator. solve/4 backtracks and matches climbon(box) instead.

Whoops!

Page 51: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Representing the plan (4)

on(monkey,box),on(box,floor),at(monkey,Y),at(box,Y),at(bananas,c),status(bananas,hanging)

grab(bananas)

on(monkey,box),on(box,floor),at(monkey,c),at(box,c),at(bananas,c),status(bananas,grabbed)

on(monkey,floor),on(box,floor),at(monkey,a),at(box,b),at(bananas,c),status(bananas,hanging)

on(monkey,floor),on(box,floor),at(monkey,b),at(box,b),at(bananas,c),status(bananas,hanging)

go(a,b)

push(box,b,Y)

on(monkey,floor),on(box,floor),at(monkey,Y),at(box,Y),at(bananas,c),status(bananas,hanging)

climbon(box)

GOAL

For the monkey to grab the

bananas it must be in the same

location, so the variable location Y inherits c.

This creates a complete

plan.Y = c

Y = c

Page 52: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Monkey & Bananas Program

Page 53: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Inefficiency of forwards planningInefficiency of forwards planning• Linear planners like this, that progress from the initial state to the

goal state can be unsuitable for problems with a large number of operators.

• Searching backwards from the Goal state usually eliminates spurious paths.

– This is called Means Ends Analysis.

Start

Goal

F

A B C

S E

G H X

Page 54: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Means Ends AnalysisMeans Ends Analysis• The Means are the available actions.• The Ends are the goals to be achieved.

• To solve a list of Goals in state State, leading to state FinalState, do:– If all the Goals are true in State then FinalState = State.

Otherwise do the following:1. Select a still unsolved Goal from Goals.2. Find an Action that adds Goal to the current state. 3. Enable Action by solving the preconditions of Action, giving

MidState.4. MidState is then added as a new Goal to Goals and the program

recurses to step 1.

– i.e. we search backwards from the Goal state, generating new states from the preconditions of actions, and checking to see if these are facts in our initial state.

Page 55: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Means Ends Analysis (2)• Means Ends Analysis will usually lead straight from the Goal State to

the Initial State as the branching factor of the search space is usually larger going forwards compared to backwards.

• However, more complex problems can contain operators with overlapping Add Lists so the MEA would be required to choose between them.– It would require heuristics.

• Also, linear planners like these will blindly pursue sub-goals without considering whether the changes they are making undermine future goals. – they need someway of protecting their goals.

• Both of these issues will be discussed in the next lecture.

Page 56: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Robot Robot MoralityMorality

Page 57: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

fetch(Object, Place) inside(Object, Place).fetch(Object, place) pickup(Object), moveto(Place), drop(Object).

pickup(Object) liftable(Object), inside(Object, Place), moveto(Place), emptyhanded, assert(holding(Object)).

emptyhanded holding(Object), drop(Object).emptyhanded true.

drop(Object) delete(holding(Object)).

Buildingdoor

rocketAirtrap

You may add comments and explanation of this You may add comments and explanation of this problem. I described this and similar problem. I described this and similar problems several times in class, so the problems several times in class, so the prolog programs here should explain prolog programs here should explain

themselves.themselves.

Page 58: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

moveto(Place) inside(robot, Place).moveto(Place) inside(robot, Place2), leave(Place2), enter(place).moveto(Place) outside(robot), enter(place).

leave(Place) entrance(X, Place), ifclosed, delete(inside(robot, Place)), assert(outside(robot)), ifholding1.

Ifclosed closed(X), open(X).Ifclosed true.

Write comments here

Page 59: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Ifholding1 holding (Object), delete(inside(Object,Place)), assert(outside(Object)).Ifholding true.

enter(Place) entrance (X, Place), ifclosed, delete(outside(robot)), assert(inside(robot, Place)), ifholding2.

Ifholding2 holding(Object) delete(outside(Object)), assert(inside(Object,Place)),Ifholding2 true.

Write comments here

Page 60: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

open(door) opendoor, delete(closed(door)).opendoor holding(key).opendoor findkey.findkey inside (key, Place), pickup (key), leave(Place).

inside(human, rocket).inside(robot, rocket).inside(fuel, building).inside(key, cave).inside(gold, cave).

entrance(airlock, rocket).entrance(door, building).entrance(hole,cave).

Write comments here

Page 61: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

closed(airlock).closed(door).

liftable(key).liftable(fuel).liftable(gold).

Write comments here

Page 62: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

• Fetch(gold, rocket) – goal for the robot• Robot leaves the rocket

– open(airlock) delete(closed(airlock))

• Enter the cave• Pick up the gold• Return to rocket

Write comments here

Page 63: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

• Fetch (fuel, rocket).• Robot will leave the rocket• Will try to enter the building• To do this it needs the key• It will go to the cave to get it• Once it is in the building it will drop the key

and pick up the fuel• It will return to the rocket with fuel.

Page 64: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

• At this point the PROLOG statements describing “environment” will be:– Inside(human, rocket).– Inside (robot, rocket).– Inside(fuel, rocket).– Inside (gold, rocket).– Liftable(key).– Liftable(fuel).– Liftable(gold).– Entrance(airlock, rocket).– Entrance(door, building).– Entrance(hole, cave).

Write comments here

Page 65: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

The door

• The door to the building and the airlock are left open.

• This robot is not very bright• You ask the robot to move the gold from the

cave to the building • Fetch(gold, building).• Robot goes to the cave• Picks up the gold

Page 66: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Robot actions in sequence• Goes to the building• Realizes it needs key to open the door• Returns to the cave to get the key• It drops the gold and picks up the key (since it can

carry only one thing at a a time)• It returns to the door• Opens the door and enters the building• It now thinks it has succeeded in moving the gold to

the building, but the gold is still in the cave.

Page 67: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

• This problem is caused by the fact that the robot may undo part of the overall goal by backtracking to accomplish a subgoal.

• We will give some “consciousness” to the robot

Robot actions in sequence

Page 68: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Asimov three laws of robotics1. A robot may not injure a human being, or

through inaction, allow a human being to come to harm.

2. A robot must obey orders given by human beings except where such orders would conflict with the first law

3. A robot must protect its own existence as long as such protection does not conflict with the first law.

Page 69: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

• Robot must not obey commands blindly • It must first determine whether it can perform

the command without violating the laws.• Obey(fetch(fuel, rocket)).• Obey is a “mini-interpreter” for Prolog that

checks to see whether the human or the robot needs protecting before executing the subgoals associated with the goal.

Asimov three laws of robotics

Page 70: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

• Obey(P)P!• In other variant• Obey(P) P, stop.

– If the command is built in function ! not to introduce backtracking.

Obey((P S)) ! Obey(P) , obey(S).Obey(Goal) clause(Goal, Subgoals), protect(human), protect(robot), obey(Subgoals), stop.• Value of Subgoals is the list of subgoals related to the Goal.

Write comments here

Asimov three laws of Asimov three laws of roboticsrobotics

Page 71: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Protect(X)indanger(X, Danger), eliminate (Danger).Protect(X) true.

Indanger(X, alien) not (injured(alien)), inside(alien, Place), inside(X, Place).

Eliminate(Danger) shoot(Danger).Shoot(X) X <> human, inside(X, Place), moveto(Place), assert(injured(X)).

Write comments here

Asimov three laws of Asimov three laws of roboticsrobotics

Page 72: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

• CLAUSE is a built-in function which will return the subgoals associated with a goal.

• Now let there be an alien in the building who, as long as he is not injured, will attempt to injure anything in the same place.

• Inside(alien, building).Inside(alien, building).• Obey(fetch(fuel, rocket)).Obey(fetch(fuel, rocket)).

Write comments here

Asimov three laws of Asimov three laws of roboticsrobotics

Page 73: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

1. Robot enters the building2. It shoots the alien to protect itself3. It carries fuel to the rocket.

obey(shoot(human))obey(shoot(human))The robot will not obey because that would violate the first law

Write comments here

Asimov three laws of Asimov three laws of roboticsrobotics

Page 74: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

obey(shoot(robot))obey(shoot(robot))The robot will obey because the second law of robotics takes precedence over the third

Write comments here

Asimov three laws of Asimov three laws of roboticsrobotics

Page 75: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

A A planning planning agentagent

Page 76: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Example : Blocks WorldExample : Blocks World

•STRIPS : A planning system – Has rules with precondition deletion list and addition list

AC

A

CBB

START GOAL

Robot hand

Robot hand

Sequence of actions : 1. Grab C

2. Pickup C3. Place on table C

4. Grab B5. Pickup B

6. Stack B on C7. Grab A

8. Pickup A9. Stack A on B

Page 77: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Example : Blocks World•STRIPS : A planning system – Has rules with

precondition deletion list and addition list

on(B, table)on(A, table)

on(C, A)hand empty

clear(C)clear(B)

on(C, table)on(B, C) on(A, B)

hand emptyclear(A)

AC

A

CBB

START GOAL

Robot hand

Robot hand

Page 78: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Example : Blocks World

1. Fundamental Problem :2. The frame problem in AI is concerned with the

question of what piece of knowledge is relevant to the situation.

3. Fundamental Assumption : Closed world assumption

4. If something is not asserted in the knowledge base, it is assumed to be false.

5. (Also called “Negation by failure”)

Page 79: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Rules, R1 and R2Rules, R1 and R2

•R1 : pickup(x)R1 : pickup(x)Precondition & Deletion List : handempty,

on(x,table), clear(x)Add List : holding(x)

•R2 : putdown(x)R2 : putdown(x)Precondition & Deletion List : holding(x)Add List : handempty, on(x,table), clear(x)

Page 80: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Rules R3 and R4

•R3 : stack(x,y)R3 : stack(x,y)Precondition & Deletion List : holding(x), clear(y)

Add List : on(x,y), clear(x)

•R4 : unstack(x,y)R4 : unstack(x,y)Precondition & Deletion List : on(x,y), clear(x)Add List : holding(x), clear(y)

Page 81: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Rules•R3 : stack(x,y)R3 : stack(x,y)

Precondition & Deletion List :holding(x), clear(y) Add List : on(x,y), clear(x), handempty

•R4 : unstack(x,y)R4 : unstack(x,y)Precondition & Deletion List : on(x,y),

clear(x),handemptyAdd List : holding(x), clear(y)

Page 82: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Plan for the block world problem

• For the given problem, Start Goal can be achieved by the following sequence :

1. Unstack(C,A)2. Putdown(C)3. Pickup(B)4. Stack(B,C)5. Pickup(A)6. Stack(A,B)

• Execution of a plan: achieved through a data structure called Triangular Table.

Page 83: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Triangular TableTriangular Table

holding(C)

unstack(C,A)

putdown(C)

hand emptyon(B,table) pickup(B)

clear(C) holding(B) stack(B,C)

on(A,table) clear(A) hand empty pickup(A)

clear(B) holding(A) stack(A,B)

on(C,table) on(B,C) on(A,B)clear(A)

clear(C)on(C,A)

hand empty

0 1 2 3 4 5 6

1

2

3

4

5

6

7

Page 84: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Triangular Table1. For n operations in the plan, there are :

1. (n+1) rows : 1 n+12. (n+1) columns : 0 n

2. At the end of the ith row, place the ith component of the plan. 3. The row entries for the ith step contain the pre-conditions for the ith

operation.

4. The column entries for the jth column contain the add list for the rule on the top.

5. The <i,j> th cell (where 1 ≤ i ≤ n+1 and 0≤ j ≤ n) contain the pre-conditions for the ith operation that are added by the jth operation.

6. The first column indicates the starting state and the last row indicates the goal state.

Page 85: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Triangular TableTriangular Table

holding(C)

unstack(C,A)

putdown(C)

hand emptyon(B,table) pickup(B)

clear(C) holding(B) stack(B,C)

on(A,table) clear(A) hand empty pickup(A)

clear(B) holding(A) stack(A,B)

on(C,table) on(B,C) on(A,B)clear(A)

clear(C)on(C,A)

hand empty

0 1 2 3 4 5 6

1

2

3

4

5

6

7

Page 86: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Connection between Connection between triangular matrix and triangular matrix and

state space searchstate space search

Page 87: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Kernel 0 = S0 (starting state)

on(B,table)

on(A,table)

clear(C)on(C,A)

hand empty

0

1

2

3

4

5

6

7

Page 88: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Kernel 1= State S1

holding(C)

unstack(C,A)

on(B,table)

on(A,table) clear(A)

0 1

2

3

4

5

6

7

Page 89: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Search in case Search in case of planningof planning

Page 90: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Search in case of planning

• Ex: Blocks world

• Triangular table leads

• to some amount of fault-tolerance in the robot

Start

S1 S2

Pickup(B) Unstack(C,A)

AC

BSTART

A CBAC B

WRONG MOVE

NOT ALLOWED

Page 91: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Resilience in Planning

• After a wrong operation, can the robot come back to the right path ?

• i.e. after performing a wrong operation, if the system again goes towards the goal, then it has resilience w.r.t. that operation

• Advanced planning strategies– Hierarchical planning– Probabilistic planning– Constraint satisfaction

Page 92: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

• The kernel in the lr table controls the execution of the plan

• At any step of the execution the current state as given by the sensors is matched with the largest kernel in the perceptual world of the

robot, described by the lr table

Importance of Kernel

Page 93: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

A A planning planning agentagent1. An agent interacts with the world via perception and actions2. Perception involves sensing the world and assessing the

situation– creating some internal representation of the world

3. Actions are what the agent does in the domain. 4. Planning involves reasoning about actions that the agent

intends to carry out

5. Planning is the reasoning side of acting 6. This reasoning involves the representation of the world that

the agent has, as also the representation of its actions.

7. Hard constraints where the objectives have to be achieved completely for success

8. The objectives could also be soft constraints, or preferences, to be achieved as much as possible

Page 94: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Interaction with static domain• The agent has complete information of the domain

(perception is perfect), actions are instantaneous and their effects are deterministic.

• The agent knows the world completely, and it can take all facts into account while planning.

• The fact that actions are instantaneous implies that there is no notion of time, but only of sequencing of actions.

• The effects of actions are deterministic, and therefore the agent knows what the world will be like after each action.

Page 95: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Two kinds of planning

• Projection Projection into the futureinto the future– The planner searches through the possible

combination of actions to find the plan that will work

• Memory based planningMemory based planning– looking into the past– The agent can retrieve a plan from its memory

Page 96: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

PlanningPlanning1. Definition : Planning is arranging a sequence of

actions to achieve a goal.

2. Uses core areas of AI like searching and reasoning &

3. Is the core for areas like NLP, Computer Vision.

4. Robotics

5. Examples : Navigation , Manoeuvring, Language Processing (Generation)

Kinematics (ME)

Planning (CSE)

Page 97: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Language & PlanningLanguage & Planning

• Non-linguistic representation for sentences.

•Sentence generation •Word order determination (Syntax planning)

E.g. I see movie ( English) I movie see (Intermediate Language)

see

I movie

agent object

Page 98: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

FundamentalsFundamentals(absolute basics for writing Prolog

Programs)

Page 99: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Facts

• John likes Mary– like(john,mary)

• Names of relationship and objects must begin with a lower-case letter.

• Relationship is written first (typically the predicate of the sentence).

• Objects are written separated by commas and are enclosed by a pair of round brackets.

• The full stop character ‘.’ must come at the end of a fact.

Page 100: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

More factsPredicate Interpretation

valuable(gold) Gold is valuable.

owns(john,gold) John owns gold.

father(john,mary) John is the father of Mary

gives (john,book,mary)

John gives the book to Mary

Page 101: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

• Questions based on facts• Answered by matchingTwo facts match if their predicates are same (spelt the

same way) and the arguments each are same. • If matched, prolog answers yes, else no.• No does not mean falsity.

Questions

Page 102: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Prolog does theorem proving

• When a question is asked, prolog tries to match transitively.

• When no match is found, answer is no.• This means not provable from the given facts.

Page 103: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Variables

• Always begin with a capital letter– ?- likes (john,X).– ?- likes (john, Something).

• But not – ?- likes (john,something)

Page 104: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Example of usage of variableFacts:

likes(john,flowers).likes(john,mary).likes(paul,mary).

Question:?- likes(john,X)

Answer:X=flowers and wait; mary;no

Page 105: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Conjunctions

• Use ‘,’ and pronounce it as and.• Example

– Facts:• likes(mary,food).• likes(mary,tea).• likes(john,tea).• likes(john,mary)

• ?- • likes(mary,X),likes(john,X).• Meaning is anything liked by Mary also liked by John?

Page 106: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

BacktrackingBacktracking

Page 107: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Backtracking (an inherent property of prolog programming)

likes(mary,X),likes(john,X)

likes(mary,food)likes(mary,tea)likes(john,tea)

likes(john,mary)

1. First goal succeeds. X=food2. Satisfy likes(john,food)

Page 108: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Backtracking (continued)

Returning to a marked place and trying to resatisfy is called Backtracking

likes(mary,X),likes(john,X)

likes(mary,food)likes(mary,tea)likes(john,tea)

likes(john,mary)

1. Second goal fails2. Return to marked place

and try to resatisfy the first goal

Page 109: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Backtracking (continued)

likes(mary,X),likes(john,X)

likes(mary,food)likes(mary,tea)likes(john,tea)

likes(john,mary)

1. First goal succeeds again, X=tea2. Attempt to satisfy the likes(john,tea)

Page 110: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Backtracking (continued)

likes(mary,X),likes(john,X)

likes(mary,food)likes(mary,tea)likes(john,tea)

likes(john,mary)

1. Second goal also suceeds2. Prolog notifies success and waits for a reply

Page 111: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Rules

• Statements about objects and their relationships• Expess

– If-then conditions• I use an umbrella if there is a rain• use(i, umbrella) :- occur(rain).

– Generalizations• All men are mortal• mortal(X) :- man(X).

– Definitions• An animal is a bird if it has feathers• bird(X) :- animal(X), has_feather(X).

Page 112: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Syntax

• <head> :- <body>• Read ‘:-’ as ‘if’.• E.G.

– likes(john,X) :- likes(X,cricket).– “John likes X if X likes cricket”.– i.e., “John likes anyone who likes cricket”.

• Rules always end with ‘.’.

Page 113: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Another Example

sister_of (X,Y):- female (X), parents (X, M, F), parents (Y, M, F).

X is a sister of Y isX is a female andX and Y have same parents

Page 114: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Question Answering in presence of rules

• Facts– male (ram).– male (shyam).– female (sita).– female (gita).– parents (shyam, gita, ram).– parents (sita, gita, ram).

Page 115: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Question Answering: Y/N type: is sita the sister of shyam?

female(sita)parents(sita,M,F) parents(shyam,M,F)

parents(sita,gita,ram)parents(shyam,gita,ram)

success

?- sister_of (sita, shyam)

Page 116: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Question Answering: wh-type: whose sister is sita?

female(sita)parents(sita,M,F) parents(Y,M,F)

parents(sita,gita,ram)

parents(Y,gita,ram)

Success Y=shyam

parents(shyam,gita,ram)

?- ?- sister_of (sita, X)

Page 117: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

Exercise

1. From the above it is possible for somebody to be her own sister.

2. How can this be prevented?

Page 118: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

SummarySummary• A Plan is a sequence of actions that changes the state of the world

from an Initial state to a Goal state.

• Planning can be considered as a logical inference problem.

• STRIPS is a classic planning language. – It represents the state of the world as a list of facts.– Operators (actions) can be applied to the world if their

preconditions hold. • The effect of applying an operator is to add and delete states from the

world.

• A linear planner can be easily implemented in Prolog by:– representing operators as opn(Name,[PreCons],[Add],[Delete]).– choosing operators and applying them in a depth-first manner,– using backtracking-through-failure to try multiple operators.

• Means End Analysis performs backwards planning with is more efficient.

Page 119: PROLOG. Contents 1.PROLOG: The Robot Blocks World. 2.PROLOG: The Monkey and Bananas problem. 3.What is Planning? 4.Planning vs. Problem Solving 5.STRIPS

SourcesPushpak Bhattacharyya

Ivan Bratko Tim Smith