Added 2 tests: comparision with dijkstra in time and length modes

This commit is contained in:
Clement Lacau 2024-05-21 01:06:24 +02:00
parent 8e111e2bbf
commit 76e734919c

View file

@ -1,5 +1,12 @@
package org.insa.graphs.algorithm.shortestpath; package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.algorithm.ArcInspectorFactory;
import org.insa.graphs.algorithm.AbstractInputData.Mode;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
import org.junit.Test;
public class AStarAlgorithmTest extends ShortestPathAlgorithmTest { public class AStarAlgorithmTest extends ShortestPathAlgorithmTest {
public AStarAlgorithmTest() { public AStarAlgorithmTest() {
@ -11,4 +18,70 @@ public class AStarAlgorithmTest extends ShortestPathAlgorithmTest {
return new AStarAlgorithm(data); return new AStarAlgorithm(data);
} }
/*
* Verifies that both paths are valid and Dijkstra path matches AStar path
* Applied either for LENGTH or TIME mode.
*/
private void assertDijkstraAStarHaveSameResult(Graph graph, Node origin, Node destination, ArcInspector arcFilter) {
final ShortestPathData data = new ShortestPathData(graph, origin, destination, arcFilter);
final ShortestPathAlgorithm Astar = initAlgo(data);
final ShortestPathSolution Astar_path = Astar.doRun();
final DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
final ShortestPathSolution dijkstra_path = dijkstra.doRun();
assert(Astar_path.getPath().isValid());
assert(Astar_path.isFeasible());
assert(dijkstra_path.getPath().isValid());
assert(dijkstra_path.isFeasible());
if (data.getMode() == Mode.LENGTH) {
assert(Math.abs(Astar.getCostPath() - Astar_path.getPath().getLength()) < 1.0);
assert(Math.abs(Astar_path.getPath().getLength() - dijkstra_path.getPath().getLength()) < 1.0);
}
else {
assert(Math.abs(Astar.getCostPath() - Astar_path.getPath().getMinimumTravelTime()) < 1.0);
assert(Math.abs(Astar_path.getPath().getMinimumTravelTime() - dijkstra_path.getPath().getMinimumTravelTime()) < 1.0);
}
}
@Test
/*
* Test supplémentaire pour Astar: on va vérifier que les résultats obtenus
* sont les mêmes que ceux obtenus avec Dijkstra.
* Origine: 16644
* Destination: 39229
* Mode: LENGTH
* PATH UTILISE : ../Paths/custom_paths/long_path_tls.path
*/
public void Dijkstra_Astar_LENGTH() {
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0);
Graph myGraph = graph.get(2);
Node origin = myGraph.get(16644);
Node destination = myGraph.get(39229);
assertDijkstraAStarHaveSameResult(myGraph, origin, destination, arcInspector);
}
@Test
/*
* Test supplémentaire pour Astar: on va vérifier que les résultats obtenus
* sont les mêmes que ceux obtenus avec Dijkstra.
* Origine: 16644
* Destination: 39229
* Mode: TIME
* PATH UTILISE : ../Paths/custom_paths/long_path_tls.path
*/
public void Dijkstra_Astar_TIME() {
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(2);
Graph myGraph = graph.get(2);
Node origin = myGraph.get(16644);
Node destination = myGraph.get(39229);
assertDijkstraAStarHaveSameResult(myGraph, origin, destination, arcInspector);
}
} }