ppt

advertisement
Informed Search
(no corresponding text chapter)
Recall: Wanted
"
An algorithm and associated data structure(s) that
can:
1) Solve an arbitrary 8-puzzle problem (or problem like
it),
2) Do it in a reasonable amount of time and space-efficiency,
3) Be guaranteed to first find the solution with the
minimum number of actions--optimality.
Available Algorithms
"
"
"
Breadth-first state space search (BFS)
➢
Pro: optimal
➢
Con: memory inefficient
Depth-first state space search (DFS)
➢
Pro: memory efficient
➢
Con: non-optimal
Solution: A* search, a kind of "informed'' search
Informed and Uninformed Search
"
"
"
"
If a search algorithm makes an attempt to
evaluate an item's children by comparing their
"goodness'' with respect to the goal, it is
informed.
If a search algorithm is not informed, it is
uninformed or blind.
BFS and DFS are uninformed
Informed search requires the concept of an
evaluation function
Evaluation Functions
"
"
"
"
"
An evaluation function takes a search space item
as an argument and returns a numerical value as
its "goodness''
One type evaluates the strength of a state (as in
chess playing programs)--the higher the better
Another type estimates the number of actions
required to get to the goal.
Such an evaluation function is also called a
"heuristic''
The smaller an item's heuristic, the better
Heuristic for the 8-puzzle
"
If n is an item in the 8-puzzle state space,
consider:
➢
"
"
h(n) = m, where m is the number of tiles out of place
h estimates, but does not overestimate, the
number of actions required to get to the goal g
Note that h(g) = 0
8-puzzle Heuristic Function:
Number of Misplaced Tiles
283
164
7 5
up
left
283
164
75
283
64
175
283
1 4
765
5
left
up
5
4
3 2 81 34
765
right
up
2 3
184
765
right
3
left
23
184
765
2
down
123
84
765
1
right
123
8 4
765
283
164
75
3
0
283
14
765
4
5
up
283
16
754
6
Recall General Search Algorithm
search(root) returns Node or null {
waiting.add(root)
while (waiting is not empty) {
node = waiting.remove()
state = node.getState()
if (problem.success(state))
return node
else {
children = expand(node)
for each child in children
waiting.add(child)
}
}
return null
}
Suppose we added children to the waiting collection
on the basis of their heuristic values.
Q: What kind of collection should be used?
Priority Queues
A (regular) queue has first-in, first-out (FIFO) behavior, where
items must enter at the rear and leave from the front.
In a priority queue items must leave from the front, but they
enter on the basis of a priority (key) value. The queue is kept
sorted by this value.
Examples of priority queues:
- CPU process queues
- Print queues
- Event-driven simulation (traffic flows)
- VLSI design (channel routing, pin layout)
- Artificial intelligence search algorithms
Implementations of Priority Queues
Priority
Queue
Implemented as:
Array
Linked List
Binary Heap
Array Implementation of Priority
Queues
Suppose items with keys 1, 3, 4, 7, 8, 9, 10, 14, 16
are to be stored in a priority queue.
Array:
A
1
1 3
N
4
7
8
Max
9 10 14 16
Suppose an item with key 2 is added:
A
Max
1
1
3
2
4
7
8
9 10 14 16
Thus inserting takes O(N) time: linear
Linked List Implementation of
Priority Queues
L 1
3
4
7
8
9
10
14
16
14
16
Suppose an item with key 15 is to be added:
L 1
3
4
7
8
9
10
15
Only O(1) (constant) pointer changes required, but it takes
O(N) pointer traversals to find the location for insertion.
Binary Heap Implementation of
Priority Queues
A binary heap is a "nearly full" binary tree:
1
3
8
4
7
10
9
14 15 16
A binary heap has the property that, for every node other
than a leaf, its key value is less than or equal to that of its
children (called the Heap Property).
Adds and removes: O(log2N) time (see java.util.PriorityQueue)
Example State Space With Heuristics
1 4
2 5
6
3 4
7
8
13
19
20
4 2
14
21
9 3
10 2
15
16
17 2
22
23
24
25
26
5 3
11 3
12 2
18 1
27
28 0
Goal State
"Best-First'' State Space Search
●
●
If the general search algorithm is used on a
priority queue where the key value is a heuristic,
it is called a "best-first'' search
"Best-first'' search, despite its name, is not
optimal
Trace of Best-First Search
PQ: (
1
4
PQ: (
4
2
5
3
3
4
2
5
PQ: (
10
2
9
3
11
3
5
3
3
4
2
5
)
PQ: (
17
2
9
3
11
3
5
3
3
4
2
5
)
PQ: (
18
1
9
3
11
3
5
3
3
4
2
5
)
PQ: (
28
0
9
3
11
3
5
3
3
4
2
5
)
)
These links are pointers
back to parent created
at expansion time
)
Notes On Best-First Search
●
Solution path found in the example:
–
●
Shortest possible solution:
–
●
●
●
1 -> 4 -> 10 -> 17 -> 18 -> 28
1 -> 5 -> 12 -> 18 -> 28
Therefore best-first search is not optimal
The problem: the heuristic function rates state 4
better than state 5
Moral: heuristic functions are not perfect
Recall 8-puzzle Heuristic
283
164
7 5
up
left
283
164
75
283
64
175
5
right
283
1 4
765
5
left
up
4
3 2 81 34
765
This state may
not lead to the
shortest solution
up
2 3
184
765
283
164
75
3
right
3
283
14
765
4
5
left
283
16
754
This state leads to the
shortest solution
6
Forcing an Optimal Solution
In Previous Example
●
●
●
●
The problem is that 18 is created as a result of
expanding 17 instead of 12
12 is preferred to 17 because it is shallower in
the state space
We can force 12 to precede 17 in the PQ by
adding a state's depth to its heuristic value
before adding it.
When we order the PQ by key = heuristic +
depth, then the search is called A*.
Taking Heuristics and Depth Into
Account
1 4+0
2 5+1
6
3 4+1
7
8
13
19
20
4 2+1
14
21
5 3+1
9 3+2 10 2+2 11 3+2
12 2+2
15
16
17 2+3
22
23
24
1+4 18 1+3
25
26
27
28
0+4
Goal State
A* Search
PQ: ( 1 )
4
PQ: ( 4
5
3
2
3
4
5
6
5
3
9
11
2
4
5
5
5
6
PQ: ( 5
17
3
9
11
2
4
5
5
5
5
6
17
3
9
11
2
5
5
5
5
6
PQ: ( 18
17
3
9
11
2 )
4
5
5
5
PQ: ( 28
17
3
9
11
2 )
5
5
5
5
6
PQ: ( 10
4
PQ: ( 12
4
4
5
)
)
)
)
6
Notes on A* Search
●
Q: What is the solution path?
– 28
descended from 18
– 18
descended from 12
12 descended from 5
5
–
descended from 1
–
●
●
●
A: 1 → 5 → 12 → 18 → 28 (optimal)
If A* is used, and the heuristic never
overestimates the number of moves required, a
shortest solution is guaranteed (theorem).
The number-of-misplaced-tiles heuristic never
overestimates
BFS As A Special Case of A*
●
Suppose the heuristic function is h(n) = 0
●
Then the priority queue key will be:
➢
●
●
h(n) + depth(n) = 0 + depth(n) = depth(n)
If the children of an item at depth k are added to a
priority queue they will go to the end (since they
are at depth k+1)
Therefore all items at depth k will be examined
before items at depth k+1= the definition of BFS
Some Heuristics Are Better Than
Others
●
Although the number-of-misplaced-tiles
heuristic never overestimates, we could do better
without overestimating
283
164
7 5
4
This state is better because
the 8 is closer to its destination.
up
283
1 4
765
left
3 2 81 34
765
3
up
right
2 3
184
765
3
123
8 4
765
0
283
14
765
4
A more informed heuristic is
the sum of the "Manhattan
distances'' that the tiles are out
of place.
8-puzzle Heuristic Function:
Sum of Manhattan Distances
283
164
7 5
up
left
283
164
75
283
64
175
283
1 4
765
6
left
up
7
5
5 2 81 34
765
right
up
2 3
184
765
right
3
left
23
184
765
2
down
123
84
765
1
right
123
8 4
765
283
164
75
4
0
283
14
765
5
6
up
283
16
754
7
Comparing Heuristic Functions
●
A heuristic h1 is more informed than another
heuristic h2 if:
–
●
●
●
For all items n in the search space, h1(n) >= h2(n)
The more informed the heuristic, the fewer items
will be expanded
If a heuristic is more informed without
overestimating, then it is more efficient as well as
optimal
The sum-of-Manhattan-distances heuristic does
not overestimate
Implementing Heuristics
●
●
●
●
Need to add depth and heuristic to Node class
as instance variables
Abstract computeHeuristic(State) method can
be added to Problem class; overridden in
subclasses
Depth and heuristic can be computed at expand
time (depth of child = 1 + depth of parent)
Use key = depth + heuristic to insert into
priority queue
Download