Sei sulla pagina 1di 42

Problem-Solving

CHAPTER 3
SOLVING PROBLEMS BY SEARCHING

Prepared by

Ali Hussain Aya Mazin Nadia Mahmmod

Supervised by
Prof DR Abbas-ALbakri

SOLVING PROBLEMS BY SEARCHING 1


Introduction
• Simple-reflex agents directly maps states to actions.

• Therefore, they cannot operate well in environments where the mapping is


too large to store or takes too much to learn

• Goal-based agents can succeed by considering future actions and desirability


of their outcomes

• Problem solving agent is a goal-based agent that decides what to do by


finding sequences of actions that lead to desirable states

SOLVING PROBLEMS BY SEARCHING 2


Outline
Problem-solving Agents
Example Problems
Searching for Solutions
Uninformed Search Strategies
Searching With Partial Information
Summary

SOLVING PROBLEMS BY SEARCHING 3


Problem solving agents

 • Intelligent agents are supposed to maximize their performance measure


 • This can be simplified if the agent can adopt a goal and aim at satisfying it
 • Goals help organize behavior by limiting the objectives that the agent is
trying to achieve
 • Goal formulation based on the current situation and the agent’s
performance measure, is the first step in problem solving
 • Goal is a set of states. The agent’s task is to find out which sequence of
actions will get it to a goal state
 • Problem formulation is the process of deciding what sorts of actions and
states to consider, given a goal

SOLVING PROBLEMS BY SEARCHING 4


Problem solving agents

 • An agent with several immediate options of unknown value can decide


what to do by first examining different possible sequences of actions that
lead to states of known value, and then choosing the best sequence
 • Looking for such a sequence is called search
 • A search algorithm takes a problem as input and returns a solution in
the form of action sequence
 • One a solution is found the actions it recommends can be carried out –
execution phase

SOLVING PROBLEMS BY SEARCHING 5


Problem solving agents
 • “formulate, search, execute” design for the agent
 • After formulating a goal and a problem to solve the agent calls
a search procedure to solve it
 • It then uses the solution to guide its actions, doing whatever the
solution recommends as the next thing to do (typically the first
action in the sequence)
 • Then removing that step from the sequence
 • Once the solution has been executed, the agent will formulate a
new goal

SOLVING PROBLEMS BY SEARCHING 6


Well-defined problems and solutions
A problem is defined formally by four components :
1. Initial state :that the agent starts in. For example, the initial state for our agent in
Romania might be described as In(Arad).
2. Actions or successor function :from the state In(Arad), the successor function for the
Romania problem would return.
{ (Go(Sibzu),I n(Sibiu)), (Go(T imisoara), In( Tzmisoara)), (Go(Zerznd),I n(Zerind)))}
3. Goal test :which determines whether a given state is a goal state, For example, in chess, the
goal is to reach a state called "checkmate,“where the opponent's king is under attack and can't
escape.
4. Path cost function : function that assigns a numeric cost to each path. The problem-solving
agent chooses a cost function that reflects its own performance measure.
• A solution is a sequence of actions leading from the initial state to a goal state
Solution quality is measured by the path cost function, and an optimal solution has the lowest
path cost among all solutions.

SOLVING PROBLEMS BY SEARCHING 7


Example Problems :
Toy problems
 Vacuum world
 8-puzzle
 8-queens
Real-world problems
 Route-finding
 Touring
 Robot navigation
 Scheduling

SOLVING PROBLEMS BY SEARCHING 8


Example: The agent is driving to Bucharest from Arad.

SOLVING PROBLEMS BY SEARCHING 9


Example: The agent is driving to Bucharest from Arad.

 On holiday in Romania; currently in Arad


 Non-refundable ticket to fly out of Bucharest tomorrow
 Formulate goal (perf. evaluation):
 be in Bucharest before the flight
 Formulate problem:
 states: various cities
 actions: drive between cities
 Search: sequence of cities

SOLVING PROBLEMS BY SEARCHING 10


Example :Vacuum world state space graph

 states : integer dirt and robot location.


– The agent is in one of two locations, each of which might or might not contain dirt – 8 possible states
Initial state: any state
 actions : Left, Right, Suck
 goal test : no dirt at all locations
 path cost : 1 per action

SOLVING PROBLEMS BY SEARCHING 11


Example: The 8-puzzle

 • states: locations of tiles


 • Initial state: any state
 • actions: move blank left, right, up,
down
 • goal test: goal state (given)
 • path cost: 1 per move

SOLVING PROBLEMS BY SEARCHING 12


Example: The 8-puzzle

SOLVING PROBLEMS BY SEARCHING 13


Example: 8-queens problem
 • States: any arrangement of 0-8
queens on the board is a State
 • Initial state: no queens on the board
 • Actions: add a queen to any empty
square
 • goal test: 8 queens are on the board,
none attacked
 Path cost : 1 per move

SOLVING PROBLEMS BY SEARCHING 14


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


SOLVING PROBLEMS BY SEARCHING 15
Real –world problem
Route-finding problem: it defined in terms of specified locations and
transition along links between them.
It is used in :
 Routing in computer network
 Military operations planning
 Air line travel planning systems
 States :each is represented by a location (e.g an airport) and the current
time.
 Initial state :this is specified by the problem.
Successor function :this returns the states resulting from taking any
scheduled flight
 Goal test :are we at the destination by some prespecified time?
 Path test :this depends on monetary cost , waiting time ,flight time, seat
quality , time of day , type of airplane, and so on.
SOLVING PROBLEMS BY SEARCHING 16
Searching for Solutions

Basic idea :
 The root node : initial state .

 Expanding the current state; that is generating applying each


legal action to the current state.

The general tree-search algorithm they vary primarily according


to how they choose which state to expand next—the so-called
search strategy.

SOLVING PROBLEMS BY SEARCHING 17


Search Tree, Example The agent is driving to Bucharest from Arad.

18
SOLVING PROBLEMS BY SEARCHING
Search Tree, Example The agent is driving to Bucharest from Arad.

SOLVING PROBLEMS BY SEARCHING 19


Search Tree, Example The agent is driving to Bucharest from Arad.

SOLVING PROBLEMS BY SEARCHING 20


Search Tree Data Structures
Queue : data structure to store
frontier of unexpanded nodes:

Nodes : are the data structures


from which the search tree is
constructed

 • Make Queue(element ) creates queue w given elements


 • Empty?(queue) returns true if queue is empty
 • Pop(queue) returns first elem. and removes it
 • Insert(element, queue) inserts elem. in queue, returns q.

SOLVING PROBLEMS BY SEARCHING 21


Search Strategies
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 ∞)

SOLVING PROBLEMS BY SEARCHING 22


Uninformed Search Strategies
 • Uninformed : (blind) search strategies use only the information
available in the problem definition

 • Breadth-first search (BFS)


 • Uniform-cost search
 • Depth-first search (DFS)
 • Depth-limited search
 • Iterative deepening search

SOLVING PROBLEMS BY SEARCHING 23


Breadth-First Search (BFS)
 • The root node is expanded first
 • Then all successors of the root node are expanded
 • Then all their successors
… and so on
 • In general, all the nodes of a given depth are expanded before
any node of the next depth is expanded.
 • Uses a standard queue as data structure

SOLVING PROBLEMS BY SEARCHING 24


Breadth-first search(BFS)

SOLVING PROBLEMS BY SEARCHING 25


Breadth-First Search Properties
 • BFS is complete (always finds goal if one exists)
 • BFS finds the shallowest path to any goal node.
 • If multiple goal nodes exist, BFS finds the shortest Path
 • If tree/graph edges have weights, BFS does not find the shortest
length path.
 • If the shallowest solution is at depth d and the goal test is done
when each node is generated then BFS generates b + b2 + b3 + …
+bd = O(bd) nodes, i.e. has a time complexity of O(bd).
 • If the goal test is done when each node is expanded the time
complexity of BFS is O(bd+1).
 • The space complexity (frontier size) is also O(bd). This is the
biggest drawback of BFS.

SOLVING PROBLEMS BY SEARCHING 26


Uniform-Cost Search (UCS)
 • Modification to BFS generates Uniform-cost search, which
works with any step-cost function ( edge weights/costs):
 • UCS expands the node n with lowest summed path cost g(n).
 • To do this, the frontier is stored as a priority queue. (Sorted list
data structure, better heap data structure).
 • The goal test is applied to a node when selected for expansion
