AStar done :) :hapy:
This commit is contained in:
parent
d203e56988
commit
bf2c35eadd
4 changed files with 39 additions and 32 deletions
|
|
@ -1,9 +1,17 @@
|
||||||
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 Label createLabel(Node node) {
|
||||||
|
return new LabelStar(node, getInputData().getDestination());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||||
import org.insa.graphs.model.Arc;
|
import org.insa.graphs.model.Arc;
|
||||||
import org.insa.graphs.model.Graph;
|
import org.insa.graphs.model.Graph;
|
||||||
import org.insa.graphs.model.Path;
|
import org.insa.graphs.model.Path;
|
||||||
|
import org.insa.graphs.model.Node;
|
||||||
|
|
||||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
|
|
@ -14,6 +15,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
super(data);
|
super(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Label createLabel(Node node) {
|
||||||
|
return new Label(node);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ShortestPathSolution doRun() {
|
protected ShortestPathSolution doRun() {
|
||||||
|
|
||||||
|
|
@ -30,7 +35,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
// Initialize array of labels.
|
// Initialize array of labels.
|
||||||
ArrayList<Label> labelsList = new ArrayList<Label>();
|
ArrayList<Label> labelsList = new ArrayList<Label>();
|
||||||
for (int i=0; i<nbNodes; i++) {
|
for (int i=0; i<nbNodes; i++) {
|
||||||
labelsList.add(new Label(graph.get(i)));
|
labelsList.add(createLabel(graph.get(i)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -58,11 +63,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
}
|
}
|
||||||
|
|
||||||
Label successorLabel = labelsList.get(arc.getDestination().getId());
|
Label successorLabel = labelsList.get(arc.getDestination().getId());
|
||||||
Double newCost = data.getCost(arc) + concurentNodeLabel.getCost();
|
Double newCost = concurentNodeLabel.getCoutRealise() + data.getCost(arc);
|
||||||
if (successorLabel.getCost() == Double.POSITIVE_INFINITY) {
|
if (successorLabel.getCoutRealise() == Double.POSITIVE_INFINITY) {
|
||||||
notifyNodeReached(arc.getDestination());
|
notifyNodeReached(arc.getDestination());
|
||||||
}
|
}
|
||||||
if (newCost < successorLabel.getCost()) {
|
if (newCost < successorLabel.getCoutRealise()) {
|
||||||
successorLabel.setPere(arc);
|
successorLabel.setPere(arc);
|
||||||
successorLabel.setCoutRealise(newCost);
|
successorLabel.setCoutRealise(newCost);
|
||||||
labelsHeap.insert(successorLabel);
|
labelsHeap.insert(successorLabel);
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,15 @@ public class Label implements Comparable<Label> {
|
||||||
|
|
||||||
private double coutRealise;
|
private double coutRealise;
|
||||||
|
|
||||||
|
private double coutEstime;
|
||||||
|
|
||||||
private Arc pere;
|
private Arc pere;
|
||||||
|
|
||||||
public Label(Node sommetCourant) {
|
public Label(Node sommetCourant) {
|
||||||
this.sommetCourant = sommetCourant;
|
this.sommetCourant = sommetCourant;
|
||||||
this.marque = false;
|
this.marque = false;
|
||||||
this.coutRealise = Double.POSITIVE_INFINITY;
|
this.coutRealise = Double.POSITIVE_INFINITY;
|
||||||
|
this.coutEstime = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getSommetCourant() {
|
public Node getSommetCourant() {
|
||||||
|
|
@ -39,21 +42,34 @@ public class Label implements Comparable<Label> {
|
||||||
this.pere = pere;
|
this.pere = pere;
|
||||||
}
|
}
|
||||||
|
|
||||||
// public double getCoutRealise() {
|
public double getCoutRealise() {
|
||||||
// return coutRealise;
|
return coutRealise;
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
public double getCoutEstime() {
|
||||||
|
return coutEstime;
|
||||||
|
}
|
||||||
|
|
||||||
public void setCoutRealise(double nouveauCout) {
|
public void setCoutRealise(double nouveauCout) {
|
||||||
this.coutRealise = nouveauCout;
|
this.coutRealise = nouveauCout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setCoutEstime(double nouveauCout) {
|
||||||
|
this.coutEstime = nouveauCout;
|
||||||
|
}
|
||||||
|
|
||||||
public double getCost() {
|
public double getCost() {
|
||||||
return coutRealise;
|
return coutRealise + coutEstime;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Label o) {
|
public int compareTo(Label o) {
|
||||||
return Double.compare(this.coutRealise, o.getCost()) ;
|
if (this.getCost() == o.getCost()) {
|
||||||
|
return Double.compare(this.getCoutEstime(), o.getCoutEstime());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return Double.compare(this.getCost(), o.getCost());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,31 +5,9 @@ import org.insa.graphs.model.Point;
|
||||||
|
|
||||||
public class LabelStar extends Label {
|
public class LabelStar extends Label {
|
||||||
|
|
||||||
private double volOiseau;
|
|
||||||
|
|
||||||
public LabelStar(Node sommetCourant, Node sommetDestination) {
|
public LabelStar(Node sommetCourant, Node sommetDestination) {
|
||||||
super(sommetCourant);
|
super(sommetCourant);
|
||||||
volOiseau = Point.distance(this.getSommetCourant().getPoint(), sommetDestination.getPoint());
|
this.setCoutEstime(Point.distance(this.getSommetCourant().getPoint(), sommetDestination.getPoint()));
|
||||||
}
|
|
||||||
|
|
||||||
public double getVolOiseau() {
|
|
||||||
return volOiseau;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public double getCost() {
|
|
||||||
return super.getCost() + this.volOiseau;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int compareTo(LabelStar o) {
|
|
||||||
double coutthis = this.getCost();
|
|
||||||
double coutautre = o.getCost();
|
|
||||||
if (coutthis == coutautre) {
|
|
||||||
return Double.compare(this.volOiseau, o.getVolOiseau());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return Double.compare(coutthis, coutautre);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue