There are many complicated ways to move through a TSP
solution space, but we will limit ourselves to simple
swaps: one customer trades places with another in the
lineup.
As before the complete source is available,
but we've duplicated it here:
import org.coinor.opents.*;
public class MySwapMove implements Move
{
public int customer;
public int movement;
public MySwapMove( int customer, int movement )
{
this.customer = customer;
this.movement = movement;
} // end constructor
public void operateOn( Solution soln )
{
int[] tour = ((MySolution)soln).tour;
int pos1 = -1;
int pos2 = -1;
// Find positions
for( int i = 0; i < tour.length && pos1 < 0; i++ )
if( tour[i] == customer )
pos1 = i;
pos2 = pos1 + movement;
// Swap
int cust2 = tour[pos2];
tour[pos1] = cust2;
tour[pos2] = customer;
} // end operateOn
/** Identify a move for SimpleTabuList */
public int hashCode()
{
return customer;
} // end hashCode
} // end class MySwapMove
The two integers customer and movement define
a move such as "Move customer Number Four three places toward the rear."
The operateOn(...)
method, required by OpenTS, will be
called if the move is the chosen one for a given iteration.
Because we're using the SimpleTabuList object that's included in
the OpenTS package, we must override the hashCode()
method which the SimpleTabuList uses to identify a move.
We'll make the hash code equal to the customer number.
That means the tabu list will be tracking which customer has moved
and will not let a customer move that has moved recently.
That's how we plan to manipulate the solution.
Next we'll take a look at how we determine which moves are valid.