0

I'm writing a 3-opt algorithm for the travelling salesman problem, i already got 2-opt working and im trying to convert it to 3-opt. I don't get how to swap the 3 points, can anyone help me?

my code:

private void ThreeOptSwap(int i, int k, int l) {
    int size = route.size();
    new_Route = new ArrayList<Point>();
    new_Route.addAll(route);
    // 1. take route[0] to route[i-1] and add them in order to new_route
    for ( int c = 0; c <= i - 1; c++ )
    {
        new_Route.set(c, route.get(c) );
    }

    // 2. take route[i] to route[k] and add them in reverse order to new_route
    int dec = 0;
    for ( int c = i; c <= k; c++ )
    {
        new_Route.set( c, route.get( k - dec ) );
        dec++;
    }

    // 3. take route[k+1] to end and add them in order to new_route
    for ( int c = k + 1; c < size; c++ )
    {
        new_Route.set( c, route.get( c ) );
    }
}
David M
  • 198
  • 1
  • 5

1 Answers1

0

You don't have to swap three points, but three edges of your route, that is why you have a problem here. For example, say that your tour is 0,1,...,i,i+1,..., j, j+1, ..., k, k+1,...,n,0. What you have to do is delete three edges [i,i+1], [j,j+1] and [k,k+1] and try to rebuild your tour (Note that if you try to rebuild using all the possibilities, you will also implement the 2-OPT and you will also get back to the current tour). You have an example here on slide 39.

Damien Prot
  • 573
  • 3
  • 7