From 5d45a5dc0236981867bcb0eb6d4ade8f635bd747 Mon Sep 17 00:00:00 2001 From: Gasson-Betuing Danyl Date: Tue, 20 May 2025 12:21:30 +0200 Subject: [PATCH] =?UTF-8?q?Dijkstra=20et=20A*=20corrig=C3=A92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shortestpath/AStarAlgorithm.java | 60 +++++++++++++++---- .../shortestpath/DijkstraAlgorithm.java | 24 +++++--- 2 files changed, 63 insertions(+), 21 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 06d49bb..79b320e 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 @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.Collections; import javax.print.attribute.standard.Destination; - +import org.insa.graphs.algorithm.AbstractInputData; import org.insa.graphs.algorithm.AbstractSolution.Status; import org.insa.graphs.algorithm.utils.BinaryHeap; import org.insa.graphs.model.Arc; @@ -35,11 +35,46 @@ public class AStarAlgorithm extends DijkstraAlgorithm { final int nbNodes = graph.size(); int nodeId = 0; + // Max speed for time mode + double maxSpeed = 150/3.6; + double Heuristicdistance = 0; + + // initialize the labelstar list LabelStar[] labels = new LabelStar[nbNodes]; - for(Node node : graph.getNodes()){ - nodeId = node.getId(); - labels[nodeId] = new LabelStar(node, false, Double.POSITIVE_INFINITY, Point.distance(node.getPoint(), data.getDestination().getPoint())); + + if(data.getMode() == AbstractInputData.Mode.LENGTH){ + + for(Node node : graph.getNodes()){ // Mode DISTANCE + nodeId = node.getId(); + Heuristicdistance = Point.distance(node.getPoint(), data.getDestination().getPoint()); + labels[nodeId] = new LabelStar(node, false, Double.POSITIVE_INFINITY, Heuristicdistance); + } + } + + else if(graph.getGraphInformation().hasMaximumSpeed()){ // Mode TEMPS + + maxSpeed = graph.getGraphInformation().getMaximumSpeed()/3.6; + + System.out.println(maxSpeed); + + for(Node node : graph.getNodes()){ + nodeId = node.getId(); + Heuristicdistance = Point.distance(node.getPoint(), data.getDestination().getPoint()); + labels[nodeId] = new LabelStar(node, false, Double.POSITIVE_INFINITY, Heuristicdistance/maxSpeed); + } + } + + else{ // Mode TEMPS sans vitesse max + + System.err.println("uh"); + + for(Node node : graph.getNodes()) + + { + nodeId = node.getId(); + labels[nodeId] = new LabelStar(node, false, Double.POSITIVE_INFINITY,0); + } } labels[data.getOrigin().getId()].computedCost = 0; @@ -55,8 +90,8 @@ public class AStarAlgorithm extends DijkstraAlgorithm { // two variables to update costs - double NewDist = 0; - double OldDist = 0; + double NewCost = 0; + double OldCost = 0; double EstimatedCost = 0; // The destination @@ -69,6 +104,7 @@ public class AStarAlgorithm extends DijkstraAlgorithm { IdxNewOrigin = minHeap.deleteMin().currentNode.getId(); labels[IdxNewOrigin].mark = true; + notifyNodeMarked(labels[IdxNewOrigin].currentNode); for (Arc arc : labels[IdxNewOrigin].currentNode.getSuccessors()) { // le arrayList de getSucessors, ça ne retourne que des arc forward. donc pas besoin de vérifier qui est l'origine o`u la destination @@ -79,18 +115,18 @@ public class AStarAlgorithm extends DijkstraAlgorithm { if (!labels[arc.getDestination().getId()].mark){ // verif du marquage - OldDist = labels[arc.getDestination().getId()].computedCost; + OldCost = labels[arc.getDestination().getId()].computedCost; EstimatedCost = labels[arc.getDestination().getId()].destCost; - NewDist = labels[IdxNewOrigin].computedCost + data.getCost(arc); // calcul de la nouvelle distance/temps + NewCost = labels[IdxNewOrigin].computedCost + data.getCost(arc); // calcul de la nouvelle distance/temps // to observe the path search - if (Double.isInfinite(OldDist) - && Double.isFinite(NewDist)) { + if (Double.isInfinite(OldCost) + && Double.isFinite(NewCost)) { notifyNodeReached(arc.getDestination()); } - if(labels[arc.getDestination().getId()].getTotalCost( ) > NewDist + EstimatedCost){ // si amélioration + if(labels[arc.getDestination().getId()].getTotalCost( ) > NewCost + EstimatedCost){ // si amélioration try { @@ -101,7 +137,7 @@ public class AStarAlgorithm extends DijkstraAlgorithm { } - labels[arc.getDestination().getId()].computedCost = NewDist; // MAJ coût + labels[arc.getDestination().getId()].computedCost = NewCost; // MAJ coût labels[arc.getDestination().getId()].father = arc; // MAJ père 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 71fa59b..793f658 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 @@ -52,15 +52,21 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { // two variables to update costs - double OldDist = 0; - double NewDist = 0; + double OldCost = 0; + double NewCost = 0; + + // label du noeud destination + + Label labelDest = labels[data.getDestination().getId()]; // TODO: implement the Dijkstra algorithm - while (!minHeap.isEmpty()) { + while (!minHeap.isEmpty() && labelDest.computedCost == Double.POSITIVE_INFINITY) { IdxNewOrigin = minHeap.deleteMin().currentNode.getId(); labels[IdxNewOrigin].mark = true; + + notifyNodeMarked(labels[IdxNewOrigin].currentNode); for (Arc arc : labels[IdxNewOrigin].currentNode.getSuccessors()) { // le arrayList de getSucessors, ça ne retourne que des arc forward. donc pas besoin de vérifier si le chemin est empruntable @@ -71,15 +77,15 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { if (!labels[arc.getDestination().getId()].mark){ // verif du marquage - NewDist = labels[IdxNewOrigin].computedCost + data.getCost(arc); // calcul de la nouvelle distance - OldDist = labels[arc.getDestination().getId()].computedCost; // lecture de l'ancienne + NewCost = labels[IdxNewOrigin].computedCost + data.getCost(arc); // calcul de la nouvelle distance + OldCost = labels[arc.getDestination().getId()].computedCost; // lecture de l'ancienne - if (Double.isInfinite(OldDist) - && Double.isFinite(NewDist)) { + if (Double.isInfinite(OldCost) + && Double.isFinite(NewCost)) { notifyNodeReached(arc.getDestination()); } - if(OldDist > NewDist){ // si amélioration + if(OldCost > NewCost){ // si amélioration try { @@ -90,7 +96,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { } - labels[arc.getDestination().getId()].computedCost = NewDist; // MAJ coût + labels[arc.getDestination().getId()].computedCost = NewCost; // MAJ coût labels[arc.getDestination().getId()].father = arc; // MAJ père