Implement Path methods

This commit is contained in:
Yohan Simard 2020-03-25 16:59:51 +01:00
parent a60676119d
commit bda9d62799

View file

@ -8,7 +8,7 @@ import java.util.List;
* <p> * <p>
* Class representing a path between nodes in a graph. * Class representing a path between nodes in a graph.
* </p> * </p>
* *
* <p> * <p>
* A path is represented as a list of {@link Arc} with an origin and not a list * A path is represented as a list of {@link Arc} with an origin and not a list
* of {@link Node} due to the multi-graph nature (multiple arcs between two * of {@link Node} due to the multi-graph nature (multiple arcs between two
@ -21,52 +21,83 @@ public class Path {
/** /**
* Create a new path that goes through the given list of nodes (in order), * Create a new path that goes through the given list of nodes (in order),
* choosing the fastest route if multiple are available. * choosing the fastest route if multiple are available.
* *
* @param graph Graph containing the nodes in the list. * @param graph Graph containing the nodes in the list.
* @param nodes List of nodes to build the path. * @param nodes List of nodes to build the path.
* *
* @return A path that goes through the given list of nodes. * @return A path that goes through the given list of nodes.
* *
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two * @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
* consecutive nodes in the list are not connected in the graph. * consecutive nodes in the list are not connected in the graph.
*
* @deprecated Need to be implemented.
*/ */
public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes) public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes)
throws IllegalArgumentException { throws IllegalArgumentException {
List<Arc> arcs = new ArrayList<Arc>(); if (nodes.size() == 0)
// TODO: return new Path(graph);
if (nodes.size() == 1)
return new Path(graph, nodes.get(0));
List<Arc> arcs = new ArrayList<>();
for (int i = 1; i < nodes.size(); i++) {
Node node = nodes.get(i);
Node previousNode = nodes.get(i - 1);
List<Arc> successors = previousNode.getSuccessors();
Arc min = null;
for (Arc successor : successors) {
if (successor.getDestination().equals(node) &&
(min == null || successor.getMinimumTravelTime() < min.getMinimumTravelTime()))
min = successor;
}
if (min == null)
throw new IllegalArgumentException("Two consecutive nodes of the argument are not actually connected in the graph");
arcs.add(min);
}
return new Path(graph, arcs); return new Path(graph, arcs);
} }
/** /**
* Create a new path that goes through the given list of nodes (in order), * Create a new path that goes through the given list of nodes (in order),
* choosing the shortest route if multiple are available. * choosing the shortest route if multiple are available.
* *
* @param graph Graph containing the nodes in the list. * @param graph Graph containing the nodes in the list.
* @param nodes List of nodes to build the path. * @param nodes List of nodes to build the path.
* *
* @return A path that goes through the given list of nodes. * @return A path that goes through the given list of nodes.
* *
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two * @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
* consecutive nodes in the list are not connected in the graph. * consecutive nodes in the list are not connected in the graph.
*
* @deprecated Need to be implemented.
*/ */
public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes) public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes)
throws IllegalArgumentException { throws IllegalArgumentException {
List<Arc> arcs = new ArrayList<Arc>(); if (nodes.size() == 0)
// TODO: return new Path(graph);
if (nodes.size() == 1)
return new Path(graph, nodes.get(0));
List<Arc> arcs = new ArrayList<>();
for (int i = 1; i < nodes.size(); i++) {
Node node = nodes.get(i);
Node previousNode = nodes.get(i - 1);
List<Arc> successors = previousNode.getSuccessors();
Arc min = null;
for (Arc successor : successors) {
if (successor.getDestination().equals(node) && (min == null || successor.getLength() < min.getLength()))
min = successor;
}
if (min == null)
throw new IllegalArgumentException("Two consecutive nodes of the argument are not actually connected in the graph");
arcs.add(min);
}
return new Path(graph, arcs); return new Path(graph, arcs);
} }
/** /**
* Concatenate the given paths. * Concatenate the given paths.
* *
* @param paths Array of paths to concatenate. * @param paths Array of paths to concatenate.
* *
* @return Concatenated path. * @return Concatenated path.
* *
* @throws IllegalArgumentException if the paths cannot be concatenated (IDs of * @throws IllegalArgumentException if the paths cannot be concatenated (IDs of
* map do not match, or the end of a path is not the beginning of the * map do not match, or the end of a path is not the beginning of the
* next). * next).
@ -105,7 +136,7 @@ public class Path {
/** /**
* Create an empty path corresponding to the given graph. * Create an empty path corresponding to the given graph.
* *
* @param graph Graph containing the path. * @param graph Graph containing the path.
*/ */
public Path(Graph graph) { public Path(Graph graph) {
@ -116,7 +147,7 @@ public class Path {
/** /**
* Create a new path containing a single node. * Create a new path containing a single node.
* *
* @param graph Graph containing the path. * @param graph Graph containing the path.
* @param node Single node of the path. * @param node Single node of the path.
*/ */
@ -128,7 +159,7 @@ public class Path {
/** /**
* Create a new path with the given list of arcs. * Create a new path with the given list of arcs.
* *
* @param graph Graph containing the path. * @param graph Graph containing the path.
* @param arcs Arcs to construct the path. * @param arcs Arcs to construct the path.
*/ */
@ -168,7 +199,7 @@ public class Path {
/** /**
* Check if this path is empty (it does not contain any node). * Check if this path is empty (it does not contain any node).
* *
* @return true if this path is empty, false otherwise. * @return true if this path is empty, false otherwise.
*/ */
public boolean isEmpty() { public boolean isEmpty() {
@ -177,7 +208,7 @@ public class Path {
/** /**
* Get the number of <b>nodes</b> in this path. * Get the number of <b>nodes</b> in this path.
* *
* @return Number of nodes in this path. * @return Number of nodes in this path.
*/ */
public int size() { public int size() {
@ -186,7 +217,7 @@ public class Path {
/** /**
* Check if this path is valid. * Check if this path is valid.
* *
* A path is valid if any of the following is true: * A path is valid if any of the following is true:
* <ul> * <ul>
* <li>it is empty;</li> * <li>it is empty;</li>
@ -195,54 +226,66 @@ public class Path {
* consecutive arcs, the destination of the first one is the origin of the * consecutive arcs, the destination of the first one is the origin of the
* second one.</li> * second one.</li>
* </ul> * </ul>
* *
* @return true if the path is valid, false otherwise. * @return true if the path is valid, false otherwise.
*
* @deprecated Need to be implemented.
*/ */
public boolean isValid() { public boolean isValid() {
// TODO: if (arcs.size() == 0) {
return false; return true;
}
if (arcs.get(0).getOrigin() != origin)
return false;
else if (arcs.size() == 1)
return true;
for (int i = 1; i < arcs.size(); i++) {
if (arcs.get(i-1).getDestination() != arcs.get(i).getOrigin())
return false;
}
return true;
} }
/** /**
* Compute the length of this path (in meters). * Compute the length of this path (in meters).
* *
* @return Total length of the path (in meters). * @return Total length of the path (in meters).
*
* @deprecated Need to be implemented.
*/ */
public float getLength() { public float getLength() {
// TODO: float length = 0;
return 0; for (Arc arc: arcs) {
length += arc.getLength();
}
return length;
} }
/** /**
* Compute the time required to travel this path if moving at the given speed. * Compute the time required to travel this path if moving at the given speed.
* *
* @param speed Speed to compute the travel time. * @param speed Speed to compute the travel time.
* *
* @return Time (in seconds) required to travel this path at the given speed (in * @return Time (in seconds) required to travel this path at the given speed (in
* kilometers-per-hour). * kilometers-per-hour).
*
* @deprecated Need to be implemented.
*/ */
public double getTravelTime(double speed) { public double getTravelTime(double speed) {
// TODO: double time = 0;
return 0; for (Arc arc : arcs) {
time += arc.getTravelTime(speed);
}
return time;
} }
/** /**
* Compute the time to travel this path if moving at the maximum allowed speed * Compute the time to travel this path if moving at the maximum allowed speed
* on every arc. * on every arc.
* *
* @return Minimum travel time to travel this path (in seconds). * @return Minimum travel time to travel this path (in seconds).
*
* @deprecated Need to be implemented.
*/ */
public double getMinimumTravelTime() { public double getMinimumTravelTime() {
// TODO: double time = 0;
return 0; for (Arc arc : arcs) {
time += arc.getMinimumTravelTime();
}
return time;
} }
} }