static ListenableGraph<LengthData> createGraph() { final Graph<LengthData> g = new TableGraph<>(); final Table<Integer, Integer, Point> leftMatrix = createMatrix(5, 10, new Point(0, 0)); for (final Map<Integer, Point> column : leftMatrix.columnMap().values()) { Graphs.addBiPath(g, column.values()); } Graphs.addBiPath(g, leftMatrix.row(LEFT_CENTER_U_ROW).values()); Graphs.addBiPath(g, leftMatrix.row(LEFT_CENTER_L_ROW).values()); final Table<Integer, Integer, Point> rightMatrix = createMatrix(10, 7, new Point(30, 6)); for (final Map<Integer, Point> row : rightMatrix.rowMap().values()) { Graphs.addBiPath(g, row.values()); } Graphs.addBiPath(g, rightMatrix.column(0).values()); Graphs.addBiPath(g, rightMatrix.column(rightMatrix.columnKeySet().size() - 1).values()); Graphs.addPath(g, rightMatrix.get(RIGHT_CENTER_U_ROW, RIGHT_COL), leftMatrix.get(LEFT_CENTER_U_ROW, LEFT_COL)); Graphs.addPath(g, leftMatrix.get(LEFT_CENTER_L_ROW, LEFT_COL), rightMatrix.get(RIGHT_CENTER_L_ROW, RIGHT_COL)); return new ListenableGraph<>(g); } }
private Point findNearestDepot(Point from) { final Iterator<Point> it = depotLocations.iterator(); Point nearestDepot = it.next(); final double dist = Graphs.pathLength(roadModel.getShortestPathTo(from, nearestDepot)); while (it.hasNext()) { final Point cur = it.next(); final double d = Graphs.pathLength(roadModel.getShortestPathTo(from, cur)); if (d < dist) { nearestDepot = cur; } } return nearestDepot; } }
/** * Computes the shortest path based on the Euclidean distance. * @param graph The {@link Graph} on which the shortest path is searched. * @param from The start point of the path. * @param to The destination of the path. * @param <E> The type of connection data. * @return The shortest path that exists between <code>from</code> and * <code>to</code>. */ public static <E extends ConnectionData> List<Point> shortestPathEuclideanDistance( Graph<E> graph, final Point from, final Point to) { return Graphs.shortestPath(graph, from, to, GeomHeuristics.euclidean()); }
@Test(expected = PathNotFoundException.class) public void noShortestPath() { final Point from = new Point(0, 0); Graphs.addBiPath(graph, from, new Point(1, 0)); final Point to = new Point(10, 0); Graphs.addBiPath(graph, to, new Point(9, 0)); Graphs.shortestPathEuclideanDistance(graph, from, to); }
/** * Create a path of bi-directional connections on the specified {@link Graph} * using the specified {@link Point}s. If the points <code>A, B, C</code> are * specified, the four connections: <code>A -> B</code>, * <code>B -> A</code>, <code>B -> C</code> and <code>C -> B</code> * will be added to the graph. * @param graph The graph to which the connections will be added. * @param path Points that will be treated as a path. * @param <E> The type of connection data. */ public static <E extends ConnectionData> void addBiPath(Graph<E> graph, Iterable<Point> path) { addPath(graph, path); addPath(graph, reverse(newArrayList(path))); }
@Test public void unmodifiable2() { final Point N = new Point(0, 5); final Point E = new Point(5, 0); final Point S = new Point(0, -5); final Point W = new Point(-5, 0); Graphs.addBiPath(graph, N, E, S, W, N); final Graph<LengthData> unmod = Graphs.unmodifiableGraph(graph); graph.addConnection(N, S); assertEquals(graph.getConnection(N, S), unmod.getConnection(N, S)); }
/** * Uses the A* algorithm: * {@link com.github.rinde.rinsim.geom.Graphs#shortestPathEuclideanDistance}. * This method can optionally be overridden by subclasses to define another * shortest path algorithm. * @param from The start point of the path. * @param to The end point of the path. * @return The shortest path. */ protected List<Point> doGetShortestPathTo(Point from, Point to) { return shortestPathEuclideanDistance(graph, from, to); }
/** * @return An unmodifiable view on the graph. */ @Override public Graph<? extends ConnectionData> getGraph() { return unmodifiableGraph(graph); }
/** * Searches the closest <code>n</code> objects to position <code>pos</code> in * collection <code>objects</code>. * @param pos The {@link Point} which is used as a reference point. * @param rm The {@link RoadModel} instance which is used to lookup the * positions of the objects in <code>objects</code>. * @param objects The list of objects which is searched. * @param n The maximum number of objects to return where n must be ≥ 0. * @param <T> The type of the objects in the returned collection. * @return A list of objects that are closest to <code>pos</code>. The list is * ordered such that the closest object appears first. An empty list * is returned when <code>objects</code> is empty. */ public static <T extends RoadUser> List<T> findClosestObjects(Point pos, RoadModel rm, Collection<T> objects, int n) { return Graphs.findClosestObjects(pos, objects, new RoadModels.RoadUserToPositionFunction<T>(rm), n); }
@Test public void testMultimapGraphConstructor() { final RandomGenerator rnd = new MersenneTwister(123); final List<Point> path = new ArrayList<Point>(); for (int i = 0; i < 20; i++) { path.add(new Point(rnd.nextInt(50), rnd.nextInt(50))); } Graphs.addBiPath(graph, path.toArray(new Point[path.size()])); final MultimapGraph<LengthData> testGraph = new MultimapGraph<>(); testGraph.merge(graph); final MultimapGraph<LengthData> newGraph = new MultimapGraph<>( testGraph.getMultimap()); assertEquals(testGraph.getMultimap(), newGraph.getMultimap()); }
@Test public void shortestPathConsistencyCheck() { Point A, B, C, D; A = new Point(0, 0); B = new Point(0, 10); C = new Point(10, 10); D = new Point(10, 0); Graphs.addBiPath(graph, A, B, C, D, A); List<Point> prevPath = Graphs.shortestPathEuclideanDistance(graph, A, C); for (int i = 0; i < 100; i++) { final List<Point> newPath = Graphs.shortestPathEuclideanDistance(graph, A, C); assertEquals(prevPath, newPath); prevPath = newPath; } }
/** * Create a path of bi-directional connections on the specified {@link Graph} * using the specified {@link Point}s. If the points <code>A, B, C</code> are * specified, the four connections: <code>A -> B</code>, * <code>B -> A</code>, <code>B -> C</code> and <code>C -> B</code> * will be added to the graph. * @param graph The graph to which the connections will be added. * @param path Points that will be treated as a path. * @param <E> The type of connection data. */ public static <E extends ConnectionData> void addBiPath(Graph<E> graph, Iterable<Point> path) { addPath(graph, path); addPath(graph, reverse(newArrayList(path))); }
@Test public void removeNode() { final Point N = new Point(0, 5); final Point E = new Point(5, 0); final Point S = new Point(0, -5); final Point W = new Point(-5, 0); Graphs.addBiPath(graph, N, E, S, W, N); final Graph<LengthData> unmod = Graphs.unmodifiableGraph(graph); assertEquals(graph, unmod); assertEquals(4, graph.getNodes().size()); assertEquals(8, graph.getConnections().size()); graph.removeNode(N); assertEquals(graph, unmod); assertEquals(3, graph.getNodes().size()); assertEquals(4, graph.getConnections().size()); }
@Test(expected = IllegalArgumentException.class) public void shortestPathNull() { Graphs.shortestPathEuclideanDistance(graph, null, new Point(2, 3)); }
@Test(expected = UnsupportedOperationException.class) public void unmodAddConns() { Graphs.unmodifiableGraph(graph).addConnections(null); }
@Test public void closestObjectsTest() { final Function<Point, Point> f = new Function<Point, Point>() { @Override public Point apply(Point input) { return input; } }; final List<Point> points = Arrays.asList(new Point(10, 34), new Point(234, 2), new Point(10, 10), new Point(1, 1)); final List<Point> results = Graphs.findClosestObjects(new Point(0, 0), points, f, 2); assertEquals(results.size(), 2); assertEquals(new Point(1, 1), results.get(0)); assertEquals(new Point(10, 10), results.get(1)); final List<Point> results2 = Graphs.findClosestObjects(new Point(0, 0), points, f, 5); assertEquals(results2.size(), 4); assertEquals(new Point(1, 1), results2.get(0)); assertEquals(new Point(10, 10), results2.get(1)); assertEquals(new Point(10, 34), results2.get(2)); assertEquals(new Point(234, 2), results2.get(3)); }
static ListenableGraph<LengthData> createGraph() { final Graph<LengthData> g = new TableGraph<>(); final Table<Integer, Integer, Point> leftMatrix = createMatrix(5, 10, new Point(0, 0)); for (final Map<Integer, Point> column : leftMatrix.columnMap().values()) { Graphs.addBiPath(g, column.values()); } Graphs.addBiPath(g, leftMatrix.row(LEFT_CENTER_U_ROW).values()); Graphs.addBiPath(g, leftMatrix.row(LEFT_CENTER_L_ROW).values()); final Table<Integer, Integer, Point> rightMatrix = createMatrix(10, 7, new Point(30, 6)); for (final Map<Integer, Point> row : rightMatrix.rowMap().values()) { Graphs.addBiPath(g, row.values()); } Graphs.addBiPath(g, rightMatrix.column(0).values()); Graphs.addBiPath(g, rightMatrix.column(rightMatrix.columnKeySet().size() - 1).values()); Graphs.addPath(g, rightMatrix.get(RIGHT_CENTER_U_ROW, RIGHT_COL), leftMatrix.get(LEFT_CENTER_U_ROW, LEFT_COL)); Graphs.addPath(g, leftMatrix.get(LEFT_CENTER_L_ROW, LEFT_COL), rightMatrix.get(RIGHT_CENTER_L_ROW, RIGHT_COL)); return new ListenableGraph<>(g); } }
@Test public void setConnData() { final Point N = new Point(0, 5); final Point E = new Point(5, 0); final Point S = new Point(0, -5); final Point W = new Point(-5, 0); Graphs.addBiPath(graph, N, E, S, W, N); assertFalse(graph.setConnectionData(N, E, LengthData.create(100)) .isPresent()); assertEquals(LengthData.create(100), graph.removeConnectionData(N, E).get()); }
/** * The shortest path changes based on the connection data. */ @Test public void shortestPathConnData() { final Point a = new Point(0, 0); final Point b = new Point(10, 0); final Point c = new Point(5, 5); Graphs.addBiPath(graph, a, b, c, a); assertEquals(asList(a, b), Graphs.shortestPathEuclideanDistance(graph, a, b)); graph.setConnectionData(a, c, LengthData.create(1d)); graph.setConnectionData(c, b, LengthData.create(1d)); assertEquals(asList(a, c, b), Graphs.shortestPathEuclideanDistance(graph, a, b)); }