Tests de correction et debut LabelStar
This commit is contained in:
parent
d258b473c7
commit
4ed0e12c25
3 changed files with 91 additions and 12 deletions
|
@ -2,9 +2,9 @@ package org.insa.graphs.algorithm.shortestpath;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.insa.graphs.algorithm.AbstractSolution.Status;
|
||||
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||
import org.insa.graphs.algorithm.utils.EmptyPriorityQueueException;
|
||||
import org.insa.graphs.model.Graph;
|
||||
import org.insa.graphs.model.Node;
|
||||
import org.insa.graphs.model.Path;
|
||||
|
@ -35,23 +35,35 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
TabLabel[data.getOrigin().getId()].setCost(0); // on met le cost du sommet à 0
|
||||
Tas.insert(TabLabel[data.getOrigin().getId()]); // on insert le label du sommet dans le tas
|
||||
|
||||
//int nbMarque = 0; // correspond au nombre de sommet marqué
|
||||
notifyOriginProcessed(data.getOrigin());
|
||||
|
||||
|
||||
int nbMarque = 0; // correspond au nombre de sommet marqué
|
||||
while(!TabLabel[data.getDestination().getId()].isMarque() ) { // tant que la destination est non marqués
|
||||
Node X = Tas.findMin().getSommetCourant(); // on prend le sommet du tas et on le stocke dans X
|
||||
Node X ;
|
||||
try {
|
||||
X = Tas.findMin().getSommetCourant(); // on prend le sommet du tas et on le stocke dans X
|
||||
}
|
||||
catch(EmptyPriorityQueueException e) {
|
||||
break;
|
||||
}
|
||||
TabLabel[X.getId()].setMarque(true); // on le marque
|
||||
this.notifyNodeMarked(X);
|
||||
//System.out.println(TabLabel[X.getId()].getCost()); //Vérification que le coût des labels marqués est croissant au cours des itérations.
|
||||
Tas.remove(TabLabel[X.getId()]);
|
||||
//nbMarque++;
|
||||
for(Arc a : X.getSuccessors() ){
|
||||
nbMarque++;
|
||||
for(Arc a : X.getSuccessors() ){ // Le nombre de successeurs explorés à chaque itération = au nombre de successeurs d'un node
|
||||
if (data.isAllowed(a)) {
|
||||
Node Y = a.getDestination();
|
||||
if(!(TabLabel[Y.getId()].isMarque() ) ) {
|
||||
if(TabLabel[Y.getId()].getCost()==Double.POSITIVE_INFINITY ) { //Y n'est pas dans le tas
|
||||
TabLabel[Y.getId()].setCost( TabLabel[X.getId()].getCost()+ a.getLength() );
|
||||
TabLabel[Y.getId()].setCost( TabLabel[X.getId()].getCost()+ data.getCost(a) );
|
||||
Tas.insert(TabLabel[Y.getId()]);
|
||||
TabLabel[Y.getId()].setFather(a);
|
||||
this.notifyNodeReached(Y);
|
||||
}
|
||||
else if(TabLabel[Y.getId()].getCost() > TabLabel[X.getId()].getCost()+ a.getLength()) {
|
||||
TabLabel[Y.getId()].setCost( TabLabel[X.getId()].getCost()+ a.getLength() );
|
||||
else if(TabLabel[Y.getId()].getCost() > TabLabel[X.getId()].getCost()+ data.getCost(a)) {
|
||||
TabLabel[Y.getId()].setCost( TabLabel[X.getId()].getCost()+ data.getCost(a) );
|
||||
Tas.remove(TabLabel[Y.getId()]);
|
||||
Tas.insert(TabLabel[Y.getId()]);
|
||||
TabLabel[Y.getId()].setFather(a);
|
||||
|
@ -60,8 +72,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create the path from the array of predecessors...
|
||||
ArrayList<Arc> arcs = new ArrayList<>();
|
||||
|
||||
Node dest = data.getDestination();
|
||||
Label Labeldest = TabLabel[dest.getId()];
|
||||
Arc a = Labeldest.getFather();
|
||||
|
@ -70,17 +84,44 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
|
||||
}
|
||||
else {
|
||||
float length = 0;
|
||||
notifyDestinationReached(dest);
|
||||
while (a != null ) {
|
||||
arcs.add(a);
|
||||
length += a.getLength();
|
||||
a = TabLabel[a.getOrigin().getId()].getFather();
|
||||
System.out.println(a);
|
||||
//System.out.println(a);
|
||||
}
|
||||
|
||||
|
||||
Collections.reverse(arcs);
|
||||
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
|
||||
}
|
||||
Path path = new Path(graph, arcs);
|
||||
|
||||
|
||||
|
||||
// Vérifier que la longueur du chemin (avec methode de la class Path) est identique au PCC de Dijsktra
|
||||
if(path.getLength() == length) {
|
||||
System.out.println(" la longueur du chemin (avec methode de la class Path) est identique au PCC de Dijsktra");
|
||||
}
|
||||
else {
|
||||
System.out.println( "Erreur : la longueur du chemin (avec methode de la class Path) est identique au PCC de Dijsktra ") ;
|
||||
}
|
||||
|
||||
//Vérifier que le chemin solution obtenu par Dijsktra est valide
|
||||
if (path.isValid()) {
|
||||
System.out.println("Le chemin obtenu est bien valide.");
|
||||
}
|
||||
else {
|
||||
System.out.println("Le chemin obtenu n'est pas valide.");
|
||||
}
|
||||
|
||||
|
||||
|
||||
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));}
|
||||
|
||||
|
||||
System.out.println("Nb iterations: " + nbMarque + ", Nb arcs du PCC: " + arcs.size());
|
||||
return solution;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package org.insa.graphs.algorithm.shortestpath;
|
||||
|
||||
import org.insa.graphs.model.Node;
|
||||
|
||||
public class LabelStar extends Label implements java.lang.Comparable<Label> {
|
||||
private double EstimateCost ;
|
||||
|
||||
public LabelStar(Node node, double EstimateCost) {
|
||||
super(node);
|
||||
this.EstimateCost = EstimateCost ;
|
||||
}
|
||||
|
||||
public double getEstimateCost() {
|
||||
return EstimateCost;
|
||||
}
|
||||
|
||||
public void setEstimateCost(double EstimateCost) {
|
||||
this.EstimateCost = EstimateCost;
|
||||
}
|
||||
|
||||
public double getTotalCost() {
|
||||
return this.getCost() + this.getEstimateCost();
|
||||
}
|
||||
|
||||
public int compareTo(LabelStar autre) {
|
||||
if (this.getTotalCost() < autre.getTotalCost()) {
|
||||
return -1;
|
||||
}
|
||||
else if (this.getTotalCost() == autre.getTotalCost() ) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -219,5 +219,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
public String toString() {
|
||||
return BinaryHeapFormatter.toStringTree(this, 8);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue