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),
|
* 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 graph Graph containing the nodes in the list.
|
||||||
* @param nodes List of nodes to build the path.
|
* @param nodes List of nodes to build the path.
|
||||||
*
|
*
|
||||||
|
@ -35,8 +35,43 @@ public class Path {
|
||||||
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>();
|
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);
|
return new Path(graph, arcs);
|
||||||
|
}
|
||||||
|
else if(nodes.size()==1)
|
||||||
|
{
|
||||||
|
return new Path(graph, nodes.get(0));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new Path(graph);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -56,8 +91,42 @@ public class Path {
|
||||||
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>();
|
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);
|
return new Path(graph, arcs);
|
||||||
|
}
|
||||||
|
else if(nodes.size()==1)
|
||||||
|
{
|
||||||
|
return new Path(graph, nodes.get(0));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new Path(graph);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -213,7 +282,6 @@ public class Path {
|
||||||
* @deprecated Need to be implemented.
|
* @deprecated Need to be implemented.
|
||||||
*/
|
*/
|
||||||
public float getLength() {
|
public float getLength() {
|
||||||
// TODO:
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -225,11 +293,9 @@ 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:
|
return (this.getLength() / speed)*3.6f;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -241,7 +307,6 @@ public class Path {
|
||||||
* @deprecated Need to be implemented.
|
* @deprecated Need to be implemented.
|
||||||
*/
|
*/
|
||||||
public double getMinimumTravelTime() {
|
public double getMinimumTravelTime() {
|
||||||
// TODO:
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue