Dépôt du be graphe
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.

Path.java 8.5KB

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