You are on page 1of 64

Chapter Three

Solving Problems by Searching

1 Prepared by Chala S. (MSc in CSE) 03/21/2023


Problem Solving Agent
 Problem-solving agent: a type of goal-based agent that:
 Decide what to do by finding sequences of actions that lead to the desirable states
 Goals help organize behavior by limiting the objectives that the agent is trying to
achieve and hence the actions it needs to consider.
 Goal formulation: based on current situation and agent’s performance measure
 Problem formulation: deciding what actions and states to consider, given a goal
 The process of looking for such a sequence of actions is called search.
 A search algorithm takes a problem as input and returns a solution in the form of an
action sequence.
2 Prepared by Chala S. (MSc in CSE) 03/21/2023
Problem Space and Search
 A problem space is an abstract space.
A problem space encompasses all valid states that can be generated by the application of
any combination of operators/actions on any combination of objects.
The problem space may contain one or more solutions. A solution is a combination of
operations and objects that achieve the goals.
 A search refers to the search for a solution in a problem space.
Search proceeds with different types of ‘search control strategies’.
The depth-first search and breadth-first search are the two common search strategies.

3 Prepared by Chala S. (MSc in CSE) 03/21/2023


Example : Romania Touring
 Suppose the agent has a nonrefundable ticket to fly out of Bucharest the following
day; currently in Arad.
 Formulate goal (performance evaluation):
be in Bucharest before the flight
 Formulate problem:
states: various cities
actions: drive between cities
 Search:

sequence of cities
4 Prepared by Chala S. (MSc in CSE) 03/21/2023
Road map of Romania Touring

5 Prepared by Chala S. (MSc in CSE) 03/21/2023


Environmental Aspects of Problem Solving agent
 Where does it fit into the agents and environments discussion?
 Static environment: the environment is unchanged while an agent is deliberating.
 Observable: the agent always knows the current state.
 Discrete: at any given state there are only finitely many actions to choose from.
 Deterministic: each action has exactly one outcome.
 Known: the agent knows which states are reached by each action
 Open-loop system: percepts are ignored when choosing an action because it quite
certain of what is going on, thus break the loop between agent and environment.

6 Prepared by Chala S. (MSc in CSE) 03/21/2023


Well-defined problems and solutions
 A problem can be defined formally by five components:
The initial state that the agent starts in.
A description of the possible actions available to the agent at each state.
Transition model: a description of what each action does (Successor).
The goal test, which determines whether a given state is a goal state.
A path cost function that assigns a numeric cost to each path.
 Solution: a path from the initial state to a goal state
 Optimal solution: the path that has the lowest path cost among all solutions;
measured by the path cost function.
7 Prepared by Chala S. (MSc in CSE) 03/21/2023
Problem Formulation: Romania Touring
 Initial
state: In(Arad)
 Actions: if current state is In(Arad), actions = {Go{Sibiu), Go(Timisoara), Go(Zerind)}
 Transition model:
 E.g., Results(In(Arad), Go(Sibiu)) = In(Sibiu)
 Goal test determines whether a given state is a goal state
 explicit, e.g. In(Bucharest)
 implicit, e.g. checkmate: when the opponent’s king is under attack and can’t escape
 Path cost function that assigns a numeric cost to each path
 e.g.,distance traveled
 step cost: c(s, a, s’)
 Solution?Any path from Arad to Bucharest.
 Optimal Solution? A path from Arad to Bucharest with lowest path cost.

8 Prepared by Chala S. (MSc in CSE) 03/21/2023


Problem Abstraction
 The above problem formulation is still a model and not the real thing.
 Real world is complex and has irrelevant details that should be removed from state space and actions,
which is called abstraction.
 In(Arad), in actual cross country trip; the traveling companions, the current radio program, the scenery
out of the window, the condition of the road, the weather, and so on are irrelevant real world
descriptions.
 What’s the appropriate level of abstraction?
 the abstraction is valid, if we can expand it into a solution in the more detailed world
 the abstraction is useful, if carrying out the actions in the solution is easier than the original problem
 remove as much detail as possible while retaining validity and usefulness.

