From b4e05a8889c0bbba57a44e9b51312eed250f66da Mon Sep 17 00:00:00 2001 From: brunetto Date: Wed, 22 Mar 2023 10:08:44 +0100 Subject: [PATCH] =?UTF-8?q?D=C3=A9finition=20des=20m=C3=A9thodes=20createF?= =?UTF-8?q?astestPathFromNodes=20et=20createShortestPathFromNodes=20pour?= =?UTF-8?q?=20classe=20Path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/org/insa/graphs/model/Path.java | 83 +++++++++++++++++-- 1 file changed, 74 insertions(+), 9 deletions(-) diff --git a/be-graphes-model/src/main/java/org/insa/graphs/model/Path.java b/be-graphes-model/src/main/java/org/insa/graphs/model/Path.java index 6ebdb73..6bcbdea 100644 --- a/be-graphes-model/src/main/java/org/insa/graphs/model/Path.java +++ b/be-graphes-model/src/main/java/org/insa/graphs/model/Path.java @@ -20,8 +20,8 @@ public class Path { /** * 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 nodes List of nodes to build the path. * @@ -35,8 +35,43 @@ public class Path { public static Path createFastestPathFromNodes(Graph graph, List nodes) throws IllegalArgumentException { List arcs = new ArrayList(); - // 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); + } + 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 nodes) throws IllegalArgumentException { List arcs = new ArrayList(); - // 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); + } + 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. */ public float getLength() { - // TODO: return 0; } @@ -225,11 +293,9 @@ public class Path { * @return Time (in seconds) required to travel this path at the given speed (in * kilometers-per-hour). * - * @deprecated Need to be implemented. */ public double getTravelTime(double speed) { - // TODO: - return 0; + return (this.getLength() / speed)*3.6f; } /** @@ -241,7 +307,6 @@ public class Path { * @deprecated Need to be implemented. */ public double getMinimumTravelTime() { - // TODO: return 0; }