Browse Source

Dijkstra v0.4, Label comparable (ahem)

Favary Pierre 2 years ago
parent
commit
a1784cc36c

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

@@ -5,7 +5,7 @@ import org.insa.graphs.algorithm.utils.BinaryHeap;
5 5
 import org.insa.graphs.model.Arc;
6 6
 import org.insa.graphs.model.Path;
7 7
 import java.util.ArrayList;
8
-import java.util.List;//trier tout ça
8
+import java.util.Collections;//trier tout ça
9 9
 
10 10
 public class DijkstraAlgorithm extends ShortestPathAlgorithm {
11 11
 	
@@ -18,56 +18,74 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
18 18
     protected ShortestPathSolution doRun() {
19 19
         final ShortestPathData data = getInputData();
20 20
         ShortestPathSolution solution = new ShortestPathSolution(data,Status.UNKNOWN);//modifié
21
-        // TODO:
22 21
         
23 22
         //initialisation
24
-        BinaryHeap<Integer> tas=new BinaryHeap<Integer>();//type comparable nécessaire (donc pas Arc)
23
+        BinaryHeap<Label> tas=new BinaryHeap<Label>();//les Label sont comparés via leurs coûts
25 24
         Label[] tablabel=new Label[data.getGraph().size()];
26 25
         for (int i=0;i<tablabel.length;i++) {
27
-        	tablabel[i]=new Label(data.getGraph().get(i),null,Integer.MAX_VALUE);//non marqué par défaut
26
+        	tablabel[i]=new Label(data.getGraph().get(i),null,Float.MAX_VALUE);//non marqué par défaut
28 27
         }//dans le tablabel[idnode] on peut retrouver node
29 28
         tablabel[data.getOrigin().getId()].cout=0;
30
-        tas.insert(data.getOrigin().getId());
31
-        
32
-        int nomark=tablabel.length;//existe sommets non marqués (ou à coût infini?)(pas optimal?)
29
+        tas.insert(tablabel[data.getOrigin().getId()]);
30
+                
31
+        int nomark=tablabel.length;//nombre de sommets non marqués (pas optimal?)
33 32
         int x;//id du node qu'on étudie
33
+        boolean arrive=false;//node de destination atteint ou pas
34
+        System.out.println("Initialisation OK");
34 35
         
35
-        if (data.getOrigin()==data.getDestination()) {
36
-        	//retourne un graphe avec juste un node si besoin de gérer ce cas particulier
37
-        	//(à vérifier, peut-être qu'une fois le reste fini ce ne sera pas nécessaire)
38
-        	Path chemin= new Path(data.getGraph(),data.getOrigin());
39
-            solution = new ShortestPathSolution(data,Status.OPTIMAL,chemin);
40
-            nomark=0;
36
+        //itérations
37
+        while (nomark>0 && !arrive){
38
+        	
39
+        	x=tas.deleteMin().sommet_courant.getId();
40
+        	
41
+        	if (x==data.getDestination().getId()) {
42
+        		arrive=true;//marche aussi si le départ et l'arrivée sont le même node
43
+        	}else {
44
+        		
45
+	        	tablabel[x].marque=true;
46
+	        	nomark--;
47
+	        	int y;
48
+	        	
49
+	        	for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {//mauvais parcours?
50
+	        		
51
+	        		y=arcy.getDestination().getId();
52
+	        		
53
+	        		if (!tablabel[y].marque) {
54
+	        			
55
+	        			if (tablabel[y].cout>tablabel[x].cout+(float)data.getCost(arcy)) {
56
+	        				
57
+	        				tablabel[y].cout=tablabel[x].cout+(float)data.getCost(arcy);
58
+	        				tablabel[y].pere=arcy;//ligne non dans le poly
59
+	        				
60
+	        				if (tablabel[y].cout!=Float.MAX_VALUE)
61
+	        					tas.remove(tablabel[y]);
62
+	        				//méthode à vérifier pour opérer update ou insert
63
+	        				tas.insert(tablabel[y]);
64
+	        				
65
+	        			}
66
+	        		}
67
+	             	//penser à gérer le cas "aucun chemin n'existe" (avec l'initialisation par défaut?)
68
+	        	}
69
+	        }
41 70
         }
71
+
72
+        System.out.println("Itérations OK");
42 73
         
43
-        //itérations
44
-        while (nomark>0){
45
-        	x=tas.deleteMin();
46
-        	tablabel[x].marque=true;
47
-        	nomark--;
48
-        	for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {
49
-        		if (!tablabel[arcy.getDestination().getId()].marque) {
50
-        			if (tablabel[arcy.getDestination().getId()].cout>tablabel[x].cout+(int)arcy.getMinimumTravelTime()) {
51
-        				tablabel[arcy.getDestination().getId()].cout=tablabel[x].cout+(int)arcy.getMinimumTravelTime();
52
-        				tablabel[arcy.getDestination().getId()].pere=arcy;//ligne non dans le poly
53
-        				try{
54
-        					tas.remove(arcy.getDestination().getId());
55
-        				} finally {//méthode *fortement douteuse* pour opérer update ou inssert selon si l'élément est déjà dans le tas ou pas
56
-        					tas.insert(arcy.getDestination().getId());
57
-        				}
58
-        			}//s'aider de l'algorithme de Bellman-Ford déjà fait
59
-        		}
60
-        	        	
61
-        	//penser à gérer le cas "aucun chemin n'existe" (avec l'initialisation par défaut?)
74
+        if (!arrive) {
75
+        		solution= new ShortestPathSolution(data,Status.INFEASIBLE);
76
+        }else {
77
+        	ArrayList<Arc> bonsarcs=new ArrayList<Arc>();
78
+        	Label labelact=tablabel[data.getDestination().getId()];
79
+        	while (labelact.pere!=null) {
80
+        		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?
84
+        	Collections.reverse(bonsarcs);
85
+        	//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
+        	solution = new ShortestPathSolution(data,Status.OPTIMAL,new Path(data.getGraph(),bonsarcs));
62 87
         	}
63
-        }
64
-        //créer le chemin solution grâce au tas (le tas nécessite un type comparable donc l'id des nodes)
65
-        List<Arc> bonsarcs=new ArrayList<Arc>();//pourquoi une erreur ici?
66
-        while (!tas.isEmpty()) {
67
-        	bonsarcs.add(tablabel[tas.deleteMin()].pere);//un node de plus qu'il n'y a d'arcs, vérifier ce que ça donne
68
-        }//est-ce que le cas "un seul node" marche bien en renvoyant un unique arc null?
69
-        Path chemin= new Path(data.getGraph(),bonsarcs);
70
-        solution = new ShortestPathSolution(data,Status.OPTIMAL,chemin);
88
+        System.out.println("Fin OK");
71 89
         return solution;
72 90
     }
73 91
 

+ 17
- 5
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.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 Label {
6
+public class Label implements Comparable<Label>{
7 7
 	
8 8
 	//quelles valeurs de protection?
9 9
 	protected Node sommet_courant;
@@ -12,21 +12,21 @@ public class Label {
12 12
 	public boolean marque;
13 13
 	
14 14
 	//valeur courante du plus court chemin, de l'origine au sommet
15
-	protected int cout;
15
+	protected float cout;
16 16
 	
17 17
 	//arc (permettant d'avoir le sommet) précédent sur le plus court chemin courant
18 18
 	protected Arc pere;
19 19
 	
20 20
 	
21 21
 	//constructeur
22
-	public Label(Node sommet,Arc padre, int prix) {
22
+	public Label(Node sommet,Arc padre, float prix) {
23 23
 		this.sommet_courant=sommet;
24 24
 		this.pere=padre;
25 25
 		this.cout=prix;
26 26
 		this.marque=false;//!\\ pas sûr que ce soit une bonne idée
27 27
 	}	
28 28
 	
29
-	public Label(Node sommet,Arc padre, int prix, boolean mark) {
29
+	public Label(Node sommet,Arc padre, float prix, boolean mark) {
30 30
 		this.sommet_courant=sommet;
31 31
 		this.pere=padre;
32 32
 		this.cout=prix;
@@ -34,7 +34,19 @@ public class Label {
34 34
 		
35 35
 	}
36 36
 	
37
-	public int getCost() {
37
+	public float getCost() {
38 38
 		return this.cout;
39 39
 	}
40
+	
41
+	//fonction de comparaison
42
+	public int compareTo(Label comp) {
43
+		float diff= this.cout-comp.cout;
44
+		if (diff>0) {
45
+			return 1;
46
+		}else if (diff<0) {
47
+			return -1;
48
+		}else {
49
+			return 0;
50
+		}
51
+	}
40 52
 }

Loading…
Cancel
Save