OpenTS Tutorial
MySolution
The MySolution Object

In our simple Traveling Salesman Problem (TSP), a solution can be easily defined as a list of customers in the order in which they will be visited. Our salesman will begin at customer zero (0) and return to this customer at the end. Our solution object then is simply an int array with the values 0 to n - 1, where n is the number of customers to visit.

The complete source is available, but we've duplicated it here:

import org.coinor.opents.*;

public class MySolution extends SolutionAdapter 
{
    public int[] tour;
    
    public MySolution(){} // Appease clone()
    
    public MySolution( double[][] customers )
    {
        // Crudely initialize solution
        tour = new int[ customers.length ];
        for( int i = 0; i < customers.length; i++ )
            tour[i] = i;
    }   // end constructor
    
    
    public Object clone()
    {   
        MySolution copy = (MySolution)super.clone();
        copy.tour = (int[])this.tour.clone();
        return copy;
    }   // end clone
    
}   // end class MySolution

We have a null constructor to appease Java when it performs clone() operations, and a "real" constructor that initializes the solution in a rather boring way by setting the visitation sequence to 0, 1, 2, 3,..., 19. A more useful initialization routine would be to create some kind of greedy heuristic to build the first solution.

It is essential that you implement a clone() method. The OpenTS Tabu Search object will create clones of your solutions whenever a new best solution is found. This enables the Tabu Search to keep a safe copy of the best known solution while the current solution gets pushed around. Note the care we take to make a clone of the tour array which is not automatically duplicated by Java's Object's clone() method.

By extending org.coinor.opents.SolutionAdapter instead of implementing org.coinor.opents.Solution, the required methods getObjectiveValue() and setObjectiveValue(...) are taken care of, along with the underlying book-keeping.

That's it for the MySolution object. In the next section we'll look at the objective function that will be used to evaluate a solution.