|
@@ -1,15 +1,17 @@
|
1
|
1
|
package org.insa.graphs.algorithm.shortestpath;
|
2
|
|
-
|
3
|
|
-import java.util.ArrayList;
|
4
|
|
-import java.util.Arrays;
|
5
|
2
|
import java.util.Collections;
|
6
|
|
-import java.util.Iterator;
|
|
3
|
+import java.util.List;
|
|
4
|
+import java.util.ArrayList;
|
7
|
5
|
|
|
6
|
+import org.insa.graphs.algorithm.AbstractInputData;
|
|
7
|
+import org.insa.graphs.algorithm.AbstractSolution.Status;
|
|
8
|
+import org.insa.graphs.algorithm.utils.BinaryHeap;
|
|
9
|
+import org.insa.graphs.algorithm.utils.Label;
|
8
|
10
|
import org.insa.graphs.model.Arc;
|
9
|
11
|
import org.insa.graphs.model.Graph;
|
10
|
|
-import org.insa.algo.AbstractSolution.Status;
|
11
|
|
-import org.insa.algo.utils.*;
|
12
|
|
-import org.insa.graph.*;
|
|
12
|
+import org.insa.graphs.model.Node;
|
|
13
|
+import org.insa.graphs.model.Path;
|
|
14
|
+
|
13
|
15
|
|
14
|
16
|
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
15
|
17
|
|
|
@@ -19,29 +21,158 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
19
|
21
|
|
20
|
22
|
@Override
|
21
|
23
|
protected ShortestPathSolution doRun() {
|
22
|
|
- ShortestPathSolution solution = null;
|
23
|
|
-
|
24
|
|
- // Initialisation
|
25
|
|
- // du graphe
|
26
|
|
- final ShortestPathData data = getInputData();
|
|
24
|
+
|
|
25
|
+ // Récupération des différentes données du graph
|
|
26
|
+
|
|
27
|
+ ShortestPathData data = getInputData();
|
27
|
28
|
Graph graph = data.getGraph();
|
|
29
|
+ List<Node> nodes = graph.getNodes();
|
28
|
30
|
final int nbNodes = graph.size();
|
29
|
|
- // des couts
|
30
|
|
- double[] distances = new double[nbNodes];
|
31
|
|
- Arrays.fill(distances, Double.POSITIVE_INFINITY);
|
32
|
|
- distances[data.getOrigin().getId()] = 0;
|
33
|
31
|
|
34
|
|
- // Notify observers about the first event (origin processed).
|
|
32
|
+ //On récupère l'index du node origine du chemin à déterminer
|
|
33
|
+ int index_origine = data.getOrigin().getId();
|
|
34
|
+ //On récupère l'index du node destination
|
|
35
|
+ int index_dest = data.getDestination().getId();
|
|
36
|
+
|
35
|
37
|
notifyOriginProcessed(data.getOrigin());
|
|
38
|
+
|
|
39
|
+ /////////////////////////////////////////////////////////////////////////////////////////////////
|
|
40
|
+ //////////////////////////////////////INITIALISATION/////////////////////////////////////////////
|
|
41
|
+ /////////////////////////////////////////////////////////////////////////////////////////////////
|
|
42
|
+ BinaryHeap<Label> tas = new BinaryHeap<Label>();
|
|
43
|
+ ArrayList<Label> labels = new ArrayList<Label>();
|
|
44
|
+ //On initialise tous les labels à +infini, avec marque à false et pere à NULL
|
|
45
|
+ for (int i = 0; i < nbNodes; i++) {
|
|
46
|
+ labels.add(new Label(nodes.get(i)));
|
|
47
|
+ }
|
|
48
|
+ //On actualise le cout du label correspondant au node d'origine
|
|
49
|
+ labels.get(index_origine).setCost(0);
|
|
50
|
+ //On insère le label actualisé dans le tas
|
|
51
|
+ tas.insert(labels.get(index_origine));
|
|
52
|
+ /////////////////////////////////////////////////////////////////////////////////////////////////
|
|
53
|
+ //////////////////////////////////////INITIALISATION/////////////////////////////////////////////
|
|
54
|
+ /////////////////////////////////////////////////////////////////////////////////////////////////
|
36
|
55
|
|
37
|
|
- // Initialize array of predecessors.
|
38
|
|
- Arc[] predecessorArcs = new Arc[nbNodes];
|
|
56
|
+ /////////////////////////////////////////////////////////////////////////////////////////////////
|
|
57
|
+ /////////////////////////////////////////ITERATIONS//////////////////////////////////////////////
|
|
58
|
+ /////////////////////////////////////////////////////////////////////////////////////////////////
|
39
|
59
|
|
40
|
|
-
|
41
|
|
- while (solution == null) { //Tant qu'il y a pas de solution
|
|
60
|
+ //Définition d'une variable pour compter le nombre d'itérations pour le debogage
|
|
61
|
+ int nbIterations = 0;
|
|
62
|
+
|
|
63
|
+ while (!labels.get(index_dest).isMarked() && tas.size() != 0) {
|
|
64
|
+ //On récupère le label minimal dans le tas
|
|
65
|
+ Label label_min = tas.deleteMin();
|
|
66
|
+ //On marque le label minimal
|
|
67
|
+ labels.get(label_min.getNode().getId()).mark();
|
|
68
|
+
|
|
69
|
+ //Vérification du coût croissant des labels marqués
|
|
70
|
+ System.out.println("Coût du label marqué : " + label_min.getCost());
|
|
71
|
+ //Vérification de la taille du tas
|
|
72
|
+ System.out.println("Taille du tas : " + tas.size());
|
|
73
|
+
|
|
74
|
+ //Debogage
|
|
75
|
+ //Incrémentation du nombre d'itérations
|
|
76
|
+ nbIterations++;
|
|
77
|
+ //Verification du tas
|
|
78
|
+ if (tas.isValid()) {
|
|
79
|
+ System.out.println("Tas valide");
|
|
80
|
+ }
|
|
81
|
+ else {
|
|
82
|
+ System.out.println("Tas non valide");
|
|
83
|
+ }
|
42
|
84
|
|
|
85
|
+
|
|
86
|
+ //On récupère les arcs successeurs du label minimal
|
|
87
|
+ List<Arc> arcs = label_min.getNode().getSuccessors();
|
|
88
|
+
|
|
89
|
+ //Debogage
|
|
90
|
+ System.out.println("Nb successeurs du label : " + arcs.size());
|
|
91
|
+
|
|
92
|
+ for (int i = 0; i < arcs.size(); i++) {
|
|
93
|
+ //On vérifie que le chemin est autorisé
|
|
94
|
+ if (!data.isAllowed(arcs.get(i))) {
|
|
95
|
+ continue;
|
|
96
|
+ }
|
|
97
|
+ //On récupère l'index de la destination de l'arc actuel
|
|
98
|
+ int index_suiv = arcs.get(i).getDestination().getId();
|
|
99
|
+
|
|
100
|
+ if (!labels.get(index_suiv).isMarked())
|
|
101
|
+ {
|
|
102
|
+ double oldDistance = labels.get(index_suiv).getCost();
|
|
103
|
+ double newDistance = label_min.getCost() + data.getCost(arcs.get(i));
|
|
104
|
+
|
|
105
|
+ //Coloration des chemins au fur et à mesure
|
|
106
|
+ if (Double.isInfinite(oldDistance) && Double.isFinite(newDistance)) {
|
|
107
|
+ notifyNodeReached(arcs.get(i).getDestination());
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ if (newDistance < oldDistance) {
|
|
111
|
+ labels.get(index_suiv).setCost(newDistance);
|
|
112
|
+ labels.get(index_suiv).setFather(arcs.get(i));
|
|
113
|
+ if (Double.isFinite(oldDistance)) {
|
|
114
|
+ tas.remove(labels.get(index_suiv));
|
|
115
|
+ }
|
|
116
|
+ tas.insert(labels.get(index_suiv));
|
|
117
|
+ }
|
|
118
|
+ }
|
|
119
|
+ }
|
|
120
|
+ }
|
|
121
|
+ /////////////////////////////////////////////////////////////////////////////////////////////////
|
|
122
|
+ /////////////////////////////////////////ITERATIONS//////////////////////////////////////////////
|
|
123
|
+ /////////////////////////////////////////////////////////////////////////////////////////////////
|
|
124
|
+
|
|
125
|
+ /////////////////////////////////////////////////////////////////////////////////////////////////
|
|
126
|
+ //////////////////////////////////CREATION DE LA SOLUTION////////////////////////////////////////
|
|
127
|
+ /////////////////////////////////////////////////////////////////////////////////////////////////
|
|
128
|
+ ShortestPathSolution solution = null;
|
|
129
|
+
|
|
130
|
+ //La destination n'a pas de prédécesseur, le chemin est infaisable
|
|
131
|
+ if (!labels.get(index_dest).isMarked()) {
|
|
132
|
+ solution = new ShortestPathSolution(data, Status.INFEASIBLE);
|
|
133
|
+ }
|
|
134
|
+ else {
|
|
135
|
+
|
|
136
|
+ //La destination a été trouvée. On en informe l'utilisateur.
|
|
137
|
+ notifyDestinationReached(data.getDestination());
|
|
138
|
+
|
|
139
|
+ //On crée un nouveau chemin à partir des prédécesseurs
|
|
140
|
+ ArrayList<Arc> chemin = new ArrayList<>();
|
|
141
|
+ Arc arc = labels.get(index_dest).getFather();
|
|
142
|
+ while (arc != null) {
|
|
143
|
+ chemin.add(arc);
|
|
144
|
+ arc = labels.get(arc.getOrigin().getId()).getFather();
|
|
145
|
+ }
|
|
146
|
+
|
|
147
|
+ //Affichages pour le debogage
|
|
148
|
+ System.out.println("Nombre d'itérations : " + nbIterations);
|
|
149
|
+ System.out.println("Nombre d'arcs dans le plus court chemin : " + chemin.size());
|
|
150
|
+
|
|
151
|
+ //On inverse ce chemin
|
|
152
|
+ Collections.reverse(chemin);
|
|
153
|
+
|
|
154
|
+ //On crée la solution finale
|
|
155
|
+ solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, chemin));
|
|
156
|
+
|
|
157
|
+ //Debogage
|
|
158
|
+ if (!solution.getPath().isValid()) {
|
|
159
|
+ System.out.println("Chemin trouvé non valide.");
|
|
160
|
+ }
|
|
161
|
+ else {
|
|
162
|
+ System.out.println("Chemin trouvé valide.");
|
|
163
|
+ }
|
|
164
|
+ if (data.getMode() == AbstractInputData.Mode.TIME) {
|
|
165
|
+ System.out.println("Durée chemin Path : " + solution.getPath().getMinimumTravelTime() + ", Dijkstra : " + labels.get(index_dest).getCost());
|
|
166
|
+ }
|
|
167
|
+ else {
|
|
168
|
+ System.out.println("Longueur chemin Path : " + solution.getPath().getLength() + ", Dijkstra : " + labels.get(index_dest).getCost());
|
|
169
|
+ }
|
|
170
|
+
|
43
|
171
|
}
|
44
|
|
- // TODO:
|
|
172
|
+ /////////////////////////////////////////////////////////////////////////////////////////////////
|
|
173
|
+ //////////////////////////////////CREATION DE LA SOLUTION////////////////////////////////////////
|
|
174
|
+ /////////////////////////////////////////////////////////////////////////////////////////////////
|
|
175
|
+
|
45
|
176
|
return solution;
|
46
|
177
|
}
|
47
|
178
|
|