add java examples

This commit is contained in:
hpid91 2020-06-25 16:00:13 +02:00 committed by nojhan
commit d345f67212
19 changed files with 1208 additions and 0 deletions

172
java/strategy/Algo.java Normal file
View file

@ -0,0 +1,172 @@
package patterns.strategy;
import java.io.StreamTokenizer;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.PriorityQueue;
import patterns.Point;
/**
*
* @author pouyllau
*
*/
public class Algo {
private static boolean DEBUG = false;
public static Map<Point, Double> run(Point seed, int iterations, Neighborhood neighborhood, Transit transit) {
Map<Point, Double> costs = new HashMap<Point, Double>();
PriorityQueue<Point> front = new PriorityQueue<Point>(new Comparator<Point>() {
public int compare(Point lhs, Point rhs) {
if (costs.get(lhs) >= costs.get(rhs))
return +1;
if (costs.get(lhs) < costs.get(rhs))
return -1;
return 0;
}
});
costs.put(seed, 0.);
front.add(seed);
for (int i = 0; i < iterations && !front.isEmpty(); i++) {
if (DEBUG)
System.out.println(i + "/" + iterations);
// Accept the node with the min cost and update neighbors.
// Accept the considered node with the min cost.
Point accepted = front.poll();
// Consider neighbors of the accepted node.
List<Point> around = neighborhood.get(accepted);
assert (around.size() > 0);
for (Point n : around) {
// If no cost has been computed (i.e. the node is "open").
if (!Transit.hasCost(n, costs)) {
double ncost = transit.transit(n, neighborhood.get(n), costs);
if (DEBUG)
System.out.println(n + " cost=" + ncost);
costs.put(n, ncost);
front.add(n);
}
}
}
return costs;
}
public static void printGrid(Map<Point, Double> grid, Point pMin, Point pMax, double step, String sep) {
if (sep == null)
sep = "\t";
String width = " ";
System.out.print(" x:" + sep);
for (double px = pMin.x; px <= pMax.x; px += step) {
System.out.print(sep + (int) px + " " + width);
}
System.out.print("\n");
System.out.print(" y\n");
for (double py = pMax.y; py >= pMin.y; py -= step) {
System.out.print(sep + (int) py + ":");
for (double px = pMin.x; px <= pMax.x; px += step) {
Point p = new Point(px, py);
if (Transit.hasCost(p, grid)) {
System.out.format(sep + "%.2f" + width, grid.get(p));
} else {
System.out.print(sep + " . " + width);
}
}
System.out.print("\n");
}
System.out.print("\n");
}
public static void performEvaluations(Point seed, int iterations, Neighborhood neighborhood, Transit transit,
int T) {
double mean = 0;
double m2 = 0;
for (int t = 0; t < T; t++) {
long startTime = System.nanoTime();
run(seed, iterations, neighborhood, transit);
startTime = System.nanoTime() - startTime;
double delta = (double)startTime - mean;
mean += delta / (double) T;
m2 += delta * delta;
}
m2 = m2 / (double)(T-1.);
m2 = Math.sqrt(m2);
System.out.println("Mean time : " + new DecimalFormat("#.##########").format((mean / 1000000000)) + " s"
+ " sd : " + new DecimalFormat("#.##########").format((m2 / 1000000000)));
}
public static void main(String[] args) {
Locale.setDefault(Locale.US);
Point seed = new Point(0, 0);
Point pMin = new Point(-5, -5);
Point pMax = new Point(15, 15);
double step = 1.;
int maxit = 300;
double eps = 1. / 100.0;
int eval = 100;
Neighborhood quad = new patterns.strategy.QuadGrid(step, pMin, pMax);
Neighborhood octo = new patterns.strategy.OctoGrid(step, pMin, pMax);
Transit graph = new TransitOnEdge();
Transit mesh = new TransitInSimplex(eps);
System.out.println("Dijkstra, 4 neighbors");
performEvaluations(seed, maxit, quad, graph, eval);
System.out.println("Dijkstra, 8 neighbors");
performEvaluations(seed, maxit, octo, graph, eval);
System.out.println("Fast marching, 4 neighbors");
performEvaluations(seed, maxit, quad, mesh, eval);
System.out.println("Fast marching, 8 neighbors");
performEvaluations(seed, maxit, octo, mesh, eval);
/*
System.out.println("Dijkstra, 4 neighbors");
long startTime = System.nanoTime();
Map<Point, Double> dijkstra4 = run(seed, maxit, quad, graph);
startTime = System.nanoTime() - startTime;
double duration = ((double)startTime / 1000000000);
System.out.println("solution Time : " + new DecimalFormat("#.##########").format(duration) + " Seconds");
printGrid(dijkstra4, pMin, pMax, step, null);
System.out.println("Dijkstra, 8 neighbors");
startTime = System.nanoTime();
Map<Point, Double> dijkstra8 = run(seed, maxit, octo, graph);
startTime = System.nanoTime() - startTime;
duration = ((double)startTime / 1000000000);
System.out.println("solution Time : " + new DecimalFormat("#.##########").format(duration) + " Seconds");
printGrid(dijkstra8, pMin, pMax, step, null);
System.out.println("Fast marching, 4 neighbors");
startTime = System.nanoTime();
Map<Point, Double> fast_marching4 = run(seed, maxit, quad, mesh);
startTime = System.nanoTime() - startTime;
duration = ((double)startTime / 1000000000);
System.out.println("solution Time : " + new DecimalFormat("#.##########").format(duration) + " Seconds");
printGrid(fast_marching4, pMin, pMax, step, null);
System.out.println("Fast marching, 8 neighbors");
startTime = System.nanoTime();
Map<Point, Double> fast_marching8 = run(seed, maxit, octo, mesh);
startTime = System.nanoTime() - startTime;
duration = ((double)startTime / 1000000000);
System.out.println("solution Time : " + new DecimalFormat("#.##########").format(duration) + " Seconds");
printGrid(fast_marching8, pMin, pMax, step, null);
*/
}
}

