Space? O(b m )

‫חיפוש‬
‫בינה מלאכותית‬
‫אבי רוזנפלד‬
‫סוכנים פותרי בעיות‬
‫• ‪ Reflex agents‬לא יכולים לתכנן קדימה‬
‫• כדי לחפש‪ ,‬יש צורך לייצר מודל לחפש בו!‬
‫להגדרת אלגוריתם חיפוש‬
‫•‬
‫•‬
‫•‬
‫•‬
‫•‬
‫סוכן צריך לדחות פעולות שלא מקדמות למטרה‬
‫‪ - Goal formulation‬נוסחת מטרה המבוססת על מדד‬
‫הביצועים הנוכחיים של הסוכן ועל המצב הנוכחי‬
‫מטרה – אוסף של מצבים בעולם שצריכים להתקיים אם‬
‫המטרה הושגה‪.‬‬
‫מטרת הסוכן היא למצוא את רצף הפעולות שיביא אותו‬
‫לאוסף הזה של מצבים‬
‫קודם לכן‪ ,‬עליו להחליט אילו פעולות ואילו מצבים הם‬
‫רלוונטיים‪.‬‬
State Space
1. initial state
2. successor function
Goal Test
3. goal test
‫מפת רומניה‬
Example: 8-Puzzle
(Partial) Search Space for 8-Puzzle
Problem
1. initial state
2. successor function
3. goal test
Example: Route Planning
in a Map
Graph: nodes are cities and links are roads.
• Map gives world dynamics
• Current state is known
• World is fully predictable
• World (set of cities) is finite and enumerable.
Cost: total distance or total time for path.
?‫על מה לומדים היום‬
BFS
DFS
Best-first search
A* search
•
•
•
•
Heuristics –
Local search algorithms •
Hill-climbing search –
Backtracking –
Simulated annealing –
10
‫איך מבצעים את החיפוש?‬
Breadth-First Search
Breadth-first search tree after 0,1,2 and 3 node expansions
CLASSIC FIFO! (Queue!) •
‫• אופטימאלי‬
Depth-First Search
Alternatively can
use a recursive
implementation.
!‫• לא אופטימאלי‬
Breadth-First Search
O
S
Z
A
F
R
P
T
L
M
D
C
B
Breadth-First Search
O
A
S
Z
A
F
R
P
T
L
M
D
C
B
Breadth-First Search
O
A
ZA SA TA
S
Z
A
F
R
P
T
L
M
D
C
B
Breadth-First Search
O
A
ZA SA TA
S
Z
A
F
R
SA TA OAZ
P
T
L
M
D
C
B
Breadth-First Search
O
A
ZA SA TA
S
Z
A
F
R
SA TA OAZ
TA OAZ OAS FAS RAS
P
T
L
M
D
C
B
Breadth-First Search
O
A
ZA SA TA
S
Z
A
F
R
SA TA OAZ
TA OAZ OAS FAS RAS
OAZ OAS FAS RAS LAT
P
T
L
M
D
C
B
Breadth-First Search
O
A
ZA SA TA
S
Z
A
F
R
SA TA OAZ
TA OAZ OAS FAS RAS
OAZ OAS FAS RAS LAT
OAS FAS RAS LAT
P
T
L
M
D
C
B
Breadth-First Search
O
A
ZA SA TA
S
Z
A
F
R
SA TA OAZ
TA OAZ OAS FAS RAS
OAZ OAS FAS RAS LAT
OAS FAS RAS LAT
P
T
L
M
D
C
B
Breadth-First Search
O
A
S
Z
ZA SA TA
A
F
R
SA TA OAZ
P
TA OAZ OAS FAS RAS
OAZ OAS FAS RAS LAT
OAS FAS RAS LAT
RAS LAT BASF
Result =
BASF
T
L
M
D
C
B
Breadth-First Search
O
S
Z
A
F
R
B
P
T
L
M
D
C
Evaluation of Search Strategies
•
•
•
•
Completeness
Time Complexity
Space Complexity
Optimality
To evaluate, we use the following terms
• b = branching factor
• m = maximum depth
• d = goal depth
Evaluation of BFS
• Complete
• Complexity:
– O(bd) time
– O(bd) space
• Optimal (counting by number of arcs).
Depth-First Search
O
S
Z
A
F
R
P
T
L
M
D
C
B
Depth-First Search
O
A
S
Z
A
F
R
P
T
L
M
D
C
B
Depth-First Search
O
A
ZA SA TA
S
Z
A
F
R
P
T
L
M
D
C
B
Depth-First Search
O
A
ZA SA TA
S
Z
A
F
R
P
T
L
M
D
C
B
Depth-First Search
O
A
ZA SA TA
S
Z
A
F
R
OAZ SA TA
P
T
L
M
D
C
B
Depth-First Search
O
A
ZA SA TA
S
Z
A
F
R
OAZ SA TA
SAZO SA TA
P
T
L
M
D
C
B
Depth-First Search
O
A
ZA SA TA
S
Z
A
F
R
OAZ SA TA
SAZO SA TA
FAZOS RAZOS SA TA
P
T
L
M
D
C
B
Depth-First Search
O
A
ZA SA TA
S
Z
A
F
R
OAZ SA TA
SAZO SA TA
FAZOS RAZOS SA TA
BAZOSF RAZOS SA TA
P
T
L
M
D
C
B
Depth-First Search
O
A
S
Z
ZA SA TA
A
F
R
OAZ SA TA
SAZO SA TA
FAZOS RAZOS SA TA
BAZOSF RAZOS SA TA
Result =
BAZOSF
P
T
L
M
D
C
B
Depth-first Search
O
S
Z
A
F
R
B
P
D
T
L
M
C
Evaluation of DFS
• Not complete
• Complexity:
– O(bm) time
– O(mb) space
• Non-optimal
Bi-Directional Search
Romania with step costs in km
38
Greedy best-first search example
39
Greedy best-first search example
40
Greedy best-first search example
41
Greedy best-first search example
42
‫‪Romania with step costs in km‬‬
‫המחיר הכולל‪ .450 :‬האם זה אופטימאלי? לא!‬
‫‪43‬‬
Properties of greedy best-first search
 Complete? No – can get stuck in loops, e.g., Iasi
 Neamt  Iasi  Neamt 
Complete in finite space with checking for
repeated states
 Time? O(bm), but a good heuristic can give
dramatic improvement
 Space? O(bm) – keeps all nodes in memory
 Optimal? No
b is branching factor, m is maximum depth of the search space
44
A* ‫חיפוש‬
‫ קח בחשבון את המחיר הכללי (עד‬:‫הרעיון המרכזי‬
)‫עכשיו‬
Evaluation function f(n) = g(n) + h(n)
g(n) = cost so far to reach n
h(n) = estimated cost from n to goal
f(n) = estimated total cost of path through n to
goal
A* search uses an admissable heuristic:
h(n) <= h*(n), where h*(n) is the true cost from n 
Also require h(n) >= 0, so h(G) = 0 for any goal G 
45






A* search example
46
A* search example
47
A* search example
48
A* search example
49
A* search example
50
A* search example
51
Romania with step costs in km
52
Heuristic functions
 E.g., for the 8-puzzle
 Avg. solution cost is about 22 steps (branching factor +/- 3)
 Exhaustive search to depth 22: 3.1 x 1010 states
 A good heuristic function can reduce the search
53
Heuristic functions
 For the 8-puzzle, two commonly used heuristics
 h1 = the number of misplaced tiles
 h1(s)=8
 h2 = the sum of the distances of the tiles from their goal
positions (manhattan distance)
 h2(s)=3+1+2+2+2+3+3+2=18
54
Inventing admissible heuristics
 Another way to find an admissible heuristic is
through learning from experience:
 Experience = solving lots of 8-puzzles
 An inductive learning algorithm can be used to
predict costs for other states that arise during
search
55
‫בעיית המלכות השחמט‬
‫)‪b‬‬
‫)‪a‬‬
‫• יש ל ‪ 8‬מלכות בשחמט‬
‫• איך אפשר לשים את כולם על הלוח כך שאחת לא יכולה‬
‫לתקוף את השני‬
‫• יש ‪ 92‬פתרונות‬
‫•‬
‫‪http://he.wikipedia.org/wiki/%D7%97%D7%99%D7%93%D7%AA_%D7%A9%D7%9E%‬‬
‫‪D7%95%D7%A0%D7%94_%D7%94%D7%9E%D7%9C%D7%9B%D7%95%D7%AA‬‬
‫‪56‬‬
‫חיפוש לוקאלי‬
‫‪ ‬אל תחפש כל מצב– רק תנסה "תיקון" מקומי‬
‫‪ ‬יתרונות‬
‫‪ ‬אין צורך בהרבה זיכרון‬
‫‪ ‬אפשר למצוא פתרונות במרחבי חיפוש גדולים‬
‫‪57‬‬
Hill-climbing search
58
Hill-climbing example
• 8-queens problem (complete-state
formulation)
• Successor function: move a single queen to
another square in the same column
• Heuristic function h(n): the number of pairs of
queens that are attacking each other (directly
or indirectly, i.e., “through” another queen)
59
Example: n-queens
 Put n queens on an n × n board with no two
queens on the same row, column, or diagonal
 Move a queen to reduce the number of conflicts
60
Hill-climbing example
a)
b)
a) shows a state of h=17 and the h-value for each
possible successor
b) A local minimum in the 8-queens state space
(h=1)
61
Drawbacks
 Ridge = sequence of local maxima difficult for greedy
algorithms to navigate
 Plateau = an area of the state space where the
evaluation function is flat
 Gets stuck 86% of the time in the 8-queens problem
62
:‫פתרון אחד‬
Backtracking
!‫ תחזור חזרה למלכה האחרונה – ותזיז אותה‬
‫ קוד שלי באתר‬
http://www.hbmeyer.de/backtrack/achtdamen/eight.h 
tm
63
Hill Climbing Variations
 Random-restart hill-climbing
 Tries to avoid getting stuck in local maxima
 Conducts a series of hill-climbing searches from randomly
generated initial states
 Stops each search at a local maxima – or better yet, stops
after a fixed amount of time
 For the 3,000,000-queens problem, solutions
found in under a minute
 The success (or failure) of hill-climbing can
very much depend on the shape of the statespace landscape
64
Simulated annealing
 Escape local maxima by allowing “bad” moves
 Idea: but gradually decrease their size and frequency
 Origin; metallurgical annealing
 Bouncing ball analogy:
 Shaking hard (= high temperature)
 Shaking less (= lower the temperature)
 If T decreases slowly enough, best state is reached
 Applied for VLSI layout, airline scheduling, etc.
65
Simulated annealing search
 Idea (again): escape local maxima by allowing some “bad”
moves but gradually decrease their size and frequency

66