A* OK
This commit is contained in:
parent
0385a19396
commit
23199c947f
4 changed files with 45 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
12
be-graphes-algos/src/test/AStarTest.java
Normal file
12
be-graphes-algos/src/test/AStarTest.java
Normal 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 {
|
||||
|
||||
}
|
12
be-graphes-algos/src/test/DijkstraTest.java
Normal file
12
be-graphes-algos/src/test/DijkstraTest.java
Normal 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 {
|
||||
|
||||
}
|
Loading…
Reference in a new issue