une itération de test dijkstra
This commit is contained in:
parent
fc04ffa90b
commit
09e10a87b9
4 changed files with 142 additions and 1 deletions
|
|
@ -0,0 +1,48 @@
|
|||
package org.insa.graphs.algorithm;
|
||||
|
||||
import org.insa.graphs.algorithm.AbstractInputData.Mode;
|
||||
import org.insa.graphs.model.Arc;
|
||||
|
||||
public class MyArcInspector implements ArcInspector {
|
||||
|
||||
private final Mode mode;
|
||||
private final int maxSpeed;
|
||||
|
||||
// Constructeur qui prend en paramètre le mode de transport (TIME ou LENGTH) et la vitesse maximale.
|
||||
public MyArcInspector(Mode mode, int maxSpeed) {
|
||||
this.mode = mode;
|
||||
this.maxSpeed = maxSpeed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAllowed(Arc arc) {
|
||||
// Ici vous pouvez définir des critères personnalisés pour autoriser ou interdire un arc
|
||||
// Par exemple, vous pouvez filtrer selon le type de transport ou d'autres critères
|
||||
return true; // Par défaut, nous autorisons tous les arcs
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getCost(Arc arc) {
|
||||
if (mode == Mode.LENGTH) {
|
||||
// Calcul du coût basé sur la longueur de l'arc (en mètres, par exemple)
|
||||
return arc.getLength();
|
||||
} else if (mode == Mode.TIME) {
|
||||
// Calcul du coût basé sur le temps de parcours
|
||||
// Le temps est calculé comme distance / vitesse maximale
|
||||
double lengthInMeters = arc.getLength(); // longueur de l'arc en mètres
|
||||
double speedInMetersPerSecond = maxSpeed / 3.6; // convertir la vitesse en m/s
|
||||
return lengthInMeters / speedInMetersPerSecond; // Temps = Distance / Vitesse
|
||||
}
|
||||
return Double.POSITIVE_INFINITY; // En cas d'erreur
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaximumSpeed() {
|
||||
return this.maxSpeed; // Retourne la vitesse maximale définie
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mode getMode() {
|
||||
return this.mode; // Retourne le mode de calcul (LENGTH ou TIME)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.insa.graphs.algorithm.shortestpath;
|
||||
/* package org.insa.graphs.algorithm.shortestpath;
|
||||
|
||||
import org.insa.graphs.model.Arc;
|
||||
import org.insa.graphs.model.Node;
|
||||
|
|
@ -14,3 +14,4 @@ public class LabelStar extends Label {
|
|||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
|
@ -10,6 +10,7 @@ import javax.swing.JFrame;
|
|||
import javax.swing.SwingUtilities;
|
||||
|
||||
import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
|
||||
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
|
||||
import org.insa.graphs.gui.drawing.Drawing;
|
||||
import org.insa.graphs.gui.drawing.components.BasicDrawing;
|
||||
import org.insa.graphs.model.Graph;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,91 @@
|
|||
package org.insa.graphs.gui.simple;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.FileInputStream;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
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.DijkstraAlgorithm;
|
||||
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
|
||||
import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
|
||||
import org.insa.graphs.gui.drawing.Drawing;
|
||||
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.io.BinaryGraphReader;
|
||||
import org.insa.graphs.model.io.GraphReader;
|
||||
|
||||
public class TestDijkstra {
|
||||
|
||||
public static Drawing createDrawing() throws Exception {
|
||||
BasicDrawing basicDrawing = new BasicDrawing();
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
JFrame frame = new JFrame("BE Graphes - Launch");
|
||||
frame.setLayout(new BorderLayout());
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setVisible(true);
|
||||
frame.setSize(new Dimension(800, 600));
|
||||
frame.setContentPane(basicDrawing);
|
||||
frame.validate();
|
||||
}
|
||||
});
|
||||
return basicDrawing;
|
||||
}
|
||||
|
||||
public static void test(String mapName, int depart, int arrivee) throws Exception {
|
||||
final Graph graph;
|
||||
final Path path;
|
||||
|
||||
// Créez un lecteur de graphes
|
||||
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
|
||||
graph = reader.read();
|
||||
}
|
||||
|
||||
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);
|
||||
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();
|
||||
|
||||
// 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.");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
test("insa.mapgr", 316,727);
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue