avancement A*

This commit is contained in:
Matteo Sabben 2025-05-20 10:48:03 +02:00
parent d59974e56c
commit 3eb0b9fdf0
3 changed files with 30 additions and 9 deletions

View file

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

View file

@ -17,6 +17,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
super(data); super(data);
} }
protected Label createLabel(Node node) {
return new Label(node, false, Double.POSITIVE_INFINITY, null);
}
@Override @Override
protected ShortestPathSolution doRun() { protected ShortestPathSolution doRun() {
@ -33,8 +38,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// Initialisation des labels // Initialisation des labels
for (Node node : graph.getNodes()) { for (Node node : graph.getNodes()) {
labels[node.getId()] = labels[node.getId()] =createLabel(node);
new Label(node, false, Double.POSITIVE_INFINITY, null);
} }
// Origine : coût 0, non marqué, pas de père // Origine : coût 0, non marqué, pas de père
labels[data.getOrigin().getId()].setCoutRealise(0); labels[data.getOrigin().getId()].setCoutRealise(0);
@ -68,13 +72,17 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
if (succLabel.getMarque()) if (succLabel.getMarque())
continue; // déjà traité 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 // Mise à jour du coût et du prédécesseur
// Si le sommet a déjà un label dans le tas, on le retire // Si le sommet a déjà un label dans le tas, on le retire
// avant de le mettre à jour // avant de le mettre à jour
if (succLabel.getCoutRealise() != Double.POSITIVE_INFINITY) { if (succLabel.getTotalCost() != Double.POSITIVE_INFINITY) {
heap.remove(succLabel); heap.remove(succLabel);
} }
succLabel.setCoutRealise(newCost); succLabel.setCoutRealise(newCost);

View file

@ -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.Arc;
import org.insa.graphs.model.Node; import org.insa.graphs.model.Node;
import static org.insa.graphs.model.Point.distance; import static org.insa.graphs.model.Point.distance;
public class LabelStar extends Label { public class LabelStar extends Label {
private Node destination; private Node destination;
@ -13,9 +14,14 @@ public class LabelStar extends Label {
} }
@Override @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())); return (getCoutRealise()+distance(getSommetCourant().getPoint(),this.destination.getPoint()));
} }
//pas nécessaire normalement
/*public int compareTo(LabelStar other) {
return Double.compare(this.getTotalCost(), other.getTotalCost());
}*/
}*/ }