Professional Documents
Culture Documents
Unit 2
Unit 2
search
Algorithm
Generate-And-Test Algorithm
.
• Generate-and-test search algorithm is a very simple
algorithm that guarantees to find a solution if done
systematically and there exists a solution.
• Algorithm: Generate-And-Test
• 1.Generate a possible solution.
2.Test to see if this is the expected solution.
3.If the solution has been found quit else go to step 1.
• Potential solutions that need to be generated vary
depending on the kinds of problems. For some problems
the possible solutions may be particular points in the
problem space and for some problems, paths from the
start state
1.Generate-and-test, like depth-first search, requires that complete
solutions be generated for testing.
2 .Solutions can also be generated randomly but solution is not
guaranteed.
Systematic Generate-And-Test
• The approach is that the search process proceeds systematically
but some paths that unlikely to lead the solution are not considered.
This evaluation is performed by a heuristic function.
• Depth-first search tree with backtracking can be used to implement
systematic generate-and-test procedure. As per this procedure, if
some intermediate states are likely to appear often in the tree, it
would be better to modify that procedure to traverse a graph rather
than a tree.
HILL CLIMBING
• In simple hill climbing, the first closer
node is chosen,
• Searching for a goal state = Climbing to
the top of a hill.
• Generate-and-test + direction to move.
• Heuristic function to estimate how close a
given state is to a goal state.
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
6
Simple Hill Climbing
• Evaluation function as a way to inject task-
specific knowledge into the control
process.
8
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.
9
Hill Climbing: Disadvantages
Local maximum
A state that is better than all of its
neighbours, but not
better than some other states far away.
10
Hill Climbing: Disadvantages
Plateau
A flat area of the search space in which all
neighbouring
states have the same value.
11
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.
12
Hill Climbing: Disadvantages
Ways Out
13
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.
14
Hill Climbing: Disadvantages
Start Goal
A D
D C
C B
B A
Blocks World
15
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.
16
Hill Climbing: Disadvantages
0 2
A
D D
C C
B B A
17
Hill Climbing: Disadvantages
D 2
B A
A 0
0
D
0
C C D C
B B A B A D
18
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. 19
Hill Climbing: Disadvantages
D -3
B A
A -6
-2
D
-1
C C D C
B B A B A D
20
Hill Climbing: Conclusion
• Can be very inefficient in a large, rough
problem space.
25
Best-First Search
• Depth-first search: not all competing branches having to be
expanded.
26
Best-First Search
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
27
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
29
• An A algorithm is a best-first search
algorithm that aims at minimizing the total
cost along a path from start to goal.
– f(n) = g(n) + h(n)
33
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 34
Applications of BFS
• Best-first search and its more advanced variants have been used in such
applications as games and web crawlers
• . In a web crawler, each web page is treated as a node, and all the
hyperlinks on the page are treated as unvisited successor nodes. A crawler
that uses best-first search generally uses an evaluation function that
assigns priority to links based on how closely the contents of their parent
page resemble the search query (Menczer, Pant, Ruiz, and Srinivasan,
2001).
• In games, best-first search may be used as a path-finding algorithm for
game characters. For example, it could be used by an enemy agent to find
the location of the player in the game world. Some games divide up the
terrain into “tiles” which can either be blocked or unblocked. In such cases,
the search algorithm treats each tile as a node, with the neighbouring
unblocked tiles being successor nodes, and the goal node being the
destination tile.
Best-First Search
• Algorithm A* (Hart et al., 1968):
If we want to get solution some how we can define g=0 choose node
that is closest to goal.
If we want the path involving the fewest no. Of steps than we sat the
cost of going from a node to its successor as a constant usually= 1.
If we find the cheapest path and some operator cost more than other
than we set the cost of going from on node to another to reflect
36
these cost
Best-First Search
• Algorithm A*:
37
h’ understimate
3+1 B C D 5+1
4+1
G
3+2
E
3+3
F 3+4
H’ overstimate
3+1 B C D 5+1
4+1
G
E 2+2
F 1+3
F 0+4
Problem Reduction
Goal: Steal TV set Goal: Earn some money Goal: Buy TV set
AND-OR Graphs
40
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
41
AO* algorithm
• Rather than OPEN and CLOSED list it contains
single structure GRAPH.
• Each node in the GRAPH will point both down its
immediate successors and up to its immediate
predecessors.
• Each node have h’ –estimated value of the cost
of a path from itself to a set of solution node
• G’---We will not store g because its is not
possible that there is a single solution path there
may be many path to the same solution.
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
43
Constraint Satisfaction
• Many AI problems can be viewed as
problems of constraint satisfaction.
Cryptarithmetic puzzle:
SEND
MORE
MONEY
44
Constraint Satisfaction
• As compared with a straightforard search
procedure, viewing a problem as one of
constraint satisfaction can reduce
substantially the amount of search.
45
Constraint Satisfaction
• Operates in a space of constraint sets.
46
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.
47
Initial state:
• No two letters have
the same value. M=1
S = 8 or 9 SEND
• The sum of the digits
must be as shown.
O=0
N=E+1
MORE
C2 = 1 MONEY
N+R>8
E9
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
48
Constraint Satisfaction
Two kinds of rules:
1. Rules that define valid constraint
propagation.
2. Rules that suggest guesses when
necessary.
49