From d68a7e5ca93c679ce46f66610a018bb7f3850909 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Wed, 29 Apr 2020 17:10:29 +0200 Subject: [PATCH] Improved tests organisation --- .../shortestpath/AStarAlgorithm.java | 12 +- .../graphs/algorithm/utils/AStarTest.java | 21 +- .../insa/graphs/algorithm/utils/AlgoTest.java | 243 +++++++++++++----- .../graphs/algorithm/utils/DijkstraTest.java | 20 +- 4 files changed, 200 insertions(+), 96 deletions(-) diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java index c425177..329cc0f 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java @@ -1,6 +1,10 @@ package org.insa.graphs.algorithm.shortestpath; +import org.insa.graphs.algorithm.AbstractInputData; import org.insa.graphs.model.Graph; +import org.insa.graphs.model.Node; +import org.insa.graphs.model.Point; +import org.insa.graphs.model.RoadInformation; public class AStarAlgorithm extends DijkstraAlgorithm { @@ -16,10 +20,14 @@ public class AStarAlgorithm extends DijkstraAlgorithm { LabelStar[] labels = new LabelStar[nbNodes]; for (int i = 0; i < nbNodes; i++) { labels[i] = new LabelStar(i); - final double estimatedCost = graph.getNodes().get(i).getPoint().distanceTo( + final double distance = graph.getNodes().get(i).getPoint().distanceTo( data.getDestination().getPoint() ); - labels[i].setEstimatedCost(estimatedCost); + final double maxSpeed = data.getMaximumSpeed(); + double estimatedCost = distance; + if (data.getMode() == ShortestPathData.Mode.TIME) + estimatedCost /= maxSpeed; + labels[i].setEstimatedCost(estimatedCost); } return this.doDijkstra(labels, data, graph); } diff --git a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AStarTest.java b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AStarTest.java index 9bd6516..7e43587 100644 --- a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AStarTest.java +++ b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AStarTest.java @@ -1,29 +1,20 @@ package org.insa.graphs.algorithm.utils; import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm; -import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm; -import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm; -import org.insa.graphs.algorithm.shortestpath.ShortestPathData; - -import java.util.ArrayList; public class AStarTest extends AlgoTest { protected void computeValidPathSolutions() { - this.validPathSolutionList = new ArrayList<>(); - validPathOptimalSolutionList = new ArrayList<>(); - for (ShortestPathData path : validPathList) { - validPathSolutionList.add(new AStarAlgorithm(path).run()); - validPathOptimalSolutionList.add(new BellmanFordAlgorithm(path).run()); + for (PathDataset data : validTestData) { + data.setSolutionInLength(new AStarAlgorithm(data.getPathDataLength()).run()); + data.setSolutionInTime(new AStarAlgorithm(data.getPathDataTime()).run()); } } protected void computeInvalidPathSolutions() { - invalidPathSolutionList = new ArrayList<>(); - invalidPathOptimalSolutionList = new ArrayList<>(); - for (ShortestPathData path : invalidPathList) { - invalidPathSolutionList.add(new AStarAlgorithm(path).run()); - invalidPathOptimalSolutionList.add(new BellmanFordAlgorithm(path).run()); + for (PathDataset data : invalidTestData) { + data.setSolutionInLength(new AStarAlgorithm(data.getPathDataLength()).run()); + data.setSolutionInTime(new AStarAlgorithm(data.getPathDataTime()).run()); } } diff --git a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AlgoTest.java b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AlgoTest.java index 5c39f42..7f2819d 100644 --- a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AlgoTest.java +++ b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AlgoTest.java @@ -2,9 +2,11 @@ package org.insa.graphs.algorithm.utils; import org.insa.graphs.algorithm.ArcInspector; import org.insa.graphs.algorithm.ArcInspectorFactory; +import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm; import org.insa.graphs.algorithm.shortestpath.ShortestPathData; import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution; import org.insa.graphs.model.Graph; +import org.insa.graphs.model.Node; import org.insa.graphs.model.io.BinaryGraphReader; import org.insa.graphs.model.io.GraphReader; import org.junit.Before; @@ -19,90 +21,160 @@ import java.util.ArrayList; import static org.junit.Assert.*; public abstract class AlgoTest { + private static ArcInspector filterLength, filterTime; + protected ArrayList validTestData; + protected ArrayList invalidTestData; - protected ArrayList validPathList; - protected ArrayList validPathSolutionList; - protected ArrayList validPathOptimalSolutionList; - - protected ArrayList invalidPathList; - protected ArrayList invalidPathSolutionList; - protected ArrayList invalidPathOptimalSolutionList; - - // List of graphs - @SuppressWarnings("unused") + // Graphs to use private Graph graphInsa, graphSquare, graphBretagne; + /** + * Class used to store all data related to a path and its computed solutions + */ + class PathDataset { + + ShortestPathData pathDataTime; + ShortestPathData pathDataLength; + + ShortestPathSolution solutionInTime; + ShortestPathSolution solutionInLength; + + ShortestPathSolution optimalSolutionInTime; + ShortestPathSolution optimalSolutionInLength; + + + PathDataset(Graph graph, Node origin, Node destination) { + this.pathDataTime = new ShortestPathData(graph, origin, destination, filterTime); + this.pathDataLength = new ShortestPathData(graph, origin, destination, filterLength); + } + + public ShortestPathData getPathDataTime() { + return pathDataTime; + } + + public ShortestPathData getPathDataLength() { + return pathDataLength; + } + + /** + * Gets all computed solutions with different filters + * @return An array of computed solutions + */ + public ArrayList getComputedSolutions() { + return new ArrayList() {{ + add(solutionInTime); + add(solutionInLength); + }}; + } + + /** + * Gets all optimal solutions with different filters + * @return An array of optimal solutions + */ + public ArrayList getOptimalSolutions() { + return new ArrayList() {{ + add(optimalSolutionInTime); + add(optimalSolutionInLength); + }}; + } + + /** + * Gets the initial path data used to compute solutions with different filters + * @return An array of initial path data + */ + public ArrayList getPathData() { + return new ArrayList() {{ + add(pathDataTime); + add(pathDataLength); + }}; + } + + public void setSolutionInLength(ShortestPathSolution solutionInLength) { + this.solutionInLength = solutionInLength; + } + + public void setSolutionInTime(ShortestPathSolution solutionInTime) { + this.solutionInTime = solutionInTime; + } + + public void setOptimalSolutionInLength(ShortestPathSolution solutionInLength) { + this.optimalSolutionInLength = solutionInLength; + } + + public void setOptimalSolutionInTime(ShortestPathSolution solutionInTime) { + this.optimalSolutionInTime = solutionInTime; + } + + } + + /** + * Opens and reads the given graph + * + * @param map The map to open + * @return The graph opened + * @throws IOException + */ private Graph readGraph(String map) throws IOException { final GraphReader reader = new BinaryGraphReader( new DataInputStream(new BufferedInputStream(new FileInputStream(map)))); return reader.read(); } + /** + * Loads all graphs for later use + * @throws IOException + */ private void initGraphs() throws IOException { graphInsa = readGraph("../Maps/insa.mapgr"); graphSquare = readGraph("../Maps/carre.mapgr"); graphBretagne = readGraph("../Maps/bretagne.mapgr"); } + /** + * Loads all filters for later use + */ private void initFilters() { filterLength = ArcInspectorFactory.getAllFilters().get(0); filterTime = ArcInspectorFactory.getAllFilters().get(2); } + /** + * Creates valid data sets + */ private void initValidPathList() { - validPathList = new ArrayList<>(); + validTestData = new ArrayList<>(); - validPathList.add(new ShortestPathData( + validTestData.add(new PathDataset( graphInsa, graphInsa.getNodes().get(512), - graphInsa.getNodes().get(526), - filterLength)); - validPathList.add(new ShortestPathData( - graphInsa, - graphInsa.getNodes().get(512), - graphInsa.getNodes().get(526), - filterTime)); - validPathList.add(new ShortestPathData( + graphInsa.getNodes().get(526))); + validTestData.add(new PathDataset( graphSquare, graphSquare.getNodes().get(21), - graphSquare.getNodes().get(17), - filterLength)); - validPathList.add(new ShortestPathData( - graphSquare, - graphSquare.getNodes().get(21), - graphSquare.getNodes().get(17), - filterTime)); + graphSquare.getNodes().get(17))); } + /** + * Creates invalid data sets + */ private void initInvalidPathList() { - invalidPathList = new ArrayList<>(); + invalidTestData = new ArrayList<>(); - invalidPathList.add(new ShortestPathData( + invalidTestData.add(new PathDataset( graphBretagne, graphBretagne.getNodes().get(29270), - graphBretagne.getNodes().get(545599), - filterLength)); + graphBretagne.getNodes().get(545599))); - invalidPathList.add(new ShortestPathData( - graphBretagne, - graphBretagne.getNodes().get(29270), - graphBretagne.getNodes().get(545599), - filterTime)); - - invalidPathList.add(new ShortestPathData( + invalidTestData.add(new PathDataset( graphSquare, graphSquare.getNodes().get(10), - graphSquare.getNodes().get(10), - filterLength)); - - invalidPathList.add(new ShortestPathData( - graphSquare, - graphSquare.getNodes().get(10), - graphSquare.getNodes().get(10), - filterTime)); + graphSquare.getNodes().get(10))); } + /** + * Init all tests data before starting + */ @Before public void init() { initFilters(); @@ -114,45 +186,86 @@ public abstract class AlgoTest { initValidPathList(); initInvalidPathList(); + computeOptimalPathSolutions(); computeValidPathSolutions(); computeInvalidPathSolutions(); } + /** + * Computes optimal solutions for all tests + */ + private void computeOptimalPathSolutions() { + for (PathDataset data : validTestData) { + data.setOptimalSolutionInLength(new BellmanFordAlgorithm(data.getPathDataLength()).run()); + data.setOptimalSolutionInTime(new BellmanFordAlgorithm(data.getPathDataTime()).run()); + } + } + + /** + * Computes valid path using the chosen algorithm + */ protected abstract void computeValidPathSolutions(); + /** + * Computes invalid paths using the chosen algorithm + */ protected abstract void computeInvalidPathSolutions(); @Test public void testPathValid() { - for (ShortestPathSolution solution : validPathSolutionList) { - assertTrue(solution.isFeasible()); - assertTrue(solution.getPath().isValid()); + for (PathDataset data: validTestData) { + for (ShortestPathSolution solution : data.getComputedSolutions()) { + assertTrue(solution.isFeasible()); + assertTrue(solution.getPath().isValid()); + } } } @Test public void testPathInvalid() { - for (ShortestPathSolution solution : invalidPathSolutionList) { - assertFalse(solution.isFeasible()); + for (PathDataset data: invalidTestData) { + for (ShortestPathSolution solution : data.getComputedSolutions()) { + assertFalse(solution.isFeasible()); + } + } + } + + @Test + public void testEndNodes() { + for (PathDataset data: validTestData) { + ArrayList initialData = data.getPathData(); + ArrayList solutions = data.getComputedSolutions(); + for (int i = 0; i < solutions.size(); i++) { + assertEquals( + initialData.get(i).getOrigin().getId(), + solutions.get(i).getPath().getOrigin().getId() + ); + assertEquals( + initialData.get(i).getDestination().getId(), + solutions.get(i).getPath().getDestination().getId() + ); + } + } } @Test public void testPathOptimalWithOracle() { - for (int i = 0; i < validPathSolutionList.size(); i++) { - System.out.println(i); - assertEquals( - validPathSolutionList.get(i).getPath().getLength(), - validPathOptimalSolutionList.get(i).getPath().getLength(), 0.0 - ); - assertEquals( - validPathSolutionList.get(i).getPath().getMinimumTravelTime(), - validPathOptimalSolutionList.get(i).getPath().getMinimumTravelTime(), 0.0 - ); - } - for (int i = 0; i < invalidPathSolutionList.size(); i++) { - assertEquals(validPathSolutionList.get(i).isFeasible(), - validPathOptimalSolutionList.get(i).isFeasible()); + for (PathDataset data: validTestData) { + ArrayList optimalSolutions = data.getOptimalSolutions(); + ArrayList computedSolutions = data.getComputedSolutions(); + for (int i = 0; i < computedSolutions.size(); i++) { + assertEquals( + optimalSolutions.get(i).getPath().getLength(), + computedSolutions.get(i).getPath().getLength(), + 0.0 + ); + assertEquals( + optimalSolutions.get(i).getPath().getMinimumTravelTime(), + computedSolutions.get(i).getPath().getMinimumTravelTime(), + 0.0 + ); + } } } diff --git a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/DijkstraTest.java b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/DijkstraTest.java index b23b77f..f4beb32 100644 --- a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/DijkstraTest.java +++ b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/DijkstraTest.java @@ -1,28 +1,20 @@ package org.insa.graphs.algorithm.utils; -import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm; import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm; -import org.insa.graphs.algorithm.shortestpath.ShortestPathData; - -import java.util.ArrayList; public class DijkstraTest extends AlgoTest { protected void computeValidPathSolutions() { - this.validPathSolutionList = new ArrayList<>(); - validPathOptimalSolutionList = new ArrayList<>(); - for (ShortestPathData path : validPathList) { - validPathSolutionList.add(new DijkstraAlgorithm(path).run()); - validPathOptimalSolutionList.add(new BellmanFordAlgorithm(path).run()); + for (PathDataset data : validTestData) { + data.setSolutionInLength(new DijkstraAlgorithm(data.getPathDataLength()).run()); + data.setSolutionInTime(new DijkstraAlgorithm(data.getPathDataTime()).run()); } } protected void computeInvalidPathSolutions() { - invalidPathSolutionList = new ArrayList<>(); - invalidPathOptimalSolutionList = new ArrayList<>(); - for (ShortestPathData path : invalidPathList) { - invalidPathSolutionList.add(new DijkstraAlgorithm(path).run()); - invalidPathOptimalSolutionList.add(new BellmanFordAlgorithm(path).run()); + for (PathDataset data : invalidTestData) { + data.setSolutionInLength(new DijkstraAlgorithm(data.getPathDataLength()).run()); + data.setSolutionInTime(new DijkstraAlgorithm(data.getPathDataTime()).run()); } }