refactor(test): dijkstra bellman assertion

This commit is contained in:
Paul Alnet 2024-05-20 22:21:34 +02:00
parent e4f9a18f22
commit 5c248f26dc

View file

@ -53,7 +53,7 @@ public class DijkstraAlgorithmTest {
// 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. X // Read the graph.
graph.add(reader.read()); graph.add(reader.read());
// free resources // free resources
reader.close(); reader.close();
@ -75,6 +75,24 @@ public class DijkstraAlgorithmTest {
* 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);
}
@Test @Test
/* /*
* Map: carre.mapgr * Map: carre.mapgr
@ -84,22 +102,12 @@ 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); ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); // all arcs
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); assertBellmanHasSameResult(myGraph, origin, destination, arcInspector);
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);
} }
@Test @Test
@ -115,18 +123,7 @@ public class DijkstraAlgorithmTest {
Node origin = myGraph.get(15); Node origin = myGraph.get(15);
Node destination = myGraph.get(9); Node destination = myGraph.get(9);
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector); assertBellmanHasSameResult(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);
} }
@ -184,18 +181,8 @@ 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); assertBellmanHasSameResult(myGraph, origin, destination, arcInspector);
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);
} }
@Test @Test
@ -215,18 +202,8 @@ 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); assertBellmanHasSameResult(myGraph, origin, destination, arcInspector);
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()) < 10.0);
} }