Implemented Astar
This commit is contained in:
parent
b57065913e
commit
8e58060d01
5 changed files with 69 additions and 11 deletions
|
@ -1,9 +1,28 @@
|
||||||
package org.insa.graphs.algorithm.shortestpath;
|
package org.insa.graphs.algorithm.shortestpath;
|
||||||
|
|
||||||
|
import org.insa.graphs.model.Graph;
|
||||||
|
|
||||||
public class AStarAlgorithm extends DijkstraAlgorithm {
|
public class AStarAlgorithm extends DijkstraAlgorithm {
|
||||||
|
|
||||||
public AStarAlgorithm(ShortestPathData data) {
|
public AStarAlgorithm(ShortestPathData data) {
|
||||||
super(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
package org.insa.graphs.algorithm.shortestpath;
|
package org.insa.graphs.algorithm.shortestpath;
|
||||||
|
|
||||||
import org.insa.graphs.algorithm.AbstractSolution;
|
import org.insa.graphs.algorithm.AbstractSolution;
|
||||||
|
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||||
import org.insa.graphs.algorithm.utils.ElementNotFoundException;
|
import org.insa.graphs.algorithm.utils.ElementNotFoundException;
|
||||||
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.algorithm.utils.BinaryHeap;
|
|
||||||
import org.insa.graphs.model.Node;
|
import org.insa.graphs.model.Node;
|
||||||
import org.insa.graphs.model.Path;
|
import org.insa.graphs.model.Path;
|
||||||
|
|
||||||
|
@ -22,15 +22,16 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
@Override
|
@Override
|
||||||
protected ShortestPathSolution doRun() {
|
protected ShortestPathSolution doRun() {
|
||||||
final ShortestPathData data = getInputData();
|
final ShortestPathData data = getInputData();
|
||||||
|
|
||||||
Graph graph = data.getGraph();
|
Graph graph = data.getGraph();
|
||||||
final int nbNodes = graph.size();
|
final int nbNodes = graph.size();
|
||||||
|
|
||||||
// Initialize array of Labels.
|
|
||||||
Label[] labels = new Label[nbNodes];
|
Label[] labels = new Label[nbNodes];
|
||||||
for (int i = 0; i < nbNodes; i++) {
|
for (int i = 0; i < nbNodes; i++) {
|
||||||
labels[i] = new Label(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 origin = data.getOrigin().getId();
|
||||||
final int destination = data.getDestination().getId();
|
final int destination = data.getDestination().getId();
|
||||||
labels[origin].setCost(0);
|
labels[origin].setCost(0);
|
||||||
|
@ -90,6 +91,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
return solution;
|
return solution;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void log(String message) {
|
private void log(String message) {
|
||||||
if (this.DEBUG)
|
if (this.DEBUG)
|
||||||
System.out.println(message);
|
System.out.println(message);
|
||||||
|
|
|
@ -3,7 +3,7 @@ package org.insa.graphs.algorithm.shortestpath;
|
||||||
import org.insa.graphs.model.Arc;
|
import org.insa.graphs.model.Arc;
|
||||||
|
|
||||||
public class Label implements Comparable<Label> {
|
public class Label implements Comparable<Label> {
|
||||||
private int associatedNode;
|
private final int associatedNode;
|
||||||
private double cost;
|
private double cost;
|
||||||
|
|
||||||
private boolean marked;
|
private boolean marked;
|
||||||
|
@ -45,8 +45,12 @@ public class Label implements Comparable<Label> {
|
||||||
this.fatherArc = fatherArc;
|
this.fatherArc = fatherArc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double getTotalCost() {
|
||||||
|
return this.getCost();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Label label) {
|
public int compareTo(Label label) {
|
||||||
return this.cost - label.getCost() > 0 ? 1 : -1;
|
return Double.compare(this.getTotalCost(), label.getTotalCost());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -149,13 +149,13 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
||||||
else if (index == this.currentSize-1)
|
else if (index == this.currentSize-1)
|
||||||
this.currentSize--;
|
this.currentSize--;
|
||||||
else {
|
else {
|
||||||
System.out.println(this.toStringTree());
|
// System.out.println(this.toStringTree());
|
||||||
E lastItem = this.array.get(--this.currentSize);
|
E lastItem = this.array.get(--this.currentSize);
|
||||||
System.out.println(lastItem);
|
// System.out.println(lastItem);
|
||||||
this.arraySet(index, lastItem);
|
this.arraySet(index, lastItem);
|
||||||
this.percolateDown(index);
|
this.percolateDown(index);
|
||||||
this.percolateUp(index);
|
this.percolateUp(index);
|
||||||
System.out.println(this.toStringTree());
|
// System.out.println(this.toStringTree());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue