LabelStar

This commit is contained in:
Gasson-Betuing Danyl 2025-05-18 12:52:41 +02:00
parent 40d68f76ad
commit b59cf7532b
3 changed files with 100 additions and 15 deletions

View file

@ -1,12 +1,13 @@
package org.insa.graphs.algorithm.shortestpath;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
import org.insa.graphs.algorithm.shortestpath.Label;
import org.insa.graphs.algorithm.AbstractSolution.Status;
import org.insa.graphs.algorithm.utils.BinaryHeap;
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@ -23,11 +24,14 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// retrieve data from the input problem (getInputData() is inherited from the
// parent class ShortestPathAlgorithm)
final ShortestPathData data = getInputData();
// the graph
Graph graph = data.getGraph();
// node number
final int nbNodes = graph.size();
int nodeId = 0;
// initialize the label list
Label[] labels = new Label[nbNodes];
for(Node node : graph.getNodes()){
@ -35,6 +39,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
labels[nodeId] = new Label(node, false, Double.POSITIVE_INFINITY);
}
labels[data.getOrigin().getId()].computedCost = 0;
labels[data.getOrigin().getId()].computedCost = 0;
// variable that will contain the solution of the shortest path problem
ShortestPathSolution solution = null;
@ -43,30 +48,82 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
BinaryHeap<Label> minHeap = new BinaryHeap<>();
minHeap.insert(labels[data.getOrigin().getId()]);
// the index nodes selected for current loop
int IdxNewOrigin = data.getOrigin().getId();
// two variables to update costs
double OldDist = 0;
double NewDist = 0;
// TODO: implement the Dijkstra algorithm
boolean found = false;
for (int i = 0; !found && i < nbNodes; ++i) {
while (!minHeap.isEmpty()) {
for (Label lbl : labels) {
IdxNewOrigin = minHeap.deleteMin().currentNode.getId();
labels[IdxNewOrigin].mark = true;
for (Arc arc : lbl.currentNode.getSuccessors()) {
for (Arc arc : labels[IdxNewOrigin].currentNode.getSuccessors()) { // le arrayList de getSucessors, ça ne retourne que des arc forward. donc pas besoin de vérifier si le chemin est empruntable
// Small test to check allowed roads...
if (!data.isAllowed(arc)) {
continue;
}
if (!labels[arc.getDestination().getId()].mark){ // verif du marquage
NewDist = labels[IdxNewOrigin].computedCost + arc.getLength(); // calcul de la nouvelle distance
OldDist = labels[arc.getDestination().getId()].computedCost; // lecture de l'ancienne
if(OldDist > NewDist){ // si amélioration
try
{
minHeap.remove(labels[arc.getDestination().getId()]); // on supprime l'ancien noeud du tas si c'est pas déjà fait
}
catch(Exception e)
{
}
labels[arc.getDestination().getId()].computedCost = NewDist; // MAJ coût
labels[arc.getDestination().getId()].father = arc; // MAJ père
minHeap.insert(labels[arc.getDestination().getId()]); // on réinsère le noeud dans le tas min
}
}
}
}
// a ce stade de l'algo, on a le tableau labels qui est rempli et les father qui sont updated. il faut s'en servir pour calculer le shortest path
// when the algorithm terminates, return the solution that has been found
if (labels[data.getDestination().getId()] == null) {
solution = new ShortestPathSolution(data, Status.INFEASIBLE); // au cas ou ce n'est apas connexe
}
else{ // création du shortest path en remontant de père en père
ArrayList<Arc> arcs = new ArrayList<>(); // contient le PCC
Arc arc = labels[data.getDestination().getId()].father; // on remonte au noeud suivant par le biais du père
while (arc != null) {
arcs.add(arc);
arc = labels[arc.getOrigin().getId()].father;
}
// reverse path because started at destination
Collections.reverse(arcs);
// Create the final solution.
solution = new ShortestPathSolution(data, Status.OPTIMAL,
new Path(graph, arcs));
}
return solution;
}
}

View file

@ -15,6 +15,12 @@ public class Label implements Comparable<Label>{
this.currentNode = currentNode;
this.mark = mark;
this.computedCost = computedCost;
this.father = null;
}
public double getTotalCost(){
return 0;
}
public double getCost(){

View file

@ -0,0 +1,22 @@
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Node;
public class LabelStar extends Label{
public LabelStar(Node currentNode, boolean mark, double computedCost){
super(currentNode, mark, computedCost);
}
public double getTotalCost(){
return 0;
}
@Override
public int compareTo(Label other){
return Double.compare(this.getCost(), other.getCost());
}
}