From 3eb0b9fdf0dbce962935385d1cad778c903c41ce Mon Sep 17 00:00:00 2001 From: Matteo Date: Tue, 20 May 2025 10:48:03 +0200 Subject: [PATCH 1/2] avancement A* --- .../algorithm/shortestpath/AStarAlgorithm.java | 9 ++++++++- .../shortestpath/DijkstraAlgorithm.java | 18 +++++++++++++----- .../algorithm/shortestpath/LabelStar.java | 12 +++++++++--- 3 files changed, 30 insertions(+), 9 deletions(-) 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 fd172f0..0457c6a 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 @@ -1,9 +1,16 @@ package org.insa.graphs.algorithm.shortestpath; - +import org.insa.graphs.model.Node; public class AStarAlgorithm extends DijkstraAlgorithm { public AStarAlgorithm(ShortestPathData data) { super(data); } + + @Override + protected Label createLabel(Node node) { + return new LabelStar(node, false, Double.POSITIVE_INFINITY, null, getInputData().getDestination()); + } + + } diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java index 28651cf..4229252 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java @@ -17,6 +17,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { super(data); } + protected Label createLabel(Node node) { + return new Label(node, false, Double.POSITIVE_INFINITY, null); + } + + @Override protected ShortestPathSolution doRun() { @@ -33,8 +38,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { // Initialisation des labels for (Node node : graph.getNodes()) { - labels[node.getId()] = - new Label(node, false, Double.POSITIVE_INFINITY, null); + labels[node.getId()] =createLabel(node); } // Origine : coût 0, non marqué, pas de père labels[data.getOrigin().getId()].setCoutRealise(0); @@ -68,13 +72,17 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { if (succLabel.getMarque()) continue; // déjà traité - double newCost = LabelActuel.getCoutRealise() + data.getCost(arc); + double newCost = LabelActuel.getTotalCost() + data.getCost(arc); - if (newCost < succLabel.getCoutRealise()) { + //Label comp = new Label(null, false, newCost, null); //juste pour faire le compareTo entre label et pas entre double + + if (newCost < succLabel.getTotalCost()) { + //int res = succLabel.compareTo(comp); + //if (res!=0){ // Mise à jour du coût et du prédécesseur // Si le sommet a déjà un label dans le tas, on le retire // avant de le mettre à jour - if (succLabel.getCoutRealise() != Double.POSITIVE_INFINITY) { + if (succLabel.getTotalCost() != Double.POSITIVE_INFINITY) { heap.remove(succLabel); } succLabel.setCoutRealise(newCost); 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 f5722ab..d5bec72 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,9 +1,10 @@ -/*package org.insa.graphs.algorithm.shortestpath; +package org.insa.graphs.algorithm.shortestpath; import org.insa.graphs.model.Arc; import org.insa.graphs.model.Node; import static org.insa.graphs.model.Point.distance; + public class LabelStar extends Label { private Node destination; @@ -13,9 +14,14 @@ public class LabelStar extends Label { } @Override - public double getTotalCost() { //pourquoi CoutRealise ? psq il est utilisé dans le compareTo + public double getTotalCost() { //pourquoi getTotalCost ? psq il est utilisé dans le compareTo return (getCoutRealise()+distance(getSommetCourant().getPoint(),this.destination.getPoint())); } + + //pas nécessaire normalement + /*public int compareTo(LabelStar other) { + return Double.compare(this.getTotalCost(), other.getTotalCost()); + }*/ -}*/ +} \ No newline at end of file From e8db0bfb33e10180b6ee7c6a7bdc0ddef12bce91 Mon Sep 17 00:00:00 2001 From: Matteo Date: Tue, 20 May 2025 12:12:25 +0200 Subject: [PATCH 2/2] avancement mineurs --- .../algorithm/shortestpath/AStarAlgorithm.java | 2 +- .../graphs/algorithm/shortestpath/LabelStar.java | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) 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 0457c6a..4db278f 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 @@ -8,7 +8,7 @@ public class AStarAlgorithm extends DijkstraAlgorithm { @Override - protected Label createLabel(Node node) { + protected LabelStar createLabel(Node node) { return new LabelStar(node, false, Double.POSITIVE_INFINITY, null, getInputData().getDestination()); } 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 d5bec72..3ebdc92 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 @@ -2,7 +2,8 @@ package org.insa.graphs.algorithm.shortestpath; import org.insa.graphs.model.Arc; import org.insa.graphs.model.Node; -import static org.insa.graphs.model.Point.distance; +import org.insa.graphs.model.Point; + public class LabelStar extends Label { @@ -10,12 +11,20 @@ public class LabelStar extends Label { private Node destination; public LabelStar(Node sommetCourant, Boolean marque, double coutRealise, Arc pere,Node destination) { super(sommetCourant, marque, coutRealise, pere); - this.destination=destination; //ou alors on le met juste en paramètre + this.destination=destination; } + + //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 public double getTotalCost() { //pourquoi getTotalCost ? psq il est utilisé dans le compareTo - return (getCoutRealise()+distance(getSommetCourant().getPoint(),this.destination.getPoint())); + //System.out.println("cout realise : " + getCoutRealise()+ "\n distance : "distance(getSommetCourant().getPoint(),this.destination.getPoint())); + double cout=getCoutRealise(); + Point calcul = this.destination.getPoint(); + cout+=calcul.distanceTo(this.getSommetCourant().getPoint()); + return (cout); } //pas nécessaire normalement