All pastes #257058 Raw Edit

Miscellany

public java v1 · immutable
#257058 ·published 2006-11-24 22:46 UTC
rendered paste body
import java.awt.*;import java.io.PrintStream;public class TSP{    private static int maxPoints = 200;    private static int maxTime = 30;    private static final String MAP_FILE_NAME = ".";    private static int population = 200;    TSPMap map;	TSPTour tour;    Thread worker;    boolean threadWorking;    public static void main(String args[])    {        if(args.length > 0 && (args[0].equals("-h") || args.length < 3))        {            System.out.println("TSP <#city> <population> <maxtime>");            return;        }        if(args.length > 0)        {            maxPoints = Integer.parseInt(args[0]);            population = Integer.parseInt(args[1]);            maxTime = Integer.parseInt(args[2]);        }        TSP tsp = new TSP();		tsp.doGa();    }	public TSP()	{        threadWorking = false;        map = new TSPMap(maxPoints, 350);	}	public void doGa()    {        if(!threadWorking)        {            worker = new Thread() {                public void run()                {                    TSPGeneticSolver tsp = new TSPGeneticSolver(map, 200);                    tour = tsp.getSolution();                    long startT = System.currentTimeMillis();                    for(int i = 0; (System.currentTimeMillis() - startT) / 1000L < (long)maxTime; i++)                    {                        tsp.doOneGeneration();                        tour = tsp.getSolution();                        System.out.println("Calculating (" + i + "): " + tsp.getSolution().getLength());                    }                    System.out.println("T:    " + (System.currentTimeMillis() - startT) / 1000L);                    System.out.println("GA-O: " + tsp.getSolution().getLength());                    threadWorking = false;                }            };            worker.start();            threadWorking = true;        }    }}