update de launch
This commit is contained in:
parent
f12e52c93d
commit
2e9b5f4b26
4 changed files with 180 additions and 128 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -62,7 +62,7 @@ public class Launch {
|
|||
|
||||
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 = {
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/toulouse.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];
|
||||
Random rand = new Random();
|
||||
|
||||
// for (int i = 0; i < 4; i++) {
|
||||
// Lecture du graphe
|
||||
try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
|
||||
new BufferedInputStream(new FileInputStream(mapName[0]))))) {
|
||||
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;
|
||||
|
||||
graph[0] = reader.read();
|
||||
System.out.println("Carte chargée avec succès : " + mapName[0]);
|
||||
}
|
||||
int j = 0;
|
||||
|
||||
// Création de l'affichage graphique
|
||||
final Drawing drawing = createDrawing();
|
||||
drawing.drawGraph(graph[0]);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
// Lecture du graphe
|
||||
try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
|
||||
new BufferedInputStream(new FileInputStream(mapName[0]))))) {
|
||||
|
||||
// Nombre de nœuds maximum disponibles sur cette carte
|
||||
int numNodes = graph[0].getNodes().size()-1;
|
||||
|
||||
// --- 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");
|
||||
graph[0] = reader.read();
|
||||
System.out.println("Carte chargée avec succès : " + mapName[0]);
|
||||
}
|
||||
// Cas 2 : Un chemin a été trouvé
|
||||
else {
|
||||
System.out.println("\n Dijkstra : Chemin trouvé !");
|
||||
|
||||
// Cas particulier : Origine égale à la destination
|
||||
if (origin.equals(destination)) {
|
||||
System.out.println("\n L'origine est égale à la destination.");
|
||||
}
|
||||
// Création de l'affichage graphique
|
||||
final Drawing drawing = createDrawing();
|
||||
drawing.drawGraph(graph[0]);
|
||||
|
||||
Path pDijkstra = paths.getPath();
|
||||
Path pBellman = paths2.getPath();
|
||||
Path pAStar = paths3.getPath();
|
||||
// Nombre de nœuds maximum disponibles sur cette carte
|
||||
int numNodes = graph[0].size() - 1;
|
||||
System.out.println("nombre de noeud max : " + numNodes);
|
||||
|
||||
// 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 (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);
|
||||
}
|
||||
// --- 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++) {
|
||||
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]);
|
||||
j++;
|
||||
}
|
||||
else {
|
||||
System.out.println(
|
||||
"\n Probleme : Bellman n'a trouvé aucun chemin alors que Dijkstra oui.");
|
||||
origin = graph[0].getNodes().get(rand.nextInt(numNodes));
|
||||
destination = graph[0].getNodes().get(rand.nextInt(numNodes));
|
||||
}
|
||||
|
||||
// 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(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(2));
|
||||
|
||||
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(
|
||||
" Distance différente entre Dijkstra et A* --> différence de "
|
||||
+ 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);
|
||||
"\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 Probleme : A* n'a trouvé aucun chemin alors que Dijkstra oui.");
|
||||
System.out.println("\n Dijkstra : Chemin trouvé !");
|
||||
|
||||
// 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 ---
|
||||
// PAS FINI (points à récup sur carte)
|
||||
// PAS FINI (points à récup sur carte)
|
||||
|
||||
Node origin = graph[0].getNodes().get(27869);
|
||||
Node destination = graph[0].getNodes().get(14534);
|
||||
|
|
@ -220,7 +251,7 @@ public class Launch {
|
|||
origin.getId(), destination.getId()));
|
||||
|
||||
data = new ShortestPathData(graph[0], origin, destination,
|
||||
ArcInspectorFactory.getAllFilters().get(0));
|
||||
ArcInspectorFactory.getAllFilters().get(2));
|
||||
|
||||
dijkstra = new DijkstraAlgorithm(data);
|
||||
bellman = new BellmanFordAlgorithm(data);
|
||||
|
|
@ -233,16 +264,17 @@ 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));
|
||||
Node destinations = origine;
|
||||
|
||||
|
|
@ -263,14 +295,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 ---
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Loading…
Reference in a new issue