This commit is contained in:
Gasson-Betuing Danyl 2025-05-20 10:01:59 +02:00
parent 0385a19396
commit 23199c947f
4 changed files with 45 additions and 3 deletions

View file

@ -56,16 +56,21 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
// two variables to update costs // two variables to update costs
double NewDist = 0; double NewDist = 0;
double OldDist = 0;
double EstimatedCost = 0; double EstimatedCost = 0;
// A* // The destination
while (!minHeap.isEmpty()) { LabelStar labelDest = labels[data.getDestination().getId()];
// A* algorithme à compléxité inférieur à dijkstra.
while (!minHeap.isEmpty() && labelDest.computedCost == Double.POSITIVE_INFINITY) { // on modifie la condition d'arrêt pour exploiter la force de A* niveau rapidité de la solution
IdxNewOrigin = minHeap.deleteMin().currentNode.getId(); IdxNewOrigin = minHeap.deleteMin().currentNode.getId();
labels[IdxNewOrigin].mark = true; labels[IdxNewOrigin].mark = true;
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 qui est l'origine o`u la destination
// Small test to check allowed roads... // Small test to check allowed roads...
if (!data.isAllowed(arc)) { if (!data.isAllowed(arc)) {
@ -74,9 +79,17 @@ 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;
EstimatedCost = labels[arc.getDestination().getId()].destCost; EstimatedCost = labels[arc.getDestination().getId()].destCost;
NewDist = labels[IdxNewOrigin].computedCost + arc.getLength(); // calcul de la nouvelle distance NewDist = labels[IdxNewOrigin].computedCost + arc.getLength(); // calcul de la nouvelle distance
// to observe the path search
if (Double.isInfinite(OldDist)
&& Double.isFinite(NewDist)) {
notifyNodeReached(arc.getDestination());
}
if(labels[arc.getDestination().getId()].getTotalCost( ) > NewDist + EstimatedCost){ // si amélioration if(labels[arc.getDestination().getId()].getTotalCost( ) > NewDist + EstimatedCost){ // si amélioration
try try

View file

@ -74,6 +74,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
NewDist = labels[IdxNewOrigin].computedCost + arc.getLength(); // calcul de la nouvelle distance NewDist = labels[IdxNewOrigin].computedCost + arc.getLength(); // calcul de la nouvelle distance
OldDist = labels[arc.getDestination().getId()].computedCost; // lecture de l'ancienne OldDist = labels[arc.getDestination().getId()].computedCost; // lecture de l'ancienne
if (Double.isInfinite(OldDist)
&& Double.isFinite(NewDist)) {
notifyNodeReached(arc.getDestination());
}
if(OldDist > NewDist){ // si amélioration if(OldDist > NewDist){ // si amélioration
try try

View file

@ -0,0 +1,12 @@
package org.insa.graphes.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.BeforeClass;
import org.junit.Test;
public class AStarTest {
}

View file

@ -0,0 +1,12 @@
package org.insa.graphes.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.BeforeClass;
import org.junit.Test;
public class DijkstraTest {
}