From parentnode to parentarc

This commit is contained in:
Clement Lacau 2024-05-21 19:08:04 +02:00
parent bed0f37cfa
commit 0e576e534b
2 changed files with 14 additions and 32 deletions

View file

@ -66,7 +66,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
double arc_cost = 0;
for (Arc successorArc : x.getNode().getSuccessors()) {
Label successor = labels.get(successorArc.getDestination().getId());
arc_cost = Double.MAX_VALUE;
if (!successor.isMarked() && data.isAllowed(successorArc)) {
// This loop serves to get the length of the arc as
// we know its origin and destination
@ -76,7 +76,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// data.getcost(arc) returns a cost considering the mode chosen:
// TIME or LENGTH
// Similar to using getLength / getMinimumTravelTime
arc_cost = data.getCost(arc);
arc_cost = Math.min(data.getCost(arc), arc_cost);
}
}
@ -84,7 +84,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
if (successor.getCost() > possible_path_cost) {
// Mise à jour du label
successor.setPathCost(possible_path_cost);
successor.setParentNode(x.getNode());
successor.setParentArc(successorArc);
// Si le noeud n'a pas déjà été rajouté au tas, on le rajoute
// isReached permet de vérifier en complexité O(1)
// C'est un léger coût en mémoire pour un gain en vitesse
@ -101,7 +101,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
}
}
if (labels.get(data.getDestination().getId()).getParentNode() == null) {
if (labels.get(data.getDestination().getId()).getParentArc() == null) {
this.pathCost = 0;
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
}
@ -109,32 +109,13 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// Create the path ...
ArrayList<Arc> arcs_path = new ArrayList<>();
Arc arc = labels.get(data.getDestination().getId()).getParentArc();
// We will find the path using the parent nodes, from the destination to the
// origin
Label current_label = labels.get(data.getDestination().getId());
Label parent_label = current_label;
while(current_label != null && current_label.getNode().getId() != data.getOrigin().getId())
{
// Find the label matching the parent node
parent_label = labels.get(current_label.getParentNode().getId());
// Knowing the parent node, get the arc between the parent and
// current node and add it to the path
if (parent_label != null) {
double minCost = Double.MAX_VALUE;
Arc minCostArc = null;
for (Arc arc : parent_label.getNode().getSuccessors()) {
if (arc.getDestination().getId() == current_label.getNode().getId()
&& data.isAllowed(arc)
&& data.getCost(arc) < minCost) {
minCost = Math.min(data.getCost(arc), minCost);
minCostArc = arc;
}
}
arcs_path.add(minCostArc);
}
current_label = parent_label;
while (arc != null && arc.getDestination().getId() != data.getOrigin().getId()) {
arcs_path.add(arc);
arc = labels.get(arc.getOrigin().getId()).getParentArc();
}
notifyDestinationReached(data.getDestination());

View file

@ -1,6 +1,7 @@
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Arc;
import org.insa.graphs.algorithm.AbstractInputData.Mode;
public class Label implements Comparable<Label> {
@ -8,7 +9,7 @@ public class Label implements Comparable<Label> {
boolean marked;
boolean reached;
double pathCost;
Node parentNode;
Arc parentArc;
public Label(Node node) {
@ -16,7 +17,7 @@ public class Label implements Comparable<Label> {
this.marked = false;
this.marked = false;
this.pathCost = Double.MAX_VALUE;
this.parentNode = null;
this.parentArc = null;
}
public Node getNode() { return this.node; }
@ -26,8 +27,8 @@ public class Label implements Comparable<Label> {
public void markReached() { this.reached = true; }
public double getPathCost() { return this.pathCost; }
public void setPathCost(double newCost) { this.pathCost = newCost; }
public Node getParentNode() { return this.parentNode; }
public void setParentNode(Node parentNode) { this.parentNode = parentNode; }
public Arc getParentArc() { return this.parentArc; }
public void setParentArc(Arc parentArc) { this.parentArc = parentArc; }
public double getCost() {
// function will be modified later