A* fonctionnel

This commit is contained in:
Guillaume Vincent 2020-06-01 16:01:36 +02:00
parent d7cca9ceee
commit dc2a6bca20
3 changed files with 168 additions and 1 deletions

View file

@ -1,9 +1,109 @@
package org.insa.graphs.algorithm.shortestpath;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.insa.graphs.algorithm.AbstractSolution.Status;
import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
public class AStarAlgorithm extends DijkstraAlgorithm {
public AStarAlgorithm(ShortestPathData data) {
super(data);
}
@Override
protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData();
ShortestPathSolution solution = null;
// TODO:
// Données
Graph graph = data.getGraph();
List<Node> nodes = graph.getNodes();
BinaryHeap<Label> tas = new BinaryHeap<Label>();
ArrayList<LabelStar> labels = new ArrayList<LabelStar>();
boolean notFinished = true;
for (Node n : nodes) {
labels.add(new LabelStar(n,null,data));
}
Node origine = data.getOrigin();
notifyOriginProcessed(origine);
labels.get(origine.getId()).setCost(0);
tas.insert(labels.get(origine.getId()));
// Algorithme
LabelStar currentLabel;
while (tas.isEmpty() != true && notFinished)
{
currentLabel = (LabelStar)tas.findMin();
tas.remove((Label)currentLabel);
Node currentNode = currentLabel.getCurrentNode();
if (currentNode == data.getDestination())
{
notFinished = false;
}
labels.get(currentNode.getId()).setMark(true);
for (Arc newArc : currentNode.getSuccessors())
{
if (data.isAllowed(newArc))
{
LabelStar destLabel = labels.get(newArc.getDestination().getId());
if (!destLabel.getMark())
{
Node destNode = destLabel.getCurrentNode();
notifyNodeReached(destNode);
Double currentCost = currentLabel.getCost();
Double destCost = destLabel.getCost();
Double arcCost = data.getCost(newArc);
if (destCost > currentCost+ arcCost)
{
if (destCost != Double.MAX_VALUE)
{
tas.remove((Label)destLabel);
}
destLabel.setCost(currentCost+arcCost);
destLabel.setFatherArc(newArc);
tas.insert((Label)destLabel);
labels.set(destNode.getId(),destLabel);
}
}
}
}
notifyNodeMarked(currentNode);
}
Arc currentArc=labels.get(data.getDestination().getId()).getFatherArc();
if (currentArc == null) {
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
}
else {
notifyDestinationReached(data.getDestination());
ArrayList<Arc> arcs = new ArrayList<>();
while (currentArc != null) {
arcs.add(currentArc);
Arc newArc=labels.get(currentArc.getOrigin().getId()).getFatherArc();
currentArc = newArc;
}
Collections.reverse(arcs);
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
}
return solution;
}
}

View file

@ -27,6 +27,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
List<Node> nodes = graph.getNodes();
BinaryHeap<Label> tas = new BinaryHeap<Label>();
ArrayList<Label> labels = new ArrayList<Label>();
boolean notFinished = true;
for (Node n : nodes) {
@ -41,11 +42,15 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// Algorithme
Label currentLabel;
while (tas.isEmpty() != true)
while (tas.isEmpty() != true && notFinished)
{
currentLabel = tas.findMin();
tas.remove(currentLabel);
Node currentNode = currentLabel.getCurrentNode();
if (currentNode == data.getDestination())
{
notFinished = false;
}
labels.get(currentNode.getId()).setMark(true);
for (Arc newArc : currentNode.getSuccessors())
{

View file

@ -0,0 +1,62 @@
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.*;
import org.insa.graphs.algorithm.AbstractInputData.Mode;
public class LabelStar extends Label
{
private double estimatedCost;
public LabelStar(Node cN, Arc fA, ShortestPathData data)
{
super(cN,fA);
if (data.getMode() == Mode.LENGTH)
{
this.estimatedCost = cN.getPoint().distanceTo(data.getDestination().getPoint());
}
if (data.getMode() == Mode.TIME)
{
int graphSpeed = data.getGraph().getGraphInformation().getMaximumSpeed();
int dataSpeed = data.getMaximumSpeed();
int currentSpeed = 130;
if (graphSpeed != -1 && dataSpeed == -1)
{
currentSpeed = graphSpeed;
}
if (graphSpeed == -1 && dataSpeed != -1)
{
currentSpeed = dataSpeed;
}
if (graphSpeed != -1 && dataSpeed != -1)
{
if (graphSpeed < dataSpeed) {currentSpeed = graphSpeed;}
else {currentSpeed = dataSpeed;}
}
this.estimatedCost = 3.6*cN.getPoint().distanceTo(data.getDestination().getPoint())/((double) currentSpeed);
}
}
public double getEstimatedCost()
{
return this.estimatedCost;
}
public void setEstimatedCost(double coutestime)
{
this.estimatedCost = coutestime;
}
public double getTotalCost()
{
return this.getCost()+this.estimatedCost;
}
public int compareTo(Label other)
{
int compare = Double.compare(getTotalCost(), ((LabelStar)other).getTotalCost());
int compareEstimate = Double.compare(getEstimatedCost(), ((LabelStar)other).getEstimatedCost());
if (compare == 0) {return compareEstimate;}
else {return compare;}
}
}