Dijkstrafin

This commit is contained in:
Fu Boyu 2023-04-14 16:38:32 +02:00
parent cd0803a07b
commit e32fb51fa4
2 changed files with 14 additions and 22 deletions

View file

@ -22,36 +22,33 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
final ShortestPathData data = getInputData();
ShortestPathSolution solution = null;
// TODO:
Label currentLabel;
Graph graph = data.getGraph();
List<Node> nodes = graph.getNodes();
BinaryHeap<Label> tas = new BinaryHeap<Label>();
ArrayList<Label> labels = new ArrayList<Label>();
boolean notfini = true;
for (Node n : nodes) {
labels.add(new Label(n,null));
for (Node node : nodes) {
labels.add(new Label(node));
}
Node origin = data.getOrigin();
notifyOriginProcessed(origin);
labels.get(origin.getId()).setCost(0);
tas.insert(labels.get(origin.getId()));
Label currentLabel;
while (tas.isEmpty() != true && notfini)
{
currentLabel = tas.findMin();
tas.remove(currentLabel);
Node currentNode = currentLabel.getCurrentNode();
if (currentNode == data.getDestination())
{
if (currentNode == data.getDestination()){
notfini = false;
}
labels.get(currentNode.getId()).setMark(true);
for (Arc newArc : currentNode.getSuccessors())
{
if (data.isAllowed(newArc))
{
for (Arc newArc : currentNode.getSuccessors()){
if (data.isAllowed(newArc)){
Label destLabel = labels.get(newArc.getDestination().getId());
if (!destLabel.getMark())
{
@ -60,11 +57,8 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
Double currentCost = currentLabel.getCost();
Double destCost = destLabel.getCost();
Double arcCost = data.getCost(newArc);
if (destCost > currentCost+ arcCost)
{
if (destCost != Double.MAX_VALUE)
{
if (destCost > currentCost + arcCost) {
if (destCost != Double.MAX_VALUE) {
tas.remove(destLabel);
}
destLabel.setCost(currentCost+arcCost);
@ -84,9 +78,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
solution = new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
}
else {
notifyDestinationReached(data.getDestination());
notifyDestinationReached(data.getDestination());
ArrayList<Arc> arcs = new ArrayList<>();
while (currentArc != null) {
arcs.add(currentArc);

View file

@ -9,10 +9,10 @@
private double cost;
private Arc fatherArc;
public Label(Node currentNode, Arc fatherArc)
public Label(Node currentNode)
{
this.currentNode = currentNode;
this.fatherArc = fatherArc;
this.fatherArc = null;
this.cost = Double.MAX_VALUE;
this.mark = false;
}
@ -37,9 +37,9 @@
return this.currentNode;
}
public void setCost(double cout)
public void setCost(double cost)
{
this.cost = cout;
this.cost = cost;
}
public void setMark(boolean mark)