feat(dijkstra): support double precision instead of float

This commit is contained in:
Paul Alnet 2024-05-20 23:27:41 +02:00
parent 6af2ae2ead
commit f4db47ee4e
4 changed files with 17 additions and 17 deletions

View file

@ -63,7 +63,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
notifyNodeMarked(x.getNode()); notifyNodeMarked(x.getNode());
// System.out.println(x.getCost()); // Pour vérifier une croissance des noeuds marqués // System.out.println(x.getCost()); // Pour vérifier une croissance des noeuds marqués
// We create a list of node successors of x, instead of a list of Arcs. // We create a list of node successors of x, instead of a list of Arcs.
float arc_cost = 0; double arc_cost = 0;
for (Arc successorArc : x.getNode().getSuccessors()) { for (Arc successorArc : x.getNode().getSuccessors()) {
Label successor = labels.get(successorArc.getDestination().getId()); Label successor = labels.get(successorArc.getDestination().getId());
@ -76,11 +76,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// data.getcost(arc) returns a cost considering the mode chosen: // data.getcost(arc) returns a cost considering the mode chosen:
// TIME or LENGTH // TIME or LENGTH
// Similar to using getLength / getMinimumTravelTime // Similar to using getLength / getMinimumTravelTime
arc_cost = (float) data.getCost(arc); arc_cost = data.getCost(arc);
} }
} }
final float possible_path_cost = x.getCost() + arc_cost; final double possible_path_cost = x.getCost() + arc_cost;
if (successor.getCost() > possible_path_cost) { if (successor.getCost() > possible_path_cost) {
// Mise à jour du label // Mise à jour du label
successor.setPathCost(possible_path_cost); successor.setPathCost(possible_path_cost);

View file

@ -7,7 +7,7 @@ public class Label implements Comparable<Label> {
Node node; Node node;
boolean marked; boolean marked;
boolean reached; boolean reached;
float pathCost; double pathCost;
Node parentNode; Node parentNode;
@ -15,7 +15,7 @@ public class Label implements Comparable<Label> {
this.node = node; this.node = node;
this.marked = false; this.marked = false;
this.marked = false; this.marked = false;
this.pathCost = Float.MAX_VALUE; this.pathCost = Double.MAX_VALUE;
this.parentNode = null; this.parentNode = null;
} }
@ -24,24 +24,24 @@ public class Label implements Comparable<Label> {
public void mark() { this.marked = true; } public void mark() { this.marked = true; }
public boolean isReached() { return this.reached; } public boolean isReached() { return this.reached; }
public void markReached() { this.reached = true; } public void markReached() { this.reached = true; }
public float getPathCost() { return this.pathCost; } public double getPathCost() { return this.pathCost; }
public void setPathCost(float newCost) { this.pathCost = newCost; } public void setPathCost(double newCost) { this.pathCost = newCost; }
public Node getParentNode() { return this.parentNode; } public Node getParentNode() { return this.parentNode; }
public void setParentNode(Node parentNode) { this.parentNode = parentNode; } public void setParentNode(Node parentNode) { this.parentNode = parentNode; }
public float getCost() { public double getCost() {
// function will be modified later // function will be modified later
return pathCost; return pathCost;
} }
public float getTotalCost() { public double getTotalCost() {
// will be overriden for A* // will be overriden for A*
return pathCost; return pathCost;
} }
public int compareTo(Label other) { public int compareTo(Label other) {
final float difference = this.getTotalCost() - other.getTotalCost(); final double difference = this.getTotalCost() - other.getTotalCost();
return (int) Math.signum(difference); return (int) Math.signum(difference);
} }
} }

View file

@ -6,7 +6,7 @@ import org.insa.graphs.algorithm.AbstractInputData.Mode;
import org.insa.graphs.model.RoadInformation; import org.insa.graphs.model.RoadInformation;
public class LabelStar extends Label { public class LabelStar extends Label {
private float distanceToDestination; private double distanceToDestination;
private int MaximumSpeed; private int MaximumSpeed;
public LabelStar(Node node, int MaximumSpeed, Node destination) { public LabelStar(Node node, int MaximumSpeed, Node destination) {
@ -14,15 +14,15 @@ public class LabelStar extends Label {
this.MaximumSpeed = MaximumSpeed; this.MaximumSpeed = MaximumSpeed;
// precision was never an answer // precision was never an answer
if (this.MaximumSpeed < 0) { if (this.MaximumSpeed < 0) {
distanceToDestination = (float) Point.distance(node.getPoint(), destination.getPoint()); distanceToDestination = (double) Point.distance(node.getPoint(), destination.getPoint());
} }
else { else {
distanceToDestination = (float) Point.distance(node.getPoint(), destination.getPoint()) / (1000 * MaximumSpeed); distanceToDestination = (double) Point.distance(node.getPoint(), destination.getPoint()) / (1000 * MaximumSpeed);
} }
} }
@Override @Override
public float getTotalCost() { public double getTotalCost() {
return this.getCost() + distanceToDestination; return this.getCost() + distanceToDestination;
} }
@ -32,7 +32,7 @@ public class LabelStar extends Label {
* "En cas d'égalité, on considèrera en premier le sommet ayant le plus petit coût estimé à la destination." * "En cas d'égalité, on considèrera en premier le sommet ayant le plus petit coût estimé à la destination."
*/ */
public int compareTo(Label other) { public int compareTo(Label other) {
final float difference = this.getTotalCost() - other.getTotalCost(); final double difference = this.getTotalCost() - other.getTotalCost();
int retour = (int) Math.signum(difference); int retour = (int) Math.signum(difference);
if (Math.abs(difference) < 0.01) { if (Math.abs(difference) < 0.01) {
// En cas d'égalité: // En cas d'égalité:

View file

@ -5,7 +5,7 @@ import org.insa.graphs.model.Node;
public abstract class ShortestPathAlgorithm extends AbstractAlgorithm<ShortestPathObserver> { public abstract class ShortestPathAlgorithm extends AbstractAlgorithm<ShortestPathObserver> {
protected float pathCost; protected double pathCost;
protected ShortestPathAlgorithm(ShortestPathData data) { protected ShortestPathAlgorithm(ShortestPathData data) {
super(data); super(data);
@ -69,7 +69,7 @@ public abstract class ShortestPathAlgorithm extends AbstractAlgorithm<ShortestPa
} }
} }
public float getCostPath() { public double getCostPath() {
return this.pathCost; return this.pathCost;
} }
} }