You are on page 1of 19

Jimma University

Jimma Institute of Technology


Department of Computing

Comp551 – Introduction to Artificial Intelligence

Handout 4 – Informed Search

1. Introduction

In the last chapter we examined a number of “uninformed” tree search algorithms that can be
used for solving single-state problems. Although they can be useful for solving some relatively
simple problems, because of their exponential time complexity they quickly come up against the
barrier of computational complexity when faced with more complex problems. In this chapter we
examine ways of employing extra “domain knowledge” to reduce the time taken to solve a
problem.

To begin with, we will describe two simple tree search algorithms that enable us to incorporate
domain knowledge, and we will demonstrate that they can achieve significant improvements in
search time. Both of these algorithms use a “heuristic” function to estimate which is the best
node to choose for expansion. We will examine what types of heuristic function we can use, and
how we can go about inventing new heuristic functions. Finally, we will look at a class of “local
search” algorithms that can be used for problems in which we don’t care about the path to a goal;
we just want to know the goal state itself.

2. Informed Search Strategies

Informed search algorithms attempt to use extra domain knowledge to inform the search, in an
attempt to reduce search time. A particular class of informed search algorithms is known as best-
first search. Note that best-first search is not an algorithm itself, but a general approach. In
normal (i.e. uninformed) tree search, the strategy is determined by the order in which we select
the next node for expansion from the fringe. In best-first search, we use a heuristic function to
estimate which of the nodes in the fringe is the “best” node for expansion. This heuristic
function, h(n), estimates the cost of the cheapest path from node n to the goal state. In other
words, it tells us which of the nodes in the fringe it think is “closest” to the goal.

We will now examine two similar, but not identical, best-first search algorithms: greedy best-
first and A* search.

2.1 Greedy Best-First Search

The simplest best-first search algorithm is greedy best-first search. This algorithm simply
expands the node that is estimated to be closest to the goal, i.e. the one with the lowest value of
the heuristic function h(n).

1
For example, let us return to the Romania example we introduced in the previous chapter (the
state space is reproduced in Figure 1 for ease of reference). What information can we use to
estimate the actual road distance from a city to Bucharest? In other words, what domain
knowledge can we use to estimate which of the unexpanded nodes is closest to Bucharest? One
possible answer is to use the straight-line distance from each city to Bucharest. Table 1 shows a
list of all these distances.

Figure 1 – The state space of the Romania problem

City Straight-line distance City Straight-line distance


Arad 366 Mehadia 241
Bucharest 0 Neamt 234
Craiova 160 Oradea 380
Drobeta 242 Pitesti 100
Eforie 161 Rimnicu Vilcea 193
Fagaras 176 Sibiu 253
Giurgiu 77 Timisoara 329
Hirsova 151 Urziceni 80
Iasi 226 Vaslui 199
Lugoj 244 Zerind 374

Table 1 – Values of hSLD(n) – the straight-line distance to Bucharest

2
Using this information, the greedy best-first search algorithm will select for expansion the node
from the unexpanded fringe list with the lowest value of hSLD(n). Let us step through the greedy
best-first algorithm when applied to the problem of finding a path from Arad to Bucharest:
 Step1:
o Fringe=[Arad]
o Lowest value of heuristic function hSLD(Arad)=366
o Action: expand Arad
 Step 2:
o Fringe=[Sibiu,Timisoara,Zerind]
o Lowest value of heuristic function hSLD(Sibiu)=253
o Action: expand Sibiu
 Step 3:
o Fringe=[Timisoara,Zerind,Arad,Fagaras,Oradea,Rimnicu Vilcea]
o Lowest value of heuristic function hSLD(Fagaras)=176
o Action: expand Fagaras
 Step 4:
o Fringe=[Timisoara,Zerind,Arad,Oradea,Rimnicu Vilcea,Sibiu,Bucharest]
o Lowest value of heuristic function hSLD(Bucharest)=0
o Action: find goal at Bucharest

We can see that in this example the algorithm has found a solution in 4 steps only, much quicker
than any of the uninformed algorithms would have reached the goal. But how well does the
algorithm perform with regard to the evaluation criteria of completeness, optimality, and time
and space complexity?

Unfortunately greedy best-first search is not complete. Because it looks only at the heuristic
function value there are cases in which the algorithm can get stuck in loops. For example,
consider the problem of getting from Iasi to Fagaras: clearly the first move would be to go to
Neamt, as its straight-line distance to Fagaras would be closer than that of the other alternative,
Vaslui. But once at Neamt the only option is to return to Iasi. Once back at Iasi the algorithm
would return to Neamt for the same reason, and so on for ever. The problem here is that the
algorithm has no “memory” – it doesn’t remember that it has already tried to get to Fagaras via
Neamt.

In addition, greedy best-first search is not optimal. In fact, the solution for the Arad to Bucharest
problem is not the shortest route – it is quicker to get from Arad to Bucharest using the route
through Rimnicu Vilcea and Pitesti. The algorithm decided to try the Fagaras route because it
looked best in the short term (i.e. it was closer to Bucharest than Rimnicu Vilcea).

The time complexity is exponential, although in terms of the number of nodes expanded the
algorithm is a big improvement on uninformed techniques, as we will see later. Exactly how
much of an improvement depends on the heuristic function used. All nodes must be kept in
memory so space complexity is also exponential.

3
Summary of Greedy Best-First Search Analysis:
 Completeness: no, can get stuck in loops
 Optimality: no, can go for non-optimal solutions that look good in the short term
 Time complexity: O(bm), but good heuristic can make dramatic improvement
 Space complexity: same as time complexity

2.2 A* Search

A* (pronounced “A star”) search is similar to greedy best-first search, except that it also takes
into account the actual path cost taken so far to reach each node. The node with the lowest
estimated total path cost, f(n), is expanded,
f(n) = g(n) + h(n)
where g(n) = total actual path cost to get to node n
h(n) = estimated path cost to get from node n to goal

For instance, in the Romania example, first we expand Arad to produce the three successors
Sibiu, Timisoara and Zerind. The value of f(Sibiu) is equal to 140 (the actual path cost to reach
Sibiu from Arad) plus 253 (the estimated remaining path cost to Bucharest), making 393.
Similarly, f(Timisoara) is equal to 118 (actual path cost so far) plus 329 (estimated remaining
path cost), making 447. Finally, the value of f(Zerind) is 75 + 374 = 449. At each iteration, A*
search selects the node with the lowest total estimated cost. At this iteration, the lowest cost is
393 for Sibiu, so this node will be expanded. A summary of the execution of A* search is given
below:
 Step1:
o Fringe=[Arad]
o Lowest value of evaluation function f(Arad)=0+366=366
o Action: expand Arad
 Step 2:
o Fringe=[Sibiu,Timisoara,Zerind]
o Lowest value of evaluation function f(Sibiu)=140+253=393
o Action: expand Sibiu
 Step 3:
o Fringe=[Timisoara,Zerind,Arad,Fagaras,Oradea,Rimnicu Vilcea]
o Lowest value of evaluation function f(Rimnicu Vilcea)=220+193=413
o Action: expand Rimnicu Vilcea
 Step 4:
o Fringe=[Timisoara,Zerind,Arad,Fagaras,Oradea,Craiova,Pitesti,Sibiu]
o Lowest value of evaluation function f(Fagaras)=239+176=415
o Action: expand Fagaras
 Step 5:
o Fringe=[Timisoara,Zerind,Arad,Oradea,Craiova,Pitesti,Sibiu,Sibiu,Bucharest]
o Lowest value of evaluation function f(Pitesti)=317+100=417
o Action: expand Pitesti

4
 Step 6:
o Fringe=[Timisoara,Zerind,Arad,Oradea,Craiova,Sibiu,Sibiu,Bucharest,
Bucharest,Craiova,Rimnicu Vilcea]
o Lowest value of evaluation function f(Bucharest)=418+0=418
o Action: find goal at Bucharest

Notice that A* search finds a different (and optimal) solution to greedy best-first search, getting
to Bucharest via Sibiu, Rimnicu Vilcea and Pitesti, rather than via Sibiu and Fagaras. The final
search tree for A* search is shown in Figure 2.

Figure 2 – Final search tree for the problem of getting from Arad to Bucharest

In addition to finding the optimal solution in this case, A* search is also complete. In the Iasi to
Fagaras example, in which greedy best-first search got stuck in a loop, with A* search the actual
cost of the “looping” path will build up with each loop, and so it will eventually become less
desirable than the correct path through Vaslui.

Regarding time and space complexity, A* search is still exponential, but as with greedy best-first
search, a good heuristic function can give dramatic improvements in search time. Because it is
both complete and optimal, A* search is the most common informed search algorithm.

Summary of A* Search Analysis:


 Completeness: yes
 Optimality: yes
 Time complexity: O(bm), but good heuristic can make dramatic improvement
 Space complexity: same as time complexity

2.3 Iterative Deepening A*

5
Although A* is the most popular informed search algorithm, it would be nice if we could
improve on the exponential space complexity. In the last chapter we saw how iterative deepening
search allowed us to obtain linear space complexity for a complete and optimal uninformed
search algorithm. A similar approach can be used here, resulting in iterative deepening A*
search. However, instead of using a depth cut-off, this time we use an f cut-off: we do not store
nodes that have a higher value for the evaluation function f than some cut-off value. At each
iteration, the new f cut-off will be the smallest f value of any node that exceeded the cut-off in
the previous iteration. For example, considering the example shown in Figure 2, at the second-
last iteration, the smallest f value exceeding the cut-off would be Bucharest at 418. Therefore on
the final iteration all nodes with an f value greater than 418 would not be stored in memory.

Iterative deepening A* search is an example of a class of algorithms called memory-bounded


heuristic search. These algorithms do reduce the amount of memory required to execute the
search at each iteration, but because only one extra node is added to the search with each
iteration, the increase in search time can be large.

3. Heuristics

In the Romania example we used the heuristic function of straight-line distance to inform the
search for a solution. How did we know that this heuristic would produce good results, or even
result in a complete and optimal algorithm? In this section we try to answer this question by
introducing the concept of an admissible heuristic.

3.1 Admissible Heuristics

The aim of heuristic functions is to estimate the cost of reaching the goal from a given state. Put
simply, a heuristic function h(n) is an admissible heuristic if it never overestimates the true cost
to reach the goal. In other words, the estimate can be less than or equal to the actual best cost, but
it should never be more than it.

Definition: A heuristic function h(n) is admissible if for every node n, h(n)≤h*(n), where h*(n) is
the true cost to reach the goal state from n.

We can see intuitively that the straight-line distance in the Romania example is an admissible
heuristic. The actual distance in this case is the shortest road route from a city to Bucharest.
Clearly, the shortest road distance can never be less than the straight-line distance; therefore the
straight-line distance must be an admissible heuristic.

Why is the concept of an admissible heuristic important? One answer is that the A* search
algorithm is only optimal if the heuristic used is admissible. Another answer will be given in the
next section.

3.2 Dominance

If we have more than one admissible heuristic, how can we decide which one is best? Actually
there is a simple answer to this question, and it is based on the concept of dominance. A heuristic

6
h2 dominates another heuristic h1 for the same problem if the value of h2 is always greater than or
equal to that of h1.
Definition: If two heuristic functions, h1(n) and h2(n), are both admissible, then h2 dominates h1 if
h2(n) ≥ h1(n) for all n.

Notice in the above definition that both heuristic functions need to be admissible for this
definition to hold. In other words, they cannot overestimate the true cost. This is another reason
why the concept of an admissible heuristic is important. Why is the concept of dominance
important? The answer is that if h2 dominates h1 then we can say that h2 will lead to a more
efficient search than h1.

To illustrate the difference that a good heuristic can make, let us return to the 8-puzzle example
that we first saw in Chapter 3 (see Figure 3). Actually this puzzle has more than 180,000
different possible states. This might be just about solvable by an uninformed search algorithm,
but if we make the grid bigger so that we have 15 tiles on a 4 by 4 grid the number of states rises
to 1013, so a heuristic function is clearly necessary. Table 2 shows the number of nodes expanded
(a good measure of time/space complexity) using the uninformed iterative deepening search
(IDS) and the A* search using two different heuristics:
 h1(n): the number of misplaced tiles in state n, compared to the goal state. For example, in
the start state in Figure 3 all 8 tiles are misplaced (not in their desired locations), so the
value of h1(n) would be 8.
 h2(n): the total Manhattan distance (or city-block distance) of each tile from its desired
location. The Manhattan distance is the distance travelled when only moving in a vertical
or horizontal direction. For example, tile 1 in the start state in Figure 3 must move up two
squares and left one square to reach its desired location, so its Manhattan distance is 3.
The total Manhattan distance of all tiles for this start state is 18.

Figure 3 – The 8-puzzle problem

7
Solution depth d IDS A* with h1 A* with h2
d=6 680 20 18
d=12 3,644,035 227 73
d=18 Too many nodes 3,056 363
d=24 Too many nodes 39,135 1,641

Table 2 – Comparison of iterative deepening search (IDS) and A* search with two different
heuristic functions, h1 and h2

We can see from Table 2 that dramatic improvements can be made by introducing a heuristic
function to inform the search process. (For a solution depth of 12, the A* search is between
16,000 and 50,000 times more efficient than uninformed iterative deepening search.) But there is
also a significant difference between the two different heuristic functions. The reason for this
difference lies in the fact that h2 dominates h1. We can see this intuitively: if a square is
misplaced (meaning it contributes 1 to the total h1 value), then its Manhattan distance must be at
least 1 - often 2, 3 or even 4. Therefore the total Manhattan distance cannot be less than the
number of misplaced tiles, meaning the Manhattan distance heuristic dominates the misplaced
tiles heuristic. The difference in performance shown in Table 2 is a direct result of this
dominance, and illustrates the importance of choosing a good heuristic function.

3.3 Relaxed Problems

Now that we know that we need to have a good heuristic function to be able to solve difficult
problems efficiently, we need a way of generating new admissible heuristic functions. Before we
can understand how to do this, we must first understand the concept of a relaxed problem. Every
problem has a set of rules and restrictions. For example, in the 8-puzzle the rules are that there
are 8 tiles, and these tiles can only move to adjacent, unoccupied squares. In chess the rules
specify the allowable moves for each piece on the board. If we remove (or relax) some of these
rules then we create a new, relaxed, version of the original problem. For example, we could relax
the constraint that there are 8 tiles in the 8-puzzle, instead having, for example, 4 tiles. Relaxed
problems tend to be much easier to solve than the original problems they are derived from.

Why is it useful to generate relaxed versions of problems? The answer lies in the important fact
that the actual cost of an optimal solution to a relaxed problem is an admissible heuristic for the
original problem. For example, in the 8-puzzle if we relax the constraint that tiles cannot occupy
the same square, then the cost of the optimal solution to this relaxed problem is the total
Manhattan distance for all tiles, i.e. we can just move each tile to its desired location without
worrying about moving other tiles out of the way first. We have already seen that the Manhattan
distance is an admissible heuristic for the 8-puzzle. Similarly if we relax the constraint that tiles
have to move to adjacent squares, then the cost of the optimal solution to this relaxed problem is
simply the number of misplaced tiles: if we can move a tile anywhere then it takes only 1 move
to get each tile in its desired location. Again, we have already seen that the number of misplaced
tiles is an admissible heuristic for the 8-puzzle.

8
The generation of relaxed problems is an important tool for AI system developers. If we can
specify the rules and constraints of a problem, then removing some or all of these constraints will
lead to an admissible heuristic to the original problem. We can then use the concept of
dominance to decide which of these heuristics is the best one.

4. Local Search

Until now, we have only considered the type of problem in which we know the goal state, and
are interested in finding out the sequence of steps that lead from the initial state to this goal.
Many problems are not like this. Often we do not know the goal state, and we are interested in
discovering it, but we are not interested in knowing what sequence of steps led us from the initial
state to the goal.

To illustrate this difference, we will examine a new problem: the n-queens problem. The aim of
this set of problems is to position n queens on a n by n chess board, so that no pair of queens is
attacking each other. For example, Figure 4 shows a solution for the 4-queens problem. This
problem can be extended to any number of queens, so long as the board size increases
accordingly: the higher the number of queens, the more difficult the problem becomes.

Figure 4 – A goal state for the 4-queens problem

In the n-queens problem we do not know what the goal state is – the goal is stated implicitly.
Also, we are not interested in how we reached the goal – all we want is to get a solution to the
problem. If we have this type of problem, we can make use of a different type of search
algorithm: local search algorithms.

Local search algorithms work by keeping a single current state in memory, and trying to improve
it by moving to other “neighbouring” states. We move to a neighbouring state by making some
minor change to the state (e.g. moving one queen). In order to be able to judge whether a move
to a different state “improves” our situation (i.e. moves us closer to the goal), we need to have an
objective function that gives us an indication of how good a state is. Because local search
algorithms keep only s single state in memory, their space complexity is much better than normal
informed search techniques.

9
4.1 Hill-Climbing

The simplest local search technique is known as hill-climbing. The hill-climbing algorithm has
been likened to “climbing Mount Everest in thick fog with amnesia”. We are attempting to
maximise the objective function, so we need to make changes to our current state that enable us
to find a maximum value of this function (like climbing Mount Everest). We are only able to
make small, local changes to the state, so we cannot “see” very far to completely different states
(like being in thick fog). Finally, we do not remember anything about the states we have visited
(like having amnesia).

We can visualise local search techniques as moving around a “state space landscape” to search
for a global maximum of the objective function. For example, Figure 5 is a graph showing a
simple (1-dimensional) state space, with corresponding values for the objective function. We
“move” (either left or right) in this state space landscape by making small changes to our current
state. The figure shows a number of common features of such landscapes:
 A global maximum is the maximum value of the objective function anywhere in the state
space – this is the aim of local search techniques
 A local maximum can appear to be a global maximum to algorithms that cannot “see”
very far in the landscape.
 A shoulder and a flat local maximum can also appear to be identical to algorithms that
cannot “see” very far. For a shoulder, it can be best for the algorithm to keep moving
through the flat part, whereas for the flat local maximum to keep moving will not gain us
anything.

Figure 5 – A sample state space “landscape”, illustrating some common features

We illustrate the hill-climbing algorithm using the 8-queens problem. Figure 6 shows a sample
(non-goal) state for 8-queens. We use as an objective function h the number of pairs of queens

10
that are attacking each other. In the state shown, h=17. Note that in this example, our aim is to
minimise, not maximise, this function. We can move to neighbouring states by moving a single
queen within a column. The numbers on the squares in Figure 6 show what the value of h would
be if we moved the queen on that column to each square. The hill-climbing algorithm will
always move towards the steepest hill, i.e. in this example, it will choose the neighbouring state
with the lowest value of h. From the state shown, the best neighbouring state has a value of
h=12. Since a number of neighbouring states share the same value, we can choose a random one.
Using hill-climbing, we repeat this process until no neighbouring states will improve the
objective function. At that point, we assume we have reached a maximum and stop.

The hill-climbing algorithm can be summarised by the following pseudocode:

current = initial state


Loop:
neighbour = successor of current with highest value of objective function h
If h(neighbour) ≤ h(current) then return current
Else current = neighbour

Hill-climbing is sometimes called greedy local search, because it always chooses short-term
advantage without thinking about what lies ahead. We can see from Figure 5 that sometimes
such behaviour can lead to getting stuck in a local maximum. The success of hill-climbing
therefore depends to a large degree on the shape of the state-space landscape: if the landscape is
smooth with few local maxima, then it is likely to perform well; however, many problems have a
state space landscape that is spiky and contains many local maxima, so in these cases we need a
different approach.

Figure 6 – A sample state of the 8-queens problem: the objective function h is the number
of pairs of queens that are attacking each other. For this state, h=17. The numbers show
what value h would take if the queen in that column were moved to that square.

11
4.2 Simulated Annealing

In order to escape from the local maximum in Figure 5, we need an algorithm that sometimes
allows us to make bad moves in the short-term, in the hope of achieving a better state in the long-
term. One such algorithm is called simulated annealing. This name reflects the fact that the
algorithm was inspired by a metal-hardening process. In metallurgy, annealing is the process of
heating a metal to a high temperature, and then gradually cooling it. This process causes the
molecules to settle into a crystalline state, resulting in a very strong material. We can simulate
this annealing process in local search. We first define a “temperature” variable. Next, at each
iteration of the algorithm, we select a random move from the set of neighbouring states. If this
move improves the objective function, we accept it. Otherwise, we accept it with probability p.
The value of p depends on how bad the move is, and also on the temperature: if the temperature
is high we are more likely to accept bad moves. We start the algorithm with a high temperature
(like heating the metal), and then gradually reduce it (cool it) according to an annealing
schedule. The following pseudocode summarises the simulated annealing algorithm:

Set annealing schedule


current = initial state
Loop:
Set temperature T according to schedule
If T=0 then return current
next = random successor state of current
ΔE = h(next) – h(current)
If ΔE > 0 then current = next
Else current = next only with probability e ΔE/T

Simulated annealing has proved to be a popular algorithm as it has a very high success rate in
finding global maxima in difficult state space landscapes. In fact, it can be shown that if the
annealing schedule is slow enough, simulated annealing will find a global maximum with
probability approaching 1. However, the slower the annealing schedule is, the longer it takes to
run the algorithm.

4.3 Local Beam Search

Local beam search works by keeping not just a single current state in memory, but several
current states. We start with k random starting states. For each of these states, we generate all
possible successors. From this pool of successor states, we choose the k best ones and repeat the
process. This iteration continues until one of the k states is a goal state. The following
pseudocode summarises local beam search:

12
current_states = k random states from the state space
Loop:
next_states = empty set
For each state s in current_states:
Add all successors of s to next_states
If any one of next_states is a goal, stop and return that state
Else current_states = best k states from next_states

Note that this algorithm is not the same as running k different hill-climbing algorithms in
parallel. The successors of the k states are pooled before choosing the next “generation” of states.
Therefore unpromising states may have no successor states chosen, whereas promising states
may have several. This can be a strength of the algorithm, but it can occasionally be a weakness
too. After several iterations of local beam search, the k states can tend to become “concentrated”
in a small region of the state space, leading to a lack of diversity. If all k states are very similar, it
becomes less advantageous to keep all k states. There are variants of local beam search that
address this problem.

4.4 Genetic Algorithms

The final local search algorithm is one of the most well-known, and was originally inspired by
evolution. In evolution creatures breed to produce the next generation of creatures, and typically
the ones that survive to breeding age are the ones that are somehow better suited to their
environment, making it more likely that their offspring will survive too.

The genetic algorithm local search technique includes many of these same features. We define a
fitness function (similar to the objective function used in hill-climbing) to determine how good a
state is and therefore how likely it is to “reproduce”. We start with a “population” of random
states. At each iteration we randomly select two individuals of the population (weighted
according to their fitness), and combine them to produce a “child” state. With a small random
probability this child state may undergo some mutation. Once we have produced enough
children. These children become the breeding population and we start all over again. The
following pseudocode summarises this process:

population = a set of k random states (each state represented as a string)


Loop:
new_population = empty set
Loop from 1 to size(population):
x,y = random states from population, selected according to their fitness
child = reproduce(x,y)
If (small random probability) then child = mutate(child)
Add child to new_population
population = new_population
Until some individual is fit enough, or enough time has passed
Return the most fit individual in population

13
Each state, or individual, of the population is represented as a string of numbers or characters.
When pairs of individuals breed, they produce two child states. The child states are constructed
by taking a certain proportion of one parent’s string, and the rest from the other parent. The exact
proportion is determined by a random crossover point. Mutations are made by randomly
changing one character in the string.

For example, consider the 8-queens problem again. We can represent each board state in 8-
queens by a string of 8 numbers between 1 and 8: since each column must contain one queen, the
numbers indicate the row on which the queen is positioned for each column. For example, the
board state in Figure 6 would be represented by the string “43254323”, because the queen on the
first column is on row 4, the one on the second column is on row 3, and so on. Figure 7 shows
one iteration (“generation”) of the genetic algorithm process. In Figure 7(a) we see 4 random
initial states. Each of these is ranked according to a fitness function, A fitness function should
return a high value for good states, so we use the number of non-attacking pairs of queens as our
fitness function. The best state has a fitness function value of 24, and the other 3 have 23, 20 and
11. These fitness function values are used to compute a percentage chance for each state of being
selected for reproduction (31%, 29%, 26% and 14%, shown in Figure 7(b)). Figure 7(c) shows
two pairs that have been randomly selected (using the percentage chances) – note that one state
was chosen twice, and one not at all. For each pair, we choose a random crossover point (shown
as a dotted line), and combine the two parents to produce two child states (Figure 7(d)). Finally,
with a small random chance each child may undergo some random mutation to their state. Figure
7(e) shows the final child states, which will be used as the next generation in the genetic
algorithm.

To illustrate the breeding process further, Figure 8(a) and (b) shows the top two parent states
from Figure 7(a), with the crossover point shown as a dark vertical line. The board state in
Figure 8(c) represents the first child state from Figure 7(c), produced by taking the first 3
columns from its first parent and the next 5 columns from its second parent.

Genetic algorithms have enjoyed a lot of popularity in recent years, and have found application
in areas such as optimising printed circuit layouts and job scheduling. However, it is still not
clear whether their popularity is because of real improvements in performance or because people
like the idea of using ideas from evolution in computer science. Much work remains to be done
to identify the types of problem in which genetic algorithms can be successful.

14
Figure 7 – Genetic algorithms: (a),(b) initial population ordered by the fitness function; (c)
selected pairs breed to produce child states in (d); (e) random mutations applied to child
states.

(a) (b) (c)

Figure 8 – Breeding in genetic algorithms: (a),(b) the top two parent states from Figure
7(a); (c) the top child state from Figure 7(d).

15
Exercises

1) Consider the following state space. There are five locations, identified by the letters A to E,
and the numbers shown are the step costs to move between them. Also shown is the value of
a heuristic function for each state: the straight-line distance to reach E. The initial state of the
problem is A, and the goal is to reach E.

a. Draw the search tree that would result after each iteration of greedy best-first search.
Is the solution optimal?
b. Draw the search tree that would result after each iteration of A* search. Is the solution
optimal?

2) Suggest some admissible heuristics for the following problems:


a. An agent trying to find a way out of a maze. The maze consists of a number of
locations. Each adjacent location may have a path linking them or a wall. The agent
may not travel through walls. Assume that the agent knows its current location and
the location of the exit, but does not have a map of the maze.
b. The vacuum-cleaner environment, in which the vacuum-cleaner agent can be in one
of two squares, and must try to clean both squares of dirt. The agent can move
between squares and clean its current square.

16
3) Consider the environment shown in the picture below. A robot agent has the task of moving
through the environment, avoiding all obstacles, and arriving at the goal G. The robot has a
GPS (Global Positioning System) that enables it to know its current location. It also knows
the location of the goal. The agent must try to arrive at the goal by the shortest distance
possible.
a. Assume that we use a hill-climbing algorithm, using the straight-line distance to the
goal as the heuristic function. Using the environment shown in the picture, and with
random initial and goal positions, is the agent guaranteed to find a solution? Is it
guaranteed to find an optimal solution?
b. In what type of environment would the agent not be guaranteed to find a solution?

17
Exercise Solutions

1) The answers are:


a. Greedy best-first search finds a solution after expanding just two nodes. However, the
path found (from A to D to E) is not optimal – the path cost is 13.

b. A* search takes longer to find a solution, expanding three nodes. But the path found
(from A to B to C to E) is optimal. This is because the heuristic used (the straight-line
distance to E) is admissible for this problem (i.e. it will never overestimate the true
distance).

2) The following are suggestions for admissible heuristics, generated from relaxed versions of
the problem. The suggestions are by no means the only correct answers.
a. The Manhattan distance from the current location to the exit. This is generated by
relaxing the rule about not being able to move through walls.
b. The number of squares still containing dirt. This is generated by relaxing the rule
about the agent only being able to clean its current square. If it were able to clean
either square then the number of dirty squares will be equal to the move of moves
taken to clean them.

18
3) The important point to note about the environment is that all obstacles are convex. If the
obstacles are convex then hill-climbing (i.e. simply trying to minimise the distance between
the agent and the goal) will always succeed.
a. The agent will always find a solution but it will not necessarily be an optimal one. For
example, the solution of hill-climbing is shown in the left picture below, whereas the
right picture actually gives a shorter solution. Hill-climbing would not find this
solution because (for example) the initial path taken was not the most direct one to
the goal.

b. The agent would not be guaranteed to find a solution if the environment contained
non-convex objects. For example, in the environment shown in the picture below,
hill-climbing gets stuck in a local maximum, as there is no path that will increase the
value of the objective function.

Source: Artificial Intelligence: A Modern Approach,


Stuart Russell & Peter Norvig,
Prentice-Hall, 2003 (Second Edition)

19

You might also like