Heuristic Optimisation
Problem sheet 1
Brief solutions
1. Mr. Smith and his wife invited four other couples for a party. When everyone arrived, some of the
people in the room shook hands with some of the others. Of course, nobody shook hands with their
spouse and nobody shook hands with the same person twice.
After that Mr. Smith asked everyone how many times they shook someone’s hand. He received
different answers from everybody.
How many times did Mrs. Smith shake someone’s hand?
Solution
This seems a difficult problem. However, by choosing a good representation, it becomes quite easy.
Represent the problem as a graph. The nodes of the graph are people given by their number of
handshakes. The arcs represent the handshakes between people.
• With 10 people in a room, the number of possible handshakes for one person can range between
0 and 8.
• So, the 9 people gave as answers the numbers 0, 1, . . . , 8.
• The person shaking hands 8 times must have shaken hands with everybody, except their spouse.
As there is one person who did not shake hands at all, that person must be 8’s spouse.
• Person 7 could not shake hands with 0 or 1 (because 1 has already shaken hands with 8), so
he/she must have shaken hands with all the others. 1 and 7 are a couple.
• Person 6 could not shake hands with 0 or 1 or 2 (because 1 has already shaken hands with 8,
and 2 has already shaken hands with 8 and 7), so he/she must have shaken hands with all the
others. 2 and 6 are a couple.
• ...
• At the end, Mrs. Smith must be the person shaking 4 hands.
1
2. Discuss possible representations, size of search space, and possible evaluation functions for Rubik’s
cube. You may want to check a Rubik’s cube application on your smart phone or an online virtual
cube, such as
http://www.speedcubing.com/CubeSolver/CubeSolver.html
http://www.mathplayground.com/rubikscube.html
and play a little bit with it before thinking on this.
Solution
Let’s look at two possible representations:
• Use the colour of the squares on each face of the cube in a given order.
The middle square has fixed colour, which can give the order for listing the faces: white, blue,
red, yellow, green, orange.
For each face we have to record 8 squares (no need for the middle one), for each square there
are 6 possible colours, and there are 6 faces, so the size of the search space is (68 )6 .
Note that if we consider the fact that we can actually have exactly 8 squares of each colour, we
have a feasible region of the search space of a much smaller size: we have 8 squares on each
sides and 6 sides, so 48 squares alltogether. Each square can be theoretically in any position,
which gives 48! possibilities. As there are 8 squares of the same colour and 6 colours, there
will be (8!)6 positions which look the same, so the size of the search space is 48!/[(8!)6 ]. In this
counting we did not consider the fact that a square which is in a corner cannot appear on an
edge and viceversa. Taking this constraint into account might reduce the size of the feasible
region at the cost of extra calculations!
Evaluation function: how many squares are of the right colour?
• Use the small cubes. There are 20 cubes to record: 12 “edges” and 8 “corners”.
Each “corner” could be in any “corner” position, and it can have 3 possible orientations, so the
number of possible options for corners is 8! × 38 , similarly for “edges”, 12! × 212 , the size of the
feasible region is 8! × 38 × 12! × 212 .
Evaluation function: number of small cubes in right position.
2
3. Consider the maze given below.
y
4
3G
2 S
1
1 2 3 4
x
The black squares are obstacles. The objective is to get from the start node S to the goal node G,
by moving horizontally and vertically. Use hill-climbing to solve this problem.
Solution
The evaluation function f is the Manhattan distance from G=(1,3) which should be minimised.
We start at S=(4,2) with an evaluation value of 4.
At next step we can go to (4,1), (4,3), or (3,2). We have f (4, 1) = 5, f (4, 3) = f (3, 2) = 3. We have
to choose the smallest value, that is, to go to either (4,3) or (3,2). Let us go to (4,3) with f (4, 3) = 3.
At next step we can go to (4,4) or (3,3) (there is no need to go back to (4,2)). We have f (4, 4) = 4
and f (3, 3) = 2. We have to choose the smallest value, that is, to go to (3, 3) with f (3, 3) = 2.
At next step we can go to (3,2) or (3,4). We have f (3, 2) = f (3, 4) = 3. But this value is worse than
f (3, 3) = 2. Hence the algorithm finishes at (3,3) with a Manhattan distance of 2 from the goal.
3
4. Consider the function f (x, y) = x2 + xy + y 2 + 3x + 4y defined on points of integer coordinates in
the box [−10, 10] × [−10, 10]. Use the hill-climbing algorithm to find the function’s minimum value
on this box.
First define the search space, then the neighbourhood. Try to find the minimum using the starting
point (x, y) = (0, 0).
Solution
The search space is the set
S = {−10, −9, . . . , −1, 0, 1, . . . , 9, 10} × {−10, −9, . . . , −1, 0, 1, . . . , 9, 10}.
Define the neighbourhood of a point (x, y) by
p
N (x, y) = {(z, t) ∈ S : (z − x)2 + (t − y)2 ≤ 1}.
The neighbourhood of (0, 0) is
N (0, 0) = {(0, 0), (−1, 0), (1, 0), (0, −1), (0, 1)}.
We have f (0, 0) = 0, f (−1, 0) = −2, f (1, 0) = 4, f (0, −1) = −3 and f (0, 1) = 5. Since we are
looking for the minimum, hill-climbing will select (x, y) = (0, −1). The neighbourhood of (0, −1) is
N (0, −1) = {(0, −1), (−1, −1), (1, −1), (0, −2), (0, 0)}.
We have f (0, −1) = −3, f (−1, −1) = −4, f (1, −1) = 0, f (0, −2) = −4 and f (0, 0) = 0. Since we
are looking for the minimum, hill-climbing will select (x, y) = (−1, −1) or (x, y) = (0, −2). Suppose
the algorithm has selected (x, y) = (−1, −1). The neighbourhood of (−1, −1) is
N (−1, −1) = {(−1, −1), (−2, −1), (0, −1), (−1, −2), (−1, 0)}.
We have f (−1, −1) = −4, f (−2, −1) = −3, f (0, −1) = −3, f (−1, −2) = −4 and f (−1, 0) = −2, so
there is no further improvement in the value of f . Since we are looking for the minimum, hill-climbing
will stop and give the answer (x, y) = (−1, −1), with the minimum f (−1, −1) = −4.
4
5. Consider the following 6 dimensional SAT problem.
_ _ ^ _ ^ _
F (x1 , x2 , x3 , x4 , x5 , x6 ) = (x1 x2 x3 ) (x2 x6 ) (x3 x6 ))
(a) Consider the incomplete solutions given by the binary strings of length 6 that start with 01.
This property determines a subspace of the search space. Does there exist a solution of the
problem which shares this property? If yes, then what should the value of x3 and x6 be for such
a solution?
(b) Consider the incomplete solutions given by the binary strings **0**1 (i.e, binary strings of
length 6 with x3 = 0, x6 = 1). This property determines a subspace of the search space. Does
there exist a solution of the problem which shares this property? If yes, then what should the
value of x2 be for such a solution?
(c) Consider the incomplete solutions given by the binary strings *1*011. This property determines
a subspace of the search space. Determine all solutions of the problem which share this property.
Solution
W W W
(a) In this subspace: The first clause is 0 0 x3 and the second clause is 0 x6 . In order to
have a solution, the value of the first and second clause must be 1, hence x3 = 1 and x6 = 0,
respectively. Since x3 = 1, the value of the third clause is 1. Thus, we got a solution which
shares the property of the subspace.
(b) In Wthis subspace: The value of the third clause is 1, because x6 = 1. The second clause is
x2 0. In order to have a solution, the value of the second clause must be 1, hence x2 = 0.
Since x2 = 0, the value of the first clause is 1. Thus, we got a solution which shares the property
of the subspace.
(c) In this subspace: The second clause is false, because x2 = 1 and x6 = 1. Hence, there is no
solution which shares the property of the subspace.