dijkstra
This commit is contained in:
		
							parent
							
								
									ce9743650b
								
							
						
					
					
						commit
						e4695e0ae7
					
				
					 4 changed files with 51 additions and 8 deletions
				
			
		|  | @ -1,9 +1,22 @@ | ||||||
| package org.insa.graphs.algorithm.shortestpath; | package org.insa.graphs.algorithm.shortestpath; | ||||||
| 
 | 
 | ||||||
|  | import java.util.ArrayList; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| public class AStarAlgorithm extends DijkstraAlgorithm { | public class AStarAlgorithm extends DijkstraAlgorithm { | ||||||
| 
 | 
 | ||||||
|     public AStarAlgorithm(ShortestPathData data) { |     public AStarAlgorithm(ShortestPathData data) { | ||||||
|         super(data); |         super(data); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /*on redéfinit l'initialisation */ | ||||||
|  |     void init(ArrayList<Label> tab){ | ||||||
|  |         /*nombre de nodes du graphe de data */ | ||||||
|  |         int n=data.getGraph().size(); | ||||||
|  |         /*on remplit tab */ | ||||||
|  |         for (int i=0;i<n;i++){ | ||||||
|  |             tab.add(new LabelStar(data.getGraph().get(i), false, Double.POSITIVE_INFINITY , null,0.0)); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -11,6 +11,15 @@ import org.insa.graphs.algorithm.utils.BinaryHeap; | ||||||
| 
 | 
 | ||||||
| public class DijkstraAlgorithm extends ShortestPathAlgorithm { | public class DijkstraAlgorithm extends ShortestPathAlgorithm { | ||||||
| 
 | 
 | ||||||
|  |     /*initialisation */ | ||||||
|  |     void init(ArrayList<Label> tab){ | ||||||
|  |         /*nombre de nodes du graphe de data */ | ||||||
|  |         int n=data.getGraph().size(); | ||||||
|  |         /*on remplit tab */ | ||||||
|  |         for (int i=0;i<n;i++){ | ||||||
|  |             tab.add(new Label(data.getGraph().get(i), false, Double.POSITIVE_INFINITY , null)); | ||||||
|  |         } | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     public DijkstraAlgorithm(ShortestPathData data) { |     public DijkstraAlgorithm(ShortestPathData data) { | ||||||
|         super(data); |         super(data); | ||||||
|  | @ -19,18 +28,12 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { | ||||||
|     @Override |     @Override | ||||||
|     protected ShortestPathSolution doRun() { |     protected ShortestPathSolution doRun() { | ||||||
|         final ShortestPathData data = getInputData(); |         final ShortestPathData data = getInputData(); | ||||||
|         /*nombre de nodes du graphe de data */ |  | ||||||
|         int n=data.getGraph().size(); |  | ||||||
|         ArrayList<Label> tab=new ArrayList<Label>(); |         ArrayList<Label> tab=new ArrayList<Label>(); | ||||||
|         BinaryHeap<Label> tas=new BinaryHeap<Label>(); |         BinaryHeap<Label> tas=new BinaryHeap<Label>(); | ||||||
|         ShortestPathSolution solution = null; |         ShortestPathSolution solution = null; | ||||||
|         /*initialisation */ |         init(tab); | ||||||
|         for (int i=0;i<n;i++){ |  | ||||||
|             tab.add(new Label(data.getGraph().get(i), false, Double.POSITIVE_INFINITY , null)); |  | ||||||
|         } |  | ||||||
|         Label label_origine = tab.get(data.getOrigin().getId()); |         Label label_origine = tab.get(data.getOrigin().getId()); | ||||||
|         label_origine.setCoutmin(0); |         label_origine.setCoutmin(0); | ||||||
| 
 |  | ||||||
|         Label label_dest =tab.get(data.getDestination().getId()); |         Label label_dest =tab.get(data.getDestination().getId()); | ||||||
|         /*insertion de label origine dans le tas */ |         /*insertion de label origine dans le tas */ | ||||||
|         tas.insert(label_origine); |         tas.insert(label_origine); | ||||||
|  |  | ||||||
|  | @ -32,7 +32,10 @@ public class Label implements Comparable<Label>{ | ||||||
|     public boolean getMarque() { return this.marque;} |     public boolean getMarque() { return this.marque;} | ||||||
|     /*récupérer le père */ |     /*récupérer le père */ | ||||||
|     public Arc getPere() { return this.pere;} |     public Arc getPere() { return this.pere;} | ||||||
|  |     /*récupérer le cout total (modifié dans LabelStar) */ | ||||||
|  |     public double getTotalCost() { return this.coutmin;} | ||||||
| 
 | 
 | ||||||
|  |     /*setters */ | ||||||
|     public void setMarque(boolean m) {this.marque=m;} |     public void setMarque(boolean m) {this.marque=m;} | ||||||
|     public void setCoutmin(double c) {this.coutmin=c;} |     public void setCoutmin(double c) {this.coutmin=c;} | ||||||
|     public void setPere(Arc p) {this.pere=p;} |     public void setPere(Arc p) {this.pere=p;} | ||||||
|  | @ -40,6 +43,6 @@ public class Label implements Comparable<Label>{ | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     public int compareTo(Label other){ |     public int compareTo(Label other){ | ||||||
|         return Double.compare(getCost(), other.getCost()); |         return Double.compare(getTotalCost(), other.getTotalCost()); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | @ -0,0 +1,24 @@ | ||||||
|  | package org.insa.graphs.algorithm.shortestpath; | ||||||
|  | import org.insa.graphs.model.Node; | ||||||
|  | import org.insa.graphs.model.Arc; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | public class LabelStar extends Label { | ||||||
|  | 
 | ||||||
|  |     /*cout estimé*/ | ||||||
|  |     private double coutest; | ||||||
|  | 
 | ||||||
|  |     /*constructeur */ | ||||||
|  |     public LabelStar(Node sommet, boolean marque, double coutmin, Arc pere, double coutest){ | ||||||
|  | 
 | ||||||
|  |         super(sommet, marque, coutmin, pere); | ||||||
|  |         this.coutest=coutest; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /*méthodes */ | ||||||
|  |     /*récupérer le cout total */ | ||||||
|  |     public double getTotalCost() { return this.getCost()+this.coutest;} | ||||||
|  |      | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
		Loading…
	
		Reference in a new issue