From 1aee5a57c3b4966b14ac38a33ed9e52af7a882d0 Mon Sep 17 00:00:00 2001 From: Matteo Date: Mon, 26 May 2025 18:21:48 +0200 Subject: [PATCH 1/3] correction fastest path pour A* et avancement ProblemeOuvert --- .../shortestpath/AStarAlgorithm.java | 3 +- .../shortestpath/LabelProblemeOuvert | 16 -- .../shortestpath/LabelProblemeOuvert.java | 30 ++++ .../algorithm/shortestpath/LabelStar.java | 55 ++++++- .../shortestpath/ProblemeOuvert.java | 150 ++++++++++++++++++ 5 files changed, 230 insertions(+), 24 deletions(-) delete mode 100644 be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelProblemeOuvert create mode 100644 be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelProblemeOuvert.java diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java index 4db278f..29df1a0 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java @@ -9,7 +9,8 @@ public class AStarAlgorithm extends DijkstraAlgorithm { @Override protected LabelStar createLabel(Node node) { - return new LabelStar(node, false, Double.POSITIVE_INFINITY, null, getInputData().getDestination()); + ShortestPathData data = getInputData(); //pour récupérer la vitesse maximale du graphe, la destination et si on est en fastestpath ou shortestpath + return new LabelStar(node, false, Double.POSITIVE_INFINITY,null,data); } diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelProblemeOuvert b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelProblemeOuvert deleted file mode 100644 index 3478429..0000000 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelProblemeOuvert +++ /dev/null @@ -1,16 +0,0 @@ -/*public class LabelProblemeOuvert extends Label { - private double autonomieRestante; - - public LabelProblemeOuvert(Node sommet, boolean marque, double cout, Arc pere, double autonomieRestante) { - super(sommet, marque, cout, pere); - this.autonomieRestante = autonomieRestante; - } - - public double getAutonomieRestante() { - return autonomieRestante; - } - - public void setAutonomieRestante(double autonomieRestante) { - this.autonomieRestante = autonomieRestante; - } -}*/ diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelProblemeOuvert.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelProblemeOuvert.java new file mode 100644 index 0000000..e4f32fd --- /dev/null +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelProblemeOuvert.java @@ -0,0 +1,30 @@ +package org.insa.graphs.algorithm.shortestpath; + +import org.insa.graphs.model.Arc; +import org.insa.graphs.model.Node; +public class LabelProblemeOuvert extends Label { + private double autonomieRestante; //double pour la cohérence avec les autres couts qui sont en double eux aussi + + public LabelProblemeOuvert(Node sommet, boolean marque, double cout, Arc pere, double autonomieRestante) { + super(sommet, marque, cout, pere); + this.autonomieRestante = autonomieRestante; + } + + public double getAutonomieRestante() { + return autonomieRestante; + } + + public void setAutonomieRestante(double autonomieRestante) { + this.autonomieRestante = autonomieRestante; + } + + @Override + public double getTotalCost() { //normalement pas besoin + return this.getCoutRealise(); + } + + /*@Override + public int compareTo(LabelProblemeOuvert other) { + return Double.compare(this.getTotalCost(), other.getTotalCost()); + }*/ +} diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelStar.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelStar.java index 3ebdc92..88a9240 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelStar.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelStar.java @@ -1,5 +1,6 @@ package org.insa.graphs.algorithm.shortestpath; +import org.insa.graphs.algorithm.AbstractInputData; import org.insa.graphs.model.Arc; import org.insa.graphs.model.Node; import org.insa.graphs.model.Point; @@ -7,30 +8,70 @@ import org.insa.graphs.model.Point; public class LabelStar extends Label { - - private Node destination; - public LabelStar(Node sommetCourant, Boolean marque, double coutRealise, Arc pere,Node destination) { +; + private ShortestPathData pathData; + public LabelStar(Node sommetCourant, Boolean marque, double coutRealise, Arc pere, ShortestPathData data) { super(sommetCourant, marque, coutRealise, pere); - this.destination=destination; + this.pathData = data; //pour récupérer la vitesse maximale du graphe, la destination et si on est en fastestpath ou shortestpath } //pour optimiser l'algo : ATTENTION PAS à L'initialisation pas le faire sinon ça va le faire pr tout les pts //on rajoute un paremètre distance ds le label et quand on passe sur le label si c'est linfini on le calcule - @Override + /*@Override public double getTotalCost() { //pourquoi getTotalCost ? psq il est utilisé dans le compareTo //System.out.println("cout realise : " + getCoutRealise()+ "\n distance : "distance(getSommetCourant().getPoint(),this.destination.getPoint())); double cout=getCoutRealise(); - Point calcul = this.destination.getPoint(); + Point calcul = this.pathData.getDestination().getPoint(); cout+=calcul.distanceTo(this.getSommetCourant().getPoint()); return (cout); - } + }*/ //pas nécessaire normalement /*public int compareTo(LabelStar other) { return Double.compare(this.getTotalCost(), other.getTotalCost()); }*/ + /** + * Calcule le coût total estimé (f = g + h) pour l'algorithme A*. + * g (getCoutRealise()) est le coût actuel depuis le départ. + * h est l'heuristique : + * - En mode DISTANCE: distance à vol d'oiseau (via distanceTo) jusqu'à la destination. + * - En mode TEMPS: distance à vol d'oiseau (via distanceTo) / vitesse maximale sur le graphe. + * @return Le coût total estimé. + */ + @Override + public double getTotalCost() { + double gCost = getCoutRealise(); // Coût actuel depuis l'origine (distance ou temps) + double hCost = 0.0; + + Node current = getSommetCourant(); + Node destinationNode = this.pathData.getDestination(); // Obtention de la destination depuis pathData + Point currentPoint = current.getPoint(); + Point destinationPoint = destinationNode.getPoint(); + if (currentPoint == null || destinationPoint == null) { + return gCost; // Heuristique nulle si points non valides + } + + //calcul vol d'oiseau + double distanceToDestinationMeters = currentPoint.distanceTo(destinationPoint); + + //différencier TIME et DISTANCE + if (this.pathData.getMode() == AbstractInputData.Mode.TIME) { + int maxSpeedKmH = this.pathData.getGraph().getGraphInformation().getMaximumSpeed(); + + // Conversion de la vitesse max en mètres par seconde (m/s) + double maxSpeedMpS = maxSpeedKmH / 3.6; + hCost = distanceToDestinationMeters / maxSpeedMpS; // hCost est maintenant en secondes + + } else { + // Heuristique pour le mode DISTANCE (ou par défaut) + hCost = distanceToDestinationMeters; // hCost est en mètres + } + + return gCost + hCost; + } + } \ No newline at end of file diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/ProblemeOuvert.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/ProblemeOuvert.java index 0658cce..d66b785 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/ProblemeOuvert.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/ProblemeOuvert.java @@ -106,3 +106,153 @@ gérer le problème de la recharge, quand la faire ? */ +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; +import org.insa.graphs.model.Graph; +import org.insa.graphs.model.Node; +import org.insa.graphs.model.Path; +import org.insa.graphs.model.RoadInformation.RoadType; +//import org.insa.graphs.algorithm.shortestpath.ProblemeOuvert; + +public class ProblemeOuvert extends DijkstraAlgorithm { + + + private double MAX_BATTERY = 200; + + public ProblemeOuvert(ShortestPathData data) { + super(data); + } + + @Override + protected LabelProblemeOuvert createLabel(Node node) { + return new LabelProblemeOuvert(node, false, Double.POSITIVE_INFINITY, null, MAX_BATTERY); + } + + protected void tryUpdateLabel(LabelProblemeOuvert[] labels, BinaryHeap