a star works perfectly
This commit is contained in:
parent
4e9989d7ae
commit
a73f89d6db
2 changed files with 72 additions and 9 deletions
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
|
|
||||||
package org.insa.graphs.algorithm.shortestpath;
|
package org.insa.graphs.algorithm.shortestpath;
|
||||||
/*
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -16,14 +16,14 @@ import org.insa.graphs.model.Graph;
|
||||||
import org.insa.graphs.model.Node;
|
import org.insa.graphs.model.Node;
|
||||||
import org.insa.graphs.model.Path;
|
import org.insa.graphs.model.Path;
|
||||||
|
|
||||||
*/
|
|
||||||
public class AStarAlgorithm extends DijkstraAlgorithm {
|
public class AStarAlgorithm extends DijkstraAlgorithm {
|
||||||
|
|
||||||
public AStarAlgorithm(ShortestPathData data) {
|
public AStarAlgorithm(ShortestPathData data) {
|
||||||
super(data);
|
super(data);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
/*
|
|
||||||
@Override
|
@Override
|
||||||
protected ShortestPathSolution doRun() {
|
protected ShortestPathSolution doRun() {
|
||||||
|
|
||||||
|
@ -47,15 +47,78 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
|
||||||
// Tas binaire pour l'extraction du label de coût f minimal
|
// Tas binaire pour l'extraction du label de coût f minimal
|
||||||
BinaryHeap<Label> heap = new BinaryHeap<>();
|
BinaryHeap<Label> heap = new BinaryHeap<>();
|
||||||
|
|
||||||
|
// Initialisation des labels : cout_estimee = distance à vol d'oiseau
|
||||||
|
for (Node node : graph.getNodes()) {
|
||||||
|
double cout_estimee = node.getPoint().distanceTo(destination.getPoint());
|
||||||
|
labels[node.getId()] = new LabelStar(node, cout_estimee);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Origine : coût réel 0 on met un coût nul pour le sommet origine
|
||||||
|
labels[origin.getId()].setcoutrealise(0);
|
||||||
|
heap.insert(labels[origin.getId()]);
|
||||||
|
notifyOriginProcessed(origin);
|
||||||
|
|
||||||
|
|
||||||
|
Label current;
|
||||||
|
// Liste des arcs du chemin
|
||||||
|
List<Arc> cheminArcs = new ArrayList<>();
|
||||||
|
|
||||||
|
// Boucle principale : tant que tas non vide et destination non marquée
|
||||||
|
while (!heap.isEmpty() && !labels[destination.getId()].getmarque()) {
|
||||||
|
current = heap.deleteMin(); // on extrait le label avec le coût le plus faible f minimal = cout_realise + cout_estimee et on le supprime du tas
|
||||||
|
current.setmarque(true);
|
||||||
|
notifyNodeMarked(current.getsommetcourant());
|
||||||
|
|
||||||
|
for (Arc arc : current.getsommetcourant().getSuccessors()) {
|
||||||
|
if (!inspector.isAllowed(arc)) {//pour verifier si c pieton ou bien voiture
|
||||||
|
continue; //si on rentre pour faire continue,on passe a larc suivant dans la liste
|
||||||
|
}
|
||||||
|
Node succ= arc.getDestination();
|
||||||
|
LabelStar succLabel = labels[succ.getId()];
|
||||||
|
if (succLabel.getmarque()) {
|
||||||
|
continue; //si on rentre pour faire continue,on passe a larc suivant dans la liste
|
||||||
|
}
|
||||||
|
|
||||||
|
double cout_realise = current.getCost();
|
||||||
|
double costArc = data.getCost(arc);
|
||||||
|
|
||||||
|
|
||||||
|
double gNew = cout_realise + costArc;
|
||||||
|
|
||||||
|
|
||||||
|
if (gNew < succLabel.getCost()) {
|
||||||
|
succLabel.setcoutrealise(gNew);
|
||||||
|
succLabel.setarcperefils(arc);
|
||||||
|
notifyNodeReached(succ);
|
||||||
|
try {
|
||||||
|
heap.remove(succLabel);
|
||||||
|
heap.insert(succLabel);
|
||||||
|
} catch (ElementNotFoundException e) {
|
||||||
|
heap.insert(succLabel);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
ShortestPathSolution solution;
|
||||||
|
if (!labels[destination.getId()].getmarque()) {
|
||||||
|
solution = new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
|
||||||
|
} else {
|
||||||
|
notifyDestinationReached(destination);
|
||||||
|
// Reconstruction du chemin
|
||||||
|
Arc arc = labels[destination.getId()].getarcpere_fils();
|
||||||
|
while (arc != null) {
|
||||||
|
cheminArcs.add(0, arc);
|
||||||
|
arc = labels[arc.getOrigin().getId()].getarcpere_fils();
|
||||||
|
}
|
||||||
|
Path path = new Path(graph, cheminArcs);
|
||||||
|
solution = new ShortestPathSolution(data, AbstractSolution.Status.OPTIMAL, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return solution;
|
||||||
|
|
||||||
//return solution;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*/
|
|
|
@ -1,6 +1,6 @@
|
||||||
package org.insa.graphs.algorithm.shortestpath;
|
package org.insa.graphs.algorithm.shortestpath;
|
||||||
|
|
||||||
import org.insa.graphs.model.Arc;
|
|
||||||
import org.insa.graphs.model.Node;
|
import org.insa.graphs.model.Node;
|
||||||
|
|
||||||
public class LabelStar extends Label{
|
public class LabelStar extends Label{
|
||||||
|
@ -11,7 +11,7 @@ public class LabelStar extends Label{
|
||||||
this.cout_estimee = cout_estimee;
|
this.cout_estimee = cout_estimee;
|
||||||
}
|
}
|
||||||
//Constructeur de copie pour cloner un LabelStar existant.
|
//Constructeur de copie pour cloner un LabelStar existant.
|
||||||
/*
|
|
||||||
public LabelStar(LabelStar other) {
|
public LabelStar(LabelStar other) {
|
||||||
super(other.getsommetcourant ());
|
super(other.getsommetcourant ());
|
||||||
// Recopie de tous les champs de Label
|
// Recopie de tous les champs de Label
|
||||||
|
@ -20,7 +20,7 @@ public class LabelStar extends Label{
|
||||||
this.setarcperefils(other.getarcpere_fils());
|
this.setarcperefils(other.getarcpere_fils());
|
||||||
// Recopie de l'heuristique
|
// Recopie de l'heuristique
|
||||||
this.cout_estimee = other.cout_estimee;
|
this.cout_estimee = other.cout_estimee;
|
||||||
}*/
|
}
|
||||||
|
|
||||||
//Setter
|
//Setter
|
||||||
public void setCoutEstimee(double cout_estimee) {
|
public void setCoutEstimee(double cout_estimee) {
|
||||||
|
|
Loading…
Reference in a new issue