Browse Source

Remise Du BE - Version fonctionnelle : Correction de l'erreur sur l'estimation dans A*, finalisation des tests d'optimalité, ajout des tests de performances

Paul Faure 3 years ago
parent
commit
1eef4d2656

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

@@ -19,26 +19,14 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
19 19
     	}
20 20
     	
21 21
         if (data.getMode() == Mode.LENGTH) {
22
-        	MaxSpeed = 1;
22
+        	MaxSpeed = 1.01;
23 23
         } else {
24 24
         	MaxSpeed = MaxSpeed / 3.6;
25 25
         }
26 26
         
27
-        //double longueurArc = 0.0;
28
-        
29 27
         for (Node node: data.getGraph().getNodes()) {
30 28
         	double estimation = Point.distance(node.getPoint(), data.getDestination().getPoint())/MaxSpeed;
31 29
         	labels[node.getId()] = new LabelStar(node, estimation);
32
-        	/*if (node.getId() == 6800) {
33
-        		for (Arc arc: node.getSuccessors()) {
34
-        			if (arc.getDestination().getId() == 6299) {
35
-        				longueurArc = arc.getLength();
36
-        			}
37
-        		}
38
-        	}*/
39 30
         }
40
-        
41
-        //System.out.println("La distance entre les nodes " + data.getGraph().getNodes().get(6800).getId() + " et " + data.getGraph().getNodes().get(6299).getId() + " est : " + Point.distance(data.getGraph().getNodes().get(6800).getPoint(), data.getGraph().getNodes().get(6299).getPoint()) + " et la longeur de l'arc est : " + longueurArc);
42
-        
43 31
     }
44 32
 }

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

@@ -55,10 +55,9 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
55 55
             	notifyNodeMarked(current.getSommetCourant());
56 56
             	
57 57
             	// POUR LE TEST SUR LES COUTS CROISSANTS
58
-            	//System.out.println("Test sur le coût : " + labels[currentID].getTotalCost());
59 58
             	if (labels[currentID].compareTo(labels[ancienID]) == -1) {
60
-    	        	System.out.println("Erreur : Couts non croissants : noeud" + ancienID + " contre " + currentID);
61
-    	    	}
59
+    	        	System.out.println("Erreur : Couts non croissants : noeud " + ancienID + " (" + labels[ancienID].getTotalCost() + "; " + labels[ancienID].getEstimation() + ") contre " + currentID + " (" + labels[currentID].getTotalCost() + "; " + labels[currentID].getEstimation() + ") ");
60
+            	}
62 61
             	ancienID = currentID;
63 62
             	
64 63
             	//nbNonMarque--;
@@ -93,8 +92,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
93 92
             	if (compteur_successeurs != labels[currentID].getSommetCourant().getNumberOfSuccessors()) {
94 93
             		System.out.println("ERREUR Nombre de successeurs visités : " + compteur_successeurs + "/" + labels[currentID].getSommetCourant().getNumberOfSuccessors());
95 94
             	}
96
-            	//System.out.println("Nombre de successeurs visités : " + compteur_successeurs + "/" + labels[currentID].getSommetCourant().getNumberOfSuccessors());
97
-        	}
95
+            }
98 96
         	catch (EmptyPriorityQueueException e) {
99 97
         		encore = false;
100 98
         	}

+ 5
- 4
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java View File

@@ -51,16 +51,17 @@ public class Label implements Comparable<Label>{
51 51
 	}	
52 52
 	
