From 2b4130887cd7b1dd8c2b40efaa2427e3834158fb Mon Sep 17 00:00:00 2001 From: Thior Youssouf-Ben-Abdallah Date: Tue, 20 May 2025 11:40:17 +0200 Subject: [PATCH] test pour dijkstra --- be-graphes-algos/src/test/DijkstraTest.java | 74 +++++++++++++++++++-- 1 file changed, 67 insertions(+), 7 deletions(-) diff --git a/be-graphes-algos/src/test/DijkstraTest.java b/be-graphes-algos/src/test/DijkstraTest.java index b8bcc00..f77b699 100644 --- a/be-graphes-algos/src/test/DijkstraTest.java +++ b/be-graphes-algos/src/test/DijkstraTest.java @@ -1,12 +1,72 @@ -package org.insa.graphes.model; +package org.insa.graphs.gui.simple; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import java.io.*; +import org.insa.graphs.model.*; +import org.insa.graphs.model.io.*; +import org.insa.graphs.algorithm.shortestpath.*; -import org.junit.BeforeClass; -import org.junit.Test; +import org.insa.graphs.algorithm.AbstractSolution.Status; +@SuppressWarnings("deprecation") +@Test public class DijkstraTest { - + + public static void main(String[] args) throws Exception { + + // === Chemins vers les fichiers de test === + final String mapPath = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr"; + final String pathPath = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path"; + + // === Chargement du graphe === + Graph graph; + try (GraphReader reader = new BinaryGraphReader( + new DataInputStream(new BufferedInputStream(new FileInputStream(mapPath))))) { + graph = reader.read(); + } + + // === Chargement du chemin de référence === + Path referencePath; + try (PathReader pathReader = new BinaryPathReader( + new DataInputStream(new BufferedInputStream(new FileInputStream(pathPath))))) { + referencePath = pathReader.readPath(graph); + } + + // === données pour Dijkstra === + Node origin = referencePath.getOrigin(); + Node destination = referencePath.getDestination(); + + ShortestPathData data = new ShortestPathData(graph, origin, destination, ArcInspectorFactory.getAllFilters().get(0)); + DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data); + + // === Exécution de l'algorithme === + ShortestPathSolution solution = dijkstra.run(); + + + // === Affichage des résultats === + if (solution.getStatus() == Status.OPTIMAL) { + System.out.println("Dijkstra: Chemin trouvé !"); + System.out.println("Longueur: " + solution.getPath().getLength()); + System.out.println("Temps estimé (sec): " + solution.getPath().getMinimumTravelTime()); + } else { + System.out.println("Dijkstra: Pas de chemin trouvé."); + } + + // === Comparaison avec Bellman-Ford ca peut etre ameliore === + BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data); + ShortestPathSolution bellmanSolution = bellman.run(); + + if (bellmanSolution.getStatus() == Status.OPTIMAL) { + System.out.println("Bellman-Ford: Chemin trouvé !"); + System.out.println("Longueur: " + bellmanSolution.getPath().getLength()); + } + + // === Comparaison en distance et vitesse === + if (solution.getPath() != null && bellmanSolution.getPath() != null) { + if (Math.abs(solution.getPath().getLength() - bellmanSolution.getPath().getLength()) < 0.001) { + System.out.println("Les deux algorithmes donnent la même longueur."); + } else { + System.out.println("Les longueurs sont différentes."); + } + } + } }