diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java index 7eac184..45f73ed 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java @@ -24,9 +24,21 @@ public class AStarAlgorithm extends DijkstraAlgorithm { MaxSpeed = MaxSpeed / 3.6; } + //double longueurArc = 0.0; + for (Node node: data.getGraph().getNodes()) { double estimation = Point.distance(node.getPoint(), data.getDestination().getPoint())/MaxSpeed; labels[node.getId()] = new LabelStar(node, estimation); + /*if (node.getId() == 6800) { + for (Arc arc: node.getSuccessors()) { + if (arc.getDestination().getId() == 6299) { + longueurArc = arc.getLength(); + } + } + }*/ } + + //System.out.println("La distance entre les nodes " + data.getGraph().getNodes().get(6800).getId() + " et " + data.getGraph().getNodes().get(6299).getId() + " est : " + Point.distance(data.getGraph().getNodes().get(6800).getPoint(), data.getGraph().getNodes().get(6299).getPoint()) + " et la longeur de l'arc est : " + longueurArc); + } } diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java index 134fb15..ec59011 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java @@ -40,6 +40,9 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { tas.insert(labels[origin.getId()]); notifyOriginProcessed(origin); + // TEST SUR LES COUTS CROISSANTS + int ancienID = origin.getId(); + boolean encore = true; while ((!labels[dest.getId()].isMarque()) && encore) { @@ -53,6 +56,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { // POUR LE TEST SUR LES COUTS CROISSANTS //System.out.println("Test sur le coût : " + labels[currentID].getTotalCost()); + if (labels[currentID].compareTo(labels[ancienID]) == -1) { + System.out.println("Erreur : Couts non croissants : noeud" + ancienID + " contre " + currentID); + } + ancienID = currentID; //nbNonMarque--; @@ -100,8 +107,8 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { //Pour le test SOMME(couts) = Cout(Label(Destination)) double SommeCouts = 0.0; - // Destination has no predecessor, the solution is infeasible... - if (labels[dest.getId()].getPere() == null) { + // Destination n'est pas marquée, the solution is infeasible... + if (!labels[dest.getId()].isMarque()) { solution = new ShortestPathSolution(data, Status.INFEASIBLE); } else { diff --git a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/OptimaliteTest.java b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/OptimaliteTest.java new file mode 100644 index 0000000..7385268 --- /dev/null +++ b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/OptimaliteTest.java @@ -0,0 +1,182 @@ +package org.insa.graphs.algorithm.shortestpath; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; + +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.BufferedInputStream; +import java.io.DataInputStream; +import java.io.FileInputStream; + +import org.insa.graphs.model.io.BinaryGraphReader; +import org.insa.graphs.model.io.GraphReader; +import org.insa.graphs.model.Graph; + +import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm; +import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm; +import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm; + +import org.insa.graphs.algorithm.ArcInspectorFactory; +import org.insa.graphs.algorithm.shortestpath.ShortestPathData; + +public class OptimaliteTest { + + // Graph utilisé pour les tests + static Graph graph; + static double delta = 0.00001; + + //Lecture du graphe + @BeforeClass + public static void initAll() throws Exception { + final String mapName = "/home/paulfaure/Documents/3A/JAVA/Cartes BE/haute-garonne/europe/france/haute-garonne.mapgr"; + + // Create a graph reader. + final GraphReader reader = new BinaryGraphReader( + new DataInputStream(new BufferedInputStream(new FileInputStream(mapName)))); + + // Read the graph. + OptimaliteTest.graph = reader.read(); + + reader.close(); + } + + @Test + public void testCheminNul() { + // On ne fait le test qu'avec un filtre sur les chemins autorisés + ShortestPathData data = new ShortestPathData(graph, graph.get(12305), graph.get(12305), ArcInspectorFactory.getAllFilters().get(0)); + ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathAlgorithm aStar = new AStarAlgorithm(data); + ShortestPathSolution solutionAStar = aStar.run(); + ShortestPathSolution solutionDijkstra = dijkstra.run(); + assertTrue(solutionDijkstra.isFeasible()); + assertTrue(solutionAStar.isFeasible()); + assertTrue(solutionDijkstra.getPath().isEmpty()); + assertTrue(solutionAStar.getPath().isEmpty()); + } + + /*@Test + public void testOriginNotInGraph() { + // On ne fait le test qu'avec un filtre sur les chemins autorisés + ShortestPathData data = new ShortestPathData(graph, graph.get(-1), graph.get(12305), ArcInspectorFactory.getAllFilters().get(0)); + ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathAlgorithm aStar = new AStarAlgorithm(data); + ShortestPathSolution solutionAStar = aStar.run(); + ShortestPathSolution solutionDijkstra = dijkstra.run(); + assertFalse(solutionDijkstra.isFeasible()); + assertFalse(solutionAStar.isFeasible()); + } + + @Test + public void testDestinationNotInGraph() { + // On ne fait le test qu'avec un filtre sur les chemins autorisés + ShortestPathData data = new ShortestPathData(graph, graph.get(12305), graph.get(1230555), ArcInspectorFactory.getAllFilters().get(0)); + ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathAlgorithm aStar = new AStarAlgorithm(data); + ShortestPathSolution solutionAStar = aStar.run(); + ShortestPathSolution solutionDijkstra = dijkstra.run(); + assertFalse(solutionDijkstra.isFeasible()); + assertFalse(solutionAStar.isFeasible()); + }*/ + + @Test + public void testAucunCheminPossible() { + // On ne fait le test qu'avec un filtre sur les chemins autorisés + ShortestPathData data = new ShortestPathData(graph, graph.get(137947), graph.get(75870), ArcInspectorFactory.getAllFilters().get(0)); + ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathAlgorithm aStar = new AStarAlgorithm(data); + ShortestPathSolution solutionAStar = aStar.run(); + ShortestPathSolution solutionDijkstra = dijkstra.run(); + assertFalse(solutionDijkstra.isFeasible()); + assertFalse(solutionAStar.isFeasible()); + } + + @Test + public void testOptimaliteLengthAllRoadAllowed() { + ShortestPathData data = new ShortestPathData(graph, graph.get(92173), graph.get(75870), ArcInspectorFactory.getAllFilters().get(0)); + ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(data); + ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathAlgorithm aStar = new AStarAlgorithm(data); + ShortestPathSolution solutionAStar = aStar.run(); + ShortestPathSolution solutionDijkstra = dijkstra.run(); + ShortestPathSolution solutionBellmanFord = bellmanFord.run(); + assertEquals(solutionBellmanFord.getPath().getLength(),solutionDijkstra.getPath().getLength(), OptimaliteTest.delta); + assertEquals(solutionBellmanFord.getPath().getLength(),solutionAStar.getPath().getLength(), OptimaliteTest.delta); + } + + @Test + public void testOptimaliteLengthOnlyRoadOpenForCarsAllowed() { + ShortestPathData data = new ShortestPathData(graph, graph.get(92173), graph.get(75870), ArcInspectorFactory.getAllFilters().get(1)); + ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(data); + ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathAlgorithm aStar = new AStarAlgorithm(data); + ShortestPathSolution solutionAStar = aStar.run(); + ShortestPathSolution solutionDijkstra = dijkstra.run(); + ShortestPathSolution solutionBellmanFord = bellmanFord.run(); + assertEquals(solutionBellmanFord.getPath().getLength(),solutionDijkstra.getPath().getLength(), OptimaliteTest.delta); + assertEquals(solutionBellmanFord.getPath().getLength(),solutionAStar.getPath().getLength(), OptimaliteTest.delta); + } + + @Test + public void testOptimaliteTimeOnlyRoadOpenForCarsAllowed() { + ShortestPathData data = new ShortestPathData(graph, graph.get(92173), graph.get(75870), ArcInspectorFactory.getAllFilters().get(2)); + ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(data); + ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathAlgorithm aStar = new AStarAlgorithm(data); + ShortestPathSolution solutionAStar = aStar.run(); + ShortestPathSolution solutionDijkstra = dijkstra.run(); + ShortestPathSolution solutionBellmanFord = bellmanFord.run(); + assertEquals(solutionBellmanFord.getPath().getLength(),solutionDijkstra.getPath().getLength(), OptimaliteTest.delta); + assertEquals(solutionBellmanFord.getPath().getLength(),solutionAStar.getPath().getLength(), OptimaliteTest.delta); + } + + @Test + public void testOptimaliteTimePedestriansAndBicyclesAllowed() { + ShortestPathData data = new ShortestPathData(graph, graph.get(92173), graph.get(75870), ArcInspectorFactory.getAllFilters().get(3)); + ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(data); + ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathAlgorithm aStar = new AStarAlgorithm(data); + ShortestPathSolution solutionAStar = aStar.run(); + ShortestPathSolution solutionDijkstra = dijkstra.run(); + ShortestPathSolution solutionBellmanFord = bellmanFord.run(); + assertEquals(solutionBellmanFord.getPath().getLength(),solutionDijkstra.getPath().getLength(), OptimaliteTest.delta); + assertEquals(solutionBellmanFord.getPath().getLength(),solutionAStar.getPath().getLength(), OptimaliteTest.delta); + } + + @Test + public void testErreurEstimationLengthAllRoadAllowed() { + ShortestPathData data = new ShortestPathData(graph, graph.get(7710), graph.get(6299), ArcInspectorFactory.getAllFilters().get(0)); + ShortestPathAlgorithm bellmanFord = new BellmanFordAlgorithm(data); + ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathAlgorithm aStar = new AStarAlgorithm(data); + ShortestPathSolution solutionAStar = aStar.run(); + ShortestPathSolution solutionDijkstra = dijkstra.run(); + ShortestPathSolution solutionBellmanFord = bellmanFord.run(); + assertEquals(solutionBellmanFord.getPath().getLength(),solutionDijkstra.getPath().getLength(), OptimaliteTest.delta); + assertEquals(solutionBellmanFord.getPath().getLength(),solutionAStar.getPath().getLength(), OptimaliteTest.delta); + } + + @Test + public void testDijkstraPlusRapide() { + // On ne fait le test qu'avec un filtre sur les chemins autorisés + ShortestPathData data = new ShortestPathData(graph, graph.get(75870), graph.get(103867), ArcInspectorFactory.getAllFilters().get(0)); + ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathAlgorithm aStar = new AStarAlgorithm(data); + ShortestPathSolution solutionAStar = aStar.run(); + ShortestPathSolution solutionDijkstra = dijkstra.run(); + assertTrue(solutionDijkstra.getSolvingTime().compareTo(solutionAStar.getSolvingTime()) < 0); + } + + @Test + public void testAStarPlusRapide() { + // On ne fait le test qu'avec un filtre sur les chemins autorisés + ShortestPathData data = new ShortestPathData(graph, graph.get(12305), graph.get(103867), ArcInspectorFactory.getAllFilters().get(0)); + ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathAlgorithm aStar = new AStarAlgorithm(data); + ShortestPathSolution solutionAStar = aStar.run(); + ShortestPathSolution solutionDijkstra = dijkstra.run(); + assertTrue(solutionDijkstra.getSolvingTime().compareTo(solutionAStar.getSolvingTime()) > 0); + } +}