test pour dijkstra

This commit is contained in:
Thior Youssouf-Ben-Abdallah 2025-05-20 11:40:17 +02:00 committed by Thior Youssouf-Ben-Abdallah
parent efeba4104c
commit 2b4130887c

View file

@ -1,12 +1,72 @@
package org.insa.graphes.model; package org.insa.graphs.gui.simple;
import static org.junit.Assert.assertEquals; import java.io.*;
import static org.junit.Assert.assertFalse; import org.insa.graphs.model.*;
import static org.junit.Assert.assertTrue; import org.insa.graphs.model.io.*;
import org.insa.graphs.algorithm.shortestpath.*;
import org.junit.BeforeClass; import org.insa.graphs.algorithm.AbstractSolution.Status;
import org.junit.Test;
@SuppressWarnings("deprecation")
@Test
public class DijkstraTest { 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.");
}
}
}
} }