From 4a1e281ec7a95055cf900869fe94694f2edcba8c Mon Sep 17 00:00:00 2001 From: Raph Date: Wed, 10 May 2023 12:14:53 +0200 Subject: [PATCH] test dij sauf test coherence --- .../shortestpath/DijkstraAlgorithm.java | 2 +- .../graphs/algorithm/utils/DijkstraTest.java | 105 ++++++++++++++++-- 2 files changed, 97 insertions(+), 10 deletions(-) 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 dfabfc1..783f07c 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 @@ -52,7 +52,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { try { tas.remove(tab.get(index)); } catch (Exception e) { - System.out.println("l'élement n'est pas dans le tas"); + //System.out.println("l'élement n'est pas dans le tas"); notifyNodeReached(tab.get(index).getSommet()); if(tab.get(index).getSommet().equals(data.getDestination())){ // The destination has been found, notify the observers. 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 162cd53..18b6812 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 @@ -3,19 +3,21 @@ 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.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.Path; 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; + import java.util.Random; public class DijkstraTest { @@ -41,39 +43,113 @@ public class DijkstraTest { private static ShortestPathSolution solDijkstra2; private static ShortestPathSolution solBellman2; + private static String mapName3; + private static GraphReader reader3; + private static Graph graph3; + private static Node origin3; + private static Node destination3; + private static DijkstraAlgorithm dijkstra3; + private static ShortestPathSolution solDijkstra3; + + private static Node origin4; + private static Node destination4; + private static DijkstraAlgorithm dijkstra4; + private static ShortestPathSolution solDijkstra4; + + private static String mapName5; + private static GraphReader reader5; + private static Graph graph5; + private static Node origin5; + private static Node destination5; + private static DijkstraAlgorithm dijkstra5; + private static ShortestPathSolution solDijkstra5; + @BeforeClass public static void initAll() throws IOException{ - mapName1 = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr"; + mapName1 = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/carre.mapgr"; // Create a graph reader. reader1 = new BinaryGraphReader( new DataInputStream(new BufferedInputStream(new FileInputStream(mapName1)))); - mapName2 = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/toulouse.mapgr"; + mapName2 = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/carre.mapgr"; // Create a graph reader. reader2 = new BinaryGraphReader( new DataInputStream(new BufferedInputStream(new FileInputStream(mapName2)))); + mapName3 = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr"; + reader3 = new BinaryGraphReader( + new DataInputStream(new BufferedInputStream(new FileInputStream(mapName3)))); + + mapName5 = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/france.mapgr"; + reader5 = new BinaryGraphReader( + new DataInputStream(new BufferedInputStream(new FileInputStream(mapName5)))); - // Read the graph. + //Read the graph graph1 = reader1.read(); final int numNodes1 = graph1.size(); - origin1 = graph1.get(random1.nextInt(numNodes1)); - destination1 = graph1.get(random1.nextInt(numNodes1)); + int i1 = random1.nextInt(numNodes1); + while(!graph1.get(i1).hasSuccessors()){ + i1 = random1.nextInt(numNodes1); + } + origin1 = graph1.get(i1); + int i2 = random1.nextInt(numNodes1); + while(i2==i1){ + i2 = random1.nextInt(numNodes1); + } + System.err.println(i1 + " " + i2); + destination1 = graph1.get(i2); dijkstra1 = new DijkstraAlgorithm(new ShortestPathData(graph1,origin1,destination1,ArcInspectorFactory.getAllFilters().get(0)));//all roads allowed and length bellman1 = new BellmanFordAlgorithm(new ShortestPathData(graph1,origin1,destination1,ArcInspectorFactory.getAllFilters().get(0))); solDijkstra1 = dijkstra1.run(); solBellman1 = bellman1.run(); + graph2 = reader2.read(); final int numNodes2 = graph2.size(); - origin2 = graph2.get(random1.nextInt(numNodes2)); - destination2 = graph2.get(random1.nextInt(numNodes2)); + int i3 = random1.nextInt(numNodes2); + while(!graph2.get(i3).hasSuccessors()){ + i3 = random1.nextInt(numNodes2); + } + origin2 = graph2.get(i3); + int i4 = random1.nextInt(numNodes2); + while(i4==i3){ + i4 = random1.nextInt(numNodes2); + } + System.out.println(i3 + " " + i4); + destination2 = graph2.get(i4); dijkstra2 = new DijkstraAlgorithm(new ShortestPathData(graph2,origin2,destination2,ArcInspectorFactory.getAllFilters().get(2)));//only roads for cars and time bellman2 = new BellmanFordAlgorithm(new ShortestPathData(graph2,origin2,destination2,ArcInspectorFactory.getAllFilters().get(2))); solDijkstra2 = dijkstra2.run(); solBellman2 = bellman2.run(); + + + graph3 = reader3.read(); + origin3 = graph3.get(1223); + destination3 = graph3.get(1047); //valeurs sur map INSA qui donnent chemin inexistant + dijkstra3 = new DijkstraAlgorithm(new ShortestPathData(graph3,origin3,destination3,ArcInspectorFactory.getAllFilters().get(0)));//all roads allowed and length + solDijkstra3 = dijkstra3.run(); + + origin4 = graph1.get(1); + destination4 = graph1.get(1); + dijkstra4 = new DijkstraAlgorithm(new ShortestPathData(graph1,origin4,destination4,ArcInspectorFactory.getAllFilters().get(0)));//all roads allowed and length + solDijkstra4 = dijkstra4.run(); + + graph5 = reader5.read(); + final int numNodes5 = graph5.size(); + int i5 = random1.nextInt(numNodes5); + while(!graph5.get(i5).hasSuccessors()){ + i5 = random1.nextInt(numNodes5); + } + origin5 = graph1.get(i5); + int i6 = random1.nextInt(numNodes5); + while(i6==i5){ + i6 = random1.nextInt(numNodes5); + } + destination5 = graph5.get(i6); + dijkstra5 = new DijkstraAlgorithm(new ShortestPathData(graph1,origin1,destination1,ArcInspectorFactory.getAllFilters().get(0)));//all roads allowed and length + solDijkstra5 = dijkstra5.run(); } @Test @@ -82,9 +158,20 @@ public class DijkstraTest { } @Test - public void testDijkstra2(){ + public void testDijkstra2(){//test en temps avec BF que pour les voitures assertEquals(solDijkstra2.getPath().getMinimumTravelTime(),solBellman2.getPath().getMinimumTravelTime(),0.00001); } + @Test + public void testDijktra3(){//test dijkstra pour chemin inexistant + assertTrue(solDijkstra3.getStatus().equals(Status.INFEASIBLE)); + } + @Test + public void testDijkstra4(){//test dijkstra pour chemin longueur nulle + assertTrue(solDijkstra4.getPath().getLength()==0); + } + + //pour le dernier test : test de coherence (dist pour minTravelTime > dist pour getLength) + }