Implement Path methods
This commit is contained in:
parent
a60676119d
commit
bda9d62799
1 changed files with 88 additions and 45 deletions
|
@ -29,13 +29,29 @@ public class Path {
|
||||||
*
|
*
|
||||||
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,13 +66,28 @@ public class Path {
|
||||||
*
|
*
|
||||||
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,24 +228,34 @@ public class Path {
|
||||||
* </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 true;
|
||||||
|
}
|
||||||
|
if (arcs.get(0).getOrigin() != origin)
|
||||||
return false;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -224,12 +265,13 @@ public class Path {
|
||||||
*
|
*
|
||||||
* @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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -237,12 +279,13 @@ public class Path {
|
||||||
* 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue