Professional Documents
Culture Documents
202 2007 0 B PDF
202 2007 0 B PDF
School of Computing
Discussion of Assignment 2
COS351D/202/2007
Table of Contents
Chapter 1
Read as background.
Chapter 2
Familiarise yourself with the concepts and terminology.
Chapter 3
Study everything. You have to understand all the algorithms and be able to apply them to a given
problem statement. You do not need to memorise any pseudo-code.
Chapter 4
Study everything except the following sections, which you can omit entirely:
- Memory-bounded heuristic search, pp. 101-104.
- 4.4 Local Search in Continuous Spaces, pp. 119-129.
You have to understand all the algorithms and be able to apply them to a given problem statement.
You do not need to memorise any pseudo-code.
Chapter 5
Study everything except the following section, which you can omit entirely:
- 5.4 The Structure of Problems, pp. 151 – 155.
You have to understand all the algorithms and be able to apply them to a given problem statement.
You do not need to memorise any pseudo-code.
Chapter 6
Study sections 6.1, 6.2 and 6.3. You have to understand the minimax and alpha-beta algorithms, and
be able to apply them to a given problem statement.
Read sections 6.4, 6.5, 6.6, 6.7 and 6.8. You have to understand the concepts covered in these
sections, but you do not need to be familiar with any particular game such as backgammon, chess or
any card game.
3
COS351D/202/2007
Solution: Assignment 2
The following assignment questions were marked: 4, 5, 8 and 9. Please compare your answers to all
the questions to the model solutions.
Answer 1
a) It suffices to show that for all possible actual environments, this agent cleans the squares at
least as fast as any other agent. This is trivially true when there is no dirt. When there is dirt in
the initial location and none in the other, the world is clean after one step; no agent can do
better. When there is no dirt in the initial location and dirt in the other, the world is clean after
two steps; no agent can do better. When there is dirt in both locations, the world is clean after
three steps. Again, no agent can do better.
b) The agent in (a) keeps on moving backwards and forwards even after the world is clean. It is
better to do NoOp once the world is clean. Now, since the agent’s percept doesn’t tell
whether the other square is clean, it must seem as if the agent must have some memory to
say if the other square has already been cleaned. To make this argument rigorous is difficult
– for example, could the agent arrange things so that it would only be in a clean left square
when the right square was already clean? As a general strategy the agent can use the
environment itself as a kind of memory – humans do this all the time with things such as
paper calendars to remember appointments. In this particular case, however, this is not
possible. Consider the reflex actions for [A, Clean] and [B, Clean]. If either of these is NoOp,
then the agent will fail in the case where this is the initial percept but the other square is dirty;
hence neither can be NoOp and therefore the simple reflex agent is doomed to keep moving.
In general, the problem with reflex agents is that they have to do the same thing in situations
that look the same, even when the situations are actually different. In the vacuum world this is
a big liability, because every interior square (except home) looks either like a square with dirt
or a square without dirt.
c) If we consider asymptotically long lifetimes, then it is clear that learning a map (in some form)
confers an advantage because it means that the agent can avoid bumping into walls. It can
also learn where dirt is most likely to accumulate and can devise an optimal inspection
strategy. The precise details of the exploration method needed to construct a complete map
appear in Chapter 4; methods for devising an optimal strategy are in Chapter 21.
Answer 2
Robot Soccer: A model-based reflex agent would suffice for most aspects: for tactical play, a
utility-based agent with lookahead would be useful.
Internet book-shopping: A goal-based agent would be appropriate for specific book requests.
For more open-ended tasks, such as “Find me something interesting to read”, tradeoffs are
involved and the agent must compare utilities for various possible books.
4
COS351D/202/2007
Autonomous mars-rover: A model-based reflex agent would be sufficient for low-level navigation
and obstacle avoidance. For route planning, exploration planning, experimentation, etc. some
combination of goal-based and utility-based agents would be needed.
Mathematician’s assistant: For specific proof-tasks goal-based agents are needed. For
exploratory tasks, such as “Prove something useful concerning operations on strings”, a
utility-based architecture might be needed.
Answer 3
a)
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
b) Breadth-first: 1 2 3 4 5 6 7 8 9 10 11
Depth-limited: 1 2 4 8 9 5 10 11
Iterative-deepening: 1; 1 2 3; 1 2 4 5 3 6 7; 1 2 4 8 9 5 10 11
c) Bi-directional search is very useful, because the only successor of n in the reverse direction
is ⎣(n / 2) ⎦ . This helps focus the search.
e) Yes, start at the goal and apply the single reverse successor action until you reach 1.
Answer 4
#include <iostream>
#include <vector>
#include <list>
#include <stack>
5
COS351D/202/2007
/* class state:
State representation for the missionaries and cannibals problem. */
class state {
public:
state(int x=0, int y=0, side z=south):
mis(x), can(y), bank(z) {;}
bool operator==(const state& s)
{return (mis == s.mis && can == s.can && bank == s.bank);}
int mis; // number of missionaries on the north bank
int can; // number of cannibals on the north bank
side bank; // which side of the river is the boat
};
list<state> closed;
/* sout << s:
overloaded operator << to write state s to the output stream sout */
bool safe(state s) {
return (s.mis == 0 || s.mis == 3 || s.mis == s.can);
}
/* generate(children, s):
generates all the successor states of s and returns it in children */
6
COS351D/202/2007
bool found(state s) {
for (list<state>::iterator p = closed.begin(); p != closed.end(); p++)
if ((*p) == s)
return true;
return false;
}
/* depthsearch(path, current_state):
recursive depth-first search procedure starting from current_state.
Returns true if a path from current_state to GOAL is found. The
answer path, if found, is returned in path. */
if (current_state == GOAL)
return true;
closed.push_front(current_state);
generate(children, current_state);
for (p = children.begin(); p != children.end(); p++)
if (!(found(*p)))
if (depthsearch(path, *p)) {
path.push_front(*p);
return true;
}
return false;
}
int main() {
list<state> answerpath;
if (depthsearch(answerpath, START)) {
answerpath.push_front(START);
for (list<state>::iterator p = answerpath.begin();
p != answerpath.end(); p++)
cout << *p << endl;
}
else
cout << "No result" << endl;
return 0;
}
Answer 5
Consider a state space in which every state has a single successor, and there is a single goal at
depth n. Depth-first search will then find the goal in n steps, whereas iterative-deepening search will
2
take 1 + 2 + 3 + ... + n = O ( n ) steps.
7
COS351D/202/2007
Answer 6
As an ordinary person browsing the web we can only generate the successors of a page by visiting
the page and following the links. We can then do a breadth-first search, or perhaps a best-first search
where the heuristic is some function of the number of words in common between the start and goal
pages; this may help keep the links on target. Search engines keep the complete graph of the web,
and may provide the user access to all (or at least some) of the pages that link to a page. This would
allow us to do bi-directional search.
Congratulations to those of you who managed to implement this program! We do not provide an
implementation here, and those who didn’t manage, were not penalized for it.
Answer 7
- L[0+244=244]
- M[70+241=311], T[111+329=440]
- L[140+244=384], D[145+242=387], T[111+329=440]
- D[145+242=387], T[111+329=440], M[210+241=451], T[251+329=580]
- C[265+160=425], T[111+329=440], M[210+241=451], M[220+241=461], T[251+329=580]
- T[111+329=440], M[210+241=451], M[220+241=461], P[403+100=503], T[251+329=580],
R[411+193=604], D[385+242=627]
- M[210+241=451], M[220+241=461], L[222+244=466], P[403+100=503], T[251+329=580],
A[229+366=595], R[411+193=604], D[385+242=627]
- M[220+241=461], L[222+244=466], P[403+100=503], L[280+244=524], D[285+242=527],
T[251+329=580], A[229+366=595], R[411+193=604], D[385+242=627]
- L[222+244=466], P[403+100=503], L[280+244=524], D[285+242=527], L[290+244=534],
D[295+242=537], T[251+329=580], A[229+366=595], R[411+193=604], D[385+242=627]
- P[403+100=503], L[280+244=524], D[285+242=527], M[292+241=533], L[290+244=534],
D[295+242=537], T[251+329=580], A[229+366=595], R[411+193=604], D[385+242=627],
T[333+329=662]
- B[504+0=504], L[280+244=524], D[285+242=527], M[292+241=533], L[290+244=534],
D[295+242=537], T[251+329=580], A[229+366=595], R[411+193=604], D[385+242=627],
T[333+329=662], R[500+193=693], C[541+160=701]
Answer 8
- w = 0 gives f (n) = 2 g (n) . This behaves exactly like uniform-cost search – the factor of 2
makes no difference in the ordering of the nodes.
- w = 1 gives A* search.
- w = 2 gives f (n) = 2h(n) which is greedy best-first search.
- We also have
⎡ w ⎤
f (n) = (2 − w) ⎢ g (n) + h( n) ⎥
⎣ 2−w ⎦
which behaves exactly like A* search with a heuristic
w
h( n) .
2−w
For w ≤ 1 , this is always less than h(n) and hence admissible, provided that h(n) is itself
admissible.
8
COS351D/202/2007
Answer 9
1 2 1
2 4
3 4
4 1
The only legal action is assigning a value from the set {1, 2, 3, 4} to a cell. Cell value can be
represented by variables, which can be defined as C ij , where i ∈ {1,2,3,4} represents the
four rows, and j ∈ {1,2,3,4} represents the four columns. For example, cell C12 = 1 is the
cell in column 2 and row 1 and has the value 1.
A successor function would take the current state as input, and return a state where the next
unassigned variable is given the first available valid value from the set {1,2,3,4}. For example,
the initial state is 2100400000040010. The variables C11 = 2 and C12 = 1 are fixed. The first
unassigned variable is C13 . The values 1 and 2 violate the rules of Sudoku since these values
are already in the first row. The values 3 and 4 do not violate the rules (yet!). We can
therefore assign C13 = 3 as the first successor of the initial state, to produce the state
2130400000040010, and assign C13 = 4 to produce the successor 2140400000040010. The
successor function for the 2x2 Sudoku problem, with input 2100400000040010 will produce
{< C13 = 3 , 2130400000040010>, < C13 = 4 , 2100400000040010>}.
The goal test is fairly straightforward. A goal state will comply with the rules of Sudoku, i.e.
every row, every column, and every one of the four 2 x 2 blocks contains each of the digits 1,
2, 3 and 4. If we assume that only legal states will be generated by the successor function,
the simplest goal test will be a state where all the variables are assigned. A complete goal
test will apply the rules of Sudoku.
b) If we assign empty cells using a row-first approach and using the lowest legal value we get
the following state space. Both the graphical and 16-digit representation described above are
shown.
9
COS351D/202/2007
1 2 1
4
4
1 2100400000040010
2 2 1 3 2 1 4 3
4 4
4 4
2130400000040010 1 1 2140040000040010
2 1 4 3 4
4
4
1 2143400000040010
2 1 4 3 5
4 3
4
1 2143430000040010
2 1 4 3 6
4 3 2
4
1 2143432000040010
2 1 4 3 7
4 3 2 1
4
1 2143432100040010
82 1 4 3 2 1 4 3 9
4 3 2 1 4 3 2 1
1 4 3 4
2143432110040010 1 1 2143432130040010
2 1 4 3
10
4 3 2 1
1 2 4
2143432112040010 1
2 1 4 3
11
4 3 2 1
1 2 3 4
2143432112340010 1
2 1 4 3
12
4 3 2 1
1 2 3 4
2143432112343010 3 1
2
13 1 4 3
4 3 2 1
1 2 3 4
2143432112343410 3 4 1
2
14 1 4 3
4 3 2 1
1 2 3 4
2143432112343412 3 4 1 2
10