ajout de createFastestPathFromNodes
This commit is contained in:
parent
66462ee535
commit
40f3d3f2ce
1 changed files with 56 additions and 16 deletions
|
@ -24,15 +24,58 @@ public class Path {
|
||||||
* @return A path that goes through the given list of nodes.
|
* @return A path that goes through the given list of nodes.
|
||||||
* @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)
|
|
||||||
throws IllegalArgumentException {
|
|
||||||
List<Arc> arcs = new ArrayList<Arc>();
|
|
||||||
// TODO : faut faire en fct du maxspeed ds road information
|
// TODO : faut faire en fct du maxspeed ds road information
|
||||||
|
|
||||||
return new Path(graph, arcs);
|
|
||||||
|
public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes)
|
||||||
|
throws IllegalArgumentException {
|
||||||
|
|
||||||
|
if (nodes == null || nodes.isEmpty())
|
||||||
|
return new Path(graph);
|
||||||
|
if (nodes.size() == 1)
|
||||||
|
return new Path(graph, nodes.get(0));
|
||||||
|
|
||||||
|
ArrayList<Arc> finalList = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int i = 0; i < nodes.size() - 1; i++) {
|
||||||
|
Node node = nodes.get(i);
|
||||||
|
Node next = nodes.get(i + 1);
|
||||||
|
|
||||||
|
List<Arc> successeurs = node.getSuccessors();
|
||||||
|
ArrayList<Arc> successeursSelect = new ArrayList<>();
|
||||||
|
|
||||||
|
for (Arc succ : successeurs) {
|
||||||
|
if (succ.getDestination().equals(next)) {
|
||||||
|
successeursSelect.add(succ);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (successeursSelect.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Pas d'arc entre " + node.getId() + " et " + next.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc arcChoisi = successeursSelect.get(0);
|
||||||
|
double minTime = arcChoisi.getMinimumTravelTime();
|
||||||
|
|
||||||
|
for (Arc succSelect : successeursSelect) {
|
||||||
|
double time = succSelect.getMinimumTravelTime();
|
||||||
|
if (time < minTime) {
|
||||||
|
arcChoisi = succSelect;
|
||||||
|
minTime = time;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
finalList.add(arcChoisi);
|
||||||
|
}
|
||||||
|
// pour chaque point (node) avec for on regarde tous les successeurs, il y a
|
||||||
|
// plusieurs chemins entre A et B , on les stocke dans une liste puis on
|
||||||
|
// selectionne le min
|
||||||
|
// puis on passe au prochain point
|
||||||
|
return new Path(graph, finalList);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new path that goes through the given list of nodes (in order), choosing
|
* Create a new path that goes through the given list of nodes (in order), choosing
|
||||||
|
@ -218,12 +261,11 @@ public class Path {
|
||||||
* 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).
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public float getLength() {
|
public float getLength() {
|
||||||
float pathLength=0f; // sinon error si on met O.O
|
float pathLength = 0f; // sinon error si on met O.O
|
||||||
for (Arc element : this.arcs) {
|
for (Arc element : this.arcs) {
|
||||||
pathLength=pathLength+((float) element.getLength());
|
pathLength = pathLength + ((float) element.getLength());
|
||||||
}
|
}
|
||||||
return pathLength;
|
return pathLength;
|
||||||
}
|
}
|
||||||
|
@ -234,12 +276,11 @@ public class Path {
|
||||||
* @param speed Speed to compute the travel time.
|
* @param speed Speed to compute the travel time.
|
||||||
* @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).
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public double getTravelTime(double speed) {
|
public double getTravelTime(double speed) {
|
||||||
double travelTime=0; // sinon error si on met O.O
|
double travelTime = 0; // sinon error si on met O.O
|
||||||
for (Arc element : this.arcs) {
|
for (Arc element : this.arcs) {
|
||||||
travelTime=travelTime+element.getTravelTime(speed);
|
travelTime = travelTime + element.getTravelTime(speed);
|
||||||
}
|
}
|
||||||
return travelTime;
|
return travelTime;
|
||||||
}
|
}
|
||||||
|
@ -249,12 +290,11 @@ public class Path {
|
||||||
* every arc.
|
* every arc.
|
||||||
*
|
*
|
||||||
* @return Minimum travel time to travel this path (in seconds).
|
* @return Minimum travel time to travel this path (in seconds).
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public double getMinimumTravelTime() {
|
public double getMinimumTravelTime() {
|
||||||
double minimumTravelTime=0;
|
double minimumTravelTime = 0;
|
||||||
for (Arc element : this.arcs) {
|
for (Arc element : this.arcs) {
|
||||||
minimumTravelTime=minimumTravelTime+element.getMinimumTravelTime();
|
minimumTravelTime = minimumTravelTime + element.getMinimumTravelTime();
|
||||||
}
|
}
|
||||||
return minimumTravelTime;
|
return minimumTravelTime;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue