finalisation des tests automatisés dijkstra

This commit is contained in:
bezza 2025-05-19 17:27:55 +02:00
parent 1beedf814d
commit c73f482d05

View file

@ -13,6 +13,7 @@ import org.insa.graphs.algorithm.AbstractInputData.Mode;
import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.algorithm.ArcInspectorFactory;
import org.insa.graphs.algorithm.MyArcInspector;
import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
@ -21,8 +22,11 @@ import org.insa.graphs.gui.drawing.components.BasicDrawing;
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.BinaryPathReader;
import org.insa.graphs.model.io.GraphReader;
import org.insa.graphs.model.io.PathReader;
public class TestDijkstra {
@ -43,49 +47,122 @@ public class TestDijkstra {
return basicDrawing;
}
public static void test(String mapName, int depart, int arrivee) throws Exception {
// fonction pour tester un scénario
public static void testScenario(String map, int depart, int arrivee, Mode mode, boolean petitGraphe) throws Exception {
final Graph graph;
final Path path;
// Créez un lecteur de graphes
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/" + mapName))))) {
// Lire le graphe
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);
// Créer les données d'entrée pour Dijkstra (origine, destination, et graph)
int maxSpeed = 130; // La vitesse maximale en km/h, par exemple
// Créer un inspecteur pour le mode de calcul basé sur le temps
ArcInspector arcInspector = new MyArcInspector(Mode.TIME, maxSpeed);
ArcInspector arcInspector = new MyArcInspector(mode, maxSpeed);
ShortestPathData data = new ShortestPathData(graph, origin, destination, arcInspector);
// Créer et exécuter l'algorithme Dijkstra
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
ShortestPathSolution solution = dijkstra.run();
// Vérifier la solution pour savoir si on la trace ou pas.
if (solution.getStatus() == ShortestPathSolution.Status.OPTIMAL) {
// Récupérez le chemin résultant de la solution
path = solution.getPath();
// Vérif validité du chemin
boolean valid = path.isValid();
System.out.println("Chemin Dijkstra valide ? " + valid);
// Récupérer la liste des noeuds du chemin trouvé
java.util.List<Node> 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();
}
System.out.println("Coût Dijkstra: " + coutAlgo);
boolean coutOk = Math.abs(coutAlgo - coutPath) < 1e-6; // pour comparer des réels
if(coutOk){
System.out.println("Coût Dijkstra vs Path: " + coutAlgo + " vs " + coutPath + " => OK");
} else {
System.out.println("Coût Dijkstra 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 Dijkstra == 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 Dijkstra == Bellman-Ford ? OK");
} else {
System.out.println("Coût Dijkstra == Bellman-Ford ? (différence entre Bellman et Dijkstra.)");
}
}
}
// Affichage graphique
/* final Drawing drawing = createDrawing();
drawing.drawGraph(graph);
drawing.drawPath(path); */
// On dessine
final Drawing drawing = createDrawing();
drawing.drawGraph(graph);
drawing.drawPath(path);
} else {
// Si aucune solution optimale n'est trouvée
System.out.println("Aucun chemin trouvé ou la solution est infaisable.");
System.out.println("Aucun chemin trouvé.");
}
System.out.println();
}
public static void main(String[] args) throws Exception {
test("insa.mapgr", 316,727);
//cartes non routières
System.out.println("== Chemin de longueur nulle ==");
testScenario("carre.mapgr", 9, 9, Mode.LENGTH, true);
System.out.println("== Sommet hors du graphe ==");
testScenario("carre.mapgr", 0, 9999, Mode.LENGTH, true);
//cartes routières
System.out.println("== Test en distance ==");
testScenario("insa.mapgr", 369, 838, Mode.LENGTH, true);
System.out.println("== Test en temps ==");
testScenario("insa.mapgr", 369, 838, Mode.TIME, true);
// autres scénarios
//System.out.println("== Trajet long (et pénible avec les enfants) ==");
// testScenario("bretagne.mapgr",48233,135047 , Mode.TIME, false);
}
}