AStar done :) :hapy:

This commit is contained in:
Yanis Mahé 2026-05-12 17:02:21 +02:00
parent d203e56988
commit bf2c35eadd
4 changed files with 39 additions and 32 deletions

View file

@ -1,9 +1,17 @@
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, getInputData().getDestination());
}
}

View file

@ -7,6 +7,7 @@ import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Path;
import org.insa.graphs.model.Node;
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@ -14,6 +15,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
super(data);
}
protected Label createLabel(Node node) {
return new Label(node);
}
@Override
protected ShortestPathSolution doRun() {
@ -30,7 +35,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// Initialize array of labels.
ArrayList<Label> labelsList = new ArrayList<Label>();
for (int i=0; i<nbNodes; i++) {
labelsList.add(new Label(graph.get(i)));
labelsList.add(createLabel(graph.get(i)));
}
@ -58,11 +63,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
}
Label successorLabel = labelsList.get(arc.getDestination().getId());
Double newCost = data.getCost(arc) + concurentNodeLabel.getCost();
if (successorLabel.getCost() == Double.POSITIVE_INFINITY) {
Double newCost = concurentNodeLabel.getCoutRealise() + data.getCost(arc);
if (successorLabel.getCoutRealise() == Double.POSITIVE_INFINITY) {
notifyNodeReached(arc.getDestination());
}
if (newCost < successorLabel.getCost()) {
if (newCost < successorLabel.getCoutRealise()) {
successorLabel.setPere(arc);
successorLabel.setCoutRealise(newCost);
labelsHeap.insert(successorLabel);

View file

@ -10,6 +10,8 @@ public class Label implements Comparable<Label> {
private Boolean marque;
private double coutRealise;
private double coutEstime;
private Arc pere;
@ -17,6 +19,7 @@ public class Label implements Comparable<Label> {
this.sommetCourant = sommetCourant;
this.marque = false;
this.coutRealise = Double.POSITIVE_INFINITY;
this.coutEstime = 0;
}
public Node getSommetCourant() {
@ -39,21 +42,34 @@ public class Label implements Comparable<Label> {
this.pere = pere;
}
// public double getCoutRealise() {
// return coutRealise;
// }
public double getCoutRealise() {
return coutRealise;
}
public double getCoutEstime() {
return coutEstime;
}
public void setCoutRealise(double nouveauCout) {
this.coutRealise = nouveauCout;
}
public void setCoutEstime(double nouveauCout) {
this.coutEstime = nouveauCout;
}
public double getCost() {
return coutRealise;
return coutRealise + coutEstime;
}
@Override
public int compareTo(Label o) {
return Double.compare(this.coutRealise, o.getCost()) ;
if (this.getCost() == o.getCost()) {
return Double.compare(this.getCoutEstime(), o.getCoutEstime());
}
else {
return Double.compare(this.getCost(), o.getCost());
}
}

View file

@ -4,32 +4,10 @@ import org.insa.graphs.model.Node;
import org.insa.graphs.model.Point;
public class LabelStar extends Label {
private double volOiseau;
public LabelStar(Node sommetCourant, Node sommetDestination) {
super(sommetCourant);
volOiseau = Point.distance(this.getSommetCourant().getPoint(), sommetDestination.getPoint());
}
public double getVolOiseau() {
return volOiseau;
}
@Override
public double getCost() {
return super.getCost() + this.volOiseau;
}
public int compareTo(LabelStar o) {
double coutthis = this.getCost();
double coutautre = o.getCost();
if (coutthis == coutautre) {
return Double.compare(this.volOiseau, o.getVolOiseau());
}
else {
return Double.compare(coutthis, coutautre);
}
this.setCoutEstime(Point.distance(this.getSommetCourant().getPoint(), sommetDestination.getPoint()));
}
}