Version Fonctionnelle : Ajout fonctions createPathsFromNodes PATH A JOUR
This commit is contained in:
parent
3303f2400c
commit
241ff4901e
1 changed files with 66 additions and 7 deletions
|
@ -3,6 +3,7 @@ package org.insa.graphs.model;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -29,13 +30,42 @@ 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.
|
||||||
*
|
|
||||||
* @deprecated Need to be implemented.
|
|
||||||
*/
|
*/
|
||||||
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() == 0) {
|
||||||
|
return new Path(graph);
|
||||||
|
} else if (nodes.size() == 1) {
|
||||||
|
return new Path(graph, nodes.get(0));
|
||||||
|
} else {
|
||||||
|
Iterator<Node> iterateur= nodes.iterator();
|
||||||
|
Node nodeOrigin;
|
||||||
|
Node nodeDest = iterateur.next();
|
||||||
|
|
||||||
|
while (iterateur.hasNext()) {
|
||||||
|
|
||||||
|
nodeOrigin = nodeDest;
|
||||||
|
nodeDest = iterateur.next();
|
||||||
|
|
||||||
|
List<Arc> successeurs = nodeOrigin.getSuccessors();
|
||||||
|
|
||||||
|
Arc shortestArc = null;
|
||||||
|
double minTravelTime = Float.MAX_VALUE;
|
||||||
|
for (Arc arc : successeurs) {
|
||||||
|
if (nodeDest.equals(arc.getDestination()) && (arc.getMinimumTravelTime() < minTravelTime)) {
|
||||||
|
minTravelTime = arc.getMinimumTravelTime();
|
||||||
|
shortestArc = arc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shortestArc == null) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
} else {
|
||||||
|
arcs.add(shortestArc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return new Path(graph, arcs);
|
return new Path(graph, arcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,14 +80,43 @@ 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.
|
||||||
*
|
|
||||||
* @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> arcs = new ArrayList<Arc>();
|
||||||
// TODO:
|
if (nodes.size() == 0) {
|
||||||
return new Path(graph, arcs);
|
return new Path(graph);
|
||||||
|
} else if (nodes.size() == 1) {
|
||||||
|
return new Path(graph, nodes.get(0));
|
||||||
|
} else {
|
||||||
|
Iterator<Node> iterateur= nodes.iterator();
|
||||||
|
Node nodeOrigin;
|
||||||
|
Node nodeDest = iterateur.next();
|
||||||
|
|
||||||
|
while (iterateur.hasNext()) {
|
||||||
|
|
||||||
|
nodeOrigin = nodeDest;
|
||||||
|
nodeDest = iterateur.next();
|
||||||
|
|
||||||
|
List<Arc> successeurs = nodeOrigin.getSuccessors();
|
||||||
|
|
||||||
|
Arc shortestArc = null;
|
||||||
|
float minLength = Float.MAX_VALUE;
|
||||||
|
for (Arc arc : successeurs) {
|
||||||
|
if (nodeDest.equals(arc.getDestination()) && (arc.getLength() < minLength)) {
|
||||||
|
minLength = arc.getLength();
|
||||||
|
shortestArc = arc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shortestArc == null) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
} else {
|
||||||
|
arcs.add(shortestArc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Path(graph, arcs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue