Implemented step 2

Now createFastestPath and createShortestPath are created. Step 2
finished
This commit is contained in:
Adrien Barbanson 2021-04-02 09:05:16 +02:00
parent 9a32897b95
commit 8eeb871b1a

View file

@ -30,12 +30,49 @@ 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>(); int nodeListSize = nodes.size();
// TODO:
List<Arc> arcs = new ArrayList<Arc>(nodeListSize);
if(nodeListSize > 1) {
//nodes.size() - 1 because we look at i and i+1
for(int i=0;i<nodes.size() - 1;i++) {
//for each node we try to find fastestPath between i and i+1
List<Arc> neighbourhood = nodes.get(i).getSuccessors();
Arc fastestPath = null;
for(Arc nextArc : neighbourhood) {
if(nextArc.getDestination().compareTo(nodes.get(i+1)) == 0) {
//here we find a path between i and i+1, now we need to see if it's the fastest one
if(fastestPath == null) {
fastestPath = nextArc;
}
else if(fastestPath.getMinimumTravelTime() > nextArc.getMinimumTravelTime()) {
fastestPath = nextArc;
}
}
}
if(fastestPath == null) {
throw new IllegalArgumentException();
}
arcs.add(fastestPath);
}
}
else if (nodeListSize == 1) {
return new Path(graph, nodes.get(0));
}
else {
//nodeListSize == 0
return new Path(graph);
}
return new Path(graph, arcs); return new Path(graph, arcs);
} }
@ -51,12 +88,51 @@ 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>();
// TODO: int nodeListSize = nodes.size();
List<Arc> arcs = new ArrayList<Arc>(nodeListSize);
if(nodeListSize > 1) {
//nodes.size() - 1 because we look at i and i+1
for(int i=0;i<nodes.size() - 1;i++) {
//for each node we try to find shortestPath between i and i+1
List<Arc> neighbourhood = nodes.get(i).getSuccessors();
Arc shortestPath = null;
for(Arc nextArc : neighbourhood) {
if(nextArc.getDestination().compareTo(nodes.get(i+1)) == 0) {
//here we find a path between i and i+1, now we need to see if it's the shortest one
if(shortestPath == null) {
shortestPath = nextArc;
}
else if(shortestPath.getLength() > nextArc.getLength()) {
shortestPath = nextArc;
}
}
}
if(shortestPath == null) {
throw new IllegalArgumentException();
}
arcs.add(shortestPath);
}
}
else if (nodeListSize == 1) {
return new Path(graph, nodes.get(0));
}
else {
//nodeListSize == 0
return new Path(graph);
}
return new Path(graph, arcs); return new Path(graph, arcs);
} }
@ -198,23 +274,44 @@ public class Path {
* *
* @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((this.isEmpty() && arcs.isEmpty()) || this.size() == 1) {
return true;
}
//the first arc has for origin the origin of the path
if(this.arcs.get(0).getOrigin() != this.origin) {
return false; return false;
} }
//for two consecutive arcs, the destination of the first one is the origin of the second one
for(int i = 0 ; i < arcs.size() - 1 ;i++) {
if(arcs.get(i).getDestination().compareTo(arcs.get(i+1).getOrigin()) != 0) {
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:
return 0; float totalLength = 0.0f;
for(Arc arc : arcs) {
totalLength += arc.getLength();
}
return totalLength;
} }
/** /**
@ -225,11 +322,17 @@ 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 totalTime = 0.0d;
return 0;
for(Arc arc : arcs) {
totalTime += arc.getTravelTime(speed);
}
return totalTime;
} }
/** /**
@ -238,11 +341,17 @@ public class Path {
* *
* @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 totalTime = 0.0d;
return 0;
for(Arc arc : arcs) {
totalTime += arc.getMinimumTravelTime();
}
return totalTime;
} }
} }