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

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