Artificial Intelligence: Problem Solving by Search Module - 2
Artificial Intelligence: Problem Solving by Search Module - 2
SimpleProblemSolvingAgent(percept)
state = UpdateState(state, percept)
if sequence is empty then
goal = FormulateGoal(state)
problem = FormulateProblem(state, g)
sequence = Search(problem)
action = First(sequence)
sequence = Rest(sequence)
Return action
Assumptions
• Static or dynamic?
Environment is static
Assumptions
• Static or dynamic?
• Fully or partially observable?
Environment is discrete
Assumptions
• Static or dynamic?
• Fully or partially observable?
• Discrete or continuous?
• Deterministic or stochastic?
Environment is deterministic
Assumptions
• Static or dynamic?
• Fully or partially observable?
• Discrete or continuous?
• Deterministic or stochastic?
• Episodic or sequential?
Environment is sequential
Assumptions
• Static or dynamic?
• Fully or partially observable?
• Discrete or continuous?
• Deterministic or stochastic?
• Episodic or sequential?
• Single agent or multiple agent?
Assumptions
• Static or dynamic?
• Fully or partially observable?
• Discrete or continuous?
• Deterministic or stochastic?
• Episodic or sequential?
• Single agent or multiple agent?
Search Example
Formulate goal: Be in
Bucharest.
Operators:
• fill jug x from faucet
• pour contents of jug x in jug y until y full
• dump contents of jug x down drain
Goal: (2,n)
b=2
Example trees
• Features
Simple to implement
Complete
Finds shortest solution (not necessarily least-cost unless all operators have equal cost)
Analysis
• See what happens with b=10
– expand 10,000 nodes/second
– 1,000 bytes/node
Depth Nodes Time Memory
• Space complexity
Only need to save one set of children at each level
1 + b + b + … + b (m levels total) = O(bm)
For previous example, DFS requires 118kb instead of 10 petabytes for d=12 (10 billion times less)
• Benefits
May not always find solution
Solution is not necessarily shortest or least cost
If many solutions, may find one quickly (quickly moves to depth d)
Simple to implement
Space often bigger constraint, so more usable than BFS for large problems
Comparison of Search Techniques
DFS BFS
Complete N Y
Optimal N N
Heuristic N N
Time bm bd+1
Space bm bd+1
Avoiding Repeated States
Can we do it?
• QueueingFn is SortByCostSoFar
• Cost from root to current node n is g(n)
– Add operator costs along path
• First goal found is least-cost solution
• Space & time can be exponential because large
subtrees with inexpensive steps may be explored
before useful paths with costly steps
• If costs are equal, time and space are O(bd)
– Otherwise, complexity related to cost of optimal
solution
UCS Example
Open list: C
UCS Example
Open list: S(5) N(5) R(6) Z(6) F(6) D(8) G(10) L(10)
UCS Example
Complete N Y Y
Optimal N N Y
Heuristic N N N
Time bm bd+1 bm
Space bm bd+1 bm
Iterative Deepening Search
• DFS with depth bound
• QueuingFn is enqueue at front as with
DFS
– Expand(state) only returns children such that
depth(child) <= threshold
– This prevents search from going down
infinite path
• First threshold is 1
– If do not find solution, increment threshold
and repeat
Examples
Analysis
• What about the repeated work?
• Time complexity (number of generated nodes)
[b] + [b + b2] + .. + [b + b2 + .. + bd]
(d)b + (d-1) b2 + … + (1) bd
O(bd)
Analysis
• Repeated work is approximately 1/b of total
work
Negligible
Example: b=10, d=5
N(BFS) = 1,111,100
N(IDS) = 123,450
• Features
– Shortest solution, not necessarily least cost
– Is there a better way to decide threshold? (IDA*)
Comparison of Search Techniques
DFS BFS UCS IDS
Complete N Y Y Y
Optimal N N Y N
Heuristic N N N N
Time bm bd+1 bm bd
Space bm bd+1 bm bd
Bidirectional Search
• Search forward from
initial state to goal AND
backward from goal state
to initial state
• Can prune many options
• Considerations
– Which goal state(s) to use
– How determine when
searches overlap
– Which search to use for
each direction
– Here, two BFS searches
• Time and space is O(bd/2)
Informed Searches
• Best-first search, Hill climbing, Beam search, A*, IDA*, RBFS, SMA*
• New terms
– Heuristics
– Optimal solution
– Informedness
– Hill climbing problems
– Admissibility
• New parameters
– g(n) = estimated cost from initial state to state n
– h(n) = estimated cost (distance) from state n to closest goal
– h(n) is our heuristic
• Robot path planning, h(n) could be Euclidean distance
• 8 puzzle, h(n) could be #tiles out of place
• Search algorithms which use h(n) to guide search are heuristic
search algorithms
Best-First Search
• QueueingFn is sort-by-h
• Best-first search only as good as heuristic
– Example heuristic for 8 puzzle:
Manhattan Distance
Example
Example
Example
Example
Example
Example
Example
Example
Example
Comparison of Search Techniques
DFS BFS UCS IDS Best
Complete N Y Y Y N
Optimal N N Y N N
Heuristic N N N N Y
Time bm bd+1 bm bd bm
Space bm bd+1 bm bd bm
Generate-and-Test
Algorithm
1. Generate a possible solution.
2. Test to see if this is actually a solution.
3. Quit if a solution has been found.
Otherwise, return to step 1.
76
Generate-and-Test
77
Generate-and-Test
• Exhaustive generate-and-test.
• Heuristic generate-and-test: not consider
paths that seem unlikely to lead to a solution.
• Plan generate-test:
- Create a list of candidates.
- Apply generate-and-test to that list.
78
Generate-and-Test
79
Generate-and-Test
80
Local search and optimization
• Previously: systematic exploration of search space.
– Backtrack search
– Can solve n-queen problems for n = 200
• Advantages:
– Use very little memory
– Can often find reasonable solutions in large or infinite (continuous) state spaces.
global maxima
values
local maxima
states
Comparison of Search Techniques
DFS BFS UCS IDS Best HC
Complete N Y Y Y N N
Optimal N N Y N N N
Heuristic N N N N Y Y
Time bm bd+1 bm bd bm mn
Space bm bd+1 bm bd bm b
Hill Climbing
88
Hill Climbing
89
Simple Hill Climbing
Algorithm
1. Evaluate the initial state.
2. Loop until a solution is found or there are no
new operators left to be applied:
- Select and apply a new operator
- Evaluate the new state:
goal quit
better than current state new current state
90
Simple Hill Climbing
• Evaluation function as a way to inject task-
specific knowledge into the control process.
91
Simple Hill Climbing
92
Steepest-Ascent Hill Climbing (Gradient
Search)
93
Steepest-Ascent Hill Climbing (Gradient
Search)
Algorithm
1. Evaluate the initial state.
2. Loop until a solution is found or a complete
iteration produces no change to current state:
- SUCC = a state such that any possible successor of the
current state will be better than SUCC (the worst state).
- For each operator that applies to the current state, evaluate
the new state:
goal quit
better than SUCC set SUCC to this state
- SUCC is better than the current state set the current
state to SUCC.
94
Hill-climbing search
function HILL-CLIMBING( problem) return a state that is a local maximum
input: problem, a problem
local variables: current, a node.
neighbor, a node.
current MAKE-NODE(INITIAL-STATE[problem])
loop do
neighbor a highest valued successor of current
if VALUE [neighbor] ≤ VALUE[current] then return STATE[current]
current neighbor
• Hill climbing does not look ahead of the immediate neighbors of the current state.
• Can randomly choose among the set of best successors, if multiple have the best value
• Characterized as “trying to find the top of Mount Everest while in a thick fog”
Hill climbing and local maxima
• When local maxima exist, hill climbing is
suboptimal
• Simple (often effective) solution
– Multiple random restarts
Hill-climbing example
• 8-queens problem, complete-state formulation
– All 8 queens on the board in some configuration
• Successor function:
– move a single queen to another square in the same column.
(c1 c2 c3 c4 c5 c6 c7 c8) = (5 6 7 4 5 6 7 6)
• However…
– Takes only 4 steps on average when it succeeds
– And 3 on average when it gets stuck
– (for a state space with ~17 million states)
Possible solution…sideways moves
• If no downhill (uphill) moves, allow sideways moves in hope that
algorithm can escape
– Need to place a limit on the possible number of sideways moves to avoid
infinite loops
• For 8-queens
– Now allow sideways moves with a limit of 100
– Raises percentage of problem instances solved from 14 to 94%
– However….
• 21 steps for every successful solution
• 64 for each failure
Hill-climbing variations
• Stochastic hill-climbing
– Random selection among the uphill moves.
– The selection probability can vary with the steepness of
the uphill move.
• First-choice hill-climbing
– stochastic hill climbing by generating successors randomly
until a better one is found
– Useful when there are a very large number of successors
• Random-restart hill-climbing
– Tries to avoid getting stuck in local maxima.
Hill-climbing with random restarts
• Different variations
– For each restart: run until termination v. run for a fixed time
– Run a fixed number of restarts or run indefinitely
• Analysis
– Say each search has probability p of success
• E.g., for 8-queens, p = 0.14 with no sideways moves
106
Hill Climbing: Disadvantages
Plateau
A flat area of the search space in which all
neighbouring
states have the same value.
107
Hill Climbing: Disadvantages
Ridge
The orientation of the high region, compared to
the set
of available moves, makes it impossible to climb
up.
However, two moves executed serially may
increase
the height.
108
Hill Climbing: Disadvantages
Ways Out
• Backtrack to some earlier node and try going
in a different direction.
• Make a big jump to try to get in a new section.
• Moving in several directions at once.
109
Hill Climbing: Disadvantages
• Hill climbing is a local method:
Decides what to do next by looking only at the
“immediate” consequences of its choices.
• Global information might be encoded in
heuristic functions.
110
Hill Climbing: Disadvantages
Start Goal
A D
D C
C B
B A
Blocks World
111
Hill Climbing: Disadvantages
Start Goal
A D
0 4
D C
C B
B A
Blocks World
Local heuristic:
+1 for each block that is resting on the thing it is supposed to be
resting on.
-1 for each block that is resting on a wrong thing.
112
Hill Climbing: Disadvantages
0 A 2
D D
C C
B B A
113
Hill Climbing: Disadvantages
D 2
C
B A
A 0
0
D
C C D C 0
B B A B A D
114
Hill Climbing: Disadvantages
Start Goal
A D
-6 6
D C
C B
B A
Blocks World
Global heuristic:
For each block that has the correct support structure: +1 to
every block in the support structure.
For each block that has a wrong support structure: -1 to
every block in the support structure.
115
Hill Climbing: Disadvantages
D -3
C
B A
A -6
-2
D
C C D C -1
B B A B A D
116
Hill Climbing: Conclusion
• Can be very inefficient in a large, rough
problem space.
Algorithm
1. Evaluate the initial state.
2. Loop until a solution is found or there are no new operators
left to be applied:
- Set T according to an annealing schedule
- Selects and applies a new operator
- Evaluate the new state:
goal quit
E = Val(current state) - Val(new state)
E < 0 new current state
else new current state with probability e-E/kT.
120
Best-First Search
• Depth-first search: not all competing branches
having to be expanded.
A A A
B C D B C D
3 5 1 3 5
E F
4 6
A A
B C D B C D
5 5
G H E F G H E F
6 5 4 6 6 5 6
I J
2 1
122
Best-First Search
• OPEN: nodes that have been generated, but
have not examined.
This is organized as a priority queue.
Algorithm
1. OPEN = {initial state}.
2. Loop until a goal is found or there are no
nodes left in OPEN:
- Pick the best node in OPEN
- Generate its successors
- For each successor:
new evaluate it, add it to OPEN, record its parent
generated before change parent, update successors
124
Best-First Search
• Greedy search:
h(n) = estimated cost of the cheapest path
from node n to a goal state.
125
Best-First Search
• Uniform-cost search:
g(n) = cost of the cheapest path from the
initial state to node n.
126
Best-First Search
• Greedy search:
h(n) = estimated cost of the cheapest path
from node n to a goal state.
Neither optimal nor complete
127
Best-First Search
• Greedy search:
h(n) = estimated cost of the cheapest path from
node n to a goal state.
Neither optimal nor complete
• Uniform-cost search:
g(n) = cost of the cheapest path from the initial
state to node n.
Optimal and complete, but very inefficient
128
Best-First Search
• Algorithm A* (Hart et al., 1968):
f(n) = g(n) + h(n)
129
Best-First Search
• Algorithm A*:
f*(n) = g*(n) + h*(n)
130
Problem Reduction
Goal: Steal TV set Goal: Earn some money Goal: Buy TV set
AND-OR Graphs
131
Problem Reduction: AO*
A A 6
5 9
B C D
3 4 5
A 9 A 11
9 12
B C D 10 B 6 C D 10
3 4 4
E F G H E F
4 4 5 7 4 4
132
Problem Reduction: AO*
A 11 A 14
B 13 C 10 B 13 C 15
D 5 E 6 F 3 D 5 E 6 F 3
G 5 G 10
133
Constraint Satisfaction
• Many AI problems can be viewed as problems
of constraint satisfaction.
Cryptarithmetic puzzle:
SEND
MORE
MONEY
134
Constraint Satisfaction
• As compared with a straightforard search
procedure, viewing a problem as one of
constraint satisfaction can reduce substantially
the amount of search.
135
Constraint Satisfaction
• Operates in a space of constraint sets.
• Initial state contains the original constraints given in
the problem.
136
Constraint Satisfaction
Two-step process:
1. Constraints are discovered and propagated as
far as possible.
2. If there is still not a solution, then search
begins, adding new constraints.
137
Initial state:
• No two letters have
the same value. M=1
• The sum of the digits S = 8 or 9 SEND
must be as shown. O=0
N=E+1 MORE
C2 = 1
N+R>8
E9
MONEY
E=2
N=3
R = 8 or 9
2 + D = Y or 2 + D = 10 + Y
C1 = 0 C1 = 1
2+D=Y 2 + D = 10 + Y
N + R = 10 + E D=8+Y
R=9 D = 8 or 9
S =8
D=8 D=9
Y=0 Y=1
138
Constraint Satisfaction
Two kinds of rules:
1. Rules that define valid constraint
propagation.
2. Rules that suggest guesses when necessary.
139
Local Beam Search
• QueueingFn is sort-by-h
– Only keep best (lowest-h) n nodes on open list
• n is the “beam width”
– n = 1, Hill climbing
– n = infinity, Best first search
Example
Example
Example
Example
Example
Example
Example
Example
Example
Comparison of Search Techniques
DFS BFS UCS IDS Best HC Beam
Complete N Y Y Y N N N
Optimal N N Y N N N N
Heuristic N N N N Y Y Y
Time bm bd+1 bm bd bm bm nm
Space bm bd+1 bm bd bm b bn
A*
• QueueingFn is sort-by-f
– f(n) = g(n) + h(n)
• Note that UCS and Best-first both improve
search
– UCS keeps solution cost low
– Best-first helps find solution quickly
• A* combines these approaches
Power of f
• If heuristic function is wrong it either
– overestimates (guesses too high)
– underestimates (guesses too low)
• Overestimating is worse than underestimating
• A* returns optimal solution if h(n) is admissible
– heuristic function is admissible if never
overestimates true cost to nearest goal
– if search finds optimal solution using admissible
heuristic, the search is admissible
Overestimating
A (15)
3 3
2
15 6 20 10 5
A* applied to 8 puzzle
A* search applet
Example
Example
Example
Example
Example
Example
Example
Example
Optimality of A*
• Suppose a suboptimal goal G2 is on the open list
• Let n be unexpanded node on smallest-cost path to
optimal goal G1
Complete N Y Y Y N N N Y
Optimal N N Y N N N N Y
Heuristic N N N N Y Y Y Y
Time bm bd+1 bm bd bm bm nm bm
Space bm bd+1 bm bd bm b bn bm
IDA*
• Series of Depth-First Searches
• Like Iterative Deepening Search, except
– Use A* cost threshold instead of depth threshold
– Ensures optimal solution
• QueuingFn enqueues at front if f(child) <= threshold
• Threshold
– h(root) first iteration
– Subsequent iterations
• f(min_child)
• min_child is the cut off child with the minimum f value
– Increase always includes at least one new node
– Makes sure search never looks beyond optimal cost solution
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Analysis
• Some redundant search
– Small amount compared to work done on last
iteration
• Dangerous if continuous-valued h(n) values or if
values very close
– If threshold = 21.1 and value is 21.2, probably only
include 1 new node each iteration
• Time complexity is O(bm)
• Space complexity is O(m)
Comparison of Search Techniques
DFS BFS UCS IDS Best HC Beam A* IDA*
Complete N Y Y Y N N N Y Y
Optimal N N Y N N N N Y Y
Heuristic N N N N Y Y Y Y Y
Time bm bd+1 bm bd bm bm nm bm bm
Space bm bd+1 bm bd bm b bn bm bm
Recursive Best First Search
• Recursive Best First Search
– Linear space variant of A*
• Perform A* search but discard subtrees when
perform recursion
• Keep track of alternative (next best) subtree
• Expand subtree until f value greater than bound
• Update f values before (from parent)
and after (from descendant) recursive call
Algorithm
// Input is current node and f limit
// Returns goal node or failure, updated limit
RBFS(n, limit)
if Goal(n)
return n
children = Expand(n)
if children empty
return failure, infinity
for each c in children
f[c] = max(g(c)+h(c), f[n]) // Update f[c] based on parent
repeat
best = child with smallest f value
if f[best] > limit
return failure, f[best]
alternative = second-lowest f-value among children
newlimit = min(limit, alternative)
result, f[best] = RBFS(best, newlimit) // Update f[best] based on descendant
if result not equal to failure
return result
Example
Example
Example
Example
Example
Example
Analysis
• Optimal if h(n) is admissible
• Space is O(bm)
• Features
– Potentially exponential time in cost of solution
– More efficient than IDA*
– Keeps more information than IDA* but benefits
from storing this information
SMA*
• Simplified Memory-Bounded A* Search
• Perform A* search
• When memory is full
– Discard worst leaf (largest f(n) value)
– Back value of discarded node to parent
• Optimal if solution fits in memory
Example
• Let MaxNodes = 3
• Initially B&G added to open list,
then hit max
• B is larger f value so discard but
save f(B)=15 at parent A
– Add H but f(H)=18. Not a goal and
cannot go deper, so set
f(h)=infinity and save at G.
• Generate next child I with
f(I)=24, bigger child of A. We
have seen all children of G, so
reset f(G)=24.
• Regenerate B and child C. This is
not goal so f(c) reset to infinity
• Generate second child D with
f(D)=24, backing up value to
ancestors
• D is a goal node, so search
terminates.
Heuristic Functions
• Q: Given that we will only use heuristic
functions that do not overestimate, what type
of heuristic functions (among these) perform
best?
• A: Those that produce higher h(n) values.
Reasons
• Higher h value means closer to actual distance
• Any node n on open list with
– f(n) < f*(goal)
– will be selected for expansion by A*
• This means if a lot of nodes have a low
underestimate (lower than actual optimum cost)
– All of them will be expanded
– Results in increased search time and space
Informedness
• If h1 and h2 are both admissible and
• For all x, h1(x) > h2(x), then h1 “dominates” h2
– Can also say h1 is “more informed” than h2
• Example
– h1(x): | xgoal x |
– h2(x): Euclidean distance ( x goal x) 2 ( y goal y ) 2
– h2 dominates h1
Effect on Search Cost
• If h2(n) >= h1(n) for all n (both are admissible)
– then h2 dominates h1 and is better for search
• Typical search costs
– d=14, IDS expands 3,473,941 nodes
• A* with h1 expands 539 nodes
• A* with h2 expands 113 nodes
– d=24, IDS expands ~54,000,000,000 nodes
• A* with h1 expands 39,135 nodes
• A* with h2 expands 1,641 nodes
Which of these heuristics are admissible?
Which are more informed?
• Basic ideas:
– like hill-climbing identify the quality of the local improvements
– instead of picking the best move, pick one randomly
– say the change in objective function is d
– if d is positive, then move to that state
– otherwise:
• move to this state with probability proportional to d
• thus: worse moves (very large negative d) are executed less often
– however, there is always a chance of escaping from local maxima
– over time, make it less likely to accept locally bad moves
– (Can also make the size of the move random as well, i.e., allow
“large” steps in state space)
Physical Interpretation of Simulated Annealing
current MAKE-NODE(INITIAL-STATE[problem])
for t 1 to ∞ do
T schedule[t]
if T = 0 then return current
next a randomly selected successor of current
∆E VALUE[next] - VALUE[current]
if ∆E > 0 then current next
else current next only with probability e∆E /T
More Details on Simulated Annealing
– Lets say there are 3 moves available, with changes in the objective
function of d1 = -0.1, d2 = 0.5, d3 = -5. (Let T = 1).
– pick a move randomly:
• if d2 is picked, move there.
• if d1 or d3 are picked, probability of move = exp(d/T)
• move 1: prob1 = exp(-0.1) = 0.9,
– i.e., 90% of the time we will accept this move
• move 3: prob3 = exp(-5) = 0.05
– i.e., 5% of the time we will accept this move
– T = “temperature” parameter
• high T => probability of “locally bad” move is higher
• low T => probability of “locally bad” move is lower
• typically, T is decreased as the algorithm runs longer
– i.e., there is a “temperature schedule”
Simulated Annealing in Practice
– method proposed in 1983 by IBM researchers for
solving VLSI layout problems (Kirkpatrick et al,
Science, 220:671-680, 1983).
• theoretically will always find the global optimum (the best
solution)
Fitness function: number of non-attacking pairs of queens (min = 0, max = 8 × 7/2 = 28)
•
• 24/(24+23+20+11) = 31%
•
• 4 states for
23/(24+23+20+11) 2 pairs
= 29% etc of 2 states randomly New states Random
• 8-queens selected based on fitness. after crossover mutation
problem Random crossover points applied
selected
Genetic algorithms
• Negative points
– Large number of “tunable” parameters
• Difficult to replicate performance from one problem to another
– Useful on some (small?) set of problems but no convincing evidence that GAs are
better than hill-climbing w/random restarts in general
Simulated Annealing
• Pure hill climbing is not complete, but pure random search is
inefficient.
• Simulated annealing offers a compromise.
• Inspired by annealing process of gradually cooling a liquid until
it changes to a low-energy state.
• Very similar to hill climbing, except include a user-defined
temperature schedule.
• When temperature is “high”, allow some random moves.
• When temperature “cools”, reduce probability of random move.
• If T is decreased slowly enough, guaranteed to reach best state.
Algorithm
function SimulatedAnnealing(problem, schedule) // returns solution state
current = MakeNode(Initial-State(problem))
for t = 1 to infinity
T = schedule[t]
if T = 0
return current
next = randomly-selected child of current
= Value[next] - Value[current]
E
if E > 0 e T
current = nextE // if better than accept state
11111 11000
00000 00111
Mutation
• With small probability, randomly alter 1 bit
• Minor operator
• An insurance policy against lost bits
• Pushes out of local minima
Population: Goal: 0 1 1 1 1 1
E) 0 | 0 1 1 0 0 Score: 4 G) 0 1 1 | 1 0 0 Score: 3
F) 1 | 1 1 0 1 1 Score: 3 H) 0 0 1 | 0 1 0 Score: 6
G) 0 1 1 0 1 | 0 Score: 4 I) 0 0 | 1 0 1 0 Score: 6
H) 1 0 1 1 0 | 1 Score: 2 J) 0 1 | 1 1 0 0 Score: 3