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;
|
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 class AStarAlgorithm extends DijkstraAlgorithm {
|
||||||
|
|
||||||
public AStarAlgorithm(ShortestPathData data) {
|
public AStarAlgorithm(ShortestPathData data) {
|
||||||
super(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 {
|
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
|
|
||||||
|
protected Label[] labels;
|
||||||
|
|
||||||
public DijkstraAlgorithm(ShortestPathData data) {
|
public DijkstraAlgorithm(ShortestPathData data) {
|
||||||
super(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
|
@Override
|
||||||
protected ShortestPathSolution doRun() {
|
protected ShortestPathSolution doRun() {
|
||||||
final ShortestPathData data = getInputData();
|
final ShortestPathData data = getInputData();
|
||||||
|
|
||||||
int numberOfNodes = data.getGraph().size();
|
initAlgo();
|
||||||
|
|
||||||
double shortestCostToDestination = Double.POSITIVE_INFINITY;
|
double shortestCostToDestination = Double.POSITIVE_INFINITY;
|
||||||
|
|
||||||
|
@ -29,13 +40,6 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
// useful
|
// useful
|
||||||
double lastMarkedNodeCost = 0;
|
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
|
// Let's set the origin cost at 0
|
||||||
labels[data.getOrigin().getId()].setNewCost(null, 0d);
|
labels[data.getOrigin().getId()].setNewCost(null, 0d);
|
||||||
|
|
|
@ -2,10 +2,10 @@ package org.insa.graphs.model;
|
||||||
|
|
||||||
public class Label implements Comparable<Label>{
|
public class Label implements Comparable<Label>{
|
||||||
|
|
||||||
private Node current;
|
protected Node current;
|
||||||
private boolean marked;
|
protected boolean marked;
|
||||||
private double cost;
|
protected double cost;
|
||||||
private Arc father;
|
protected Arc father;
|
||||||
|
|
||||||
|
|
||||||
public Label(Node current) {
|
public Label(Node current) {
|
||||||
|
@ -41,9 +41,14 @@ public class Label implements Comparable<Label>{
|
||||||
return this.father;
|
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) {
|
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