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.5KB

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