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 @Override
protected ShortestPathSolution doRun() { protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData(); final ShortestPathData data = getInputData();
// TODO:
int tailleGraphe = data.getGraph().size(); int tailleGraphe = data.getGraph().size();
int nbMarques = 0; int nbMarques = 0;
int index = 0; int index = 0;
@ -24,33 +23,40 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
ArrayList<Label> labelSommets = new ArrayList<Label>(); ArrayList<Label> labelSommets = new ArrayList<Label>();
Label currentLabel; Label currentLabel;
BinaryHeap<Node> leTas = new BinaryHeap<Node>(); BinaryHeap<Label> leTas = new BinaryHeap<Label>();
//Initialisation ----------------------------------------------------
for(int i = 0; i< tailleGraphe; i++ ) for(int i = 0; i< tailleGraphe; i++ )
{ {
currentLabel = new Label(data.getGraph().get(i)); currentLabel = new Label(data.getGraph().get(i));
labelSommets.set(i, currentLabel); labelSommets.add(currentLabel);
} }
currentLabel = labelSommets.get(data.getOrigin().getId()); currentLabel = labelSommets.get(data.getOrigin().getId());
currentLabel.setNouveauChemin(null, 0); currentLabel.setNouveauChemin(null, 0);
labelSommets.set(data.getOrigin().getId(), currentLabel); currentLabel.setMarqueTrue();
leTas.insert(data.getOrigin()); leTas.insert(currentLabel);
Node currentNode; System.out.println("Init finie");
Node currentNode = data.getOrigin() ;
double currentCost; double currentCost;
double newCost; double newCost;
Label newLabel; 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(); currentLabel.setMarqueTrue();
labelSommets.set(index, currentLabel);
nbMarques++; nbMarques++;
@ -59,23 +65,37 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
newLabel = labelSommets.get(arc.getDestination().getId()); newLabel = labelSommets.get(arc.getDestination().getId());
if(!newLabel.getMarque()) if(!newLabel.getMarque())
{ {
currentCost = newLabel.getCost() + currentLabel.getCost(); currentCost = newLabel.getCost();
newCost = arc.getLength(); newCost = arc.getLength() + currentLabel.getCost();
if(newCost < currentCost || currentCost == -1.0) if(currentCost == -1.0)
{ {
newLabel.setNouveauChemin(arc, newCost); newLabel.setNouveauChemin(arc, newCost);
leTas.insert(arc.getDestination()); leTas.insert(newLabel);
labelSommets.set(arc.getDestination().getId(), currentLabel); }
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(); Node finalNode = data.getDestination();
index = 0; index = 0;
ArrayList<Arc> arcListe = new ArrayList<Arc>() ; 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); arcListe.add(labelSommets.get(finalNode.getId()).pere);
finalNode = arcListe.get(arcListe.size()-1).getOrigin(); 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 )); return new ShortestPathSolution(data, AbstractSolution.Status.FEASIBLE, new Path(data.getGraph(),arcListe ));
} }
else
{ return new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
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.Arc;
import org.insa.graphs.model.Node; import org.insa.graphs.model.Node;
public class Label { public class Label implements Comparable<Label>{
/* ATTRIBUTS */ /* ATTRIBUTS */
@ -66,7 +66,26 @@ public class Label {
this.coutRealise = coutRealise; 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;
}
} }