Anda di halaman 1dari 9

COS3751/201/1/2011

School of Computing

Techniques of Artificial Intelligence


COS3751

TUTORIAL LETTER 201/2011


First semester
COS3751/201/1/2011

Afrikaanse studente:

Die studiebrief vir COS3751 is slegs in Engels beskikbaar. Indien u enige probleme ondervind met die
Engelse terminologie of verduidelikings is u welkom om die dosente te skakel.

Table of Contents

1. Solutions to Assignment 1 Semester 1 .......................................................................... 3

Important Notice
Assignment 1 is NOT AN OPTIONAL assignment. Should you not submit Assignment 1 by its due date
you will not be allowed to write examination for this module.

2
COS3751/201/1/2011

1. Solutions to Assignment 1 Semester 1

Question 1

a. We’ll define the coordinate system so that the centre of the maze is at (0, 0), and the maze
itself is a square from (−1,−1) to (1, 1).

Initial state:
robot at coordinate (0, 0), facing north.
Goal test:
either |x| > 1 or |y| > 1 where (x, y) is the current location.
Successor function:
move forwards any distance d; change direction robot is facing.
Cost function:
total distance moved.

The state space is infinitely large, since the robot’s position is continuous.

b. The state will record the intersection the robot is currently at, along with the direction it’s
facing. At the end of each corridor leaving the maze we will have an exit node. We’ll assume
some node corresponds to the centre of the maze.

Initial state:
at the centre of the maze facing north.
Goal test:
at an exit node.
Successor function:
move to the next intersection in front of us, if there is one; turn to face a new
direction.
Cost function:
total distance moved.

There are 4n states, where n is the number of intersections.

c. Initial state:
at the centre of the maze.
Goal test:
at an exit node.
Successor function:
move to next intersection to the north, south, east, or west.
Cost function:
total distance moved.

We no longer need to keep track of the robot’s orientation since it is irrelevant to predicting
the outcome of our actions, and not part of the goal test. The motor system that executes this
plan will need to keep track of the robot’s current orientation, to know when to rotate the
robot.

d. State abstractions:
(i) Ignoring the height of the robot off the ground, whether it is tilted off the vertical.
(ii) The robot can face in only four directions.
(iii) Other parts of the world ignored: possibility of other robots in the maze, the weather in the
Caribbean.

Action abstractions:
(i) We assumed all positions were safely accessible: the robot couldn’t get stuck or damaged.
(ii) The robot can move as far as it wants, without having to recharge its batteries.

3
COS3751/201/1/2011

(iii) Simplified movement system: moving forwards some distance, rather than controlling
each individual motor and watching the sensors to detect collisions.

Question 2

a) Here is one possible representation: A state is a three-tuple giving the number of missionaries
and cannibals on the start side of the river, and an indication of which side of the river the
boat is (N for the north side, and S for the south side). Assume that the start side is the north
bank of the river (N). A state description has the following form:

(number of missionaries on N, number of cannibals on N, location of the boat)

Note that we only consider the state on the north bank of the river. If we do this, we implicitly
also know the state on the south bank.
Our start state is: (3,3,N). This means we have 3 missionaries, 3 cannibals and the boat on
the north bank of the river. Our goal state is (0,0,S). We reach the goal when we have 3
missionaries, 3 cannibals and the boat on the south bank (hence, 0 missionaries and 0
cannibals on the north side).
The cost function is one per action, and the successors of a state are all the states that move
1 or 2 people and the boat from one side to the other. The diagram of the state space is given
as follows:

(3,3,N)
(2,2,S)
(3,1,S)
(3,2,S)
(3,2,N)

(3,0,S)
(3,1,N)

(1,1,S)
(2,2,N)
(0,2,S)
(0,3,N)
(0,1,S)
(0,2,N) (1,1,N)

(0,0,S)

b) The search space is small, so any optimal algorithm works. We give a depth-first search
implementation below.
c) It is not obvious that almost all moves are either illegal or revert to the previous state. There is
a feeling of a large branching factor, and no clear way to proceed initially.

4
COS3751/201/1/2011

#include <iostream>
#include <vector>
#include <list>
#include <stack>

using namespace std;


enum side {north, south};

/* 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
};

/* Global constants and variables:


GOAL: final state
START: initial state
closed: states that have already been encountered during the search are
added to the closed list to prevent cycles and unnecessary dead ends */

const state GOAL(0,0,south);


const state START(3,3,north);

list<state> closed;

/* sout << s:
overloaded operator << to write state s to the output stream sout */

ostream& operator<<(ostream& sout, state s) {


if (s.bank == north)
sout << " boat, " << s.mis << " missionaries and " << s.can
<< " cannibals on north bank";
else
sout << " boat, " << 3-s.mis << " missionaries and " << 3-s.can
<< " cannibals on south bank";
return sout;
}

/* safe(s): returns true if s is a safe state, i.e. if the cannibals


do not outnumber the missionaries */

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 */

void generate(list<state>& children, state s) {


state new_state;
if (s.bank == north) {
for (int i=0; i<=s.mis; i++)
for (int j=0; j<=s.can; j++)

5
COS3751/201/1/2011

if (i+j > 0 && i+j < 3) {


new_state = state(s.mis-i, s.can-j, south);
if (safe(new_state))
children.push_back(new_state);
}
}
else {
for (int k=0; k<=(3-s.mis); k++)
for (int m=0; m<=(3-s.can); m++)
if (k+m > 0 && k+m < 3) {
new_state = state(s.mis+k, s.can+m, north);
if (safe(new_state))
children.push_back(new_state);
}
}
}

/* found(s): returns true if s is in the global closed list */

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. */

bool depthsearch(list<state>& path, state current_state) {


list<state> children;
list<state>::iterator p;

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;
}

6
COS3751/201/1/2011

Question 3

The formulation puts one queen per column, with a new queen placed only in a square that is not
attacked by any other queen. To simplify matters, we’ll first consider the n –rooks problem (in the
game of chess, a rook is a piece that can move along any number of squares in straight lines parallel
to the sides of the board). The first rook can be placed in any square in column 1 ( n choices), the
second in any square in column 2 except the same row as the rook in column 1 ( n − 1 choices), and
so on. This gives n! elements of the search space.

For n queens, notice that a queen attacks at most three squares in any given column, so in column 2
there are at least ( n − 3) choices, in column 3 at least ( n − 6) choices, and so on. Thus the state
space size is S ≥ n ⋅ (n − 3) ⋅ (n − 6)  . Hence we have
S 3
≥ n ⋅ n ⋅ n ⋅ (n − 3) ⋅ (n − 3) ⋅ (n − 3) ⋅ (n − 6) ⋅ (n − 6) ⋅ (n − 6)
≥ n ⋅ (n − 1) ⋅ (n − 2) ⋅ (n − 3) ⋅ (n − 4) ⋅ (n − 5) ⋅ (n − 6) ⋅ (n − 7) ⋅ (n − 8)
≥ n!
or S ≥ 3 n!

Question 4

a. Local beam search with k = 1 is hill-climbing search.


b. Local beam search with one initial state and no limit on the number of states retained,
resembles breadth-first search in that it adds one complete layer of nodes before adding the
next layer. Starting from one state, the algorithm would be essentially identical to breadth-first
search except that each layer is generated all at once.
c. Simulated annealing with T = 0 at all times: ignoring the fact that the termination step would
be triggered immediately, the search would be identical to first-choice hill climbing because
every downward successor would be rejected with probability 1.
d. Simulated annealing with T = ∞ at all times is a random-walk search: it always accepts a new
state.
e. Genetic algorithm with population size N = 1: if the population size is 1, then the two selected
parents will be the same individual; crossover yields an exact copy of the individual; then
there is a small chance of mutation. Thus, the algorithm executes a random walk in the space
of individuals.

Question 5

a) There are many possible state descriptions. Some possibilities are:


- A string a 16 digits is a very compact representation. Digits represent cells from left to
right and top to bottom. Digit “0” would represent an empty cell. The initial state using this
description would be 2100400000040010.
- A graphic representation using the Sudoku grid as given is much easier for humans to
understand. The initial state using this description would be shown as:
1 2 3 4

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

7
COS3751/201/1/2011

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 , 2140400000040010>}.

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 (diagram on next page). Both the graphical and 16-digit
representation described above are shown.

c) Breadth-first and depth-first searches should be applied to the diagram (tree) representing the
state space of the Sudoku problem as provided in the next page. In this tree each node
represents a different state, and we have labeled the nodes with numbers from 1 to 15. The
root node is labeled 1 and represents the initial state, and the node labeled 15 represents the
goal state.
The search methods are used to explore the nodes of the tree, starting from the root node (1),
and following the order that is appropriate to each search method, until the goal node (15) is
encountered. Note that these search methods are not meant to be applied to explore the cells
(squares) of a Sudoku grid!
The order in which the nodes are explored is given as follows:

Breadth-first search: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15.


Depth-first search: 1,2,3,4,5,6,7,8,10,12,13,14, 15.

8
COS3751/201/1/2011

1 2 1
4
4
1 2100400000040010

2 2 1 3 2 1 4 3
4 4
2130400000040010 4 4 2140400000040010
1 1

2 1 4 3 4
4
4 2143400000040010
1

2 1 4 3 5
4 3
4
2143430000040010
1

2 1 4 3 6
4 3 2
4 2143432000040010
1

2 1 4 3 7
4 3 2 1
4 2143432100040010
1

8 2 1 4 3 2 1 4 3 9
4 3 2 1 4 3 2 1
1 4 3 4
2143432110040010 1 1 2143432130040010

10 2 1 4 3 2 1 4 3 11
4 3 2 1 4 3 2 1
1 2 4 3 2 4
2143432112040010 1 1 2143432132040010

12 2 1 4 3
4 3 2 1
1 2 3 4
2143432112340010 1

13 2 1 4 3
4 3 2 1
1 2 3 4
2143432112343010
3 1

14 2 1 4 3
4 3 2 1
1 2 3 4
2143432112343410
3 4 1

15 2 1 4 3
4 3 2 1
2143432112343412 1 2 3 4
3 4 1 2

©
UNISA 2011

Anda mungkin juga menyukai