Summary | The program will implement a simple genetic algorithm (GA) to solve instances of the Traveling Salesman Problem (TSP). |
Topics | Intermediate object-orientation; responsibility driven design; unit testing; principles of genetic algorithms |
Audience | CS1 or CS2 |
Difficulty | Intermediate. Typically takes 1 to 1.5 weeks. (Depends on how much code students are given a priori and how much they are expected to write on their own.) |
Strengths |
|
Weaknesses | How much background code to give depends on the prior preparation of the students and the focus of the class. On occasion, some students have felt that I give them too much help code thereby making the assignment less challenging. But one of the points of this exercise is to read and understand another persons code and then to extend it. |
Dependencies | Requires a brief discussion of the general idea GAs and how they
work. I hand out a chapter on genetic algorithms from
A.K. Dewdney's |
Variants |
|
File name Given Code My Solution
LOC SLOC LOC SLOC City.java 27 12 38 27 GenerateCities.java 57 34 57 34 Map.java 59 39 44 29 SplitInterval.java 62 36 62 36 TSP.java 126 77 126 77 Tour.java 180 120 204 155 Util.java 29 12 33 20 UtilTest.java 0 0 37 26 Total 540 330 639 426
LOC: Lines of code (source + comments + blank lines)
SLOC: Source Lines of Code
The files marked in purple don't
need to be changed at all. The files marked with green will need modifications. In your
final version of your code you will want to use the TSP.java code I've provided as is. But
during the process of developing your solution you may want to edit TSP.java
to experiment with your evolving code.
Util.java
write the methods for:
rndInt(int i, int j)
i <= r < j
count(int[] a, int key, int start, int end)
a[start..end-1]
i.e., upto end but not including it. This
method will be used in Tour.java
when writing the
crossover
method. It will be used to ensure that the tours we get from
the cross-over process are indeed valid tours.
UtilTest.java
to develop a suite of units tests for
both these methods
City.java
complete the method distance(City)
which calculates the
distance between two cities
City.java
move the definition of
display(Graphics,int)
from Map.java
to
City.java
Tour.java
write the method distance to find out the distance of an
entire tour
crossover(Tour, int, int)
Tour crossOver(Tour p2, int x1, int x2)
this --------------------------------- ^ ^ x1 x2 p2 +++++++++++++++++++++++++++++++++ ^ ^ x1 x2 c1 ------+++++++++++++-------------- | | | | c2 ++++++-------------++++++++++++++At a high level the array copying takes place in three stages:
c1[ 0 .. x1-1 ] = this[ 0 .. x1-1 ] c1[ x1 .. x2 ] = p2 [ x1 .. x2 ] c1[ x2+1 .. lenth-1 ] = this[ x2+1 .. length-1 ]The potential problem is that after we copy the array segments we may have an invalid tour with duplicate cities as shown below:
this a b c d e f g h i ^^^^^ p2 d h i c b e a g f ^^^^^ c1 a b c c b e g h i ^^^^^We can resolve this by: (here is where you will need to use
count
)
uniq: d f
e.g., replace 'b' with 'd' 'c' with 'f'giving the new child:
a d f c b e g h i
Extra info about this assignment: