0% found this document useful (0 votes)
19 views

Problem Solving

Uploaded by

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

Problem Solving

Uploaded by

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

Problem Solving

• The goal is to develop a program that can


solve puzzles:

• Solution ProblemSolver(problem){
… //algoithm
}
Problem Specification
• Initial state description
• Goal state description
• A set of operations
• [Sometimes] The cost of each operation
(needed to find the cost of the solution)
Problem Search Space
• The set of all states that can be reached
by applying a sequence of zero or more
operations starting at the initial state
• Represented as a directed graph. Nodes
are states and edges are operations
• A solution of the problem is a sequence of
operations along a path from the initial to
the goal state
8-puzzle
• Initial state 3 2 1
6 5 7
8 4

1 2 3

• Goal state 4 5 6
7 8

• Operations: move blank up, down, left,


right
• Cost for every operation = 1
• Search space size <= 9!=362,880
• Part of the search space …
Part of Search Space of 8-puzzle
3 2 1
6 5 7
right
8 4 left

left up right
down

3 2 1 3 2 1 3 2 1
6 5 7 6 7 6 5 7
8 4 8 5 4 8 4
Coins Problem
• Initial state HHH
• Goal state TTT
• Operations:
1. flip first coin
2. flip second coin
3. flip third coin
• Cost=1
• Search space and solutions …
Search Space for 3-coins Problem
HHH 1
2
HTH THH

1 2 3
3
TTH THT
3
3 1
2
TTT HHT

1 2
HTT
Missionaries and Canibals
• 3 missionaries (M) and 3 cannibals (C)
and a boat on the left bank of a river
• Boat carries only 1 or 2 persons at a time
• Boat cannot move alone
• Cannibals should not outnumber
missionaries on either side
• Find a way to move the six persons to the
right side
Missionaries and Canibals (cont.)
• Initial state (MMMCCCB,-)
• Goal state (-,MMMCCCB)
• Operations:
C C
CC CC
MC MC
MM MM
M M
• Cost=1. Other logical values?
• Search space size is 15 states
Missionaries and Canibals Search Space
Part of search space

MMMCCCB, ----
C <- MC <-
CC -> MC ->
C -> CC <-
MMMCC, CB MMMC, CCB MMCC, MCB
C <-
C ->
MMMCCB, C

CC <- CC ->

MMM,CCCB
C ->

Route Finding
Problem: Given a map (Graph) Find a route
from X to Y.
– Initial state: in X
– Goal state: in Y
– Operations: edges
– Cost: distance, travel time, …
– Search space is the graph itself
Example
Searching for solutions
• State expansion: applying operations and
finding resulting states. Examples from
coin, 8-puzzle, MC
• Searching for a solution is done by
building a search tree
Building Search tree
1. Initialize search tree to initial state (root)
2. Select a terminal node for expansion
using some search strategy. If no
candidates return failure
3. If the chosen node contains a goal state
then return the solution else expand the
node and add resulting nodes to the tree
4. Go to 2
Search tree examples
• Route finding
B D

A
G
E

• 8-puzzle
• 3-coins
• MC
Data structures for search tree
• To be able to return a solution, a node in the
search tree is a structure containing:
1. state
2. a pointer to the parent
3. the operator used to generate the state
4. cost = cost(parent) + cost(operator)
• Expand operation computes each of these
components
• Examples: 8-puzzle, MC
Search Tree Node Example
Initial state node in 8-puzzle
state Parent Operation Cost

2 3 2
4 7 NULL none 0
5 6 8
Search Strategies Evaluation
• Completeness: Guaranteeing reaching a
solution if it exists.
• Optimality: Guaranteeing reaching the
best solution.
• Time complexity
• Space complexity
Search Strategies Types
• Blind: No study of states
breadth-first, depth-first search, uniform-
cost search
• Informed: search strategy is based on
states evaluation
Breadth-First Search
• Select the terminal node with minimum
depth. Break ties randomly
• Travels the search space level by level
• Best implemented using a queue
Breadth-First Search Example
Find a route from A to G
A

B
C

D E F G
Breadth Example Cont
• Do search tree trace
– States expanded: A, B, C, D, E, F, G
– Solution A, C, G
• Tracing using OPEN (queue) and
CLOSED lists
OPEN CLOSD
A -
B, C // or C,B A
C, D, E A, B
D, E, F, G A, B, C
E, F, G A, B, C, D
F, G A, B, C, D, E
G A, B, C, D, E, F
- A, B, C, D, E, F, G
Repeated states
• Example
A B C

• Allowing repeated states costs memory


and execution time
• Avoiding repeated states requires
checking every resulting state during
expansion
Breadth-First search Algorithm
(without repeated states)
Breadth First search Algorithm
Repeated states are not allowed
1. create a list of nodes called OPEN containing one node representing the
initial state of the problem.
2. create an empty list of nodes called CLOSED.
3. while (OPEN is not empty) do {
3.1. N=first node in OPEN.
3.2 Delete N from OPEN.
3.3 add N to CLOSED.
3.4 if N contains a goal state return N and the solution.
3.5. E= expand(N). // E is the set of states that result from applying the
operators to the state inside N
3.6 for every state i in E do
if (i is not neither in any OPEN node nor in any CLOSED node)
Create a node for i and add it at the end of OPEN;
// i should point to N which is on the CLOSED list
}//while
4. return failure.
Breadth-First Evaluation
• BFS is complete
• BFS is not optimal
• Branching factor: average number of
states resulting from expansion. E.g. 8-
puzzle is approx 3.
• Assume goal is at depth d and branching
factor is b.
Breadth-First Evaluation Cont.
• Time and memory analysis assumes all
nodes at level d are dead ends
• Number of nodes expanded is
1+b+b^2+b^3+…+b^d
• Time complexity is O(b^d)
• Space complexity (OPEN+CLOSED size)
is O(b^d)
Example
• Assume b=10
• CPU can expand 1 million nodes/second
• Node size 1000 bytes
Example Cont.
Depth Nodes Time Memory
0 1 1 ms 100 bytes
2 111 0.1 sec 11 kb
4 11,111 11 sec 1mb
6 10^6 18 min 111 mb
8 10^8 31 hrs 11 gb
10 10^10 128 days 1 tb
12 10^12 35 years 111 tb
14 10^14 3500 years 11,111 tb
Depth-First Search
• Select the terminal node with maximum
depth. Break ties randomly
• Travels the search space down
• Best implemented using a stack
Depth-First Search Example
Find a route from A to G
A

B
C

D E F G
Depth-First Example Cont.
• Do search tree trace
– States expanded: A, B, D, E, C, F, G
– Solution A, C, G
• Tracing using OPEN (stack) and CLOSED
lists
OPEN CLOSD
A -
B, C // or C,B A
D, E, C A, B
E, C A, B, D
C A, B, D, E
F, G A, B, D, E, C
G A, B, D, E, C, F
- A, B, D, E, C, F, G
Depth-First search Algorithm
Depth First search Algorithm
Repeated states are not allowed
1. create a list of nodes called OPEN containing one node representing the
initial state of the problem.
2. create an empty list of nodes called CLOSED.
3. while (OPEN is not empty) do {
3.1. N=first node in OPEN.
3.2 Delete N from OPEN.
3.3 add N to CLOSED.
3.4 if N contains a goal state return N and the solution.
3.5. E= expand(N). // E is the set of states that result from applying
the //operators to the state inside N
3.6 for every state i in E do
if (i is not neither in any OPEN node nor in any CLOSED node)
Create a node for i and add it at the front of OPEN;
// i should point to N which is on the CLOSED list
}//while
4. return failure.
Depth-First Evaluation
• DFS is not complete; can go into an
infinite section of search space missing
goal
• Depth-First is not also complete when
states are repeated:
A G

C B

States expanded: A, B, C, A, B, C, …
Depth-First Evaluation Cont.
DFS is not optimal

5
S
G

1 1

A
Depth-First Evaluation Cont.
• Number of nodes expanded (worst case)
is
1+b+b^2+b^3+…+b^d
• Time complexity is O(b^d)
• Space complexity (OPEN+CLOSED size)
is O(b^d)
• When states are not repeated and path to
the goal is not needed, space complexity
is O(b.d)
Uniform Cost Search
• Search Strategy: select the terminal node
form OPEN that has minimum cost. Break
ties randomly
• Implemented by avoiding repeated states.
Keep the node that has lower cost
Uniform Example

A
1 10

5 B 5
S G

15
5
C
Implementation
• Use two lists OPEN, CLOSED
• OPEN is a minimum heap. Key is cost.
• Previous example
Evaluation
• Uniform is both complete and optimal
provided that the search space has no
loop with a total negative cost.
• Example
2
S 11
A G

-8 3

B
Memory and Time
• Branching factor = b
• Goal at depth = d
• Operators have all equal cost
• Uniform works as breadth and worst case
memory and time complexities are O(b^d)
Informed Search Methods
• Search strategy is based on states
evaluation
• States are evaluated using a heuristic
• A heuristic estimates the cost of reaching
the goal from state
• A heuristic is represented by a function
h:state  number
• h(goal)=0
Example: 8-puzzle Cont
• h1 = # of tiles in the wrong location. h1 is between
0 and 8
• Example
Goal X Y
1 2 3 8 2 3 1 2 3
4 5 6 4 5 6 4 5 6
7 8 7 1 7 8

• Problem: although Y is much better than X, h1


assigns both X and Y an equal value 2
Example: 8-puzzle, cont.
• h2 = sum of Manhattan distances of tiles from correct
location. Manhattan distance is minimum number of
moves between locations
• Example
Goal X Y
1 2 3 4 2 3 1 2 3
4 5 6 1 5 6 4 5 6
7 8 7 8 7 8

• Problem: h2 gives a good evaluation for adjacent tiles


that need to be exchanged. h2(X)=2 and h2(Y)=2
• h3=h1+ w*number of adjacent pairs when exchanged
become in correct locations
Example: Missionaries and
Canibals
• h= # of persons on the left side of the river
• Initial: mmmcccb,- h=6
Goal: -,mmmcccb h=0
cb,mmmcc h=1
Route Finding
• Short-line distance heuristic
• h(s)=short-line distance between s and
goal
• h(s)=0
Best-First search
• Select the terminal node from OPEN that
has minimum h value. Break ties randomly
• Example

A G

S H

C D E

F
Exercize
3-puzzle
initial goal
3 2 1 2
1 3

h = sum of Manhatten Distances


Algorithm
Best First search Algorithm
Repeated states are not allowed
// The algorithm is capable of finding the path to the goal state
Steps:
1. create a list of nodes called OPEN containing one node representing the initial state of the
problem. Compute h value for the node.
2. create an empty list of nodes called CLOSED.
3. while (OPEN is not empty) do {
3.1. N=the node in OPEN having best h value.
3.2. Delete N from OPEN;
3.3 add N to CLOSED.
3.4 if N contains a goal state return N and the solution.
3.5. E=expand(N); // E is the set of states that result from applying the operators to the
state inside N
3.6. for every state i in E do{
compute h(i);
if (i is not neither in any OPEN node nor in any CLOSED node){
Create a node for i and add it to OPEN;
Make i point to N which is on the CLOSED list;
}//if
}//for
}//while
4. return failure.
Evaluation
• Not complete. Can go through an infinite search
space in which all the nodes have a better
heuristic than the node at the other branch
leading to the goal
• The algorithm might not also be complete when
states are repeated

B C
Evaluation Cont
• Not optimal
• In the worst case the algorithm works
similar to breadth first search; h always 1.
Worst case time and memory complexities
is O(b^d).
• Performance largely dependent on
heuristic quality
A* Search
• Search strategy is based on:
f(n)=g(n)+h(n), where
g(n)=the cost of the path from initial state to n
h(n)=estimate of the cost of the path from n
to goal
• f(n)=an estimate of the cost of a solution
through n
Example
• Repeated states are avoided

h=17

10
7 h=7 h=0
7
1 1 D G
B A

h=9 h=5
A* Algorithm
1. Create OPEN containing one node representing initial state
2. Create CLOSED, empty
3. While (OPEN is not empty){
3.1 N=node on OPEN that has best f
3.2 delete N from OPEN
3.3 add N to CLOSED
3.4 if (N contains goal) return N
3.5 E=expand(N)
3.6 for every state (i,op) in E do
if (i is new)
create a new node for i and add it to OPEN
else
if ((g(N) + cost(op)) < g(oldNode)){
remove oldNode;
create a new node for (i,op) and add it to OPEN
}
}//for
}//while
4. Return Failure
Admissability
• A hueristic function h is admissable if it
never over estimates the actual cost
• Shortest-line-distance is admissable
• So is SLD-10, sqrt(SLD)
• But not SLD+10, SLD^2
Evaluation
A* is complete and optimal if
1.H is admissable
2.Search space has no cycle with a total
negative weight
Example
H is not admissable and A* is not optimal
S
1 1

1
h=5
h=3 A h=4
B C
1

h=2 D 1

1 E G
1
h=1 h=0
Evaluation Cont.
• Worst case time and memory is O(b^d):
• H=0
• All operators has equal cost=1
• A* works similar to breadth first search
Hill-climbing
• Based on a heuristic
• Follows a single path when searches for a
solution
• Stops when current state is better than
neighboring states
Algorithm
Current=initial-state;
E=expand(Current);
S=best state in E;
while (S is better than Current){
S=Current;
E=expand(Current);
S=best state in E;
}
Return Current; //not complete
Example

S A B

D E
G
Hill-Climbing problems
• Hill-climbing scenario
• Local maxima problem
• Plateau problem
• Possible solution: run algorithm multiple
times starting from different random initial
states and return the best result found.

You might also like