réparation de l'algo astar

This commit is contained in:
Tiphaine Pellerin 2026-06-04 23:38:59 +02:00
parent 2e9b5f4b26
commit 1747043e2a
6 changed files with 25 additions and 11 deletions

View file

@ -8,9 +8,15 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
super(data);
}
@Override
// creates an 'empty' labelstar for the node given in argument
public LabelStar newLabel(Node s, ShortestPathData dataPath) {
return new LabelStar(s, false, Integer.MAX_VALUE, null,
s.getPoint().distanceTo(dataPath.getDestination().getPoint()));
public LabelStar newLabel(Node s, Node d) {
return new LabelStar(s, false, Double.MAX_VALUE, null,
s.getPoint().distanceTo(d.getPoint()));
}
@Override
public LabelStar newLabelOrigine(Node s, Node d){
return new LabelStar(s, false, 0, null, s.getPoint().distanceTo(d.getPoint()) );
}
}

View file

@ -10,6 +10,8 @@ import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
import java.sql.Date;
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
public DijkstraAlgorithm(ShortestPathData data) {
@ -17,10 +19,13 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
}
// creates an 'empty' label for the node given in argument
public Label newLabel(Node s) {
public Label newLabel(Node s, Node d) {
return new Label(s, false, Double.MAX_VALUE, null);
}
public Label newLabelOrigine(Node s, Node d){
return new Label(s, false, 0, null);
}
@Override
public ShortestPathSolution doRun() {
@ -35,16 +40,18 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// variable that will contain the solution of the shortest path problem
ShortestPathSolution solution;
Node destination = dataInput.getDestination();
// initialisation
for (Node nod : graph.getNodes()) {
listLabel[nod.getId()] = null;
if (nod.equals(dataInput.getOrigin())) {
listLabel[nod.getId()] = new Label(nod, false, 0, null);
listLabel[nod.getId()] = newLabelOrigine(nod, destination);
tas.insert(listLabel[nod.getId()]);
notifyOriginProcessed(nod);
}
else if (nod.equals(dataInput.getDestination())){
listLabel[nod.getId()] = newLabel(nod);
listLabel[nod.getId()] = newLabel(nod, destination);
}
}
Label xl = tas.findMin();
@ -59,11 +66,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
if (dataInput.isAllowed(a)) {
Node n = a.getDestination();
if (listLabel[n.getId()] == null) {
listLabel[n.getId()] = newLabel(n);
listLabel[n.getId()] = newLabel(n, destination);
}
if (!listLabel[n.getId()].getMarque()) {
final var c = listLabel[n.getId()].getTotalCost();
final var w = listLabel[x.getId()].getTotalCost() + dataInput.getCost(a);
final var c = listLabel[n.getId()].getCost();
final var w = listLabel[x.getId()].getCost() + dataInput.getCost(a);
if (c > w) {
if (listLabel[n.getId()].getCost() != Double.MAX_VALUE) {
tas.remove(listLabel[n.getId()]);
@ -80,6 +87,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
tas.remove(xl);
}
// when the algorithm terminates, return the solution that has been found
if (listLabel[dataInput.getDestination().getId()].getCost() == Double.MAX_VALUE) {
solution = new ShortestPathSolution(dataInput, Status.INFEASIBLE);

View file

@ -46,10 +46,10 @@ public class Label implements Comparable<Label> {
// compares the labels in term of cost
@Override
public int compareTo(Label l) {
if (this.getCost() < l.getCost()) {
if (this.getTotalCost() < l.getTotalCost()) {
return -1;
}
else if (this.getCost() > l.getCost()) {
else if (this.getTotalCost() > l.getTotalCost()) {
return 1;
}
else {