Dijkstra v0.4, Label comparable (ahem)

This commit is contained in:
Favary Pierre 2021-04-25 12:22:36 +02:00
parent 7ec0ebf44e
commit a1784cc36c
2 changed files with 76 additions and 46 deletions

View file

@ -5,7 +5,7 @@ import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Path;
import java.util.ArrayList;
import java.util.List;//trier tout ça
import java.util.Collections;//trier tout ça
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@ -18,56 +18,74 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData();
ShortestPathSolution solution = new ShortestPathSolution(data,Status.UNKNOWN);//modifié
// TODO:
//initialisation
BinaryHeap<Integer> tas=new BinaryHeap<Integer>();//type comparable nécessaire (donc pas Arc)
BinaryHeap<Label> tas=new BinaryHeap<Label>();//les Label sont comparés via leurs coûts
Label[] tablabel=new Label[data.getGraph().size()];
for (int i=0;i<tablabel.length;i++) {
tablabel[i]=new Label(data.getGraph().get(i),null,Integer.MAX_VALUE);//non marqué par défaut
tablabel[i]=new Label(data.getGraph().get(i),null,Float.MAX_VALUE);//non marqué par défaut
}//dans le tablabel[idnode] on peut retrouver node
tablabel[data.getOrigin().getId()].cout=0;
tas.insert(data.getOrigin().getId());
int nomark=tablabel.length;//existe sommets non marqués (ou à coût infini?)(pas optimal?)
tas.insert(tablabel[data.getOrigin().getId()]);
int nomark=tablabel.length;//nombre de sommets non marqués (pas optimal?)
int x;//id du node qu'on étudie
if (data.getOrigin()==data.getDestination()) {
//retourne un graphe avec juste un node si besoin de gérer ce cas particulier
//(à vérifier, peut-être qu'une fois le reste fini ce ne sera pas nécessaire)
Path chemin= new Path(data.getGraph(),data.getOrigin());
solution = new ShortestPathSolution(data,Status.OPTIMAL,chemin);
nomark=0;
}
boolean arrive=false;//node de destination atteint ou pas
System.out.println("Initialisation OK");
//itérations
while (nomark>0){
x=tas.deleteMin();
tablabel[x].marque=true;
nomark--;
for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {
if (!tablabel[arcy.getDestination().getId()].marque) {
if (tablabel[arcy.getDestination().getId()].cout>tablabel[x].cout+(int)arcy.getMinimumTravelTime()) {
tablabel[arcy.getDestination().getId()].cout=tablabel[x].cout+(int)arcy.getMinimumTravelTime();
tablabel[arcy.getDestination().getId()].pere=arcy;//ligne non dans le poly
try{
tas.remove(arcy.getDestination().getId());
} finally {//méthode *fortement douteuse* pour opérer update ou inssert selon si l'élément est déjà dans le tas ou pas
tas.insert(arcy.getDestination().getId());
}
}//s'aider de l'algorithme de Bellman-Ford déjà fait
}
//penser à gérer le cas "aucun chemin n'existe" (avec l'initialisation par défaut?)
}
while (nomark>0 && !arrive){
x=tas.deleteMin().sommet_courant.getId();
if (x==data.getDestination().getId()) {
arrive=true;//marche aussi si le départ et l'arrivée sont le même node
}else {
tablabel[x].marque=true;
nomark--;
int y;
for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {//mauvais parcours?
y=arcy.getDestination().getId();
if (!tablabel[y].marque) {
if (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
if (tablabel[y].cout!=Float.MAX_VALUE)
tas.remove(tablabel[y]);
//méthode à vérifier pour opérer update ou insert
tas.insert(tablabel[y]);
}
}
//penser à gérer le cas "aucun chemin n'existe" (avec l'initialisation par défaut?)
}
}
}
//créer le chemin solution grâce au tas (le tas nécessite un type comparable donc l'id des nodes)
List<Arc> bonsarcs=new ArrayList<Arc>();//pourquoi une erreur ici?
while (!tas.isEmpty()) {
bonsarcs.add(tablabel[tas.deleteMin()].pere);//un node de plus qu'il n'y a d'arcs, vérifier ce que ça donne
}//est-ce que le cas "un seul node" marche bien en renvoyant un unique arc null?
Path chemin= new Path(data.getGraph(),bonsarcs);
solution = new ShortestPathSolution(data,Status.OPTIMAL,chemin);
System.out.println("Itérations OK");
if (!arrive) {
solution= new ShortestPathSolution(data,Status.INFEASIBLE);
}else {
ArrayList<Arc> bonsarcs=new ArrayList<Arc>();
Label labelact=tablabel[data.getDestination().getId()];
while (labelact.pere!=null) {
bonsarcs.add(labelact.pere);
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);
//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));
}
System.out.println("Fin OK");
return solution;
}

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>{
//quelles valeurs de protection?
protected Node sommet_courant;
@ -12,21 +12,21 @@ public class Label {
public boolean marque;
//valeur courante du plus court chemin, de l'origine au sommet
protected int cout;
protected float cout;
//arc (permettant d'avoir le sommet) précédent sur le plus court chemin courant
protected Arc pere;
//constructeur
public Label(Node sommet,Arc padre, int prix) {
public Label(Node sommet,Arc padre, float prix) {
this.sommet_courant=sommet;
this.pere=padre;
this.cout=prix;
this.marque=false;//!\\ pas sûr que ce soit une bonne idée
}
public Label(Node sommet,Arc padre, int prix, boolean mark) {
public Label(Node sommet,Arc padre, float prix, boolean mark) {
this.sommet_courant=sommet;
this.pere=padre;
this.cout=prix;
@ -34,7 +34,19 @@ public class Label {
}
public int getCost() {
public float getCost() {
return this.cout;
}
//fonction de comparaison
public int compareTo(Label comp) {
float diff= this.cout-comp.cout;
if (diff>0) {
return 1;
}else if (diff<0) {
return -1;
}else {
return 0;
}
}
}