123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package org.insa.graphs.algorithm.shortestpath;
-
- import org.insa.graphs.algorithm.AbstractSolution.Status;
- import org.insa.graphs.algorithm.utils.BinaryHeap;
- import org.insa.graphs.model.Arc;
- import org.insa.graphs.model.Node;
- import org.insa.graphs.model.Path;
- import java.util.ArrayList;
- import java.util.Collections;//trier tout ça
-
- public class DijkstraAlgorithm extends ShortestPathAlgorithm {
-
-
- public DijkstraAlgorithm(ShortestPathData data) {
- super(data);
- }
-
- public Label LabelTyped(Node sommet,Arc padre, float prix){
- return new Label(sommet,padre, prix);
- }
-
- @Override
- protected ShortestPathSolution doRun() {
-
- final ShortestPathData data = getInputData();
- ShortestPathSolution solution = new ShortestPathSolution(data,Status.UNKNOWN);//modifié
-
- //initialisation
- 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]=LabelTyped(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(tablabel[data.getOrigin().getId()]);
- int nomark=tablabel.length;//nombre de sommets non marqués (pas optimal?)
- int x;//id du node qu'on étudie
- boolean arrive=false;//node de destination atteint ou pas
-
-
- //itérations
- while (nomark>0 && !arrive && !tas.isEmpty()){
-
- 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;
- this.notifyNodeMarked(tablabel[x].sommet_courant);
- //System.out.printf("%f\n",tablabel[x].getTotalCost());
- //System.out.printf("%d\n",tablabel[x].sommet_courant.getNumberOfSuccessors());
- nomark--;
- int y;
-
- for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {
-
- y=arcy.getDestination().getId();
- //System.out.println(tas.isValid());
-
- if (!tablabel[y].marque && data.isAllowed(arcy)) {//ligne 108 de l'excel d'avancement
-
- if (tablabel[y].getCost()>tablabel[x].getCost()+(float)data.getCost(arcy)) {
-
- if (tablabel[y].getCost()!=Float.MAX_VALUE) {
- tas.remove(tablabel[y]);
- this.notifyNodeReached(arcy.getDestination());
- }
-
- tablabel[y].cout=tablabel[x].getCost()+(float)data.getCost(arcy);
- tablabel[y].pere=arcy;//ligne non dans le poly
- tas.insert(tablabel[y]);
-
- }
- }
- }
- }
- }
-
- if (!arrive) {
- solution= new ShortestPathSolution(data,Status.INFEASIBLE);
- }else {
-
- this.notifyDestinationReached(data.getDestination());
-
- 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()];
- }
-
- Collections.reverse(bonsarcs);
- solution = new ShortestPathSolution(data,Status.OPTIMAL,new Path(data.getGraph(),bonsarcs));
-
- //Path chemin= new Path(data.getGraph(),bonsarcs);//y'a t-il un retour si le chemin est infaisable?
- //System.out.printf("valide: %b, longueur: %f\n",chemin.isValid(), chemin.getLength());
- //System.out.printf("distance réelle origine-destination (test): %f", (float)this.getInputData().getOrigin().getPoint().distanceTo(this.getInputData().getDestination().getPoint()));
-
-
- }
-
- return solution;
- }
-
- }
|