access labels node
This commit is contained in:
parent
ceeb5e1203
commit
9e994c2104
1 changed files with 69 additions and 71 deletions
|
@ -20,35 +20,36 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
protected ShortestPathSolution doRun() {
|
||||
final ShortestPathData data = getInputData();
|
||||
ShortestPathSolution solution = null;
|
||||
// TODO:
|
||||
|
||||
final Graph graph = data.getGraph();
|
||||
|
||||
final int nbNodes = graph.size();
|
||||
|
||||
// List of sommets, but with type Label
|
||||
ArrayList<Label> labels = new ArrayList<Label>();
|
||||
ArrayList<Label> labels = new ArrayList<Label>(nbNodes);
|
||||
// Heap of sommets
|
||||
BinaryHeap<Label> tas = new BinaryHeap<Label>();
|
||||
|
||||
Graph graph = data.getGraph();
|
||||
final int nbNodes = graph.size();
|
||||
|
||||
// Notify observers about the first event (origin processed).
|
||||
notifyOriginProcessed(data.getOrigin());
|
||||
|
||||
// Init Dijkstra
|
||||
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
|
||||
//Label s = origin;//labels.get(0);
|
||||
// Label s = origin;//labels.get(0);
|
||||
s.setPathCost(0);
|
||||
tas.insert(s);
|
||||
|
||||
// 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.mark();
|
||||
|
||||
|
@ -57,70 +58,68 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
|
||||
// We create a list of node successors of x, instead of a list of Arcs.
|
||||
float arc_cost = 0;
|
||||
ArrayList<Node> successors = new ArrayList<Node>();
|
||||
for (Arc k : x.getNode().getSuccessors()) {
|
||||
successors.add(k.getDestination());
|
||||
}
|
||||
for (Arc successorArc : x.getNode().getSuccessors()) {
|
||||
Label successor = labels.get(successorArc.getDestination().getId());
|
||||
|
||||
// We loop only on the successors of the node with this for and if.
|
||||
for (Label y : labels) {
|
||||
if (successors.contains(y.getNode())) {
|
||||
if (!y.isMarked()) {
|
||||
if (successor.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()) {
|
||||
if (successor.getNode() == arc.getDestination()) {
|
||||
arc_cost = arc.getLength();
|
||||
}
|
||||
}
|
||||
float possible_path_cost = x.getCost() + arc_cost;
|
||||
if (y.getCost() > possible_path_cost) {
|
||||
if (successor.getCost() > possible_path_cost) {
|
||||
// Mise à jour du label
|
||||
y.setPathCost(possible_path_cost);
|
||||
y.setParentNode(x.getNode());
|
||||
successor.setPathCost(possible_path_cost);
|
||||
successor.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);
|
||||
if (tas.array.contains(successor)) {
|
||||
tas.remove(successor);
|
||||
tas.insert(successor);
|
||||
}
|
||||
// il n'y est pas déjà on l'insère
|
||||
else {
|
||||
tas.insert(y);
|
||||
}
|
||||
tas.insert(successor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Create the path ...
|
||||
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();
|
||||
Label current_label = new Label(current_node);
|
||||
// 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
|
||||
for (Label label : labels) {
|
||||
if (label.getNode() == current_node){
|
||||
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){
|
||||
if (current_node != null) {
|
||||
for (Arc arc : current_node.getSuccessors()) {
|
||||
if (arc.getDestination().getId() == current_label.getNode().getId() ) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue