Nabzzz 3 years ago
parent
commit
1e2c385211

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

@@ -13,26 +13,37 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
13 13
     
14 14
     @Override protected void init(Label[] tabLabel, ShortestPathData data)
15 15
 	{	double maxspeed=data.getMaximumSpeed();
16
-		System.out.println("Max speed avant: "+maxspeed+"\n");
17
-		if(maxspeed==-1)
18
-		{
19
-			maxspeed=(data.getGraph().getGraphInformation().getMaximumSpeed());
20
-			//System.out.println("Max speed 2: "+maxspeed+"\n");
21
-			maxspeed=maxspeed/3.6;
22
-		}
23
-		System.out.println("Max speed 3: "+maxspeed+"\n");
16
+		//System.out.println("Max speed avant: "+maxspeed+"\n");
17
+		
18
+		//System.out.println("Max speed 3: "+maxspeed+"\n");
24 19
     	if(data.getMode()==Mode.LENGTH)
25 20
 		{
26 21
 			maxspeed=1.0;
27 22
 		}
28
-    	System.out.println("Max speed 4: "+maxspeed+"\n");
29
-		//Initialization 
30
-        for(Node n: data.getGraph().getNodes())
23
+    	else
24
+    	{
25
+    		if(maxspeed==-1)
26
+    		{
27
+    			maxspeed=(data.getGraph().getGraphInformation().getMaximumSpeed());
28
+    			//System.out.println("Max speed 2: "+maxspeed+"\n");
29
+    			maxspeed=maxspeed/3.6;
30
+    		}
31
+    	}
32
+    	//System.out.println("Max speed 4: "+maxspeed+"\n");
33
+    	//System.out.println("Origin latitude: "+data.getOrigin().getPoint().getLatitude() +"; origin longitude: "+data.getOrigin().getPoint().getLongitude()+"\n");
34
+    	System.out.println("Destination latitude: "+data.getDestination().getPoint().getLatitude() +"; destination longitude: "+data.getDestination().getPoint().getLongitude()+"\n");
35
+    	//Initialization 
36
+    	for(Node n: data.getGraph().getNodes())
31 37
         {
32
-        	tabLabel[n.getId()]=new LabelStar(n,Point.distance(n.getPoint(),data.getDestination().getPoint())/maxspeed);
38
+        	
39
+        		tabLabel[n.getId()]=new LabelStar(n,Point.distance(n.getPoint(),data.getDestination().getPoint())/maxspeed);
40
+ 
41
+        	
33 42
         }
43
+    	
34 44
         //tabLabel[data.getOrigin().getId()].setCout(Point.distance(data.getOrigin().getPoint(),data.getDestination().getPoint())/maxspeed);
35 45
 		
46
+        
36 47
 	}
37 48
     
38 49
 }

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

@@ -37,12 +37,13 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
37 37
 
38 38
     @Override
39 39
     protected ShortestPathSolution doRun() {
40
+    	double temps_debut=System.currentTimeMillis();
40 41
         final ShortestPathData data = getInputData();
41 42
         ShortestPathSolution solution = null;
42 43
         Graph graph = data.getGraph();
43 44
         final int nbNodes=graph.size();
44 45
         BinaryHeap<Label> tas=new BinaryHeap<Label>();
45
-        double coutPrec=0.0;
46
+        double coutPrec=0.0; //Sert uniquement pour les tests
46 47
         Label []tabLabel=new Label[nbNodes];
47 48
         init(tabLabel, data);
48 49
         //Initialization 
@@ -53,11 +54,12 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
53 54
         tabLabel[data.getOrigin().getId()].setCout(0);
54 55
         tas.insert(tabLabel[data.getOrigin().getId()]);
55 56
         this.notifyOriginProcessed(data.getOrigin());
56
-        //int nbMarque=0;
57
-        int iterations=0;
57
+        int nbExplore=0;// sert uniquement pour les tests
58
+        int nbMarque=1;// sert uniquement pour les tests
59
+        int iterations=0; // sert uniquement pour les tests
58 60
         while(!tabLabel[data.getDestination().getId()].isMarque())
59 61
         {
60
-        	iterations++;
62
+        	iterations++; //sert uniquement pour les tests.
61 63
         	Node X;
62 64
         	try
63 65
         	{
@@ -70,7 +72,6 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
70 72
         	}
71 73
         	tabLabel[X.getId()].setMarque(true);
72 74
         	this.notifyNodeMarked(X);
73
-        	
74 75
         	if(tabLabel[X.getId()].getCoutTotal()<coutPrec)
75 76
         	{
76 77
         		System.out.println("Problème! Les coûts ne sont pas croissants \n");
@@ -78,7 +79,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
78 79
         		System.out.println("Coût précédent: "+ coutPrec +"\n");
79 80
         	}
80 81
         	coutPrec=tabLabel[X.getId()].getCoutTotal();
81
-        	//nbMarque++;
82
+        	if(tabLabel[data.getDestination().getId()].isMarque())
83
+        	{
84
+        		break;
85
+        	}
86
+        	nbMarque++;
82 87
         	try
83 88
 			{
84 89
 				tas.remove(tabLabel[X.getId()]);
@@ -104,7 +109,8 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
104 109
         					}
105 110
         					catch(ElementNotFoundException e) {tas.insert(tabLabel[Y.getId()]);
106 111
             				tabLabel[Y.getId()].setPere(a);
107
-            				this.notifyNodeReached(Y);}
112
+            				this.notifyNodeReached(Y);
113
+            				nbExplore++;}
108 114
         		
109 115
         			}
110 116
         			
@@ -134,18 +140,16 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
134 140
         }
135 141
         System.out.println("Nombre d'arcs: "+arcs.size() + "\n");
136 142
         System.out.println("Nombre d'itérations: "+iterations + "\n");
143
+        System.out.println("Nombre de noeuds marqués: "+ nbMarque + "\n");
144
+        System.out.println("Nombre de noeuds explorés: "+ nbExplore + "\n");
137 145
         // Reverse the path...
138 146
         Collections.reverse(arcs);
139 147
         
140 148
         //Vérification grâce à Path.createShortestPathFromNodes
141 149
         Path p= new Path(graph,arcs);
142 150
         //List<Node> noeuds=new ArrayList<Node>();
143
-        float cout_total=0;
144
-        for(Arc a: arcs)
145
-        {
146
-        	cout_total += a.getLength();
147
-        }
148
-        if(p.getLength()==cout_total)
151
+        
152
+        if((int) p.getLength()*100== (int) tabLabel[data.getDestination().getId()].getCout()*100)
149 153
         {
150 154
         	System.out.println("La longueur du plus court chemin est la même que celle calculée grâce à l'algo \n");
151 155
         }
@@ -153,7 +157,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
153 157
         {
154 158
         	
155 159
         	System.out.println("ERREUR: La longueur du plus court chemin n'est la même que celle calculée grâce à l'algo \n");
156
-        	System.out.println("Cout pcc="+cout_total);
160
+        	System.out.println("Cout pcc="+ tabLabel[data.getDestination().getId()].getCout());
157 161
         	System.out.println("Cout algo="+p.getLength());
158 162
         }
159 163
         /*noeuds.add(data.getDestination());
@@ -198,9 +202,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
198 202
         // Create the final solution.
199 203
         solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
200 204
         }
201
-        
202
-        
203
-        return solution;
205
+       double temps_fin=System.currentTimeMillis();
206
+       double total_temps=temps_fin-temps_debut;
207
+       System.out.println("Duree d'execution:"+total_temps);
208
+       return solution;
204 209
     }
205 210
 
206 211
 }

+ 224
- 0
be-graphes-algos/src/test/java/OptiTest.java View File

@@ -0,0 +1,224 @@
1
+import org.insa.graphs.model.*;
2
+import org.insa.graphs.algorithm.*;
3
+import org.insa.graphs.algorithm.shortestpath.*;
4
+import static org.junit.Assert.assertEquals;
5
+import static org.junit.Assert.assertFalse;
6
+import static org.junit.Assert.assertTrue;
7
+
8
+import java.io.BufferedInputStream;
9
+import java.io.DataInputStream;
10
+import java.io.FileInputStream;
11
+import java.io.IOException;
12
+import java.util.ArrayList;
13
+import java.util.Arrays;
14
+import java.util.Collection;
15
+import java.util.Collections;
16
+import org.insa.graphs.model.Arc;
17
+import org.insa.graphs.model.Graph;
18
+import org.insa.graphs.model.Node;
19
+import org.insa.graphs.model.Point;
20
+import org.insa.graphs.model.Path;
21
+import org.insa.graphs.model.RoadInformation;
22
+import org.insa.graphs.model.RoadInformation.RoadType;
23
+import org.insa.graphs.model.io.BinaryGraphReader;
24
+import org.insa.graphs.model.io.GraphReader;
25
+import org.junit.BeforeClass;
26
+import org.junit.Test;
27
+import org.junit.Assume;
28
+import org.junit.Before;
29
+import org.junit.Test;
30
+import org.junit.runner.RunWith;
31
+import org.junit.runners.Parameterized;
32
+import org.junit.runners.Parameterized.Parameter;
33
+import org.junit.runners.Parameterized.Parameters;
34
+
35
+@RunWith(Parameterized.class)
36
+public class OptiTest {
37
+	private static Graph graph1;
38
+	private static Graph graph2;
39
+	private static Graph graph3;
40
+	
41
+	
42
+    
43
+    @Parameters
44
+    public static Collection<ShortestPathData> data() {
45
+    	//**************************************************************
46
+    	//**************************************************************
47
+    	//CREATION DU GRAPHE DE LA CARTE HAUTE-GARONNE
48
+        final String mapName1 = "C:\\Users\\nmouk\\Documents\\GIT\\MAPS\\haute-garonne\\europe\\france\\haute-garonne.mapgr" ;
49
+        try {
50
+        	final FileInputStream F1= new FileInputStream(mapName1);
51
+        }
52
+        catch(Exception e) {System.out.println("Fichier non existant!");}
53
+        // Create a graph reader.
54
+        try {
55
+        final GraphReader reader = new BinaryGraphReader(
56
+                new DataInputStream(new BufferedInputStream(new FileInputStream(mapName1))));
57
+        
58
+        
59
+        // Small graph use for tests
60
+        graph1 = reader.read();
61
+        }
62
+        catch(Exception e) {System.out.println("Fichier non lu!");}
63
+      //******************************************************************
64
+      //******************************************************************
65
+      //CREATION DU GRAPHE DE LA CARTE CARRE
66
+        final String mapName2 = "C:\\Users\\nmouk\\Documents\\GIT\\MAPS\\carre\\extras\\carre.mapgr" ;
67
+       
68
+        try {
69
+        	final FileInputStream F2= new FileInputStream(mapName2);
70
+        }
71
+        catch(Exception e) {System.out.println("Fichier non existant!");}
72
+        // Create a graph reader.
73
+        try {
74
+        final GraphReader reader = new BinaryGraphReader(
75
+                new DataInputStream(new BufferedInputStream(new FileInputStream(mapName2))));
76
+        
77
+        
78
+        // Small graph use for tests
79
+        graph2 = reader.read();
80
+        }
81
+        catch(Exception e) {System.out.println("Fichier non lu!");}
82
+        
83
+        //******************************************************************
84
+        //******************************************************************
85
+        //CREATION DU GRAPHE DE LA CARTE POLYNESIE FRANCAISE
86
+          final String mapName3 = "C:\\Users\\nmouk\\Documents\\GIT\\MAPS\\french-polynesia\\oceania\\french-polynesia.mapgr" ;
87
+         
88
+          try {
89
+          	final FileInputStream F3= new FileInputStream(mapName2);
90
+          }
91
+          catch(Exception e) {System.out.println("Fichier non existant!");}
92
+          // Create a graph reader.
93
+          try {
94
+          final GraphReader reader = new BinaryGraphReader(
95
+                  new DataInputStream(new BufferedInputStream(new FileInputStream(mapName3))));
96
+          
97
+          
98
+          // Small graph use for tests
99
+          graph3 = reader.read();
100
+          }
101
+          catch(Exception e) {System.out.println("Fichier non lu!");}
102
+          
103
+          
104
+        
105
+    	Collection<ShortestPathData> objects = new ArrayList<>();
106
+    	
107
+    	//***************CARTE HAUTE-GARONNE****************************
108
+    	//**************************************************************
109
+    	//CHEMIN NUL AVEC 2 MODES DE DEPLACEMENTS
110
+    	objects.add(new ShortestPathData(graph1, graph1.get(10991),graph1.get(10991),ArcInspectorFactory.getAllFilters().get(0)));
111
+    	objects.add(new ShortestPathData(graph1, graph1.get(10991),graph1.get(10991),ArcInspectorFactory.getAllFilters().get(2)));
112
+    	//CHEMIN AVEC 3 MODES DE DEPLACEMENTS
113
+    	objects.add(new ShortestPathData(graph1, graph1.get(10991),graph1.get(10991),ArcInspectorFactory.getAllFilters().get(0)));
114
+    	objects.add(new ShortestPathData(graph1, graph1.get(10991),graph1.get(10991),ArcInspectorFactory.getAllFilters().get(2)));
115
+    	//***************CARTE CARRE************************************
116
+    	//**************************************************************
117
+    	//CHEMIN NUL AVEC 2 MODES DE DEPLACEMENTS
118
+    	objects.add(new ShortestPathData(graph2, graph2.get(19),graph2.get(19),ArcInspectorFactory.getAllFilters().get(0)));
119
+    	objects.add(new ShortestPathData(graph2, graph2.get(19),graph2.get(19),ArcInspectorFactory.getAllFilters().get(2)));
120
+    	//CHEMIN AVEC 2 MODES DE DEPLACEMENTS
121
+    	objects.add(new ShortestPathData(graph2, graph2.get(19),graph2.get(5),ArcInspectorFactory.getAllFilters().get(0)));
122
+    	objects.add(new ShortestPathData(graph2, graph2.get(19),graph2.get(5),ArcInspectorFactory.getAllFilters().get(2)));
123
+    	objects.add(new ShortestPathData(graph2, graph2.get(19),graph2.get(5),ArcInspectorFactory.getAllFilters().get(1)));
124
+    	
125
+    	
126
+    	
127
+    	//*************CARTE POLYNESIE FRANCAISE**********************
128
+    	//************************************************************
129
+    	//CHEMIN IMPOSSIBLE AVEC 2 MODES DE DEPLACEMENTS
130
+    	objects.add(new ShortestPathData(graph3, graph3.get(8302),graph3.get(4904),ArcInspectorFactory.getAllFilters().get(0)));
131
+    	objects.add(new ShortestPathData(graph3, graph3.get(8302),graph3.get(4904),ArcInspectorFactory.getAllFilters().get(2)));
132
+    return objects;
133
+    }
134
+    
135
+    
136
+    @Parameter
137
+    public ShortestPathData parameter;
138
+    
139
+    
140
+    @Test
141
+    public void testCheminNul()
142
+    {	//Shortest path, all roads allowed
143
+    	Assume.assumeTrue(parameter.getOrigin().getId()==parameter.getDestination().getId() && parameter.getGraph().getMapId() != graph3.getMapId());
144
+    	ShortestPathAlgorithm Dijkstra= new DijkstraAlgorithm(parameter);
145
+    	ShortestPathAlgorithm BF= new BellmanFordAlgorithm(parameter);
146
+    	DijkstraAlgorithm Astar= new AStarAlgorithm(parameter);
147
+    	assertEquals(Dijkstra.run().isFeasible(),BF.run().isFeasible());
148
+    	assertEquals(Astar.run().isFeasible(),BF.run().isFeasible());
149
+    }
150
+    
151
+   
152
+    @Test
153
+    public void testCheminCourt()
154
+    {
155
+    	Assume.assumeTrue(parameter.getOrigin().getId()!=parameter.getDestination().getId() && parameter.getGraph().getMapId() != graph3.getMapId());
156
+    	ShortestPathAlgorithm Dijkstra= new DijkstraAlgorithm(parameter);
157
+    	ShortestPathAlgorithm BF= new BellmanFordAlgorithm(parameter);
158
+    	DijkstraAlgorithm Astar= new AStarAlgorithm(parameter);
159
+    	assertEquals(Dijkstra.run().getPath().getLength(), BF.run().getPath().getLength(),0.01);
160
+    	assertEquals(Astar.run().getPath().getLength(),BF.run().getPath().getLength(),0.01);
161
+    	assertEquals(Dijkstra.run().getPath().getMinimumTravelTime(), BF.run().getPath().getMinimumTravelTime(),0.01);
162
+    	assertEquals(Astar.run().getPath().getMinimumTravelTime(),BF.run().getPath().getMinimumTravelTime(),0.01);
163
+    	
164
+    }
165
+    
166
+    @Test
167
+    public void testCheminImpossible()
168
+    {	//Shortest path, all roads allowed
169
+    	Assume.assumeTrue(parameter.getGraph().getMapId() == graph3.getMapId());
170
+    	ShortestPathAlgorithm Dijkstra= new DijkstraAlgorithm(parameter);
171
+    	ShortestPathAlgorithm BF= new BellmanFordAlgorithm(parameter);
172
+    	DijkstraAlgorithm Astar= new AStarAlgorithm(parameter);
173
+    	assertEquals(Dijkstra.run().isFeasible(),BF.run().isFeasible());
174
+    	assertEquals(Astar.run().isFeasible(),BF.run().isFeasible());
175
+    }
176
+   
177
+    /*
178
+    @Test
179
+    public void testCheminLong()
180
+    {
181
+    	ShortestPathData data1=new ShortestPathData(graph, longPath.getOrigin(),longPath.getDestination(),ArcInspectorFactory.getAllFilters().get(0));
182
+    	ShortestPathAlgorithm Dijkstra= new DijkstraAlgorithm(data1);
183
+    	ShortestPathAlgorithm BF= new BellmanFordAlgorithm(data1);
184
+    	DijkstraAlgorithm Astar= new AStarAlgorithm(data1);
185
+    	assertEquals(Dijkstra.run().toString(),BF.run().toString());
186
+    	assertEquals(Astar.run().toString(),BF.run().toString());
187
+    }
188
+    
189
+    @Test
190
+    public void testgrandeBoucle()
191
+    {
192
+    	ShortestPathData data1=new ShortestPathData(graph, loopPath.getOrigin(),loopPath.getDestination(),ArcInspectorFactory.getAllFilters().get(0));
193
+    	ShortestPathAlgorithm Dijkstra= new DijkstraAlgorithm(data1);
194
+    	ShortestPathAlgorithm BF= new BellmanFordAlgorithm(data1);
195
+    	DijkstraAlgorithm Astar= new AStarAlgorithm(data1);
196
+    	assertEquals(Dijkstra.run().toString(),BF.run().toString());
197
+    	assertEquals(Astar.run().toString(),BF.run().toString());
198
+    }
199
+    
200
+    @Test
201
+    public void testBoucle()
202
+    {
203
+    	ShortestPathData data1=new ShortestPathData(graph, longLoopPath.getOrigin(),longLoopPath.getDestination(),ArcInspectorFactory.getAllFilters().get(0));
204
+    	ShortestPathAlgorithm Dijkstra= new DijkstraAlgorithm(data1);
205
+    	ShortestPathAlgorithm BF= new BellmanFordAlgorithm(data1);
206
+    	DijkstraAlgorithm Astar= new AStarAlgorithm(data1);
207
+    	assertEquals(Dijkstra.run().toString(),BF.run().toString());
208
+    	assertEquals(Astar.run().toString(),BF.run().toString());
209
+    }
210
+    
211
+    @Test
212
+    public void Testinvalide()
213
+    {
214
+    	ShortestPathData data1=new ShortestPathData(graph, nodes[0],nodes[5],ArcInspectorFactory.getAllFilters().get(0));
215
+    	ShortestPathAlgorithm Dijkstra= new DijkstraAlgorithm(data1);
216
+    	ShortestPathAlgorithm BF= new BellmanFordAlgorithm(data1);
217
+    	DijkstraAlgorithm Astar= new AStarAlgorithm(data1);
218
+    	assertEquals(Dijkstra.run().toString(),BF.run().toString());
219
+    	assertEquals(Astar.run().toString(),BF.run().toString());
220
+    }
221
+    
222
+    */
223
+    
224
+}

+ 1
- 0
be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/PriorityQueueTest.java View File

@@ -19,6 +19,7 @@ import org.junit.runners.Parameterized;
19 19
 import org.junit.runners.Parameterized.Parameter;
20 20
 import org.junit.runners.Parameterized.Parameters;
21 21
 
22
+
22 23
 @RunWith(Parameterized.class)
23 24
 public abstract class PriorityQueueTest {
24 25
 

BIN
be-graphes-algos/target/classes/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.class View File


BIN
be-graphes-algos/target/classes/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.class View File


BIN
be-graphes-algos/target/test-classes/org/insa/graphs/algorithm/utils/PriorityQueueTest$MutableInteger.class View File


BIN
be-graphes-algos/target/test-classes/org/insa/graphs/algorithm/utils/PriorityQueueTest$TestParameters.class View File


BIN
be-graphes-algos/target/test-classes/org/insa/graphs/algorithm/utils/PriorityQueueTest.class View File


+ 2
- 0
be-graphes-model/src/test/java/org/insa/graphes/model/PathTest.java View File

@@ -86,6 +86,8 @@ public class PathTest {
86 86
     public void testImmutability() {
87 87
         emptyPath.getArcs().add(a2b);
88 88
     }
89
+    
90
+
89 91
 
90 92
     @Test
91 93
     public void testIsEmpty() {

BIN
be-graphes-model/target/test-classes/org/insa/graphes/model/PathTest.class View File


Loading…
Cancel
Save