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>
* Class representing a path between nodes in a graph.
* </p>
*
*
* <p>
* 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
@ -21,52 +21,83 @@ public class Path {
/**
* Create a new path that goes through the given list of nodes (in order),
* choosing the fastest route if multiple are available.
*
*
* @param graph Graph containing the nodes in the list.
* @param nodes List of nodes to build the path.
*
*
* @return A path that goes through the given list of nodes.
*
*
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
* 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)
throws IllegalArgumentException {
List<Arc> arcs = new ArrayList<Arc>();
// TODO:
if (nodes.size() == 0)
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);
}
/**
* Create a new path that goes through the given list of nodes (in order),
* choosing the shortest route if multiple are available.
*
*
* @param graph Graph containing the nodes in the list.
* @param nodes List of nodes to build the path.
*
*
* @return A path that goes through the given list of nodes.
*
*
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
* 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)
throws IllegalArgumentException {
List<Arc> arcs = new ArrayList<Arc>();
// TODO:
if (nodes.size() == 0)
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);
}
/**
* Concatenate the given paths.
*
*
* @param paths Array of paths to concatenate.
*
*
* @return Concatenated path.
*
*
* @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
* next).
@ -105,7 +136,7 @@ public class Path {
/**
* Create an empty path corresponding to the given graph.
*
*
* @param graph Graph containing the path.
*/
public Path(Graph graph) {
@ -116,7 +147,7 @@ public class Path {
/**
* Create a new path containing a single node.
*
*
* @param graph Graph containing 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.
*
*
* @param graph Graph containing 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).
*
*
* @return true if this path is empty, false otherwise.
*/
public boolean isEmpty() {
@ -177,7 +208,7 @@ public class Path {
/**
* Get the number of <b>nodes</b> in this path.
*
*
* @return Number of nodes in this path.
*/
public int size() {
@ -186,7 +217,7 @@ public class Path {
/**
* Check if this path is valid.
*
*
* A path is valid if any of the following is true:
* <ul>
* <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
* second one.</li>
* </ul>
*
*
* @return true if the path is valid, false otherwise.
*
* @deprecated Need to be implemented.
*/
public boolean isValid() {
// TODO:
return false;
if (arcs.size() == 0) {
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).
*
*
* @return Total length of the path (in meters).
*
* @deprecated Need to be implemented.
*/
public float getLength() {
// TODO:
return 0;
float length = 0;
for (Arc arc: arcs) {
length += arc.getLength();
}
return length;
}
/**
* Compute the time required to travel this path if moving at the given speed.
*
*
* @param speed Speed to compute the travel time.
*
*
* @return Time (in seconds) required to travel this path at the given speed (in
* kilometers-per-hour).
*
* @deprecated Need to be implemented.
*/
public double getTravelTime(double speed) {
// TODO:
return 0;
double time = 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
* on every arc.
*
*
* @return Minimum travel time to travel this path (in seconds).
*
* @deprecated Need to be implemented.
*/
public double getMinimumTravelTime() {
// TODO:
return 0;
double time = 0;
for (Arc arc : arcs) {
time += arc.getMinimumTravelTime();
}
return time;
}
}