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
final GraphReader reader = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(mapName))));
// Read the graph. X
// Read the graph.
graph.add(reader.read());
// free resources
reader.close();
@ -74,6 +74,24 @@ public class DijkstraAlgorithmTest {
* Chemins courts testés par comparaison avec Bellman.
* 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
/*
@ -84,22 +102,12 @@ public class DijkstraAlgorithmTest {
* PATH UTILISE : ../Paths/custom_paths/short_path_carre.path
*/
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);
Node origin = myGraph.get(19);
Node destination = myGraph.get(4);
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);
assertBellmanHasSameResult(myGraph, origin, destination, arcInspector);
}
@Test
@ -115,18 +123,7 @@ public class DijkstraAlgorithmTest {
Node origin = myGraph.get(15);
Node destination = myGraph.get(9);
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);
assertBellmanHasSameResult(myGraph, origin, destination, arcInspector);
}
@ -184,18 +181,8 @@ public class DijkstraAlgorithmTest {
Graph myGraph = graph.get(2);
Node origin = myGraph.get(8423);
Node destination = myGraph.get(8435);
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);
assertBellmanHasSameResult(myGraph, origin, destination, arcInspector);
}
@Test
@ -215,18 +202,8 @@ public class DijkstraAlgorithmTest {
Graph myGraph = graph.get(2);
Node origin = myGraph.get(16644);
Node destination = myGraph.get(39229);
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()) < 10.0);
assertBellmanHasSameResult(myGraph, origin, destination, arcInspector);
}