No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DijkstraAlgorithm.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package org.insa.graphs.algorithm.shortestpath;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.ArrayList;
  5. import org.insa.graphs.algorithm.AbstractInputData;
  6. import org.insa.graphs.algorithm.AbstractSolution.Status;
  7. import org.insa.graphs.algorithm.utils.BinaryHeap;
  8. import org.insa.graphs.algorithm.utils.Label;
  9. import org.insa.graphs.model.Arc;
  10. import org.insa.graphs.model.Graph;
  11. import org.insa.graphs.model.Node;
  12. import org.insa.graphs.model.Path;
  13. public class DijkstraAlgorithm extends ShortestPathAlgorithm {
  14. public DijkstraAlgorithm(ShortestPathData data) {
  15. super(data);
  16. }
  17. @Override
  18. protected ShortestPathSolution doRun() {
  19. // Récupération des différentes données du graph
  20. ShortestPathData data = getInputData();
  21. Graph graph = data.getGraph();
  22. List<Node> nodes = graph.getNodes();
  23. final int nbNodes = graph.size();
  24. //On récupère l'index du node origine du chemin à déterminer
  25. int index_origine = data.getOrigin().getId();
  26. //On récupère l'index du node destination
  27. int index_dest = data.getDestination().getId();
  28. notifyOriginProcessed(data.getOrigin());
  29. /////////////////////////////////////////////////////////////////////////////////////////////////
  30. //////////////////////////////////////INITIALISATION/////////////////////////////////////////////
  31. /////////////////////////////////////////////////////////////////////////////////////////////////
  32. BinaryHeap<Label> tas = new BinaryHeap<Label>();
  33. ArrayList<Label> labels = new ArrayList<Label>();
  34. //On initialise tous les labels à +infini, avec marque à false et pere à NULL
  35. for (int i = 0; i < nbNodes; i++) {
  36. labels.add(new Label(nodes.get(i)));
  37. }
  38. //On actualise le cout du label correspondant au node d'origine
  39. labels.get(index_origine).setCost(0);
  40. //On insère le label actualisé dans le tas
  41. tas.insert(labels.get(index_origine));
  42. /////////////////////////////////////////////////////////////////////////////////////////////////
  43. //////////////////////////////////////INITIALISATION/////////////////////////////////////////////
  44. /////////////////////////////////////////////////////////////////////////////////////////////////
  45. /////////////////////////////////////////////////////////////////////////////////////////////////
  46. /////////////////////////////////////////ITERATIONS//////////////////////////////////////////////
  47. /////////////////////////////////////////////////////////////////////////////////////////////////
  48. //Définition d'une variable pour compter le nombre d'itérations pour le debogage
  49. int nbIterations = 0;
  50. while (!labels.get(index_dest).isMarked() && tas.size() != 0) {
  51. //On récupère le label minimal dans le tas
  52. Label label_min = tas.deleteMin();
  53. //On marque le label minimal
  54. labels.get(label_min.getNode().getId()).mark();
  55. //Vérification du coût croissant des labels marqués
  56. System.out.println("Coût du label marqué : " + label_min.getCost());
  57. //Vérification de la taille du tas
  58. System.out.println("Taille du tas : " + tas.size());
  59. //Debogage
  60. //Incrémentation du nombre d'itérations
  61. nbIterations++;
  62. //Verification du tas
  63. if (tas.isValid()) {
  64. System.out.println("Tas valide");
  65. }
  66. else {
  67. System.out.println("Tas non valide");
  68. }
  69. //On récupère les arcs successeurs du label minimal
  70. List<Arc> arcs = label_min.getNode().getSuccessors();
  71. //Debogage
  72. System.out.println("Nb successeurs du label : " + arcs.size());
  73. for (int i = 0; i < arcs.size(); i++) {
  74. //On vérifie que le chemin est autorisé
  75. if (!data.isAllowed(arcs.get(i))) {
  76. continue;
  77. }
  78. //On récupère l'index de la destination de l'arc actuel
  79. int index_suiv = arcs.get(i).getDestination().getId();
  80. if (!labels.get(index_suiv).isMarked())
  81. {
  82. double oldDistance = labels.get(index_suiv).getCost();
  83. double newDistance = label_min.getCost() + data.getCost(arcs.get(i));
  84. //Coloration des chemins au fur et à mesure
  85. if (Double.isInfinite(oldDistance) && Double.isFinite(newDistance)) {
  86. notifyNodeReached(arcs.get(i).getDestination());
  87. }
  88. if (newDistance < oldDistance) {
  89. labels.get(index_suiv).setCost(newDistance);
  90. labels.get(index_suiv).setFather(arcs.get(i));
  91. if (Double.isFinite(oldDistance)) {
  92. tas.remove(labels.get(index_suiv));
  93. }
  94. tas.insert(labels.get(index_suiv));
  95. }
  96. }
  97. }
  98. }
  99. /////////////////////////////////////////////////////////////////////////////////////////////////
  100. /////////////////////////////////////////ITERATIONS//////////////////////////////////////////////
  101. /////////////////////////////////////////////////////////////////////////////////////////////////
  102. /////////////////////////////////////////////////////////////////////////////////////////////////
  103. //////////////////////////////////CREATION DE LA SOLUTION////////////////////////////////////////
  104. /////////////////////////////////////////////////////////////////////////////////////////////////
  105. ShortestPathSolution solution = null;
  106. //La destination n'a pas de prédécesseur, le chemin est infaisable
  107. if (!labels.get(index_dest).isMarked()) {
  108. solution = new ShortestPathSolution(data, Status.INFEASIBLE);
  109. }
  110. else {
  111. //La destination a été trouvée. On en informe l'utilisateur.
  112. notifyDestinationReached(data.getDestination());
  113. //On crée un nouveau chemin à partir des prédécesseurs
  114. ArrayList<Arc> chemin = new ArrayList<>();
  115. Arc arc = labels.get(index_dest).getFather();
  116. while (arc != null) {
  117. chemin.add(arc);
  118. arc = labels.get(arc.getOrigin().getId()).getFather();
  119. }
  120. //Affichages pour le debogage
  121. System.out.println("Nombre d'itérations : " + nbIterations);
  122. System.out.println("Nombre d'arcs dans le plus court chemin : " + chemin.size());
  123. //On inverse ce chemin
  124. Collections.reverse(chemin);
  125. //On crée la solution finale
  126. solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, chemin));
  127. //Debogage
  128. if (!solution.getPath().isValid()) {
  129. System.out.println("Chemin trouvé non valide.");
  130. }
  131. else {
  132. System.out.println("Chemin trouvé valide.");
  133. }
  134. if (data.getMode() == AbstractInputData.Mode.TIME) {
  135. System.out.println("Durée chemin Path : " + solution.getPath().getMinimumTravelTime() + ", Dijkstra : " + labels.get(index_dest).getCost());
  136. }
  137. else {
  138. System.out.println("Longueur chemin Path : " + solution.getPath().getLength() + ", Dijkstra : " + labels.get(index_dest).getCost());
  139. }
  140. }
  141. /////////////////////////////////////////////////////////////////////////////////////////////////
  142. //////////////////////////////////CREATION DE LA SOLUTION////////////////////////////////////////
  143. /////////////////////////////////////////////////////////////////////////////////////////////////
  144. return solution;
  145. }
  146. }