be_graphes version finale contenant labelStar, Astar, et la classe Launch

This commit is contained in:
Benmouffok Helwen 2026-06-07 20:55:42 +02:00
parent cad511386b
commit c9cb1ae65e
5 changed files with 274 additions and 129 deletions

View file

@ -1,9 +1,14 @@
package org.insa.graphs.algorithm.shortestpath;
public class AStarAlgorithm extends DijkstraAlgorithm {
import org.insa.graphs.model.Node;
public class AStarAlgorithm extends DijkstraAlgorithm {
public AStarAlgorithm(ShortestPathData data) {
super(data);
}
}
@Override
protected Label createLabel(Node node, ShortestPathData data) {
return new LabelStar(node, data.getDestination());
}
}

View file

@ -2,7 +2,6 @@ package org.insa.graphs.algorithm.shortestpath;
import java.util.ArrayList;
import java.util.Collections;
import org.insa.graphs.algorithm.AbstractSolution.Status;
import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.model.Arc;
@ -11,80 +10,68 @@ import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
public DijkstraAlgorithm(ShortestPathData data) {
super(data);
}
protected Label createLabel(Node node, ShortestPathData data) {
return new Label(node);
}
@Override
protected ShortestPathSolution doRun() {
// retrieve data from the input problem (getInputData() is inherited from the
// parent class ShortestPathAlgorithm)
final ShortestPathData data = getInputData();
// variable that will contain the solution of the shortest path problem
ShortestPathSolution solution = null;
Graph graph = data.getGraph();
final int nbNodes = graph.size();
//Remplir le tableau
Label[] labels=new Label[nbNodes];
for (Node node:graph.getNodes()){
labels[node.getId()]=new Label(node);
// Cas special : origine == destination
if (data.getOrigin().getId() == data.getDestination().getId()) {
return new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, data.getOrigin()));
}
// Remplir le tableau
Label[] labels = new Label[nbNodes];
for (Node node : graph.getNodes()) {
labels[node.getId()] = createLabel(node, data);
}
BinaryHeap<Label> tas = new BinaryHeap<>();
labels[data.getOrigin().getId()].setCoutRealise(0);
notifyOriginProcessed(data.getOrigin());
tas.insert(labels[data.getOrigin().getId()]);
while (!tas.isEmpty()) {
Label labelMin = tas.deleteMin();
labelMin.setMarque();
notifyNodeMarked(labelMin.getSommetCourant());
if (labelMin.getSommetCourant().equals(data.getDestination())) {
break;
}
BinaryHeap<Label> tas=new BinaryHeap<>();
labels[data.getOrigin().getId()].setCoutRealise(0);
tas.insert(labels[data.getOrigin().getId()]);
while(!tas.isEmpty()){
Label labelMin=tas.deleteMin();
labelMin.setMarque();
if (labelMin.getSommetCourant().equals(data.getDestination())){
break;
}
for (Arc arc : labelMin.getSommetCourant().getSuccessors()) {
if (!data.isAllowed(arc)) continue;
if (!data.isAllowed(arc))
continue;
Label labelSucc = labels[arc.getDestination().getId()];
if (!labelSucc.isMarque()) {
double nouveauCout = labelMin.getCoutRealise() + data.getCost(arc);
if (nouveauCout < labelSucc.getCoutRealise()) {
boolean dejaDansTas = Double.isFinite(labelSucc.getCoutRealise());
if (dejaDansTas) {
tas.remove(labelSucc);
tas.remove(labelSucc);
} else {
notifyNodeReached(arc.getDestination());
}
labelSucc.setCoutRealise(nouveauCout);
labelSucc.setCoutRealise(nouveauCout);
labelSucc.setPere(arc);
tas.insert(labelSucc);
}
}
}
}
// Construction de la solution
}
// Construction de la solution
if (labels[data.getDestination().getId()].getPere() == null) {
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
} else {
notifyDestinationReached(data.getDestination());
ArrayList<Arc> arcs = new ArrayList<>();
Arc arc = labels[data.getDestination().getId()].getPere();
while (arc != null) {
@ -94,9 +81,6 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
Collections.reverse(arcs);
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
}
return solution;
}
}
}

View file

