Browse Source

Ajout algo A*

Adrien Barbanson 2 years ago
parent
commit
ac8a04c154

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

@@ -1,9 +1,33 @@
1 1
 package org.insa.graphs.algorithm.shortestpath;
2 2
 
3
+import org.insa.graphs.algorithm.AbstractInputData.Mode;
4
+import org.insa.graphs.model.LabelStar;
5
+import org.insa.graphs.model.Node;
6
+
3 7
 public class AStarAlgorithm extends DijkstraAlgorithm {
4 8
 
5 9
     public AStarAlgorithm(ShortestPathData data) {
6 10
         super(data);
7 11
     }
12
+    
13
+    @Override
14
+    protected void initAlgo() {
15
+    	
16
+        // Dijkstra guidée init
17
+        this.labels = new LabelStar[data.getGraph().size()];
18
+        
19
+        if(data.getMode() == Mode.TIME) {
20
+        	//time based, need to set LabelStar with maxspeed
21
+            for(Node node : data.getGraph().getNodes()) {
22
+            	labels[node.getId()] = new LabelStar(node, getInputData().getDestination(), data.getGraph().getGraphInformation().getMaximumSpeed());
23
+            }
24
+        }
25
+        else {
26
+            for(Node node : data.getGraph().getNodes()) {
27
+            	labels[node.getId()] = new LabelStar(node, getInputData().getDestination());
28
+            }
29
+        }
30
+
31
+    }
8 32
 
9 33
 }

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

@@ -12,16 +12,27 @@ import org.insa.graphs.model.Node;
12 12
 import org.insa.graphs.model.Path;
13 13
 
14 14
 public class DijkstraAlgorithm extends ShortestPathAlgorithm {
15
+	
16
+	
17
+	protected Label[] labels;
15 18
 
16 19
     public DijkstraAlgorithm(ShortestPathData data) {
17 20
         super(data);
18 21
     }
19 22
 
23
+    protected void initAlgo() {
24
+        // Dijkstra Init
25
+        this.labels = new Label[data.getGraph().size()];
26
+        for(Node node : data.getGraph().getNodes()) {
27
+        	labels[node.getId()] = new Label(node);
28
+        }
29
+    }
30
+    
20 31
     @Override
21 32
     protected ShortestPathSolution doRun() {
22 33
         final ShortestPathData data = getInputData();
23 34
         
24
-        int numberOfNodes =  data.getGraph().size();
35
+        initAlgo();
25 36
         
26 37
         double shortestCostToDestination = Double.POSITIVE_INFINITY;
27 38
         
@@ -29,13 +40,6 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
29 40
         // useful
30 41
         double lastMarkedNodeCost        = 0; 
31 42
         
32
-        // Dijkstra Init
33
-        Label[] labels = new Label[numberOfNodes];
34
-        
35
-        
36
-        for(Node node : data.getGraph().getNodes()) {
37
-        	labels[node.getId()] = new Label(node);
38
-        }
39 43
         
40 44
         // Let's set the origin cost at 0
41 45
         labels[data.getOrigin().getId()].setNewCost(null, 0d);

+ 11
- 6
be-graphes-model/src/main/java/org/insa/graphs/model/Label.java View File

@@ -2,10 +2,10 @@ package org.insa.graphs.model;
2 2
 
3 3
 public class Label implements Comparable<Label>{
4 4
 
5
-		private Node current;
6
-		private boolean marked;
7
-		private double cost;
8
-		private Arc father;
5
+		protected Node current;
6
+		protected boolean marked;
7
+		protected double cost;
8
+		protected Arc father;
9 9
 		
10 10
 		
11 11
 		public Label(Node current) {
@@ -41,9 +41,14 @@ public class Label implements Comparable<Label>{
41 41
 			return this.father;
42 42
 		}
43 43
 
44
-		@Override
44
+		public double getMinCostToDestination() {
45
+			return 0; // not used in Label but only in LabelStar
46
+		}
47
+		
48
+		// We calculate the cost to reach destination only if needed
45 49
 		public int compareTo(Label arg0) {
46
-			return Double.compare(this.cost, arg0.cost);
50
+			//In this case, the comparison is based on cost to reach the node + min cost to reach dest
51
+			return Double.compare(this.cost + getMinCostToDestination(), arg0.cost + arg0.getMinCostToDestination());
47 52
 		}
48 53
 	
49 54
 }

+ 39
- 0
be-graphes-model/src/main/java/org/insa/graphs/model/LabelStar.java View File

@@ -0,0 +1,39 @@
1
+package org.insa.graphs.model;
2
+
3
+public class LabelStar extends Label {
4
+
5
+	protected double minCostToDestination = -1;
6
+	protected Node destination;
7
+	protected double maxSpeedPossible = -1;
8
+	
9
+	public LabelStar(Node current, Node destination) {
10
+		super(current);
11
+		this.destination = destination;
12
+	}	
13
+	
14
+	public LabelStar(Node current, Node destination, double maxSpeed) {
15
+		super(current);
16
+		this.destination = destination;
17
+		this.maxSpeedPossible = maxSpeed;
18
+	}
19
+
20
+	public double getMinCostToDestination() {
21
+		if(this.minCostToDestination < 0) {
22
+			//need to calculate
23
+			
24
+			if(this.maxSpeedPossible > 0) {
25
+				//if maxspeedpossible is initialized, then we use time shortest
26
+				this.minCostToDestination = this.current.getPoint().distanceTo(this.destination.getPoint()) / this.maxSpeedPossible;
27
+			}
28
+			else {
29
+				//if maxspeedpossible is not initialized, then we use shortest path
30
+				this.minCostToDestination = this.current.getPoint().distanceTo(this.destination.getPoint());
31
+
32
+			}
33
+			
34
+			
35
+		}
36
+		return this.minCostToDestination;
37
+	}
38
+	
39
+}

Loading…
Cancel
Save