diff --git a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithmTest.java b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithmTest.java index ea8c546..87c943d 100644 --- a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithmTest.java +++ b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithmTest.java @@ -1,32 +1,28 @@ package org.insa.graphs.algorithm.shortestpath; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import org.insa.graphs.algorithm.AbstractSolution.Status; import org.insa.graphs.algorithm.ArcInspector; import org.insa.graphs.algorithm.ArcInspectorFactory; 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.BinaryPathReader; import org.insa.graphs.model.io.GraphReader; import org.insa.graphs.model.io.PathReader; import org.junit.BeforeClass; -import org.junit.Assume; -import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameter; -import org.junit.runners.Parameterized.Parameters; -import java.util.ArrayList; -import java.util.Arrays; public class DijkstraAlgorithmTest { @@ -37,102 +33,254 @@ public class DijkstraAlgorithmTest { public static ArrayList path = new ArrayList(); @Parameter - static ArrayList Maps = new ArrayList(Arrays.asList("../Maps/carre.mapgr", - "../Maps/insa.mapgr","../Maps/toulouse.mapgr")); + // Liste de cartes + static ArrayList Maps = new ArrayList(Arrays.asList("../Maps/carre.mapgr", + "../Maps/insa.mapgr", + "../Maps/toulouse.mapgr", + "../Maps/midi-pyrenees.mapgr")); //TODO: chemins map carree à creer // chemins maps insa à remettre // chemins map toulouse à créer - // Chemin inexistant: carte INSA : nodes 224 --> 814 // test optimalité : long chemin: belgique espagne - static ArrayList Paths_CARREE = new ArrayList(Arrays.asList("")); - static ArrayList Paths_TLS = new ArrayList(Arrays.asList("")); - static ArrayList Paths_INSA = new ArrayList(Arrays.asList( "../Paths/path_fr31insa_rangueil_r2.path", - "../Paths/path_fr31insa_rangueil_insa.path")); - - static ArrayList[] Paths = {Paths_CARREE,Paths_TLS,Paths_TLS}; + static ArrayList Paths_CARREE = new ArrayList(Arrays.asList("../Paths/custom_paths/short_path_carre.path", + "../Paths/custom_paths/long_path_carre.path")); + static ArrayList Paths_INSA = new ArrayList(Arrays.asList( "../Paths/path_fr31insa_rangueil_r2.path", + "../Paths/custom_paths/long_path_insa.path")); + static ArrayList Paths_TLS = new ArrayList(Arrays.asList("../Paths/custom_paths/short_path_tls.path", + "../Paths/custom_paths/long_path_tls.path")); + static ArrayList Paths_Midi_Pyrenees = new ArrayList(Arrays.asList("../Paths/custom_paths/long_chemin_midi_pyrenees.path")); + + // A list containing all the paths to be tested + static ArrayList[] Paths = {Paths_CARREE,Paths_INSA,Paths_TLS, Paths_Midi_Pyrenees}; @BeforeClass + // Init all the graphs and paths we need for the tests + // We create for each map all the known paths (custom ones) public static void init() { - - // Visit these directory to see the list of available files on Commetud. - // When running with VSC, paths are relative to the BE_Graphes directory. - ArrayList actual_path_list = new ArrayList(); - for (int j = 0 ; j < Maps.size() ; j++) { - actual_path_list = Paths[j]; - for (int i = 0 ; i < Paths_TLS.size() ; i ++) { - final String mapName = Maps.get(j); - final String pathName = actual_path_list.get(i); - // System.out.println("Working Directory = " + System.getProperty("user.dir")); - // Create a graph reader. - System.out.println(pathName); - System.out.println(mapName); - - - try { + try { + ArrayList actual_path_list = new ArrayList(); + // Create the map + for (int j = 0 ; j < Maps.size() ; j++) { + actual_path_list = Paths[j]; + final String mapName = Maps.get(j); + // Create a graph reader final GraphReader reader = new BinaryGraphReader( - new DataInputStream(new BufferedInputStream(new FileInputStream(mapName)))); - + new DataInputStream(new BufferedInputStream(new FileInputStream(mapName)))); // Read the graph. X graph.add(reader.read()); // free resources reader.close(); - - // Create a PathReader. - final PathReader pathReader = new BinaryPathReader(new DataInputStream(new BufferedInputStream(new FileInputStream(pathName)))); - - System.out.println(graph.get(i)); - // Read the path. - path.add(pathReader.readPath(graph.get(i))); - - // free resources - pathReader.close(); - } - catch (FileNotFoundException e) { - System.err.println("File not found: " + e.getMessage()); - fail("File not found: " + e.getMessage()); - } - catch (IOException e ) { - System.err.println("Error reading file: " + e.getMessage()); - fail("Error reading file: " + e.getMessage()); + + // Create the paths of the map + for (int i = 0 ; i < actual_path_list.size() ; i ++) { + final String pathName = actual_path_list.get(i); + // Create a PathReader. + final PathReader pathReader = new BinaryPathReader(new DataInputStream(new BufferedInputStream(new FileInputStream(pathName)))); + // System.out.println(pathName); + // System.out.println(mapName); + // Read the path. + path.add(pathReader.readPath(graph.get(j))); + // free resources + pathReader.close(); + } } } + catch (FileNotFoundException e) { + System.err.println("File not found: " + e.getMessage()); + fail("File not found: " + e.getMessage()); + } + catch (IOException e ) { + System.err.println("Error reading file: " + e.getMessage()); + fail("Error reading file: " + e.getMessage()); } } @Test + /* Stratégie: + * Chemins courts testés par comparaison avec Bellman. + * Chemin longs testés par comparaison avec chemins déjà construits. Bellman trop long. + */ // TODO - // fonction pour code au dessus - // appeler constructeur dijkstra - // donner le path à afficher en renvoyant le path de dijkstra dans path. - // Chemin court => Bellman OK et vérifié. On compare les résultats obtenus par les 2 algos - public void testToulouseCourtChemin() { + + // Map: carre.mapgr + // Chemin: 19 --> 4 + // Tous chemins permis + public void chemin_court_CARRE() { ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); Graph myGraph = graph.get(0); - Path myPath = path.get(0); - ShortestPathData data = new ShortestPathData(myGraph, myPath.getOrigin(), myPath.getDestination(), arcInspector); + Node origin = myGraph.get(19); + Node destination = myGraph.get(4); + ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector); DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data); ShortestPathSolution dijk_path = dijkstra.doRun(); BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data); ShortestPathSolution bell_path = bellman.doRun(); - assert(dijk_path.getPath().getLength() == bell_path.getPath().getLength()); + + assert(dijk_path.getPath().isValid()); + assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0); } - // Long chemin => Bellman trop long, on compare dijkstra au chemin récupéré directement - public void testToulouseLongChemin() { + @Test + /* + * Chemin long relativement à la carte carrée. + * Chemin: 15 --> 9 + * Tous chemins permis + */ + public void chemin_long_CARRE() { + ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); Graph myGraph = graph.get(0); - Path myPath = path.get(0); - ArcInspector arcInspector; - ShortestPathData data = new ShortestPathData(myGraph, myPath.getOrigin(), myPath.getDestination(), null); + Node origin = myGraph.get(15); + Node destination = myGraph.get(9); + ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector); + DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data); ShortestPathSolution dijk_path = dijkstra.doRun(); - assert(dijk_path.getPath() == this.path); + BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data); + ShortestPathSolution bell_path = bellman.doRun(); + + assert(dijk_path.getPath().isValid()); + assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0); } - + @Test + /* + * Chemin nul sur carte carrée. + * L'origine et la destination sont les mêmes (noeud 3). + * Tous chemins permis + */ + public void chemin_nul_CARRE() { + ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); + Graph myGraph = graph.get(0); + Node origin = myGraph.get(3); + Node destination = myGraph.get(3); + ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector); + + DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathSolution dijk_path = dijkstra.doRun(); + + assertEquals(dijk_path.getStatus(), Status.INFEASIBLE); + assert(dijk_path.getPath() == null); + } + + @Test + /* + * Chemin inexistant sur la carte INSA. + * Origine: 224 + * Destination: 814. + * Les 2 noeuds font partie de deux composantes non connexes. + * Tous chemins permis. + */ + public void chemin_inexistant_INSA() { + ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); + Graph myGraph = graph.get(1); + Node origin = myGraph.get(224); + Node destination = myGraph.get(814); + ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector); + + DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathSolution dijk_path = dijkstra.doRun(); + + assertEquals(dijk_path.getStatus(), Status.INFEASIBLE); + assert(dijk_path.getPath() == null); + } + + @Test + /* + * Chemin court sur la carte de Toulouse. + * Tous chemins permis. + */ + public void chemin_court_TLS() { + ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); + Graph myGraph = graph.get(2); + Node origin = myGraph.get(8423); + Node destination = myGraph.get(8435); + ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector); + + DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathSolution dijk_path = dijkstra.doRun(); + + BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data); + ShortestPathSolution bell_path = bellman.doRun(); + + assert(dijk_path.getPath().isValid()); + assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0); + } + + @Test + /* + * Chemin long sur la carte de Toulouse. + * Comme Bellman est long à faire, on utilise un chemin déjà construit qu'on + * importe via la fonction init. Ce chemin a été obtenu par Bellman mais une seule fois. + * Sinon, on peut trouver d'autres techniques pour se rassurer sur le chemin obtenue comme: + * -vérifier certains noeuds comme départ, destination, noeud pivot. + * -vérifier le cout avec une estimation gentille. + * -etc. + */ + public void chemin_long_TLS() { + ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); + Graph myGraph = graph.get(2); + Path myPath = path.get(1); + ShortestPathData data = new ShortestPathData(myGraph, myPath.getOrigin(), myPath.getDestination(), arcInspector); + + DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathSolution dijk_path = dijkstra.doRun(); + + assert(dijk_path.getPath().isValid()); + assert(Math.abs(dijk_path.getPath().getLength() - myPath.getLength()) < 1.0); + } + + @Test + /* + * Test du mode à vélo facultatif. + * Nous prenons une origine sur l'autoroute et une destination en dehors. + * Ce chemin est donc censé être utilisable en vélo mais pas en voiture. + * Avec le filtre vélos, on obtient pas de chemin. + * Avec le filtre voitures on obtient un chemin. + */ + public void chemin_velo_uniquement() { + // Filter: forBicyclesCustomT + ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(4); + + Graph myGraph = graph.get(2); + Node origin = myGraph.get(19135); + Node destination = myGraph.get(1980); + ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector); + + DijkstraAlgorithm dijkstra_bicycle = new DijkstraAlgorithm(data); + ShortestPathSolution dijk_path_bicycle = dijkstra_bicycle.doRun(); + + // Filter: forCarsL + ArcInspector new_Inspector = ArcInspectorFactory.getAllFilters().get(1); + data = new ShortestPathData(myGraph, origin, destination, new_Inspector); + + DijkstraAlgorithm dijkstra_car = new DijkstraAlgorithm(data); + ShortestPathSolution dijk_path_car = dijkstra_car.doRun(); + + assertEquals(dijk_path_bicycle.getPath(), null); + assert(dijk_path_car.getPath() != null); + } + + @Test + /* + * Cette fois-ci, Bellman est trop long à utiliser même une seule fois. + * On va donc utiliser quelques techniques pour se rassurer. + */ + public void chemin_long_Midi_pyrenees() { + ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); + Graph myGraph = graph.get(3); + Path myPath = path.get(0); + ShortestPathData data = new ShortestPathData(myGraph, myPath.getOrigin(), myPath.getDestination(), arcInspector); + + DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data); + ShortestPathSolution dijk_path = dijkstra.doRun(); + + assert(dijk_path.getPath().isValid()); + assert(Math.abs(dijk_path.getPath().getLength() - myPath.getLength()) < 1.0); + } }