@ -3,55 +3,53 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node;
public class Label implements Comparable<Label>{
public class Label implements Comparable<Label> {
private Node sommetCourant;
private boolean marque;
private double coutRealise;
private Arc pere;
public Label(Node sommetCourant) {
this.sommetCourant=sommetCourant;
this.marque=false;
this.coutRealise=Double.POSITIVE_INFINITY;
this.pere=null;
}
public Node getSommetCourant(){
return this.sommetCourant;
}
public boolean isMarque(){
return this.marque;
this.sommetCourant = sommetCourant;
this.marque = false;
this.coutRealise = Double.POSITIVE_INFINITY;
this.pere = null;
}
public double getCoutRealise(){
return this.coutRealise;
}
public Arc getPere(){
return this.pere;
}
public void setMarque(){
this.marque=true;
}
public void setCoutRealise(double coutRealise){
this.coutRealise=coutRealise;
}
public void setPere(Arc pere){
this.pere=pere;
}
public double getCost(){
return this.coutRealise;
}
public Node getSommetCourant() {
return this.sommetCourant;
}
@Override
public int compareTo(Label other){
return Double.compare(this.getCost(), other.getCost());
public boolean isMarque() {
return this.marque;
}
}
}
public double getCoutRealise() {
return this.coutRealise;
}
public Arc getPere() {
return this.pere;
}
public void setMarque() {
this.marque = true;
}
public void setCoutRealise(double coutRealise) {
this.coutRealise = coutRealise;
}
public void setPere(Arc pere) {
this.pere = pere;
}
public double getTotalCost() {
return this.coutRealise;
}
@Override
public int compareTo(Label other) {
return Double.compare(this.getTotalCost(), other.getTotalCost());
}
}

View file

@ -1,12 +1,30 @@
//import org.insa.graphs.model.Arc;
//import org.insa.graphs.model.Node;
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Node;
public class LabelStar extends Label {
private final double heuristique;
//package org.insa.graphs.algorithm.shortestpath;
public LabelStar(Node sommetCourant, Node destination) {
super(sommetCourant);
this.heuristique = sommetCourant.getPoint().distanceTo(destination.getPoint());
}
//import org.insa.graphs.model.Arc;
public double getHeuristique() {
return this.heuristique;
}
//blic class LabelStar extends Label
//ublic LabelStar()
@Override
public double getTotalCost() {
return this.getCoutRealise() + this.heuristique;
}
@Override
public int compareTo(Label other) {
int cmp = Double.compare(this.getTotalCost(), other.getTotalCost());
if (cmp == 0 && other instanceof LabelStar) {
return Double.compare(this.heuristique, ((LabelStar) other).heuristique);
}
return cmp;
}
}

View file

@ -5,15 +5,17 @@ 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.ArcInspectorFactory;
import org.insa.graphs.algorithm.AbstractSolution.Status;
import org.insa.graphs.algorithm.shortestpath.*;
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.Path;
import org.insa.graphs.model.io.BinaryGraphReader;
import org.insa.graphs.model.io.BinaryPathReader;
import org.insa.graphs.model.io.GraphReader;
import org.insa.graphs.model.io.PathReader;
@ -42,38 +44,176 @@ public class Launch {
return basicDrawing;
}
public static void main(String[] args) throws Exception {
// Un scenario est defini par une carte, un filtre, une origine, une destination
// et optionnellement un fichier .path de reference pour valider le resultat
static class Scenario {
final String carte;
final String cheminRef;
final int filtre;
final int origine, destination;
final String description;
// visit these directory to see the list of available files on commetud.
final String mapName =
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
final String pathName =
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path";
final Graph graph;
final Path path;
// create a graph reader
try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
new BufferedInputStream(new FileInputStream(mapName))))) {
// TODO: read the graph
graph = null;
Scenario(String carte, String cheminRef, int filtre,
int origine, int destination, String description) {
this.carte = carte;
this.cheminRef = cheminRef;
this.filtre = filtre;
this.origine = origine;
this.destination = destination;
this.description = description;
}
// create the drawing
final Drawing drawing = createDrawing();
// TODO: draw the graph on the drawing
// TODO: create a path reader
try (final PathReader pathReader = null) {
// TODO: read the path
path = null;
}
// TODO: draw the path on the drawing
}
}
// Lance Dijkstra, A* et Bellman-Ford sur un scenario et affiche les resultats
public static Graph testerScenario(Scenario sc) throws Exception {
System.out.println("\n=== " + sc.description + " ===");
// create a graph reader
Graph graph;
try (final GraphReader reader = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(
new FileInputStream(sc.carte))))) {
graph = reader.read();
}
ShortestPathData data = new ShortestPathData(
graph,
graph.get(sc.origine),
graph.get(sc.destination),
ArcInspectorFactory.getAllFilters().get(sc.filtre)
);
// On lance les trois algorithmes
ShortestPathSolution solDijkstra = new DijkstraAlgorithm(data).run();
ShortestPathSolution solAstar = new AStarAlgorithm(data).run();
ShortestPathSolution solBellman = new BellmanFordAlgorithm(data).run();
// Affichage des statuts
System.out.println("Dijkstra : " + solDijkstra.getStatus());
System.out.println("A* : " + solAstar.getStatus());
System.out.println("Bellman : " + solBellman.getStatus());
boolean estTemps = (sc.filtre == 2 || sc.filtre == 3);
if (solDijkstra.getStatus() == Status.OPTIMAL) {
Path pd = solDijkstra.getPath();
Path pa = solAstar.getPath();
// Verification que les chemins sont valides
System.out.println("Dijkstra chemin valide : " + pd.isValid());
System.out.println("A* chemin valide : " + pa.isValid());
// Calcul des couts selon le mode distance ou temps
double coutDijkstra = estTemps ? pd.getMinimumTravelTime() : pd.getLength();
double coutAstar = estTemps ? pa.getMinimumTravelTime() : pa.getLength();
System.out.printf("Cout Dijkstra : %.3f%n", coutDijkstra);
System.out.printf("Cout A* : %.3f%n", coutAstar);
// Verification que Dijkstra et A* donnent le meme cout
double seuil = 1e-2;
System.out.println("Dijkstra == A* : "
+ (Math.abs(coutDijkstra - coutAstar) < seuil));
// Bellman peut etre inutilisable sur les grands graphes ou origine==destination
if (solBellman.getStatus() == Status.OPTIMAL) {
Path pb = solBellman.getPath();
double coutBellman = estTemps
? pb.getMinimumTravelTime()
: pb.getLength();
System.out.printf("Cout Bellman : %.3f%n", coutBellman);
System.out.println("Dijkstra == Bellman : "
+ (Math.abs(coutDijkstra - coutBellman) < seuil));
} else {
System.out.println("Bellman : inutilisable sur ce scenario");
}
// Verification par rapport au fichier .path de reference si disponible
if (sc.cheminRef != null) {
Path cheminAttendu;
try (final PathReader pathReader = new BinaryPathReader(
new DataInputStream(new BufferedInputStream(
new FileInputStream(sc.cheminRef))))) {
cheminAttendu = pathReader.readPath(graph);
}
double coutRef = estTemps
? cheminAttendu.getMinimumTravelTime()
: cheminAttendu.getLength();
System.out.printf("Cout reference : %.3f%n", coutRef);
System.out.println("Dijkstra == reference : "
+ (Math.abs(coutDijkstra - coutRef) < seuil));
}
}
return graph;
}
public static void main(String[] args) throws Exception {
// visit these directory to see the list of available files on commetud.
final String maps = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/";
final String paths = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/";
Scenario[] scenarios = {
// Chemin de longueur nulle : origine == destination
new Scenario(maps + "insa.mapgr", null, 0, 552, 552,
"Longueur nulle - origine == destination"),
// Trajet court en distance sans filtre
new Scenario(maps + "insa.mapgr",
paths + "path_fr31insa_rangueil_r2.path",
0, 552, 526,
"Court trajet en distance - insa"),
// Meme trajet en temps
new Scenario(maps + "insa.mapgr", null, 2, 552, 526,
"Court trajet en temps - insa"),
// Trajet INSA -> Bikini voiture (ne doit pas passer par le canal)
new Scenario(maps + "toulouse.mapgr", null, 1, 14531, 5910,
"INSA -> Bikini en voiture"),
// Trajet INSA -> Bikini pietonne
new Scenario(maps + "toulouse.mapgr", null, 3, 14531, 5910,
"INSA -> Bikini pietonne"),
};
for (Scenario sc : scenarios) {
Graph graph = testerScenario(sc);
ShortestPathData data = new ShortestPathData(
graph,
graph.get(sc.origine),
graph.get(sc.destination),
ArcInspectorFactory.getAllFilters().get(sc.filtre)
);
final Path pd = new DijkstraAlgorithm(data).run().getPath();
final Path pa = new AStarAlgorithm(data).run().getPath();
final Path cheminRef;
if (sc.cheminRef != null) {
try (final PathReader pathReader = new BinaryPathReader(
new DataInputStream(new BufferedInputStream(
new FileInputStream(sc.cheminRef))))) {
cheminRef = pathReader.readPath(graph);
}
} else {
cheminRef = null;
}
// create the drawing
final Drawing drawing = createDrawing();
// draw the graph and paths on the drawing, same mechanism as createDrawing
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
try {
// draw the graph on the drawing
drawing.drawGraph(graph);
// draw the path on the drawing
if (pd != null) drawing.drawPath(pd);
if (pa != null) drawing.drawPath(pa);
if (cheminRef != null) drawing.drawPath(cheminRef);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
}