import java.awt.BorderLayout; import java.awt.Dimension; import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.FileInputStream; import javax.swing.JFrame; import javax.swing.SwingUtilities; import org.insa.graphs.algorithm.AbstractInputData.Mode; import org.insa.graphs.algorithm.ArcInspector; import org.insa.graphs.algorithm.MyArcInspector; import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm; import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm; import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm; import org.insa.graphs.algorithm.shortestpath.ProblemeOuvert; import org.insa.graphs.algorithm.shortestpath.ShortestPathData; import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution; import org.insa.graphs.gui.drawing.Drawing; import org.insa.graphs.gui.drawing.components.BasicDrawing; import org.insa.graphs.gui.simple.Launch; import org.insa.graphs.model.Graph; import org.insa.graphs.model.Node; import org.insa.graphs.model.Path; import org.insa.graphs.model.Arc; import org.insa.graphs.model.io.BinaryGraphReader; import org.insa.graphs.model.io.GraphReader; public class TestDijkstra { public static Drawing createDrawing() throws Exception { BasicDrawing basicDrawing = new BasicDrawing(); SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { JFrame frame = new JFrame("BE Graphes - Launch"); frame.setLayout(new BorderLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setSize(new Dimension(800, 600)); frame.setContentPane(basicDrawing); frame.validate(); } }); return basicDrawing; } // fonction pour tester un scénario: type: 0 dijkstra, 1 Astar, 2 ProblemeOuvert public static void testScenario(String map, int depart, int arrivee, Mode mode, boolean petitGraphe, int type, boolean restreint) throws Exception { final Graph graph; final Path path; int maxSpeed = 130; // La vitesse maximale en km/h try (final GraphReader reader = new BinaryGraphReader(new DataInputStream( new BufferedInputStream(new FileInputStream("/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/" +map))))) { graph = reader.read(); } if (depart >= graph.size() || arrivee >= graph.size()) { System.out.println("Origine ou destination hors du graphe."); System.out.println(); return; //pour sortir de la fonction } Node origin = graph.getNodes().get(depart); Node destination = graph.getNodes().get(arrivee); ArcInspector arcInspector = new MyArcInspector(mode, maxSpeed,restreint); ShortestPathData data = new ShortestPathData(graph, origin, destination, arcInspector); DijkstraAlgorithm dijkstra; switch (type) { case 1: dijkstra = new AStarAlgorithm(data); break; case 2: dijkstra = new ProblemeOuvert(data); break; default: dijkstra = new DijkstraAlgorithm(data); break; } // Affichage graphique final Drawing drawing = Launch.createDrawing(); drawing.drawGraph(graph); ShortestPathSolution solution = dijkstra.run(); if (solution.getStatus() == ShortestPathSolution.Status.OPTIMAL) { path = solution.getPath(); // Vérif validité du chemin boolean valid = path.isValid(); System.out.println("Chemin Algo valide ? " + valid); // Récupérer la liste des noeuds du chemin trouvé java.util.List nodeList = new java.util.ArrayList<>(); if (!path.isEmpty()) { nodeList.add(origin); for (Arc arc : path.getArcs()) { nodeList.add(arc.getDestination()); } } // construire le chemin avec la classe Path Path pathFromNodes; double coutAlgo; double coutPath; if (mode == Mode.TIME) { pathFromNodes = Path.createFastestPathFromNodes(graph, nodeList); coutAlgo = path.getMinimumTravelTime(); coutPath = pathFromNodes.getMinimumTravelTime(); } else { pathFromNodes = Path.createShortestPathFromNodes(graph, nodeList); coutAlgo = path.getLength(); coutPath = pathFromNodes.getLength(); } switch (type) { case 1: System.out.println("Coût DijkstraAstar: " + coutAlgo); break; case 2: System.out.println("Coût ProblemeOuvert: " + coutAlgo); break; default: System.out.println("Coût Dijkstra: " + coutAlgo); break; } boolean coutOk = Math.abs(coutAlgo - coutPath) < 1e-6; // pour comparer des réels if(coutOk){ System.out.println("Coût Algo vs Path: " + coutAlgo + " vs " + coutPath + " => OK"); } else { System.out.println("Coût Algo vs Path: " + coutAlgo + " vs " + coutPath + " => (différence attendue)"); } // Pour petits graphes, comparaison Bellman-Ford if (petitGraphe) { BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data); ShortestPathSolution solBellman = bellman.run(); boolean memeStatut = solution.getStatus() == solBellman.getStatus(); System.out.println("Statut Algo == Bellman-Ford ? " + memeStatut); if (solBellman.getPath() != null) { double coutBellman; if (mode==Mode.TIME) { coutBellman=solBellman.getPath().getMinimumTravelTime(); } else { coutBellman=solBellman.getPath().getLength(); } boolean memeCout = Math.abs(coutAlgo - coutBellman) < 1e-6; //méthode pour comparer des floats. if(memeCout){ System.out.println("Coût Algo == Bellman-Ford ? OK"); } else { System.out.println("Coût Algo == Bellman-Ford ? (différence présente entre Bellman et Algo.)"); } } } drawing.drawPath(path); } else { System.out.println("Aucun chemin trouvé."); } System.out.println(); } public static void main(String[] args) throws Exception { //cartes non routières System.out.println("== Chemin de longueur nulle =="); testScenario("carre.mapgr", 9, 9, Mode.LENGTH, true,0,false); System.out.println("== Sommet hors du graphe =="); testScenario("carre.mapgr", 0, 9999, Mode.LENGTH, true,0,false); //cartes routières System.out.println("== Test en distance =="); testScenario("insa.mapgr", 369, 838, Mode.LENGTH, true,0,false); System.out.println("== Test en temps =="); testScenario("insa.mapgr", 369, 838, Mode.TIME, true,0,false); // autres scénarios System.out.println("== Trajet long (et pénible avec les enfants) =="); testScenario("bretagne.mapgr",564429,602395 , Mode.LENGTH, false,0,false); System.out.println("== Trajet impossible (piste cyclable) =="); testScenario("insa.mapgr",90,922 , Mode.LENGTH, false,0,true); //tests probleme ouvert System.out.println("== Test Probleme Ouvert =="); testScenario("toulouse.mapgr",33056,16303 , Mode.TIME, false,2,false); System.out.println("== Test Probleme Ouvert =="); testScenario("bretagne.mapgr",165317,74644 , Mode.TIME, false,2,false); System.out.println("== Test Probleme Ouvert =="); testScenario("bretagne.mapgr",165317,74644 , Mode.TIME, false,2,false); } }