few fixes: dijkstra

This commit is contained in:
Clement Lacau 2024-04-18 01:05:14 +02:00
parent 4bd0c74fa4
commit f51d3a26b5
2 changed files with 43 additions and 19 deletions

View file

@ -21,7 +21,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// TODO: // TODO:
// Heap of "sommets" // Heap of "sommets"
BinaryHeap<Label> queue = new BinaryHeap<Label>(); // change to a comparable type BinaryHeap<Label> labels = new BinaryHeap<Label>(); // change to a comparable type
Graph graph = data.getGraph(); Graph graph = data.getGraph();
final int nbNodes = graph.size(); final int nbNodes = graph.size();
@ -35,41 +35,65 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
notifyOriginProcessed(data.getOrigin()); notifyOriginProcessed(data.getOrigin());
// Init Dijkstra // Init Dijkstra
ArrayList<Label> labels = new ArrayList<Label>(); //ArrayList<Label> labels = new ArrayList<Label>();
for (Node node : graph.getNodes()) { for (Node node : graph.getNodes()) {
labels.add(new Label(node)); labels.insert(new Label(node));
} }
// Nodes set // Nodes set
for (Label label : labels) { for (Label label : labels.array) {
label.marked = false; label.marked = false;
label.setPathCost(Float.MAX_VALUE); label.setPathCost(Float.MAX_VALUE);
label.parentNode = null; label.parentNode = null;
} }
// Origin set // Origin set
Label s = labels.get(0); Label s = labels.array.get(0);
s.setPathCost(0); s.setPathCost(0);
queue.insert(s); labels.insert(s);
boolean trouve = false; // TODO : breaking condition boolean trouve = false; // TODO : breaking condition
Label x; Label x;
while (!queue.isEmpty() && !trouve) { while (!labels.isEmpty() && !trouve) {
x = queue.ExtractMin(); x = labels.deleteMin();
x.mark(); x.mark();
for (Arc arc : x.node.getSuccessors()) { // y shall be all the successors of x
// TODO : Retrieve label with node float arc_cost = 0;
Label y; for (Label y : labels.array) {
if (!arc.getDestination().marked) { if (y.getParentNode() == x.getNode()) {
float new_cost = x.getCost() + (float) data.getCost(arc); if (y.isMarked()) {
// This loop serves to get the lentgh of the arc as
// we know its origin and destination
for (Arc arc : x.getNode().getSuccessors()) {
if (y.getNode() == arc.getDestination()) {
arc_cost = arc.getLength();
}
}
float new_cost = x.getCost() + arc_cost;
if (y.getCost() > new_cost) { if (y.getCost() > new_cost) {
y.setPathCost(new_cost); y.setPathCost(new_cost);
queue.insert(y); labels.insert(y);
y.setParentNode(x.getNode()); y.setParentNode(x.getNode());
} }
} }
} }
} }
}
// Create the path ...
ArrayList<Arc> arcs_path = new ArrayList<>();
Label dest;
for (Label label : labels.array) {
if (label.getNode() == data.getDestination()){
dest = label;
}
}
Node current = dest.getParentNode();
while (current != null) {
current
}
arc = predecessorArcs[arc.getOrigin().getId()];
}
return solution; return solution;
} }

View file

@ -17,7 +17,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
private int currentSize; private int currentSize;
// The heap array, which can be larger than currentSize. // The heap array, which can be larger than currentSize.
protected final ArrayList<E> array; public final ArrayList<E> array;
/** /**
* Construct a new empty binary heap. * Construct a new empty binary heap.