From 1747043e2ae2b1ee066628b00a97a144af8c1af6 Mon Sep 17 00:00:00 2001 From: Tiphaine Pellerin Date: Thu, 4 Jun 2026 23:38:59 +0200 Subject: [PATCH] =?UTF-8?q?r=C3=A9paration=20de=20l'algo=20astar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shortestpath/AStarAlgorithm.java | 12 ++++++++--- .../shortestpath/DijkstraAlgorithm.java | 20 ++++++++++++------ .../graphs/algorithm/shortestpath/Label.java | 4 ++-- .../shortestpath/AStarAlgorithm.class | Bin 1381 -> 1573 bytes .../shortestpath/DijkstraAlgorithm.class | Bin 5379 -> 5573 bytes .../graphs/algorithm/shortestpath/Label.class | Bin 1742 -> 1742 bytes 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java index 050c21f..3233751 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java @@ -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()) ); } } diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java index 6f6a34f..1d83f52 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java @@ -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); diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java index c5c1e9d..fa8b241 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java @@ -46,10 +46,10 @@ public class Label implements Comparable