Dijkstra avec Observers

This commit is contained in:
Favary Pierre 2021-04-26 16:59:54 +02:00
parent 91e3152d6d
commit 31de9cb5f5

View file

@ -16,6 +16,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@Override @Override
protected ShortestPathSolution doRun() { protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData(); final ShortestPathData data = getInputData();
ShortestPathSolution solution = new ShortestPathSolution(data,Status.UNKNOWN);//modifié ShortestPathSolution solution = new ShortestPathSolution(data,Status.UNKNOWN);//modifié
@ -31,10 +32,9 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
int nomark=tablabel.length;//nombre de sommets non marqués (pas optimal?) int nomark=tablabel.length;//nombre de sommets non marqués (pas optimal?)
int x;//id du node qu'on étudie int x;//id du node qu'on étudie
boolean arrive=false;//node de destination atteint ou pas boolean arrive=false;//node de destination atteint ou pas
System.out.println("Initialisation OK");
//itérations //itérations
while (nomark>0 && !arrive){ while (nomark>0 && !arrive && !tas.isEmpty()){
x=tas.deleteMin().sommet_courant.getId(); x=tas.deleteMin().sommet_courant.getId();
@ -43,23 +43,25 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
}else { }else {
tablabel[x].marque=true; tablabel[x].marque=true;
this.notifyNodeMarked(tablabel[x].sommet_courant);
nomark--; nomark--;
int y; int y;
for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {//mauvais parcours? for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {
y=arcy.getDestination().getId(); y=arcy.getDestination().getId();
if (!tablabel[y].marque) { if (!tablabel[y].marque && data.isAllowed(arcy)) {//ligne 108 de l'excel d'avancement
if (tablabel[y].cout>tablabel[x].cout+(float)data.getCost(arcy)) { if (tablabel[y].cout>tablabel[x].cout+(float)data.getCost(arcy)) {
if (tablabel[y].cout!=Float.MAX_VALUE) if (tablabel[y].cout!=Float.MAX_VALUE) {
tas.remove(tablabel[y]); tas.remove(tablabel[y]);
this.notifyNodeReached(arcy.getDestination());
}
tablabel[y].cout=tablabel[x].cout+(float)data.getCost(arcy); tablabel[y].cout=tablabel[x].cout+(float)data.getCost(arcy);
tablabel[y].pere=arcy;//ligne non dans le poly tablabel[y].pere=arcy;//ligne non dans le poly
//méthode à vérifier pour opérer update ou insert (avec le if float.max plus haut)
tas.insert(tablabel[y]); tas.insert(tablabel[y]);
} }
@ -68,24 +70,26 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
} }
} }
} }
System.out.println("Itérations OK");
if (!arrive) { if (!arrive) {
solution= new ShortestPathSolution(data,Status.INFEASIBLE); solution= new ShortestPathSolution(data,Status.INFEASIBLE);
}else { }else {
this.notifyDestinationReached(data.getDestination());
ArrayList<Arc> bonsarcs=new ArrayList<Arc>(); ArrayList<Arc> bonsarcs=new ArrayList<Arc>();
Label labelact=tablabel[data.getDestination().getId()]; Label labelact=tablabel[data.getDestination().getId()];
while (labelact.pere!=null) { while (labelact.pere!=null) {
bonsarcs.add(labelact.pere); bonsarcs.add(labelact.pere);
labelact=tablabel[labelact.pere.getOrigin().getId()]; labelact=tablabel[labelact.pere.getOrigin().getId()];
//à vérifier }
}//est-ce que le cas "un seul node" marche bien en renvoyant un unique arc null?
Collections.reverse(bonsarcs); Collections.reverse(bonsarcs);
//Path chemin= new Path(data.getGraph(),bonsarcs);//y'a t-il un retour si le chemin est infaisable? éviterait la condition précédente si oui //Path chemin= new Path(data.getGraph(),bonsarcs);//y'a t-il un retour si le chemin est infaisable? éviterait la condition précédente si oui
solution = new ShortestPathSolution(data,Status.OPTIMAL,new Path(data.getGraph(),bonsarcs)); solution = new ShortestPathSolution(data,Status.OPTIMAL,new Path(data.getGraph(),bonsarcs));
} }
System.out.println("Fin OK");
return solution; return solution;
} }