No Description
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 9.5KB

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