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;
|
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 java.util.org.insa.graphs.algorithm.shortestpath.Label;
|
||||||
|
|
||||||
import org.insa.graphs.algorithm.AbstractSolution.Status;
|
import org.insa.graphs.algorithm.AbstractSolution.Status;
|
||||||
|
|
@ -45,7 +48,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
BinaryHeap<Label> leTas=new BinaryHeap<Label>;
|
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
|
//chercher les successeurs de l'origine
|
||||||
|
|
@ -55,26 +61,82 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
System.out.println("Le node originel n'a pas de successeurs \n");
|
System.out.println("Le node originel n'a pas de successeurs \n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for(Arc arcActuel : listeSuccesseurs){
|
for(Arc arcActuel : listeSuccesseurs){
|
||||||
Label labelAInserer ; //On set le label à insérer
|
Label labelAInserer = new Label(arcActuel.getDestination(),true,arcActuel.getMinimumTravelTime(),arcActuel) ; //On set le label à insérer
|
||||||
labelAInserer.setSommetCourant(arcActuel.getDestination());
|
/*labelAInserer.setSommetCourant(arcActuel.getDestination());
|
||||||
labelAInserer.setMarque(false);
|
labelAInserer.setMarque(true);
|
||||||
labelAInserer.setCoutRealise(arcActuel.getMinimumTravelTime());
|
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
|
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()){
|
while (!leTas.isEmpty()){
|
||||||
Label labelMin = leTas.findMin() ;
|
Label labelMin = leTas.findMin();
|
||||||
leTas.deleteMin();
|
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