View file

@ -0,0 +1,27 @@
package patterns.strategy;
import java.util.List;
import patterns.Point;
/**
*
* @author pouyllau
*
*/
public abstract class Neighborhood {
protected double step;
protected Point pMin;
protected Point pMax;
protected List<Point> directions;
public Neighborhood(double step, Point pMin, Point pMax) {
super();
this.step = step;
this.pMin = pMin;
this.pMax = pMax;
}
public abstract List<Point> get(Point p);
}

View file

@ -0,0 +1,24 @@
package patterns.strategy;
import java.util.Arrays;
import java.util.List;
import patterns.Point;
/**
*
* @author pouyllau
*
*/
public class OctoGrid extends Neighborhood {
public OctoGrid(double step, Point pMin, Point pMax) {
super(step, pMin, pMax);
this.directions = Arrays.asList(new Point(1, 0), new Point(1, -1), new Point(0, -1), new Point(-1, -1),
new Point(-1, 0), new Point(-1, 1), new Point(0, 1), new Point(1, 1));
}
public List<Point> get(Point p) {
return Point.neighbors_grid(p, step,pMin, pMax, directions);
}
}

View file

@ -0,0 +1,24 @@
package patterns.strategy;
import java.util.Arrays;
import java.util.List;
import patterns.Point;
/**
*
* @author pouyllau
*
*/
public class QuadGrid extends Neighborhood {
public QuadGrid(double step, Point pMin, Point pMax) {
super(step, pMin, pMax);
this.directions = Arrays.asList(new Point(1,0), new Point (0,-1), new Point(-1,0), new Point(0,1));
}
public List<Point> get(Point p) {
return Point.neighbors_grid(p, step,pMin, pMax, directions);
}
}

View file

@ -0,0 +1,19 @@
package patterns.strategy;
import java.util.List;
import java.util.Map;
import patterns.Point;
/**
*
* @author pouyllau
*
*/
public abstract class Transit {
public abstract double transit(Point p, List<Point> neighbors, Map<Point, Double> costs);
public static boolean hasCost(Point p ,Map<Point, Double> costs) {
return (costs.get(p) != null && costs.get(p) != Double.POSITIVE_INFINITY);
}
}

View file

@ -0,0 +1,80 @@
package patterns.strategy;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import patterns.Point;
/**
* Find the transit in minimal cost within the given simplexes.
*
* That is, find the minimal distance between the given point and one of the
* edges formed by the sequence of pairs of neighbors. Neighbors should thus be
* given in clockwise order. The minimal transit is searched across 1/eps
* distances, regularly spaced on each edge.
* @author pouyllau
*/
public class TransitInSimplex extends Transit {
protected double eps;
public TransitInSimplex(double eps) {
super();
this.eps = eps;
}
public double transit(Point p, List<Point> neighbors, Map<Point, Double> costs) {
double mincost = Double.MAX_VALUE;
List<Point> list = neighbors.stream().filter(n -> hasCost(n, costs)).collect(Collectors.toList());
int k = list.size();
if (k == 1) {
mincost = costs.get(list.get(0))+ Point.distance(list.get(0), p);
} else {
for (int i = 0; i < neighbors.size(); i++) {
Point pj = neighbors.get(i);
Point pk = (i+1 < neighbors.size() ? neighbors.get(i+1) : neighbors.get(0));
double c = 0;
if (hasCost(pj, costs) && hasCost(pk, costs)) {
// Cost of the transition from/to p from/to edge e.
// This is the simplest way to minimize the transit, even if
// not the most efficient.
for (double z = 0; z <= 1; z += eps) {
double zx = z * pj.x + (1 - z) * pk.x;
double zy = z * pj.y + (1 - z) * pk.y;
// Linear interpolation of costs.
c = z *costs.get(pj) + (1 - z) * costs.get(pk)+ Point.distance(p, new Point(zx, zy));
if (c < mincost) {
mincost = c;
}
} // for z
// If the front is reached on a single point.
} else if (hasCost(pj, costs) && !hasCost(pk, costs)) {
c = costs.get(pj)+ Point.distance(p, pj);
if (c < mincost) {
mincost = c;
}
} else if (!hasCost(pj, costs) && hasCost(pk, costs)) {
c = costs.get(pk) + Point.distance(p, pk);
if (c < mincost) {
mincost = c;
}
}
}
}
assert (mincost < Double.POSITIVE_INFINITY);
return mincost;
}
}

View file

@ -0,0 +1,32 @@
package patterns.strategy;
import java.util.List;
import java.util.Map;
import patterns.Point;
/**
* Find the transit of minimal cost among the given edges.
Edges are given as the considered point and the sequence of neighbors points.
* @author pouyllau
*/
public class TransitOnEdge extends Transit {
public double transit(Point p, List<Point> neighbors, Map<Point, Double> costs) {
double mincost = Double.POSITIVE_INFINITY;
for (Point n : neighbors) {
if (hasCost(n, costs)) {
double c = costs.get(n) + Point.distance(p, n);
if (c < mincost) {
mincost = c;
}
}
}
assert (mincost != Double.POSITIVE_INFINITY);
return mincost;
}
}