Browse Source

Implemented step 2

Now createFastestPath and createShortestPath are created. Step 2
finished
Adrien Barbanson 3 years ago
parent
commit
8eeb871b1a
1 changed files with 335 additions and 226 deletions
  1. 335
    226
      be-graphes-model/src/main/java/org/insa/graphs/model/Path.java

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

@@ -18,231 +18,340 @@ import java.util.List;
18 18
  */
19 19
 public class Path {
20 20
 
21
-    /**
22
-     * Create a new path that goes through the given list of nodes (in order),
23
-     * choosing the fastest route if multiple are available.
24
-     * 
25
-     * @param graph Graph containing the nodes in the list.
26
-     * @param nodes List of nodes to build the path.
27
-     * 
28
-     * @return A path that goes through the given list of nodes.
29
-     * 
30
-     * @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.
34
-     */
35
-    public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes)
36
-            throws IllegalArgumentException {
37
-        List<Arc> arcs = new ArrayList<Arc>();
38
-        // TODO:
39
-        return new Path(graph, arcs);
40
-    }
41
-
42
-    /**
43
-     * Create a new path that goes through the given list of nodes (in order),
44
-     * choosing the shortest route if multiple are available.
45
-     * 
46
-     * @param graph Graph containing the nodes in the list.
47
-     * @param nodes List of nodes to build the path.
48
-     * 
49
-     * @return A path that goes through the given list of nodes.
50
-     * 
51
-     * @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.
55
-     */
56
-    public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes)
57
-            throws IllegalArgumentException {
58
-        List<Arc> arcs = new ArrayList<Arc>();
59
-        // TODO:
60
-        return new Path(graph, arcs);
61
-    }
62
-
63
-    /**
64
-     * Concatenate the given paths.
65
-     * 
66
-     * @param paths Array of paths to concatenate.
67
-     * 
68
-     * @return Concatenated path.
69
-     * 
70
-     * @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).
73
-     */
74
-    public static Path concatenate(Path... paths) throws IllegalArgumentException {
75
-        if (paths.length == 0) {
76
-            throw new IllegalArgumentException("Cannot concatenate an empty list of paths.");
77
-        }
78
-        final String mapId = paths[0].getGraph().getMapId();
79
-        for (int i = 1; i < paths.length; ++i) {
80
-            if (!paths[i].getGraph().getMapId().equals(mapId)) {
81
-                throw new IllegalArgumentException(
82
-                        "Cannot concatenate paths from different graphs.");
83
-            }
84
-        }
85
-        ArrayList<Arc> arcs = new ArrayList<>();
86
-        for (Path path: paths) {
87
-            arcs.addAll(path.getArcs());
88
-        }
89
-        Path path = new Path(paths[0].getGraph(), arcs);
90
-        if (!path.isValid()) {
91
-            throw new IllegalArgumentException(
92
-                    "Cannot concatenate paths that do not form a single path.");
93
-        }
94
-        return path;
95
-    }
96
-
97
-    // Graph containing this path.
98
-    private final Graph graph;
99
-
100
-    // Origin of the path
101
-    private final Node origin;
102
-
103
-    // List of arcs in this path.
104
-    private final List<Arc> arcs;
105
-
106
-    /**
107
-     * Create an empty path corresponding to the given graph.
108
-     * 
109
-     * @param graph Graph containing the path.
110
-     */
111
-    public Path(Graph graph) {
112
-        this.graph = graph;
113
-        this.origin = null;
114
-        this.arcs = new ArrayList<>();
115
-    }
116
-
117
-    /**
118
-     * Create a new path containing a single node.
119
-     * 
120
-     * @param graph Graph containing the path.
121
-     * @param node Single node of the path.
122
-     */
123
-    public Path(Graph graph, Node node) {
124
-        this.graph = graph;
125
-        this.origin = node;
126
-        this.arcs = new ArrayList<>();
127
-    }
128
-
129
-    /**
130
-     * Create a new path with the given list of arcs.
131
-     * 
132
-     * @param graph Graph containing the path.
133
-     * @param arcs Arcs to construct the path.
134
-     */
135
-    public Path(Graph graph, List<Arc> arcs) {
136
-        this.graph = graph;
137
-        this.arcs = arcs;
138
-        this.origin = arcs.size() > 0 ? arcs.get(0).getOrigin() : null;
139
-    }
140
-
141
-    /**
142
-     * @return Graph containing the path.
143
-     */
144
-    public Graph getGraph() {
145
-        return graph;
146
-    }
147
-
148
-    /**
149
-     * @return First node of the path.
150
-     */
151
-    public Node getOrigin() {
152
-        return origin;
153
-    }
154
-
155
-    /**
156
-     * @return Last node of the path.
157
-     */
158
-    public Node getDestination() {
159
-        return arcs.get(arcs.size() - 1).getDestination();
160
-    }
161
-
162
-    /**
163
-     * @return List of arcs in the path.
164
-     */
165
-    public List<Arc> getArcs() {
166
-        return Collections.unmodifiableList(arcs);
167
-    }
168
-
169
-    /**
170
-     * Check if this path is empty (it does not contain any node).
171
-     * 
172
-     * @return true if this path is empty, false otherwise.
173
-     */
174
-    public boolean isEmpty() {
175
-        return this.origin == null;
176
-    }
177
-
178
-    /**
179
-     * Get the number of <b>nodes</b> in this path.
180
-     * 
181
-     * @return Number of nodes in this path.
182
-     */
183
-    public int size() {
184
-        return isEmpty() ? 0 : 1 + this.arcs.size();
185
-    }
186
-
187
-    /**
188
-     * Check if this path is valid.
189
-     * 
190
-     * A path is valid if any of the following is true:
191
-     * <ul>
192
-     * <li>it is empty;</li>
193
-     * <li>it contains a single node (without arcs);</li>
194
-     * <li>the first arc has for origin the origin of the path and, for two
195
-     * consecutive arcs, the destination of the first one is the origin of the
196
-     * second one.</li>
197
-     * </ul>
198
-     * 
199
-     * @return true if the path is valid, false otherwise.
200
-     * 
201
-     * @deprecated Need to be implemented.
202
-     */
203
-    public boolean isValid() {
204
-        // TODO:
205
-        return false;
206
-    }
207
-
208
-    /**
209
-     * Compute the length of this path (in meters).
210
-     * 
211
-     * @return Total length of the path (in meters).
212
-     * 
213
-     * @deprecated Need to be implemented.
214
-     */
215
-    public float getLength() {
216
-        // TODO:
217
-        return 0;
218
-    }
219
-
220
-    /**
221
-     * Compute the time required to travel this path if moving at the given speed.
222
-     * 
223
-     * @param speed Speed to compute the travel time.
224
-     * 
225
-     * @return Time (in seconds) required to travel this path at the given speed (in
226
-     *         kilometers-per-hour).
227
-     * 
228
-     * @deprecated Need to be implemented.
229
-     */
230
-    public double getTravelTime(double speed) {
231
-        // TODO:
232
-        return 0;
233
-    }
234
-
235
-    /**
236
-     * Compute the time to travel this path if moving at the maximum allowed speed
237
-     * on every arc.
238
-     * 
239
-     * @return Minimum travel time to travel this path (in seconds).
240
-     * 
241
-     * @deprecated Need to be implemented.
242
-     */
243
-    public double getMinimumTravelTime() {
244
-        // TODO:
245
-        return 0;
246
-    }
21
+	/**
22
+	 * Create a new path that goes through the given list of nodes (in order),
23
+	 * choosing the fastest route if multiple are available.
24
+	 * 
25
+	 * @param graph Graph containing the nodes in the list.
26
+	 * @param nodes List of nodes to build the path.
27
+	 * 
28
+	 * @return A path that goes through the given list of nodes.
29
+	 * 
30
+	 * @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
+	 */
34
+	public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes)
35
+			throws IllegalArgumentException {
36
+		int nodeListSize = nodes.size();
37
+
38
+		List<Arc> arcs = new ArrayList<Arc>(nodeListSize);
39
+
40
+		if(nodeListSize > 1) {
41
+			//nodes.size() - 1 because we look at i and i+1
42
+			for(int i=0;i<nodes.size() - 1;i++) {
43
+
44
+				//for each node we try to find fastestPath between i and i+1
45
+				List<Arc> neighbourhood = nodes.get(i).getSuccessors();
46
+
47
+				Arc fastestPath = null;
48
+
49
+				for(Arc nextArc : neighbourhood) {
50
+					if(nextArc.getDestination().compareTo(nodes.get(i+1)) == 0) {
51
+						//here we find a path between i and i+1, now we need to see if it's the fastest one
52
+						if(fastestPath == null) {
53
+							fastestPath = nextArc;
54
+						}
55
+						else if(fastestPath.getMinimumTravelTime() > nextArc.getMinimumTravelTime()) {
56
+							fastestPath = nextArc;
57
+						}
58
+					}
59
+				}
60
+
61
+				if(fastestPath == null) {
62
+					throw new IllegalArgumentException();
63
+				}
64
+
65
+				arcs.add(fastestPath);       	
66
+			}
67
+		}
68
+		else if (nodeListSize == 1) {
69
+			return new Path(graph, nodes.get(0));
70
+		}
71
+		else {
72
+			//nodeListSize == 0
73
+			return new Path(graph);
74
+		}        
75
+
76
+		return new Path(graph, arcs);
77
+	}
78
+
79
+	/**
80
+	 * Create a new path that goes through the given list of nodes (in order),
81
+	 * choosing the shortest route if multiple are available.
82
+	 * 
83
+	 * @param graph Graph containing the nodes in the list.
84
+	 * @param nodes List of nodes to build the path.
85
+	 * 
86
+	 * @return A path that goes through the given list of nodes.
87
+	 * 
88
+	 * @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
89
+	 *         consecutive nodes in the list are not connected in the graph.
90
+	 * 
91
+	 * 
92
+	 */
93
+	public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes)
94
+			throws IllegalArgumentException {
95
+
96
+		int nodeListSize = nodes.size();
97
+
98
+		List<Arc> arcs = new ArrayList<Arc>(nodeListSize);
99
+
100
+		if(nodeListSize > 1) {
101
+			//nodes.size() - 1 because we look at i and i+1
102
+			for(int i=0;i<nodes.size() - 1;i++) {
103
+
104
+				//for each node we try to find shortestPath between i and i+1
105
+				List<Arc> neighbourhood = nodes.get(i).getSuccessors();
106
+
107
+				Arc shortestPath = null;
108
+
109
+				for(Arc nextArc : neighbourhood) {
110
+					if(nextArc.getDestination().compareTo(nodes.get(i+1)) == 0) {
111
+						//here we find a path between i and i+1, now we need to see if it's the shortest one
112
+						if(shortestPath == null) {
113
+							shortestPath = nextArc;
114
+						}
115
+						else if(shortestPath.getLength() > nextArc.getLength()) {
116
+							shortestPath = nextArc;
117
+						}
118
+					}
119
+				}
120
+
121
+				if(shortestPath == null) {
122
+					throw new IllegalArgumentException();
123
+				}
124
+
125
+				arcs.add(shortestPath);       	
126
+			}
127
+		}
128
+		else if (nodeListSize == 1) {
129
+			return new Path(graph, nodes.get(0));
130
+		}
131
+		else {
132
+			//nodeListSize == 0
133
+			return new Path(graph);
134
+		}        
135
+
136
+		return new Path(graph, arcs);
137
+	}
138
+
139
+	/**
140
+	 * Concatenate the given paths.
141
+	 * 
142
+	 * @param paths Array of paths to concatenate.
143
+	 * 
144
+	 * @return Concatenated path.
145
+	 * 
146
+	 * @throws IllegalArgumentException if the paths cannot be concatenated (IDs of
147
+	 *         map do not match, or the end of a path is not the beginning of the
148
+	 *         next).
149
+	 */
150
+	public static Path concatenate(Path... paths) throws IllegalArgumentException {
151
+		if (paths.length == 0) {
152
+			throw new IllegalArgumentException("Cannot concatenate an empty list of paths.");
153
+		}
154
+		final String mapId = paths[0].getGraph().getMapId();
155
+		for (int i = 1; i < paths.length; ++i) {
156
+			if (!paths[i].getGraph().getMapId().equals(mapId)) {
157
+				throw new IllegalArgumentException(
158
+						"Cannot concatenate paths from different graphs.");
159
+			}
160
+		}
161
+		ArrayList<Arc> arcs = new ArrayList<>();
162
+		for (Path path: paths) {
163
+			arcs.addAll(path.getArcs());
164
+		}
165
+		Path path = new Path(paths[0].getGraph(), arcs);
166
+		if (!path.isValid()) {
167
+			throw new IllegalArgumentException(
168
+					"Cannot concatenate paths that do not form a single path.");
169
+		}
170
+		return path;
171
+	}
172
+
173
+	// Graph containing this path.
174
+	private final Graph graph;
175
+
176
+	// Origin of the path
177
+	private final Node origin;
178
+
179
+	// List of arcs in this path.
180
+	private final List<Arc> arcs;
181
+
182
+	/**
183
+	 * Create an empty path corresponding to the given graph.
184
+	 * 
185
+	 * @param graph Graph containing the path.
186
+	 */
187
+	public Path(Graph graph) {
188
+		this.graph = graph;
189
+		this.origin = null;
190
+		this.arcs = new ArrayList<>();
191
+	}
192
+
193
+	/**
194
+	 * Create a new path containing a single node.
195
+	 * 
196
+	 * @param graph Graph containing the path.
197
+	 * @param node Single node of the path.
198
+	 */
199
+	public Path(Graph graph, Node node) {
200
+		this.graph = graph;
201
+		this.origin = node;
202
+		this.arcs = new ArrayList<>();
203
+	}
204
+
205
+	/**
206
+	 * Create a new path with the given list of arcs.
207
+	 * 
208
+	 * @param graph Graph containing the path.
209
+	 * @param arcs Arcs to construct the path.
210
+	 */
211
+	public Path(Graph graph, List<Arc> arcs) {
212
+		this.graph = graph;
213
+		this.arcs = arcs;
214
+		this.origin = arcs.size() > 0 ? arcs.get(0).getOrigin() : null;
215
+	}
216
+
217
+	/**
218
+	 * @return Graph containing the path.
219
+	 */
220
+	public Graph getGraph() {
221
+		return graph;
222
+	}
223
+
224
+	/**
225
+	 * @return First node of the path.
226
+	 */
227
+	public Node getOrigin() {
228
+		return origin;
229
+	}
230
+
231
+	/**
232
+	 * @return Last node of the path.
233
+	 */
234
+	public Node getDestination() {
235
+		return arcs.get(arcs.size() - 1).getDestination();
236
+	}
237
+
238
+	/**
239
+	 * @return List of arcs in the path.
240
+	 */
241
+	public List<Arc> getArcs() {
242
+		return Collections.unmodifiableList(arcs);
243
+	}
244
+
245
+	/**
246
+	 * Check if this path is empty (it does not contain any node).
247
+	 * 
248
+	 * @return true if this path is empty, false otherwise.
249
+	 */
250
+	public boolean isEmpty() {
251
+		return this.origin == null;
252
+	}
253
+
254
+	/**
255
+	 * Get the number of <b>nodes</b> in this path.
256
+	 * 
257
+	 * @return Number of nodes in this path.
258
+	 */
259
+	public int size() {
260
+		return isEmpty() ? 0 : 1 + this.arcs.size();
261
+	}
262
+
263
+	/**
264
+	 * Check if this path is valid.
265
+	 * 
266
+	 * A path is valid if any of the following is true:
267
+	 * <ul>
268
+	 * <li>it is empty;</li>
269
+	 * <li>it contains a single node (without arcs);</li>
270
+	 * <li>the first arc has for origin the origin of the path and, for two
271
+	 * consecutive arcs, the destination of the first one is the origin of the
272
+	 * second one.</li>
273
+	 * </ul>
274
+	 * 
275
+	 * @return true if the path is valid, false otherwise.
276
+	 * 
277
+	 */
278
+	public boolean isValid() {
279
+
280
+		if((this.isEmpty() && arcs.isEmpty()) || this.size() == 1) {
281
+			return true;
282
+		}
283
+
284
+		//the first arc has for origin the origin of the path
285
+		if(this.arcs.get(0).getOrigin() != this.origin) {
286
+			return false;
287
+		}
288
+
289
+		//for two consecutive arcs, the destination of the first one is the origin of the second one
290
+		for(int i = 0 ; i < arcs.size() - 1 ;i++) {
291
+			if(arcs.get(i).getDestination().compareTo(arcs.get(i+1).getOrigin()) != 0) {
292
+				return false;
293
+			}
294
+		}
295
+
296
+		return true;
297
+	}
298
+
299
+	/**
300
+	 * Compute the length of this path (in meters).
301
+	 * 
302
+	 * @return Total length of the path (in meters).
303
+	 * 
304
+	 */
305
+	public float getLength() {
306
+
307
+		float totalLength = 0.0f;
308
+
309
+		for(Arc arc : arcs) {
310
+			totalLength += arc.getLength();
311
+		}
312
+
313
+
314
+		return totalLength;
315
+	}
316
+
317
+	/**
318
+	 * Compute the time required to travel this path if moving at the given speed.
319
+	 * 
320
+	 * @param speed Speed to compute the travel time.
321
+	 * 
322
+	 * @return Time (in seconds) required to travel this path at the given speed (in
323
+	 *         kilometers-per-hour).
324
+	 * 
325
+	 * 
326
+	 */
327
+	public double getTravelTime(double speed) {
328
+		double totalTime = 0.0d;
329
+
330
+		for(Arc arc : arcs) {
331
+			totalTime += arc.getTravelTime(speed);
332
+		}
333
+
334
+
335
+		return totalTime;
336
+	}
337
+
338
+	/**
339
+	 * Compute the time to travel this path if moving at the maximum allowed speed
340
+	 * on every arc.
341
+	 * 
342
+	 * @return Minimum travel time to travel this path (in seconds).
343
+	 * 
344
+	 * 
345
+	 */
346
+	public double getMinimumTravelTime() {
347
+		double totalTime = 0.0d;
348
+
349
+		for(Arc arc : arcs) {
350
+			totalTime += arc.getMinimumTravelTime();
351
+		}
352
+
353
+
354
+		return totalTime;
355
+	}
247 356
 
248 357
 }

Loading…
Cancel
Save