From d1a0096430dba2fe463329baec4db916b52a06cd Mon Sep 17 00:00:00 2001 From: Raph Date: Thu, 18 May 2023 13:28:11 +0200 Subject: [PATCH] test Astar, vfinale --- .../graphs/algorithm/utils/AstarTest.java | 144 ++++++++++++++++++ .../graphs/algorithm/utils/DijkstraTest.java | 4 +- 2 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AstarTest.java diff --git a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AstarTest.java b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AstarTest.java new file mode 100644 index 0000000..cba3e11 --- /dev/null +++ b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AstarTest.java @@ -0,0 +1,144 @@ +package org.insa.graphs.algorithm.utils; +import java.io.BufferedInputStream; +import java.io.DataInputStream; +import java.io.FileInputStream; +import org.insa.graphs.algorithm.ArcInspectorFactory; +import org.insa.graphs.algorithm.AbstractSolution.Status; +import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm; +import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm; +import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm; +import org.insa.graphs.algorithm.shortestpath.ShortestPathData; +import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution; +import org.insa.graphs.model.Graph; +import org.insa.graphs.model.Node; +import org.insa.graphs.model.io.BinaryGraphReader; +import org.insa.graphs.model.io.GraphReader; +import java.io.IOException; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class AstarTest{ + + private static ShortestPathSolution solBellman1; + private static ShortestPathSolution solDijkstra1; + private static ShortestPathSolution solAstar1; + + private static ShortestPathSolution solBellman1p; + private static ShortestPathSolution solDijkstra1p; + private static ShortestPathSolution solAstar1p; + + private static ShortestPathSolution solBellman2; + private static ShortestPathSolution solDijkstra2; + private static ShortestPathSolution solAstar2; + + private static ShortestPathSolution solAstar3; + + private static ShortestPathSolution solAstar4; + + private static ShortestPathSolution solAstar5; + private static ShortestPathSolution solAstar6; + + + @BeforeClass + public static void initAll() throws IOException{ + final String mapName1 = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr"; + final GraphReader reader1 = new BinaryGraphReader( + new DataInputStream(new BufferedInputStream(new FileInputStream(mapName1)))); + + final String mapName2 = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/carre.mapgr"; + final GraphReader reader2 = new BinaryGraphReader( + new DataInputStream(new BufferedInputStream(new FileInputStream(mapName2)))); + + //pour test Astar1 + final Graph graph1 = reader1.read(); + final Node origin1 = graph1.get(969); //chemin long + final Node destination1 = graph1.get(1193); + final Node origin1p = graph1.get(871); //chemin court + final Node destination1p = graph1.get(239); + final DijkstraAlgorithm dijkstra1 =new DijkstraAlgorithm(new ShortestPathData(graph1,origin1,destination1,ArcInspectorFactory.getAllFilters().get(0)));//all roads allowed and length + solDijkstra1 = dijkstra1.run(); + final DijkstraAlgorithm dijkstra1p =new DijkstraAlgorithm(new ShortestPathData(graph1,origin1p,destination1p,ArcInspectorFactory.getAllFilters().get(0)));//all roads allowed and length + solDijkstra1p = dijkstra1p.run(); + final AStarAlgorithm Astar1p = new AStarAlgorithm(new ShortestPathData(graph1,origin1p,destination1p,ArcInspectorFactory.getAllFilters().get(0)));//all roads allowed and length + final AStarAlgorithm Astar1 = new AStarAlgorithm(new ShortestPathData(graph1,origin1,destination1,ArcInspectorFactory.getAllFilters().get(0)));//all roads allowed and length + final BellmanFordAlgorithm bellman1 = new BellmanFordAlgorithm(new ShortestPathData(graph1,origin1,destination1,ArcInspectorFactory.getAllFilters().get(0))); + final BellmanFordAlgorithm bellman1p = new BellmanFordAlgorithm(new ShortestPathData(graph1,origin1p,destination1p,ArcInspectorFactory.getAllFilters().get(0))); + solDijkstra1 = dijkstra1.run(); + solBellman1 = bellman1.run(); + + solAstar1 = Astar1.run(); + solDijkstra1p = dijkstra1p.run(); + solBellman1p = bellman1p.run(); + solAstar1p = Astar1p.run(); + + //pour test Astar2 + final Graph graph2 = reader2.read(); + final Node origin2 = graph2.get(23); + final Node destination2 = graph2.get(1); + final DijkstraAlgorithm dijkstra2 =new DijkstraAlgorithm(new ShortestPathData(graph2,origin2,destination2,ArcInspectorFactory.getAllFilters().get(2))); + solDijkstra2 = dijkstra2.run(); + final BellmanFordAlgorithm bellman2 = new BellmanFordAlgorithm(new ShortestPathData(graph2,origin2,destination2,ArcInspectorFactory.getAllFilters().get(2))); + solBellman2 = bellman2.run(); + final AStarAlgorithm Astar2 = new AStarAlgorithm(new ShortestPathData(graph2,origin2,destination2,ArcInspectorFactory.getAllFilters().get(2))); + solAstar2 = Astar2.run(); + + //pour test Astar3 + final Node origin3 = graph1.get(1223); + final Node destination3 = graph1.get(1047); //valeurs sur map INSA qui donnent chemin inexistant + final AStarAlgorithm Astar3 = new AStarAlgorithm(new ShortestPathData(graph1,origin3,destination3,ArcInspectorFactory.getAllFilters().get(0)));//all roads allowed and length + solAstar3 = Astar3.run(); + + //pour test Astar4 + final Node origin4 = graph1.get(0); + final Node destination4 = graph1.get(0); //valeurs sur map INSA qui donnent chemin inexistant + final AStarAlgorithm Astar4 = new AStarAlgorithm(new ShortestPathData(graph1,origin4,destination4,ArcInspectorFactory.getAllFilters().get(0)));//all roads allowed and length + solAstar4 = Astar4.run(); + + //pour test Astar5 + final AStarAlgorithm Astar5 = new AStarAlgorithm(new ShortestPathData(graph2,origin2,destination2,ArcInspectorFactory.getAllFilters().get(1))); + solAstar5 = Astar5.run(); + final AStarAlgorithm Astar6 = new AStarAlgorithm(new ShortestPathData(graph2,origin2,destination2,ArcInspectorFactory.getAllFilters().get(2))); + solAstar6 = Astar6.run(); + + + + } + + @Test + public void testAstar1(){//test en distance (chemin court) + assertEquals(solAstar1.getPath().getLength(),solBellman1.getPath().getLength(),0.00001); + assertEquals(solAstar1.getPath().getLength(),solDijkstra1.getPath().getLength(),0.00001); + } + + @Test//test trajet court + public void testAstar1p(){//test en distance (chemin long) + assertEquals(solAstar1p.getPath().getLength(),solBellman1p.getPath().getLength(),0.00001); + assertEquals(solAstar1p.getPath().getLength(),solDijkstra1p.getPath().getLength(),0.00001); + } + + @Test + public void testAstar2(){//test en temps que pour les voitures + assertEquals(solAstar2.getPath().getMinimumTravelTime(),solBellman2.getPath().getMinimumTravelTime(),0.00001); + assertEquals(solAstar2.getPath().getMinimumTravelTime(),solDijkstra2.getPath().getMinimumTravelTime(),0.00001); + } + + @Test + public void testAstar3(){//test Astar pour chemin inexistant + assertTrue(solAstar3.getStatus().equals(Status.INFEASIBLE)); + } + + @Test + public void testAstar4(){//test Astar pour chemin longueur nulle + assertTrue(solAstar4.getPath().getLength()==0); + } + + @Test + public void testDijkstra5(){//test de cohérence + assertTrue(solAstar5.getPath().getLength() <= solAstar6.getPath().getLength()); + assertTrue(solAstar5.getPath().getMinimumTravelTime() >= solAstar6.getPath().getMinimumTravelTime()); + + } + +} \ No newline at end of file diff --git a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/DijkstraTest.java b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/DijkstraTest.java index 7648401..a9cfc40 100644 --- a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/DijkstraTest.java +++ b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/DijkstraTest.java @@ -110,7 +110,7 @@ public class DijkstraTest { //} //System.out.println(i3 + " " + i4); destination2 = graph1.get(i2); - dijkstra2 = new DijkstraAlgorithm(new ShortestPathData(graph1,origin1,destination1,ArcInspectorFactory.getAllFilters().get(2)));//only roads for cars and time1 + dijkstra2 = new DijkstraAlgorithm(new ShortestPathData(graph1,origin1,destination1,ArcInspectorFactory.getAllFilters().get(2)));//only roads for cars and time bellman2 = new BellmanFordAlgorithm(new ShortestPathData(graph1,origin1,destination1,ArcInspectorFactory.getAllFilters().get(2))); solDijkstra2 = dijkstra2.run(); solBellman2 = bellman2.run(); @@ -149,7 +149,7 @@ public class DijkstraTest { } @Test - public void testDijkstra5(){//test de cohérence + public void testDijkstra5(){//test de cohérence assertTrue(solDijkstra5.getPath().getLength() <= solDijkstra2.getPath().getLength()); assertTrue(solDijkstra5.getPath().getMinimumTravelTime() >= solDijkstra2.getPath().getMinimumTravelTime());