BCS/BIT: ARTIFICIAL
INTELLIGENCE
Lecture PowerPoints
1
Topics Covered
• Heuristic search
– Nearest neighbor heuristic
– Generate-and-test algorithm
– Hill climbing
– Simulated annealing
– Best First Search
2
Heuristic search
• In order to solve hard problems efficiently,
– it is often necessary to compromise the requirements of mobility
and systematicity
– and construct a control structure that is no longer guaranteed to
find the best answer but that will always find a very good answer
• A heuristic is a technique that improves efficiency of a
search process, possibly sacrificing claims of
completeness
• Heuristics are like tour guides.
– They are good to the extent that they point in generally interesting
directions
– They are bad to the extent that they may miss points of interest to
particular individuals.
3
Heuristic search (2)
• Sometimes heuristics help to guide a search process
without sacrificing any claims to completeness that the
process might previously have had.
• Others may occasionally cause an excellent path to be
overlooked
• On average heuristics improve the quality of the paths
that are explored.
• Using good heuristics, we can hope to get good (though
possibly nonoptimal) solutions to hard problems, such as
the travelling salesman, in less than exponential time
4
Nearest neighbor heuristic
• One example of a good general-purpose heuristic that is
useful for a variety of combinatorial problems is the nearest
neighbor heuristic,
– which works by selecting the locally superior alternative at each
step
• Applying this to the travelling salesman problem, we
produce the following procedure:
1. Arbitrarily select a starting city
2. To select the next city, look at all cities not yet visited, and select
the one closest to the current city. Go to it next.
3. Repeat step 2 until all cities have been visited
• This procedure executes in time proportional to N2, a
significant improvement compared to N! (factorial).
5
Generate-and-test algorithm (1)
• The generate-and-test strategy is the simplest of all
approaches. It consists of the following steps:
1. Generate a possible solution. For some problems, this means
generating a path from start state.
2. Test to see if this is actually a solution by comparing a chosen
point or the endpoint of the chosen path to the set of
acceptable goal states.
3. If a solution has been found, quit. Otherwise return to step 1.
• If generation of possible solutions is done systematically,
– then this procedure will find a solution eventually, if one exist.
– If the problem space is very large, “eventually” may be a very
long time.
6
Generate-and-test algorithm (2)
• The generate-and-test algorithm is a depth-first search
procedure since complete solutions must be generated
before they can be tested.
• In its most systematic form, it is simply an exhaustive
search of the problem space.
• Generate-and-test can also operate by generating
solutions randomly, but then there is no guarantee that a
solution will ever be found.
• The most straightforward way to implement systematic
generate and test is as a depth-first search tree with
backtracking.
• A heuristic is needed to sharpen up the search.
7
Generate-and-test algorithm (3)
• Consider the problem of four 6-sided cubes, and each
side of the cube is painted in one of four colours.
• The four cubes are placed next to one another and the
problem lies in arranging them so that the four available
colours are displayed whichever way the 4 cubes are
viewed.
• The problem can only be solved if there are at least four
sides coloured in each colour and the number of options
tested can be reduced using heuristics if the most
popular colour is hidden by the adjacent cube.
8
Hill climbing (1)
• Hill climbing is a variant of generate-and-test
– in which feedback from the test procedure is used to help the
generator decide which direction to move in search space.
• In a pure generate-and-test procedure, the test function
responds with only a yes or no.
• Here the generate-and-test method is augmented by a
heuristic function which measures the closeness of the
current state to the goal state.
9
Hill climbing (2)
• The simplest way to implement hill climbing is as follows
– Evaluate the initial state. If it is a goal state, then return it and quit.
Otherwise continue with the initial state as the current state
– Loop until a solution is found or until there are no new operators left to be
applied in the current state:
• Select an operator that has not yet been applied to the current state and apply it
to produce a new state.
• Evaluate the new state.
if it is a goal state, then return it and quit
if it is not a goal state but is better than the current state, then make it the current
state.
If it is not better than the current state, then continue in the loop.
• The key difference between this algorithm and the generate-and-
test algorithm
– is the use of evaluation function as a way to inject task-specific knowledge
into the control process.
10
Hill climbing (3)
• It is the use of such knowledge that makes this a
heuristic search method.
• This gives such methods their power to solve some
otherwise intractable problems.
• In the case of the four cubes a suitable heuristic is the
sum of the number of different colours on each of the
four sides,
– and the goal state is 16 - four on each side.
– The set of rules is simply choose a cube and rotate the cube
through 90 degrees.
– The starting arrangement can either be specified or is at
random.
11
Simulated annealing (1)
• This is a variation on hill climbing in which, at the
beginning of the process, some downhill moves may be
made.
– The idea is to do enough exploration of the whole space early on
so that the final solution is relatively insensitive to starting state
– This should lower the chances of getting caught at a local
maximum, a plateau or a ridge
• We use the term objective function in place of a heuristic
function
• We attempt to minimize rather than maximize the value
of the objective function.
– We are valley descending rather than hill climbing
12
Simulated annealing (2)
• Where does the term simulated annealing come from?
– It comes from the metallurgical process of heating metals and
then cool until they reach a minimal energy steady final state.
• The goal of this process is to produce a minimum-energy
final state.
• This process is one of valley descending in which the
objective function is the energy level.
• The probability (p) that a transition to a higher energy
level will occur is given by
p= e-ΔE/(kT)
where ΔE is the positive change in the energy level, T is the
temperature, and k is Boltzmann’s constant
13
Simulated annealing (3)
• In the physical valley descending that occurs during
annealing, the probability of a large uphill move is lower than
the probability of a small one.
• The probability that an uphill move will be made decreases as
the temperature decreases.
• The probability of making a large uphill move is lower than a
small move
– and the probability of making large moves decreases with temperature.
• Downward moves are allowed at any time.
• The properties of physical annealing can be used to define an
analogous process of simulated annealing
– which can be used whenever simple hill climbing can be used.
14
Simulated annealing (4)
• In the analogous process, ΔE is generalized so that it
represents not specifically the change in energy
– but the change in the value of the objective function.
• The analogy for kT is slightly less straightforward.
– The variable k describes the correspondence between units of
temperature and units of energy.
• We can select values for T that produce desirable behavior
on the part of the algorithm and incorporate k into T.
• Simulated annealing is often used to solve problems where
number of moves from a given state is very large
– such as the number of permutations that can be made to a
proposed travelling salesman route.
15
Simulated annealing algorithm
• Evaluate the initial state. If it is a goal state, then return it
and quit. Otherwise, continue with the initial state as the
current state.
• Initialize BEST-SO-FAR to the current state.
• Initialize T according to the annealing schedule
• Loop until a solution is found or until there are no new
operators left to be applied in the current state.
– Select an operator that has not yet been applied to the current
state and apply it to produce a new state.
– Evaluate the new state. Compute
ΔE= (value of current state) – (value of new state)
• If the new state is a goal state, then return it and quit
16
Simulated annealing algorithm (cntd.)
• If it is not a goal state but is better than the current state,
then make it the current state. Also set BEST-SO-FAR to
this new state
• If it is not better than the current state, then make it the
current state with probability
– Revise T as necessary according to the annealing schedule
• Return BEST-SO-FAR as the answer
17
Best First Search (1)
• A combination of depth first and breadth first searches.
• Depth first is good because a solution can be found
without computing all nodes
– and breadth first is good because it does not get trapped on dead
ends.
• The best first search allows us to switch between paths
thus gaining the benefit of both approaches.
• At each step the most promising node is chosen by
applying appropriate heuristic function to each of them
• We then expand the chosen node by using the rules to
generate its successors.
– If one of the nodes is a solution, we can quit
18
Best First Search (2)
– If not, all those new nodes are added to the set of nodes generated
so far. Again the most promising node is selected and the process
continues
– If a solution is not found, that branch will start to look less promising
than one of the top-level branches that has been ignored.
– The now more promising previously ignored branch will be explored.
– The old branch is not forgotten, its last node remains in the set of
generated but unexpanded nodes.
– The search can return to it whenever all the others get bad enough
that it is again the most promising path.
• Figure 11 shows the beginning of a best-first search
procedure.
• Initially, there is only one node, so it will be expanded.
19
Step 1 Step 2 Step 3
A A A
B (3) C (5) D (1) B (3) C (5) D
E (4) F (6)
Step 4 Step 5
A A
B C (5) D B C (5) D
G (6) H (5) E (4) F (6) G (6) H (5) E F (6)
I (2) J (1)
Figure 11: A Best-First Search
Best First Search (3)
• This generates three new nodes.
– The heuristic function in this example is an estimate of the cost
of getting to a solution from a given node, is applied to each of
these new nodes
• Since node D is the most promising, it is expanded next,
producing two successor nodes E and F.
– The heuristic function is applied to them
• Another path, going through node B, looks more
promising, so it is pursued, generating nodes G and H.
• When these two nodes are evaluated, they look less
promising than another path so attention is returned to
the path through D to E.
21
Best First Search (4)
• Node E is then expanded yielding nodes I and J. At the next
step J is expanded, since it is most promising. This process
can continue until a solution is found.
• This procedure is similar to the steepest-ascent hill climbing,
with two exceptions
– In hill climbing, one move is selected and all the others are rejected,
never to be reconsidered
– In best-first search, one move is selected, but the others are kept
around so they can be revisited later if the selected path becomes less
promising.
– Also the best available state is selected in best-first search, even if
that state has a value lower than the value of the state just explored.
– Hill climbing will stop if there are no successor states with better
values than the current state.
22
Best First Search (5)
• The above example shows a best-first search of a tree
• It is sometimes important to search a graph instead so that
duplicate paths will not be pursued.
• The best first search algorithm will involve an OR graph
– (since each of its branches represents an alternative problem-
solving path)
• This avoids the problem of node duplication
– and assumes that each node has a parent link to give the best
node from which it came and a link to all its successors.
• In this way if a better node is found this path can be
propagated down to the successors.
• To use an OR graph requires 2 lists of nodes
23
Best First Search (6)
To implement such a graph-search procedure, we will need to
use two list of nodes:
• OPEN
– Nodes that have been generated and have had the heuristic function
applied to them
– but which have not been examined
• CLOSED
– Nodes that have already been examined.
• To find the most promising nodes a heuristic function is
needed called f'
• f' is an approximation to f and is made of two parts g and h'
where g is cost of going from the initial state to the current
node
24
Best First Search (7)
• Function g is the exact sum of costs of applying each of
the rules that were applied along the best path to the
node.
• Function h' is an estimate of the additional cost of getting
from the current node to a goal state.
• The function f' is the approximate value or estimate of
getting from the initial state to the goal state.
• Both g and h' are positive valued variables.
25
Best First Search Algorithm
1. Start with OPEN containing just the initial state
2. Until a goal is found or there are no nodes left on
OPEN do:
(a) Pick the best node on OPEN
(b) Generate its successors
(c) For each successor do:
(i) If it has not been generated before, evaluate it, add it to OPEN, and
record its parent
(ii) If it has been generated before, change the parent if this new path
is better than the previous one. In that case, update the cost of
getting to this node and to an successors that this node may already
have
26
The A* Algorithm
• Best first search is a simplified A*.
– Start with OPEN holding the initial nodes.
– Pick the BEST node on OPEN such that f = g + h' is minimal.
– If BEST is goal node quit and return the path from initial to
BEST. Otherwise
– Remove BEST from OPEN and all of BEST's children, labelling
each with its path from initial node.
27