update de launch

This commit is contained in:
Tiphaine Pellerin 2026-06-04 20:00:43 +02:00
parent f12e52c93d
commit 2e9b5f4b26
4 changed files with 180 additions and 128 deletions

View file

@ -0,0 +1,20 @@
package org.insa.graphs.gui.simple;
import java.io.IOException;
/**
* Exception thrown when a format-error is detected when reading a graph (e.g.,
* non-matching check bytes).
*/
public class ExceptionCheminImpossible extends IOException {
/**
* Create a new format exception with the given message.
*
* @param message Message for the exception.
*/
public ExceptionCheminImpossible(String message) {
super(message);
}
}

View file

@ -62,7 +62,7 @@ public class Launch {
public static void main(String[] args) throws Exception, ExceptionCheminImpossible { public static void main(String[] args) throws Exception, ExceptionCheminImpossible {
// Chemins vers les cartes (les fichiers .path ne sont plus nécessaires ici) // Chemins vers les cartes (les fichiers .path ne sont plus nécessaires ici)
final String[] mapName = { final String[] mapName = {
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/toulouse.mapgr", "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/toulouse.mapgr",
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr", "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr",
@ -72,38 +72,60 @@ public class Launch {
final Graph[] graph = new Graph[4]; final Graph[] graph = new Graph[4];
Random rand = new Random(); Random rand = new Random();
// for (int i = 0; i < 4; i++) { int[] orig = new int[4];
orig[0] = 9396;
orig[1] = 17670;
orig[2] = 1034;
orig[3] = 3099;
int[] dest = new int[4];
dest[0] = 16820;
dest[1] = 39143;
dest[2] = 1290;
dest[3] = 16818;
int j = 0;
for (int i = 0; i < 4; i++) {
// Lecture du graphe // Lecture du graphe
try (final GraphReader reader = new BinaryGraphReader(new DataInputStream( try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
new BufferedInputStream(new FileInputStream(mapName[0]))))) { new BufferedInputStream(new FileInputStream(mapName[0]))))) {
graph[0] = reader.read(); graph[0] = reader.read();
System.out.println("Carte chargée avec succès : " + mapName[0]); System.out.println("Carte chargée avec succès : " + mapName[0]);
} }
// Création de l'affichage graphique // Création de l'affichage graphique
final Drawing drawing = createDrawing(); final Drawing drawing = createDrawing();
drawing.drawGraph(graph[0]); drawing.drawGraph(graph[0]);
// Nombre de nœuds maximum disponibles sur cette carte // Nombre de uds maximum disponibles sur cette carte
int numNodes = graph[0].getNodes().size()-1; int numNodes = graph[0].size() - 1;
System.out.println("nombre de noeud max : " + numNodes);
// --- DEBUT DES TESTS ALEATOIRES --- // --- DEBUT DES TESTS ALEATOIRES ---
// On crée 10 paires de points aléatoires pour chaque carte // On crée 10 paires de points aléatoires pour chaque carte
int nbTests = 2; int nbTests = 2;
for (int t = 1; t <= nbTests; t++) { for (int t = 1; t <= nbTests; t++) {
Node origin;
Node destination;
// Choix de l'origine et de la destination // Choix de l'origine et de la destination
Node origin = graph[0].getNodes().get(rand.nextInt(numNodes)); if (i == 1 || i == 2) {
Node destination = graph[0].getNodes().get(rand.nextInt(numNodes)); origin = graph[0].getNodes().get(orig[j]);
destination = graph[0].getNodes().get(dest[j]);
j++;
}
else {
origin = graph[0].getNodes().get(rand.nextInt(numNodes));
destination = graph[0].getNodes().get(rand.nextInt(numNodes));
}
System.out.println( System.out.println(String.format(
String.format("\n[Test %d/%d] Origine ID: %d | Destination ID: %d", "\n[Test %d/%d] Origine ID: %d | Destination ID: %d", t,
t, nbTests, origin.getId(), destination.getId())); nbTests, origin.getId(), destination.getId()));
data = new ShortestPathData(graph[0], origin, destination, data = new ShortestPathData(graph[0], origin, destination,
ArcInspectorFactory.getAllFilters().get(1)); ArcInspectorFactory.getAllFilters().get(2));
dijkstra = new DijkstraAlgorithm(data); dijkstra = new DijkstraAlgorithm(data);
bellman = new BellmanFordAlgorithm(data); bellman = new BellmanFordAlgorithm(data);
@ -115,24 +137,25 @@ public class Launch {
// --- VERIFICATION ET COMPARAISON DES RESULTATS --- // --- VERIFICATION ET COMPARAISON DES RESULTATS ---
// Cas 1 : Aucun chemin trouvé par Dijkstra // Cas 1 : Aucun chemin trouvé par Dijkstra
if (!paths.isFeasible() || paths.getPath() == null) { if (!paths.isFeasible() || paths.getPath() == null) {
System.out.println("\n Dijkstra : Aucun chemin trouvé (Infeasible)."); System.out
.println("\n Dijkstra : Aucun chemin trouvé (Infeasible).");
// Alerte de sécurité si un autre algo prétend en trouver un // Alerte de sécurité si un autre algo prétend en trouver un
if (paths2.isFeasible() || paths3.isFeasible()) { if (paths2.isFeasible() || paths3.isFeasible()) {
System.out.println( System.out.println(
"\n Bellman ou A* ont trouvé un chemin alors que Dijkstra dit que c'est impossible !"); "\n Bellman ou A* ont trouvé un chemin alors que Dijkstra dit que c'est impossible !");
} }
throw new ExceptionCheminImpossible("pas de chemin possible"); throw new ExceptionCheminImpossible("pas de chemin possible");
} }
// Cas 2 : Un chemin a été trouvé // Cas 2 : Un chemin a é© trouvé
else { else {
System.out.println("\n Dijkstra : Chemin trouvé !"); System.out.println("\n Dijkstra : Chemin trouvé !");
// Cas particulier : Origine égale à la destination // Cas particulier : Origine égale à la destination
if (origin.equals(destination)) { if (origin.equals(destination)) {
System.out.println("\n L'origine est égale à la destination."); System.out.println("\n L'origine est égale à la destination.");
} }
Path pDijkstra = paths.getPath(); Path pDijkstra = paths.getPath();
@ -145,72 +168,80 @@ public class Launch {
// Comparaison des algos Dijkstra et Bellman-Ford // Comparaison des algos Dijkstra et Bellman-Ford
if (pBellman != null) { if (pBellman != null) {
// test sur les distances (on laisse 1 cm de marge) // test sur les distances (on laisse 1 cm de marge)
if (Math.abs(pDijkstra.getLength() - pBellman.getLength()) < 1e-2) { if (Math.abs(
System.out pDijkstra.getLength() - pBellman.getLength()) < 1e-2) {
.println("\n Même distance entre Dijkstra et Bellman"); System.out.println(
"\n Même distance entre Dijkstra et Bellman");
} }
else { else {
float diff = float diff = Math
Math.abs(pDijkstra.getLength() - pBellman.getLength()); .abs(pDijkstra.getLength() - pBellman.getLength());
System.out.println( System.out.println(
"\n Distance différente entre Dijkstra et Bellman --> différence de" "\n Distance différente entre Dijkstra et Bellman --> différence de"
+ diff); + diff);
} }
// test sur le temps de parcours (on laisse 1 seconde de marge) // test sur le temps de parcours (on laisse 1 seconde de marge)
if (i != 2) {
if (Math.abs(pDijkstra.getMinimumTravelTime() if (Math.abs(pDijkstra.getMinimumTravelTime()
- pBellman.getMinimumTravelTime()) < 1.0) { - pBellman.getMinimumTravelTime()) < 1.0) {
System.out.println( System.out.println(
"\n Même temps de parcours entre Dijkstra et Bellman"); "\n Même temps de parcours entre Dijkstra et Bellman");
} }
else { else {
double diff = Math.abs(pDijkstra.getMinimumTravelTime() double diff = Math.abs(pDijkstra.getMinimumTravelTime()
- pBellman.getMinimumTravelTime()); - pBellman.getMinimumTravelTime());
System.out.println( System.out.println(
"\n Temps différents entre Dijkstra et Bellman --> différence de" "\n Temps différents entre Dijkstra et Bellman --> différence de"
+ diff); + diff);
} }
} }
}
else { else {
System.out.println( System.out.println(
"\n Probleme : Bellman n'a trouvé aucun chemin alors que Dijkstra oui."); "\n Probleme : Bellman n'a trouvé aucun chemin alors que Dijkstra oui.");
} }
// Comparaison des algos Dijkstra et A* // Comparaison des algos Dijkstra et A*
if (pAStar != null) { if (pAStar != null) {
// test sur les distances // test sur les distances
if (Math.abs(pDijkstra.getLength() - pAStar.getLength()) < 1e-2) { if (Math.abs(
System.out.println("\n Même distance entre Dijkstra et A* "); pDijkstra.getLength() - pAStar.getLength()) < 1e-2) {
System.out
.println("\n Même distance entre Dijkstra et A* ");
} }
else { else {
float diff2 = float diff2 = Math
Math.abs(pDijkstra.getLength() - pAStar.getLength()); .abs(pDijkstra.getLength() - pAStar.getLength());
System.out.println( System.out.println(
" Distance différente entre Dijkstra et A* --> différence de " " Distance différente entre Dijkstra et A* --> différence de "
+ diff2); + diff2);
} }
// test sur le temps de parcours // test sur le temps de parcours
if (i != 2) {
if (Math.abs(pDijkstra.getMinimumTravelTime() if (Math.abs(pDijkstra.getMinimumTravelTime()
- pAStar.getMinimumTravelTime()) < 1.0) { - pAStar.getMinimumTravelTime()) < 1.0) {
System.out.println( System.out.println(
"\n Même temps de parcours entre Dijkstra et A*"); "\n Même temps de parcours entre Dijkstra et A*");
} }
else { else {
double diff2 = Math.abs(pDijkstra.getMinimumTravelTime() double diff2 = Math.abs(pDijkstra.getMinimumTravelTime()
- pAStar.getMinimumTravelTime()); - pAStar.getMinimumTravelTime());
System.out.println( System.out.println(
"\n Temps différents entre Dijkstra et A* --> différence de" "\n Temps différents entre Dijkstra et A* --> différence de"
+ diff2); + diff2);
} }
} }
}
else { else {
System.out.println( System.out.println(
"\n Probleme : A* n'a trouvé aucun chemin alors que Dijkstra oui."); "\n Probleme : A* n'a trouvé aucun chemin alors que Dijkstra oui.");
}
} }
} }
} }
// --- CAS : CHEMIN IMPOSSIBLE --- // --- CAS : CHEMIN IMPOSSIBLE ---
// PAS FINI (points à cup sur carte) // PAS FINI (points à ©cup sur carte)
Node origin = graph[0].getNodes().get(27869); Node origin = graph[0].getNodes().get(27869);
Node destination = graph[0].getNodes().get(14534); Node destination = graph[0].getNodes().get(14534);
@ -220,7 +251,7 @@ public class Launch {
origin.getId(), destination.getId())); origin.getId(), destination.getId()));
data = new ShortestPathData(graph[0], origin, destination, data = new ShortestPathData(graph[0], origin, destination,
ArcInspectorFactory.getAllFilters().get(0)); ArcInspectorFactory.getAllFilters().get(2));
dijkstra = new DijkstraAlgorithm(data); dijkstra = new DijkstraAlgorithm(data);
bellman = new BellmanFordAlgorithm(data); bellman = new BellmanFordAlgorithm(data);
@ -233,16 +264,17 @@ public class Launch {
// --- VERIFICATION --- // --- VERIFICATION ---
if (!paths.isFeasible() || paths.getPath() == null) { if (!paths.isFeasible() || paths.getPath() == null) {
System.out.println("\n Dijkstra : Aucun chemin trouvé (Infeasible)."); System.out.println("\n Dijkstra : Aucun chemin trouvé (Infeasible).");
// Alerte de sécurité si un autre algo prétend en trouver un // Alerte de sécurité si un autre algo prétend en trouver un
if (paths2.isFeasible() || paths3.isFeasible()) { if (paths2.isFeasible() || paths3.isFeasible()) {
System.out.println( System.out.println(
"\n Bellman ou A* ont trouvé un chemin alors que Dijkstra dit que c'est impossible !"); "\n Bellman ou A* ont trouvé un chemin alors que Dijkstra dit que c'est impossible !");
} }
throw new ExceptionCheminImpossible("pas de chemin possible"); throw new ExceptionCheminImpossible("pas de chemin possible");
} }
// CAS : DEPART = ARRIVEE // CAS : DEPART = ARRIVEE
int numNodes = graph[0].size() - 1;
Node origine = graph[0].getNodes().get(rand.nextInt(numNodes)); Node origine = graph[0].getNodes().get(rand.nextInt(numNodes));
Node destinations = origine; Node destinations = origine;
@ -263,11 +295,11 @@ public class Launch {
// --- VERIFICATION --- // --- VERIFICATION ---
System.out.println("\n Dijkstra : Chemin trouvé !"); System.out.println("\n Dijkstra : Chemin trouvé !");
// Cas particulier : Origine égale à la destination // Cas particulier : Origine égale à la destination
if (origine.equals(destinations)) { if (origine.equals(destinations)) {
System.out.println("\n L'origine est égale à la destination."); System.out.println("\n L'origine est égale à la destination.");
} }
// --- FIN DES TESTS ALEATOIRES POUR CETTE CARTE --- // --- FIN DES TESTS ALEATOIRES POUR CETTE CARTE ---