0% found this document useful (0 votes)
54 views61 pages

Artificial Intelligence: Lecture 4: Problem Solving Search

The document describes problem formulation for the 8-puzzle problem and river crossing problem using search methods. For the 8-puzzle problem, it defines the initial state, goal state, operators to move tiles, and path cost. For the river crossing problem, it defines the initial state of farmer and items on the south shore, goal state with items on north shore, and operators for the farmer to carry items across in the boat. Sample solutions and path costs are provided for both problems.

Uploaded by

rferreira85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views61 pages

Artificial Intelligence: Lecture 4: Problem Solving Search

The document describes problem formulation for the 8-puzzle problem and river crossing problem using search methods. For the 8-puzzle problem, it defines the initial state, goal state, operators to move tiles, and path cost. For the river crossing problem, it defines the initial state of farmer and items on the south shore, goal state with items on north shore, and operators for the farmer to carry items across in the boat. Sample solutions and path costs are provided for both problems.

Uploaded by

rferreira85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Artificial Intelligence

Lecture 4: Problem Solving Search


Dr. Nada Ghneim
[email protected]
8-Puzzle problem
 Solve the following 8-Puzzle problem by moving tiles left, down, up
and right.

1 2 3 1 2 3

4 8 4 5 6

7 6 5 7 8

Initial State goal State

2/16
8-Puzzle
Problem formulation 1 3

State Representation: matrix of tiles 2 8 4


7 6 5
1 2 3
Initial state 4 8
7 6 5

1 2 3
Goal State 4 5 6
7 8

Operators:
slide-blank-up, slide-blank-down,
slide-blank-left, slide-blank-right
3/16
Path Cost: The number of steps to reach the goal state
Problem Formulation

A Problem Space consists of

 The current state of the world (initial state)

 A description of the actions we can take to transform one state of the


world into another (operators).

 A description of the desired state of the world (goal state), this could be
implicit or explicit.

 A solution consists of the goal state, or a path to the goal state.


4/16
Problem Formulation :8-Puzzle Problem

Initial State Operators Goal State

2 1 3 1 2 3
Slide blank square left.
4 7 6 Slide blank square right. 4 5 6
….
5 8 7 8

5/16
Problem Formulation :8-Puzzle Problem
Representing states:

 3 by 3 array
 5, 6, 7 5 6 7
 8, 4, BLANK
8 4
 3, 1, 2
3 1 2
 A vector of length nine
 5,6,7,8,4, BLANK,3,1,2

 A list of facts
 Upper_left = 5

 Upper_middle = 6
6/16
 Upper_right = 7

 Middle_left = 8
Problem Formulation :8-Puzzle Problem

Initial state 1 2 3
4 8
7 6 5

Goal state 1 2 3
4 5 6
7 8

Operators: slide blank up, slide blank down, slide blank left, slide blank right

Solution: ?

Path cost: ?
7/16
Problem Formulation: 8-Puzzle Problem
Solution1: sb-down, sb-left, sb-up, sb-right, sb-down

Operators: slide blank up, slide blank down, slide blank left, slide blank right

1 2 3 1 2 3 1 2 3
4 8 4 8
5 5
6 4 5 6
7 6 5 7 6
8 5
6 7 8

Initial state Goal state

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

8/16
Path cost: 5 steps to reach the goal
Problem Formulation: 8-Puzzle Problem

Solution2: sb-left, sb-down, sb-right, sb-up, sb-left, sb-down, sb-right

1 2 3 1 2 3 1 2 3
4 8 4 8
6
5 8
6 4 5 6
7 6 5 7 6
5
8 5
8 7 8

1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
4 8 4 8 4 6 8 4 6 8 4 6 4 6
7 6 5 7 6 5 7 5 7 5 7 5 8 7 5 8

1 2 3 1 2 3
4 5 6 4 5 6
9/16
Path cost: 6 steps to reach the goal 7 8 7 8
Problem Formulation: River problem

 consider 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 is the proud owner of a small rowing boat called Bounty which he feels is
easily up to the job. Unfortunately the boat is only large enough to carry at most the farmer and
one other item. Worse again, if left unattended the wolf will eat the duck and the duck will eat
the corn.

Farmer, Wolf,
Duck and Corn

10/16
 Give a Formulation for this problem.
Problem Formulation: River problem

 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])

