# Artificial Intelligence Assignment 1

Artificial Intelligence
Assignment 1
The University of AdelaideSchool of Computer Science
Semester 1 2020
Due 11:59pm Monday 30 March 2020
1 Pathfinding
Pathfinding is the problem of finding a path between two points on a plane. It is a
fundamental task in robotics and AI. Perhaps the most obvious usage of pathfinding is
in computer games, when an object is instructed to move from its current position to a
goal position, while avoiding obstacles (e.g., walls, enemy fire) along the way.
Pathfinding in commercial games is frequently accomplished using search algorithms1
We consider a simplified version in this assignment. The following shows a 2D map
drawn using ASCII characters:
1 1 1 1 1 1 4 7 8 X
1 1 1 1 1 1 1 5 8 8
1 1 1 1 1 1 1 4 6 7
1 1 1 1 1 X 1 1 3 6
1 1 1 1 1 X 1 1 1 1
1 1 1 1 1 1 1 1 1 1
6 1 1 1 1 X 1 1 1 1
7 7 1 X X X 1 1 1 1
8 8 1 1 1 1 1 1 1 1
X 8 7 1 1 1 1 1 1 1
Given a start position and an end position on the map, our aim is to find a path from the
start position to the end position. The character ‘X’ denotes an obstacle that cannot be
traversed by a path, while the digits represent the elevation at the respective positions.
Any position is indicated by the coordinates (i, j), where i is the row number (ordered
top to bottom) and j is the column number (ordered left to right). For example, the
1http://theory.stanford.edu/~amitp/GameProgramming/
Semester 1 2020 Page 1 by Tat-Jun Chin
top left position is (1, 1), the bottom right is (10, 10), while the position with elevation
‘3’ is (4, 9). Given start position (1, 1) and end position (10, 10), a possible path is
* * * 1 1 1 4 7 8 X
1 1 * 1 1 1 1 5 8 8
1 1 * * * * * * * 7
1 1 1 1 1 X 1 1 * 6
1 1 1 1 1 X 1 * * 1
1 1 1 1 1 1 1 * 1 1
6 1 1 1 1 X 1 * * *
7 7 1 X X X 1 1 1 *
8 8 1 1 1 1 1 1 1 *
X 8 7 1 1 1 1 1 1 *
Note that we use 4-connectedness for paths, which means any two points on the path
are only connected if they are vertically or horizontally (but not diagonally!) adjacent.
1.1 Problem formulation
Following the lecture notes, we formulate the problem as follows:
• States: Any obstacle-free position (i, j) on the map.
• Initial state: A position (i0, j0) given by the user.
• Actions: Since we consider 4-connectedness, only four actions are available: Up,
down, left and right (your program must expand each node in this order).
Available actions for positions adjacent to the map boundary or obstacles are
reduced accordingly.
• Transition model: Moving left moves the current position to the left, etc.
• Goal test: Check if the current state is the end position (i∗, j∗) given by the user.
• Path cost: Given a map M and a path P = {(i0, j0),(i1, j1), . . . ,(iN , jN )}, the
cost of the path is calculated as
g(P) = X
N
k=1
c(ikk 1, jkk 1, ik, jk, M),
Semester 1 2020 Page 2 by Tat-Jun Chin
where
c(a, b, c, d, M) = (
1 + M(c, d) ) M(a, b) if M(c, d) ) M(a, b) > 0
1 otherwise
and M(a, b) is the elevation at position (a, b). In words, the cost of a path is the
sum of the costs between two adjacent points of the path, and the cost between
adjacent points is 1 plus the difference between the elevation of the two points if
we climb “uphill”, or simply 1 if we stay “level” or slide “downhill”.
This means shorter paths which avoid climbing cost less. As an example, the cost
in the path in the previous page is 25. What is the optimal (cheapest) path?
Solve pathfinding using Breadth-First Search (BFS), Uniform-Cost Search (UCS) and
A* Search. You should base your program on the pseudocode GRAPH-SEARCH in the
lecture slides, and carefully think about the appropriate data structures to use. For A*
Search, you must implement two heuristics:
• Euclidean distance between current position and end position.
• Manhattan distance between current position and end position.
For the map in Page 1 with start position (1, 1) and end position (10, 10), your program
1. Are the paths returned by the three methods different?
2. What about the optimality of the returned paths?
3. Which method is the most computationally and memory efficient?
4. Do the two heuristics for A* Search provide different solutions?
5. Does checking for repeated states matter in this problem?
Semester 1 2020 Page 3 by Tat-Jun Chin
1.3 Deliverables
Write your pathfinding program in C/C++, Java or Python.
In the case of C/C++, you must supply a makefile (named exactly as ‘Makefile’)
with a rule called pathfinder to compile your program into a Linux executable binary
named pathfinder.bin. Your program must be able to be compiled and run as follows:
\$ make pathfinder
\$ ./pathfinder.bin [map] [algorithm] [heuristic]
In the case of Java, write your program in the file pathfinder.java. Your program
must be able to be compiled and run as follows:
\$ javac pathfinder.java
\$ java pathfinder [map] [algorithm] [heuristic]
In the case of Python, write you program in the file pathfinder.py. Your program
must be able to be run as follows:
\$ python pathfinder.py [map] [algorithm] [heuristic]
The marking program will decide which of the above to invoke using the following
structure:
if Makefile exists then
Compile and run C/C++ submission.
else if pathfinder.java exists then
Compile and run Java submission.
else
Run Python submission.
end if
The inputs/options to the program are as follows.
• [map] specifies the path to map, which is a text file formatted according to this
example (see next page):
Semester 1 2020 Page 4 by Tat-Jun Chin
10 10
1 1
10 10
1 1 1 1 1 1 4 7 8 X
1 1 1 1 1 1 1 5 8 8
1 1 1 1 1 1 1 4 6 7
1 1 1 1 1 X 1 1 3 6
1 1 1 1 1 X 1 1 1 1
1 1 1 1 1 1 1 1 1 1
6 1 1 1 1 X 1 1 1 1
7 7 1 X X X 1 1 1 1
8 8 1 1 1 1 1 1 1 1
X 8 7 1 1 1 1 1 1 1
The first line indicates the size of the map (rows by columns), while the second
and third line represent the start and end positions respectively. The map data
then follows, where all elevation values are integers from 0 to 9 inclusive.
• [algorithm] specifies the search algorithm to use, with the possible values of bfs,
ucs, and astar.
• [heuristic] specifies the heuristic to use for A* search, with the possible values
of euclidean and manhattan. This input is ignored for BFS and UCS.
Your program must then print to standard output the path returned by the
search algorithm, in the following format:
* * * 1 1 1 4 7 8 X
1 1 * 1 1 1 1 5 8 8
1 1 * * * * * * * 7
1 1 1 1 1 X 1 1 * 6
1 1 1 1 1 X 1 * * 1
1 1 1 1 1 1 1 * 1 1
6 1 1 1 1 X 1 * * *
7 7 1 X X X 1 1 1 *
8 8 1 1 1 1 1 1 1 *
X 8 7 1 1 1 1 1 1 *
where the path is indicated by asterisks ‘*’ superimposed on the original map beginning
from the start position and leading to the end position. Do not include extraneous
spaces or other characters in the output.
Semester 1 2020 Page 5 by Tat-Jun Chin
If the given map or problem do not have a feasible path, your program must print
null
with no extraneous spaces or characters.
1.4 Submission
You must submit your program files on the Computer Science Web Submission System.
This means you must create the assignment under your own SVN repository to store
the submission files. The SVN key for this submission is
2020/s1/ai/Assignment1
The link to the Web Submission System used for this assignment is
For more details on the online submission procedures including SVN, visit the home
page of the school and look under “Information for Current Students”.
1.5 Assessment
I will compile and run your code on several test problems. If it passes all tests you will
There will be no further manual inspection/grading of your program to award marks
on the basis of coding style, commenting or “amount of code written.
1.6 Using other source code
You may not use other source code for this assignment. You should personally and
carefully implement the search algorithms to fully understand the concept.
1.7 Due date and late submission policy
This assignment is due by 11:59pm Monday 30 March 2020. If your submission is late
the maximum mark you can obtain will be reduced by 25% per day (or part thereof)
past the due date or any extension you are granted.
Continues next page for postgraduate section.
Semester 1 2020 Page 6 by Tat-Jun Chin
2 Pathfinding by direct optimisation
For postgraduate students, completing this section successfully will give you the remain￾ing 3% of the marks.
Here we shall attempt to directly optimise the path instead of step-by-step search￾ing. We consider the simulated annealing algorithm shown in Algorithm 1. For more
background on simulated annealing, see Section 4.1 of Russell and Norvig (3rd ed.).
Algorithm 1 Simulated annealing for path optimisation
1: input Initial path P
0
, initial temperature Tini, final temperature Tf in, cooling rate
α, segment length d.
2: output Optimised path P.
The algorithm receives as input a feasible (but non-optimal) path joining a start
position and an end position on a map. The core idea is to iteratively perform random
local adjustments to the path, and accept the new path if the adjustments improve the
path cost (defined in Sec. 1.1), or accept it probabilistically if the cost is not improved.
The process is repeated until the annealing temperature T falls below a small value
Tf in given by the user. The temperature reduction is conducted as T = αT , where
0 < α < 1 is the cooling rate (also supplied by the user). See Section 4.1 of Russell and
Norvig (3rd ed.) for more details.
The main body of the algorithm is conceptually simple — the hardest part is the rou￾tine to perform the random adjustments. Fortunately we can rely on the BFS program
written in the previous section. The method is shown in Algorithm 2.
Note that the adjustments cannot make the path infeasible, i.e., any resulting path
still joins the original start position and end position required by the user.
Semester 1 2020 Page 7 by Tat-Jun Chin
Algorithm 2 Make random local adjustment on path
1: input Path P, segment length d.School of Computer Science