Implemented Astar

This commit is contained in:
Arnaud Vergnet 2020-04-22 16:27:01 +02:00
parent b57065913e
commit 8e58060d01
5 changed files with 69 additions and 11 deletions

View file

@ -1,9 +1,28 @@
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Graph;
public class AStarAlgorithm extends DijkstraAlgorithm {
public AStarAlgorithm(ShortestPathData data) {
super(data);
}
@Override
protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData();
Graph graph = data.getGraph();
final int nbNodes = graph.size();
LabelStar[] labels = new LabelStar[nbNodes];
for (int i = 0; i < nbNodes; i++) {
labels[i] = new LabelStar(i);
final double estimatedCost = graph.getNodes().get(i).getPoint().distanceTo(
data.getDestination().getPoint()
);
labels[i].setEstimatedCost(estimatedCost);
}
return this.doDijkstra(labels, data, graph);
}
}

View file

@ -1,10 +1,10 @@
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.algorithm.AbstractSolution;
import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.algorithm.utils.ElementNotFoundException;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph;
import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
@ -22,15 +22,16 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@Override
protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData();
Graph graph = data.getGraph();
final int nbNodes = graph.size();
// Initialize array of Labels.
Label[] labels = new Label[nbNodes];
for (int i = 0; i < nbNodes; i++) {
labels[i] = new Label(i);
}
return this.doDijkstra(labels, data, graph);
}
protected ShortestPathSolution doDijkstra(Label[] labels, ShortestPathData data, Graph graph) {
final int origin = data.getOrigin().getId();
final int destination = data.getDestination().getId();
labels[origin].setCost(0);
@ -39,7 +40,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
BinaryHeap<Label> heap = new BinaryHeap<>();
heap.insert(labels[origin]);
int iterationCounter= 0;
int iterationCounter = 0;
while (!heap.isEmpty() && labels[destination].isNotMarked()) {
iterationCounter++;
@ -90,11 +91,12 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
return solution;
}
private void log(String message) {
if (this.DEBUG)
System.out.println(message);
}
private void updateCost(Arc arc, Label parent, Label current, BinaryHeap<Label> heap, ShortestPathData data) {
double newCost = parent.getCost() + data.getCost(arc);
if (newCost < current.getCost()) {

View file

@ -3,7 +3,7 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Arc;
public class Label implements Comparable<Label> {
private int associatedNode;
private final int associatedNode;
private double cost;
private boolean marked;
@ -45,8 +45,12 @@ public class Label implements Comparable<Label> {
this.fatherArc = fatherArc;
}
public double getTotalCost() {
return this.getCost();
}
@Override
public int compareTo(Label label) {
return this.cost - label.getCost() > 0 ? 1 : -1;
return Double.compare(this.getTotalCost(), label.getTotalCost());
}
}

View file

@ -0,0 +1,33 @@
package org.insa.graphs.algorithm.shortestpath;
public class LabelStar extends Label {
private double estimatedCost;
public LabelStar(int associatedNode) {
super(associatedNode);
this.estimatedCost = Double.POSITIVE_INFINITY;
}
public double getEstimatedCost() {
return estimatedCost;
}
public void setEstimatedCost(double estimatedCost) {
this.estimatedCost = estimatedCost;
}
@Override
public double getTotalCost() {
return this.getCost() + this.getEstimatedCost();
}
@Override
public int compareTo(Label label) {
final int comparison = super.compareTo(label);
return comparison == 0
? Double.compare(this.getEstimatedCost(), label.getTotalCost() - label.getCost())
: comparison;
}
}

View file

@ -149,13 +149,13 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
else if (index == this.currentSize-1)
this.currentSize--;
else {
System.out.println(this.toStringTree());
// System.out.println(this.toStringTree());
E lastItem = this.array.get(--this.currentSize);
System.out.println(lastItem);
// System.out.println(lastItem);
this.arraySet(index, lastItem);
this.percolateDown(index);
this.percolateUp(index);
System.out.println(this.toStringTree());
// System.out.println(this.toStringTree());
}
}