access labels node
This commit is contained in:
parent
ceeb5e1203
commit
9e994c2104
1 changed files with 69 additions and 71 deletions
|
@ -20,25 +20,25 @@ 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);
|
||||||
|
@ -46,9 +46,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
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,52 +58,49 @@ 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) {
|
|
||||||
if (successors.contains(y.getNode())) {
|
|
||||||
if (!y.isMarked()) {
|
|
||||||
// This loop serves to get the lentgh of the arc as
|
// This loop serves to get the lentgh of the arc as
|
||||||
// we know its origin and destination
|
// we know its origin and destination
|
||||||
for (Arc arc : x.getNode().getSuccessors()) {
|
for (Arc arc : x.getNode().getSuccessors()) {
|
||||||
if (y.getNode() == arc.getDestination()) {
|
if (successor.getNode() == arc.getDestination()) {
|
||||||
arc_cost = arc.getLength();
|
arc_cost = arc.getLength();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
float possible_path_cost = x.getCost() + arc_cost;
|
float possible_path_cost = x.getCost() + arc_cost;
|
||||||
if (y.getCost() > possible_path_cost) {
|
if (successor.getCost() > possible_path_cost) {
|
||||||
// Mise à jour du label
|
// Mise à jour du label
|
||||||
y.setPathCost(possible_path_cost);
|
successor.setPathCost(possible_path_cost);
|
||||||
y.setParentNode(x.getNode());
|
successor.setParentNode(x.getNode());
|
||||||
// Si y est déjà dans le tas, on va l'update, pas l'insérer
|
// 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
|
// On a pas de fonction update donc on fait un remove puis insert
|
||||||
if (tas.array.contains(y)){
|
if (tas.array.contains(successor)) {
|
||||||
tas.remove(y);
|
tas.remove(successor);
|
||||||
tas.insert(y);
|
tas.insert(successor);
|
||||||
}
|
}
|
||||||
// il n'y est pas déjà on l'insère
|
// il n'y est pas déjà on l'insère
|
||||||
else {
|
else {
|
||||||
tas.insert(y);
|
tas.insert(successor);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Create the path ...
|
// Create the path ...
|
||||||
ArrayList<Arc> arcs_path = new ArrayList<>();
|
ArrayList<Arc> arcs_path = new ArrayList<>();
|
||||||
|
|
||||||
// We will find the path using the parent nodes, from the destination to the origin
|
// We will find the path using the parent nodes, from the destination to the
|
||||||
|
// origin
|
||||||
Node current_node = data.getDestination();
|
Node current_node = data.getDestination();
|
||||||
Label current_label = new Label(current_node);
|
Label current_label = new Label(current_node);
|
||||||
// System.out.println(current_node.getId());
|
// System.out.println(current_node.getId());
|
||||||
|
|
||||||
while (current_node != null && current_node != data.getOrigin()) {
|
while(current_node!=null&¤t_node!=data.getOrigin())
|
||||||
|
{
|
||||||
// Find the label matching the parent node
|
// Find the label matching the parent node
|
||||||
for (Label label : labels) {
|
for (Label label : labels) {
|
||||||
if (label.getNode() == current_node) {
|
if (label.getNode() == current_node) {
|
||||||
|
@ -121,6 +119,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
notifyDestinationReached(data.getDestination());
|
notifyDestinationReached(data.getDestination());
|
||||||
|
|
||||||
|
|
||||||
|
@ -137,5 +136,4 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
}*/
|
}*/
|
||||||
return solution;
|
return solution;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue