Skip to content
Snippets Groups Projects
Commit d6ce7e13 authored by Frédéric Bapst's avatar Frédéric Bapst
Browse files

add s15=tsp

parent 5e19a139
No related branches found
No related tags found
No related merge requests found
package tsp;
public interface TSP {
void salesman(TSPPoint[] t, int[] path);
}
package tsp;
//======================================================================
public class TSP0 implements TSP {
public void salesman(TSPPoint[] t, int[] path) {
int i, j;
int n = t.length;
boolean[] visited = new boolean[n];
int thisPt, closestPt = 0;
double shortestDist;
thisPt = n-1;
if (thisPt < 0) return;
visited[thisPt] = true;
path[0] = n-1; // chose the starting city
for(i=1; i<n; i++) {
shortestDist = Double.MAX_VALUE;
for(j=0; j<n; j++) {
if (visited[j])
continue;
if (distance(t[thisPt], t[j]) < shortestDist ) {
shortestDist = distance(t[thisPt], t[j]);
closestPt = j;
}
}
path[i] = closestPt;
visited[closestPt] = true;
thisPt = closestPt;
}
}
//----------------------------------
static private double sqr(double a) {
return a*a;
}
//---------------------------------
static private double distance(TSPPoint p1, TSPPoint p2) {
return Math.sqrt(sqr(p1.x-p2.x) + sqr(p1.y-p2.y));
}
}
package tsp;
public class TSPPoint {public double x; public double y;}
package tsp;
import java.util.Random;
// ========================================================================
class TSPTest {
static private double sqr(double a) {
return a*a;
}
static private double distance(TSPPoint p1, TSPPoint p2) {
return Math.sqrt(sqr(p1.x-p2.x) + sqr(p1.y-p2.y));
}
static private TSPPoint[] generatePoints(int n) {
TSPPoint[] p = new TSPPoint[n];
Random r = new Random(15);
for(int i=0; i<n; i++) {
p[i] = new TSPPoint();
p[i].x = 500 * r.nextFloat();
p[i].y = 500 * r.nextFloat();
}
return p;
}
static private double pathLength(TSPPoint[] t, int[] path) {
if (t.length!=path.length) return -1.0;
double res = 0.0;
TSPPoint p = t[path[0]];
boolean[] v = new boolean[t.length];
v[path[0]]=true;
TSPPoint a;
for(int i=1; i<path.length; i++) {
if (v[path[i]]) return -1.0;
a = t[path[i]];
res += distance(a, p);
p = a;
v[path[i]]=true;
}
return res;
}
static TSP[] loadClasses(int[] algos) {
TSP[] res = new TSP[algos.length];
for(int i=0; i<algos.length; i++) {
try {
res[i] = getTSP("tsp.TSP"+algos[i]);
} catch (Exception e) {
System.out.println("Problem loading TSP class: "+algos[i]);
}
}
return res;
}
static TSP getTSP(String className) throws Exception {
Class<?> c = Class.forName(className);
return (TSP) c.getDeclaredConstructor().newInstance();
}
//------------------------------------------------------------
public static void main (String[] args) {
int n = 15_000; // TODO: tune this to reach a reasonable execution time
if (args.length < 1) {
System.out.println("Usage: TSPTest nbOfCities [algoID...]");
System.out.println(" ok, let's try with " + n + " cities...");
} else {
n = Integer.parseInt(args[0]);
}
TSPPoint[] pts = generatePoints(n);
int[] path = new int[n];
double res;
int[] algos = {0, 101, 0}; // TODO: add your versions: ,11, 12,...
TSP[] tspClasses;
long t1, t2;
if (args.length>1) {
algos = new int[args.length-1];
for(int i=0; i<algos.length; i++)
algos[i]=Integer.parseInt(args[1+i]);
}
tspClasses = loadClasses(algos);
System.out.println("n="+n);
for(int i=0; i<algos.length; i++) {
if (tspClasses[i] == null) continue;
java.util.Arrays.fill(path, 0);
t1 = System.nanoTime();
tspClasses[i].salesman(pts, path); // <------ calling salesman
t2 = System.nanoTime();
res = pathLength(pts, path);
System.out.print (""+algos[i]+"\t- running time [ms] = " + (t2-t1)/1000/1000);
System.out.println(" \t path length = " + res);
/* System.out.print ("path steps : " );
for(int i=0; i<n; i++)
System.out.print(" " + path[i]);
System.out.println();
*/
}
}
//------------------------------------------------------------
}
package tsp;
import java.io.IOException;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
public class TspViaJmh {
public static void main(String... args) throws RunnerException, IOException {
if (args.length>0) {
// Use standard JHM Main.
// Command line options: try "-h" for a description
org.openjdk.jmh.Main.main(args);
return;
}
// "-bm avgt -f 2 -wi 3 -i 4 -p algoId=120,177,0 -p nbOfCities=100000"
new Runner(config(true)).run(); // normal (with JIT)
// idem but: "-p nbOfCities=20000 -jvmArgsAppend -Xint"
// new Runner(config(false)).run(); // without JIT (far slower)
}
static Options config(boolean withJit) {
int nbOfCities = withJit ? 50_000 : 10_000;
ChainedOptionsBuilder ob = new OptionsBuilder()
.warmupIterations(3)
.measurementIterations(4)
.forks(2)
.mode(Mode.AverageTime)
.param("algoId", "0", "101")
.param("nbOfCities", ""+nbOfCities);
if (!withJit) ob=ob.jvmArgsAppend("-Xint");
return ob.build();
}
}
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment