Dijkstra v0.4, Label comparable (ahem)
This commit is contained in:
parent
7ec0ebf44e
commit
a1784cc36c
2 changed files with 76 additions and 46 deletions
|
@ -5,7 +5,7 @@ import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||||
import org.insa.graphs.model.Arc;
|
import org.insa.graphs.model.Arc;
|
||||||
import org.insa.graphs.model.Path;
|
import org.insa.graphs.model.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;//trier tout ça
|
import java.util.Collections;//trier tout ça
|
||||||
|
|
||||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
|
@ -18,56 +18,74 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
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é
|
||||||
// TODO:
|
|
||||||
|
|
||||||
//initialisation
|
//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()];
|
Label[] tablabel=new Label[data.getGraph().size()];
|
||||||
for (int i=0;i<tablabel.length;i++) {
|
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
|
}//dans le tablabel[idnode] on peut retrouver node
|
||||||
tablabel[data.getOrigin().getId()].cout=0;
|
tablabel[data.getOrigin().getId()].cout=0;
|
||||||
tas.insert(data.getOrigin().getId());
|
tas.insert(tablabel[data.getOrigin().getId()]);
|
||||||
|
|
||||||
int nomark=tablabel.length;//existe sommets non marqués (ou à coût infini?)(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
|
||||||
if (data.getOrigin()==data.getDestination()) {
|
System.out.println("Initialisation OK");
|
||||||
//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;
|
|
||||||
}
|
|
||||||
|
|
||||||
//itérations
|
//itérations
|
||||||
while (nomark>0){
|
while (nomark>0 && !arrive){
|
||||||
x=tas.deleteMin();
|
|
||||||
tablabel[x].marque=true;
|
x=tas.deleteMin().sommet_courant.getId();
|
||||||
nomark--;
|
|
||||||
for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {
|
if (x==data.getDestination().getId()) {
|
||||||
if (!tablabel[arcy.getDestination().getId()].marque) {
|
arrive=true;//marche aussi si le départ et l'arrivée sont le même node
|
||||||
if (tablabel[arcy.getDestination().getId()].cout>tablabel[x].cout+(int)arcy.getMinimumTravelTime()) {
|
}else {
|
||||||
tablabel[arcy.getDestination().getId()].cout=tablabel[x].cout+(int)arcy.getMinimumTravelTime();
|
|
||||||
tablabel[arcy.getDestination().getId()].pere=arcy;//ligne non dans le poly
|
tablabel[x].marque=true;
|
||||||
try{
|
nomark--;
|
||||||
tas.remove(arcy.getDestination().getId());
|
int y;
|
||||||
} 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());
|
for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {//mauvais parcours?
|
||||||
}
|
|
||||||
}//s'aider de l'algorithme de Bellman-Ford déjà fait
|
y=arcy.getDestination().getId();
|
||||||
}
|
|
||||||
|
if (!tablabel[y].marque) {
|
||||||
//penser à gérer le cas "aucun chemin n'existe" (avec l'initialisation par défaut?)
|
|
||||||
}
|
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?
|
System.out.println("Itérations OK");
|
||||||
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
|
if (!arrive) {
|
||||||
}//est-ce que le cas "un seul node" marche bien en renvoyant un unique arc null?
|
solution= new ShortestPathSolution(data,Status.INFEASIBLE);
|
||||||
Path chemin= new Path(data.getGraph(),bonsarcs);
|
}else {
|
||||||
solution = new ShortestPathSolution(data,Status.OPTIMAL,chemin);
|
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;
|
return solution;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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>{
|
||||||
|
|
||||||
//quelles valeurs de protection?
|
//quelles valeurs de protection?
|
||||||
protected Node sommet_courant;
|
protected Node sommet_courant;
|
||||||
|
@ -12,21 +12,21 @@ public class Label {
|
||||||
public boolean marque;
|
public boolean marque;
|
||||||
|
|
||||||
//valeur courante du plus court chemin, de l'origine au sommet
|
//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
|
//arc (permettant d'avoir le sommet) précédent sur le plus court chemin courant
|
||||||
protected Arc pere;
|
protected Arc pere;
|
||||||
|
|
||||||
|
|
||||||
//constructeur
|
//constructeur
|
||||||
public Label(Node sommet,Arc padre, int prix) {
|
public Label(Node sommet,Arc padre, float prix) {
|
||||||
this.sommet_courant=sommet;
|
this.sommet_courant=sommet;
|
||||||
this.pere=padre;
|
this.pere=padre;
|
||||||
this.cout=prix;
|
this.cout=prix;
|
||||||
this.marque=false;//!\\ pas sûr que ce soit une bonne idée
|
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.sommet_courant=sommet;
|
||||||
this.pere=padre;
|
this.pere=padre;
|
||||||
this.cout=prix;
|
this.cout=prix;
|
||||||
|
@ -34,7 +34,19 @@ public class Label {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCost() {
|
public float getCost() {
|
||||||
return this.cout;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue