Tests simples AStar

This commit is contained in:
Auriane Lartigue 2020-05-03 12:18:56 +02:00
parent 4ed0e12c25
commit b428e17ce8
4 changed files with 69 additions and 27 deletions

View file

@ -1,9 +1,35 @@
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.algorithm.AbstractInputData.Mode;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Point;
public class AStarAlgorithm extends DijkstraAlgorithm {
public AStarAlgorithm(ShortestPathData data) {
super(data);
}
public void init( Graph graph, Label[] TabLabel, ShortestPathData data) {
//Initialisation
double vitesse = data.getMaximumSpeed();
if(data.getMode() == Mode.LENGTH) {
vitesse = 1 ;
}
else {
if(vitesse == -1) {
vitesse = (double) data.getGraph().getGraphInformation().getMaximumSpeed() ;
}
vitesse = vitesse / 3.6 ;
}
for (Node node: graph.getNodes()) { // on crée un Label pour chaque node
double distance = Point.distance( node.getPoint(), data.getDestination().getPoint() );
TabLabel[node.getId()]= new LabelStar(node,distance/vitesse);
}
}
}

View file

@ -16,6 +16,13 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
super(data);
}
public void init( Graph graph, Label[] TabLabel, ShortestPathData data) {
//Initialisation
for (Node node: graph.getNodes()) {
TabLabel[node.getId()]= new Label(node); // on crée un Label pour chaque node
}
}
@Override
protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData();
@ -24,19 +31,16 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
Graph graph = data.getGraph();
final int nbNodes = graph.size();
BinaryHeap<Label> Tas = new BinaryHeap<Label>() ;
Label[] TabLabel = new Label[nbNodes];
//Initialisation
for (Node node: graph.getNodes()) {
TabLabel[node.getId()]= new Label(node); // on crée un Label pour chaque node
}
this.init(graph, TabLabel, data);
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
notifyOriginProcessed(data.getOrigin());
double old_cost = 0 ; // on utilisera cette variable pour vérifier si le coût est croissant
int nbMarque = 0; // correspond au nombre de sommet marqué
while(!TabLabel[data.getDestination().getId()].isMarque() ) { // tant que la destination est non marqués
@ -49,7 +53,12 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
}
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.
if(old_cost > TabLabel[X.getId()].getTotalCost()) { //Vérification que le coût des labels marqués est croissant au cours des itérations.
System.out.println("Le coût des Labels marqués n'est pas croissant");
}
old_cost = TabLabel[X.getId()].getTotalCost();
Tas.remove(TabLabel[X.getId()]);
nbMarque++;
for(Arc a : X.getSuccessors() ){ // Le nombre de successeurs explorés à chaque itération = au nombre de successeurs d'un node
@ -99,15 +108,17 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// 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");
// Vérifier que la longueur du chemin (avec methode de la class Path) est identique au PCC de l'algorithme
System.out.println("TEST : Vérifier que la longueur du chemin (avec methode de la class Path) est identique au PCC de l'algorithme");
if( (int) path.getLength() == (int)length) {
System.out.println("La longueur du chemin (avec methode de la class Path) est identique au PCC de l'algorithme");
}
else {
System.out.println( "Erreur : la longueur du chemin (avec methode de la class Path) est identique au PCC de Dijsktra ") ;
System.out.println( "Erreur : la longueur du chemin (avec methode de la class Path) n'est pas identique au PCC de l'algorithme, " + "Avec methode Path "+ path.getLength() + ", algorithme " + length) ;
}
//Vérifier que le chemin solution obtenu par Dijsktra est valide
System.out.println("TEST : Vérifier que le chemin solution obtenu par l'algorithme est bien valide");
if (path.isValid()) {
System.out.println("Le chemin obtenu est bien valide.");
}

View file

@ -48,12 +48,28 @@ public class Label implements java.lang.Comparable<Label> {
this.sommetCourant = sommetCourant;
}
public double getTotalCost() {
return this.cost;
}
public double getEstimateCost() {
return 0;
}
public int compareTo(Label autre) {
if (this.getCost() < autre.getCost()) {
if (this.getTotalCost() < autre.getTotalCost()) {
return -1;
}
else if (this.getCost() == autre.getCost() ) {
return 0;
else if (this.getTotalCost() == autre.getTotalCost() ) {
if(this.getEstimateCost() < autre.getEstimateCost()) {
return -1;
}
else if( this.getEstimateCost() > autre.getEstimateCost() ) {
return 1;
}
else {
return 0 ;
}
}
else {
return 1;

View file

@ -2,7 +2,7 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Node;
public class LabelStar extends Label implements java.lang.Comparable<Label> {
public class LabelStar extends Label {
private double EstimateCost ;
public LabelStar(Node node, double EstimateCost) {
@ -19,18 +19,7 @@ public class LabelStar extends Label implements java.lang.Comparable<Label> {
}
public double getTotalCost() {
return this.getCost() + this.getEstimateCost();
return this.EstimateCost + super.getCost() ;
}
public int compareTo(LabelStar autre) {
if (this.getTotalCost() < autre.getTotalCost()) {
return -1;
}
else if (this.getTotalCost() == autre.getTotalCost() ) {
return 0;
}
else {
return 1;
}
}
}