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;
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());
}
}

View file

@ -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);

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.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());
}*/
}