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 b0a4837..6e6083c 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 @@ -56,16 +56,21 @@ public class AStarAlgorithm extends DijkstraAlgorithm { // two variables to update costs double NewDist = 0; + double OldDist = 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(); 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... if (!data.isAllowed(arc)) { @@ -74,9 +79,17 @@ public class AStarAlgorithm extends DijkstraAlgorithm { if (!labels[arc.getDestination().getId()].mark){ // verif du marquage + OldDist = labels[arc.getDestination().getId()].computedCost; EstimatedCost = labels[arc.getDestination().getId()].destCost; 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 try 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 f57397a..1073543 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 @@ -74,6 +74,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { NewDist = labels[IdxNewOrigin].computedCost + arc.getLength(); // calcul de la nouvelle distance 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 try diff --git a/be-graphes-algos/src/test/AStarTest.java b/be-graphes-algos/src/test/AStarTest.java new file mode 100644 index 0000000..6b38bb0 --- /dev/null +++ b/be-graphes-algos/src/test/AStarTest.java @@ -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 { + +} diff --git a/be-graphes-algos/src/test/DijkstraTest.java b/be-graphes-algos/src/test/DijkstraTest.java new file mode 100644 index 0000000..b8bcc00 --- /dev/null +++ b/be-graphes-algos/src/test/DijkstraTest.java @@ -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 { + +}