Browse Source

Amélioration des tests existants, ajout tests A* et tests d'optimalité

Cavailles Kevin 3 years ago
parent
commit
568f779be7

+ 11
- 11
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java View File

@@ -22,14 +22,7 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
22 22
     
23 23
    @Override
24 24
     protected Label createLabel(ShortestPathData data, Arc pere, Label noeudPrecedent) {
25
-    	if(pere == null && noeudPrecedent == null) {
26
-    		return new LabelStar(data.getOrigin(),
27
-					0,
28
-					Point.distance(data.getOrigin().getPoint(), data.getDestination().getPoint() ) ,
29
-					null);
30
-    	}
31
-    	
32
-    	
25
+    
33 26
     	double coutEstime = 0;
34 27
     	
35 28
     	
@@ -67,8 +60,15 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
67 60
 				noeudPrecedent.getCost() + data.getCost(pere),
68 61
 				coutEstime,
69 62
 				pere);
70
-    	
71
-		
72
-		
73 63
 	}
64
+   
65
+   @Override
66
+   protected Label createLabelOrigin(ShortestPathData data) {
67
+	  
68
+	   return new LabelStar(data.getOrigin(),
69
+				0,
70
+				Point.distance(data.getOrigin().getPoint(), data.getDestination().getPoint() ) ,
71
+				null);
72
+	   
73
+   }
74 74
 }

+ 7
- 8
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java View File

@@ -31,7 +31,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
31 31
 		Label labels[] = new Label[graph.getNodes().size()];
32 32
 		Arrays.fill(labels, null);
33 33
 		
34
-		Label origine = createLabel(data, null, null);
34
+		Label origine = createLabelOrigin(data);
35 35
 		labels[data.getOrigin().getId()] = origine;
36 36
 		tas.insert(origine);
37 37
 		
@@ -135,17 +135,16 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
135 135
 			solution = new ShortestPathSolution(data, Status.INFEASIBLE);
136 136
 		}
137 137
 			
138
-		System.out.println("nb erreurs  = "+cptErreurs);
138
+//		System.out.println("nb erreurs  = "+cptErreurs);
139 139
 		return solution;
140 140
 	}
141 141
 	
142 142
 	protected Label createLabel(ShortestPathData data, Arc pere, Label noeudPrecedent) {
143
-		if(pere != null && noeudPrecedent != null) {
144
-			return new Label( pere.getDestination() , noeudPrecedent.getCost()+ data.getCost(pere), pere);
145
-		}else {
146
-			return new Label(data.getOrigin(), 0, null);
147
-		}
148
-		
143
+			return new Label( pere.getDestination() , noeudPrecedent.getCost()+ data.getCost(pere), pere);	
144
+	}
145
+	
146
+	protected Label createLabelOrigin(ShortestPathData data) {
147
+		return new Label(data.getOrigin(), 0, null);
149 148
 	}
150 149
 
151 150
 }

+ 10
- 1
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelStar.java View File

@@ -3,7 +3,7 @@ package org.insa.graphs.algorithm.shortestpath;
3 3
 import org.insa.graphs.model.Arc;
4 4
 import org.insa.graphs.model.Node;
5 5
 
