ajout de createFastestPathFromNodes

This commit is contained in:
Bezza Younes 2025-04-11 10:17:56 +02:00
parent 66462ee535
commit 40f3d3f2ce

View file

@ -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,7 +261,6 @@ 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
@ -234,7 +276,6 @@ 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
@ -249,7 +290,6 @@ 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;