Browse Source

Dijkstra avec Observers

Favary Pierre 2 years ago
parent
commit
31de9cb5f5

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

@@ -16,6 +16,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
16 16
 
17 17
     @Override
18 18
     protected ShortestPathSolution doRun() {
19
+    	
19 20
         final ShortestPathData data = getInputData();
20 21
         ShortestPathSolution solution = new ShortestPathSolution(data,Status.UNKNOWN);//modifié
21 22
         
@@ -31,10 +32,9 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
31 32
         int nomark=tablabel.length;//nombre de sommets non marqués (pas optimal?)
32 33
         int x;//id du node qu'on étudie
33 34
         boolean arrive=false;//node de destination atteint ou pas
34
-        System.out.println("Initialisation OK");
35 35
         
36 36
         //itérations
37
-        while (nomark>0 && !arrive){
37
+        while (nomark>0 && !arrive && !tas.isEmpty()){
38 38
         	
39 39
         	x=tas.deleteMin().sommet_courant.getId();
40 40
         	
@@ -43,23 +43,25 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
43 43
         	}else {
44 44
         		
45 45
 	        	tablabel[x].marque=true;
46
+	        	this.notifyNodeMarked(tablabel[x].sommet_courant);
46 47
 	        	nomark--;
47 48
 	        	int y;
48 49
 	        	
49
-	        	for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {//mauvais parcours?
50
+	        	for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {
50 51
 	        		
51 52
 	        		y=arcy.getDestination().getId();
52 53
 	        		
53
-	        		if (!tablabel[y].marque) {
54
+	        		if (!tablabel[y].marque && data.isAllowed(arcy)) {//ligne 108 de l'excel d'avancement
54 55
 	        			
55 56
 	        			if (tablabel[y].cout>tablabel[x].cout+(float)data.getCost(arcy)) {
56 57
 	        				
57
-	        				if (tablabel[y].cout!=Float.MAX_VALUE)
58
+	        				if (tablabel[y].cout!=Float.MAX_VALUE) {
58 59
 	        					tas.remove(tablabel[y]);
60
+	        					this.notifyNodeReached(arcy.getDestination());
61
+	        				}
59 62
 	        				
60 63
 	        				tablabel[y].cout=tablabel[x].cout+(float)data.getCost(arcy);
61 64
 	        				tablabel[y].pere=arcy;//ligne non dans le poly
62
-	        				//méthode à vérifier pour opérer update ou insert (avec le if float.max plus haut)
63 65
 	        				tas.insert(tablabel[y]);
64 66
 	        				
65 67
 	        			}
@@ -68,24 +70,26 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
68 70
 	        	}
69 71
 	        }
70 72
         }
71
-
72
-        System.out.println("Itérations OK");
73 73
         
74 74
         if (!arrive) {
75 75
         		solution= new ShortestPathSolution(data,Status.INFEASIBLE);
76 76
         }else {
77
+        	
78
+        	this.notifyDestinationReached(data.getDestination());
79
+        	
77 80
         	ArrayList<Arc> bonsarcs=new ArrayList<Arc>();
78 81
         	Label labelact=tablabel[data.getDestination().getId()];
82
+        	
79 83
         	while (labelact.pere!=null) {
80 84
         		bonsarcs.add(labelact.pere);
81
-        		labelact=tablabel[labelact.pere.getOrigin().getId()];		        	
82
-		        	//à vérifier
83
-        		}//est-ce que le cas "un seul node" marche bien en renvoyant un unique arc null?
85
+        		labelact=tablabel[labelact.pere.getOrigin().getId()];
86
+        		}
87
+        	
84 88
         	Collections.reverse(bonsarcs);
85 89
         	//Path chemin= new Path(data.getGraph(),bonsarcs);//y'a t-il un retour si le chemin est infaisable? éviterait la condition précédente si oui
86 90
         	solution = new ShortestPathSolution(data,Status.OPTIMAL,new Path(data.getGraph(),bonsarcs));
87 91
         	}
88
-        System.out.println("Fin OK");
92
+        
89 93
         return solution;
90 94
     }
91 95
 

Loading…
Cancel
Save