Browse Source

Methode fastestpath

Auriane Lartigue 4 years ago
parent
commit
0fcee12cd3
1 changed files with 32 additions and 4 deletions
  1. 32
    4
      be-graphes-model/src/main/java/org/insa/graphs/model/Path.java

+ 32
- 4
be-graphes-model/src/main/java/org/insa/graphs/model/Path.java View File

@@ -30,13 +30,41 @@ public class Path {
30 30
      * @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
31 31
      *         consecutive nodes in the list are not connected in the graph.
32 32
      * 
33
-     * @deprecated Need to be implemented.
33
+     *  Need to be implemented.
34 34
      */
35 35
     public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes)
36 36
             throws IllegalArgumentException {
37
-        List<Arc> arcs = new ArrayList<Arc>();
38
-        // TODO:
39
-        return new Path(graph, arcs);
37
+    	List<Arc> arcs = new ArrayList<Arc>();
38
+    	if (nodes.size()==1){
39
+        	return new Path(graph, nodes.get(0));
40
+        }
41
+        else {	
42
+	        for (int i = 0 ; i < nodes.size()-1; i++) {	
43
+	            double min = Double.POSITIVE_INFINITY;
44
+	            boolean found = false;
45
+	           
46
+		         if (nodes.get(i).getSuccessors().size() >= 1) {
47
+		        	Arc min_arc = null; 
48
+		        	for(Arc a : nodes.get(i).getSuccessors()) {
49
+		        		if( a.getDestination()==nodes.get(i+1) ) {
50
+		        		
51
+		        			if( a.getMinimumTravelTime() < min) {
52
+		        				min_arc = a ; // choose fastest route
53
+		        				min =  a.getMinimumTravelTime() ; 
54
+		        			}		
55
+		        			found = true;
56
+		        		}
57
+		        	}
58
+		        	
59
+		        	if (!found) {
60
+		                throw new IllegalArgumentException("two consecutive nodes in the list are not connected in the graph.");
61
+		            }
62
+		        	arcs.add( min_arc );
63
+	        	}
64
+		       
65
+	        }
66
+	        return new Path(graph, arcs);
67
+        }
40 68
     }
41 69
 
42 70
     /**

Loading…
Cancel
Save