53 53
 	public int compareTo(Label o) {
54
+		double epsilon = 0.0;
54 55
 		double diff = this.getTotalCost() - o.getTotalCost();
55
-		if (diff < 0.0) {
56
+		if (diff < -epsilon) {
56 57
 			return -1;
57
-		} else if (diff > 0.0) {
58
+		} else if (diff > epsilon) {
58 59
 			return 1;
59 60
 		} else {
60 61
 			diff = this.getEstimation() - o.getEstimation();
61
-			if (diff < 0.0) {
62
+			if (diff < -epsilon) {
62 63
 				return -1;
63
-			} else if (diff > 0.0) {
64
+			} else if (diff > epsilon) {
64 65
 				return 1;
65 66
 			} else {
66 67
 				return 0;

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

@@ -17,6 +17,6 @@ public class LabelStar extends Label implements Comparable<Label>{
17 17
 	}
18 18
 	
19 19
 	public double getTotalCost() {
20
-		return this.estimation + this.getCost();
20
+		return this.estimation + super.getCost();
21 21
 	}
22 22
 }

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

@@ -175,14 +175,18 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
175 175
     
176 176
     public boolean IsValid() {
177 177
     	boolean valid = true;
178
-    	int last_element_a_check = (int)(Math.pow(2.0, Math.floor(Math.log(this.currentSize))) - 1);
179
-    	int i = 0;
180
-    	while (i < last_element_a_check && valid) {
181
-    		if ((this.array.get(i).compareTo(this.array.get(this.indexLeft(i))) == 1) || (this.array.get(i).compareTo(this.array.get(this.indexLeft(i) + 1))) == 1) {
182
-    			valid = false;
183
-    		} else {
184
-    			i++;
185
-    		}
178
+    	if (!this.isEmpty()) {
179
+	    	int last_element_a_check = (int)(Math.pow(2.0, Math.floor(Math.log(this.currentSize))) - 1);
180
+	    	int i = 0;
181
+	    	while (i < last_element_a_check && valid) {
182
+	    		if (this.indexLeft(i) < this.currentSize && (this.array.get(this.indexLeft(i)).compareTo(this.array.get(i)) == -1)) {
183
+	    			valid = false;
184
+	    		}
185
+	    		if (this.indexLeft(i)+1 < this.currentSize &&(this.array.get(this.indexLeft(i) + 1).compareTo(this.array.get(i)) == -1)) {
186
+	    			valid = false;
187
+	    		} 
188
+	    		i++;
189
+	    	}
186 190
     	}
187 191
     	return valid;
188 192
     }

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

@@ -157,26 +157,4 @@ public class OptimaliteTest {
157 157
         assertEquals(solutionBellmanFord.getPath().getLength(),solutionDijkstra.getPath().getLength(), OptimaliteTest.delta);
158 158
         assertEquals(solutionBellmanFord.getPath().getLength(),solutionAStar.getPath().getLength(), OptimaliteTest.delta);
159 159
     }
160
-    
161
-    @Test
162
-    public void testDijkstraPlusRapide() {
163
-    	// On ne fait le test qu'avec un filtre sur les chemins autorisés
164
-    	ShortestPathData data = new ShortestPathData(graph, graph.get(75870), graph.get(103867), ArcInspectorFactory.getAllFilters().get(0));
165
-    	ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data);
166
-    	ShortestPathAlgorithm aStar = new AStarAlgorithm(data);
167
-    	ShortestPathSolution solutionAStar = aStar.run();
168
-    	ShortestPathSolution solutionDijkstra = dijkstra.run();
169
-        assertTrue(solutionDijkstra.getSolvingTime().compareTo(solutionAStar.getSolvingTime()) < 0);
170
-    }
171
-    
172
-    @Test
173
-    public void testAStarPlusRapide() {
174
-    	// On ne fait le test qu'avec un filtre sur les chemins autorisés
175
-    	ShortestPathData data = new ShortestPathData(graph, graph.get(12305), graph.get(103867), ArcInspectorFactory.getAllFilters().get(0));
176
-    	ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data);
177
-    	ShortestPathAlgorithm aStar = new AStarAlgorithm(data);
178
-    	ShortestPathSolution solutionAStar = aStar.run();
179
-    	ShortestPathSolution solutionDijkstra = dijkstra.run();
180
-        assertTrue(solutionDijkstra.getSolvingTime().compareTo(solutionAStar.getSolvingTime()) > 0);
181
-    }
182 160
 }

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

@@ -0,0 +1,85 @@
1
+package org.insa.graphs.algorithm.shortestpath;
2
+
3
+import static org.junit.Assert.assertTrue;
4
+
5
+import org.junit.BeforeClass;
6
+import org.junit.Test;
7
+
8
+import java.io.BufferedInputStream;
9
+import java.io.DataInputStream;
10
+import java.io.FileInputStream;
11
+
12
+import org.insa.graphs.model.io.BinaryGraphReader;
13
+import org.insa.graphs.model.io.GraphReader;
14
+import org.insa.graphs.model.Graph;
15
+
16
+import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
17
+import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm;
18
+
19
+import org.insa.graphs.algorithm.ArcInspectorFactory;
20
+import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
21
+
22
+public class PerformancesTest {
23
+
24
+    // Graph utilisé pour les tests
25
+    static Graph graph;
26
+    
27
+    //Lecture du graphe
28
+    @BeforeClass
29
+    public static void initAll() throws Exception {
30
+    	final String mapName = "/home/paulfaure/Documents/3A/JAVA/Cartes BE/haute-garonne/europe/france/haute-garonne.mapgr";
31
+
32
+        // Create a graph reader.
33
+        final GraphReader reader = new BinaryGraphReader(
34
+                new DataInputStream(new BufferedInputStream(new FileInputStream(mapName))));
35
+
36
+        // Read the graph.
37
+        PerformancesTest.graph = reader.read();
38
+
39
+        reader.close();
40
+    }
41
+    
42
+    @Test
43
+    public void testDijkstraPlusRapidePetitTrajet() {
44
+    	// On ne fait le test qu'avec un filtre sur les chemins autorisés
45
+    	ShortestPathData data = new ShortestPathData(graph, graph.get(87500), graph.get(103867), ArcInspectorFactory.getAllFilters().get(0));
46
+    	ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data);
47
+    	ShortestPathAlgorithm aStar = new AStarAlgorithm(data);
48
+    	ShortestPathSolution solutionAStar = aStar.run();
49
+    	ShortestPathSolution solutionDijkstra = dijkstra.run();
50
+        assertTrue(solutionDijkstra.getSolvingTime().compareTo(solutionAStar.getSolvingTime()) < 0);
51
+    }
52
+    
53
+    @Test
54
+    public void testDijkstraPlusRapideCheminNul() {
55
+    	// On ne fait le test qu'avec un filtre sur les chemins autorisés
56
+    	ShortestPathData data = new ShortestPathData(graph, graph.get(103867), graph.get(103867), ArcInspectorFactory.getAllFilters().get(0));
57
+    	ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data);
58
+    	ShortestPathAlgorithm aStar = new AStarAlgorithm(data);
59
+    	ShortestPathSolution solutionAStar = aStar.run();
60
+    	ShortestPathSolution solutionDijkstra = dijkstra.run();
61
+        assertTrue(solutionDijkstra.getSolvingTime().compareTo(solutionAStar.getSolvingTime()) < 0);
62
+    }
63
+    
64
+    @Test
65
+    public void testDijkstraPlusRapideCheminInexistant() {
66
+    	// On ne fait le test qu'avec un filtre sur les chemins autorisés
67
+    	ShortestPathData data = new ShortestPathData(graph, graph.get(135847), graph.get(103867), ArcInspectorFactory.getAllFilters().get(0));
68
+    	ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data);
69
+    	ShortestPathAlgorithm aStar = new AStarAlgorithm(data);
70
+    	ShortestPathSolution solutionAStar = aStar.run();
71
+    	ShortestPathSolution solutionDijkstra = dijkstra.run();
72
+        assertTrue(solutionDijkstra.getSolvingTime().compareTo(solutionAStar.getSolvingTime()) < 0);
73
+    }
74
+    
75
+    @Test
76
+    public void testAStarPlusRapideLongTrajet() {
77
+    	// On ne fait le test qu'avec un filtre sur les chemins autorisés
78
+    	ShortestPathData data = new ShortestPathData(graph, graph.get(12305), graph.get(103867), ArcInspectorFactory.getAllFilters().get(0));
79
+    	ShortestPathAlgorithm dijkstra = new DijkstraAlgorithm(data);
80
+    	ShortestPathAlgorithm aStar = new AStarAlgorithm(data);
81
+    	ShortestPathSolution solutionAStar = aStar.run();
82
+    	ShortestPathSolution solutionDijkstra = dijkstra.run();
83
+        assertTrue(solutionDijkstra.getSolvingTime().compareTo(solutionAStar.getSolvingTime()) > 0);
84
+    }
85
+}

Loading…
Cancel
Save