Browse Source

Implemented all functions to pass all tests

Arnaud Vergnet 4 years ago
parent
commit
fa236a612f
1 changed files with 76 additions and 45 deletions
  1. 76
    45
      be-graphes-model/src/main/java/org/insa/graphs/model/Path.java

+ 76
- 45
be-graphes-model/src/main/java/org/insa/graphs/model/Path.java View File

@@ -8,68 +8,105 @@ import java.util.List;
8 8
  * <p>
9 9
  * Class representing a path between nodes in a graph.
10 10
  * </p>
11
- * 
11
+ *
12 12
  * <p>
13 13
  * A path is represented as a list of {@link Arc} with an origin and not a list
14 14
  * of {@link Node} due to the multi-graph nature (multiple arcs between two
15 15
  * nodes) of the considered graphs.
16 16
  * </p>
17
- *
18 17
  */
19 18
 public class Path {
20 19
 
21 20
     /**
22 21
      * Create a new path that goes through the given list of nodes (in order),
23 22
      * choosing the fastest route if multiple are available.
24
-     * 
23
+     *
25 24
      * @param graph Graph containing the nodes in the list.
26 25
      * @param nodes List of nodes to build the path.
27
-     * 
28 26
      * @return A path that goes through the given list of nodes.
29
-     * 
30 27
      * @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
31
-     *         consecutive nodes in the list are not connected in the graph.
32
-     * 
33
-     * @deprecated Need to be implemented.
28
+     *                                  consecutive nodes in the list are not connected in the graph.
34 29
      */
35 30
     public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes)
36 31
             throws IllegalArgumentException {
37
-        List<Arc> arcs = new ArrayList<Arc>();
38
-        // TODO:
39
-        return new Path(graph, arcs);
32
+        return Path.createSmallestPath(graph, nodes, false);
40 33
     }
41 34
 
42 35
     /**
43 36
      * Create a new path that goes through the given list of nodes (in order),
44 37
      * choosing the shortest route if multiple are available.
45
-     * 
38
+     *
46 39
      * @param graph Graph containing the nodes in the list.
47 40
      * @param nodes List of nodes to build the path.
48
-     * 
49 41
      * @return A path that goes through the given list of nodes.
50
-     * 
51 42
      * @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
52
-     *         consecutive nodes in the list are not connected in the graph.
53
-     * 
54
-     * @deprecated Need to be implemented.
43
+     *                                  consecutive nodes in the list are not connected in the graph.
55 44
      */
56 45
     public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes)
57 46
             throws IllegalArgumentException {
58
-        List<Arc> arcs = new ArrayList<Arc>();
59
-        // TODO:
47
+        return Path.createSmallestPath(graph, nodes, true);
48
+    }
49
+
50
+    /**
51
+     * Create a new path that goes through the given list of nodes (in order),
52
+     * choosing the shortest or fastest route if multiple are available.
53
+     *
54
+     * @param graph Graph containing the nodes in the list.
55
+     * @param nodes List of nodes to build the path.
56
+     * @param isLength Should we search the shortest path (true) or the fastest path (false)
57
+     * @return A path that goes through the given list of nodes.
58
+     * @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
59
+     *                                  consecutive nodes in the list are not connected in the graph.
60
+     */
61
+    private static Path createSmallestPath(Graph graph, List<Node> nodes, boolean isLength) {
62
+        List<Arc> arcs = new ArrayList<>();
63
+        if (nodes.size() != 0 && nodes.size() != 1) {
64
+            for (int i = 0; i < (nodes.size() - 1); i++) {
65
+                Arc shortest = Path.getSmallestArc(
66
+                        nodes.get(i).getSuccessors(), nodes.get(i + 1), isLength
67
+                );
68
+                arcs.add(shortest);
69
+            }
70
+        } else if (nodes.size() == 1)
71
+            return new Path(graph, nodes.get(0));
60 72
         return new Path(graph, arcs);
61 73
     }
62 74
 
63 75
     /**
76
+     * Gets the smallest arc in the given list.
77
+     * If an arc destination does not match the given node, it is ignored.
78
+     *
79
+     * @param arcs     The arcs list to search in
80
+     * @param nextNode The next node to match the arc destination
81
+     * @param isLength Should we search the shortest arc (true) or the fastest arc (false)
82
+     * @return The smallest arc to nextNode
83
+     * @throws IllegalArgumentException If no node is found
84
+     */
85
+    private static Arc getSmallestArc(List<Arc> arcs, Node nextNode, boolean isLength) {
86
+        Arc shortestArc = null;
87
+        double smallestComparator = -1;
88
+        for (Arc arc : arcs) {
89
+            if (nextNode.equals(arc.getDestination())) {
90
+                double comparator = isLength ? arc.getLength() : arc.getMinimumTravelTime();
91
+                if (comparator < smallestComparator || smallestComparator < 0) {
92
+                    smallestComparator = comparator;
93
+                    shortestArc = arc;
94
+                }
95
+            }
96
+        }
97
+        if (shortestArc == null)
98
+            throw new IllegalArgumentException();
99
+        return shortestArc;
100
+    }
101
+
102
+    /**
64 103
      * Concatenate the given paths.
65
-     * 
104
+     *
66 105
      * @param paths Array of paths to concatenate.
67
-     * 
68 106
      * @return Concatenated path.
69
-     * 
70 107
      * @throws IllegalArgumentException if the paths cannot be concatenated (IDs of
71
-     *         map do not match, or the end of a path is not the beginning of the
72
-     *         next).
108
+     *                                  map do not match, or the end of a path is not the beginning of the
109
+     *                                  next).
73 110
      */
74 111
     public static Path concatenate(Path... paths) throws IllegalArgumentException {
75 112
         if (paths.length == 0) {
@@ -83,7 +120,7 @@ public class Path {
83 120
             }
84 121
         }
85 122
         ArrayList<Arc> arcs = new ArrayList<>();
86
-        for (Path path: paths) {
123
+        for (Path path : paths) {
87 124
             arcs.addAll(path.getArcs());
88 125
         }
89 126
         Path path = new Path(paths[0].getGraph(), arcs);
@@ -105,7 +142,7 @@ public class Path {
105 142
 
106 143
     /**
107 144
      * Create an empty path corresponding to the given graph.
108
-     * 
145
+     *
109 146
      * @param graph Graph containing the path.
110 147
      */
111 148
     public Path(Graph graph) {
@@ -116,9 +153,9 @@ public class Path {
116 153
 
117 154
     /**
118 155
      * Create a new path containing a single node.
119
-     * 
156
+     *
120 157
      * @param graph Graph containing the path.
121
-     * @param node Single node of the path.
158
+     * @param node  Single node of the path.
122 159
      */
123 160
     public Path(Graph graph, Node node) {
124 161
         this.graph = graph;
@@ -128,9 +165,9 @@ public class Path {
128 165
 
129 166
     /**
130 167
      * Create a new path with the given list of arcs.
131
-     * 
168
+     *
132 169
      * @param graph Graph containing the path.
133
-     * @param arcs Arcs to construct the path.
170
+     * @param arcs  Arcs to construct the path.
134 171
      */
135 172
     public Path(Graph graph, List<Arc> arcs) {
136 173
         this.graph = graph;
@@ -168,7 +205,7 @@ public class Path {
168 205
 
169 206
     /**
170 207
      * Check if this path is empty (it does not contain any node).
171
-     * 
208
+     *
172 209
      * @return true if this path is empty, false otherwise.
173 210
      */
174 211
     public boolean isEmpty() {
@@ -177,7 +214,7 @@ public class Path {
177 214
 
178 215
     /**
179 216
      * Get the number of <b>nodes</b> in this path.
180
-     * 
217
+     *
181 218
      * @return Number of nodes in this path.
182 219
      */
183 220
     public int size() {
@@ -186,7 +223,7 @@ public class Path {
186 223
 
187 224
     /**
188 225
      * Check if this path is valid.
189
-     * 
226
+     * <p>
190 227
      * A path is valid if any of the following is true:
191 228
      * <ul>
192 229
      * <li>it is empty;</li>
@@ -195,10 +232,8 @@ public class Path {
195 232
      * consecutive arcs, the destination of the first one is the origin of the
196 233
      * second one.</li>
197 234
      * </ul>
198
-     * 
235
+     *
199 236
      * @return true if the path is valid, false otherwise.
200
-     * 
201
-     * @deprecated Need to be implemented.
202 237
      */
203 238
     public boolean isValid() {
204 239
         boolean valid = isEmpty() || (this.arcs.size() == 0 && this.origin != null);
@@ -206,7 +241,7 @@ public class Path {
206 241
             valid = this.arcs.get(0).getOrigin().equals(this.origin);
207 242
             for (int i = 1; i < 3; i++) {
208 243
                 if (this.arcs.size() > i)
209
-                    valid = valid && (this.arcs.get(i).getOrigin().equals(this.arcs.get(i-1).getDestination()));
244
+                    valid = valid && (this.arcs.get(i).getOrigin().equals(this.arcs.get(i - 1).getDestination()));
210 245
             }
211 246
         }
212 247
         return valid;
@@ -214,9 +249,8 @@ public class Path {
214 249
 
215 250
     /**
216 251
      * Compute the length of this path (in meters).
217
-     * 
218
-     * @return Total length of the path (in meters).
219 252
      *
253
+     * @return Total length of the path (in meters).
220 254
      */
221 255
     public float getLength() {
222 256
         float length = 0;
@@ -228,12 +262,10 @@ public class Path {
228 262
 
229 263
     /**
230 264
      * Compute the time required to travel this path if moving at the given speed.
231
-     * 
265
+     *
232 266
      * @param speed Speed to compute the travel time.
233
-     * 
234 267
      * @return Time (in seconds) required to travel this path at the given speed (in
235
-     *         kilometers-per-hour).
236
-     *
268
+     * kilometers-per-hour).
237 269
      */
238 270
     public double getTravelTime(double speed) {
239 271
         float time = 0;
@@ -246,9 +278,8 @@ public class Path {
246 278
     /**
247 279
      * Compute the time to travel this path if moving at the maximum allowed speed
248 280
      * on every arc.
249
-     * 
250
-     * @return Minimum travel time to travel this path (in seconds).
251 281
      *
282
+     * @return Minimum travel time to travel this path (in seconds).
252 283
      */
253 284
     public double getMinimumTravelTime() {
254 285
         float time = 0;

Loading…
Cancel
Save