Sunteți pe pagina 1din 15

COS3751/201/1/2017

Tutorial Letter 201/1/2017

Techniques of Artificial Intelligence


COS3751

Semester 1

School of Computing

IMPORTANT INFORMATION

This tutorial letter contains model solutions for assignment 01

BAR CODE

university
Define Tomorrow. of south africa
ASSIGNMENT 01
Solution
Total Marks: 150
UNIQUE ASSIGNMENT NUMBER: 798161
Study material: Chapters 1 through 4. You may skip sections 4.2 and 4.5.

Question 1: 13 Marks

(1.1) Explain the difference between a single and multi-agent environment. (6)
An agent solving a problem by itself is a single agent environmentX. The key distinc-
tion is if an agent Xs behavior is best described as maximizing a performance measure
whose value depends on agent Ys behaviorXX. For example in a chess game the
opponent agent X is trying to maximize its performance measure, which by the rules of
chess minimises agent Ys performance measureX. Thus chess is a competitive multi
agent environment. In multiagent environments communication emerges as a rational
behaviorXwhile non-existent in single agent environmentsX. Pg 42-43

(1.2) Explain the difference between a Deterministic and Stochastic environment. (4)
In a deterministic environment the next state is completely Xdetermined by the current
state and the agent’s action X. In a stochastic environment one cannot completely
Xdetermine the next state based solely on the current environment and on the agent’s
actions X.

(1.3) Consider a game of chess. Is this a fully observable, partially observable, or unob- (3)
servable environment? Clearly explain your answer.
Fully observable X. The entire stateXcan be observed at each distinct state in the
state spaceX.

Question 2: 22 Marks

(2.1) List the 5 components that can be used to define a problem. (5)

1. Initial stateX
2. ActionsX
3. Transition modelX
4. Goal testX
5. Path costX

(2.2) Differentiate between search space and goal space. (2)


The search space is the set of states that have to be searched for a solutionX,
whereas a goal space is a set of goal statesX.

2
COS3751/201

(2.3) What is the purpose of the explored set? (1)


Avoids infinite loops since it holds the list of nodes that have already been explored.
X
(2.4) List and discuss three types of queues that may be employed in a search. (6)

1. FIFOX: used in DFS searches, nodes are added in reverse order to ensure that
the last node added will be the first node to be explored.X
2. LIFOX: Typically used in BFS searches: nodes are added in the order they are
generated.X
3. Priority queueX: Nodes are added and sorted based on some key, this ensures
that certain states take priority over others during the expansion phase.X

(2.5) List and explain the measures used to determine problem solving performance. (8)

1. CompletenessX: Will the algorithm find a solution if it exists? X


2. OptimalityX: Will the algorithm find the best solution (optimal path cost among
all solutions)? X
3. Time complexityX: How long does the algorithm take to find a solution? X
4. Space complexityX: How much memory is needed to perform the search for a
solution? X

Question 3: 23 Marks

Three (3) hikers (Andile, Barbara, and Ché) have just descended down a valley to find themselves
confronted by a river they cannot get across. After walking downstream for a while they find two
young boys with a boat and ask them if they would help them get across the river. The boys agree,
but inform the hikers that since their boat is so small, it can only hold only the two boys or one of
the hikers at a time. We can assume that everyone knows how to row the boat.

(3.1) Define a state using a mathematical notation (pictures or any form of graphical notation (7)
will not be accepted). Discuss the appropriateness of your choice, and provide an
example to show that it will be suitable to be employed during a search.
We can abbreviate the hikers with the labels A, B, and C. The boys we could label b1
and b2.
There are many possible answers. We need not distinguish between the boys as it
would not make a difference to the solution, however, set theory dictates that sets do
not contain duplicates, thus we need to properly indicate that the boys are two unique
entities.
State {A, B, C, b1 , b2 }{}(Xfor hikers, Xfor boys – Xif boys are unique) represents the
two sidesXof the river and the people on that sideX. We can use a more compact
representation {A, B, C, b1 , b2 }X, since if a person is not on the one side he/she will
be on the other sideX.

3
(3.2) Define the start and goal states using your representation. (6)
Start state: {A, B, C, b1 , b2 }X2 There are 3 possible goal states! (I show the state
representing the left (beginning) side of the river – the complement is also correct)

1. {} X2

2. {bi } with i ∈ {1, 2} X

3. {b1 , b2 } X

(3.3) Define an appropriate cost function or functions for this problem. (2)
Cost function: A cost function is not relevant hereX, since it would not impact which
solution would be usedX(if more than one exists).

(3.4) Provide a formal definition of a valid action function for this problem – you need not (8)
provide a formal definition for the operation of the function. Discuss the operation of
the function and provide an example to illustrate its use.
Successor: move 1 hikerX, 1 boyX, or 2 boys Xto the other side. Mathematically we
either remove one or two of the existing elements from our state representation when
we move these people to the other side, or we add one or two missing elements to
the set when we move these people back to this sideX. One such function could be:
move(X , Y )Xwith X , Y ∈ {A, B, C, b1 , b2 , X∅X}, X 6= Y X, where the function would
alternately remove or add the function arguments from/to the state set (in the compact
representation).

Question 4: 12 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.X
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.XX

4
COS3751/201

(4.2) How does a Breadth First Search (BFS) differ from the general tree search algorithm? (5)
What is the major reason for implementing the algorithm in this way? Provide an
example to aid your discussion.
The most prominent difference is the goal test. In the general tree search, the goal test
is performed during expansion, and in the BFS the goal test is done during generation.
This means that a goal on level n may be identified while still expanding level n−1. The
obvious benefit here is avoiding an expensive expansion goal test cycle. For example,
consider a binary tree that is 20 levels deep. Depending on the order in which nodes
are expanded, it may mean that a goal on a particular level may only be identified
when all of the nodes on a level has been expanded. If the test is done on generation,
the number of nodes that have to be considered may be significantly less. X5

(4.3) Consider an example of a sliding-block puzzle game state provided below (Figure 1). (4)
How many distinct states are there for puzzles of this sort? 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

Figure 1: Sliding-block puzzle

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.X4
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!.
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!.

Question 5: 6 Marks

Consider the search tree in Figure 2.

(5.1) Show the order in which the nodes will be expanded (from limit 0 to limit 4) given that (6)
IDS is used. Assume the goal node is F , and that nodes are expanded from left to

5
O

M E

D C L B

K I J H F G N A

Figure 2: Search Tree (Iterative Deepening Search (IDS))

right.
So expansion means we apply legal actions to a chosen node – this, by definition,
means that our order look somewhat different from what one may expect.

1. Limit 0: (No expansion – A is generated but not expanded) X

2. Limit 1: O X

3. Limit 2: O M E X

4. Limit 3: O M E D C L X2

5. Limit 4: None, search terminates once L is expanded. X

Question 6: 16 Marks

Consider the graph in Figure 3.

(6.1) Explain the concept of Uniform Cost Search (UCS). Provide an example to aid your (4)
discussion
Uniform cost search is an uninformed search – thus no heuristics!. It calculates the
cost of moving from this state to the next (keeping the cost of reaching the current
state in mind), and chooses the cheapest (as defined in the problem) next state from
the frontierXX.
It helps to think of it as explained in the textbook: it uses an evaluation function (similar
to an informed search such as A∗ ): f̂ (n) = ĝ(n) or just ĝ(n).
exampleXX

6
COS3751/201

11

B 20

15
C

D 4

24
14
22 F

G E

9
12

Figure 3: Search Graph (Uniform Cost Search (UCS))

(6.2) Suppose we start at C and the goal is G. 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.

1. C is placed on the frontier. (Technically we would put (C,C,0) on the frontier –


see the next step for more detail) Frontier is: (C,C,0)X

2. Choose C (and remove) for expansion: generate the path (C,F, 4), (C,A, 20), and
(C,E, 24) (where (C,A,20) means that there is a path from C to A which costs
20), and place on frontier. Frontier is: (C,F,4), (C,A,20), (C,E,24) X

3. Choose (and remove) (C,F,4) (lowest cost, but not goal) – generate (C,F,H,13)
and place on the frontier. Frontier is: (C,F,H,13), (C,A,20), (C,E,24) X

4. Choose (and remove) (C,F,H,13) (lowest cost, but not goal) – generate (C,F,H,G,25)
and place on the frontier. Frontier is: (C,A,20), (C,E,24), (C,F,H,G,25)X

7
5. Choose (and remove) (C,A,20) (lowest cost, but not goal) – generate (C,A,B,31)
and place on the frontier. Frontier is: (C,E,24), (C,F,H,G,25),(C,A,B,31)X

6. Choose (and remove) (C,E,24) (lowest cost, but not goal) – generate (C,E,D,46)
and place on the frontier. Frontier is: (C,F,H,G,25), (C,A,B,31), (C,E,D,46)X

7. Choose (and remove) (C,F,H,G,25) (lower cost, and goal) – search terminates.X

Taking the nodes from the map as they are expanded, you thus have CX,FX,HX,AX,EX.

Question 7: 4 Marks

(7.1) Differentiate between an admissible and a consistent heuristic. (4)


An admissible heuristic does not overestimate the actual cost of getting from a node in
the search space to the goalX2 . A consistent heuristic is more strict than an admissi-
ble one: it obeys the triangle inequality: it is not possible for one side of a triangle to be
longer than the sum of the lengths of the other two sides – thus, the heuristic should
not estimate a higher cost from a node to a goal than a shorter actual path to the goal
from that node.X2

Question 8: 31 Marks

Consider Figure 4 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 ĝ).

Node Estimated distance to N


A 10
B 12
C 4
D 5
E 4
F 7
G 4
H 5
I 4
K 2
L 1
M 1
N 0

Table 1: Distance Table

8
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 4: A∗ Search

(8.1) Supose the start node is A, and the goal node is N. List the nodes in the order in (10)
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
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.

Current frontier (first item is always chosen next):

1. << A, D >, (8) >

2. << A, C >, (12) >

3. << A, B >, (19) >

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:

9
1. << A, D, K >, (9) >

2. << A, D, L >, (10) >

3. << A, C >, (12) >

4. << A, D, I >, (14) >

5. << A, B >, (19) >


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:
1. << A, D, L >, (10) >

2. << A, D, K , M >, (10)

3. << A, C >, (12) >

4. << A, D, I >, (14) >

5. << A, D, K , E >, (16)

6. << A, B >, (19) >


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:
1. << A, D, K , M >, (10)

2. << A, C >, (12) >

3. << A, D, L, N >, (12) >

4. << A, D, I >, (14) >

5. << A, D, K , E >, (16)

6. << A, B >, (19) >


Although we’ve found a path to N, the algorithm first tries to find a shorter route: it
doesn’t exit once it finds a path to N. Now we get to choose the path through M.
Expanded thus far: A,D,K,L,M
Frontier:
1. << A, D, K , M, N >, (10)

2. << A, C >, (12) >

10
COS3751/201

3. << A, D, I >, (14) >

4. << A, D, K , E >, (16)

5. << A, B >, (19) >

Notice that we found a shorter route to N: we thus remove the old route << A, D, L, N >
, (12) > from the frontier.
Choose the node on the frontier with the lowest f̂ value. In this case it is actually our
goal path. However, we have started the process of expansion, although we won’t
apply actions to the selected node.
So our final answer is: AX2 , DX2 , KX2 , LX2 , MX2

(8.2) When adding nodes to the frontier (during the search), is it enough to simply add (4)
nodes from the state space to the frontier? Justify your answer.
Simply adding the nodes in the state space may not convey the needed information
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
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.XXXX

(8.3) Provide the content of the frontier at the time the search terminates. (10)
Frontier:

1. << A, D, K , M, N >, (10)X2

2. << A, C >, (12) >X2

3. << A, D, I >, (14) >X2

4. << A, D, K , E >, (16)X2

5. << A, B >, (19) >X2

(8.4) Is the heuristic being applied consistent? Justify your answer by providing proof from (7)
the graph.
NoX2 . We can easily show that the heuristic is not consistent by providing a single
case in which it does not satisfy the triangle inequalityX2 . ĥ(A) > ĝ(D) + ĥ(D)X3 (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.

11
Question 9: 23 Marks

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:

8 1 6

3 5 7

4 9 2

Figure 5: Magic Square for n = 3

It is easy to verify that every row, column, and diagonal in the above example adds up to 15.

(9.1) Differentiate between a global and local maxima/minima. (4)


Local minima/maxima are local solutions in the search space that are optimal solutions
to the problemX2 . A global maxima/minima is a solution to the problem that is the best
solution in the solution space (no other solution beats it).X2

(9.2) One way of solving a magic square is to randomly populate the entries, and then (4)
swap values until a solution is found. Define an appropriate objective function for this
approach. Explain its function and provide and example to illustrate its use.
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

12
COS3751/201

( P3
1 if k=1 Mk(4−k) = 15
d0 =
0 otherwise
Finally we define our objective function:
3 3
c(k) + d + d 0 X4
X X
o(M) = r (j) + (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.
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
d 0 =| 15 − Mk (4−k) |
k=1

Finally we define our objective function:


3
X 3
X
o(M) = r (j) + c(k) + d + d 0 (2)
j=1 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)X2 . 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 plateauxX2 .
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).X.
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.

13
(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

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.X2 (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.X2

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 boardX2 .
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.X2
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

14
COS3751/201

3 4 8
9 5 1
2 6 7

(The objective function for this board evaluates to 6.)X2


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.

Copyright UNISA
c 2017 (v2017.1.3)

15

S-ar putea să vă placă și