Merge branch 'main' of https://git.etud.insa-toulouse.fr/bezza/BE-Graphe
This commit is contained in:
commit
367bc7e877
3 changed files with 42 additions and 12 deletions
|
|
@ -1,9 +1,16 @@
|
||||||
package org.insa.graphs.algorithm.shortestpath;
|
package org.insa.graphs.algorithm.shortestpath;
|
||||||
|
import org.insa.graphs.model.Node;
|
||||||
public class AStarAlgorithm extends DijkstraAlgorithm {
|
public class AStarAlgorithm extends DijkstraAlgorithm {
|
||||||
|
|
||||||
public AStarAlgorithm(ShortestPathData data) {
|
public AStarAlgorithm(ShortestPathData data) {
|
||||||
super(data);
|
super(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected LabelStar createLabel(Node node) {
|
||||||
|
return new LabelStar(node, false, Double.POSITIVE_INFINITY, null, getInputData().getDestination());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
super(data);
|
super(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Label createLabel(Node node) {
|
||||||
|
return new Label(node, false, Double.POSITIVE_INFINITY, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ShortestPathSolution doRun() {
|
protected ShortestPathSolution doRun() {
|
||||||
|
|
||||||
|
|
@ -33,8 +38,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
// Initialisation des labels
|
// Initialisation des labels
|
||||||
for (Node node : graph.getNodes()) {
|
for (Node node : graph.getNodes()) {
|
||||||
labels[node.getId()] =
|
labels[node.getId()] =createLabel(node);
|
||||||
new Label(node, false, Double.POSITIVE_INFINITY, null);
|
|
||||||
}
|
}
|
||||||
// Origine : coût 0, non marqué, pas de père
|
// Origine : coût 0, non marqué, pas de père
|
||||||
labels[data.getOrigin().getId()].setCoutRealise(0);
|
labels[data.getOrigin().getId()].setCoutRealise(0);
|
||||||
|
|
@ -68,13 +72,17 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
if (succLabel.getMarque())
|
if (succLabel.getMarque())
|
||||||
continue; // déjà traité
|
continue; // déjà traité
|
||||||
|
|
||||||
double newCost = LabelActuel.getCoutRealise() + data.getCost(arc);
|
double newCost = LabelActuel.getTotalCost() + data.getCost(arc);
|
||||||
|
|
||||||
if (newCost < succLabel.getCoutRealise()) {
|
//Label comp = new Label(null, false, newCost, null); //juste pour faire le compareTo entre label et pas entre double
|
||||||
|
|
||||||
|
if (newCost < succLabel.getTotalCost()) {
|
||||||
|
//int res = succLabel.compareTo(comp);
|
||||||
|
//if (res!=0){
|
||||||
// Mise à jour du coût et du prédécesseur
|
// Mise à jour du coût et du prédécesseur
|
||||||
// Si le sommet a déjà un label dans le tas, on le retire
|
// Si le sommet a déjà un label dans le tas, on le retire
|
||||||
// avant de le mettre à jour
|
// avant de le mettre à jour
|
||||||
if (succLabel.getCoutRealise() != Double.POSITIVE_INFINITY) {
|
if (succLabel.getTotalCost() != Double.POSITIVE_INFINITY) {
|
||||||
heap.remove(succLabel);
|
heap.remove(succLabel);
|
||||||
System.out.println(succLabel.getCoutRealise());// print de confirmation , pour verif si tous les couts qui sortent du tas sont croissant. getTotalcost pas croissant!!
|
System.out.println(succLabel.getCoutRealise());// print de confirmation , pour verif si tous les couts qui sortent du tas sont croissant. getTotalcost pas croissant!!
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,36 @@
|
||||||
/*package org.insa.graphs.algorithm.shortestpath;
|
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;
|
||||||
import static org.insa.graphs.model.Point.distance;
|
import org.insa.graphs.model.Point;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class LabelStar extends Label {
|
public class LabelStar extends Label {
|
||||||
|
|
||||||
private Node destination;
|
private Node destination;
|
||||||
public LabelStar(Node sommetCourant, Boolean marque, double coutRealise, Arc pere,Node destination) {
|
public LabelStar(Node sommetCourant, Boolean marque, double coutRealise, Arc pere,Node destination) {
|
||||||
super(sommetCourant, marque, coutRealise, pere);
|
super(sommetCourant, marque, coutRealise, pere);
|
||||||
this.destination=destination; //ou alors on le met juste en paramètre
|
this.destination=destination;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//pour optimiser l'algo : ATTENTION PAS à L'initialisation pas le faire sinon ça va le faire pr tout les pts
|
||||||
|
//on rajoute un paremètre distance ds le label et quand on passe sur le label si c'est linfini on le calcule
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double getTotalCost() { //pourquoi CoutRealise ? psq il est utilisé dans le compareTo
|
public double getTotalCost() { //pourquoi getTotalCost ? psq il est utilisé dans le compareTo
|
||||||
return (getCoutRealise()+distance(getSommetCourant().getPoint(),this.destination.getPoint()));
|
//System.out.println("cout realise : " + getCoutRealise()+ "\n distance : "distance(getSommetCourant().getPoint(),this.destination.getPoint()));
|
||||||
|
double cout=getCoutRealise();
|
||||||
|
Point calcul = this.destination.getPoint();
|
||||||
|
cout+=calcul.distanceTo(this.getSommetCourant().getPoint());
|
||||||
|
return (cout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//pas nécessaire normalement
|
||||||
|
/*public int compareTo(LabelStar other) {
|
||||||
|
return Double.compare(this.getTotalCost(), other.getTotalCost());
|
||||||
|
}*/
|
||||||
|
|
||||||
}*/
|
}
|
||||||
|
|
||||||
Loading…
Reference in a new issue