9 Prepared by Chala S. (MSc in CSE) 03/21/2023


Example Problems – Vacuum cleaner

Problem Formulation
States: 2 agent location *2 possible dirt locations
*2 different dirt states= 8 states
Initial state: any state
Actions: Left, Right, and Suck
Transition model: complete state space, see next
slide.
Goal test: check if both squares are clean
Path cost: each step costs 1
10 Prepared by Chala S. (MSc in CSE) 03/21/2023
Complete state space

11 Prepared by Chala S. (MSc in CSE) 03/21/2023


Example Problems – Eight Puzzle
Problem Formulation
States: location of each of the 8 tiles and a blank
Initial state: one specific tile configuration
Actions: move blank tile left, right, up, or down
Transition model: given a state and action, returns the
resulting state
Goal: tiles are numbered from one to eight around the
square
Path cost: cost of 1 per move (solution cost same as
number of most or path length)
12 Prepared by Chala S. (MSc in CSE) 03/21/2023
Example Problems – Eight Puzzle
 The 8-puzzle belongs to the family of sliding-block puzzles-- often used as test
problems for new search algorithms in AI.
 This family is known to be NP-complete, so one does not expect to find methods
significantly better in the worst case than the search algorithms.
 The 8-puzzle has 9!/2 = 181, 440 reachable states
 The 15-puzzle (on a 4×4 board) has around 1.3 trillion states, and
 The 24-puzzle (on a 5 × 5 board) has around 1025 states.

13 Prepared by Chala S. (MSc in CSE) 03/21/2023


Example Problems – Eight Queens
States: locations of 8 queens on chess board
Initial state: one specific queens board
Actions: Add a queen to any empty square
Transition model: Returns the board with a queen added to the
specified square
Goal: 8 queens on the board and no queen can attack another
(cannot be in same row, column, or diagonal)
Path cost: 0 per move

14 Prepared by Chala S. (MSc in CSE) 03/21/2023


Example Problems – Eight Queens
 In eight-queens, 64!/56!= 1.8*1014 possible sequences are investigated. A better
formulation prohibits placing a queen in any square that is already attacked:
 States:

 Arrangements of n queens, one per column in the leftmost n columns, with no queen
attacking another.

 Actions:

 Add a queen to any square in the leftmost empty column such that it is not attacked by any
other queen
 2057 sequences to investigate and solutions are easy to find.
15 Prepared by Chala S. (MSc in CSE) 03/21/2023
Example Problems – Towers of Hanoi
States: combinations of poles and disks
Actions: move disk x from pole y to pole z
subject to constraints
• cannot move disk on top of smaller disk
• cannot move disk if other disks on top
Goal test: disks from largest (at bottom) to
smallest on goal pole
Path cost: 1 per move

16 Prepared by Chala S. (MSc in CSE) 03/21/2023


Example Problems – Rubik’s Cube

Problem Formulation
States: list of colors for each cell on each face
Initial state: one specific cube configuration
Actions: rotate row x or column y on face z
direction
Goal: configuration has only one color on
each face
Path cost: 1 per move

17 Prepared by Chala S. (MSc in CSE) 03/21/2023


Searching for Solutions
 A solution is an action sequence, so search algorithms work by considering various
possible action sequences.
 The possible action sequences starting at the initial state form a search tree with:
The initial state at the root;
The branches are actions applied on current state to create a new state and
the nodes correspond to states having no successor.
 Search algorithms all share this basic structure; they vary primarily according to
how they choose which state to expand next—the so-called search strategy.
Good choice  fewer work  faster
18 Prepared by Chala S. (MSc in CSE) 03/21/2023
Terminologies
 Frontier/Fringe: set of all leaf nodes available for expansion at any given state. The
appropriate data structure for this is a queue that implemented in FIFO.
 Repeated state: in Romanian tour, path from Arad to Sibiu and back to Arad again.
We say that In(Arad) is a repeated state.
 Loopy path: the path form Arad to Sibiu to Arad.
 Redundant path: more than one way to get from one state to another, Arad–Sibiu
