Improved tests organisation
This commit is contained in:
parent
f658b678af
commit
d68a7e5ca9
4 changed files with 200 additions and 96 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<PathDataset> validTestData;
|
||||
protected ArrayList<PathDataset> invalidTestData;
|
||||
|
||||
protected ArrayList<ShortestPathData> validPathList;
|
||||
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")
|
||||
// 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<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 {
|
||||
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<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
|
||||
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<ShortestPathSolution> optimalSolutions = data.getOptimalSolutions();
|
||||
ArrayList<ShortestPathSolution> 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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue