Ajout algo A*
This commit is contained in:
parent
5cb458cc25
commit
ac8a04c154
4 changed files with 86 additions and 14 deletions
|
@ -1,9 +1,33 @@
|
|||
package org.insa.graphs.algorithm.shortestpath;
|
||||
|
||||
import org.insa.graphs.algorithm.AbstractInputData.Mode;
|
||||
import org.insa.graphs.model.LabelStar;
|
||||
import org.insa.graphs.model.Node;
|
||||
|
||||
public class AStarAlgorithm extends DijkstraAlgorithm {
|
||||
|
||||
public AStarAlgorithm(ShortestPathData data) {
|
||||
super(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initAlgo() {
|
||||
|
||||
// Dijkstra guidée init
|
||||
this.labels = new LabelStar[data.getGraph().size()];
|
||||
|
||||
if(data.getMode() == Mode.TIME) {
|
||||
//time based, need to set LabelStar with maxspeed
|
||||
for(Node node : data.getGraph().getNodes()) {
|
||||
labels[node.getId()] = new LabelStar(node, getInputData().getDestination(), data.getGraph().getGraphInformation().getMaximumSpeed());
|
||||
}
|
||||
}
|
||||
else {
|
||||
for(Node node : data.getGraph().getNodes()) {
|
||||
labels[node.getId()] = new LabelStar(node, getInputData().getDestination());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -13,15 +13,26 @@ import org.insa.graphs.model.Path;
|
|||
|
||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||
|
||||
|
||||
protected Label[] labels;
|
||||
|
||||
public DijkstraAlgorithm(ShortestPathData data) {
|
||||
super(data);
|
||||
}
|
||||
|
||||
protected void initAlgo() {
|
||||
// Dijkstra Init
|
||||
this.labels = new Label[data.getGraph().size()];
|
||||
for(Node node : data.getGraph().getNodes()) {
|
||||
labels[node.getId()] = new Label(node);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ShortestPathSolution doRun() {
|
||||
final ShortestPathData data = getInputData();
|
||||
|
||||
int numberOfNodes = data.getGraph().size();
|
||||
initAlgo();
|
||||
|
||||
double shortestCostToDestination = Double.POSITIVE_INFINITY;
|
||||
|
||||
|
@ -29,13 +40,6 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
// useful
|
||||
double lastMarkedNodeCost = 0;
|
||||
|
||||
// Dijkstra Init
|
||||
Label[] labels = new Label[numberOfNodes];
|
||||
|
||||
|
||||
for(Node node : data.getGraph().getNodes()) {
|
||||
labels[node.getId()] = new Label(node);
|
||||
}
|
||||
|
||||
// Let's set the origin cost at 0
|
||||
labels[data.getOrigin().getId()].setNewCost(null, 0d);
|
||||
|
|
|
@ -2,10 +2,10 @@ package org.insa.graphs.model;
|
|||
|
||||
public class Label implements Comparable<Label>{
|
||||
|
||||
private Node current;
|
||||
private boolean marked;
|
||||
private double cost;
|
||||
private Arc father;
|
||||
protected Node current;
|
||||
protected boolean marked;
|
||||
protected double cost;
|
||||
protected Arc father;
|
||||
|
||||
|
||||
public Label(Node current) {
|
||||
|
@ -41,9 +41,14 @@ public class Label implements Comparable<Label>{
|
|||
return this.father;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getMinCostToDestination() {
|
||||
return 0; // not used in Label but only in LabelStar
|
||||
}
|
||||
|
||||
// We calculate the cost to reach destination only if needed
|
||||
public int compareTo(Label arg0) {
|
||||
return Double.compare(this.cost, arg0.cost);
|
||||
//In this case, the comparison is based on cost to reach the node + min cost to reach dest
|
||||
return Double.compare(this.cost + getMinCostToDestination(), arg0.cost + arg0.getMinCostToDestination());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
package org.insa.graphs.model;
|
||||
|
||||
public class LabelStar extends Label {
|
||||
|
||||
protected double minCostToDestination = -1;
|
||||
protected Node destination;
|
||||
protected double maxSpeedPossible = -1;
|
||||
|
||||
public LabelStar(Node current, Node destination) {
|
||||
super(current);
|
||||
this.destination = destination;
|
||||
}
|
||||
|
||||
public LabelStar(Node current, Node destination, double maxSpeed) {
|
||||
super(current);
|
||||
this.destination = destination;
|
||||
this.maxSpeedPossible = maxSpeed;
|
||||
}
|
||||
|
||||
public double getMinCostToDestination() {
|
||||
if(this.minCostToDestination < 0) {
|
||||
//need to calculate
|
||||
|
||||
if(this.maxSpeedPossible > 0) {
|
||||
//if maxspeedpossible is initialized, then we use time shortest
|
||||
this.minCostToDestination = this.current.getPoint().distanceTo(this.destination.getPoint()) / this.maxSpeedPossible;
|
||||
}
|
||||
else {
|
||||
//if maxspeedpossible is not initialized, then we use shortest path
|
||||
this.minCostToDestination = this.current.getPoint().distanceTo(this.destination.getPoint());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return this.minCostToDestination;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue