add java examples
This commit is contained in:
parent
624c92e934
commit
d345f67212
19 changed files with 1208 additions and 0 deletions
171
java/functionnal/Algo.java
Normal file
171
java/functionnal/Algo.java
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
package patterns.functionnal;
|
||||
|
||||
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 Neighborhood(Neighborhood.quad(), step, pMin, pMax);
|
||||
Neighborhood octo = new Neighborhood(Neighborhood.octo(), step, pMin, pMax);
|
||||
|
||||
Transit graph = new Transit(Transit.edge());
|
||||
Transit mesh = new Transit(Transit.simplex(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, quadCrtp, 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, octoCrtp, 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, quadCrtp, 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, octoCrtp, 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);*/
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
43
java/functionnal/Neighborhood.java
Normal file
43
java/functionnal/Neighborhood.java
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package patterns.functionnal;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.function.Function;
|
||||
|
||||
import patterns.Point;
|
||||
/**
|
||||
*
|
||||
* @author pouyllau
|
||||
*
|
||||
*/
|
||||
public class Neighborhood {
|
||||
|
||||
protected static double step;
|
||||
protected static Point pMin;
|
||||
protected static Point pMax;
|
||||
Function<Point, List<Point>> f;
|
||||
|
||||
public Neighborhood(Function<Point, List<Point>> f,double step, Point pMin, Point pMax) {
|
||||
super();
|
||||
this.f = f;
|
||||
this.step = step;
|
||||
this.pMin = pMin;
|
||||
this.pMax = pMax;
|
||||
}
|
||||
|
||||
public List<Point> get( Point p) {
|
||||
return f.apply(p);
|
||||
}
|
||||
|
||||
public static Function<Point, List<Point>> quad() {
|
||||
return x -> Point.neighbors_grid(x, step, pMin, pMax,
|
||||
Arrays.asList(new Point(1, 0), new Point(0, -1), new Point(-1, 0), new Point(0, 1)));
|
||||
}
|
||||
|
||||
public static Function<Point, List<Point>> octo() {
|
||||
return x -> Point.neighbors_grid(x, step, pMin, pMax,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)));
|
||||
}
|
||||
|
||||
}
|
||||
104
java/functionnal/Transit.java
Normal file
104
java/functionnal/Transit.java
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
package patterns.functionnal;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import patterns.Point;
|
||||
/**
|
||||
*
|
||||
* @author pouyllau
|
||||
*
|
||||
*/
|
||||
public class Transit {
|
||||
|
||||
public static boolean hasCost(Point p ,Map<Point, Double> costs) {
|
||||
return (costs.get(p) != null && costs.get(p) != Double.POSITIVE_INFINITY);
|
||||
}
|
||||
|
||||
TriFunction<Point, List<Point>, Map<Point, Double>, Double> f;
|
||||
|
||||
|
||||
public Transit(TriFunction<Point, List<Point>, Map<Point, Double>, Double> f) {
|
||||
super();
|
||||
this.f = f;
|
||||
}
|
||||
|
||||
public double transit(Point p, List<Point> neighbors, Map<Point, Double> costs) {
|
||||
return f.apply(p,neighbors,costs);
|
||||
}
|
||||
|
||||
public static TriFunction<Point, List<Point>, Map<Point, Double>, Double> simplex(double eps) {
|
||||
return (x, y, z) -> transitInSimplex(x,y,z, eps);
|
||||
}
|
||||
|
||||
public static TriFunction<Point, List<Point>, Map<Point, Double>, Double> edge() {
|
||||
return (x, y, z) -> transitOnEdge(x,y,z);
|
||||
}
|
||||
|
||||
public static double transitOnEdge(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;
|
||||
}
|
||||
|
||||
public static double transitInSimplex(Point p, List<Point> neighbors, Map<Point, Double> costs, double eps) {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
20
java/functionnal/TriFunction.java
Normal file
20
java/functionnal/TriFunction.java
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package patterns.functionnal;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
/**
|
||||
*
|
||||
* @author pouyllau
|
||||
*
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface TriFunction<A,B,C,R> {
|
||||
|
||||
R apply(A a, B b, C c);
|
||||
|
||||
default <V> TriFunction<A, B, C, V> andThen(
|
||||
Function<? super R, ? extends V> after) {
|
||||
Objects.requireNonNull(after);
|
||||
return (A a, B b, C c) -> after.apply(apply(a, b, c));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue