1
0
Fork 0

Test for A*

This commit is contained in:
Yanis Mahé 2026-05-19 15:59:15 +02:00
parent bf2c35eadd
commit cb5f211cee

View file

@ -12,6 +12,7 @@ import java.util.List;
import org.insa.graphs.algorithm.AbstractSolution.Status; import org.insa.graphs.algorithm.AbstractSolution.Status;
import org.insa.graphs.algorithm.ArcInspector; import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.algorithm.ArcInspectorFactory; import org.insa.graphs.algorithm.ArcInspectorFactory;
import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm;
import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm; import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm; import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm; import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
@ -84,18 +85,23 @@ public class ShortestPathTest {
// System.out.println("" + insaBikiniCanal.getOrigin().getId() +" "+ insaBikiniCanal.getDestination().getId()); // System.out.println("" + insaBikiniCanal.getOrigin().getId() +" "+ insaBikiniCanal.getDestination().getId());
ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(insaBikiniCanalPathData); ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(insaBikiniCanalPathData);
ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(insaBikiniCanalPathData); ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(insaBikiniCanalPathData);
ShortestPathAlgorithm aStar = new AStarAlgorithm(insaBikiniCanalPathData);
ShortestPathSolution solutionBellmanFord = bellmanFord.run(); ShortestPathSolution solutionBellmanFord = bellmanFord.run();
ShortestPathSolution solutionDijkstra = dijkstra.run(); ShortestPathSolution solutionDijkstra = dijkstra.run();
ShortestPathSolution solutionAStar = aStar.run();
assertEquals(Status.OPTIMAL, solutionBellmanFord.getStatus()); assertEquals(Status.OPTIMAL, solutionBellmanFord.getStatus());
assertEquals(Status.OPTIMAL, solutionDijkstra.getStatus()); assertEquals(Status.OPTIMAL, solutionDijkstra.getStatus());
assertEquals(Status.OPTIMAL, solutionAStar.getStatus());
assertEquals(insaBikiniCanal.getLength(), solutionBellmanFord.getPath().getLength(),1.0); assertEquals(insaBikiniCanal.getLength(), solutionBellmanFord.getPath().getLength(),1.0);
assertEquals(insaBikiniCanal.getLength(), solutionDijkstra.getPath().getLength(), 1.0); assertEquals(insaBikiniCanal.getLength(), solutionDijkstra.getPath().getLength(), 1.0);
assertEquals(insaBikiniCanal.getLength(), solutionAStar.getPath().getLength(), 1.0);
assertEquals(insaBikiniCanal.getTravelTime(120), solutionBellmanFord.getPath().getTravelTime(120),1.0); assertEquals(insaBikiniCanal.getTravelTime(120), solutionBellmanFord.getPath().getTravelTime(120),1.0);
assertEquals(insaBikiniCanal.getTravelTime(120), solutionDijkstra.getPath().getTravelTime(120), 1.0); assertEquals(insaBikiniCanal.getTravelTime(120), solutionDijkstra.getPath().getTravelTime(120), 1.0);
assertEquals(insaBikiniCanal.getTravelTime(120), solutionAStar.getPath().getTravelTime(120), 1.0);
} }
@Test @Test
@ -103,41 +109,54 @@ public class ShortestPathTest {
ShortestPathData cheminInexistant = new ShortestPathData(insa, insa.get(940), insa.get(702), onlyCarsByLengthArcInspector); ShortestPathData cheminInexistant = new ShortestPathData(insa, insa.get(940), insa.get(702), onlyCarsByLengthArcInspector);
ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(cheminInexistant); ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(cheminInexistant);
ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(cheminInexistant); ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(cheminInexistant);
ShortestPathAlgorithm aStar = new AStarAlgorithm(cheminInexistant);
ShortestPathSolution solutionBellmanFord = bellmanFord.run(); ShortestPathSolution solutionBellmanFord = bellmanFord.run();
ShortestPathSolution solutionDijkstra = dijkstra.run(); ShortestPathSolution solutionDijkstra = dijkstra.run();
ShortestPathSolution solutionAStar = aStar.run();
assertEquals(Status.INFEASIBLE, solutionBellmanFord.getStatus()); assertEquals(Status.INFEASIBLE, solutionBellmanFord.getStatus());
assertEquals(Status.INFEASIBLE, solutionDijkstra.getStatus()); assertEquals(Status.INFEASIBLE, solutionDijkstra.getStatus());
assertEquals(Status.INFEASIBLE, solutionAStar.getStatus());
} }
@Test @Test
public void testCheminLongueurNulle() { public void testCheminLongueurNulle() {
ShortestPathData cheminInexistant = new ShortestPathData(insa, insa.get(940), insa.get(940), noFilterByLengthArcInspector); ShortestPathData cheminNul = new ShortestPathData(insa, insa.get(940), insa.get(940), noFilterByLengthArcInspector);
ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(cheminInexistant); ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(cheminNul);
ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(cheminInexistant); ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(cheminNul);
ShortestPathAlgorithm aStar = new AStarAlgorithm(cheminNul);
ShortestPathSolution solutionBellmanFord = bellmanFord.run(); ShortestPathSolution solutionBellmanFord = bellmanFord.run();
ShortestPathSolution solutionDijkstra = dijkstra.run(); ShortestPathSolution solutionDijkstra = dijkstra.run();
ShortestPathSolution solutionAStar = aStar.run();
assertEquals(Status.INFEASIBLE, solutionBellmanFord.getStatus()); assertEquals(Status.INFEASIBLE, solutionBellmanFord.getStatus());
assertEquals(Status.INFEASIBLE, solutionDijkstra.getStatus()); assertEquals(Status.INFEASIBLE, solutionDijkstra.getStatus());
assertEquals(Status.INFEASIBLE, solutionAStar.getStatus());
assertNull(solutionBellmanFord.getPath()); assertNull(solutionBellmanFord.getPath());
assertNull(solutionDijkstra.getPath()); assertNull(solutionDijkstra.getPath());
assertNull(solutionAStar.getPath());
} }
@Test @Test
public void testCheminLong() { // En comparant les deux algos public void testCheminLong() { // En comparant les deux algos
ShortestPathData cheminInexistant = new ShortestPathData(belgique, belgique.get(956754), belgique.get(842938), noFilterByLengthArcInspector); ShortestPathData cheminLong = new ShortestPathData(belgique, belgique.get(956754), belgique.get(842938), noFilterByLengthArcInspector);
ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(cheminInexistant); ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(cheminLong);
ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(cheminInexistant); ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(cheminLong);
ShortestPathAlgorithm aStar = new AStarAlgorithm(cheminLong);
ShortestPathSolution solutionBellmanFord = bellmanFord.run(); ShortestPathSolution solutionBellmanFord = bellmanFord.run();
ShortestPathSolution solutionDijkstra = dijkstra.run(); ShortestPathSolution solutionDijkstra = dijkstra.run();
ShortestPathSolution solutionAStar = aStar.run();
assertEquals(solutionBellmanFord.getStatus(), solutionDijkstra.getStatus()); assertEquals(solutionBellmanFord.getStatus(), solutionDijkstra.getStatus());
assertEquals(solutionBellmanFord.getPath().getLength(), solutionDijkstra.getPath().getLength(), 1.0); assertEquals(solutionBellmanFord.getPath().getLength(), solutionDijkstra.getPath().getLength(), 1.0);
assertEquals(solutionBellmanFord.getPath().getTravelTime(120), solutionDijkstra.getPath().getTravelTime(120), 1.0); assertEquals(solutionBellmanFord.getPath().getTravelTime(120), solutionDijkstra.getPath().getTravelTime(120), 1.0);
assertEquals(solutionAStar.getStatus(), solutionDijkstra.getStatus());
assertEquals(solutionAStar.getPath().getLength(), solutionDijkstra.getPath().getLength(), 1.0);
assertEquals(solutionAStar.getPath().getTravelTime(120), solutionDijkstra.getPath().getTravelTime(120), 1.0);
} }
} }