Tests simples AStar
This commit is contained in:
parent
4ed0e12c25
commit
b428e17ce8
4 changed files with 69 additions and 27 deletions
|
@ -1,9 +1,35 @@
|
||||||
package org.insa.graphs.algorithm.shortestpath;
|
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 class AStarAlgorithm extends DijkstraAlgorithm {
|
||||||
|
|
||||||
public AStarAlgorithm(ShortestPathData data) {
|
public AStarAlgorithm(ShortestPathData data) {
|
||||||
super(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,13 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
super(data);
|
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
|
@Override
|
||||||
protected ShortestPathSolution doRun() {
|
protected ShortestPathSolution doRun() {
|
||||||
final ShortestPathData data = getInputData();
|
final ShortestPathData data = getInputData();
|
||||||
|
@ -24,19 +31,16 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
Graph graph = data.getGraph();
|
Graph graph = data.getGraph();
|
||||||
final int nbNodes = graph.size();
|
final int nbNodes = graph.size();
|
||||||
BinaryHeap<Label> Tas = new BinaryHeap<Label>() ;
|
BinaryHeap<Label> Tas = new BinaryHeap<Label>() ;
|
||||||
|
|
||||||
Label[] TabLabel = new Label[nbNodes];
|
Label[] TabLabel = new Label[nbNodes];
|
||||||
|
|
||||||
//Initialisation
|
this.init(graph, TabLabel, data);
|
||||||
for (Node node: graph.getNodes()) {
|
|
||||||
TabLabel[node.getId()]= new Label(node); // on crée un Label pour chaque node
|
|
||||||
}
|
|
||||||
|
|
||||||
TabLabel[data.getOrigin().getId()].setCost(0); // on met le cost du sommet à 0
|
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
|
Tas.insert(TabLabel[data.getOrigin().getId()]); // on insert le label du sommet dans le tas
|
||||||
|
|
||||||
notifyOriginProcessed(data.getOrigin());
|
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é
|
int nbMarque = 0; // correspond au nombre de sommet marqué
|
||||||
while(!TabLabel[data.getDestination().getId()].isMarque() ) { // tant que la destination est non marqués
|
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
|
TabLabel[X.getId()].setMarque(true); // on le marque
|
||||||
this.notifyNodeMarked(X);
|
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()]);
|
Tas.remove(TabLabel[X.getId()]);
|
||||||
nbMarque++;
|
nbMarque++;
|
||||||
for(Arc a : X.getSuccessors() ){ // Le nombre de successeurs explorés à chaque itération = au nombre de successeurs d'un node
|
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
|
// Vérifier que la longueur du chemin (avec methode de la class Path) est identique au PCC de l'algorithme
|
||||||
if(path.getLength() == length) {
|
System.out.println("TEST : Vérifier que la longueur du chemin (avec methode de la class Path) est identique au PCC de l'algorithme");
|
||||||
System.out.println(" la longueur du chemin (avec methode de la class Path) est identique au PCC de Dijsktra");
|
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 {
|
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
|
//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()) {
|
if (path.isValid()) {
|
||||||
System.out.println("Le chemin obtenu est bien valide.");
|
System.out.println("Le chemin obtenu est bien valide.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,12 +48,28 @@ public class Label implements java.lang.Comparable<Label> {
|
||||||
this.sommetCourant = sommetCourant;
|
this.sommetCourant = sommetCourant;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double getTotalCost() {
|
||||||
|
return this.cost;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getEstimateCost() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
public int compareTo(Label autre) {
|
public int compareTo(Label autre) {
|
||||||
if (this.getCost() < autre.getCost()) {
|
if (this.getTotalCost() < autre.getTotalCost()) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
else if (this.getCost() == autre.getCost() ) {
|
else if (this.getTotalCost() == autre.getTotalCost() ) {
|
||||||
return 0;
|
if(this.getEstimateCost() < autre.getEstimateCost()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else if( this.getEstimateCost() > autre.getEstimateCost() ) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return 0 ;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
@ -2,7 +2,7 @@ package org.insa.graphs.algorithm.shortestpath;
|
||||||
|
|
||||||
import org.insa.graphs.model.Node;
|
import org.insa.graphs.model.Node;
|
||||||
|
|
||||||
public class LabelStar extends Label implements java.lang.Comparable<Label> {
|
public class LabelStar extends Label {
|
||||||
private double EstimateCost ;
|
private double EstimateCost ;
|
||||||
|
|
||||||
public LabelStar(Node node, 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() {
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue