7

Based on this pseudo code I am trying to implement a java fittness function for the Traveling Salesman problem but I am not sure if I am doing it right, can someone please help me out.

N   The number of cities to visit
T   A tour (list of integers of size N)
D   An N by N matrix containing each d(i,j)

Let s = 0
For i = 1 to (N-1)
Let a = ti
Let b = ti+1
Let s = s + d(a,b)
 End For
     Let end_city = tn
     Let start_city = t1
     Let s = s + d(end_city,start_city)
The tour length s

My attempt at writing this in java

public static ArrayList<Integer> Fitness(){
    int n = 10; // Number of cities to visit
    ArrayList<Integer> t = new ArrayList<Integer>();
    int[][] d = null;
    int s = 0, a, b;

    for (int i = 1; i<n; i++){
        for (int j = 1; j<n; j++){
            d = new int[i][j];
        }

        for( i = 1; i<n; i++){
            a = t.get(i);
            b = t.get(i+1);
            s = s + d[a][b];
        }
        int end_city = t.get(n);
        int start_city = t.get(1);
        s = s + d[end_city][start_city];
    }
    return t;

Can someone please help me out. Thanks.

Mikey
  • 687
  • 4
  • 17
  • note - array indexes in java are 0 based, see [tutorial](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) for details – harshtuna Feb 26 '15 at 01:47

1 Answers1

1

You should start be deciding what you have and what you want.


What you have

For a fitness function for Travelling Salesman Problem, according to your pseudo-code, you will have following input.

  • Number of cities
  • A tour whose fitness is to be calculated
  • Map with distances (in this case adjacency matrix).

This should be the formal parameters of your fitness function.


What you want

The purpose of a fitness function is to have a way to measure quality in terms of a single parameter.

In this case, the length is serving that purpose.

This should be the returning value of your fitness function.


This makes the prototype of your fitness function to be as following

public double fitness(List<Integer> tour,
                      int numberOfCities,
                      double[][] distanceBetween);

Now the pseudo-code for the body is easy to decode if you indent it correctly and have another look.

Let s = 0
For i = 1 to (N-1)
    Let a = ti
    Let b = ti+1
    Let s = s + d(a,b)
End For
Let end_city = tn
Let start_city = t1
Let s = s + d(end_city,start_city)
The tour length s

It should be fairly easy to work out the rest. It is pretty straightforward to translate it to Java.

Good luck.

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
  • you see for this part Let a = ti, Let b = ti+1, Let s = s + d(a,b). Have I done it correctly. Also, I have what variable type does 'Let' respresent? e.g. let a = ti would a be a int? – Mikey Feb 25 '15 at 21:33
  • 1
    Since pseudo-code avoids specifying types as much as possible, you will have to work it out yourself. In this case, all those variables indicate distances. So they would be variables of type `double`. In general, `Let` represents assigning a value to a variable. – Tanmay Patil Feb 25 '15 at 21:35
  • I am getting an error on List it says The type List is not generic; it cannot be parameterized with arguments – Mikey Feb 25 '15 at 21:36
  • 1
    Check if you have imported some `List` class other than `java.util.List`. – Tanmay Patil Feb 25 '15 at 21:37
  • 1
    Thanks, I was missing that import. – Mikey Feb 25 '15 at 21:41
  • Glad to know it helped. – Tanmay Patil Feb 25 '15 at 21:44
  • Btw, what does this line respresent? D An N by N matrix containing each d(i,j) – Mikey Feb 25 '15 at 21:46
  • Also, you see for this part Let a = ti, Let b = ti+1, Let s = s + d(a,b). Have I done it correctly. – Mikey Feb 25 '15 at 21:53
  • Matrix D (N by N) - see [TSP page](http://www.math.uwaterloo.ca/tsp/problem/index.html) - "Given a collection of cities and **the cost of travel between each pair of them**". In the other wording D represents distances between each pair of cities. – harshtuna Feb 26 '15 at 01:43
  • 1
    The matrix is the 2D array `distanceBetween` that I mentioned in the prototype. When you access it's value at two indices corresponding to the two cities between which the distance is to be calculated, it gives you the distance. – Tanmay Patil Feb 26 '15 at 01:52
  • 1
    You have done that part correctly, but you should not create the array in the method. The array should be passed to the method as an argument. – Tanmay Patil Feb 26 '15 at 01:54
  • Hi, sorry for some strange reason it won't let me vote up but I marked it as correct. Thanks for your help. – Mikey Feb 28 '15 at 09:07