Title: logic
1Dave Reed
- heuristics informed search
- hill-climbing
- bold informed, heuristics, potential dangers,
simulated annealing - best first search
- tentative uninformed, BFS hill-climbing
- Algorithm A
- optimal cost solutions, admissibility, A
2Search strategies so far
- bold uninformed STUPID
- tentative uninformed DFS (and variants), BFS
- bold informed hill-climbing
- essentially, DFS utilizing a "measure of
closeness" to the goal - (a.k.a. a heuristic function)
- from a given state, pick the best successor state
- i.e., the state with highest heuristic value
- stop if no successor is better than the current
state
EXAMPLE travel problem (loc(omaha) ?
loc(los_angeles)) heuristic(State)
-(crow-flies distance from L.A.)
3Hill-climbing example
move(loc(omaha), loc(chicago)). move(loc(omaha),
loc(denver)). move(loc(chicago),
loc(denver)). move(loc(chicago),
loc(los_angeles)). move(loc(chicago),
loc(omaha)). move(loc(denver), loc(los_angeles)).
move(loc(denver), loc(omaha)). move(loc(los_angele
s), loc(chicago)). move(loc(los_angeles),
loc(denver)).
heuristic(loc(omaha)) -1700 heuristic(loc(chicag
o)) -2200 heuristic(loc(denver))
-1400 heuristic(loc(los_angeles)) 0
the heuristic guides the search, always moving
closer to the goal
what if the flight from Denver to L.A. were
cancelled?
48-puzzle example
heuristic(State) of tiles in place, including
the space
- start state has 5 tiles in place
hval 5
of the 3 possible moves, 2 improve the situation
if assume left-to-right preference, move leads to
a dead-end (no successor state improves the
situation) so STOP!
5Intuition behind hill-climbing
- if you think of the state space as a
topographical map (heuristic value elevation),
then hill-climbing selects the steepest gradient
to move up towards the goal
potential dangers plateau successor states have
same values, no way to choose foothill local
maximum, can get stuck on minor peak ridge
foothill where N-step lookahead might help
6Hill-climbing variants
- could generalize hill-climbing to continue even
if the successor states look worse - always choose best successor
- don't stop unless reach the goal or no successors
- dead-ends are still possible (and likely if the
heuristic is not perfect)
- simulated annealing
- allow moves in the wrong direction on a
probabilistic basis - decrease the probability of a backward move as
the search continues - idea early in the search, when far from the
goal, heuristic may not be good - heuristic should improve as you get closer
to the goal - approach is based on a metallurgical technique
7Best first search
- hill-climbing is dependent on a near-perfect
heuristic since bold - tentative informed best first search
- like breadth first search, keep track of all
paths searched - like hill-climbing, use heuristics to guide the
search - always expand the "most promising" path
- i.e., the path ending in a state with highest
heuristic value
8Best first example
hval 5
9Breadth first search vs. best first search
- procedural description of breadth first search
- must keep track of all current paths, initially
Start - look at first path in the list of current paths
- if the path ends in a goal, then DONE
- otherwise,
- remove the first path
- generate all paths by extending to each possible
move - add the newly extended paths to the end of the
list - recurse
- procedural description of best first search
- must keep track of all current paths w/ hvals,
initially HvalStart - look at first path in the list of current paths
- if the path ends in a goal, then DONE
- otherwise,
- remove the first path
- generate all paths by extending to each possible
move - add the newly extended paths to the list, sorted
by (decreasing) hval - recurse
10Best first implementation
best.pro Dave Reed 3/15/02 Best
first search with cycle checking
best(Current, Goal, Path) Path is a list of
states that lead from Current to Goal with
no duplicate states. best_help(ListOfPat
hs, Goal, Path) Path is a list of states
(with associated hval) that lead from one of
the paths in ListOfPaths (a list of lists) to
Goal with no duplicate states.
extend(Path, Goal, ListOfPaths) ListOfPaths is
the list of all possible paths (with
associated hvals) obtainable by extending
Path (at the head) with no duplicate
states.
best(State, Goal, Path)
- best_help(0State, Goal, RevPath),
reverse(RevPath, Path). best_help(_GoalPath
_, Goal, GoalPath). best_help(_PathRestPath
s, Goal, SolnPath) - extend(Path, Goal,
NewPaths), append(RestPaths, NewPaths,
TotalPaths), sort(TotalPaths, SortedPaths),
reverse(SortedPaths,RevPaths),
best_help(RevPaths, Goal, SolnPath). extend(Stat
ePath, Goal, NewPaths) -
bagof(HNextState,StatePath,
(move(State, NextState),
not(member(NextState, StatePath)),
heuristic(NextState,Goal,H)),
NewPaths), !. extend(_, _, ).
differences from BFS associate heuristic value
with each path HPath since extend needs to
know heuristic values for paths, must pass Goal
to extend once the new paths have been appended,
they must be sorted (in decreasing order by
heuristic value)
11Travel example
heuristic(Loc, Goal, Value) Value is
-distance from Goal
heuristic(
loc(omaha), loc(los_angeles),
-1700). heuristic(loc(chicago),
loc(los_angeles), -2200). heuristic(loc(denver),
loc(los_angeles), -1400). heuristic(loc(los_an
geles), loc(los_angeles), 0).
must define the heuristic function for this
problem
?- best(loc(omaha), loc(los_angeles),
Path). Path loc(omaha), loc(denver),
loc(los_angeles) Yes
best first search is able to find a path
128-puzzle example
heuristic(Board, Goal, Value) Value is the
of tiles in place
heuristic(
tiles(Row1,Row2,Row3),tiles(Goal1,Goal2,Goal3),
Value) - same_count(Row1, Goal1, V1),
same_count(Row2, Goal2, V2), same_count(Row3,
Goal3, V3), Value is V1 V2
V3. same_count(, , 0). same_count(H1T1,
H2T2, Count) - same_count(T1, T2,
TCount), (H1 H2, Count is TCount 1 H1
\ H2, Count is TCount).
must define the heuristic function for this
problem
?- best(tiles(1,2,3,8,6,space,7,5,4),
tiles(1,2,3,8,space,4,7,6,5),
Path). Path tiles(1, 2, 3, 8, 6, space,
7, 5, 4), tiles(1, 2, 3, 8, 6, 4, 7, 5,
space), tiles(1, 2, 3, 8, 6, 4, 7, space,
5), tiles(1, 2, 3, 8, space, 4, 7, 6, 5)
Yes ?- best(tiles(2,3,4,1,8,space,7,6,5),
tiles(1,2,3,8,space,4,7,6,5),
Path). Path tiles(2, 3, 4, 1, 8, space,
7, 6, 5), tiles(2, 3, space, 1, 8, 4, 7,
6, 5), tiles(2, space, 3, 1, 8, 4, 7, 6,
5), tiles(space, 2, 3, 1, 8, 4, 7, 6, 5),
tiles(1, 2, 3, space, 8, 4, 7, 6, 5),
tiles(1, 2..., 8, space..., 7, 6...)
Yes
best first search is able to find a path
13Other examples
heuristic function for the Water Jug Problem?
move(jugs(J1,J2), jugs(4,J2)) - J1 4. move(jugs(J1,J2), jugs(J1,3)) - J2 3. move(jugs(J1,J2), jugs(0,J2)) - J1
0. move(jugs(J1,J2), jugs(J1,0)) - J2
0. move(jugs(J1,J2), jugs(4,N)) - J2 0, J1
J2 4, N is J2 - (4 - J1). move(jugs(J1,J2),
jugs(N,3)) - J1 0, J1 J2 3, N is J1 - (3
- J2). move(jugs(J1,J2), jugs(N,0)) - J2 0,
J1 J2 jugs(0,N)) - J1 0, J1 J2 heuristic function for Missionaries Cannibals?
14Comparing best first search
- unlike hill-climbing, best first search can
handle "dips" in the search - ? not so dependent on a perfect heuristic
- depth first search and breadth first search may
be seen as special cases of best first search - DFS heuristic value is distance (number of
moves) from start state - BFS heuristic value is inverse of distance
(number of moves) from start state
- or, procedurally
- DFS assign all states the same heuristic value
- when adding new paths, add equal heuristic
values at the front - BFS assign all states the same heuristic value
- when adding new paths, add equal heuristic
values at the end
15Optimizing costs
- consider a related problem
- instead of finding the shortest path (i.e.,
fewest moves) to a solution, suppose we want to
minimize some cost - EXAMPLE airline travel problem
- could associate costs with each flight, try to
find the cheapest route - could associate distances with each flight, try
to find the shortest route
- we could use a strategy similar to breadth first
search - repeatedly extend the minimal cost path
- search is guided by the cost of the path so far
- but such a strategy ignores heuristic information
- would like to utilize a best first approach, but
not directly applicable - ? search is guided by the remaining cost of the
path
- IDEAL combine the intelligence of both
strategies - cost-so-far component of breadth first search (to
optimize actual cost) - cost-remaining component of best first search (to
make use of heuristics)
16Travel problem revisited
travelcost.pro Dave Reed
3/15/02 This file contains the state
space definition for air travel, with costs
(distances) associated with each flight and
heuristics providing as-the-crow-flies
distance. loc(City) defines the state
where the person is in the specified
City.
move(loc(omaha), loc(chicago),
500). move(loc(omaha), loc(denver),
600). move(loc(chicago), loc(denver),
1000). move(loc(chicago), loc(los_angeles),
2200). move(loc(chicago), loc(omaha),
500). move(loc(denver), loc(los_angeles),
1400). move(loc(denver), loc(omaha),
600). move(loc(los_angeles), loc(chicago),
2200). move(loc(los_angeles), loc(denver),
1400). heuristic(loc(omaha),
loc(los_angeles), 1700). heuristic(loc(chicago),
loc(los_angeles), 2200). heuristic(loc(denver),
loc(los_angeles), 1400). heuristic(loc(los_a
ngeles), loc(los_angeles), 0).
add the actual cost (number of miles per flight)
to each move heuristic is crow-flies distance
(note no longer negative, since want estimate of
remaining cost) want to minimize the total
flight distance from Omaha to L.A.
17Algorithm A
- associate 2 costs with a path
- g actual cost of the path so far
- h heuristic estimate of the remaining cost to
the goal - f g h combined heuristic cost estimate
- Algorithm A (for trees note that usual
formulation is for graphs) - best first search using f as the heuristic
18Algorithm A implementation
Algorithm A (for trees) atree(Current,
Goal, Path) Path is a list of states that
lead from Current to Goal with no duplicate
states. atree_help(ListOfPaths, Goal,
Path) Path is a list of states (with
associated F and G values) that lead from one
of the paths in ListOfPaths (a list of lists)
to Goal with no duplicate states.
extend(GPath, Goal, ListOfPaths) ListOfPaths is
the list of all possible paths (with
associated F and G values) obtainable by
extending Path (at the head) with no duplicate
states.
atree(State,
Goal, GPath) - atree_help(00State,
Goal, GRevPath), reverse(RevPath,
Path). atree_help(_GGoalPath_, Goal,
GGoalPath). atree_help(_GPathRestPaths,
Goal, SolnPath) - extend(GPath, Goal,
NewPaths), append(RestPaths, NewPaths,
TotalPaths), sort(TotalPaths, SortedPaths),
atree_help(SortedPaths, Goal,
SolnPath). extend(GStatePath, Goal,
NewPaths) - bagof(NewFNewGNextState,State
Path, CostH(move(State, NextState,
Cost), not(member(NextState,
StatePath)),
heuristic(NextState,Goal,H),
NewG is GCost, NewF is NewGH),
NewPaths), !. extend(_, _, ).
differences from best associate two values with
each path (F is total estimated cost, G is
actual cost so far) FGPath since extend
needs to know of current path, must pass G new
feature of bagof if a variable appears only in
the 2nd arg, must identify it as backtrackable
19Travel example
can use semi-colon to see alternative paths in
order
?- atree(loc(omaha), loc(los_angeles),
Path). Path 2000loc(omaha), loc(denver),
loc(los_angeles) Path 2700loc(omaha),
loc(chicago), loc(los_angeles) Path
2900loc(omaha), loc(chicago), loc(denver),
loc(los_angeles) No
- note Algorithm A finds the path with least cost
(here, distance) - not necessarily the path with fewest steps
- suppose the flight from Chicago to L.A. was 2500
miles (instead of 2200) - then Omaha?Chicago?Denver?L.A. is shorter than
Omaha?Chicago?L.A.
208-puzzle example
- could apply Alg. A to the 8-puzzle problem
- actual cost of each move (g) is 1
- remaining cost estimate (h) is of tiles out of
place
?- atree(tiles(1,2,3,8,6,space,7,5,4),
tiles(1,2,3,8,space,4,7,6,5),
Path). Path 3tiles(1, 2, 3, 8, 6, space,
7, 5, 4), tiles(1, 2, 3, 8, 6, 4, 7, 5,
space), tiles(1, 2, 3, 8, 6, 4, 7, space,
5), tiles(1, 2, 3, 8, space, 4, 7, 6, 5)
Yes ?- atree(tiles(2,3,4,1,8,space,7,6,5
), tiles(1,2,3,8,space,4,7,6,5),
Path). Path 5tiles(2, 3, 4, 1, 8, space,
7, 6, 5), tiles(2, 3, space, 1, 8, 4, 7,
6, 5), tiles(2, space, 3, 1, 8, 4, 7, 6,
5), tiles(space, 2, 3, 1, 8, 4, 7, 6, 5),
tiles(1, 2..., space, 8..., 7, 6...),
tiles(1..., 8..., 7...) Yes
- here, Algorithm A finds the same paths as best
first search - not surprising since actual cost component (g) is
trivial - still, not guaranteed to be the case (recall
best only cares about the future)
21Algorithm A vs. hill-climbing
- if the cost estimate function h is perfect, then
f is a perfect heuristic - ? Algorithm A is deterministic
if know actual costs for each state, Alg. A
reduces to hill-climbing
22Admissibility
- in general, actual costs are unknown at start
must rely on heuristics - if the heuristic is imperfect, Alg. A is NOT
guaranteed to find an optimal solution
if a control strategy is guaranteed to find an
optimal solution (when a solution exists), we say
it is admissible if cost estimate h is never
overestimates actual cost, then Alg. A is
admissible (when admissible, Alg. A is commonly
referred to as Alg. A)
23Admissible examples
- is our heuristic for the travel problem
admissible? - h (State) crow-flies distance from L.A.
is our heuristic for the 8-puzzle admissible? h
(State) number of tiles out of place, including
the space
is our heuristic for the Missionaries Cannibals
admissible?
24Cost of the search
- the closer h is to the actual cost, the fewer
states considered - however, the cost of computing h tends to go up
as it improves
the best algorithm is one that minimizes the
total cost of the solution
- also, admissibility is not always needed or
desired - Graceful Decay of Admissibility
- If h rarely overestimates the actual cost by more
than D, then Alg. A will rarely find a solution
whose cost is more than D greater than optimal.