(not when it is generated).
 • Also a test is added if a better node is found to a node on the
frontier.

SOLVING PROBLEMS BY SEARCHING 27


Uniform-Cost Search
 • Uniform cost search is similar to 0 99

Dijkstra’s algorithm! Uniform-


Dijkstra s 80
 • It requires that all step costs are
177
non-negative 310
 • It may get stuck if there is a path 278
with an infinite sequence of zero
cost steps.
 • Otherwise it is complete

SOLVING PROBLEMS BY SEARCHING 28


Depth-First Search (DFS)
 • DFS always expands the deepest node in the current frontier of the search tree.
 • It uses a stack (LIFO queue last in first out)
 • DFS is frequently programmed recursively, then the program call stack is the LIFO queue.
 • DFS is complete, if the graph is finite.
 • The tree search version of DFS is complete on a finite tree, if a test is included whether
the node has already been visited
 • DFS is incomplete on infinite trees or graphs.

SOLVING PROBLEMS BY SEARCHING 29


Depth VS Breath

SOLVING PROBLEMS BY SEARCHING 30


Depth-First Search Properties
 • DFS has time complexity O(b^m), if m is the maximum depth of any node
(may be infinite).
 • DFS has space complexity of O(b m).
 • In many AI problems space complexity is more severe than time
complexity
 • Therefore DFS is used as the basic algorithm in
 • Constraint satisfaction .
 • Propositional satisfiability .
 • Logic programming .
 • Backtracking search, a variant of DFS, uses still less memory, only O(m),
by generating successors not at once, but one at a time.

SOLVING PROBLEMS BY SEARCHING 31


Depth-Limited Search
 • The failure of DFS in infinite search spaces can be prevented by
giving it a search limit l.
 • This approach is called depth limited search
 • Unfortunately, it is not complete if we choose l < d, where d is the
depth of the goal node.
 • This happens easily, because d is unknown.
 • Depth-limited search has time complexity O(b^l ).
 • It has space complexity of O(b l).
 • However, in some applications we know a depth limit (# nodes in
a graph, maximum diameter, …)

SOLVING PROBLEMS BY SEARCHING 32


Iterative Deepening Search (IDS)

 THE ITERATIVE DEEPENING SEARCH ALGORITHM


REPEATEDLY APPLIES DEPTH-LIMITED SEARCH WITH
INCREASING LIMITS.
 IT TERMINATES WHEN A SOLUTION IS FOUND OR IF THE
DEPTH LIMITED SEARCH RETURNS FAILURE, MEANING
THAT NO SOLUTION EXISTS
 THIS SEARCH IS ALSO FREQUENTLY CALLED DEPTH-FIRST
ITERATIVE DEEPENING (DFID).

SOLVING PROBLEMS BY SEARCHING 33


Iterative Deepening Search (IDS)

Solving Problems By Searching 34


Iterative Deepening Search (IDS)

Solving Problems By Searching 35


Iterative Deepening Search Properties
 • REPEATEDLY VISITING THE SAME NODES SEEMS LIKE
A WASTE OF TIME (NOT SPACE). HOW COSTLY IS THIS?

 • THIS HEAVILY DEPENDS ON THE BRANCHING FACTOR


B AND THE SPARSENESS OF THE SEARCH TREE.

 • IDS IS THE PREFERRED UNINFORMED SEARCH


METHOD WHEN THE SEARCH SPACE IS LARGE AND
THE SOLUTION DEPTH IS UNKNOWN.

SOLVING PROBLEMS BY SEARCHING 36


Bidirectional search
 • ADVANTAGE: DELAYS EXPONENTIAL
GROWTH BY REDUCING THE EXPONENT
FOR TIME AND SPACE
COMPLEXITY IN HALF
 • DISADVANTAGE: AT EVERY TIME POINT
THE TWO FRINGES MUST BE COMPARED.
THIS REQUIRES AN EFFICIENT HASHING
DATA STRUCTURE

 • BIDIRECTIONAL SEARCH ALSO


REQUIRES TO SEARCH BACKWARD
(PREDECESSORS OF A STATE).
THIS IS NOT ALWAYS POSSIBLE

SOLVING PROBLEMS BY SEARCHING 37


Comparing Uninformed Search Strategies
CRITERI BREADT UNIFOR DEPTH DEPTH ITERATI BIDIREC
ON H M FIRST LIMITED VE TIO
FIRST COST DEEPEN NAL
COMPLET YESA YESA,B NO NO YESA YESA,D
E

TIME O(BD) O(B1+ O(BM) O(BL) O(BD) O(BD/2)


[C*/Ε])
SPACE O(BD) O(B1+ O(B M) O(B L) O(B D) O(BD/2)
[C*/Ε])
OPTIMAL YESC YES NO NO YESC YESC,D

SOLVING PROBLEMS BY SEARCHING 38


Comparing Uninformed Search Strategies
 • EVALUATION OF TREE-SEARCH STRATEGIES.
 • B IS THE BRANCHING FACTOR
 • D IS THE DEPTH OF THE SHALLOWEST SOLUTION
 • M IS THE MAXIMUM DEPTH OF THE SEARCH TREE L IS
THE DEPTH
LIMIT .

 • A COMPLETE, IF B IS FINITE;
 • B COMPLETE, IF STEP COSTS >= Ε > 0;
 • C OPTIMAL, IF STEP COSTS ARE ALL IDENTICAL;
 • D IF BOTH DIRECTIONS USE BFS
SOLVING PROBLEMS BY SEARCHING 39
Searching With Partial Information :
 WE HAVE COVERED:
1- DETERMINISTIC, FULLY OBSERVABLE :
 SINGLE STATE PROBLEM
 AGENT KNOWS EXACTLY WHICH STATE IT WILL BE IN
 SOLUTION IS A SEQUENCE
2- DETERMINISTIC, NON-OBSERVABLE :
 MULTI-STATE PROBLEM
 ALSO CALLED SENSOR LESS PROBLEMS (CONFORMANT PROBLEMS)
 AGENT MAY HAVE NO IDEA WHERE IT IS
 SOLUTION IS A SEQUENCE
3- NONDETERMINISTIC AND/OR PARTIALLY OBSERVABLE :
 CONTINGENCY PROBLEM
 PERCEPTS PROVIDE NEW INFORMATION ABOUT CURRENT STATE
 OFTEN INTERLEAVE SEARCH, EXECUTION
 SOLUTION IS A TREE OR POLICY
4- UNKNOWN STATE SPACE :
 EXPLORATION PROBLEM (“ONLINE”)
 STATES AND ACTIONS OF THE ENVIRONMENT ARE UNKNOWN

SOLVING PROBLEMS BY SEARCHING 40


Summary
 • SEARCH IS USED IN ENVIRONMENTS WHICH ARE DETERMINISTIC,
OBSERVABLE, STATIC AND COMPLETELY KNOWN.
 • FIRST A GOAL MUST BE IDENTIFIED AND A WELL-DEFINED PROBLEM
MUST BE FORMULATED.
 • A PROBLEM CONSISTS OF FIVE PARTS: THE INITIAL STATE, A SET OF
ACTIONS, A TRANSITION FUNCTION DESCRIBING THE RESULTS OF
ACTIONS, A GOAL TEST FUNCTION AND A PATH COST FUNCTION.
 • THE ENVIRONMENT OF THE PROBLEM IS REPRESENTED BY A STATE
SPACE. A PATH FROM INITIAL STATE TO A GOAL STATE IS A SOLUTION.
 • SEARCH ALGORITHMS TREAT STATES AND ACTIONS AS ATOMIC, THEY
DO NOT CONSIDER THEIR INTERNAL STRUCTURE.
 • THE TREE-SEARCH ALGORITHM CONSIDERS ALL POSSIBLE PATHS TO
FIND A SOLUTION, WHEREAS GRAPH-SEARCH AVOIDS REDUNDANT
PATHS.
 • SEARCH ALGORITHMS ARE JUDGED ON THE BASIS OF COMPLETENESS,
OPTIMALITY, TIME AND SPACE COMPLEXITY. 41
Solving Problems By Searching
Thank You

SOLVING PROBLEMS BY SEARCHING 42

Potrebbero piacerti anche