W3-Problem Representation and Searches
W3-Problem Representation and Searches
2
Problem Solving: Hit and Trial
The classical approach to solving a problem is pretty simple. Given
a problem at hand use hit and trial method to check for various
solutions to that problem.
This hit and trial approach usually works well for trivial problems
and is referred to as the classical approach to problem solving.
3
Problem Solving: Hit and Trial
Consider the maze searching problem. The mouse travels
though one path and finds that the path leads to a dead end, it
then back tracks somewhat and goes along some other path
and again finds that there is no way to proceed. It goes on
performing such search, trying different solutions to solve
the problem until a sequence of turns in the maze takes it to
the cheese. Hence, of all the solutions the mouse tries, the
one that reached the cheese was the one that solved the
problem.
4
Problem Solving by Searching
Why search ?
Early works of AI was mainly towards
proving theorems
solving puzzles
playing games
All AI is search!
Not totally true (obviously) but more true than you
might think.
5
Introduction
As in this problem:
A navigation problem
There are many choices to start
with
Complexity comes from picking
the right choice now and picking
the right choice at the next
intersection and the intersection
after that.
Streaming together a sequence of
actions.
6
In contrast in the picture below:
Complexity ?
the partial observability that we can't see through the fog where the possible
paths are.
We can't see the results of our actions and even the actions themselves are
not known.
This type of complexity will be covered later.
7
What we study now
Several general-purpose search algorithms that can be used to solve
these problems.
1. Blind/Uninformed search algorithms
algorithms that are given no information about the problem other than its
definition
Although some of these algorithms can solve any solvable problem, none
of them can do so efficiently
2. Informed search algorithms/Heuristic
can do quite well given some guidance on where to look for solutions
8
Blind/Uninformed search algorithms
Suppose the mouse does not know where and how far is the
cheese and is totally blind to the configuration of the maze.
The mouse would blindly search the maze without any hints
that will help it turning left or right at any junction.
The mouse will purely use a hit and trial approach and will
check all combinations till one takes it to the cheese. Such
searching is called blind or uninformed searching.
9
Informed search algorithms/Heuristic
Consider now that the cheese is fresh and the smell of cheese
is spread through the maze. The mouse will now use this
smell as a guide, or heuristic (we will comment on this word
in detail later) to guess the position of the cheese and choose
the best from the alternative choices.
As the smell gets stronger, the mouse knows that the cheese
is closer. Hence the mouse is informed about the cheese
through the smell and thus performs an informed search in
the maze.
For now you might think that the informed search will
always give us a better solution and will always solve our
problem.
10
Any-path/non optimal searches
In any-path/non optimal searches we are concerned with
finding any one solution to our problem.
As soon as we find a solution, we stop, without thinking that
there might as well be a better way to solve the problem
which might take lesser time or fewer operators.
11
Optimal path searches
Contrary to this, in optimal path searches we try to find the
best solution. For example, in the diagram the optimal path is
the blue one because it is smaller and requires lesser
operators.
Hence in optimal searches we find solutions that are least
costly, where cost of the solution may be different for each
problem.
12
Assumptions about the task
environment
static
single agent
deterministic
observable
discrete
13
Classic AI Search Problems
Map searching (navigation)
14
Classic AI Search Problems
8-Puzzle
2 1 3 1 2 3
4 7 6 4 5 6
5 8 7 8
15
Classic AI Search Problems
N-Queens
Problem of placing n chess queens on an n×n chessboard so that
no two queens attack each other
A solution requires that no two queens share the same row,
column, or diagonal
16
Classic AI Search Problems
5-Queens:
1 2 3 4 5
1
5
17
Classic AI Search Problems
5-Queens:
1 2 3 4 5
1
5
18
Classic AI Search Problems
5-Queens:
1 2 3 4 5
1
5
19
Classic AI Search Problems
5-Queens:
1 2 3 4 5
1
5
20
Classic AI Search Problems
5-Queens:
1 2 3 4 5
1
5
21
Classic AI Search Problems
5-Queens:
1 2 3 4 5
1
2
Solution !!
No Queen is 3
under Attack
4
5
22
Missionaries and cannibals
Three missionaries and three cannibals are on the left bank of
a river.
23
Missionaries and Cannibals
Initial State
24
Missionaries and Cannibals
Goal State
25
The River Problem
A farmer wishes to carry a wolf, a duck and corn across a river, from the
south to the north shore. The farmer has a small rowing boat. The boat
can only carry at most the farmer and one other item.
If left unattended the wolf will eat the duck and the duck will eat the
corn.
Farmer, Wolf,
Duck and Corn
River River boat
boat
Farmer, Wolf,
Duck and Corn
How can the farmer safely transport the wolf, the duck and the corn to
the opposite shore?
26
Problem Solving by Searching
Problem Formulation
Search Problem Formulation
Agent:entity that perceives its environment and acts upon
that environment
State: a configuration of the agent and its environment
28
Search Problem Formulation
Actions: choices that can be made in a state
ACTIONS(s) returns the set of actions that can be executed in
state s
29
Search Problem Formulation
Transition Model
A description of what state results from performing any
applicable action in any state
RESULT(s, a) returns the state resulting from performing
action a in state s
30
Search Problem Formulation
State Space
the set of all states reachable from the initial state by any
sequence of actions
31
Search Problem Formulation
Goal Test
way to determine whether a given state is a goal state
Path Cost
numerical cost associated with a given path
32
Search Problem Formulation
Solution:
a sequence of actions that leads from the initial state to a goal
state
Optimal Solution
a solution that has the lowest path cost among all solutions
33
Problem Formulation
A Problem Space consists of
35
Problem Formulation
8-Puzzle Problem
36
Problem Formulation
8-Puzzle Problem
Representing states:
For the 8-puzzle
3 by 3 array
5, 6, 7 5 6 7
8, 4, BLANK
3, 1, 2 8 4
A list of facts
Upper_left = 5
Upper_middle = 6
Upper_right = 7
37 Middle_left = 8
Problem Formulation
8-Puzzle Problem
Specifying operators
There are often many ways to specify the operators, some will be
much easier to implement...
• Move 1 left
• Move 1 right
• Move 1 up
• Move 1 down 5 6 7
• Move 2 left • Move Blank left
• Move 2 right • Move Blank right 8 4
• Move 2 up • Move Blank up
• Move 2 down • Move Blank down
• Move 3 left
3 1 2
• Move 3 right
• Move 3 up
• Move 3 down
• Move 4 left
38
• …
Problem Formulation
8-Puzzle Problem
1 2 3 1 2 3
4 8 4 5 6
7 6 5 7 8
Operators: slide blank up, slide blank down, slide blank left,
slide blank right
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
4 8 4 8 5 4 8 5 4 5 4 5 4 5 6
7 6 5 7 6 7 6 7 8 6 7 8 6 7 8
40
Missionaries and cannibals
States: three numbers (i, j, k) representing the number of
missionaries, cannibals, and boats on the left bank of the
river.
Initial state: (3, 3, 1)
Operators: take one missionary, one cannibal, two
missionaries, two cannibals, one missionary and one cannibal
across the river in a given direction
Goal Test: reached state (0, 0, 0)?
Path Cost: Number of crossings.
41
Missionaries and Cannibals
(3,3,1): Initial State
42
Missionaries and Cannibals
A missionary and cannibal cross
43
Missionaries and Cannibals
(2,2,0)
44
Missionaries and Cannibals
One missionary returns
45
Missionaries and Cannibals
(3,2,1)
46
Missionaries and Cannibals
Two cannibals cross
47
Missionaries and Cannibals
(3,0,0)
48
Missionaries and Cannibals
A cannibal returns
49
Missionaries and Cannibals
(3,1,1)
50
Missionaries and Cannibals
Two missionaries cross
51
Missionaries and Cannibals
(1,1,0)
52
Missionaries and Cannibals
A missionary and cannibal return
53
Missionaries and Cannibals
(2,2,1)
54
Missionaries and Cannibals
Two Missionaries cross
55
Missionaries and Cannibals
(0,2,0)
56
Missionaries and Cannibals
A cannibal returns
57
Missionaries and Cannibals
(0,3,1)
58
Missionaries and Cannibals
Two cannibals cross
59
Missionaries and Cannibals
(0,1,0)
60
Missionaries and Cannibals
A cannibal returns
61
Missionaries and Cannibals
(0,2,1)
62
Missionaries and Cannibals
The last two cannibals cross
63
Missionaries and Cannibals
(0,0,0) : Goal State
64
Missionaries and Cannibals
Solution = the sequence of actions within the path :
[(3,3,1)→ (2,2,0)→(3,2,1) →(3,0,0) →(3,1,1)
→(1,1,0) →(2,2,1) →(0,2,0) →(0,3,1) →(0,1,0) →
(0,2,1) →(0,0,0)]
Cost = 11 crossings
65
The River Problem
A farmer wishes to carry a wolf, a duck and corn across a river, from the
south to the north shore. The farmer has a small rowing boat. The boat
can only carry at most the farmer and one other item.
If left unattended the wolf will eat the duck and the duck will eat the
corn.
Farmer, Wolf,
Duck and Corn
River River boat
boat
Farmer, Wolf,
Duck and Corn
How can the farmer safely transport the wolf, the duck and the corn to
the opposite shore?
66
The River Problem
-/FWCD
FWCD/-
How can the farmer safely transport the wolf, the duck and the corn to the
opposite shore?
67
The River Problem
Problem formulation:
State representation: location of farmer and items in both sides of river
[items in South shore / items in North shore] : (FWDC/-, FD/WC, C/FWD
…)
Initial State: farmer, wolf, duck and corn in the south shore
FWDC/-
Goal State: farmer, duck and corn in the north shore
-/FWDC
Operators: the farmer takes in the boat at most one item from
one side to the other side
(F-Takes-W, F-Takes-D, F-Takes-C, F-Takes-Self [himself only])
68
Path cost: the number of crossings
The River Problem
Problem solution: (path Cost = 7)
While there are other possibilities here is one 7 step solution to the river
problem
F D D F W D
F-Takes-D F-Takes-S F-Takes-W
F W D C W C F W C C
Initial State WC/FD FWC/D C/FWD
F-Takes-D
F W D C W C F W C W
72