Added log method

This commit is contained in:
Arnaud Vergnet 2020-04-03 08:15:21 +02:00
parent daec1d198f
commit 304b71e1d9

View file

@ -5,18 +5,20 @@ import org.insa.graphs.algorithm.utils.ElementNotFoundException;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph;
import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
import java.util.ArrayList;
import java.util.Collections;
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
public DijkstraAlgorithm(ShortestPathData data) {
super(data);
}
final private boolean DEBUG = false;
@Override
protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData();
@ -40,20 +42,26 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
int iterationCounter= 0;
while (!heap.isEmpty() && labels[destination].isNotMarked()) {
Label parent = heap.deleteMin();
parent.setMarked(true);
System.out.println(parent.getCost());
iterationCounter++;
for (Arc arc : graph.getNodes().get(parent.getAssociatedNode()).getSuccessors()) {
Label current = labels[arc.getDestination().getId()];
Label parentLabel = heap.deleteMin();
parentLabel.setMarked(true);
Node parentNode = graph.getNodes().get(parentLabel.getAssociatedNode());
notifyNodeMarked(parentNode);
this.log("Cout marqué : " + parentLabel.getCost());
this.log("Nb successeurs : " + parentNode.getNumberOfSuccessors());
for (Arc arc : parentNode.getSuccessors()) {
Label currentLabel = labels[arc.getDestination().getId()];
notifyNodeReached(arc.getDestination());
if (current.isNotMarked() && data.isAllowed(arc)) {
this.updateCost(arc, parent, current, heap, data);
if (currentLabel.isNotMarked() && data.isAllowed(arc)) {
this.updateCost(arc, parentLabel, currentLabel, heap, data);
}
}
}
System.out.println("Nb itérations: " + iterationCounter);
this.log("===============");
ShortestPathSolution solution = null;
// Destination has no predecessor, the solution is infeasible...
if (labels[destination].getFatherArc() == null) {
@ -76,10 +84,16 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// Create the final solution.
solution = new ShortestPathSolution(data, AbstractSolution.Status.OPTIMAL, new Path(graph, arcs));
}
this.log("Nb arcs solution : " + solution.getPath().getArcs().size());
this.log("Nb itérations solution: " + iterationCounter);
return solution;
}
private void log(String message) {
if (this.DEBUG)
System.out.println(message);
}
private void updateCost(Arc arc, Label parent, Label current, BinaryHeap<Label> heap, ShortestPathData data) {
double newCost = parent.getCost() + data.getCost(arc);
if (newCost < current.getCost()) {