6
-public class LabelStar extends Label{
6
+public class LabelStar extends Label implements Comparable<Label>{
7 7
 	
8 8
 	private Double coutEstime;
9 9
 
@@ -16,4 +16,13 @@ public class LabelStar extends Label{
16 16
 	public double getTotalCost(){
17 17
 		return this.getCost() + this.coutEstime;
18 18
 	}
19
+	
20
+	@Override
21
+	public int compareTo(Label o) {
22
+		LabelStar other = (LabelStar) o;
23
+		if(this.getTotalCost() == other.getTotalCost()) {
24
+			return Double.compare(this.coutEstime, other.coutEstime);
25
+		}
26
+		return Double.compare(this.getTotalCost(), o.getTotalCost()); 
27
+	}
19 28
 }

+ 25
- 17
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java View File

@@ -32,7 +32,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
32 32
     public BinaryHeap() {
33 33
         this.currentSize = 0;
34 34
         this.array = new ArrayList<E>();
35
-//        this.indexArray = new Couple[70];
35
+
36 36
     }
37 37
 
38 38
     /**
@@ -163,7 +163,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
163 163
         }
164 164
         
165 165
         int index = -1;
166
-//        Iterate over the array and check if the x exists
166
+//       Iterate over the array and check if the x exists
167 167
         for(int i=0; i<this.currentSize;i++) {
168 168
         	if(this.array.get(i).equals(x)) {
169 169
         		index = i;
@@ -203,8 +203,30 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
203 203
         return minItem;
204 204
     }
205 205
 
206
+   
207
+    /**
208
+     * Check that the binary heap is valid
209
+     * 
210
+     * @return a boolean indicating whether the tree is valid
211
+     */
212
+    public boolean isValid() {
213
+    	E current;
214
+    	E parent;
215
+    	for(int i=1; i<this.currentSize;  i++) {
216
+    		current = this.array.get(i);
217
+    		parent = this.array.get(this.indexParent(i));
218
+    		if( current.compareTo(parent) < 0) {
219
+    			return false;
220
+    		}	
221
+    	}
222
+    	return true;
223
+    }
224
+    
206 225
     
207
-    //Met à jour l'arbre à partir à partir d'un élément dont le coût a changé
226
+    /**
227
+     * Update the element x in the heap if it exists
228
+     * Raise ElementNotFoundException otherwise
229
+     */
208 230
     public void miseAJour(E x) throws ElementNotFoundException {
209 231
     	
210 232
     	if(this.isEmpty() || x == null){
@@ -230,20 +252,6 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
230 252
         
231 253
     }
232 254
     
233
-    
234
-    public boolean isValid() {
235
-    	E current;
236
-    	E parent;
237
-    	for(int i=1; i<this.currentSize;  i++) {
238
-    		current = this.array.get(i);
239
-    		parent = this.array.get(this.indexParent(i));
240
-    		if( current.compareTo(parent) < 0) {
241
-    			return false;
242
-    		}	
243
-    	}
244
-    	return true;
245
-    }
246
-    
247 255
     /**
248 256
      * Creates a multi-lines string representing a sorted view of this binary heap.
249 257
      * 

+ 0
- 107
be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithmTest.java View File

@@ -1,107 +0,0 @@
1
-package org.insa.graphs.algorithm.shortestpath;
2
-
3
-import static org.junit.Assert.*;
4
-
5
-import java.util.ArrayList;
6
-import java.util.List;
7
-
8
-import org.insa.graphs.algorithm.AbstractSolution.Status;
9
-import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm;
10
-import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
11
-import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
12
-import org.insa.graphs.model.Arc;
13
-import org.insa.graphs.model.Graph;
14
-import org.insa.graphs.model.Node;
15
-import org.insa.graphs.model.Path;
16
-import org.junit.After;
17
-import org.junit.Test;
18
-
19
-public class AStarAlgorithmTest extends algorithmTest{
20
-
21
-	@After
22
-	public void tearDown() throws Exception {
23
-	}
24
-
25
-	@Test
26
-	public void testAStarShortest() {
27
-		
28
-		for(int i=0; i<dataShortest.length; i++) {
29
-			AStarAlgorithm aStar = new AStarAlgorithm(dataShortest[i]);
30
-			ShortestPathSolution solution = aStar.run();
31
-			Path cheminSolution = solution.getPath();
32
-			
33
-			Graph graph = solution.getInputData().getGraph();
34
-			 
35
-			//Vérifie chemin valide
36
-			assertTrue(cheminSolution.isValid());
37
-			assertEquals(cheminSolution.getOrigin(), graph.get(tabOrigin[i]));
38
-			assertEquals(cheminSolution.getDestination(), graph.get(tabDestination[i]));
39
-			   
40
-			 // List of nodes
41
-			 ArrayList<Node> nodes = new ArrayList<Node>();
42
-			 List<Arc> arcsSolution = cheminSolution.getArcs();
43
-			 nodes.add(arcsSolution.get(0).getOrigin());
44
-			 
45
-			 for(int j=0;j<arcsSolution.size(); j++) {
46
-		        nodes.add(arcsSolution.get(j).getDestination());	
47
-		     }
48
-			 
49
-			 Path cheminShortest = Path.createShortestPathFromNodes(graph, nodes);
50
-			 
51
-			 //Vérifie que les chemins sont identiques (Arcs, taille, temps)
52
-			 assertEquals(cheminSolution.getArcs(), cheminShortest.getArcs());
53
-			 assertTrue(cheminSolution.getLength() == cheminShortest.getLength());
54
-			 assertTrue(cheminSolution.getMinimumTravelTime() == cheminShortest.getMinimumTravelTime());
55
-		}
56
-	}
57
-	
58
-	@Test
59
-	public void testAStarFastest() {
60
-		
61
-		for(int i=0; i<dataShortest.length; i++) {
62
-			AStarAlgorithm aStar = new AStarAlgorithm(dataFastest[i]);
63
-			ShortestPathSolution solution = aStar.run();
64
-			Path cheminSolution = solution.getPath();
65
-			
66
-			Graph graph = solution.getInputData().getGraph();
67
-			 
68
-			//Vérifie chemin valide
69
-			assertTrue(cheminSolution.isValid());
70
-			assertEquals(cheminSolution.getOrigin(), graph.get(tabOrigin[i]));
71
-			assertEquals(cheminSolution.getDestination(), graph.get(tabDestination[i]));
72
-			   
73
-			 // List of nodes
74
-			 ArrayList<Node> nodes = new ArrayList<Node>();
75
-			 List<Arc> arcsSolution = cheminSolution.getArcs();
76
-			 nodes.add(arcsSolution.get(0).getOrigin());
77
-			 
78
-			 for(int j=0;j<arcsSolution.size(); j++) {
79
-		        nodes.add(arcsSolution.get(j).getDestination());	
80
-		     }
81
-			 
82
-			 Path cheminFastest = Path.createFastestPathFromNodes(graph, nodes);
83
-			 
84
-			 //Vérifie que les chemins sont identiques (Arcs, taille, temps)
85
-			 assertEquals(cheminSolution.getArcs(), cheminFastest.getArcs());
86
-			 assertTrue(cheminSolution.getLength() == cheminFastest.getLength());
87
-			 assertTrue(cheminSolution.getMinimumTravelTime() == cheminFastest.getMinimumTravelTime());
88
-			 
89
-		}
90
-	}
91
-	
92
-	@Test
93
-	public void testDijkstraInfeasable() {
94
-		
95
-		 AStarAlgorithm aStar = new AStarAlgorithm(dataInfeasable);
96
-		 ShortestPathSolution solution =  aStar.run();
97
-		 Path cheminSolution = solution.getPath();
98
-		 
99
-		 //Vérifie chemin valide
100
-		 assertEquals(cheminSolution, null);
101
-		 assertEquals(solution.getStatus(), Status.INFEASIBLE);
102
-		
103
-	}
104
-	
105
-	
106
-
107
-}

+ 0
- 127
be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithmTest.java View File

@@ -1,127 +0,0 @@
1
-package org.insa.graphs.algorithm.shortestpath;
2
-
3
-import static org.junit.Assert.assertEquals;
4
-import static org.junit.Assert.assertTrue;
5
-import static org.junit.Assume.assumeTrue;
6
-
7
-import java.io.BufferedInputStream;
8
-import java.io.DataInputStream;
9
-import java.io.FileInputStream;
10
-import java.util.ArrayList;
11
-import java.util.List;
12
-
13
-import org.insa.graphs.algorithm.ArcInspector;
14
-import org.insa.graphs.algorithm.ArcInspectorFactory;
15
-import org.insa.graphs.algorithm.AbstractSolution.Status;
16
-import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
17
-import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
18
-import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
19
-import org.insa.graphs.model.Arc;
20
-import org.insa.graphs.model.Graph;
21
-import org.insa.graphs.model.Node;
22
-import org.insa.graphs.model.Path;
23
-import org.insa.graphs.model.io.BinaryGraphReader;
24
-import org.insa.graphs.model.io.BinaryPathReader;
25
-import org.insa.graphs.model.io.GraphReader;
26
-import org.insa.graphs.model.io.PathReader;
27
-import org.junit.After;
28
-import org.junit.Before;
29
-import org.junit.Test;
30
-
31
-public class DijkstraAlgorithmTest extends algorithmTest {
32
-	
33
-	// Small graph use for tests
34
-   
35
-
36
-	@After
37
-	public void tearDown() throws Exception {
38
-	}
39
-
40
-	
41
-	@Test
42
-	public void testDijkstraShortest() {
43
-		
44
-		for(int i=0; i<dataShortest.length; i++) {
45
-			
46
-			DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(dataShortest[i]);
47
-			ShortestPathSolution solution = dijkstra.run();
48
-			 Path cheminSolution = solution.getPath();
49
-			 
50
-			 Graph graph = solution.getInputData().getGraph();
51
-			 
52
-			//Vérifie chemin valide
53
-			assertTrue(cheminSolution.isValid());
54
-			assertEquals(cheminSolution.getOrigin(), graph.get(tabOrigin[i]));
55
-			assertEquals(cheminSolution.getDestination(), graph.get(tabDestination[i]));
56
-			   
57
-			 // List of nodes
58
-			 ArrayList<Node> nodes = new ArrayList<Node>();
59
-			 List<Arc> arcsSolution = cheminSolution.getArcs();
60
-			 nodes.add(arcsSolution.get(0).getOrigin());
61
-			 
62
-			 for(int j=0;j< arcsSolution.size(); j++) {
63
-		        nodes.add(arcsSolution.get(j).getDestination());	
64
-		     }
65
-			 
66
-			 Path cheminShortest = Path.createShortestPathFromNodes(graph, nodes);
67
-			 
68
-			 //Vérifie que les chemins sont identiques (Arcs, taille, temps)
69
-			 assertEquals(cheminSolution.getArcs(), cheminShortest.getArcs());
70
-			 assertTrue(cheminSolution.getLength() == cheminShortest.getLength());
71
-			 assertTrue(cheminSolution.getMinimumTravelTime() == cheminShortest.getMinimumTravelTime());
72
-		}
73
-
74
-	}
75
-	
76
-	
77
-	@Test
78
-	public void testDijkstraFastest() {
79
-		
80
-		for(int i=0; i<dataShortest.length; i++) {
81
-			DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(dataFastest[i]);
82
-			ShortestPathSolution solution = dijkstra.run();
83
-			Path cheminSolution = solution.getPath();
84
-			 
85
-			Graph graph = solution.getInputData().getGraph();
86
-			 
87
-			//Vérifie chemin valide
88
-			assertTrue(cheminSolution.isValid());
89
-			assertEquals(cheminSolution.getOrigin(), graph.get(tabOrigin[i]));
90
-			assertEquals(cheminSolution.getDestination(), graph.get(tabDestination[i]));
91
-			   
92
-			 // List of nodes
93
-			 ArrayList<Node> nodes = new ArrayList<Node>();
94
-			 List<Arc> arcsSolution = cheminSolution.getArcs();
95
-			 nodes.add(arcsSolution.get(0).getOrigin());
96
-			 
97
-			 for(int j=0; j<arcsSolution.size(); j++) {
98
-		        nodes.add(arcsSolution.get(j).getDestination());	
99
-		     }
100
-			 
101
-			 Path cheminFastest = Path.createFastestPathFromNodes(graph, nodes);
102
-			 
103
-			 //Vérifie que les chemins sont identiques (Arcs, taille, temps)
104
-			 assertEquals(cheminSolution.getArcs(), cheminFastest.getArcs());
105
-			 assertTrue(cheminSolution.getLength() == cheminFastest.getLength());
106
-			 assertTrue(cheminSolution.getMinimumTravelTime() == cheminFastest.getMinimumTravelTime());
107
-			 
108
-		}
109
-	
110
-		
111
-	}
112
-	
113
-	@Test
114
-	public void testDijkstraInfeasable() {
115
-		
116
-		 DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(dataInfeasable);
117
-		 ShortestPathSolution solution =  dijkstra.run();
118
-		 Path cheminSolution = solution.getPath();
119
-		 
120
-		 //Vérifie chemin valide
121
-		 assertEquals(cheminSolution, null);
122
-		 assertEquals(solution.getStatus(), Status.INFEASIBLE);
123
-		
124
-	}
125
-	
126
-
127
-}

+ 0
- 96
be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/algorithmTest.java View File

@@ -1,96 +0,0 @@
1
-package org.insa.graphs.algorithm.shortestpath;
2
-
3
-import static org.junit.Assert.*;
4
-
5
-import java.io.BufferedInputStream;
6
-import java.io.DataInputStream;
7
-import java.io.FileInputStream;
8
-import java.util.List;
9
-
10
-import org.insa.graphs.algorithm.ArcInspector;
11
-import org.insa.graphs.algorithm.ArcInspectorFactory;
12
-import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
13
-import org.insa.graphs.model.Graph;
14
-import org.insa.graphs.model.io.BinaryGraphReader;
15
-import org.insa.graphs.model.io.GraphReader;
16
-import org.junit.After;
17
-import org.junit.Before;
18
-
19
-public class algorithmTest {
20
-
21
-	 protected Graph graph, graphPolynesie, graphBelgique;
22
-
23
-	    
24
-    final String hauteGaronne = "/Users/Kevin/Desktop/programmation/java_files/Maps/europe/france/haute-garonne.mapgr";
25
-    final String polynesie = "/Users/Kevin/Desktop/programmation/java_files/Maps/europe/france/french-polynesia.mapgr";
26
-    final String belgique = "/Users/Kevin/Desktop/programmation/java_files/Maps/europe/belgique/belgium.mapgr";
27
-    
28
-    final static int taille = 5;
29
-    
30
-    protected ShortestPathData dataShortest[];
31
-    protected ShortestPathData dataFastest[];
32
-    protected ShortestPathData dataInfeasable;
33
-    protected int tabOrigin[], tabDestination[];
34
-    
35
-	   
36
-
37
-	@Before
38
-	public void setUp() throws Exception {
39
-	
40
-		
41
-		List<ArcInspector> arcList = ArcInspectorFactory.getAllFilters();
42
-		ArcInspector arcShortestAllRoad = arcList.get(0);
43
-		ArcInspector arcFastestAllRoad = arcList.get(2);
44
-
45
-        try (GraphReader readerHauteGaronne = new BinaryGraphReader(new DataInputStream(
46
-                new BufferedInputStream(new FileInputStream(hauteGaronne))))) {
47
-
48
-            // TODO: Read the graph.
49
-            graph = readerHauteGaronne.read();
50
-        }
51
-        
52
-        try (GraphReader readerPolynesie = new BinaryGraphReader(new DataInputStream(
53
-                new BufferedInputStream(new FileInputStream(polynesie))))) {
54
-
55
-            // TODO: Read the graph.
56
-            graphPolynesie = readerPolynesie.read();
57
-        }
58
-        
59
-        try (GraphReader readerBelgique = new BinaryGraphReader(new DataInputStream(
60
-                new BufferedInputStream(new FileInputStream(belgique))))) {
61
-
62
-            // TODO: Read the graph.
63
-            graphBelgique = readerBelgique.read();
64
-        }
65
-        
66
-        dataShortest = new ShortestPathData[taille];
67
-        dataFastest = new ShortestPathData[taille];
68
-        tabOrigin = new int[taille]; tabDestination = new int[taille];
69
-        
70
-        tabOrigin[0] = 10991; tabOrigin[1] = 102582; tabOrigin[2]= 81460; tabOrigin[3]= 942294;  tabOrigin[4]= 10228;
71
-        
72
-        tabDestination[0] = 89149; tabDestination[1]= 84718; tabDestination[2] = 129594; tabDestination[3] = 412806 ; tabDestination[4]= 666164 ;
73
-        
74
-        
75
-        dataShortest[0] = new ShortestPathData(graph, graph.get(tabOrigin[0]), graph.get(tabDestination[0]), arcShortestAllRoad);
76
-        dataShortest[1] = new ShortestPathData(graph, graph.get(tabOrigin[1]), graph.get(tabDestination[1]), arcShortestAllRoad);
77
-        dataShortest[2] = new ShortestPathData(graph, graph.get(tabOrigin[2]), graph.get(tabDestination[2]), arcShortestAllRoad);
78
-        dataShortest[3] = new ShortestPathData(graphBelgique, graphBelgique.get(tabOrigin[3]), graphBelgique.get(tabDestination[3]), arcShortestAllRoad);
79
-        dataShortest[4] = new ShortestPathData(graphBelgique, graphBelgique.get(tabOrigin[4]), graphBelgique.get(tabDestination[4]), arcShortestAllRoad);
80
-       
81
-        dataFastest[0] = new ShortestPathData(graph, graph.get(tabOrigin[0]), graph.get(tabDestination[0]) , arcFastestAllRoad);
82
-        dataFastest[1] = new ShortestPathData(graph, graph.get(tabOrigin[1]), graph.get(tabDestination[1]), arcFastestAllRoad);
83
-        dataFastest[2] = new ShortestPathData(graph, graph.get(tabOrigin[2]), graph.get(tabDestination[2]), arcFastestAllRoad);
84
-        dataFastest[3] = new ShortestPathData(graphBelgique, graphBelgique.get(tabOrigin[3]), graphBelgique.get(tabDestination[3]), arcFastestAllRoad);
85
-        dataFastest[4] = new ShortestPathData(graphBelgique, graphBelgique.get(tabOrigin[4]), graphBelgique.get(tabDestination[4]), arcFastestAllRoad);
86
-
87
-        
88
-        
89
-        dataInfeasable = new ShortestPathData(graphPolynesie, graphPolynesie.get(2984), graphPolynesie.get(10467), arcFastestAllRoad);
90
-	}
91
-
92
-	@After
93
-	public void tearDown() throws Exception {
94
-	}
95
-
96
-}

+ 144
- 0
be-graphes-algos/src/test/java/tests/AStarAlgorithmTest.java View File

@@ -0,0 +1,144 @@
1
+package tests;
2
+
3
+import static org.junit.Assert.*;
4
+
5
+import java.util.ArrayList;
6
+import java.util.List;
7
+
8
+import org.insa.graphs.algorithm.AbstractSolution.Status;
9
+import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm;
10
+import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
11
+import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
12
+import org.insa.graphs.model.Arc;
13
+import org.insa.graphs.model.Graph;
14
+import org.insa.graphs.model.Node;
15
+import org.insa.graphs.model.Path;
16
+import org.junit.After;
17
+import org.junit.Test;
18
+
19
+public class AStarAlgorithmTest extends algorithmTest{
20
+
21
+	@After
22
+	public void tearDown() throws Exception {
23
+	}
24
+
25
+	//Test pour vérifier que le chemin obtenu avec l'algorithme A* 
26
+	//est le même que celui crée par la méthode createShortestPathFromNodes de la classe Path
27
+	@Test
28
+	public void testAStarShortest() {
29
+		
30
+		for(int i=0; i<dataShortest.length; i++) {
31
+			AStarAlgorithm aStar = new AStarAlgorithm(dataShortest[i]);
32
+			ShortestPathSolution solution = aStar.run();
33
+			Path cheminSolution = solution.getPath();
34
+			
35
+			Graph graph = solution.getInputData().getGraph();
36
+			 
37
+			if (cheminSolution == null) {
38
+
39
+				assertEquals(solution.getStatus(), Status.INFEASIBLE);
40
+
41
+			} else {
42
+				//Vérifie chemin valide
43
+				assertTrue(cheminSolution.isValid());
44
+//				assertEquals(cheminSolution.getOrigin(), graph.get(tabOrigin[i]));
45
+//				assertEquals(cheminSolution.getDestination(), graph.get(tabDestination[i]));
46
+				   
47
+				 // List of nodes
48
+				 ArrayList<Node> nodes = new ArrayList<Node>();
49
+				 List<Arc> arcsSolution = cheminSolution.getArcs();
50
+				 nodes.add(arcsSolution.get(0).getOrigin());
51
+				 
52
+				 for(int j=0;j<arcsSolution.size(); j++) {
53
+			        nodes.add(arcsSolution.get(j).getDestination());	
54
+			     }
55
+				 
56
+				 Path cheminShortest = Path.createShortestPathFromNodes(graph, nodes);
57
+				 
58
+				 //Vérifie que les chemins sont identiques (Arcs, taille, temps)
59
+				 assertEquals(cheminSolution.getArcs(), cheminShortest.getArcs());
60
+				 assertEquals(cheminSolution.getLength(), cheminShortest.getLength(), 1e-6);
61
+				 assertEquals(cheminSolution.getMinimumTravelTime(), cheminShortest.getMinimumTravelTime(), 1e-6);
62
+			}
63
+		}
64
+	}
65
+	
66
+	
67
+	//Test pour vérifier que le chemin obtenu avec l'algorithme A* 
68
+	//est le même que celui crée par la méthode createFastestPathFromNodes de la classe Path
69
+	@Test
70
+	public void testAStarFastest() {
71
+		
72
+		for(int i=0; i<dataShortest.length; i++) {
73
+			AStarAlgorithm aStar = new AStarAlgorithm(dataFastest[i]);
74
+			ShortestPathSolution solution = aStar.run();
75
+			Path cheminSolution = solution.getPath();
76
+			
77
+			Graph graph = solution.getInputData().getGraph();
78
+			 
79
+			if (cheminSolution == null) {
80
+
81
+				assertEquals(solution.getStatus(), Status.INFEASIBLE);
82
+
83
+			} else {
84
+				//Vérifie chemin valide
85
+				assertTrue(cheminSolution.isValid());
86
+//				assertEquals(cheminSolution.getOrigin(), graph.get(tabOrigin[i]));
87
+//				assertEquals(cheminSolution.getDestination(), graph.get(tabDestination[i]));
88
+				   
89
+				 // List of nodes
90
+				 ArrayList<Node> nodes = new ArrayList<Node>();
91
+				 List<Arc> arcsSolution = cheminSolution.getArcs();
92
+				 nodes.add(arcsSolution.get(0).getOrigin());
93
+				 
94
+				 for(int j=0;j<arcsSolution.size(); j++) {
95
+			        nodes.add(arcsSolution.get(j).getDestination());	
96
+			     }
97
+				 
98
+				 Path cheminFastest = Path.createFastestPathFromNodes(graph, nodes);
99
+				 
100
+				 //Vérifie que les chemins sont identiques (Arcs, taille, temps)
101
+				 assertEquals(cheminSolution.getArcs(), cheminFastest.getArcs());
102
+				 assertEquals(cheminSolution.getLength(), cheminFastest.getLength(), 1e-6);
103
+				 assertEquals(cheminSolution.getMinimumTravelTime(), cheminFastest.getMinimumTravelTime(), 1e-6);
104
+				
105
+			}
106
+			 
107
+		}
108
+	}
109
+	
110
+	
111
+	//Test pour un chemin entre 2 îles 
112
+	@Test
113
+	public void testAStarInfeasable() {
114
+		
115
+		 AStarAlgorithm aStar = new AStarAlgorithm(dataInfeasable);
116
+		 ShortestPathSolution solution =  aStar.run();
117
+		 Path cheminSolution = solution.getPath();
118
+		 
119
+		 //Vérifie chemin valide
120
+		 assertEquals(cheminSolution, null);
121
+		 assertEquals(solution.getStatus(), Status.INFEASIBLE);
122
+		
123
+	}
124
+	
125
+	
126
+	//Test pour un chemin d'un seul noeud
127
+	@Test
128
+	public void testAStarOneNode() {
129
+
130
+		AStarAlgorithm aStar = new AStarAlgorithm(dataOneNode);
131
+		ShortestPathSolution solution = aStar.run();
132
+		Path cheminSolution = solution.getPath();
133
+
134
+		// Vérifie que le chemin est de longueur nulle, de temps nul et n'a pas d'arc
135
+		assertEquals(solution.getStatus(), Status.OPTIMAL);
136
+		assertEquals(cheminSolution.getLength(), 0f, 0.1e-6);
137
+		assertEquals(cheminSolution.getMinimumTravelTime(), 0d, 1e-6);
138
+		assertEquals(cheminSolution.getArcs().size(), 0);
139
+
140
+	}
141
+	
142
+	
143
+
144
+}

+ 11
- 0
be-graphes-algos/src/test/java/tests/AllTests.java View File

@@ -0,0 +1,11 @@
1
+package tests;
2
+
3
+import org.junit.runner.RunWith;
4
+import org.junit.runners.Suite;
5
+import org.junit.runners.Suite.SuiteClasses;
6
+
7
+@RunWith(Suite.class)
8
+@SuiteClasses({ AStarAlgorithmTest.class, DijkstraAlgorithmTest.class, OptimaliteTest.class})
9
+public class AllTests {
10
+
11
+}

+ 159
- 0
be-graphes-algos/src/test/java/tests/DijkstraAlgorithmTest.java View File

@@ -0,0 +1,159 @@
1
+package tests;
2
+
3
+import static org.junit.Assert.assertEquals;
4
+import static org.junit.Assert.assertTrue;
5
+import static org.junit.Assume.assumeTrue;
6
+
7
+import java.io.BufferedInputStream;
8
+import java.io.DataInputStream;
9
+import java.io.FileInputStream;
10
+import java.util.ArrayList;
11
+import java.util.List;
12
+
13
+import org.insa.graphs.algorithm.ArcInspector;
14
+import org.insa.graphs.algorithm.ArcInspectorFactory;
15
+import org.insa.graphs.algorithm.AbstractSolution.Status;
16
+import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
17
+import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
18
+import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
19
+import org.insa.graphs.model.Arc;
20
+import org.insa.graphs.model.Graph;
21
+import org.insa.graphs.model.Node;
22
+import org.insa.graphs.model.Path;
23
+import org.insa.graphs.model.io.BinaryGraphReader;
24
+import org.insa.graphs.model.io.BinaryPathReader;
25
+import org.insa.graphs.model.io.GraphReader;
26
+import org.insa.graphs.model.io.PathReader;
27
+import org.junit.After;
28
+import org.junit.Before;
29
+import org.junit.Test;
30
+
31
+public class DijkstraAlgorithmTest extends algorithmTest {
32
+
33
+	// Small graph use for tests
34
+
35
+	@After
36
+	public void tearDown() throws Exception {
37
+	}
38
+
39
+	
40
+	//Test pour vérifier que le chemin obtenu avec l'algorithme de dijkstra 
41
+	//est le même que celui crée par la méthode createShortestPathFromNodes de la classe Path
42
+	@Test
43
+	public void testDijkstraShortest() {
44
+
45
+		for (int i = 0; i < dataShortest.length; i++) {
46
+
47
+			DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(dataShortest[i]);
48
+			ShortestPathSolution solution = dijkstra.run();
49
+			Path cheminSolution = solution.getPath();
50
+
51
+			Graph graph = solution.getInputData().getGraph();
52
+
53
+			if (cheminSolution == null) {
54
+
55
+				assertEquals(solution.getStatus(), Status.INFEASIBLE);
56
+
57
+			} else {
58
+
59
+				// Vérifie chemin valide
60
+				assertTrue(cheminSolution.isValid());
61
+//				assertEquals(cheminSolution.getOrigin(), graph.get(tabOrigin[i]));
62
+//				assertEquals(cheminSolution.getDestination(), graph.get(tabDestination[i]));
63
+
64
+				// List of nodes
65
+				ArrayList<Node> nodes = new ArrayList<Node>();
66
+				List<Arc> arcsSolution = cheminSolution.getArcs();
67
+				nodes.add(arcsSolution.get(0).getOrigin());
68
+
69
+				for (int j = 0; j < arcsSolution.size(); j++) {
70
+					nodes.add(arcsSolution.get(j).getDestination());
71
+				}
72
+
73
+				Path cheminShortest = Path.createShortestPathFromNodes(graph, nodes);
74
+
75
+				// Vérifie que les chemins sont identiques (Arcs, taille, temps)
76
+				assertEquals(cheminSolution.getArcs(), cheminShortest.getArcs());
77
+				assertEquals(cheminSolution.getLength(), cheminShortest.getLength(), 1e-6);
78
+				 assertEquals(cheminSolution.getMinimumTravelTime(), cheminShortest.getMinimumTravelTime(), 1e-6);
79
+			}
80
+
81
+		}
82
+
83
+	}
84
+
85
+	//Test pour vérifier que le chemin obtenu avec l'algorithme de dijkstra 
86
+	//est le même que celui crée par la méthode createFastestPathFromNodes de la classe Path
87
+	@Test
88
+	public void testDijkstraFastest() {
89
+
90
+		for (int i = 0; i < dataShortest.length; i++) {
91
+			DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(dataFastest[i]);
92
+			ShortestPathSolution solution = dijkstra.run();
93
+			Path cheminSolution = solution.getPath();
94
+
95
+			Graph graph = solution.getInputData().getGraph();
96
+
97
+			if (cheminSolution == null) {
98
+
99
+				assertEquals(solution.getStatus(), Status.INFEASIBLE);
100
+
101
+			} else {
102
+				// Vérifie chemin valide
103
+				assertTrue(cheminSolution.isValid());
104
+//				assertEquals(cheminSolution.getOrigin(), graph.get(tabOrigin[i]));
105
+//				assertEquals(cheminSolution.getDestination(), graph.get(tabDestination[i]));
106
+
107
+				// List of nodes
108
+				ArrayList<Node> nodes = new ArrayList<Node>();
109
+				List<Arc> arcsSolution = cheminSolution.getArcs();
110
+				nodes.add(arcsSolution.get(0).getOrigin());
111
+
112
+				for (int j = 0; j < arcsSolution.size(); j++) {
113
+					nodes.add(arcsSolution.get(j).getDestination());
114
+				}
115
+
116
+				Path cheminFastest = Path.createFastestPathFromNodes(graph, nodes);
117
+
118
+				// Vérifie que les chemins sont identiques (Arcs, taille, temps)
119
+				assertEquals(cheminSolution.getArcs(), cheminFastest.getArcs());
120
+				assertEquals(cheminSolution.getLength(), cheminFastest.getLength(), 1e-6);
121
+				assertEquals(cheminSolution.getMinimumTravelTime(), cheminFastest.getMinimumTravelTime(), 1e-6);
122
+			}
123
+
124
+		}
125
+
126
+	}
127
+
128
+	//Test pour un chemin entre 2 îles 
129
+	@Test
130
+	public void testDijkstraInfeasable() {
131
+
132
+		DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(dataInfeasable);
133
+		ShortestPathSolution solution = dijkstra.run();
134
+		Path cheminSolution = solution.getPath();
135
+
136
+		// Vérifie qu'il n'y a pas de chemin
137
+		assertEquals(cheminSolution, null);
138
+		assertEquals(solution.getStatus(), Status.INFEASIBLE);
139
+
140
+	}
141
+	
142
+	//Test pour un chemin d'un seul noeud
143
+	@Test
144
+	public void testDijkstraOneNode() {
145
+
146
+		DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(dataOneNode);
147
+		ShortestPathSolution solution = dijkstra.run();
148
+		Path cheminSolution = solution.getPath();
149
+
150
+		// Vérifie que le chemin est de longueur nulle, de temps nul et n'a pas d'arc
151
+		assertEquals(solution.getStatus(), Status.OPTIMAL);
152
+		assertEquals(cheminSolution.getLength(), 0f, 1e-6);
153
+		assertEquals(cheminSolution.getMinimumTravelTime(), 0d, 1e-6);
154
+		assertEquals(cheminSolution.getArcs().size(), 0);
155
+
156
+	}
157
+
158
+
159
+}

+ 109
- 0
be-graphes-algos/src/test/java/tests/OptimaliteTest.java View File

@@ -0,0 +1,109 @@
1
+package tests;
2
+
3
+import static org.junit.Assert.*;
4
+
5
+import java.util.ArrayList;
6
+
7
+import org.insa.graphs.algorithm.AbstractSolution.Status;
8
+import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm;
9
+import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
10
+import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
11
+import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
12
+import org.insa.graphs.model.Path;
13
+import org.junit.After;
14
+import org.junit.Test;
15
+
16
+public class OptimaliteTest extends algorithmTest {
17
+
18
+	@After
19
+	public void tearDown() throws Exception {
20
+	}
21
+
22
+	@Test
23
+	public void testShortest() {
24
+		
25
+		ArrayList<Long> tempsDijkstra = new ArrayList<>(),
26
+						tempsAStar = new ArrayList<Long>();
27
+		
28
+		for (int i = 0; i < dataShortest.length; i++) {
29
+			BellmanFordAlgorithm BF = new BellmanFordAlgorithm(dataShortest[i]);
30
+			DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(dataShortest[i]);
31
+			AStarAlgorithm aStar = new AStarAlgorithm(dataShortest[i]);
32
+			
33
+			ShortestPathSolution solutionBF = BF.run(),
34
+								 solutionDijkstra = dijkstra.run(),
35
+								 solutionAStar = aStar.run();
36
+			
37
+			
38
+			Path cheminSolutionBF = solutionBF.getPath(),
39
+				 cheminSolutionDijkstra = solutionDijkstra.getPath(),
40
+				 cheminSolutionAStar = solutionAStar.getPath();
41
+			
42
+			if (cheminSolutionBF == null) {
43
+				assertEquals(solutionBF.getStatus(), Status.INFEASIBLE);
44
+				assertEquals(solutionDijkstra.getStatus(), Status.INFEASIBLE);
45
+				assertEquals(solutionAStar.getStatus(), Status.INFEASIBLE);
46
+			}else {
47
+			
48
+			
49
+				tempsDijkstra.add(solutionDijkstra.getSolvingTime().toMillis());
50
+				tempsAStar.add(solutionAStar.getSolvingTime().toMillis());
51
+				
52
+				assertEquals(cheminSolutionAStar.getArcs(), cheminSolutionBF.getArcs());
53
+				assertEquals(cheminSolutionAStar.getLength(), cheminSolutionBF.getLength(), 1e-6);
54
+				assertEquals(cheminSolutionAStar.getMinimumTravelTime(), cheminSolutionBF.getMinimumTravelTime(), 1e-6);
55
+			
56
+				assertEquals(cheminSolutionDijkstra.getArcs(), cheminSolutionBF.getArcs());
57
+				assertEquals(cheminSolutionDijkstra.getLength(), cheminSolutionBF.getLength(), 1e-6);
58
+				assertEquals(cheminSolutionDijkstra.getMinimumTravelTime(), cheminSolutionBF.getMinimumTravelTime(), 1e-6);
59
+			}
60
+		}
61
+		
62
+		System.out.println("Dijkstra : "+tempsDijkstra.toString());
63
+		System.out.println("A* : "+tempsAStar.toString());
64
+		
65
+	}
66
+	
67
+	@Test
68
+	public void testFastest() {
69
+		
70
+		ArrayList<Long> tempsDijkstra = new ArrayList<>(),
71
+						tempsAStar = new ArrayList<Long>();
72
+		
73
+		for (int i = 0; i < dataFastest.length; i++) {
74
+			BellmanFordAlgorithm BF = new BellmanFordAlgorithm(dataFastest[i]);
75
+			DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(dataFastest[i]);
76
+			AStarAlgorithm aStar = new AStarAlgorithm(dataFastest[i]);
77
+			
78
+			ShortestPathSolution solutionBF = BF.run(),
79
+								 solutionDijkstra = dijkstra.run(),
80
+								 solutionAStar = aStar.run();
81
+			
82
+			Path cheminSolutionBF = solutionBF.getPath(),
83
+				 cheminSolutionDijkstra = solutionDijkstra.getPath(),
84
+				 cheminSolutionAStar = solutionAStar.getPath();
85
+			
86
+			if (cheminSolutionBF == null) {
87
+				assertEquals(solutionBF.getStatus(), Status.INFEASIBLE);
88
+				assertEquals(solutionDijkstra.getStatus(), Status.INFEASIBLE);
89
+				assertEquals(solutionAStar.getStatus(), Status.INFEASIBLE);
90
+			}else {
91
+				
92
+				tempsDijkstra.add(solutionDijkstra.getSolvingTime().toMillis());
93
+				tempsAStar.add(solutionAStar.getSolvingTime().toMillis());
94
+				
95
+				assertEquals(cheminSolutionAStar.getArcs(), cheminSolutionBF.getArcs());
96
+				assertEquals(cheminSolutionAStar.getLength(), cheminSolutionBF.getLength(), 1e-6);
97
+				assertEquals(cheminSolutionAStar.getMinimumTravelTime(), cheminSolutionBF.getMinimumTravelTime(), 1e-6);
98
+			
99
+				assertEquals(cheminSolutionDijkstra.getArcs(), cheminSolutionBF.getArcs());
100
+				assertEquals(cheminSolutionDijkstra.getLength(), cheminSolutionBF.getLength(), 1e-6);
101
+				assertEquals(cheminSolutionDijkstra.getMinimumTravelTime(), cheminSolutionBF.getMinimumTravelTime(), 1e-6);
102
+			}
103
+		}
104
+		
105
+		System.out.println("Dijkstra : "+tempsDijkstra.toString());
106
+		System.out.println("A* : "+tempsAStar.toString());
107
+	}
108
+
109
+}

+ 85
- 0
be-graphes-algos/src/test/java/tests/algorithmTest.java View File

@@ -0,0 +1,85 @@
1
+package tests;
2
+
3
+import java.io.BufferedInputStream;
4
+import java.io.DataInputStream;
5
+import java.io.FileInputStream;
6
+import java.util.List;
7
+import java.util.Random;
8
+
9
+import org.insa.graphs.algorithm.ArcInspector;
10
+import org.insa.graphs.algorithm.ArcInspectorFactory;
11
+import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
12
+import org.insa.graphs.model.Graph;
13
+import org.insa.graphs.model.io.BinaryGraphReader;
14
+import org.insa.graphs.model.io.GraphReader;
15
+import org.junit.BeforeClass;
16
+
17
+public class algorithmTest {
18
+
19
+	protected static Graph graphs[];
20
+
21
+	//3 cartes, chacune testée pour les mode "shortest path all roads" et "fastest path all roads"
22
+	// /!\ Chemin des cartes à adapter /!\
23
+	final static String hauteGaronne = "/Users/Kevin/Desktop/programmation/java_files/Maps/europe/france/haute-garonne.mapgr";
24
+	final static String polynesie = "/Users/Kevin/Desktop/programmation/java_files/Maps/europe/france/french-polynesia.mapgr";
25
+	final static String bretagne = "/Users/Kevin/Desktop/programmation/java_files/Maps/europe/france/bretagne.mapgr";
26
+
27
+	final static String maps[] = { hauteGaronne, polynesie, bretagne };
28
+
29
+	final static int nbRoutesTest = 5; //Variable utilisée pour déterminer le nombre d'itinéraires à tester par carte et par mode
30
+									   //Dans l'état actuel, les tests seront effectués pour nbRoutesTest*3*2 itinéraires
31
+
32
+	protected static ShortestPathData dataShortest[];
33
+	protected static ShortestPathData dataFastest[];
34
+	protected static ShortestPathData dataInfeasable, dataOneNode;
35
+	protected static int tabOrigin[], tabDestination[];
36
+
37
+	@BeforeClass
38
+	public static void setUpBeforeClass() throws Exception {
39
+
40
+		List<ArcInspector> arcList = ArcInspectorFactory.getAllFilters();
41
+		ArcInspector arcShortestAllRoad = arcList.get(0);
42
+		ArcInspector arcFastestAllRoad = arcList.get(2);
43
+
44
+		graphs = new Graph[3];
45
+		
46
+		//Lecture des différentes cartes
47
+		for (int i = 0; i < 3; i++) {
48
+			try (GraphReader reader = new BinaryGraphReader(
49
+					new DataInputStream(new BufferedInputStream(new FileInputStream(maps[i]))))) {
50
+				// TODO: Read the graph.
51
+				graphs[i] = reader.read();
52
+			}
53
+
54
+		}
55
+
56
+		dataShortest = new ShortestPathData[nbRoutesTest * 3];
57
+		dataFastest = new ShortestPathData[nbRoutesTest * 3];
58
+		tabOrigin = new int[nbRoutesTest * 3];
59
+		tabDestination = new int[nbRoutesTest * 3];
60
+
61
+		Random rand = new Random(100l);
62
+		int origine, destination;
63
+
64
+		// Pour chaque carte, génère un nombre nbRoutes*2 d'itinéraires (plus court et
65
+		// plus rapide)
66
+		for (int i = 0; i < 3; i++) {
67
+
68
+			for (int j = 0; j < nbRoutesTest; j++) {
69
+
70
+				origine = rand.nextInt(graphs[i].getNodes().size());
71
+				destination = rand.nextInt(graphs[i].getNodes().size());
72
+				dataShortest[i * nbRoutesTest + j] = new ShortestPathData(graphs[i], graphs[i].get(origine),
73
+						graphs[i].get(destination), arcShortestAllRoad);
74
+				dataFastest[i * nbRoutesTest + j] = new ShortestPathData(graphs[i], graphs[i].get(origine),
75
+						graphs[i].get(destination), arcFastestAllRoad);
76
+
77
+			}
78
+		}
79
+
80
+		dataInfeasable = new ShortestPathData(graphs[1], graphs[1].get(2984), graphs[1].get(10467), arcFastestAllRoad);
81
+		dataOneNode = new ShortestPathData(graphs[0], graphs[0].get(535), graphs[0].get(535), arcShortestAllRoad);
82
+
83
+	}
84
+
85
+}

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

@@ -298,7 +298,11 @@ public class Path {
298 298
      *         kilometers-per-hour).
299 299
      */
300 300
     public double getTravelTime(double speed) {
301
-        return this.getLength()/(speed/3.6f);
301
+    	 float res = 0.0f;
302
+         for(Arc a : this.arcs) {
303
+         	res+= a.getTravelTime(speed);
304
+         }
305
+        return res;//this.getLength()/(speed/3.6f);
302 306
     }
303 307
 
304 308
     /**

Loading…
Cancel
Save