Dijkstra et A* corrigé2

This commit is contained in:
Gasson-Betuing Danyl 2025-05-20 12:21:30 +02:00
parent c38b7448c7
commit 5d45a5dc02
2 changed files with 63 additions and 21 deletions

View file

@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import javax.print.attribute.standard.Destination; import javax.print.attribute.standard.Destination;
import org.insa.graphs.algorithm.AbstractInputData;
import org.insa.graphs.algorithm.AbstractSolution.Status; import org.insa.graphs.algorithm.AbstractSolution.Status;
import org.insa.graphs.algorithm.utils.BinaryHeap; import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.model.Arc; import org.insa.graphs.model.Arc;
@ -35,11 +35,46 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
final int nbNodes = graph.size(); final int nbNodes = graph.size();
int nodeId = 0; int nodeId = 0;
// Max speed for time mode
double maxSpeed = 150/3.6;
double Heuristicdistance = 0;
// initialize the labelstar list // initialize the labelstar list
LabelStar[] labels = new LabelStar[nbNodes]; LabelStar[] labels = new LabelStar[nbNodes];
for(Node node : graph.getNodes()){
nodeId = node.getId(); if(data.getMode() == AbstractInputData.Mode.LENGTH){
labels[nodeId] = new LabelStar(node, false, Double.POSITIVE_INFINITY, Point.distance(node.getPoint(), data.getDestination().getPoint()));
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; labels[data.getOrigin().getId()].computedCost = 0;
@ -55,8 +90,8 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
// two variables to update costs // two variables to update costs
double NewDist = 0; double NewCost = 0;
double OldDist = 0; double OldCost = 0;
double EstimatedCost = 0; double EstimatedCost = 0;
// The destination // The destination
@ -69,6 +104,7 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
IdxNewOrigin = minHeap.deleteMin().currentNode.getId(); IdxNewOrigin = minHeap.deleteMin().currentNode.getId();
labels[IdxNewOrigin].mark = true; 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 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 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; 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 // to observe the path search
if (Double.isInfinite(OldDist) if (Double.isInfinite(OldCost)
&& Double.isFinite(NewDist)) { && Double.isFinite(NewCost)) {
notifyNodeReached(arc.getDestination()); 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 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 labels[arc.getDestination().getId()].father = arc; // MAJ père

View file

@ -52,15 +52,21 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// two variables to update costs // two variables to update costs
double OldDist = 0; double OldCost = 0;
double NewDist = 0; double NewCost = 0;
// label du noeud destination
Label labelDest = labels[data.getDestination().getId()];
// TODO: implement the Dijkstra algorithm // TODO: implement the Dijkstra algorithm
while (!minHeap.isEmpty()) { while (!minHeap.isEmpty() && labelDest.computedCost == Double.POSITIVE_INFINITY) {
IdxNewOrigin = minHeap.deleteMin().currentNode.getId(); IdxNewOrigin = minHeap.deleteMin().currentNode.getId();
labels[IdxNewOrigin].mark = true; 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 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 if (!labels[arc.getDestination().getId()].mark){ // verif du marquage
NewDist = labels[IdxNewOrigin].computedCost + data.getCost(arc); // calcul de la nouvelle distance NewCost = labels[IdxNewOrigin].computedCost + data.getCost(arc); // calcul de la nouvelle distance
OldDist = labels[arc.getDestination().getId()].computedCost; // lecture de l'ancienne OldCost = labels[arc.getDestination().getId()].computedCost; // lecture de l'ancienne
if (Double.isInfinite(OldDist) if (Double.isInfinite(OldCost)
&& Double.isFinite(NewDist)) { && Double.isFinite(NewCost)) {
notifyNodeReached(arc.getDestination()); notifyNodeReached(arc.getDestination());
} }
if(OldDist > NewDist){ // si amélioration if(OldCost > NewCost){ // si amélioration
try 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 labels[arc.getDestination().getId()].father = arc; // MAJ père