Dijkstra et A* corrigé2
This commit is contained in:
parent
c38b7448c7
commit
5d45a5dc02
2 changed files with 63 additions and 21 deletions
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -52,16 +52,22 @@ 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
|
||||
|
||||
// Small test to check allowed roads...
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue