102 行
3.7 KiB
Java
102 行
3.7 KiB
Java
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 AStarAlgorithmTest() {
|
|
super();
|
|
}
|
|
|
|
@Override
|
|
protected ShortestPathAlgorithm initAlgo(ShortestPathData 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();
|
|
|
|
if (dijkstra_path.getPath() != null) {
|
|
assert(Astar_path.getPath().isValid());
|
|
if (dijkstra_path.isFeasible()) {
|
|
assert(Astar_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);
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
assert(Astar_path.getPath() == null);
|
|
assert(!Astar_path.isFeasible());
|
|
}
|
|
}
|
|
|
|
@Test
|
|
/*
|
|
* Test supplémentaire pour Astar: on va vérifier que les résultats obtenus
|
|
* sont les mêmes que ceux obtenus avec Dijkstra.
|
|
* Mode: LENGTH
|
|
* Carte utilisée : ../Maps/toulouse.mapgr
|
|
*/
|
|
public void Dijkstra_Astar_LENGTH() {
|
|
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0);
|
|
Graph myGraph = graph.get(2);
|
|
|
|
Node origin = myGraph.get(0);
|
|
Node destination = myGraph.get(0);
|
|
|
|
int size_graph = myGraph.size();
|
|
|
|
for (int i = 0 ; i < 50 ; i++) {
|
|
origin = myGraph.get(Math.abs(rand.nextInt()) % size_graph);
|
|
destination = myGraph.get(Math.abs(rand.nextInt()) % size_graph);
|
|
|
|
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.
|
|
* Mode: TIME
|
|
* Carte utilisée : ../Maps/toulouse.mapgr
|
|
*/
|
|
public void Dijkstra_Astar_TIME() {
|
|
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(2);
|
|
Graph myGraph = graph.get(2);
|
|
Node origin = myGraph.get(0);
|
|
Node destination = myGraph.get(0);
|
|
|
|
int size_graph = myGraph.size();
|
|
|
|
for (int i = 0 ; i < 50 ; i++) {
|
|
origin = myGraph.get(Math.abs(rand.nextInt()) % size_graph);
|
|
destination = myGraph.get(Math.abs(rand.nextInt()) % size_graph);
|
|
|
|
assertDijkstraAStarHaveSameResult(myGraph, origin, destination, arcInspector);
|
|
}
|
|
}
|
|
|
|
}
|