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

View file

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