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,145 +72,176 @@ 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];
// Lecture du graphe orig[0] = 9396;
try (final GraphReader reader = new BinaryGraphReader(new DataInputStream( orig[1] = 17670;
new BufferedInputStream(new FileInputStream(mapName[0]))))) { orig[2] = 1034;
orig[3] = 3099;
int[] dest = new int[4];
dest[0] = 16820;
dest[1] = 39143;
dest[2] = 1290;
dest[3] = 16818;
graph[0] = reader.read(); int j = 0;
System.out.println("Carte chargée avec succès : " + mapName[0]);
}
// Création de l'affichage graphique for (int i = 0; i < 4; i++) {
final Drawing drawing = createDrawing(); // Lecture du graphe
drawing.drawGraph(graph[0]); try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
new BufferedInputStream(new FileInputStream(mapName[0]))))) {
// Nombre de nœuds maximum disponibles sur cette carte graph[0] = reader.read();
int numNodes = graph[0].getNodes().size()-1; System.out.println("Carte chargée avec succès : " + mapName[0]);
// --- DEBUT DES TESTS ALEATOIRES ---
// On crée 10 paires de points aléatoires pour chaque carte
int nbTests = 2;
for (int t = 1; t <= nbTests; t++) {
// Choix de l'origine et de la destination
Node origin = graph[0].getNodes().get(rand.nextInt(numNodes));
Node destination = graph[0].getNodes().get(rand.nextInt(numNodes));
System.out.println(
String.format("\n[Test %d/%d] Origine ID: %d | Destination ID: %d",
t, nbTests, origin.getId(), destination.getId()));
data = new ShortestPathData(graph[0], origin, destination,
ArcInspectorFactory.getAllFilters().get(1));
dijkstra = new DijkstraAlgorithm(data);
bellman = new BellmanFordAlgorithm(data);
AEtoile = new AStarAlgorithm(data);
paths = dijkstra.doRun();
paths2 = bellman.doRun();
paths3 = AEtoile.doRun();
// --- VERIFICATION ET COMPARAISON DES RESULTATS ---
// Cas 1 : Aucun chemin trouvé par Dijkstra
if (!paths.isFeasible() || paths.getPath() == null) {
System.out.println("\n Dijkstra : Aucun chemin trouvé (Infeasible).");
// Alerte de sécurité si un autre algo prétend en trouver un
if (paths2.isFeasible() || paths3.isFeasible()) {
System.out.println(
"\n Bellman ou A* ont trouvé un chemin alors que Dijkstra dit que c'est impossible !");
}
throw new ExceptionCheminImpossible("pas de chemin possible");
} }
// Cas 2 : Un chemin a été trouvé
else {
System.out.println("\n Dijkstra : Chemin trouvé !");
// Cas particulier : Origine égale à la destination // Création de l'affichage graphique
if (origin.equals(destination)) { final Drawing drawing = createDrawing();
System.out.println("\n L'origine est égale à la destination."); drawing.drawGraph(graph[0]);
}
Path pDijkstra = paths.getPath(); // Nombre de uds maximum disponibles sur cette carte
Path pBellman = paths2.getPath(); int numNodes = graph[0].size() - 1;
Path pAStar = paths3.getPath(); System.out.println("nombre de noeud max : " + numNodes);
// Affichage du chemin de Dijkstra sur l'interface // --- DEBUT DES TESTS ALEATOIRES ---
drawing.drawPath(pDijkstra); // On crée 10 paires de points aléatoires pour chaque carte
int nbTests = 2;
// Comparaison des algos Dijkstra et Bellman-Ford for (int t = 1; t <= nbTests; t++) {
if (pBellman != null) { Node origin;
// test sur les distances (on laisse 1 cm de marge) Node destination;
if (Math.abs(pDijkstra.getLength() - pBellman.getLength()) < 1e-2) { // Choix de l'origine et de la destination
System.out if (i == 1 || i == 2) {
.println("\n Même distance entre Dijkstra et Bellman"); origin = graph[0].getNodes().get(orig[j]);
} destination = graph[0].getNodes().get(dest[j]);
else { j++;
float diff =
Math.abs(pDijkstra.getLength() - pBellman.getLength());
System.out.println(
"\n Distance différente entre Dijkstra et Bellman --> différence de"
+ diff);
}
// test sur le temps de parcours (on laisse 1 seconde de marge)
if (Math.abs(pDijkstra.getMinimumTravelTime()
- pBellman.getMinimumTravelTime()) < 1.0) {
System.out.println(
"\n Même temps de parcours entre Dijkstra et Bellman");
}
else {
double diff = Math.abs(pDijkstra.getMinimumTravelTime()
- pBellman.getMinimumTravelTime());
System.out.println(
"\n Temps différents entre Dijkstra et Bellman --> différence de"
+ diff);
}
} }
else { else {
System.out.println( origin = graph[0].getNodes().get(rand.nextInt(numNodes));
"\n Probleme : Bellman n'a trouvé aucun chemin alors que Dijkstra oui."); destination = graph[0].getNodes().get(rand.nextInt(numNodes));
} }
// Comparaison des algos Dijkstra et A* System.out.println(String.format(
if (pAStar != null) { "\n[Test %d/%d] Origine ID: %d | Destination ID: %d", t,
// test sur les distances nbTests, origin.getId(), destination.getId()));
if (Math.abs(pDijkstra.getLength() - pAStar.getLength()) < 1e-2) {
System.out.println("\n Même distance entre Dijkstra et A* ");
} data = new ShortestPathData(graph[0], origin, destination,
else { ArcInspectorFactory.getAllFilters().get(2));
float diff2 =
Math.abs(pDijkstra.getLength() - pAStar.getLength()); dijkstra = new DijkstraAlgorithm(data);
bellman = new BellmanFordAlgorithm(data);
AEtoile = new AStarAlgorithm(data);
paths = dijkstra.doRun();
paths2 = bellman.doRun();
paths3 = AEtoile.doRun();
// --- VERIFICATION ET COMPARAISON DES RESULTATS ---
// Cas 1 : Aucun chemin trouvé par Dijkstra
if (!paths.isFeasible() || paths.getPath() == null) {
System.out
.println("\n Dijkstra : Aucun chemin trouvé (Infeasible).");
// Alerte de ©curité si un autre algo prétend en trouver un
if (paths2.isFeasible() || paths3.isFeasible()) {
System.out.println( System.out.println(
" Distance différente entre Dijkstra et A* --> différence de " "\n Bellman ou A* ont trouvé un chemin alors que Dijkstra dit que c'est impossible !");
+ diff2);
}
// test sur le temps de parcours
if (Math.abs(pDijkstra.getMinimumTravelTime()
- pAStar.getMinimumTravelTime()) < 1.0) {
System.out.println(
"\n Même temps de parcours entre Dijkstra et A*");
}
else {
double diff2 = Math.abs(pDijkstra.getMinimumTravelTime()
- pAStar.getMinimumTravelTime());
System.out.println(
"\n Temps différents entre Dijkstra et A* --> différence de"
+ diff2);
} }
throw new ExceptionCheminImpossible("pas de chemin possible");
} }
// Cas 2 : Un chemin a é© trouvé
else { else {
System.out.println( System.out.println("\n Dijkstra : Chemin trouvé !");
"\n Probleme : A* n'a trouvé aucun chemin alors que Dijkstra oui.");
// Cas particulier : Origine égale à la destination
if (origin.equals(destination)) {
System.out.println("\n L'origine est égale à la destination.");
}
Path pDijkstra = paths.getPath();
Path pBellman = paths2.getPath();
Path pAStar = paths3.getPath();
// Affichage du chemin de Dijkstra sur l'interface
drawing.drawPath(pDijkstra);
// Comparaison des algos Dijkstra et Bellman-Ford
if (pBellman != null) {
// test sur les distances (on laisse 1 cm de marge)
if (Math.abs(
pDijkstra.getLength() - pBellman.getLength()) < 1e-2) {
System.out.println(
"\n Même distance entre Dijkstra et Bellman");
}
else {
float diff = Math
.abs(pDijkstra.getLength() - pBellman.getLength());
System.out.println(
"\n Distance différente entre Dijkstra et Bellman --> différence de"
+ diff);
}
// test sur le temps de parcours (on laisse 1 seconde de marge)
if (i != 2) {
if (Math.abs(pDijkstra.getMinimumTravelTime()
- pBellman.getMinimumTravelTime()) < 1.0) {
System.out.println(
"\n Même temps de parcours entre Dijkstra et Bellman");
}
else {
double diff = Math.abs(pDijkstra.getMinimumTravelTime()
- pBellman.getMinimumTravelTime());
System.out.println(
"\n Temps différents entre Dijkstra et Bellman --> différence de"
+ diff);
}
}
}
else {
System.out.println(
"\n Probleme : Bellman n'a trouvé aucun chemin alors que Dijkstra oui.");
}
// Comparaison des algos Dijkstra et A*
if (pAStar != null) {
// test sur les distances
if (Math.abs(
pDijkstra.getLength() - pAStar.getLength()) < 1e-2) {
System.out
.println("\n Même distance entre Dijkstra et A* ");
}
else {
float diff2 = Math
.abs(pDijkstra.getLength() - pAStar.getLength());
System.out.println(
" Distance différente entre Dijkstra et A* --> différence de "
+ diff2);
}
// test sur le temps de parcours
if (i != 2) {
if (Math.abs(pDijkstra.getMinimumTravelTime()
- pAStar.getMinimumTravelTime()) < 1.0) {
System.out.println(
"\n Même temps de parcours entre Dijkstra et A*");
}
else {
double diff2 = Math.abs(pDijkstra.getMinimumTravelTime()
- pAStar.getMinimumTravelTime());
System.out.println(
"\n Temps différents entre Dijkstra et A* --> différence de"
+ diff2);
}
}
}
else {
System.out.println(
"\n Probleme : A* n'a trouvé aucun chemin alors que Dijkstra oui.");
}
} }
} }
} }
// --- CAS : CHEMIN IMPOSSIBLE --- // --- CAS : CHEMIN IMPOSSIBLE ---
// PAS FINI (points à ré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 ---