ajout de createShortestPathFromNodes

This commit is contained in:
Matteo Sabben 2025-04-11 10:35:56 +02:00
parent 40f3d3f2ce
commit 86704ff601

View file

@ -1,6 +1,8 @@
package org.insa.graphs.model; package org.insa.graphs.model;
import java.awt.RenderingHints; // import java.awt.RenderingHints; commenté parce qu'il soulevait une erreur (doesn't
// exist)
import java.io.ObjectInputStream;
import java.util.*; import java.util.*;
/** /**
@ -25,7 +27,7 @@ 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.
*/ */
// TODO : faut faire en fct du maxspeed ds road information //
public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes) public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes)
@ -86,16 +88,87 @@ 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 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> arcsFinaux = new ArrayList<Arc>();
// TODO: en fonction des mètres
Boolean fini = false;
return new Path(graph, arcs); if (nodes.size() == 1) {
System.out.println("Attention liste avec un seul node\n");
return new Path(graph, nodes.get(0));
}
else {
for (Node nodeE : nodes) {
if (nodes.indexOf(nodeE) == nodes.size() - 1) { // on ne regarde pas le
// dernier node qui est
// la
// destination
} // sinon on fait notre recherche
else if (nodeE.hasSuccessors()) {
List<Arc> temporaryArcs = nodeE.getSuccessors();
List<Arc> temporaryCorrespondingArc = new ArrayList<>();
for (Arc arcE : temporaryArcs) {
if (arcE.getDestination()
.equals(nodes.get(nodes.indexOf(nodeE) + 1))) { // si
// la
// destination
// de
// l'arc
// est
// égal
// au
// prochain
// node
temporaryCorrespondingArc.add(arcE);// ajouter cet arc dans
// la
// liste
// temporaryCorrespondingArcs
}
}
// la liste temporaryCorrespondingArc devrait être remplie
if (temporaryCorrespondingArc.isEmpty()) { // si elle est vide les 2
// nodes ne sont pas
// connectés
throw new IllegalArgumentException(
"les nodes de la liste ne sont pas connectés \n");
}
Arc ArcPlusCourt = temporaryCorrespondingArc.get(0);
for (Arc ArcTemporaire : temporaryCorrespondingArc) {// comparer ts
// les
// arcs de la
// liste
// et prendre
// le
// plus rapide
if (ArcTemporaire.getLength() < ArcPlusCourt.getLength()) {
ArcPlusCourt = ArcTemporaire;
}
}
arcsFinaux.add(ArcPlusCourt);// l'ajouter dans la liste finale qui
// est
// arcsFinaux
}
else if (!nodeE.hasSuccessors()) {
System.out.println("node has no successors \n");
}
else {
System.out.println(
"on ne devrait pas pouvoir être ici gros problème\n");
}
}
return new Path(graph, arcsFinaux);
}
} }
/** /**
@ -263,9 +336,9 @@ public class Path {
* @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;
} }