Documente Academic
Documente Profesional
Documente Cultură
Semester 2
School of Computing
IMPORTANT INFORMATION
BAR CODE
university
Define Tomorrow. of south africa
ASSIGNMENT 01
Solution
Total Marks: 149
UNIQUE ASSIGNMENT NUMBER: 691007
Study material: Chapters 1 through 4. You may skip sections 4.2 and 4.5.
Question 1: 9 Marks
(1.1) Compare Simple Reflex Agents to Model Based Agents. Pay attention to: (6)
• the environments in which one may outperform the other,
• the difference in their architecture,
• their underlying implementation and functioning.
Simple reflex agents react purely based on the rules governing their current percepts.
They work well in fully observable environments and consist of simple percept and
rules (condition-actions), They have no memory, or model of the world. As all agents
do, they have sensors-rules-actuators.
Model based agents maintain an internal state/model of the world (thus, there is mem-
ory, as well as rules that modify their internal model in order to keep track of those parts
of the world they cannot observe), they work particularly well in partially observable
environments (and will typically outperform simple reflex agents here).
(1.2) Consider a Global Positioning System (GPS) navigation unit one might find in a car. (3)
Does this unit constitute an agent as defined in the text book? Justify your answer.
Based on the definition in the textbook it does not. It does bear a striking resemblance
to a simple problem solving (goal based) agent with one difference: it has no ability
to execute the plan that was created, that is, there is no way for the GPS unit in your
vehicle to effect a left or right turn in the car, unless we account for the fact that YOU
may form part of the ’agency’ (i.e you are the actuators the GPS agent uses to effect
change in its world.).
Question 2: 23 Marks
(2.1) List the 5 components that can be used to define a problem. (5)
1. Initial state
2. Actions
3. Transition model
4. Goal test
5. Path cost
2
COS3751/201
(2.3) Differentiate between expanding and generating with respect to searching. (2)
Expansion is the action of applying all legal actions to the ’current’ state (i.e. a partic-
ular state that was chosen in some way), when applying a legal action to a state, other
states may be generated which can be placed on the frontier..
(2.4) List and discuss three types of queues that may be employed in a search. (6)
1. FIFO: Oldest nodes are removed first. BFS makes use of these types of queues
to ensure that nodes are expanded level by level.
2. LIFO: Also known as a stack. Newest nodes are removed first. DFS makes use
of this to ensure that nodes are expanded based on depth.
3. Priority queue: Nodes are added and sorted based on some key, this ensures
that certain states take priority over others during the expansion phase.
(2.5) List and explain the measures used to determine problem solving performance. (8)
2. Optimality: If the algorithm does find a solution, will it be the best one? Optimal
path cost among all solutions?
3. Time complexity: Does the algorithm run within an accetpable time range?This
is part of complexity analysis, and is typically done expressed in Big-Oh notation
to remain architecture agnostic. For example, an O(n) algorithm is ovbiuosly
preferred to an O(n2 ) algorithm.
3
O
Question 3: 12 Marks
Consider the simple game of tic-tac-toe (see Figure 1), or as we call it naughts-and-crosses (https:
//en.wikipedia.org/wiki/Tic-tac-toe). The game is played by two players on a three by three
square board, and each player takes a turn to place a token in a square. Each player has their own
token: either an ’X’ (crosses) or an ’O’ (naughts).
(3.1) If we can formulate the problem in a way that allows it to be translated into a computer (2)
based solution, we can easily implement an agent that can play the game.
Define a mathematical state representation for the game. Remember: a state rep-
resentation is supposed provide a snapshot of the world at a particular point in time.
In terms of an agent, this means it will be what it can perceive at a particular point
in time. Hint: Read the section on arrays in the mathematical background chapter of
your textbook.
Being able to encode state is an important step in designing an algorithm to solve a
problem. If we cannot represent a state, abstracting away all the unneeded informa-
tion, and ensuring what remains is sufficient for the algorithm to operate, we won’t be
able to solve the problem. Most state representations are simple sets, or arrays, or
matrices. However, you can choose any state representation, as long as it can be
used to solve the problem at hand. Graphical state reprentations are redundant since
we would have to figure out how to represent the graphical state representation in a
format that the algorithm can use. This means we are back where we started without
having moved closer to solving the problem. A graphical state representation will, how-
ever, help you to start conceptualising the problem, so it is normally a good starting
point. However, when a question asks for a formal state representation, don’t provide
your graphical model.
A simple state representation is a matrix representing the board in its entirety. Thus
we have the tic-tac-toe board: B, with bij ∈ {0, 1, 2}, 1 ≤ i, j ≤ 3, where a 0 represents
an empty square, a 1 presents a ’O’, and a 2 represents an ’X’.
(3.2) Now that you have defined a state representation, define the initial state for the prob- (2)
lem.
Using the proposed state representation above, we can easily use the following for our
start/initial state.
0 0 0
B0 = 0 0 0
0 0 0
4
COS3751/201
(3.3) Define an applicable action for the agent using the start state. Assume that naughts (2)
moves first (that is, the user using token ‘O’ moves first).
So an action should simply define a legal action that can be taken given a state. Ideally
it should be the same for every state.
A good general action would thus be something like Put(P, i, j). With P ∈ {1, 2}, and
1 ≤ i, j ≤ 3. This allows us to place a 1 or a 2 in the appropriate place given the row
and column indicators i and j.
For the start state we may have Put(1, 2, 2) which would result in an naught right in
the middle of the board.
(3.4) Now define an applicable action for the state resulting from the action you defined (2)
first.
Now suppose the first move was in fact an ’O’ being placed in the centre of the board.
That is:
0 0 0
B0 = 0 1 0
0 0 0
We now simply define an action to place an ’X’ somewhere on the board. For example:
Put(2, 1, 1) which would result in
2 0 0
B00 = 0 1 0
0 0 0
(3.5) Define a goal state for the problem based on your state representation. Explain how (4)
an agent might detect a goal state using your state representation. (Remember, as
humans we can easily see a state and interpret a state as a goal – it is not so simple
for computers).
A goal state, based on the rules of the game, results from a row, column, or any one
of the diagonals containing the same symbol: an ’X’ or an ’O’. Suppose for example
’O’ won the game. A sample goal state here would be:
1 2 2
BG = 0 1 0
0 0 1
(it is not enough to simply show a board with just 1s (’O’s) on it, remember, the other
player will move as well.)
There are many more variations on this.
How would we determine if the goal state was reached? Any algorithm which tests if
all the columns or rows is 1 (or 2), as well as testing for the diagonals will do the job.
5
Question 4: 15 Marks
(4.1) Highlight the differences between a tree and graph search. (3)
Remember that a tree is a simple-connected acyclic graph. (That means that there are
no loops, and that by definition there must be nodes that have no children.) A graph,
on the other hand may have cycles or loops.
The main difference between a tree search is thus that we don’t need to keep track of
already explored nodes, since a simple tree cannot have revisited states. Thus, the
tree search simple selects a leaf node from the frontier, goal tests, and if it is a goal it
returns the path (solution) to that node.
Applying a tree search to a graph creates problems since there may be redundant
paths and loops. The graph search solves this problem by augmenting the tree search
with a ’closed list’ (or explored list). When nodes are generated during the search that
are already on the closed list they are not added to the frontier.
(4.2) Explain what a redundant path is, and why redundant paths may cause intractability (3)
problems during goal searches.
A redundant path is a (worse/another) solution to the same problem. For example
generating a slower (or longer) route to a destination.
Redundant paths may not always be a problem as far as tractability is concerned,
but a redundant path could be a loop, in which case the problem may have several
redundant paths (which can quickly bloom into a vast number of paths to consider).
This could cause a relatively straight-forward problem to become intractable.
(4.3) Consider the sliding-block puzzle game state provided below (figure 2). How many (4)
distinct states are there for this puzzle? How many search nodes? Explain how you
reached your answer.
I N L D
E C K
B H J O
A F G M
It is interesting to note that half of random start states for the 15 puzzle are not solvable.
For the 15-puzzle, we will always have 16! distinct states. However, since only half of
the random start states are solvable, we have a possible search space of 16!/2.
Why use 16!? If we start with an empty board we can choose to place any of the
16 tokens (numbers 1 to 15 and space) in the top-left position, from here on we can
choose any one of the remaining 15 tokens for the position just to the right of the
top-left, and so we carry on: 16 × 15 × 14 × ... × 2 × 1 = 16!.
6
COS3751/201
If you’re concerned about the number of distinct states for the example provided above,
you will have to consider the number of moves that can be used to solve the puzzle
– but that would only yield a possible lower-bound (assuming our search is super-
efficient) on the search space. From a space-complexity viewpoint, it will suffice to
state that the upper bound of the distinct number of states is 16!.
(4.4) Explain the separation property of the graph search. Provide an example to support (5)
your discussion.
The graph search separates the state space of the search into explored and unex-
plored states.
An example is straight-forward: finding a route from one town to the next – we have a
set of paths representing towns already visited during our search, and a set of paths
for towns we still have to visit.
Question 5: 8 Marks
B C
D E F G
H I J K L M N O
(5.1) Show the order in which the nodes will be expanded (from limit 0 to limit 4) given that (5)
IDS is used. Assume the goal node is O, and that nodes are expanded from left to
right.
Expansion means we apply legal actions to a chosen node – this, by definition, means
that our order looks somewhat different from what one may expect.
Look at the algorithm for the IDS. The goal test is performed on generation. This
means that a node is tested as being a goal when it is generated not when it is ex-
panded. This is important since it effectively means that the last level in the tree is
never expanded.
For example, in the figure, suppose the goal was C. At limit 0, A is only goal tested.
At limit 1, A is goal tested, and then expanded. B is generated, and goal tested –
7
however, the test fails, and B is not expanded because the limit has been reached. C
is then generated, and goal tested. The test succeeds, and the search terminates.
Thus:
3. Limit 2: A B C (Now B and C are expanded, and the goal test is applied to the
children of each in turn, but their children are not expanded).
(5.2) Has this search performed better than a regular Depth First Search (DFS) in terms (3)
of space complexity? Explain your answer. (Assume a DFS in which nodes are also
expanded from left to right.)
Remember that the IDS is a dynamic version of the depth limited search which is in
essence a DFS. In terms of space requirements it should thus not use more space
than a DFS (remember: it generates nodes in the same fashion as DFS, but discards
them when the depth limit is reached).
No. To find the solution we had to generate the entire search treethat the DFS would
have generated in any case.
Question 6: 18 Marks
(6.3) How does UCS select the next node for expansion? (2)
It simply chooses the node from the frontier that will minimise total path cost.
8
COS3751/201
11
B 20
15
C
D 4
24
14
22 F
G E
9
12
(6.4) Suppose we start at B and the goal is F . Write down the nodes in the order they (12)
are expanded. Show your steps and calculations. The step cost between nodes is
provided next to the edges in the graph.
Since we’ve been talking about uniform cost, we’ll use the UCS algorithm.
2. Choose (B, B, 0) (and remove) for expansion: generate the path (B,A, 11) and
(B,D, 15) (where (B,A,11) means that there is a path from B to A which costs 11)
3. Choose (and remove) (B,A,11) (lowest cost, but not goal) – generate (B,A,C,31)
and place on the frontier.
4. Choose (and remove) (B,D,15) (lowest cost, but not goal) – generate (B,D,E,37)
and (B,D,G,29) and place on the frontier.
9
5. Choose (and remove) (B,D,G,29) (lowest cost, but not goal) – generate (B,D,G,H,41)
and place on the frontier.
6. Choose (and remove) (B,A,C,31) (lowest cost, but not goal) – generate (B,A,C,E,55),
(B,A,C,F,35) and place on the frontier.
7. Choose (and remove) (B,A,C,F,35) (lower cost, and goal) – search terminates.
Taking the nodes from the map as they are expanded, you thus have B,A,D,G,C..
Question 7: 12 Marks
Question 8: 31 Marks
Consider figure 5 and table 1 and answer the questions that follow. Table 1 provides the estimated
distances from each node to H (thus ĥ for each node). The label next to each edge provides the
cost between nodes (thus ĝ).
10
COS3751/201
A
7
3
8
B D 10
10 C 4
7
F K
8
6 5
10 3 2
5 I
E
G M
5 4 L
1
H 3
Figure 5: A∗ Search
(8.1) Suppose the start node is node A and the goal node is node H. List the nodes in the (10)
order in which they are expanded (not generated). Show your steps (don’t just list the
nodes, explain the reason for each choice).
Lets start by adding A to the frontier so that the search can kick-off. In actual fact we
add A with some additional information so that the search can discriminate between
11
good choices during the search. We’ll add < Psn , csn >, where Psn is a path than can be
explored (from the start node S to node n), and csn is the cost of getting to n added to
the estimate of getting to G (the goal). But we know that for A∗ , csn is f̂ (n) = ĥ(G) + ĝ(n)
So, we choose the node in the frontier with the lowest f̂ : < A, (10) >, and we expand
it.
Expanded thus far: A.
Now we choose, again, the node from the frontier with the lowest f̂ value: << A, D >
, (8) >. This means we have to expand D next. Expanded thus far:A,D
So we expand D, and the frontier then looks thus:
The search node with the lowest f̂ is the one with the path through K , so we choose
that, and thus we expand K . Expanded thus far:A,D,K
Frontier:
Notice that I’ve placed the path to M below the path to L even though they have the
same f̂ values. I did this as an internal conflict protocol (internal to the algorithm). As
long as I am consistent in applying this conflict resolution (and it is not grossly wrong)
everything should turn out fine. In this case the rule is simple: paths with fewer nodes
have priority.
So we choose the path through L, and thus expand L. Expanded thus far: A,D,K,L.
Frontier:
12
COS3751/201
13
(8.2) When adding nodes to the frontier, many people assume that we simply add the nodes (4)
in the search space (in this case the graph). This is not always the correct approach.
Explain why this is so, and state the difference that will exist between the nodes in the
search space and the nodes in the frontier.
Simply adding the nodes in the state/search space may not convey the needed infor-
mation to solve the problem. It is not always the case, if the search space naturally
encodes the solution (or an intermediate solution) then the nodes from the state space
should suffice.
For example: if the nodes in the search space for the previous question are simply
nodes in the state-space, then the path to follow from start to finish is not properly
encoded in the solution to the problem. We need to have the path to follow (or the
route to travel) in order to be able to solve the problem. Thus it is often better to
encode partial solutions in the nodes in the search space: in this way they become
part of the solution to the problem, and will be used as they are in the frontier.
(8.3) Provide the content of the frontier at the time the search terminates. (10)
Frontier:
1. << A, D, I >, (14) >
2. << A, B >, (15) >
3. << A, D, K , E >, (16)
4. << A, D, K , M, N >, (16)
5. << A, C, G >, (20) >
(8.4) Is the heuristic being applied consistent? Justify your answer by providing proof from (7)
the graph.
No. We can easily show that the heuristic is not consistent by providing a single case
in which it does not satisfy the triangle inequality. ĥ(A) > ĝ(D) + ĥ(D) (it is somehow
cheaper to reach the goal by moving to D from A and then moving onto the goal than it
is estimated in reaching it from A), in other words, the one side of the triangle formed
AH (cost = 10), is longer than the sum of the sides AD + DH (cost = 8), which violates
the inequality.
Question 9: 21 Marks
Beyond classical search: A magic square is a square of n × n cells, and which has some very
interesting properties: all the rows, columns, and diagonals, when summed, adds up to the magic
constant. The magic constant is calculated as follows:
n(n2 + 1)
M=
2
2
Thus, for n = 3, M = 3(32+1) = 15. The entries in the square are limited to the integers between 1
and n2 . For example, a solution for n = 3 is:
14
COS3751/201
8 1 6
3 5 7
4 9 2
It is easy to verify that every row, column, and diagonal in the above example adds up to 15.
(9.2) One way of solving a magic square is to randomly populate the entries, and then swap (2)
out values until a solution is found. Define an appropriate objective function for this
approach.
A simple objective function is simply the number of rows, columns, and diagonals
that sum to 15. The idea is then to favour moves that have higher values for the
objective function – for example if we have a square with an objective function value of
4, chances are that we will only need to make minor adjustments to get to 5,6,7, and
then 8.
We can define some auxiliary functions to help define our objective function: r (x)
which returns 1 if row x is 15, c(y) which returns 1 if row y sums to 15, and d and d 0
for each diagonal. ( P3
1 if j=1 Mxj = 15
r (x) =
0 otherwise
( P3
1 if k=1 Mky = 15
c(y) =
0 otherwise
( P3
1 if j=1 Mjj = 15
d=
0 otherwise
( P3
0 1 if k=1 Mk(4−k) = 15
d =
0 otherwise
Finally we define our objective function:
3
X 3
X
o(M) = r (j) + c(k) + d + d 0 (1)
j=1 k=1
Another possible solution is to get the absolute value of 15 minus the sum of a
row/column/diagonal, and add these together. The objective would then be to min-
imise the objective function.
15
3
X
r (x) =| 15 − Mxj |
j=1
3
X
c(y) =| 15 − Mky |
k=1
3
X
d =| 15 − Mjj |
j=1
3
X
0
d =| 15 − Mk(4−k) |
k=1
(9.3) Differentiate between a standard hill-climb search and a simulated annealing search. (5)
Would simulated annealing fit the approach in the previous question? Explain your
answer. (Hint: think about what simulated annealing is supposed to do with respect to
local/global maxima, also think about shoulders and plateaux.)
A standard hill-climb attempts to find a maximum/minimum by ’climbing/descending
the hill’ (moving in the direction of a bigger/smaller objective function result). It may
get stuck at a local max/min. Simulated annealing has some features that try to ’shake
things up’ (amongst others it also employs the concept of hardening, or annealing) –
in order to avoid getting stuck on plateaux.
Simulated annealing works quite well in this case: a random board is chosen at the
start, and a random successor is generated, if it is better (using the objective function)
it is always accepted, if it is not better, it is accepted with probability e∆E/T : this means
the longer we search, the less happy we are to accept ’poor’ moves (this allows us to
quickly get out of local minima/maxima at the start)..
On a side note: remember that SA is supposed to give you a good chance of getting
out of local minima/maxima: it does not always succeed. The magic square problem
can quite easily be solved using a brute force search.
(9.4) Using the approach suggested above, show the first 5 states in the search as well as (10)
the value of your objective function for each state. (Start off with a randomly populated
magic square, and show 4 subsequent “moves”. In each case, show the value the
objective function would take for each “move”.)
.
9 2 6
3 5 7
1 8 4
16
COS3751/201
Our objective function (the first one in the sample solution) is 2. This is our random
start, so we perform a random swap of the values on the board. Let’s assume a
temperature T = 100.0, and we’ll use a simple linear discounting factor of 0.9 at each
interval. (This doesn’t mean T is linear.)
9 2 4
3 5 7
1 8 6
Here our objective function for the board is 4. This is better than the previous situation
(∆E = 2), so we will definitely use this going forward. T was discounted to 90.0 at the
start of the loop.
9 4 2
3 5 7
1 8 6
T is discounted to 81.0 Our objective function evaluates to 3, so we have ∆E = −1.
At this point we use thus choose this board with probability e∆E/T = e(−1/81.0) = 0.988
(this is an extremely high probability, so for our purposes let’s say we accept the board.
Think about it like this: suppose we create 100 cards, each one with a unique number
from 1 to 100. We then colour 99 of them red, and one of them white. The cards are
placed in a hat, and we randomly draw one: if the card is red, we accept the move, if
it is white, we don’t.).
3 4 2
9 5 7
1 8 6
T is discounted to 72.9. Our objective function evaluates to 2. This is even worse than
before (but we let the annealing process worry about that). ∆E = −1, and we accept
the new board with probability e∆E/T = e(−1/72.9) = 0.986, also very high, and (again for
illustrative purposes) we accept.
The penultimate state provided here does not follow from the one above, but it is done
with a view on the very last state given (particularly to show how the temperature
affects the acceptance of boards).
Assume we arrived at the following during the search
3 4 8
9 5 1
2 6 7
(The objective function for this board evaluates to 6.)
Suppose now the following random board is:
3 4 8
9 5 1
2 6 7
And that T = 1.4 (after about 40 iterations). The objective function for this board
evaluates to 5. That means ∆E = −1, and now we accept this board with probability
e−1/1.4 = 0.508. This is basically a coin flip, and from here on things don’t get better
for these ’worse’ cases.
17
Copyright
2017
c UNISA (v2017.2.1)
18