avancement Dijkstra
This commit is contained in:
parent
21301949a8
commit
2d42cfe956
1 changed files with 70 additions and 8 deletions
|
|
@ -1,5 +1,8 @@
|
|||
package org.insa.graphs.algorithm.shortestpath;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.org.insa.graphs.algorithm.shortestpath.Label;
|
||||
|
||||
import org.insa.graphs.algorithm.AbstractSolution.Status;
|
||||
|
|
@ -45,7 +48,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
|
||||
BinaryHeap<Label> leTas=new BinaryHeap<Label>;
|
||||
|
||||
List<Arc> listeSuccesseurs;
|
||||
List<Arc> listeSuccesseurs = new List<Arc>;
|
||||
|
||||
ArrayList<Label> listeFinal=new List<Label>;
|
||||
|
||||
|
||||
|
||||
//chercher les successeurs de l'origine
|
||||
|
|
@ -57,24 +63,80 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
|
||||
|
||||
for(Arc arcActuel : listeSuccesseurs){
|
||||
Label labelAInserer ; //On set le label à insérer
|
||||
labelAInserer.setSommetCourant(arcActuel.getDestination());
|
||||
labelAInserer.setMarque(false);
|
||||
Label labelAInserer = new Label(arcActuel.getDestination(),true,arcActuel.getMinimumTravelTime(),arcActuel) ; //On set le label à insérer
|
||||
/*labelAInserer.setSommetCourant(arcActuel.getDestination());
|
||||
labelAInserer.setMarque(true);
|
||||
labelAInserer.setCoutRealise(arcActuel.getMinimumTravelTime());
|
||||
labelAInserer.setPere(arcActuel);
|
||||
labelAInserer.setPere(arcActuel);*/
|
||||
|
||||
leTas.insert(labelAInserer); //pour la comparaison la class BinaryHeap utilise la fct compareTo qu'on a redéfinit dans Label
|
||||
}
|
||||
int indexLabel=0;
|
||||
int indexSolution=-1;
|
||||
|
||||
ArrayList<Node> listeSommetsTraites=new List<Node>;
|
||||
|
||||
while (!leTas.isEmpty()){
|
||||
Label labelMin = leTas.findMin() ;
|
||||
leTas.deleteMin();
|
||||
Label labelMin = leTas.findMin();
|
||||
listeFinal.add(indexLabel,labelMin);
|
||||
listeSommetsTraites.add(indexLabel,labelMin.getSommetCourant());
|
||||
|
||||
double coutActuel = labelMin.getCoutRealise();
|
||||
|
||||
listeSuccesseurs=labelMin.getSommetCourant().getSuccessors();
|
||||
for(Arc arcActuel : listeSuccesseurs){
|
||||
|
||||
//problème faudrait un if (le sommet a déjà un label dans le tas (vérifier qu'il est dans listeFinal) && le successeur a un cout > coutActuel+arcActuel.getMinimumTravelTime())
|
||||
//alors on remplace ce label par le nouveau avec un nouveau père, et un nouveau coût minimum
|
||||
//sinon si (le sommet a déjà un label dans le tas (vérifier qu'il est dans listeFinal))
|
||||
//ne rien faire
|
||||
//sinon //alors on a nouveau label et on l'instancie
|
||||
|
||||
if (listeSommetsTraites.contains(arcActuel.getOrigin()) && ) //problème => comment identifier ce label ? j'ai juste l'arcActuel
|
||||
|
||||
|
||||
Label labelAInserer = new Label(arcActuel.getDestination(),true,coutActuel+arcActuel.getMinimumTravelTime(),arcActuel) ; //On set le label à insérer
|
||||
|
||||
//puis ds tous les cas on insert
|
||||
leTas.insert(labelAInserer); //pour la comparaison la class BinaryHeap utilise la fct compareTo qu'on a redéfinit dans Label
|
||||
}
|
||||
if(labelMin.getSommetCourant().equals(nodeDestination)){
|
||||
indexSolution=indexLabel;
|
||||
}
|
||||
indexLabel++;
|
||||
leTas.deleteMin(); //on enlève l'élément traité
|
||||
}
|
||||
|
||||
|
||||
//remplir ShortestPathSolution
|
||||
//d'abord remplir Path puis ShortestPathData puis status
|
||||
|
||||
//////////////////// ATTENTION predecessorArcs à bien faire (vient de l'algo bellmanFord)
|
||||
|
||||
// Destination has no predecessor, the solution is infeasible...
|
||||
if (predecessorArcs[data.getDestination().getId()] == null) {
|
||||
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
|
||||
}
|
||||
else {
|
||||
|
||||
// The destination has been found, notify the observers.
|
||||
notifyDestinationReached(data.getDestination());
|
||||
|
||||
// Create the path from the array of predecessors...
|
||||
ArrayList<Arc> arcs = new ArrayList<>();
|
||||
Arc arc = predecessorArcs[data.getDestination().getId()];
|
||||
while (arc != null) {
|
||||
arcs.add(arc);
|
||||
arc = predecessorArcs[arc.getOrigin().getId()];
|
||||
}
|
||||
|
||||
// Reverse the path...
|
||||
Collections.reverse(arcs);
|
||||
|
||||
// Create the final solution.
|
||||
solution = new ShortestPathSolution(data, Status.OPTIMAL,
|
||||
new Path(graphe, arcs));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue