Vertaa committeja

..

No commits in common. "f62e31215655364b742e039c291e182f42aba022" and "e4f9a18f22a2b737c96391348e7aef5bfe3e9bd6" have entirely different histories.

Näytä tiedosto

@ -48,11 +48,12 @@ public class DijkstraAlgorithmTest {
public static void init() { public static void init() {
try { try {
// Create the map // Create the map
for (String mapName : Maps) { for (int j = 0 ; j < Maps.size() ; j++) {
final String mapName = Maps.get(j);
// Create a graph reader // Create a graph reader
final GraphReader reader = new BinaryGraphReader( final GraphReader reader = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(mapName)))); new DataInputStream(new BufferedInputStream(new FileInputStream(mapName))));
// Read the graph. // Read the graph. X
graph.add(reader.read()); graph.add(reader.read());
// free resources // free resources
reader.close(); reader.close();
@ -73,37 +74,6 @@ public class DijkstraAlgorithmTest {
* Chemins courts testés par comparaison avec Bellman. * Chemins courts testés par comparaison avec Bellman.
* Chemin longs testés par comparaison avec chemins déjà construits. Bellman trop long. * Chemin longs testés par comparaison avec chemins déjà construits. Bellman trop long.
*/ */
/*
* Verifies that path is valid and mathes the one found by the Bellman algo
*/
private void assertBellmanHasSameResult(Graph graph, Node origin, Node destination, ArcInspector arcFilter) {
final ShortestPathData data = new ShortestPathData(graph, origin, destination, arcFilter);
final DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
final ShortestPathSolution dijk_path = dijkstra.doRun();
final BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data);
final ShortestPathSolution bell_path = bellman.doRun();
assert(dijk_path.getPath().isValid());
assert(dijk_path.isFeasible());
assert(Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getLength()) < 1.0);
assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0);
}
/*
* Verifies that no path is found
*/
private void assertNoPathFound(Graph graph, Node origin, Node destination, ArcInspector arcFilter) {
ShortestPathData data = new ShortestPathData(graph, origin, destination, arcFilter);
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
ShortestPathSolution dijk_path = dijkstra.doRun();
assert(!dijk_path.isFeasible());
assert(dijk_path.getPath() == null);
}
@Test @Test
/* /*
@ -114,12 +84,22 @@ public class DijkstraAlgorithmTest {
* PATH UTILISE : ../Paths/custom_paths/short_path_carre.path * PATH UTILISE : ../Paths/custom_paths/short_path_carre.path
*/ */
public void chemin_court_CARRE_length() { public void chemin_court_CARRE_length() {
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); // all arcs ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0);
Graph myGraph = graph.get(0); Graph myGraph = graph.get(0);
Node origin = myGraph.get(19); Node origin = myGraph.get(19);
Node destination = myGraph.get(4); Node destination = myGraph.get(4);
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
ShortestPathSolution dijk_path = dijkstra.doRun();
assertBellmanHasSameResult(myGraph, origin, destination, arcInspector); BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data);
ShortestPathSolution bell_path = bellman.doRun();
assert(dijk_path.getPath().isValid());
assert(dijk_path.isFeasible());
assert(Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getLength()) < 1.0);
assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0);
} }
@Test @Test
@ -135,7 +115,18 @@ public class DijkstraAlgorithmTest {
Node origin = myGraph.get(15); Node origin = myGraph.get(15);
Node destination = myGraph.get(9); Node destination = myGraph.get(9);
assertBellmanHasSameResult(myGraph, origin, destination, arcInspector); ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
ShortestPathSolution dijk_path = dijkstra.doRun();
BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data);
ShortestPathSolution bell_path = bellman.doRun();
assert(dijk_path.getPath().isValid());
assert(dijk_path.isFeasible());
assert(Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getLength()) < 1.0);
assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0);
} }
@ -150,8 +141,13 @@ public class DijkstraAlgorithmTest {
Graph myGraph = graph.get(0); Graph myGraph = graph.get(0);
Node origin = myGraph.get(3); Node origin = myGraph.get(3);
Node destination = myGraph.get(3); Node destination = myGraph.get(3);
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
ShortestPathSolution dijk_path = dijkstra.doRun();
assertNoPathFound(myGraph, origin, destination, arcInspector); assert(!dijk_path.isFeasible());
assert(dijk_path.getPath() == null);
} }
@Test @Test
@ -167,8 +163,12 @@ public class DijkstraAlgorithmTest {
Graph myGraph = graph.get(1); Graph myGraph = graph.get(1);
Node origin = myGraph.get(224); Node origin = myGraph.get(224);
Node destination = myGraph.get(814); Node destination = myGraph.get(814);
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
assertNoPathFound(myGraph, origin, destination, arcInspector);
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
ShortestPathSolution dijk_path = dijkstra.doRun();
assert(!dijk_path.isFeasible());
assert(dijk_path.getPath() == null);
} }
@Test @Test
@ -184,8 +184,18 @@ public class DijkstraAlgorithmTest {
Graph myGraph = graph.get(2); Graph myGraph = graph.get(2);
Node origin = myGraph.get(8423); Node origin = myGraph.get(8423);
Node destination = myGraph.get(8435); Node destination = myGraph.get(8435);
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
ShortestPathSolution dijk_path = dijkstra.doRun();
assertBellmanHasSameResult(myGraph, origin, destination, arcInspector); BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data);
ShortestPathSolution bell_path = bellman.doRun();
assert(dijk_path.getPath().isValid());
assert(dijk_path.isFeasible());
assert(Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getLength()) < 1.0);
assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0);
} }
@Test @Test
@ -205,8 +215,18 @@ public class DijkstraAlgorithmTest {
Graph myGraph = graph.get(2); Graph myGraph = graph.get(2);
Node origin = myGraph.get(16644); Node origin = myGraph.get(16644);
Node destination = myGraph.get(39229); Node destination = myGraph.get(39229);
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
ShortestPathSolution dijk_path = dijkstra.doRun();
assertBellmanHasSameResult(myGraph, origin, destination, arcInspector); BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data);
ShortestPathSolution bell_path = bellman.doRun();
assert(dijk_path.getPath().isValid());
assert(dijk_path.isFeasible());
assert((Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getLength()) < 1.0));
assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 10.0);
} }