(140 km long) and Arad–Zerind–Oradea–Sibiu (297 km long).
 Sometimes, redundant paths are unavoidable
 Sliding-block puzzle and route-finding problems
19 Prepared by Chala S. (MSc in CSE) 03/21/2023
Tree-Search vs. Graph-Search Algorithms
TREE-SEARCH
 Here we only check a node for possibly being
a goal state, after we select the node for
expansion. Redundant Path???
 A “node” is a data structure containing state +
additional info (parent node, etc.)

GRAPH-SEARCH
 Uses “explored” set to avoid visiting already
explored states.
 Uses “frontier” set to store states that remain
to be explored and expanded.
20 Prepared by Chala S. (MSc in CSE) 03/21/2023
Graph-Search
 Redundant paths can cause a tractable problem to become intractable.
 The way to avoid exploring redundant paths is to remember where one has been.
 To do this, we augment the TREE-SEARCH algorithm with a data structure called the
explored set (also known as the closed list), which remembers every expanded node.
 Newly generated nodes that match previously generated nodes—ones in the explored set or
the frontier—can be discarded instead of being added to the frontier.

21 Prepared by Chala S. (MSc in CSE) 03/21/2023


Tree Search Example

22 Prepared by Chala S. (MSc in CSE) 03/21/2023


Tree Search Example

23 Prepared by Chala S. (MSc in CSE) 03/21/2023


Tree Search Example

24 Prepared by Chala S. (MSc in CSE) 03/21/2023


Measuring problem-solving performance
 Strategies are evaluated along the following four dimensions:
Completeness:

 is the strategy guaranteed to find a solution when there is one?


Optimality:

 does the strategy find the highest-quality solution when there are several different solutions?
Time complexity:
 how long does it take to find a solution?
Space complexity:
 how much memory is needed to perform the search?

25 Prepared by Chala S. (MSc in CSE) 03/21/2023


Measuring problem-solving performance
 In AI, complexity is expressed in:
b, branching factor, maximum number of successors/child of any node
d, the depth of the shallowest goal node. (depth of the least-cost solution)
m, the maximum length of any path in the state space
 For effectiveness of a search algorithm, we can consider:
the search cost- depends on the time complexity but can also include a term for
memory usage to find solution.
the total cost, which combines the search cost and the path cost (g) of the
solution found.
26 Prepared by Chala S. (MSc in CSE) 03/21/2023
Searching Strategies
 We may want to search for the first answer that satisfies our goal, or we may want to keep
searching until we find the best answer.
 All search strategies are distinguished by the order in which nodes are expanded.

 Uninformed search
 no information about the number of steps or the path cost from the current state to the goal.
 search the state space blindly
 Informed search, or heuristic search uses some kind of:
 evaluation function to tell us how far each expanded state is from a goal state,
 heuristic function to help us decide which state is likely to be the best one to expand next.
27 Prepared by Chala S. (MSc in CSE) 03/21/2023
Uninformed Search Strategies
 Given a state, we only know whether it is a goal state or not
 Cannot say one non-goal state looks better than another non-goal state
 Traverse state space blindly in hope of somehow hitting a goal state at some point
 General uninformed search strategies:
Breadth-first search g e o f t he
f o r t h e frin
q u e ue used e r a t ed but
Uniform-cost search u e : t y p e of f no d es gen
Key iss n o f t ree lea
( co lle c ti o
re e
search t
Depth-first search
p a n d e d)
ex
Depth-limited search not yet

Iterative deepening search


28 Prepared by Chala S. (MSc in CSE) 03/21/2023
Breadth-first search
 The root node is expanded first (FIFO) i.e new successors go at end
 All the nodes generated by the root node are then expanded
 And then their successors and so on.
 In general, all the nodes are expanded at a given depth in the search tree before any
nodes at the next level are expanded.

29 Prepared by Chala S. (MSc in CSE) 03/21/2023


S

A D

B D A E

C E E B B F
11

D F B F C E A C G
14 17 15 15 13
G C G F
19 19 17 G 25
30 Prepared by Chala S. (MSc in CSE) 03/21/2023
BFS on a Graph

31 Prepared by Chala S. (MSc in CSE) 03/21/2023


Breadth first search Analysis
 Breadth-first search features:
 Complete – find the solution eventually i.e. b is finite.
 Optimal, finds shortest solution if step cost is 1.
 Simple to implement

 The disadvantage:
 if the branching factor of a node is large,
 for even small instances (e.g., chess)
 the space complexity and the time complexity are enormous

32 Prepared by Chala S. (MSc in CSE) 03/21/2023


Analysis(Cont..)
 Time complexity? (measured by Number of nodes generated)
b+b2+b3+… +bd = O(bd)
 Space?
O (bd) (keeps every node in memory)
 Exponential time and space: assuming the branching factor b = 10; 1 million nodes/sec; 1k
bytes/node.
 It takes 13 days for the solution to a problem with
search d = 12, nodes 1012 and 350 years at depth 16.
 Memory is more of a problem than time
 Requires 103GB when d = 8
 Exponential-complexity search problems cannot be
solved by uninformed methods but for smallest instances
33 Prepared by Chala S. (MSc in CSE) 03/21/2023
Uniform cost search
 Breadth-first finds the shallowest goal state
 but not necessarily be the least-cost solution
 work only if all step costs are equal

 Uniform cost search


 modifies breadth-first strategy
 by always expanding the lowest-cost node
 The lowest-cost node is measured by the path cost g(n)

 This is done by storing the frontier as a priority queue ordered by g(n).

34 Prepared by Chala S. (MSc in CSE) 03/21/2023


Uniform cost search

35 Prepared by Chala S. (MSc in CSE) 03/21/2023


Uniform cost Search

36 Prepared by Chala S. (MSc in CSE) 03/21/2023


Uniform cost search
Complete? Yes, if every step cost ≥ ε

 Time? : O(b1+(C*/ ε)) where C* is the cost of the optimal solution


 Space? : O(b1+(C*/ ε))
 Optimal? Yes – nodes expanded in increasing order of g(n)

37 Prepared by Chala S. (MSc in CSE) 03/21/2023


Depth first Search
 Always expands one of the nodes at the deepest level of the tree
 Only when the search hits a dead end
 goes back and expands nodes at shallower levels
 Dead end  leaf nodes but not the goal

 Implementation:

 fringe = LIFO queue, i.e., put successors at front

38 Prepared by Chala S. (MSc in CSE) 03/21/2023


Depth first Search

39 Prepared by Chala S. (MSc in CSE) 03/21/2023


Depth first Search
S

A D

B D A E

C E E B B F
11

D F B F C E A C G
14 17 15 15 13
G C G F
19 19 17 G 25
40 Prepared by Chala S. (MSc in CSE) 03/21/2023
DFS Analysis
 It overcomes
 the time and space complexities
 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
 b: max. branching factor
 Space? d: depth of the shallowest (least-
O(bm), i.e., linear space!
cost) soln.
 Optimal? No, it doesn't guarantee the best solution
m: maximum depth of state space

41 Prepared by Chala S. (MSc in CSE) 03/21/2023


Backtracking search
 Backtracking search is a variant of DFS
 Only one successor is generated at a time rather than all successors
 Each partially expanded node remembers which successor to generate next
 Memory requirement: only O(m) vs. O(bm)

42 Prepared by Chala S. (MSc in CSE) 03/21/2023


Depth- limited search
 Depth-first with depth cutoff k (maximal depth below which nodes are not expanded)

 Three possible outcomes:


 Solution
 Failure (no solution)
 Cutoff (no solution within cutoff)
 It is depth-first search
 with a predefined maximum depth
 However, it is usually not easy to define the suitable maximum depth
 too small  no solution can be found
 too large  the same problems are suffered from
 Anyway the search is
 complete
 but still not optimal

43 Prepared by Chala S. (MSc in CSE) 03/21/2023


Depth- limited search
S depth = 3
A D 3
6
B D A E

C E E B B F
11

D F B F C E A C G
14 17 15 15 13
G C G F
44
19 19 17 G 25 Prepared by Chala S. (MSc in CSE) 03/21/2023
Iterative deepening search
 No choosing of the best depth limit
 It tries all possible depth limits: first 0, then 1, 2, and so on
 combines the benefits of depth-first and breadth-first search

45 Prepared by Chala S. (MSc in CSE) 03/21/2023


Iterative deepening Analysis
 optimal
 complete
 Time and space complexities
 reasonable
 suitable for the problem
 having a large search space
 and the depth of the solution is not known
 Complete? Yes

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

 Space? O(bd)

 Optimal? Yes, if step cost = 1


46 Prepared by Chala S. (MSc in CSE) 03/21/2023
Bi-directional Search
 The idea behind bidirectional search is to run two simultaneous searches: one forward from
the initial state and the other backward from the goal, hoping that the two searches meet in
the middle.
 The motivation is that bd/2 + bd/2 much less than bd, or in the figure ,the area of the two small circles is
less than the area of one big circle centered on the start and reaching to the goal.

Start Goal

47 Prepared by Chala S. (MSc in CSE) 03/21/2023


Comparing Uninformed Search strategies
 This comparison is for tree-search versions.
 For graph searches, the main differences are that DFS is complete for finite state spaces and
that the space and time complexities are bounded by the size of the state space.

48 Prepared by Chala S. (MSc in CSE) 03/21/2023


Avoiding repeated states
 In searching, time is wasted by expanding states that have already been encountered
and expanded before.
 For some problems repeated states are unavoidable.
 The search trees for these problems are infinite.
 Repeated states ,can cause a solvable problem to become unsolvable if the algorithm
does not detect them.
 Repeated states can be the source of great inefficiency: identical sub trees will be
explored many times.

49 Prepared by Chala S. (MSc in CSE) 03/21/2023


Avoiding repeated states
 Three ways to deal with this possibility
 Do not return to the state it just came from
 Refuse generation of any successor same as its parent state
 Do not create paths with cycles
 Refuse generation of any successor same as its ancestor states
 Do not generate any generated state
 Not only its ancestor states, but also all other expanded states have to be checked against

50 Prepared by Chala S. (MSc in CSE) 03/21/2023


Avoiding repeated states Cont..
Strategies for avoiding repeated states
 We can modify the general TREE-SEARCH algorithm to include the data structure
called the closed list ,which stores every expanded node.
 The fringe of unexpanded nodes is called the open list.
 If the current node matches a node on the closed list, it is discarded instead of being
expanded.

51 Prepared by Chala S. (MSc in CSE) 03/21/2023


Informed (heuristic) search strategies
 Informed search uses problem-specific knowledge beyond the definition of the problem itself
 can find solutions more efficiently than an uninformed strategy.

 The general approach we consider is called best-first search: a general TREE-SEARCH or GRAPH-
SEARCH algorithm in which a node is selected for expansion based on an evaluation function, f(n).
 f(n): construed as a cost estimate, so the node with the lowest evaluation is expanded first.
 Identical to that for uniform-cost search, except for the use of f instead of g to order the priority
queue.
 The choice of f determines the search strategy

52 Prepared by Chala S. (MSc in CSE) 03/21/2023


Informed (heuristic) search strategies
 Most best-first algorithms include as a component of f a heuristic function, denoted
h(n). Gets called by the f(n) function.
 h(n) = estimated cost of the cheapest path from the state at node n to a goal state. if n
is a goal node, then h(n) = 0.

53 Prepared by Chala S. (MSc in CSE) 03/21/2023


Greedy best-first search
 Tries to expand the node that is closest to the goal, on the grounds that this is likely to lead to a solution
quickly.
 Thus, it evaluates nodes by using just the heuristic function; that is, f(n) = h(n).

 Example: We use the straight line distance heuristic, which we will call hSLD

 hSLD(In(Arad))= 366

 Let’s examine:
Arad  Sibiu  Fagaras Bucharest

54 Prepared by Chala S. (MSc in CSE) 03/21/2023


Greedy best-first search

hSLD cannot be computed from the problem description


itself. It takes a certain amount of experience to know
that hSLD is correlated with actual road distances and is,
therefore, a useful heuristic.

55 Prepared by Chala S. (MSc in CSE) 03/21/2023


Analysis
 Problem: path through Fagaras is not the optimal.
 Complete? No – can get stuck in loops. (The graph search version is complete in finite spaces, but not
in infinite ones).
 E.g., Iasi  Neamt  Iasi  Neamt 

 Time? O(bm), but a good heuristic can give dramatic improvement.


 Space? O(bm), -- keeps all nodes in memory
 Optimal? No
 Arad  Sibiu  Fagaras Bucharest
Is 32 km longer than
 Arad  Sibiu  Riminicu Vilcea  Pitesti  Bucharest
 56 Greedy because it only looks at the immediate next step.
Prepared by Chala S. (MSc in CSE) 03/21/2023
A* search
 The most widely known form of best-first search.
 It evaluates nodes by combining g(n), the cost to reach the node, and h(n), the cost to get from the
node to the goal: f(n) = g(n) + h(n)
 g(n) gives the path cost from the start node to node n, and
 h(n) is the estimated cost of the cheapest path from node n to the goal node,
 f(n)=estimated cost of the cheapest solution through n .
 Tries the node with the lowest value of f(n)= g(n) + h(n).
 Provided that the heuristic function h(n) satisfies certain conditions, A∗ search is both complete and
optimal.
 Identical to UNIFORM-COST-SEARCH except that A∗ uses g(n) + h(n) instead of g(n).
57 Prepared by Chala S. (MSc in CSE) 03/21/2023
58 Prepared by Chala S. (MSc in CSE) 03/21/2023
Optimality of A*
 The tree-search version of A∗ is optimal if h(n) is admissible, while the graph-search version is
optimal if h(n) is consistent.
 An admissible heuristic is one that never overestimates the cost to reach the goal. Because g(n) is the
actual cost to reach n along the current path, and f(n) = g(n) + h(n), we have as an
immediate consequence that f(n) never overestimates the true cost of a solution along the
current path through n.
 A heuristic h(n) is consistent if, for every node n and every successor n of n’ generated by any action a,
the estimated cost of reaching the goal from n is no greater than the step cost of getting to n ’ plus the
estimated cost of reaching the goal from n ’ :

h(n) ≤ c(n, a, n ’) + h(n ’) .

59 Prepared by Chala S. (MSc in CSE) 03/21/2023


Constraint Satisfaction Problems (CSP)
 A Constraint Satisfaction Problem (CSP) is defined by a set of variables X1,X2,

….Xn, and a set of constraints C1,C2,…,Cm.

 An assignment that does not violate any constraints is called a consistent or legal
assignment.
 A complete assignment is one in which every variable is mentioned, and a solution to
a CSP is a complete assignment that satisfies all the constraints.

60 Prepared by Chala S. (MSc in CSE) 03/21/2023


Example for Constraint Satisfaction Problem:

 The task is coloring each region either red, green, or blue in such a way that the neighboring: regions
have no the same color.

61 Prepared by Chala S. (MSc in CSE) 03/21/2023


CSP Cont..
 To formulate this as CSP, we define the variable to be the regions
WA,NT,Q,NSW,V,SA, and T. The domain of each variable is the set {red,green,blue}.
 The constraints require neighboring regions to have distinct colors; for example, the
allowable combinations for WA and NT are the pairs. {(red,green),(red,blue),
(green,red),(green,blue),(blue,red),(blue,green)}.

62 Prepared by Chala S. (MSc in CSE) 03/21/2023


CSP Cont..
Varieties of constraints :
 unary constraints involve a single variable. Example : SA # green
 Binary constraints involve pairs of variables. Example : SA # WA
 Higher order constraints involve 3 or more variables.

63 Prepared by Chala S. (MSc in CSE) 03/21/2023


End Of Chapter Three

Any Question?

64 Prepared by Chala S. (MSc in CSE) 03/21/2023

You might also like