Improved tests organisation

This commit is contained in:
Arnaud Vergnet 2020-04-29 17:10:29 +02:00
parent f658b678af
commit d68a7e5ca9
4 changed files with 200 additions and 96 deletions

View file

@ -1,6 +1,10 @@
package org.insa.graphs.algorithm.shortestpath; package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.algorithm.AbstractInputData;
import org.insa.graphs.model.Graph; 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 { public class AStarAlgorithm extends DijkstraAlgorithm {
@ -16,10 +20,14 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
LabelStar[] labels = new LabelStar[nbNodes]; LabelStar[] labels = new LabelStar[nbNodes];
for (int i = 0; i < nbNodes; i++) { for (int i = 0; i < nbNodes; i++) {
labels[i] = new LabelStar(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() 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); return this.doDijkstra(labels, data, graph);
} }

View file

@ -1,29 +1,20 @@
package org.insa.graphs.algorithm.utils; package org.insa.graphs.algorithm.utils;
import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm; 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 { public class AStarTest extends AlgoTest {
protected void computeValidPathSolutions() { protected void computeValidPathSolutions() {
this.validPathSolutionList = new ArrayList<>(); for (PathDataset data : validTestData) {
validPathOptimalSolutionList = new ArrayList<>(); data.setSolutionInLength(new AStarAlgorithm(data.getPathDataLength()).run());
for (ShortestPathData path : validPathList) { data.setSolutionInTime(new AStarAlgorithm(data.getPathDataTime()).run());
validPathSolutionList.add(new AStarAlgorithm(path).run());
validPathOptimalSolutionList.add(new BellmanFordAlgorithm(path).run());
} }
} }
protected void computeInvalidPathSolutions() { protected void computeInvalidPathSolutions() {
invalidPathSolutionList = new ArrayList<>(); for (PathDataset data : invalidTestData) {
invalidPathOptimalSolutionList = new ArrayList<>(); data.setSolutionInLength(new AStarAlgorithm(data.getPathDataLength()).run());
for (ShortestPathData path : invalidPathList) { data.setSolutionInTime(new AStarAlgorithm(data.getPathDataTime()).run());
invalidPathSolutionList.add(new AStarAlgorithm(path).run());
invalidPathOptimalSolutionList.add(new BellmanFordAlgorithm(path).run());
} }
} }

View file

@ -2,9 +2,11 @@ package org.insa.graphs.algorithm.utils;
import org.insa.graphs.algorithm.ArcInspector; import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.algorithm.ArcInspectorFactory; 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.ShortestPathData;
import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution; import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
import org.insa.graphs.model.Graph; 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.BinaryGraphReader;
import org.insa.graphs.model.io.GraphReader; import org.insa.graphs.model.io.GraphReader;
import org.junit.Before; import org.junit.Before;
@ -19,90 +21,160 @@ import java.util.ArrayList;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public abstract class AlgoTest { public abstract class AlgoTest {
private static ArcInspector filterLength, filterTime; private static ArcInspector filterLength, filterTime;
protected ArrayList<PathDataset> validTestData;
protected ArrayList<PathDataset> invalidTestData;
protected ArrayList<ShortestPathData> validPathList; // Graphs to use
protected ArrayList<ShortestPathSolution> validPathSolutionList;
protected ArrayList<ShortestPathSolution> validPathOptimalSolutionList;
protected ArrayList<ShortestPathData> invalidPathList;
protected ArrayList<ShortestPathSolution> invalidPathSolutionList;
protected ArrayList<ShortestPathSolution> invalidPathOptimalSolutionList;
// List of graphs
@SuppressWarnings("unused")
private Graph graphInsa, graphSquare, graphBretagne; 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<ShortestPathSolution> getComputedSolutions() {
return new ArrayList<ShortestPathSolution>() {{
add(solutionInTime);
add(solutionInLength);
}};
}
/**
* Gets all optimal solutions with different filters
* @return An array of optimal solutions
*/
public ArrayList<ShortestPathSolution> getOptimalSolutions() {
return new ArrayList<ShortestPathSolution>() {{
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<ShortestPathData> getPathData() {
return new ArrayList<ShortestPathData>() {{
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 { private Graph readGraph(String map) throws IOException {
final GraphReader reader = new BinaryGraphReader( final GraphReader reader = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(map)))); new DataInputStream(new BufferedInputStream(new FileInputStream(map))));
return reader.read(); return reader.read();
} }
/**
* Loads all graphs for later use
* @throws IOException
*/
private void initGraphs() throws IOException { private void initGraphs() throws IOException {
graphInsa = readGraph("../Maps/insa.mapgr"); graphInsa = readGraph("../Maps/insa.mapgr");
graphSquare = readGraph("../Maps/carre.mapgr"); graphSquare = readGraph("../Maps/carre.mapgr");
graphBretagne = readGraph("../Maps/bretagne.mapgr"); graphBretagne = readGraph("../Maps/bretagne.mapgr");
} }
/**
* Loads all filters for later use
*/
private void initFilters() { private void initFilters() {
filterLength = ArcInspectorFactory.getAllFilters().get(0); filterLength = ArcInspectorFactory.getAllFilters().get(0);
filterTime = ArcInspectorFactory.getAllFilters().get(2); filterTime = ArcInspectorFactory.getAllFilters().get(2);
} }
/**
* Creates valid data sets
*/
private void initValidPathList() { private void initValidPathList() {
validPathList = new ArrayList<>(); validTestData = new ArrayList<>();
validPathList.add(new ShortestPathData( validTestData.add(new PathDataset(
graphInsa, graphInsa,
graphInsa.getNodes().get(512), graphInsa.getNodes().get(512),
graphInsa.getNodes().get(526), graphInsa.getNodes().get(526)));
filterLength)); validTestData.add(new PathDataset(
validPathList.add(new ShortestPathData(
graphInsa,
graphInsa.getNodes().get(512),
graphInsa.getNodes().get(526),
filterTime));
validPathList.add(new ShortestPathData(
graphSquare, graphSquare,
graphSquare.getNodes().get(21), graphSquare.getNodes().get(21),
graphSquare.getNodes().get(17), graphSquare.getNodes().get(17)));
filterLength));
validPathList.add(new ShortestPathData(
graphSquare,
graphSquare.getNodes().get(21),
graphSquare.getNodes().get(17),
filterTime));
} }
/**
* Creates invalid data sets
*/
private void initInvalidPathList() { private void initInvalidPathList() {
invalidPathList = new ArrayList<>(); invalidTestData = new ArrayList<>();
invalidPathList.add(new ShortestPathData( invalidTestData.add(new PathDataset(
graphBretagne, graphBretagne,
graphBretagne.getNodes().get(29270), graphBretagne.getNodes().get(29270),
graphBretagne.getNodes().get(545599), graphBretagne.getNodes().get(545599)));
filterLength));
invalidPathList.add(new ShortestPathData( invalidTestData.add(new PathDataset(
graphBretagne,
graphBretagne.getNodes().get(29270),
graphBretagne.getNodes().get(545599),
filterTime));
invalidPathList.add(new ShortestPathData(
graphSquare, graphSquare,
graphSquare.getNodes().get(10), graphSquare.getNodes().get(10),
graphSquare.getNodes().get(10), graphSquare.getNodes().get(10)));
filterLength));
invalidPathList.add(new ShortestPathData(
graphSquare,
graphSquare.getNodes().get(10),
graphSquare.getNodes().get(10),
filterTime));
} }
/**
* Init all tests data before starting
*/
@Before @Before
public void init() { public void init() {
initFilters(); initFilters();
@ -114,45 +186,86 @@ public abstract class AlgoTest {
initValidPathList(); initValidPathList();
initInvalidPathList(); initInvalidPathList();
computeOptimalPathSolutions();
computeValidPathSolutions(); computeValidPathSolutions();
computeInvalidPathSolutions(); 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(); protected abstract void computeValidPathSolutions();
/**
* Computes invalid paths using the chosen algorithm
*/
protected abstract void computeInvalidPathSolutions(); protected abstract void computeInvalidPathSolutions();
@Test @Test
public void testPathValid() { public void testPathValid() {
for (ShortestPathSolution solution : validPathSolutionList) { for (PathDataset data: validTestData) {
assertTrue(solution.isFeasible()); for (ShortestPathSolution solution : data.getComputedSolutions()) {
assertTrue(solution.getPath().isValid()); assertTrue(solution.isFeasible());
assertTrue(solution.getPath().isValid());
}
} }
} }
@Test @Test
public void testPathInvalid() { public void testPathInvalid() {
for (ShortestPathSolution solution : invalidPathSolutionList) { for (PathDataset data: invalidTestData) {
assertFalse(solution.isFeasible()); for (ShortestPathSolution solution : data.getComputedSolutions()) {
assertFalse(solution.isFeasible());
}
}
}
@Test
public void testEndNodes() {
for (PathDataset data: validTestData) {
ArrayList<ShortestPathData> initialData = data.getPathData();
ArrayList<ShortestPathSolution> 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 @Test
public void testPathOptimalWithOracle() { public void testPathOptimalWithOracle() {
for (int i = 0; i < validPathSolutionList.size(); i++) { for (PathDataset data: validTestData) {
System.out.println(i); ArrayList<ShortestPathSolution> optimalSolutions = data.getOptimalSolutions();
assertEquals( ArrayList<ShortestPathSolution> computedSolutions = data.getComputedSolutions();
validPathSolutionList.get(i).getPath().getLength(), for (int i = 0; i < computedSolutions.size(); i++) {
validPathOptimalSolutionList.get(i).getPath().getLength(), 0.0 assertEquals(
); optimalSolutions.get(i).getPath().getLength(),
assertEquals( computedSolutions.get(i).getPath().getLength(),
validPathSolutionList.get(i).getPath().getMinimumTravelTime(), 0.0
validPathOptimalSolutionList.get(i).getPath().getMinimumTravelTime(), 0.0 );
); assertEquals(
} optimalSolutions.get(i).getPath().getMinimumTravelTime(),
for (int i = 0; i < invalidPathSolutionList.size(); i++) { computedSolutions.get(i).getPath().getMinimumTravelTime(),
assertEquals(validPathSolutionList.get(i).isFeasible(), 0.0
validPathOptimalSolutionList.get(i).isFeasible()); );
}
} }
} }

View file

@ -1,28 +1,20 @@
package org.insa.graphs.algorithm.utils; 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.DijkstraAlgorithm;
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
import java.util.ArrayList;
public class DijkstraTest extends AlgoTest { public class DijkstraTest extends AlgoTest {
protected void computeValidPathSolutions() { protected void computeValidPathSolutions() {
this.validPathSolutionList = new ArrayList<>(); for (PathDataset data : validTestData) {
validPathOptimalSolutionList = new ArrayList<>(); data.setSolutionInLength(new DijkstraAlgorithm(data.getPathDataLength()).run());
for (ShortestPathData path : validPathList) { data.setSolutionInTime(new DijkstraAlgorithm(data.getPathDataTime()).run());
validPathSolutionList.add(new DijkstraAlgorithm(path).run());
validPathOptimalSolutionList.add(new BellmanFordAlgorithm(path).run());
} }
} }
protected void computeInvalidPathSolutions() { protected void computeInvalidPathSolutions() {
invalidPathSolutionList = new ArrayList<>(); for (PathDataset data : invalidTestData) {
invalidPathOptimalSolutionList = new ArrayList<>(); data.setSolutionInLength(new DijkstraAlgorithm(data.getPathDataLength()).run());
for (ShortestPathData path : invalidPathList) { data.setSolutionInTime(new DijkstraAlgorithm(data.getPathDataTime()).run());
invalidPathSolutionList.add(new DijkstraAlgorithm(path).run());
invalidPathOptimalSolutionList.add(new BellmanFordAlgorithm(path).run());
} }
} }