This commit is contained in:
bezza 2025-05-20 13:34:18 +02:00
commit 367bc7e877
3 changed files with 42 additions and 12 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 LabelStar 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);
System.out.println(succLabel.getCoutRealise());// print de confirmation , pour verif si tous les couts qui sortent du tas sont croissant. getTotalcost pas croissant!!
}

View file

@ -1,21 +1,36 @@
/*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;
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) {
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 CoutRealise ? psq il est utilisé dans le compareTo
return (getCoutRealise()+distance(getSommetCourant().getPoint(),this.destination.getPoint()));
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();
cout+=calcul.distanceTo(this.getSommetCourant().getPoint());
return (cout);
}
//pas nécessaire normalement
/*public int compareTo(LabelStar other) {
return Double.compare(this.getTotalCost(), other.getTotalCost());
}*/
}*/
}