Implemented all functions to pass all tests

This commit is contained in:
Arnaud Vergnet 2020-03-22 19:24:52 +01:00
parent 52df139bd8
commit fa236a612f

View file

@ -14,7 +14,6 @@ import java.util.List;
* of {@link Node} due to the multi-graph nature (multiple arcs between two
* nodes) of the considered graphs.
* </p>
*
*/
public class Path {
@ -24,19 +23,13 @@ public class Path {
*
* @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:
return new Path(graph, arcs);
return Path.createSmallestPath(graph, nodes, false);
}
/**
@ -45,28 +38,72 @@ public class Path {
*
* @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:
return Path.createSmallestPath(graph, nodes, true);
}
/**
* Create a new path that goes through the given list of nodes (in order),
* choosing the shortest or fastest route if multiple are available.
*
* @param graph Graph containing the nodes in the list.
* @param nodes List of nodes to build the path.
* @param isLength Should we search the shortest path (true) or the fastest path (false)
* @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.
*/
private static Path createSmallestPath(Graph graph, List<Node> nodes, boolean isLength) {
List<Arc> arcs = new ArrayList<>();
if (nodes.size() != 0 && nodes.size() != 1) {
for (int i = 0; i < (nodes.size() - 1); i++) {
Arc shortest = Path.getSmallestArc(
nodes.get(i).getSuccessors(), nodes.get(i + 1), isLength
);
arcs.add(shortest);
}
} else if (nodes.size() == 1)
return new Path(graph, nodes.get(0));
return new Path(graph, arcs);
}
/**
* Gets the smallest arc in the given list.
* If an arc destination does not match the given node, it is ignored.
*
* @param arcs The arcs list to search in
* @param nextNode The next node to match the arc destination
* @param isLength Should we search the shortest arc (true) or the fastest arc (false)
* @return The smallest arc to nextNode
* @throws IllegalArgumentException If no node is found
*/
private static Arc getSmallestArc(List<Arc> arcs, Node nextNode, boolean isLength) {
Arc shortestArc = null;
double smallestComparator = -1;
for (Arc arc : arcs) {
if (nextNode.equals(arc.getDestination())) {
double comparator = isLength ? arc.getLength() : arc.getMinimumTravelTime();
if (comparator < smallestComparator || smallestComparator < 0) {
smallestComparator = comparator;
shortestArc = arc;
}
}
}
if (shortestArc == null)
throw new IllegalArgumentException();
return shortestArc;
}
/**
* 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).
@ -186,7 +223,7 @@ public class Path {
/**
* Check if this path is valid.
*
* <p>
* A path is valid if any of the following is true:
* <ul>
* <li>it is empty;</li>
@ -197,8 +234,6 @@ public class Path {
* </ul>
*
* @return true if the path is valid, false otherwise.
*
* @deprecated Need to be implemented.
*/
public boolean isValid() {
boolean valid = isEmpty() || (this.arcs.size() == 0 && this.origin != null);
@ -216,7 +251,6 @@ public class Path {
* Compute the length of this path (in meters).
*
* @return Total length of the path (in meters).
*
*/
public float getLength() {
float length = 0;
@ -230,10 +264,8 @@ public class Path {
* 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).
*
*/
public double getTravelTime(double speed) {
float time = 0;
@ -248,7 +280,6 @@ public class Path {
* on every arc.
*
* @return Minimum travel time to travel this path (in seconds).
*
*/
public double getMinimumTravelTime() {
float time = 0;