Main
import net.iharder.tabusearch25.*;
public class Main
{
// Random number generator.
private static java.util.Random R;
public static void main( String[] args )
{
// Use a constant random number seed.
long seed = 123456789;
R = new java.util.Random( seed );
// Define an operating region.
double x1 = 0;
double y1 = 0;
double x2 = 400;
double y2 = 400;
// The first argument when calling the
// program can be the number of customers.
// Otherwise, it defaults to 100 customers.
int custCount = 100;
try{ custCount = Integer.parseInt( args[0] ); }
catch( Exception e ){}
System.out.println(
"Number of customers: " + custCount );
// The second argument when calling the
// program can be the tabu list tenure.
// Otherwise, it defaults to 10.
int tenure = 10;
try{ tenure = Integer.parseInt( args[1] ); }
catch( Exception e ){}
System.out.println(
"Tabu list tenure: " + tenure );
// The third argument when calling the
// program can be the number of iterations.
// Otherwise, it defaults to 500.
int iterations = 500;
try{ iterations = Integer.parseInt( args[2] ); }
catch( Exception e ){}
System.out.println(
"Number of iterations: " + iterations );
// Create customers in an area.
Customer[] customers = createRandomCustomers(
x1, y1, x2, y2, custCount );
// Create a salesman.
Salesman salesman = createRandomSalesman(
x1, y1, x2, y2 );
// Instantiate all the tabu search objects
TSPSolution initialSoln = new TSPSolution(
salesman, customers );
MoveManager moveMgr = new MoveManager();
TSPTabuList tabuList = new TSPTabuList( tenure );
ObjectiveFunction objFunc =
new ObjectiveFunction( initialSoln );
// Create the Engine that will do the iterations.
// The 'false' indicates minimization.
TSEngine engine = new TSEngine( initialSoln,
tabuList, objFunc, moveMgr, false );
// Start the engine and immediately return
// control to this thread.
engine.startSolving( iterations );
// Just stall this thread until the engine finishes.
engine.waitToFinish();
// Get the best solution (after casting it back).
TSPSolution bestSoln =
(TSPSolution) engine.getBestSolution();
// Print the results
printSolution( bestSoln );
showSolutionFrame( bestSoln );
} // end main
private static Customer[] createRandomCustomers(
double lowerBoundX, double lowerBoundY,
double upperBoundX, double upperBoundY, int count )
{
// Some initializing
Customer[] customers = new Customer[ count ];
double spanX = upperBoundX - lowerBoundX;
double spanY = upperBoundY - lowerBoundY;
// Create customers within bounds
for( int i = 0; i < count; i++ )
{
// 0.0 to 0.999...
double rx = R.nextDouble();
double ry = R.nextDouble();
double x = ( rx * spanX ) + lowerBoundX;
double y = ( ry * spanY ) + lowerBoundY;
customers[ i ] = new Customer( x, y );
} // end for: through each customer
return customers;
} // end createRandomCustomers
private static Salesman createRandomSalesman(
double lowerBoundX, double lowerBoundY,
double upperBoundX, double upperBoundY )
{
// Some initializing
double spanX = upperBoundX - lowerBoundX;
double spanY = upperBoundY - lowerBoundY;
// Create salesman within bounds
// 0.0 to 0.999...
double rx = R.nextDouble();
double ry = R.nextDouble();
double x = ( rx * spanX ) + lowerBoundX;
double y = ( ry * spanY ) + lowerBoundY;
return new Salesman( x, y );
} // end createRandomSalesman
// Print out a solution.
private static void printSolution( TSPSolution soln )
{
Salesman salesman = soln.getSalesman();
Customer[] customers = soln.getCustomers();
double cost = soln.getObjectiveValue()[0];
System.out.println( "Solution" );
System.out.println( " Cost: " + cost );
System.out.print ( " Sequence: H" );
for( int i = 0; i <= customers.length; i++ )
System.out.print(
( i < customers.length )
? " - " + customers[ i ].getID()
: " - H" );
} // end printSolution
private static void showSolutionFrame( final TSPSolution soln )
{ final Salesman salesman = soln.getSalesman();
final Customer[] customers = soln.getCustomers();
java.awt.Panel panel = new java.awt.Panel()
{ public void paint( java.awt.Graphics g )
{ // Paint trip.
for( int i = 0; i < customers.length; i++ )
{ // First?
if( i == 0 )
{ g.setColor( java.awt.Color.red );
g.drawLine( (int)salesman.getHomeX(),
(int)salesman.getHomeY(),
(int)customers[0].getX(),
(int)customers[0].getY() );
g.drawString( "HOME",
(int)salesman.getHomeX(),
(int)salesman.getHomeY() );
g.setColor( java.awt.Color.green.darker() );
} // end if: first customer
// Last?
else if( i == (customers.length-1) )
{ g.setColor( java.awt.Color.blue );
g.drawLine(
(int)customers[customers.length-1].getX(),
(int)customers[customers.length-1].getY(),
(int)salesman.getHomeX(),
(int)salesman.getHomeY() );
} // end else if: last
// In between
else
{ g.drawLine(
(int)customers[i].getX(),
(int)customers[i].getY(),
(int)customers[i+1].getX(),
(int)customers[i+1].getY() );
} // end else: in between
} // end for
} // end paint
}; // end panel
java.awt.Frame frame = new java.awt.Frame();
frame.add( panel, java.awt.BorderLayout.CENTER );
java.awt.Dimension dim =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int width, height;
width = height = 420;
frame.setBounds(
(dim.width-width)/2,
(dim.height-height)/2,
width, height );
frame.addWindowListener( new java.awt.event.WindowAdapter()
{ public void windowClosing( java.awt.event.WindowEvent evt )
{ System.exit(0);
} // end windowClosing
}); // end window adapter
frame.show();
} // end showSolutionFrame
} // end class Main