Permalink
Browse files

TSP: show the best out of 30 runs

Signed-off-by: Daniel Ritz <daniel.ritz@gmx.ch>
  • Loading branch information...
1 parent aae4d68 commit 489f85de32f104f5deb981575b7135b33f59de0b @dr-itz committed May 13, 2012
Showing with 26 additions and 7 deletions.
  1. +26 −7 java/src/ch/dritz/zhaw/ci/tsp/TravelingSalesman.java
@@ -18,20 +18,23 @@
private Table table;
private int size;
private Random rand;
+ private Path initialPath;
public TravelingSalesman(File file)
throws IOException
{
table = Parser.parse(file);
size = table.getSize();
rand = new Random();
+
+ initialize();
}
/**
* calculates the initial path using a nearest-neighbor heuristic, starting
* with the first town
*/
- private Path initialize()
+ private void initialize()
{
Path ret = new Path(table);
@@ -59,7 +62,15 @@ private Path initialize()
visited.put(minIdx, Boolean.TRUE);
prevIdx = minIdx;
}
- return ret;
+ initialPath = ret;
+ }
+
+ /**
+ * @return the initialPath
+ */
+ public Path getInitialPath()
+ {
+ return initialPath;
}
/**
@@ -110,9 +121,7 @@ private double newTemp(double temperature)
*/
public Path findSolution()
{
- Path s = initialize();
- System.out.println(s);
-
+ Path s = initialPath;
Path sBest = s;
double t = 300D;
while (t > 20) {
@@ -142,7 +151,17 @@ public static void main(String[] args)
}
TravelingSalesman tsp = new TravelingSalesman(new File(args[0]));
- Path s = tsp.findSolution();
- System.out.println(s);
+ Path initial = tsp.getInitialPath();
+ System.out.print("INITIAL : ");
+ System.out.println(initial);
+
+ Path best = initial;
+ for (int i = 0; i < 30; i++) {
+ Path sol = tsp.findSolution();
+ if (sol.measure() < best.measure())
+ best = sol;
+ }
+ System.out.print("BEST: ");
+ System.out.println(best);
}
}

0 comments on commit 489f85d

Please sign in to comment.