access labels node

This commit is contained in:
Clement Lacau 2024-04-26 16:55:23 +02:00
parent ceeb5e1203
commit 9e994c2104

View file

@ -20,35 +20,36 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
protected ShortestPathSolution doRun() { protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData(); final ShortestPathData data = getInputData();
ShortestPathSolution solution = null; ShortestPathSolution solution = null;
// TODO:
final Graph graph = data.getGraph();
final int nbNodes = graph.size();
// List of sommets, but with type Label // List of sommets, but with type Label
ArrayList<Label> labels = new ArrayList<Label>(); ArrayList<Label> labels = new ArrayList<Label>(nbNodes);
// Heap of sommets // Heap of sommets
BinaryHeap<Label> tas = new BinaryHeap<Label>(); BinaryHeap<Label> tas = new BinaryHeap<Label>();
Graph graph = data.getGraph();
final int nbNodes = graph.size();
// Notify observers about the first event (origin processed). // Notify observers about the first event (origin processed).
notifyOriginProcessed(data.getOrigin()); notifyOriginProcessed(data.getOrigin());
// Init Dijkstra // Init Dijkstra
for (Node node : graph.getNodes()) { for (Node node : graph.getNodes()) {
labels.add(new Label(node)); labels.set(node.getId(), new Label(node));
} }
Label s = new Label(data.getOrigin()); Label s = labels.get(data.getOrigin().getId());
// Add origin in the heap // Add origin in the heap
//Label s = origin;//labels.get(0); // Label s = origin;//labels.get(0);
s.setPathCost(0); s.setPathCost(0);
tas.insert(s); tas.insert(s);
// On considère le sommet x à chaque itération // On considère le sommet x à chaque itération
Label x; Label x = s;
while (!tas.isEmpty()) { int dest_id = data.getDestination().getId();
while (!tas.isEmpty() || labels.get(dest_id).getNode().equals(x.getNode())) {
x = tas.deleteMin(); x = tas.deleteMin();
x.mark(); x.mark();
@ -57,71 +58,69 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// 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; float arc_cost = 0;
ArrayList<Node> successors = new ArrayList<Node>(); for (Arc successorArc : x.getNode().getSuccessors()) {
for (Arc k : x.getNode().getSuccessors()) { Label successor = labels.get(successorArc.getDestination().getId());
successors.add(k.getDestination());
}
// We loop only on the successors of the node with this for and if. if (successor.isMarked()) {
for (Label y : labels) { // This loop serves to get the lentgh of the arc as
if (successors.contains(y.getNode())) { // we know its origin and destination
if (!y.isMarked()) { for (Arc arc : x.getNode().getSuccessors()) {
// This loop serves to get the lentgh of the arc as if (successor.getNode() == arc.getDestination()) {
// we know its origin and destination arc_cost = arc.getLength();
for (Arc arc : x.getNode().getSuccessors()) {
if (y.getNode() == arc.getDestination()) {
arc_cost = arc.getLength();
}
}
float possible_path_cost = x.getCost() + arc_cost;
if (y.getCost() > possible_path_cost) {
// Mise à jour du label
y.setPathCost(possible_path_cost);
y.setParentNode(x.getNode());
// Si y est déjà dans le tas, on va l'update, pas l'insérer
// On a pas de fonction update donc on fait un remove puis insert
if (tas.array.contains(y)){
tas.remove(y);
tas.insert(y);
}
// il n'y est pas déjà on l'insère
else {
tas.insert(y);
}
} }
} }
} float possible_path_cost = x.getCost() + arc_cost;
} if (successor.getCost() > possible_path_cost) {
} // Mise à jour du label
successor.setPathCost(possible_path_cost);
// Create the path ... successor.setParentNode(x.getNode());
ArrayList<Arc> arcs_path = new ArrayList<>(); // Si y est déjà dans le tas, on va l'update, pas l'insérer
// On a pas de fonction update donc on fait un remove puis insert
// We will find the path using the parent nodes, from the destination to the origin if (tas.array.contains(successor)) {
Node current_node = data.getDestination(); tas.remove(successor);
Label current_label = new Label(current_node); tas.insert(successor);
// System.out.println(current_node.getId()); }
// il n'y est pas déjà on l'insère
while (current_node != null && current_node != data.getOrigin()) { else {
// Find the label matching the parent node tas.insert(successor);
for (Label label : labels) { }
if (label.getNode() == current_node){
current_label = label;
current_node = current_label.getParentNode();
}
}
// Knowing the parent node, get the arc between the parent and
// current node and add it to the path
if (current_node != null){
for (Arc arc : current_node.getSuccessors()) {
if (arc.getDestination().getId() == current_label.getNode().getId() ) {
arcs_path.add(arc);
break;
} }
} }
} }
} }
notifyDestinationReached(data.getDestination());
// Create the path ...
ArrayList<Arc> arcs_path = new ArrayList<>();
// We will find the path using the parent nodes, from the destination to the
// origin
Node current_node = data.getDestination();
Label current_label = new Label(current_node);
// System.out.println(current_node.getId());
while(current_node!=null&&current_node!=data.getOrigin())
{
// Find the label matching the parent node
for (Label label : labels) {
if (label.getNode() == current_node) {
current_label = label;
current_node = current_label.getParentNode();
}
}
// Knowing the parent node, get the arc between the parent and
// current node and add it to the path
if (current_node != null) {
for (Arc arc : current_node.getSuccessors()) {
if (arc.getDestination().getId() == current_label.getNode().getId()) {
arcs_path.add(arc);
break;
}
}
}
}
notifyDestinationReached(data.getDestination());
@ -137,5 +136,4 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
}*/ }*/
return solution; return solution;
} }
} }