Implemented dijkstra unit tests
This commit is contained in:
parent
72a3a840ff
commit
b57065913e
3 changed files with 186 additions and 6 deletions
|
@ -20,7 +20,7 @@ public class ArcInspectorFactory {
|
|||
|
||||
// Common filters:
|
||||
|
||||
// No filter (all arcs allowed):
|
||||
// No filter (all arcs allowed) using length:
|
||||
filters.add(new ArcInspector() {
|
||||
@Override
|
||||
public boolean isAllowed(Arc arc) {
|
||||
|
@ -78,8 +78,7 @@ public class ArcInspectorFactory {
|
|||
}
|
||||
});
|
||||
|
||||
// Only road allowed for cars and time:
|
||||
|
||||
// No filter (all arcs allowed) using time:
|
||||
filters.add(new ArcInspector() {
|
||||
@Override
|
||||
public boolean isAllowed(Arc arc) {
|
||||
|
@ -107,6 +106,7 @@ public class ArcInspectorFactory {
|
|||
}
|
||||
});
|
||||
|
||||
// Only road allowed for cars and time:
|
||||
filters.add(new ArcInspector() {
|
||||
@Override
|
||||
public boolean isAllowed(Arc arc) {
|
||||
|
|
|
@ -17,7 +17,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
super(data);
|
||||
}
|
||||
|
||||
final private boolean DEBUG = true;
|
||||
final private boolean DEBUG = false;
|
||||
|
||||
@Override
|
||||
protected ShortestPathSolution doRun() {
|
||||
|
@ -63,7 +63,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
}
|
||||
}
|
||||
this.log("===============");
|
||||
ShortestPathSolution solution = null;
|
||||
ShortestPathSolution solution;
|
||||
// Destination has no predecessor, the solution is infeasible...
|
||||
if (labels[destination].getFatherArc() == null) {
|
||||
solution = new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
|
||||
|
@ -84,8 +84,8 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
|
||||
// Create the final solution.
|
||||
solution = new ShortestPathSolution(data, AbstractSolution.Status.OPTIMAL, new Path(graph, arcs));
|
||||
this.log("Nb arcs solution : " + solution.getPath().getArcs().size());
|
||||
}
|
||||
this.log("Nb arcs solution : " + solution.getPath().getArcs().size());
|
||||
this.log("Nb itérations solution: " + iterationCounter);
|
||||
return solution;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,180 @@
|
|||
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.DijkstraAlgorithm;
|
||||
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.io.BinaryGraphReader;
|
||||
import org.insa.graphs.model.io.GraphReader;
|
||||
import org.junit.Assume;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class DijkstraTest {
|
||||
|
||||
private static ArcInspector filterLength, filterTime;
|
||||
|
||||
private static ArrayList<ShortestPathData> validPathList;
|
||||
private static ArrayList<ShortestPathSolution> validPathSolutionList;
|
||||
private static ArrayList<ShortestPathSolution> validPathOptimalSolutionList;
|
||||
|
||||
private static ArrayList<ShortestPathData> invalidPathList;
|
||||
private static ArrayList<ShortestPathSolution> invalidPathSolutionList;
|
||||
private static ArrayList<ShortestPathSolution> invalidPathOptimalSolutionList;
|
||||
|
||||
// List of graphs
|
||||
@SuppressWarnings("unused")
|
||||
private static Graph graphInsa, graphSquare, graphBretagne;
|
||||
|
||||
private static Graph readGraph(String map) throws IOException {
|
||||
final GraphReader reader = new BinaryGraphReader(
|
||||
new DataInputStream(new BufferedInputStream(new FileInputStream(map))));
|
||||
return reader.read();
|
||||
}
|
||||
|
||||
private static void initGraphs() throws IOException {
|
||||
graphInsa = readGraph("../Maps/insa.mapgr");
|
||||
graphSquare = readGraph("../Maps/carre.mapgr");
|
||||
graphBretagne = readGraph("../Maps/bretagne.mapgr");
|
||||
}
|
||||
|
||||
private static void initFilters() {
|
||||
filterLength = ArcInspectorFactory.getAllFilters().get(0);
|
||||
filterTime = ArcInspectorFactory.getAllFilters().get(2);
|
||||
}
|
||||
|
||||
private static void initValidPathList() {
|
||||
validPathList = new ArrayList<>();
|
||||
|
||||
validPathList.add(new ShortestPathData(
|
||||
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(
|
||||
graphSquare,
|
||||
graphSquare.getNodes().get(21),
|
||||
graphSquare.getNodes().get(17),
|
||||
filterLength));
|
||||
validPathList.add(new ShortestPathData(
|
||||
graphSquare,
|
||||
graphSquare.getNodes().get(21),
|
||||
graphSquare.getNodes().get(17),
|
||||
filterTime));
|
||||
}
|
||||
|
||||
private static void initInvalidPathList() {
|
||||
invalidPathList = new ArrayList<>();
|
||||
|
||||
invalidPathList.add(new ShortestPathData(
|
||||
graphBretagne,
|
||||
graphBretagne.getNodes().get(29270),
|
||||
graphBretagne.getNodes().get(545599),
|
||||
filterLength));
|
||||
|
||||
invalidPathList.add(new ShortestPathData(
|
||||
graphBretagne,
|
||||
graphBretagne.getNodes().get(29270),
|
||||
graphBretagne.getNodes().get(545599),
|
||||
filterTime));
|
||||
|
||||
invalidPathList.add(new ShortestPathData(
|
||||
graphSquare,
|
||||
graphSquare.getNodes().get(10),
|
||||
graphSquare.getNodes().get(10),
|
||||
filterLength));
|
||||
|
||||
invalidPathList.add(new ShortestPathData(
|
||||
graphSquare,
|
||||
graphSquare.getNodes().get(10),
|
||||
graphSquare.getNodes().get(10),
|
||||
filterTime));
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void initAll() {
|
||||
initFilters();
|
||||
try {
|
||||
initGraphs();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
initValidPathList();
|
||||
initInvalidPathList();
|
||||
|
||||
computeValidPathSolutions();
|
||||
computeInvalidPathSolutions();
|
||||
}
|
||||
|
||||
private static void computeValidPathSolutions() {
|
||||
validPathSolutionList = new ArrayList<>();
|
||||
validPathOptimalSolutionList = new ArrayList<>();
|
||||
for (ShortestPathData path : validPathList) {
|
||||
validPathSolutionList.add(new DijkstraAlgorithm(path).run());
|
||||
validPathOptimalSolutionList.add(new BellmanFordAlgorithm(path).run());
|
||||
}
|
||||
}
|
||||
|
||||
private static void computeInvalidPathSolutions() {
|
||||
invalidPathSolutionList = new ArrayList<>();
|
||||
invalidPathOptimalSolutionList = new ArrayList<>();
|
||||
for (ShortestPathData path : invalidPathList) {
|
||||
invalidPathSolutionList.add(new DijkstraAlgorithm(path).run());
|
||||
invalidPathOptimalSolutionList.add(new BellmanFordAlgorithm(path).run());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPathValid() {
|
||||
for (ShortestPathSolution solution : validPathSolutionList) {
|
||||
assertTrue(solution.isFeasible());
|
||||
assertTrue(solution.getPath().isValid());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPathInvalid() {
|
||||
for (ShortestPathSolution solution : invalidPathSolutionList) {
|
||||
assertFalse(solution.isFeasible());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPathOptimalWithOracle() {
|
||||
for (int i = 0; i < validPathSolutionList.size(); 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());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPathOptimalWithoutOracle() {
|
||||
// TODO
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue