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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package org.insa.graphs.algorithm.shortestpath;
  2. import org.insa.graphs.algorithm.AbstractSolution.Status;
  3. import org.insa.graphs.algorithm.utils.BinaryHeap;
  4. import org.insa.graphs.model.Arc;
  5. import org.insa.graphs.model.Node;
  6. import org.insa.graphs.model.Path;
  7. import java.util.ArrayList;
  8. import java.util.Collections;//trier tout ça
  9. public class DijkstraAlgorithm extends ShortestPathAlgorithm {
  10. public DijkstraAlgorithm(ShortestPathData data) {
  11. super(data);
  12. }
  13. public Label LabelTyped(Node sommet,Arc padre, float prix){
  14. return new Label(sommet,padre, prix);
  15. }
  16. @Override
  17. protected ShortestPathSolution doRun() {
  18. final ShortestPathData data = getInputData();
  19. ShortestPathSolution solution = new ShortestPathSolution(data,Status.UNKNOWN);//modifié
  20. //initialisation
  21. BinaryHeap<Label> tas=new BinaryHeap<Label>();//les Label sont comparés via leurs coûts
  22. Label[] tablabel=new Label[data.getGraph().size()];
  23. for (int i=0;i<tablabel.length;i++) {
  24. tablabel[i]=LabelTyped(data.getGraph().get(i),null,Float.MAX_VALUE);//non marqué par défaut
  25. }//dans le tablabel[idnode] on peut retrouver node
  26. tablabel[data.getOrigin().getId()].cout=0;
  27. tas.insert(tablabel[data.getOrigin().getId()]);
  28. int nomark=tablabel.length;//nombre de sommets non marqués (pas optimal?)
  29. int x;//id du node qu'on étudie
  30. boolean arrive=false;//node de destination atteint ou pas
  31. //itérations
  32. while (nomark>0 && !arrive && !tas.isEmpty()){
  33. x=tas.deleteMin().sommet_courant.getId();
  34. if (x==data.getDestination().getId()) {
  35. arrive=true;//marche aussi si le départ et l'arrivée sont le même node
  36. }else {
  37. tablabel[x].marque=true;
  38. this.notifyNodeMarked(tablabel[x].sommet_courant);
  39. //System.out.printf("%f\n",tablabel[x].cout);
  40. //System.out.printf("%d\n",tablabel[x].sommet_courant.getNumberOfSuccessors());
  41. nomark--;
  42. int y;
  43. for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {
  44. y=arcy.getDestination().getId();
  45. //System.out.println(tas.isValid());
  46. if (!tablabel[y].marque && data.isAllowed(arcy)) {//ligne 108 de l'excel d'avancement
  47. if (tablabel[y].getCost()>tablabel[x].getCost()+(float)data.getCost(arcy)) {
  48. if (tablabel[y].getCost()!=Float.MAX_VALUE) {
  49. tas.remove(tablabel[y]);
  50. this.notifyNodeReached(arcy.getDestination());
  51. }
  52. tablabel[y].cout=tablabel[x].getCost()+(float)data.getCost(arcy);
  53. tablabel[y].pere=arcy;//ligne non dans le poly
  54. tas.insert(tablabel[y]);
  55. }
  56. }
  57. //penser à gérer le cas "aucun chemin n'existe" (avec l'initialisation par défaut?)
  58. }
  59. }
  60. }
  61. if (!arrive) {
  62. solution= new ShortestPathSolution(data,Status.INFEASIBLE);
  63. }else {
  64. this.notifyDestinationReached(data.getDestination());
  65. ArrayList<Arc> bonsarcs=new ArrayList<Arc>();
  66. Label labelact=tablabel[data.getDestination().getId()];
  67. while (labelact.pere!=null) {
  68. bonsarcs.add(labelact.pere);
  69. labelact=tablabel[labelact.pere.getOrigin().getId()];
  70. }
  71. Collections.reverse(bonsarcs);
  72. solution = new ShortestPathSolution(data,Status.OPTIMAL,new Path(data.getGraph(),bonsarcs));
  73. //Path chemin= new Path(data.getGraph(),bonsarcs);//y'a t-il un retour si le chemin est infaisable?
  74. //System.out.printf("valide: %b, longueur: %f\n",chemin.isValid(), chemin.getLength());
  75. //System.out.printf("distance réelle origine-destination (test): %f", (float)this.getInputData().getOrigin().getPoint().distanceTo(this.getInputData().getDestination().getPoint()));
  76. //comparer le coût avec celui de la méthode Path en utilisant equals ou compareTo, mais Dijkstra ne renvoie pas de coût de lui-même, ni ShortestPathSolution?
  77. }
  78. return solution;
  79. }
  80. }