3. Problem Solving Agents
3. Problem Solving Agents
Module 3
Part 1
Solving Problems by Searching
• Agent
• Problem-solving agents
• Agent may need to plan ahead
• Uses Search Algorithms
• To consider a sequence of actions that form a path to a goal state
• Consider Atomic representations
• Planning agents
• Consider Factored or Structured representations instead
• Search Algorithms
• Informed algorithms
• Agent can estimate how far it is from the goal
• Uninformed algorithms
• No such estimate is available.
3
Example : Partial map of Romania
4
Problem-solving agents
• Goal formulation
• A set of states, where the goal is satisfied
• Reaching from initial state -> goal state
• Actions are required
• Example Goal: Initial State(Arad) -> Goal State(Bucharest)
• Problem formulation
• process of deciding what actions and states to consider
• Example:
• Traveling from one city to an adjacent city
• Arad -> Sibiu
5
Problem-solving agents
• Search
• Many ways to achieve the same goal
• Simulate multiple sequences to reach the goal
• Solution
• Sequence of actions that reaches the goal
• Possibility of NO solutions
• Example: Arad -> Sibiu -> Fagaras -> Bucharest
• Execution
• Execute the actions in the solution
• One at a time
6
Problem-solving agents
• Open-loop system
• Agent ignores the percepts
• Breaks the loop between agent and environment
• Closed-loop system
• Agent monitors the percepts
• Chance of the model being incorrect
• Or, the environment is nondeterministic
7
Problem-solving agents
• return action
8
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
9
Problem types
10
Example: vacuum world
• Contingency, start in #5
• Local sensing: dirt, location only.
• Solution??
• [Right, if dirt then Suck]
11
Single-state problem formulation
• State space
• A set of possible states that the environment can be in
• Initial state
• the agent starts in this state.
• For example: Arad
• A set of one or more goal states
• sometimes the goal is defined by a property that applies to many states
• specify an IS-GOAL() method for a problem
• The actions available to the agent ⇒ ACTIONS(s)
• returns a set of actions that can be executed in s
• Given a state s
• example: ACTIONS(Arad) = {ToSibiu, ToTimisoara, ToZerind}
12
Single-state problem formulation
• Path
• Formed by a sequence of actions
• Solution
• path from the initial state to a goal state
• An optimal solution has the lowest path cost among all solutions
13
• State-space graph
Example : Revisited • The vertices are states
• The directed edges are actions
14
Abstraction in Problem Formulation
• (Abstract) state
• set of real states
• (Abstract) action
• complex combination of real actions
• e.g., “Arad → Zerind” represents a complex set of possible routes, detours, rest
stops, etc.
• For guaranteed realizability,
• any real state “in Arad” must get to some real state “in Zerind”
• (Abstract) solution
• set of real paths that are solutions in the real world
• Each abstract action should be “easier”
• than the original problem!
15
Example: vacuum world state space graph
• states: (integer) dirt and robot locations (ignore dirt amounts etc.)
• actions: Left, Right, Suck
• goal states: no dirt
• path cost: 1 per action
16
Example: The 8-puzzle
18
Example: robotic assembly
R R
R R
20
Search algorithms
21
Implementation: states vs. nodes
• EXPAND function
• Creates new nodes (child node or successor node)
• considering available ACTIONS for the present state
• using the RESULT function for destination
• Create the corresponding states
• using the SUCCESSOR function
• Fills in the various fields
• Sets Parent node to the present state
22
Tree search algorithms
• Basic idea:
• offline and simulated exploration of state space
• by generating successors of already-explored states (expanding states)
23
Tree search example
Arad
24
Tree search example
Arad
25
Tree search example
Arad
26
General tree searching
function Tree-Search( problem, frontier) returns a solution, or failure
frontier ← Insert (Make-Node(Initial-Stat e [problem]),frontier)
loop do
if frontier is empty then return failure
node ← Remove-Fro n t (frontier)
if Goal-Test(problem,State(node)) then return node
frontier ← Insert A l l (Expand(node,problem),frontier)
end
function Expand( node, problem) returns a set of nodes
successors ← the empty set
for each action, result in Successor-Fn(problem,State[node]) do
s ← a new Node
Parent-Node[s]← node
Action [s]← action
St at e [s] ← result
Path-Cost[s] ← Path-Cost[node] + Action-Cost(node,action,s)
Depth[s] ← Depth[node] + 1
add s to successors
return successors 27
Search Data Structures and Methods
• node.STATE
• the state to which the node corresponds
• node.PARENT
• the node in the tree that generated this node
• node.ACTION
• the action that was applied to the parent’s state to generate this node
• node.PATH-COST
• the total cost of the path from the initial state to this node.
• In mathematical formulas, we will call this as g(node)
28
Search Data Structures and Methods
30
Search strategies
31
Uninformed Search Strategies
Module 3
Part 3
32
News
Break
Source: https://www.bbc.com/ 33
Uninformed search strategies
• Examples:
1. Breadth-first search
2. Uniform-cost search
3. Depth-first search
4. Depth-limited search
5. Iterative deepening search
34
Breadth-First Search
35
Breadth-First Search
36
Properties of Breadth-First Search
• Complete
• Yes, if b (max branching factor) is finite
• Time
• 1 + b + b2 + … + bd = O(bd)
• exponential in d
• Space
• O(bd)
• Keeps every node in memory
• This is the big problem
• If an agent generates nodes at 10 MB/sec → produce 860 MB in 24 hours
• Optimal
• Yes (if cost is 1 per step)
• not optimal in general
37
Lessons From Breadth First Search
38
Uniform-cost search (AI)
Dijkstra’s algorithm (Theo.CompSc.)
• Evaluation function
• the cost of the path from the root to the current node
• BEST-FIRST-SEARCH
• evaluation function => PATH-COST
• Example:
39
Uniform-cost search / Dijkstra’s algorithm
• Complete
• Yes if the cost is greater than some threshold
• step cost >= ε
• Time
• Complexity cannot be determined easily by d or d
• Let C* be the cost of the optimal solution
• O(bceil(C*/ ε)) or O(b1+floor(C*/ ε))
• Space
• O(bceil(C*/ ε)) or O(b1+floor(C*/ ε))
• Optimal
• Yes, Nodes are expanded in increasing order
40
Depth-First Search (DFS)
41
Depth-First Search:
example binary tree
42
Depth-First Search:
example binary tree
43
Depth-First Search:
example binary tree
44
Depth-First Search
• Complete
• Yes: in finite state spaces
• No: fails in
• infinite-depth state spaces
• spaces with loops
• Modify to avoid repeated spaces along path
• Time
• O(bm)
• Not great if m (max depth) is much larger than d
• But if the solutions are dense, this may be faster than breadth-first search
• Space
• O(bm)
• linear space
• Optimal
• No
45
Depth-Limited Search
46
Depth-Limited Search
Recursive implementation:
function Depth-Limited-Search( problem,limit) returns soln/fail/cutoff
Recursive-DLS(Make-Node(Initial-Stat e [problem]),problem,limit)
47
Depth-Limited Search
• Complete
• Yes if l < d
• Time
• O(bl)
• Space
• O(bl)
• Optimal
• No if l > d
48
Iterative Deepening Search (IDS)
49
Iterative Deepening Search
50
IDS: Example
51
IDS: Example
52
Iterative Deepening Search
• Complete
• Yes
• Time
• (d ) b 1 + (d − 1)b 2 + (d − 2)b 3 + . . . + b d = O(b d )
• Space
• O(bd)
• Optimal
• far-right if step cost = 1
• Can be modified to explore uniform cost tree
53
Iterative Deepening Search: Take-away
54
Bidirectional Search
• Previous Algorithms
• start at an initial state
• can reach any one of multiple possible goal states
• Bidirectional search
• Simultaneously searches
• forward from the initial state, and
• backwards from the goal state(s),
• Hopes that the two searches will meet
• Motivation
• (bd/2 +bd/2) is much less than (bd)
55
Bidirectional Best-First Search
56
Bidirectional Best-First Search
57
Comparison of Uninformed Search Strategies
1. complete if b is finite, and the state space either has a solution or is finite
2. complete if all action costs are ≥ ε > 0
3. cost-optimal if action costs are all identical
4. if both directions are breadth-first or uniform-cost.
58
Repeated States
59
Avoiding repeated states
60
Avoiding repeated states: Graph search
61
Graph search
• Completeness
• for finite state spaces
• Time complexity
• Bounded by the size of the state space
• i.e. the number of vertices and edges, |V | + |E|
• Space complexity
• Bounded by the size of the state space
• i.e. the number of vertices and edges, |V | + |E|
62
Informed (Heuristic) Search
Strategies
Module 3
Part 4
63
Informed (Heuristic) Search
• Informed
• use problem-specific knowledge
• Which search strategies?
• Best-first search and its variants
• Heuristic functions?
• “A rule of thumb, simplification, or educated guess that reduces or limits
the search for solutions in domains that are difficult and poorly
understood.”
• h(n) = estimated cost of the cheapest path from node n to goal
node.
• If n is goal then h(n)=0
64
Greedy Search
65
Example: Romania map with hSLD
66
Example: Arad to Bucharest
Arad
366
67
Example: Arad to Bucharest
Arad
68
Example: Arad to Bucharest
Arad
69
Example: Arad to Bucharest
Arad
Sibiu Bucharest
253 0
70
Properties of greedy search
• Complete?
• No–can get stuck in loops
• e.g., Iasi → Neamt → Iasi → Neamt →
• Complete in finite space with
repeated-state checking
• Time?
• O(bm),
• but a good heuristic can give
dramatic improvement
• Space?
• O(bm)—keeps all nodes in memory
• Optimal?
• No
71
A* search
72
A* search Example
Arad
366=0+366
73
A* search Example
Arad
74
A* search Example
Arad
75
A* search Example
Arad
76
A* search Example
Arad
77
A* search Example
Arad
78
Consistency
• Triangle inequality:
79
Optimality of A ∗ (standard proof )
G G2
• a contradiction,
• so, the assumption must be wrong
81
Optimality of A∗
(Search Contours)
82
Properties of A∗
• Complete?
• Yes, unless there are infinitely many nodes with f ≤ f (G)
• Time?
• Exponential in [relative error in h X length of soln.]
• Space?
• Keeps all nodes in memory
• Optimal?
• Yes—cannot expand f i + 1 until f i is finished
83
Admissibility
Formally:
1. h(n) <= h*(n) where h*(n) is the true cost from n
2. h(n) >= 0 so h(G)=0 for any goal G.
84
Admissible heuristics
• h1(S) = 8
• h2(S) = 3+1+2+2+2+3+3+2 = 18
Dominance
86
Dealing with hard problems
• Relaxed problem
• A problem with fewer restrictions on the actions
• The cost of an optimal solution to a relaxed problem
• is an admissible heuristic for the original problem
88
Relaxed problems (2)
89
Take-away
90
Search in Complex Environments
Module 3
Part 5
91
Local Search and Optimization Problems
92
Local Search and Optimization Problems
93
Local Search and Optimization Problems
94
Local Search and Optimization Problems
95
Example: Travelling Salesperson Problem
96
Example: n-queens
97
Hill-climbing (or gradient ascent/descent)
98
Hill climbing example f(n) = -(number of tiles out of place)
2 8 3 1 2 3
start 1 6 4 h = -4 goal 8 4 h=0
7 5 7 6 5
-5 -5 -2
2 8 3 1 2 3
1 4 h = -3 8 4 h = -1
7 6 5 7 6 5
-3 -4
2 3 2 3
1 8 4 1 8 4 h = -2
7 6 5 7 6 5
h = -3 -4
Hill-climbing search: 8-queens problem
• Plateaus
• the space has a broad flat region
• gives the search algorithm no
direction (random walk)
ridge
• Ridges
• flat like a plateau
• but with drop-offs to the sides
• steps to the North, East, South
and West may go down,
• but a step to the NW may go up.
• Possible remedies
• Random-restart hill climbing: overcomes local maxima—trivially complete
• Random sideways moves: (i) escape from shoulders (ii)loop on flat maxima
102
Simulated Annealing
• Annealing
• thermal process
• obtains low energy states of a solid in a heat bath
• The process contains two steps:
• Increase the temperature of the heat bath to a maximum value
• at which the solid melts
• Decrease carefully the temperature of the heat bath
• Until the particles arrange themselves in the ground state of the solid
• Ground state is a minimum energy state of the solid
103
Simulated Annealing
• Idea: escape local maxima by allowing some “bad” moves
• but gradually decrease their size and frequency
105
Local beam search
• Idea:
• keep k states instead of 1
• choose top k of all their successors
• Not the same as k searches run in parallel!
106
Summary
• Nondeterministic environments
• agents can apply AND–OR search to generate contingency plans that
• reach the goal regardless of which outcomes occur during execution.
107
See you next day!!!
10
8