Définition des méthodes createFastestPathFromNodes et createShortestPathFromNodes pour classe Path
This commit is contained in:
parent
b4f4b6665f
commit
b4e05a8889
1 changed files with 74 additions and 9 deletions
|
@ -20,8 +20,8 @@ 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.
|
||||
*
|
||||
* choosing the fastest route
|
||||
return new Path(graph, arcs);
|
||||
* @param graph Graph containing the nodes in the list.
|
||||
* @param nodes List of nodes to build the path.
|
||||
*
|
||||
|
@ -35,9 +35,44 @@ public class Path {
|
|||
public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes)
|
||||
throws IllegalArgumentException {
|
||||
List<Arc> arcs = new ArrayList<Arc>();
|
||||
// TODO:
|
||||
if(nodes.size()>1)
|
||||
{
|
||||
for(int i = 0; i< nodes.size()-1; i++)
|
||||
{
|
||||
double fastestPath = -1;
|
||||
for(Arc arc : nodes.get(i).getSuccessors())
|
||||
{
|
||||
if(arc.getDestination().equals(nodes.get(i+1)))
|
||||
{
|
||||
if(fastestPath == -1)
|
||||
{
|
||||
fastestPath = arc.getMinimumTravelTime();
|
||||
arcs.add(arc);
|
||||
}
|
||||
else if (fastestPath > arc.getMinimumTravelTime())
|
||||
{
|
||||
fastestPath = arc.getMinimumTravelTime();
|
||||
arcs.set(arcs.size()-1, arc);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(fastestPath == -1)
|
||||
{
|
||||
throw(new IllegalArgumentException());
|
||||
}
|
||||
}
|
||||
return new Path(graph, arcs);
|
||||
}
|
||||
else if(nodes.size()==1)
|
||||
{
|
||||
return new Path(graph, nodes.get(0));
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Path(graph);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new path that goes through the given list of nodes (in order),
|
||||
|
@ -56,9 +91,43 @@ public class Path {
|
|||
public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes)
|
||||
throws IllegalArgumentException {
|
||||
List<Arc> arcs = new ArrayList<Arc>();
|
||||
// TODO:
|
||||
if(nodes.size()>1)
|
||||
{
|
||||
for(int i = 0; i< nodes.size()-1; i++)
|
||||
{
|
||||
double shortestPath = -1;
|
||||
for(Arc arc : nodes.get(i).getSuccessors())
|
||||
{
|
||||
if(arc.getDestination().equals(nodes.get(i+1)))
|
||||
{
|
||||
if(shortestPath == -1)
|
||||
{
|
||||
shortestPath = arc.getLength();
|
||||
arcs.add(arc);
|
||||
}
|
||||
else if (shortestPath > arc.getLength())
|
||||
{
|
||||
shortestPath = arc.getLength();
|
||||
arcs.set(arcs.size()-1, arc);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(shortestPath == -1)
|
||||
{
|
||||
throw(new IllegalArgumentException());
|
||||
}
|
||||
}
|
||||
return new Path(graph, arcs);
|
||||
}
|
||||
else if(nodes.size()==1)
|
||||
{
|
||||
return new Path(graph, nodes.get(0));
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Path(graph);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenate the given paths.
|
||||
|
@ -213,7 +282,6 @@ public class Path {
|
|||
* @deprecated Need to be implemented.
|
||||
*/
|
||||
public float getLength() {
|
||||
// TODO:
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -225,11 +293,9 @@ public class Path {
|
|||
* @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;
|
||||
return (this.getLength() / speed)*3.6f;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -241,7 +307,6 @@ public class Path {
|
|||
* @deprecated Need to be implemented.
|
||||
*/
|
||||
public double getMinimumTravelTime() {
|
||||
// TODO:
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue