Djikstra okay

This commit is contained in:
Marie Brunetto 2023-04-05 16:55:24 +02:00
parent aff74a7bad
commit 2d5fc87aca
2 changed files with 60 additions and 23 deletions

View file

@ -16,7 +16,6 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@Override
protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData();
// TODO:
int tailleGraphe = data.getGraph().size();
int nbMarques = 0;
int index = 0;
@ -24,33 +23,40 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
ArrayList<Label> labelSommets = new ArrayList<Label>();
Label currentLabel;
BinaryHeap<Node> leTas = new BinaryHeap<Node>();
BinaryHeap<Label> leTas = new BinaryHeap<Label>();
//Initialisation ----------------------------------------------------
for(int i = 0; i< tailleGraphe; i++ )
{
currentLabel = new Label(data.getGraph().get(i));
labelSommets.set(i, currentLabel);
labelSommets.add(currentLabel);
}
currentLabel = labelSommets.get(data.getOrigin().getId());
currentLabel.setNouveauChemin(null, 0);
labelSommets.set(data.getOrigin().getId(), currentLabel);
leTas.insert(data.getOrigin());
currentLabel.setMarqueTrue();
leTas.insert(currentLabel);
Node currentNode;
System.out.println("Init finie");
Node currentNode = data.getOrigin() ;
double currentCost;
double newCost;
Label newLabel;
//Boucle principale ----------------------------------------------------
while (nbMarques != tailleGraphe)
while (!leTas.isEmpty() && !currentNode.equals(data.getDestination()))
{
currentNode = leTas.deleteMin();
index = currentNode.getId();
currentLabel = labelSommets.get(currentNode.getId());
currentLabel = leTas.deleteMin();
currentNode = currentLabel.getSommetCourant();
index = currentNode.getId();
currentLabel.setMarqueTrue();
labelSommets.set(index, currentLabel);
nbMarques++;
@ -59,23 +65,37 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
newLabel = labelSommets.get(arc.getDestination().getId());
if(!newLabel.getMarque())
{
currentCost = newLabel.getCost() + currentLabel.getCost();
newCost = arc.getLength();
if(newCost < currentCost || currentCost == -1.0)
currentCost = newLabel.getCost();
newCost = arc.getLength() + currentLabel.getCost();
if(currentCost == -1.0)
{
newLabel.setNouveauChemin(arc, newCost);
leTas.insert(arc.getDestination());
labelSommets.set(arc.getDestination().getId(), currentLabel);
leTas.insert(newLabel);
}
else if(newCost < currentCost)
{
leTas.remove(newLabel);
newLabel.setNouveauChemin(arc, newCost);
leTas.insert(newLabel);
}
}
}
}
}
System.out.println("Chemin obtenu");
//Retour chemin obtenu ----------------------------------------------------
Node finalNode = data.getDestination();
index = 0;
ArrayList<Arc> arcListe = new ArrayList<Arc>() ;
while(!finalNode.equals(data.getOrigin()) || index < tailleGraphe)
if(labelSommets.get(data.getDestination().getId()).getCost() == -1)
{
return new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
}
while(!finalNode.equals(data.getOrigin()) && index < tailleGraphe)
{
arcListe.add(labelSommets.get(finalNode.getId()).pere);
finalNode = arcListe.get(arcListe.size()-1).getOrigin();
@ -85,10 +105,8 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
{
return new ShortestPathSolution(data, AbstractSolution.Status.FEASIBLE, new Path(data.getGraph(),arcListe ));
}
else
{
return new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
}
}

View file

@ -3,7 +3,7 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node;
public class Label {
public class Label implements Comparable<Label>{
/* ATTRIBUTS */
@ -66,7 +66,26 @@ public class Label {
this.coutRealise = coutRealise;
}
/**
* Compare the ID of this node with the ID of the given node.
*
* @param other Node to compare this node with.
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(Label other) {
if (this.getCost() < other.getCost())
{
return -1;
}
else if (this.getCost() > other.getCost())
{
return 1;
}
return 0;
}
}