11/16
 Path cost: the number of crossings
Problem Formulation: River problem

Solution: F-Takes-D, F-Takes-Self, F-Takes-W, F-Takes-D, F-Takes-C, F-Takes-


Self, F-Takes-D.

path Cost = 7 (Problem solution)

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
12/16
F-Takes-D F-Takes-S F-Takes-C
F D D F D C
Goal State FD/WC D/FWC FDC/W
Problem Formulation: River problem
by search Method

F WD C
F-Takes-D, F-Takes-Self, F-Takes-W,
F-Takes-D, F-Takes-C, F-Takes-Self, F
WD C
FW
DC
F
W C
D F
WD
C

F-Takes-D.
FW C F WD C
D

W C C W
F D F WD F DC

F C FW C F DC FW F WD FW C
WD D W DC C D

DC C D WD D W
FW F WD FW C F C FW C F DC

F D F WD F DC
W C C W

D
FW C F WD C
Problem Formulation: Missionaries and cannibals

 Three missionaries and three cannibals are on the left bank of a river.

 There is one canoe which can hold one or two people.

 Find a way to get everyone to the right bank, without ever leaving a group of
missionaries in one place outnumbered by cannibals in that place.

Initial state: (3, 3, 1) Goal State: (0,0,0)


Problem Formulation: Missionaries and cannibals

 States Representation: three numbers (i, j, k) representing the number of


missionaries, cannibals, and canoes 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 (I.e. ten operators).

 Goal Test: reached state (0, 0, 0) or Goal State: (0,0,0)

 Path Cost: Number of crossings.


Problem Formulation: Missionaries and cannibals

Solution : [ (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

Operations (i, j, k)

16/16

Initial state: (3, 3, 1) Goal State: (0,0,0)


Example: robotic assembly

 states?: real-valued coordinates of robot joint angles parts of the object to be assembled

 actions?: continuous motions of robot joints

 goal test?: complete assembly

 path cost?: time to execute


Vacuum world state space graph

 states? integer dirt and robot location


 actions? Left, Right, Suck
 goal test? no dirt at all locations
 path cost? 1 per action
Example: Romania
 On holiday in Romania; currently in Arad.
 Flight leaves tomorrow from Bucharest

 Formulate goal:
 be in Bucharest

 Formulate problem:
 states: various cities
 actions: drive between cities

 Find solution:
 sequence of cities, e.g., Arad, Sibiu, Fagaras, Bucharest
Single-state problem formulation

1. initial state e.g., "at Arad“

2. actions or successor function S(x) = set of action–state pairs


 e.g., S(Arad) = {<Arad  Zerind, Zerind>, … }

3. goal test, can be


 explicit, e.g., x = "at Bucharest"

4. path cost (additive)


 e.g., sum of distances, number of actions executed, etc.
 c(x,a,y) is the step cost, assumed to be ≥ 0

 A solution is a sequence of actions leading from the initial state to a goal state
Tree search algorithms
Basic idea:
 offline, simulated exploration of state space by generating successors of
already-explored states (a.k.a.~expanding states)
Tree search example
Tree search example
Tree search example
Implementation: states vs. nodes

 A state is a (representation of) a physical configuration


 A node is a data structure constituting part of a search tree includes state, parent node,
action, path cost g(x), depth

 The Expand function creates new nodes, filling in the various fields and using the
SuccessorFn of the problem to create the corresponding states.
Implementation: general tree search

26
Search strategies
 A search strategy is defined by picking the order of node expansion
 Strategies are evaluated along the following dimensions:
 completeness: does it always find a solution if one exists?
 time complexity: number of nodes generated
 space complexity: maximum number of nodes in memory
 optimality: does it always find a least-cost solution?

 Time and space complexity are measured in terms of


 b: maximum branching factor of the search tree
 d: depth of the least-cost solution
 m: maximum depth of the state space (may be ∞)
Uninformed search strategies
Uninformed search strategies use only the information
available in the problem definition
 Breadth-first search
 Uniform-cost search
 Depth-first search
 Depth-limited search
 Iterative deepening search
Breadth-first search
Expand shallowest unexpanded node

Implementation:
 fringe is a FIFO queue, i.e., new successors go at end
Breadth-first search
Expand shallowest unexpanded node

Implementation:
fringe is a FIFO queue, i.e., new successors go at end
Breadth-first search

Expand shallowest unexpanded node

Implementation:
 fringe is a FIFO queue, i.e., new successors go at end
Breadth-first search

Expand shallowest unexpanded node

Implementation:
 fringe is a FIFO queue, i.e., new successors go at end
Properties of breadth-first search

Complete? Yes (if b is finite)

Time? 1+b+b2+b3+… +bd + b(bd-1) = O(bd+1)

Space? O(bd+1) (keeps every node in memory)

Optimal? Yes (if cost = 1 per step)

Space is the bigger problem (more than time)


Uniform-cost search
 Expand least-cost unexpanded node
 Implementation:
 fringe = queue ordered by path cost

 Equivalent to breadth-first if step costs all equal


 Complete? Yes, if step cost ≥ ε
 Time? # of nodes with g ≤ cost of optimal solution, O(b(C*/ ε)) where C* is the cost of
the optimal solution
 Space? # of nodes with g ≤ cost of optimal solution, O(b(C*/ ε))
 Optimal? Yes – nodes expanded in increasing order of g(n)
Depth-first search
Expand deepest unexpanded node
Implementation:
 fringe = LIFO queue, i.e., put successors at front
Depth-first search
Expand deepest unexpanded node
Implementation:
 fringe = LIFO queue, i.e., put successors at front
Depth-first search
Expand deepest unexpanded node
Implementation:
 fringe = LIFO queue, i.e., put successors at front
Depth-first search
Expand deepest unexpanded node
Implementation:
 fringe = LIFO queue, i.e., put successors at front
Depth-first search
Expand deepest unexpanded node
Implementation:
 fringe = LIFO queue, i.e., put successors at front
Depth-first search
Expand deepest unexpanded node
Implementation:
 fringe = LIFO queue, i.e., put successors at front
Depth-first search
Expand deepest unexpanded node
Implementation:
 fringe = LIFO queue, i.e., put successors at front
Depth-first search
Expand deepest unexpanded node
Implementation:
 fringe = LIFO queue, i.e., put successors at front
Depth-first search
Expand deepest unexpanded node
Implementation:
 fringe = LIFO queue, i.e., put successors at front
Depth-first search
Expand deepest unexpanded node
Implementation:
 fringe = LIFO queue, i.e., put successors at front
Depth-first search

Expand deepest unexpanded node


Implementation:
 fringe = LIFO queue, i.e., put successors at front
Depth-first search
Expand deepest unexpanded node
Implementation:
 fringe = LIFO queue, i.e., put successors at front
Properties of depth-first search

Complete? No: fails in infinite-depth spaces, spaces with loops


 Modify to avoid repeated states along path
 complete in finite spaces

Time? O(bm): terrible if m is much larger than d


 but if solutions are dense, may be much faster than breadth-first

Space? O(bm), i.e., linear space!

Optimal? No
Depth-limited search
= depth-first search with depth limit l,
i.e., nodes at depth l have no successors
Recursive implementation:

49
Iterative deepening search
Iterative deepening search l =0
Iterative deepening search l =1
Iterative deepening search l =2
Iterative deepening search l =3
Iterative deepening search
 Number of nodes generated in a depth-limited search to depth d with branching factor b:
NDLS = b0 + b1 + b2 + … + bd-2 + bd-1 + bd

 Number of nodes generated in an iterative deepening search to depth d with branching


factor b:
NIDS = (d+1)b0 + d b^1 + (d-1)b^2 + … + 3bd-2 +2bd-1 + 1bd

 For b = 10, d = 5,

 NDLS = 1 + 10 + 100 + 1,000 + 10,000 + 100,000 = 111,111

 NIDS = 6 + 50 + 400 + 3,000 + 20,000 + 100,000 = 123,456

 Overhead = (123,456 - 111,111)/111,111 = 11%


Properties of iterative deepening search
Complete? Yes

Time? (d+1)b0 + d b1 + (d-1)b2 + … + bd = O(bd)

Space? O(bd)

Optimal? Yes, if step cost = 1


Summary of algorithms
Repeated states
Failure to detect repeated states can turn a linear problem
into an exponential one!

Graph search
Summary
 Problem formulation usually requires abstracting away real-world details to define
a state space that can feasibly be explored

 Variety of uninformed search strategies

 Iterative deepening search uses only linear space and not much more time than
other uninformed algorithms

You might also like