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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.AccessRestrictions.AccessMode;
  5. import org.insa.graphs.model.AccessRestrictions.AccessRestriction;
  6. import org.insa.graphs.model.Arc;
  7. import org.insa.graphs.model.Node;
  8. import org.insa.graphs.model.Path;
  9. import java.util.ArrayList;
  10. import java.util.Collections;//trier tout ça
  11. public class DijkstraAlgorithm extends ShortestPathAlgorithm {
  12. //facilite la comparaison de Dijkstra et A* en temps
  13. private int nbnodes= Integer.MAX_VALUE;
  14. public int getnbnodes() {
  15. return this.nbnodes;
  16. }
  17. public DijkstraAlgorithm(ShortestPathData data) {
  18. super(data);
  19. }
  20. public Label LabelTyped(Node sommet,Arc padre, float prix){
  21. return new Label(sommet,padre, prix);
  22. }
  23. @Override
  24. protected ShortestPathSolution doRun() {
  25. this.nbnodes=0;
  26. int mul=1;//problème ouvert
  27. final ShortestPathData data = getInputData();
  28. ShortestPathSolution solution = new ShortestPathSolution(data,Status.UNKNOWN);//modifié
  29. if (data.getDestination()==null || data.getOrigin()==null) {
  30. solution= new ShortestPathSolution(data, Status.INFEASIBLE, new Path(data.getGraph()));
  31. }else {
  32. //initialisation
  33. BinaryHeap<Label> tas=new BinaryHeap<Label>();//les Label sont comparés via leurs coûts
  34. Label[] tablabel=new Label[data.getGraph().size()];
  35. for (int i=0;i<tablabel.length;i++) {
  36. tablabel[i]=LabelTyped(data.getGraph().get(i),null,Float.MAX_VALUE);//non marqué par défaut
  37. }//dans le tablabel[idnode] on peut retrouver node
  38. tablabel[data.getOrigin().getId()].cout=0;
  39. tas.insert(tablabel[data.getOrigin().getId()]);
  40. int nomark=tablabel.length;//nombre de sommets non marqués (pas optimal?)
  41. int x;//id du node qu'on étudie
  42. boolean arrive=false;//node de destination atteint ou pas
  43. //itérations
  44. while (nomark>0 && !arrive && !tas.isEmpty()){
  45. x=tas.deleteMin().sommet_courant.getId();
  46. if (x==data.getDestination().getId()) {
  47. arrive=true;//marche aussi si le départ et l'arrivée sont le même node
  48. }else {
  49. tablabel[x].marque=true;
  50. this.nbnodes++;
  51. this.notifyNodeMarked(tablabel[x].sommet_courant);
  52. //System.out.printf("%f\n",tablabel[x].getTotalCost());
  53. //System.out.printf("%d\n",tablabel[x].sommet_courant.getNumberOfSuccessors());
  54. nomark--;
  55. int y;
  56. for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {
  57. y=arcy.getDestination().getId();
  58. //System.out.println(tas.isValid());
  59. if (!tablabel[y].marque && data.isAllowed(arcy)) {
  60. if (arcy.getRoadInformation().getAccessRestrictions().isAllowedFor(AccessMode.MOTORCAR,
  61. AccessRestriction.ALLOWED))
  62. mul=1;
  63. else//ce bloc sert à l'implémentation du problème ouvert
  64. mul=1;
  65. if (tablabel[y].getCost()>tablabel[x].getCost()+mul*(float)data.getCost(arcy)) {
  66. if (tablabel[y].getCost()!=Float.MAX_VALUE) {
  67. tas.remove(tablabel[y]);
  68. this.notifyNodeReached(arcy.getDestination());
  69. }
  70. tablabel[y].cout=tablabel[x].getCost()+mul*(float)data.getCost(arcy);
  71. tablabel[y].pere=arcy;
  72. tas.insert(tablabel[y]);
  73. }
  74. }
  75. }
  76. }
  77. }
  78. if (!arrive) {
  79. solution= new ShortestPathSolution(data,Status.INFEASIBLE, new Path(data.getGraph()));
  80. }else {
  81. this.notifyDestinationReached(data.getDestination());
  82. ArrayList<Arc> bonsarcs=new ArrayList<Arc>();
  83. Label labelact=tablabel[data.getDestination().getId()];
  84. while (labelact.pere!=null) {
  85. bonsarcs.add(labelact.pere);
  86. labelact=tablabel[labelact.pere.getOrigin().getId()];
  87. }
  88. Collections.reverse(bonsarcs);
  89. solution = new ShortestPathSolution(data,Status.OPTIMAL,new Path(data.getGraph(),bonsarcs));
  90. //Path chemin= new Path(data.getGraph(),bonsarcs);//y'a t-il un retour si le chemin est infaisable?
  91. //System.out.printf("valide: %b, longueur: %f\n",chemin.isValid(), chemin.getLength());
  92. //System.out.printf("distance réelle origine-destination (test): %f", (float)this.getInputData().getOrigin().getPoint().distanceTo(this.getInputData().getDestination().getPoint()));
  93. }
  94. }
  95. return solution;
  96. }
  97. }