import java.util.Scanner; import java.util.Random; import java.awt.Point; import java.util.Collections; import java.util.ArrayList; import java.util.Arrays; public class TSP { static Random die = new Random(1954); static int MAX_X = 500; static int MAX_Y = 300; private Point[] city; private double[][] distance; private int[] tour; private double scoreSolution; public static void main( String[] args ) { TwoOptDisplay gui = new TwoOptDisplay(10); // start off with 10 cities System.out.println("gui up and running"); } public boolean edgesCross(int a, int b, int c, int d) { return true; // replace with your own code } public double interchange(int i, int j) { return 0; // replace with your own code } // code tests your two methods ************************** // will run a long time if your code is bad ************ public ArrayList removeCrossings() { ArrayList list = new ArrayList(); list.add(solution()); computeScore(); int n = city.length; boolean foundCrossing; int count = 0; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n-1; ++j) { if ( ( i == j ) || ( j == succ(i) ) ||( i == succ(j) ) ) { continue; } int a = tour[ i ]; int b = tour[ succ(i) ]; int c = tour[ j ]; int d = tour[ succ(j) ]; if ( edgesCross(a, b, c, d) ) { ++count; } } } System.out.println("edge crossing count before 2-opting = " + count); for (int trial = 1; trial <= n*n*n; ++trial) { foundCrossing = false; for (int i = 0; i < n; ++i) { for (int j = i+1; j < n; ++j) { if ( ( i == j ) || ( j == succ(i) ) ||( i == succ(j) ) ) { continue; } int a = tour[ i ]; int b = tour[ succ(i) ]; int c = tour[ j ]; int d = tour[ succ(j) ]; if ( edgesCross(a, b, c, d) ) { foundCrossing = true; interchange(i, j); int[] copy = new int[ n ]; for (int k = 0; k < n; ++k) { copy[k] = tour[k]; } list.add(copy); computeScore(); } } } if (! foundCrossing ) { break; } } System.out.println("edge crossings that remained after 2-opt has completed"); count = 0; for (int i = 0; i < n; ++i) { for (int j = i+1; j < n; ++j) { if ( ( i == j ) || ( j == succ(i) ) ||( i == succ(j) ) ) { continue; } int a = tour[ i ]; int b = tour[ succ(i) ]; int c = tour[ j ]; int d = tour[ succ(j) ]; if ( edgesCross(a, b, c, d) ) { System.out.printf("(%2d, %2d) x (%2d, %2d)\n", a, b, c, d); ++count; } } } System.out.println("edge crossing count after 2-opting= " + count); return list; } public TSP(Scanner fin) { this( extractLocations(fin) ); } public TSP() { this ( smallTestSet() ); } public TSP(int n) { this( randomPoints(n, MAX_X, MAX_Y) ); } public TSP( Point[] city ) { int n = city.length; this.city = city; distance = new double[n][n]; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { double xdiff = city[i].x - city[j].x; double ydiff = city[i].y - city[j].y; distance[i][j] = Math.sqrt(xdiff*xdiff + ydiff*ydiff); } } randomize(); computeScore(); } public static Point[] smallTestSet() { Point[] list = new Point[9]; for (int i = 0; i < 3; ++i) { list[i] = new Point(); list[i+3] = new Point(); list[i+6] = new Point(); list[i].x = 30; list[i+3].x = 80; list[i+6].x = 50; list[i].y = 20 + i*20; list[i+3].y = 20 + i*20; list[i+6].y = 15 + i*20; } return list; } int pred(int i) { return (i + (city.length-1)) % city.length; } int succ(int i) { return (i + 1) % city.length; } public void randomize() { int n = city.length; tour = new int[n]; for (int i = 0; i < n; ++i) { tour[i] = i; } for (int i = n; i > 0; --i ) { int j = die.nextInt(i); int rmbr = tour[i-1]; tour[i-1] = tour[j]; tour[j] = rmbr; } computeScore(); } public double score() { return scoreSolution; } public double computeScore() { scoreSolution = 0; for (int i = 0; i < city.length; ++i) { scoreSolution += distance(tour[i], tour[succ(i)]); } return scoreSolution; } public double distance(int i, int j) { return distance[i][j]; } static Point[] extractLocations(Scanner fin) { ArrayList list = new ArrayList(); while ( fin.hasNext() ) { Scanner line = new Scanner( fin.nextLine() ); int x = line.nextInt(); int y = line.nextInt(); list.add( new Point(x, y) ); } return ( Point[] ) list.toArray(); } int[] solution() { int[] result = new int[tour.length]; for (int i = 0; i < tour.length; ++i) { result[i] = tour[i]; } return result; } static Point[] randomPoints(int n, int xmax, int ymax ) { Point[] list = new Point[n]; for (int i = 0; i < list.length; ++i) { list[i] = new Point(die.nextInt(xmax), die.nextInt(ymax)); } return list; } public Point[] getCities() { return city; } void println() { println(tour); } static void println(int[] a) { System.out.print("list(" + a.length + "): "); for (int i = 0; i < a.length; ++i) { System.out.print(a[i] + " "); } System.out.println(); } }