You are on page 1of 39

Knowledge

Representation
Shaishav Agrawal
Lecture 5, 6, 7,8

Using Search in Problem Solving


• Intro
• Basic Search Techniques
• Heuristic Search
Problem Solving
• Define the problem precisely. (Precise
Specifications).
• Define Initial and goal situation.
• Analyze the problem. (i.e. some important
features can impact the appropriateness of the
possible design technique).
• Isolate and represent the task knowledge.
• Choose the best problem solving technique.
Intro: Search and AI
• In solving problems, we sometimes have to
search through many possible ways of doing
something.
o We may know all the possible actions our
robot can do, but we have to consider various
sequences to find a sequence of actions to
achieve a goal.
o We may know all the possible moves in a
chess game, but we must consider many
possibilities to find a good move.
• Many problems can be formalised in a
general way as search problems.
Search and Problem
Solving
• Search problems described in terms of:
o An initial state. (e.g., initial chessboard, current
positions of objects in world, current location)
o A target state.(e.g., winning chess position,
target location)
o Some possible actions, that get you from one
state to another. (e.g. chess move, robot action,
simple change in location).
• Search techniques systematically consider all
possible action sequences to find a path from
the initial to target state.
Problem solving as search
• How can we formulate more interesting
problems as search?
• Have to think of problems in terms of initial state,
target state, and primitive actions that change
state.
• Consider:
o Game playing: actions are moves, which change
the board state.
o Planning robot behaviours: actions are basic
moves, like “open door”, or “put block1 on top of
block2”, which change situation/state.
Robot planning problem.
• Consider pet robot (or not very intelligent flat-
mate) in small flat with two rooms. You and
your robot are in room1, your beer is in room
2, the door is closed between the rooms.
• Actions:
o move(robot, Room, AnotherRoom)
o open(robot, door)
o pickup(robot, Object).
• Initial state:
o in(robot, room1) etc.
Robot planning search
tree
Me
Rob Beer
Robit opens Robot picks up
door Me
Me Me
Rob Beer Rob Beer

Robot moves to next


room
Me Rob Etc
Beer etc
Or.. To solve a puzzle
• “You are given two jugs, a 4 gallon one, and a 3
gallon one. Neither has any measuring markers
on it. There is a tap that can be used to fill the
jugs with water. How can you get exactly 2 gallons
of water in the 4 gallon jug?”
• How do we represent the problem state? Can
represent just as pair or numbers.
o {4, 1} means 4 gallons in 4 gallon jug, 1 gallon in 3
gallon jug.
• How do we represent the possible actions.
o Can give simple rules for how to get from old to
new state given various actions.
Jug actions
• 1. Fill 4-gallon jug. {X, Y} -> {4, Y}
• 2. Fill 3-gallon jug. {X, Y} -> {X, 3}
• 3. Empty 4 gallon jug into 3 gallon jug.
{X, Y} -> {0, X+Y} (but only OK if X+Y <= 3)
• 4. Fill the 4 gallon jug from the 3 gallon jug.
{X, Y} -> {4, X+Y-4} (if X+Y > 4)
• etc (full set given in textbook}
Search Tree for Jugs
{0, 0}
Fill 4 gallon Fill 3 gallon

{4, 0} {0, 3}
Fill 3 gallon Fill 3 gallon from 4
gallon
{4, 3} {1, 3}

.. And so
on.
Chess Problem
8
7
6
5
4
3
2
1
a b c d e f g h
Approach
• Specify the starting position of chess board.
• The rules that define legal moves.
• The winning board position.
• The main goal is not only to make legal moves but
also to win the game.
Problems in defining rules
• 10120 separate rules may be possible.
• Can any single person make all possible rules?
• Can any program easily handle all such rules?
Solution
• Generalization of rules.
Example of one generalized
rule
White pawn at square (file e, rank 2) AND
Square (file e, rank 3) is empty AND
Square (file e, rank 4) is empty AND →
Move pawn from Square (file e, rank 2) to
Square (file e, rank 4)

Other rules can be designed for:


Rook, Queen, Bishop, Knight, King.
So..
• To solve a moderately complex puzzle what we
can do is:
o Express it in terms of search.
o Decide how “problem state” may be expressed
formally.
o Decide how to encode primitive actions as rules
for getting from one state to another.
o Use a standard tree/graph search
algorithm/program, which uses uses a general
“successor state” function which you define for
your problem.
Problem Characteristics
• Is the problem decomposable into a set of
independent sub problems?
• Can solution steps be ignored or at least undone if
they prove unwise?
• Is the problem’s universe predictable?
• Is a good solution to the problem obvious without
comparison to all other possible solutions?
• Is the desired solution a state or a path to state?
Problem Characteristics
• Is a large knowledge absolutely required to solve
the problem or the knowledge important only to
constrain the search?
• Can Computer give the solution or will the
solution require interaction between computer and
person?
Production System
Characteristics
• Production systems are a good way to describe
the operations that can be performed in a search
for a solution to a problem.
Some questions arises while designing a production
system:
1. Are production systems and problems same?
2. Can these also be described by a set of
characteristics?
3. If yes then what relationships are there between
problem types and the types of production systems
best suited to solve the problems?
Monotonic Production
systems
• It is a production system in which the application of
a rule never prevents the later application of
another rule that could also have been applied at
the time the first rule was selected.
Non Monotonic Production
System
• It is one in which the previous one is not true.
Partially cumulative
production system
• It is a production system with the property that if
the application of a particular sequence of rules
transforms state x into state y, then any
permutations of these rules that is allowable also
transforms state x into state y.
• i.e. each rule’s preconditions are satisfied when it
is applied.
Cumulative production
system
• It is a production system that is both monotonic
and partially cumulative.
Monotonic Non Monotonic
Comparision
Partially Cumulative Theorem Proving Robot Navigation

Non Partially Cumulative Chemical synthesis Bridge


Simple Example
• Easiest to first look at simple examples
based on searching for route on a map.
School Factory

Hospital Newsagen church


Library
t
• How do we systematically
Park and University
exhaustively
search possible routes, in order to find, say,
route from library to university?
Search Space
• The set of all possible states reachable from
the initial state defines the search space.
• We can represent the search space as a tree.

library

school hospital

factory park newsage


• We refer to nodes connected to and nt “under” a
node in the tree as “successoruniversity
nodes”. church
Simple Search Techniques
• How do we search this tree to find a possible
route from library to University?
• May use simple systematic search techniques,
which try every possibility in systematic way.
• Breadth first search - Try shortest paths first.
• Depth first search - Follow a path as far as it
goes, and when reach dead end, backup and try
last encountered alternative.
Breadth first search
Explore nodes in tree order: library,
school,
hospital, factory, park, newsagent, uni,
library
church.
(conventionally explore left to right at each level)
school hospital

factory park newsage


nt
university
church
Depth first search
• Nodes explored in order: library, school,
factory, hospital, park, newsagent,
university.
library

school hospital

factory park newsage


nt
university
Algorithms for breadth first
and depth first search.
• Very easy to implement algorithms to do these
kinds of search.
• Both algorithms keep track of the list of nodes
found, but for which routes from them have yet
to be considered.
o E.g., [school, hospital] -have found school and
hospital in tree, but not yet considered the
nodes connected to these.
• List is sometimes referred to as an agenda. But
implemented using stack for depth first, queue
for breadth first.
Algorithm for breadth first:
• Start with queue = [initial-state] and
found=FALSE.
• While queue not empty and not found do:
o Remove the first node N from queue.
o If N is a goal state, then found = TRUE.
o Find all the successor nodes of N, and put them
on the end of the queue.
Algorithm for depth first:
• Start with stack = [initial-state] and
found=FALSE.
• While stack not empty and not found do:
o Remove the first node N from stack.
o If N is a goal state, then found = TRUE.
o Find all the successor nodes of N, and put them
on the top of the stack.
Note: Detailed workthrough of algorithms and
discussion of trees/graphs in textbook.
Extensions to basic
algorithm
• Loops: What if there are loops (ie, we are
search a graph)? How do you avoid (virtually)
driving round and round in circles?
o Algorithm should keep track of which nodes
have already been explored, and avoid redoing
these nodes.
• Returning the path: How do you get it to
actually tell you what the path it has found is!
o One way: Make an item on the agenda be a
path, rather than a node.
Heuristic search
algorithms.
• Depth first and breadth first search turn out to be too
inefficient for really complex problems.
• Instead we turn to “heuristic search” methods, which
don’t search the whole search space, but focus on
promising areas.
• Simplest is best first search. We define some
“heuristic evaluation function” to say roughly how
close a node is to our target.
o E.g., map search: heuristic might be “as the crow
flies” distance based on map coords,
o Jug problem: How close to 2 gallons there are in 4
gallon jug.
Best first search algorithm
• Best first search algorithm almost same as
depth/breadth.. But we use a priority queue,
where nodes with best scores are taken off the
queue first.
• While queue not empty and not found do:
o Remove the BEST node N from queue.
o If N is a goal state, then found = TRUE.
o Find all the successor nodes of N, assign them
a score, and put them on the queue..
Best first search
• Order nodes searched: Library, hospital, park,
newsagent, university.
Library (6)

School Hospital
(5) (3)
Factory Park Newsagent
(4) (1) (2)
University (0)
Other heuristic search
methods
• Hill climbing: always choose successor node
with highest score.
• A*: Score based on predicted total path “cost”,
so sum of
o actual cost/distance from initial to current node,
o predicted cost/distance to target node.
Summary
• General search methods can be used to
solve complex problems.
• Problems are formulated in terms of initial
and target state, and the primitive actions that
take you from one state to next.
• May need to use heuristic search for complex
problems, as search space can be too large.

You might also like