commentaires
This commit is contained in:
parent
728f069973
commit
9b1b344d29
4 changed files with 17 additions and 3 deletions
|
@ -13,6 +13,7 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
|
||||||
@Override
|
@Override
|
||||||
public Label CreationLabel(Node x, Double cout, Arc parent){
|
public Label CreationLabel(Node x, Double cout, Arc parent){
|
||||||
ShortestPathData data = getInputData();
|
ShortestPathData data = getInputData();
|
||||||
|
//On utilise Point.distance qui permet d'obtenir la distance à vol d'oiseau d'un point x vers un point destination
|
||||||
return new LabelStar(x,cout,parent,Point.distance(data.getGraph().get(x.getId()).getPoint(), data.getDestination().getPoint()));
|
return new LabelStar(x,cout,parent,Point.distance(data.getGraph().get(x.getId()).getPoint(), data.getDestination().getPoint()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,6 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
ArrayList<Arc> Arcs = new ArrayList<Arc>();
|
ArrayList<Arc> Arcs = new ArrayList<Arc>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Initialise nos label */
|
/* Initialise nos label */
|
||||||
for (Node x: data.getGraph().getNodes())
|
for (Node x: data.getGraph().getNodes())
|
||||||
{
|
{
|
||||||
|
@ -47,6 +46,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
}
|
}
|
||||||
Tas.insert(List_Label.get(data.getOrigin().getId()));
|
Tas.insert(List_Label.get(data.getOrigin().getId()));
|
||||||
Label x;
|
Label x;
|
||||||
|
//Tant que le tas n'est pas vide et que notre destination n'est pas marquée (noir)
|
||||||
while (!List_Label.get(data.getDestination().getId()).isMarque() && !Tas.isEmpty()){
|
while (!List_Label.get(data.getDestination().getId()).isMarque() && !Tas.isEmpty()){
|
||||||
x = Tas.findMin();
|
x = Tas.findMin();
|
||||||
x.setMarque(true);
|
x.setMarque(true);
|
||||||
|
@ -57,13 +57,16 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
if (!data.isAllowed(suivant)) {
|
if (!data.isAllowed(suivant)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
// On récupère le label correspondant au sommet destination de notre arc suivant
|
||||||
Label l=List_Label.get(suivant.getDestination().getId());
|
Label l=List_Label.get(suivant.getDestination().getId());
|
||||||
|
|
||||||
if(!l.isMarque()){
|
if(!l.isMarque()){
|
||||||
Boolean changé = false;
|
Boolean changé = false;
|
||||||
|
//On vérifie si le coût sera amélioré avec notre label destination
|
||||||
if (x.getRealCost()+data.getCost(suivant) < l.getRealCost()){
|
if (x.getRealCost()+data.getCost(suivant) < l.getRealCost()){
|
||||||
changé = true;
|
changé = true;
|
||||||
}
|
}
|
||||||
|
//Si oui, si le label à déjà été atteint, on le sort du tas, met à jour ses attributs et le réinsère
|
||||||
if(changé){
|
if(changé){
|
||||||
if (l.getRealCost() != Double.MAX_VALUE){
|
if (l.getRealCost() != Double.MAX_VALUE){
|
||||||
|
|
||||||
|
@ -72,6 +75,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
Tas.insert(l);
|
Tas.insert(l);
|
||||||
l.setParent(suivant);
|
l.setParent(suivant);
|
||||||
}
|
}
|
||||||
|
//Sinon, on met juste à jour ses attributs et l'insère dans le tas
|
||||||
else{
|
else{
|
||||||
l.setCost(x.getRealCost()+data.getCost(suivant));
|
l.setCost(x.getRealCost()+data.getCost(suivant));
|
||||||
Tas.insert(l);
|
Tas.insert(l);
|
||||||
|
@ -84,18 +88,27 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//On récupère le chemin (liste d'arcs) de la destination vers la source
|
||||||
Label dest =List_Label.get(data.getDestination().getId());
|
Label dest =List_Label.get(data.getDestination().getId());
|
||||||
while (dest.getParent() != null){
|
while (dest.getParent() != null){
|
||||||
Arcs.add(dest.getParent());
|
Arcs.add(dest.getParent());
|
||||||
dest = List_Label.get(dest.getParent().getOrigin().getId());
|
dest = List_Label.get(dest.getParent().getOrigin().getId());
|
||||||
}
|
}
|
||||||
|
//On inverse la liste d'arcs
|
||||||
Collections.reverse(Arcs);
|
Collections.reverse(Arcs);
|
||||||
|
//On construit la solution
|
||||||
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), Arcs));
|
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), Arcs));
|
||||||
|
|
||||||
|
|
||||||
ArrayList<Node> solutionNodes = new ArrayList<Node>();
|
ArrayList<Node> solutionNodes = new ArrayList<Node>();
|
||||||
|
//On récupère la liste de nodes du Path solution
|
||||||
for (Arc a : Arcs){
|
for (Arc a : Arcs){
|
||||||
solutionNodes.add(a.getOrigin());
|
solutionNodes.add(a.getOrigin());
|
||||||
}
|
}
|
||||||
solutionNodes.add(data.getDestination());
|
solutionNodes.add(data.getDestination());
|
||||||
|
|
||||||
|
|
||||||
|
//Vérifications de la validité de la solution
|
||||||
ShortestVerif(solution, solutionNodes, data);
|
ShortestVerif(solution, solutionNodes, data);
|
||||||
fastestVerif(solution, solutionNodes, data);
|
fastestVerif(solution, solutionNodes, data);
|
||||||
volDoiseauVerif(data, solution);
|
volDoiseauVerif(data, solution);
|
||||||
|
@ -113,7 +126,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Permet de vérifier si le temps de parcours de la solution est bien le plus rapide
|
||||||
public void fastestVerif(ShortestPathSolution solution,ArrayList<Node> solutionNodes,ShortestPathData data){
|
public void fastestVerif(ShortestPathSolution solution,ArrayList<Node> solutionNodes,ShortestPathData data){
|
||||||
Path p= Path.createFastestPathFromNodes(data.getGraph(), solutionNodes);
|
Path p= Path.createFastestPathFromNodes(data.getGraph(), solutionNodes);
|
||||||
System.out.println("fastest path : " + p.getMinimumTravelTime());
|
System.out.println("fastest path : " + p.getMinimumTravelTime());
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in a new issue