finalisation de la classe de tests

This commit is contained in:
Tiphaine Pellerin 2026-06-05 08:46:31 +02:00
parent 1747043e2a
commit b345659e13
3 changed files with 58 additions and 54 deletions

View file

@ -24,8 +24,6 @@ import org.insa.graphs.model.Path;
import org.insa.graphs.model.io.BinaryGraphReader;
import org.insa.graphs.model.io.GraphReader;
import org.insa.graphs.gui.simple.ExceptionCheminImpossible;
public class Launch {
/**
@ -62,14 +60,10 @@ public class Launch {
public static void main(String[] args) throws Exception, ExceptionCheminImpossible {
// Chemins vers les cartes (les fichiers .path ne sont plus ©cessaires ici)
final String[] mapName = {
"/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/toulouse.mapgr",
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/france.mapgr" };
// Chemins vers les cartes (les fichiers .path ne sont plus nécessaires ici)
final String mapName = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/toulouse.mapgr";
final Graph[] graph = new Graph[4];
Graph graph ;
Random rand = new Random();
int[] orig = new int[4];
@ -88,35 +82,35 @@ public class Launch {
for (int i = 0; i < 4; i++) {
// Lecture du graphe
try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
new BufferedInputStream(new FileInputStream(mapName[0]))))) {
new BufferedInputStream(new FileInputStream(mapName))))) {
graph[0] = reader.read();
System.out.println("Carte chargée avec succès : " + mapName[0]);
graph = reader.read();
System.out.println("Carte chargée avec succès : " + mapName);
}
// Création de l'affichage graphique
// Création de l'affichage graphique
final Drawing drawing = createDrawing();
drawing.drawGraph(graph[0]);
drawing.drawGraph(graph);
// Nombre de nÅuds maximum disponibles sur cette carte
int numNodes = graph[0].size() - 1;
// Nombre de nœuds maximum disponibles sur cette carte
int numNodes = graph.size() - 1;
System.out.println("nombre de noeud max : " + numNodes);
// --- DEBUT DES TESTS ALEATOIRES ---
// On crée 10 paires de points aléatoires pour chaque carte
// On crée 2 paires de points aléatoires pour chaque carte
int nbTests = 2;
for (int t = 1; t <= nbTests; t++) {
Node origin;
Node destination;
// Choix de l'origine et de la destination
if (i == 1 || i == 2) {
origin = graph[0].getNodes().get(orig[j]);
destination = graph[0].getNodes().get(dest[j]);
origin = graph.getNodes().get(orig[j]);
destination = graph.getNodes().get(dest[j]);
j++;
}
else {
origin = graph[0].getNodes().get(rand.nextInt(numNodes));
destination = graph[0].getNodes().get(rand.nextInt(numNodes));
origin = graph.getNodes().get(rand.nextInt(numNodes));
destination = graph.getNodes().get(rand.nextInt(numNodes));
}
System.out.println(String.format(
@ -124,7 +118,7 @@ public class Launch {
nbTests, origin.getId(), destination.getId()));
data = new ShortestPathData(graph[0], origin, destination,
data = new ShortestPathData(graph, origin, destination,
ArcInspectorFactory.getAllFilters().get(2));
dijkstra = new DijkstraAlgorithm(data);
@ -137,25 +131,25 @@ public class Launch {
// --- 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) {
System.out
.println("\n Dijkstra : Aucun chemin trouvé (Infeasible).");
.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()) {
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");
}
// Cas 2 : Un chemin a é© trouvé
// Cas 2 : Un chemin a été trouvé
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)) {
System.out.println("\n L'origine est égale à la destination.");
System.out.println("\n L'origine est égale à la destination.");
}
Path pDijkstra = paths.getPath();
@ -171,13 +165,13 @@ public class Launch {
if (Math.abs(
pDijkstra.getLength() - pBellman.getLength()) < 1e-2) {
System.out.println(
"\n Même distance entre Dijkstra et Bellman");
"\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"
"\n Distance différente entre Dijkstra et Bellman --> différence de"
+ diff);
}
// test sur le temps de parcours (on laisse 1 seconde de marge)
@ -185,20 +179,20 @@ public class Launch {
if (Math.abs(pDijkstra.getMinimumTravelTime()
- pBellman.getMinimumTravelTime()) < 1.0) {
System.out.println(
"\n Même temps de parcours entre Dijkstra et Bellman");
"\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"
"\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.");
"\n Probleme : Bellman n'a trouvé aucun chemin alors que Dijkstra oui.");
}
// Comparaison des algos Dijkstra et A*
@ -207,13 +201,13 @@ public class Launch {
if (Math.abs(
pDijkstra.getLength() - pAStar.getLength()) < 1e-2) {
System.out
.println("\n Même distance entre Dijkstra et A* ");
.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 "
" Distance différente entre Dijkstra et A* --> différence de "
+ diff2);
}
// test sur le temps de parcours
@ -221,36 +215,46 @@ public class Launch {
if (Math.abs(pDijkstra.getMinimumTravelTime()
- pAStar.getMinimumTravelTime()) < 1.0) {
System.out.println(
"\n Même temps de parcours entre Dijkstra et A*");
"\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"
"\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.");
"\n Probleme : A* n'a trouvé aucun chemin alors que Dijkstra oui.");
}
}
}
}
// --- CAS : CHEMIN IMPOSSIBLE ---
// PAS FINI (points à ©cup sur carte)
// PAS FINI (points à récup sur carte)
try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
new BufferedInputStream(new FileInputStream(mapName))))) {
Node origin = graph[0].getNodes().get(27869);
Node destination = graph[0].getNodes().get(14534);
graph = reader.read();
System.out.println("Carte chargée avec succès : " + mapName);
}
// Création de l'affichage graphique
final Drawing drawing = createDrawing();
drawing.drawGraph(graph);
Node origin = graph.getNodes().get(27869);
Node destination = graph.getNodes().get(14534);
System.out.println(String.format(
"\n[Test chemin impossible] Origine ID: %d | Destination ID: %d",
origin.getId(), destination.getId()));
data = new ShortestPathData(graph[0], origin, destination,
data = new ShortestPathData(graph, origin, destination,
ArcInspectorFactory.getAllFilters().get(2));
dijkstra = new DijkstraAlgorithm(data);
@ -264,25 +268,25 @@ public class Launch {
// --- VERIFICATION ---
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
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 !");
"\n Bellman ou A* ont trouvé un chemin alors que Dijkstra dit que c'est impossible !");
}
throw new ExceptionCheminImpossible("pas de chemin possible");
}
// CAS : DEPART = ARRIVEE
int numNodes = graph[0].size() - 1;
Node origine = graph[0].getNodes().get(rand.nextInt(numNodes));
int numNodes = graph.size() - 1;
Node origine = graph.getNodes().get(rand.nextInt(numNodes));
Node destinations = origine;
System.out.println(String.format(
"\n[Test origine = dest] Origine ID: %d | Destination ID: %d",
origine.getId(), destinations.getId()));
data = new ShortestPathData(graph[0], origine, destinations,
data = new ShortestPathData(graph, origine, destinations,
ArcInspectorFactory.getAllFilters().get(0));
dijkstra = new DijkstraAlgorithm(data);
@ -295,14 +299,14 @@ public class Launch {
// --- 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)) {
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 ---
// }
}
}
}