|
@@ -5,7 +5,7 @@ import org.insa.graphs.algorithm.utils.BinaryHeap;
|
5
|
5
|
import org.insa.graphs.model.Arc;
|
6
|
6
|
import org.insa.graphs.model.Path;
|
7
|
7
|
import java.util.ArrayList;
|
8
|
|
-import java.util.List;//trier tout ça
|
|
8
|
+import java.util.Collections;//trier tout ça
|
9
|
9
|
|
10
|
10
|
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
11
|
11
|
|
|
@@ -18,56 +18,74 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
18
|
18
|
protected ShortestPathSolution doRun() {
|
19
|
19
|
final ShortestPathData data = getInputData();
|
20
|
20
|
ShortestPathSolution solution = new ShortestPathSolution(data,Status.UNKNOWN);//modifié
|
21
|
|
- // TODO:
|
22
|
21
|
|
23
|
22
|
//initialisation
|
24
|
|
- BinaryHeap<Integer> tas=new BinaryHeap<Integer>();//type comparable nécessaire (donc pas Arc)
|
|
23
|
+ BinaryHeap<Label> tas=new BinaryHeap<Label>();//les Label sont comparés via leurs coûts
|
25
|
24
|
Label[] tablabel=new Label[data.getGraph().size()];
|
26
|
25
|
for (int i=0;i<tablabel.length;i++) {
|
27
|
|
- tablabel[i]=new Label(data.getGraph().get(i),null,Integer.MAX_VALUE);//non marqué par défaut
|
|
26
|
+ tablabel[i]=new Label(data.getGraph().get(i),null,Float.MAX_VALUE);//non marqué par défaut
|
28
|
27
|
}//dans le tablabel[idnode] on peut retrouver node
|
29
|
28
|
tablabel[data.getOrigin().getId()].cout=0;
|
30
|
|
- tas.insert(data.getOrigin().getId());
|
31
|
|
-
|
32
|
|
- int nomark=tablabel.length;//existe sommets non marqués (ou à coût infini?)(pas optimal?)
|
|
29
|
+ tas.insert(tablabel[data.getOrigin().getId()]);
|
|
30
|
+
|
|
31
|
+ int nomark=tablabel.length;//nombre de sommets non marqués (pas optimal?)
|
33
|
32
|
int x;//id du node qu'on étudie
|
|
33
|
+ boolean arrive=false;//node de destination atteint ou pas
|
|
34
|
+ System.out.println("Initialisation OK");
|
34
|
35
|
|
35
|
|
- if (data.getOrigin()==data.getDestination()) {
|
36
|
|
- //retourne un graphe avec juste un node si besoin de gérer ce cas particulier
|
37
|
|
- //(à vérifier, peut-être qu'une fois le reste fini ce ne sera pas nécessaire)
|
38
|
|
- Path chemin= new Path(data.getGraph(),data.getOrigin());
|
39
|
|
- solution = new ShortestPathSolution(data,Status.OPTIMAL,chemin);
|
40
|
|
- nomark=0;
|
|
36
|
+ //itérations
|
|
37
|
+ while (nomark>0 && !arrive){
|
|
38
|
+
|
|
39
|
+ x=tas.deleteMin().sommet_courant.getId();
|
|
40
|
+
|
|
41
|
+ if (x==data.getDestination().getId()) {
|
|
42
|
+ arrive=true;//marche aussi si le départ et l'arrivée sont le même node
|
|
43
|
+ }else {
|
|
44
|
+
|
|
45
|
+ tablabel[x].marque=true;
|
|
46
|
+ nomark--;
|
|
47
|
+ int y;
|
|
48
|
+
|
|
49
|
+ for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {//mauvais parcours?
|
|
50
|
+
|
|
51
|
+ y=arcy.getDestination().getId();
|
|
52
|
+
|
|
53
|
+ if (!tablabel[y].marque) {
|
|
54
|
+
|
|
55
|
+ if (tablabel[y].cout>tablabel[x].cout+(float)data.getCost(arcy)) {
|
|
56
|
+
|
|
57
|
+ tablabel[y].cout=tablabel[x].cout+(float)data.getCost(arcy);
|
|
58
|
+ tablabel[y].pere=arcy;//ligne non dans le poly
|
|
59
|
+
|
|
60
|
+ if (tablabel[y].cout!=Float.MAX_VALUE)
|
|
61
|
+ tas.remove(tablabel[y]);
|
|
62
|
+ //méthode à vérifier pour opérer update ou insert
|
|
63
|
+ tas.insert(tablabel[y]);
|
|
64
|
+
|
|
65
|
+ }
|
|
66
|
+ }
|
|
67
|
+ //penser à gérer le cas "aucun chemin n'existe" (avec l'initialisation par défaut?)
|
|
68
|
+ }
|
|
69
|
+ }
|
41
|
70
|
}
|
|
71
|
+
|
|
72
|
+ System.out.println("Itérations OK");
|
42
|
73
|
|
43
|
|
- //itérations
|
44
|
|
- while (nomark>0){
|
45
|
|
- x=tas.deleteMin();
|
46
|
|
- tablabel[x].marque=true;
|
47
|
|
- nomark--;
|
48
|
|
- for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {
|
49
|
|
- if (!tablabel[arcy.getDestination().getId()].marque) {
|
50
|
|
- if (tablabel[arcy.getDestination().getId()].cout>tablabel[x].cout+(int)arcy.getMinimumTravelTime()) {
|
51
|
|
- tablabel[arcy.getDestination().getId()].cout=tablabel[x].cout+(int)arcy.getMinimumTravelTime();
|
52
|
|
- tablabel[arcy.getDestination().getId()].pere=arcy;//ligne non dans le poly
|
53
|
|
- try{
|
54
|
|
- tas.remove(arcy.getDestination().getId());
|
55
|
|
- } finally {//méthode *fortement douteuse* pour opérer update ou inssert selon si l'élément est déjà dans le tas ou pas
|
56
|
|
- tas.insert(arcy.getDestination().getId());
|
57
|
|
- }
|
58
|
|
- }//s'aider de l'algorithme de Bellman-Ford déjà fait
|
59
|
|
- }
|
60
|
|
-
|
61
|
|
- //penser à gérer le cas "aucun chemin n'existe" (avec l'initialisation par défaut?)
|
|
74
|
+ if (!arrive) {
|
|
75
|
+ solution= new ShortestPathSolution(data,Status.INFEASIBLE);
|
|
76
|
+ }else {
|
|
77
|
+ ArrayList<Arc> bonsarcs=new ArrayList<Arc>();
|
|
78
|
+ Label labelact=tablabel[data.getDestination().getId()];
|
|
79
|
+ while (labelact.pere!=null) {
|
|
80
|
+ bonsarcs.add(labelact.pere);
|
|
81
|
+ labelact=tablabel[labelact.pere.getOrigin().getId()];
|
|
82
|
+ //à vérifier
|
|
83
|
+ }//est-ce que le cas "un seul node" marche bien en renvoyant un unique arc null?
|
|
84
|
+ Collections.reverse(bonsarcs);
|
|
85
|
+ //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
|
|
86
|
+ solution = new ShortestPathSolution(data,Status.OPTIMAL,new Path(data.getGraph(),bonsarcs));
|
62
|
87
|
}
|
63
|
|
- }
|
64
|
|
- //créer le chemin solution grâce au tas (le tas nécessite un type comparable donc l'id des nodes)
|
65
|
|
- List<Arc> bonsarcs=new ArrayList<Arc>();//pourquoi une erreur ici?
|
66
|
|
- while (!tas.isEmpty()) {
|
67
|
|
- bonsarcs.add(tablabel[tas.deleteMin()].pere);//un node de plus qu'il n'y a d'arcs, vérifier ce que ça donne
|
68
|
|
- }//est-ce que le cas "un seul node" marche bien en renvoyant un unique arc null?
|
69
|
|
- Path chemin= new Path(data.getGraph(),bonsarcs);
|
70
|
|
- solution = new ShortestPathSolution(data,Status.OPTIMAL,chemin);
|
|
88
|
+ System.out.println("Fin OK");
|
71
|
89
|
return solution;
|
72
|
90
|
}
|
73
|
91
|
|