Compare commits

...

2 commits

Author SHA1 Message Date
f658b678af Implemented A* tests 2020-04-22 16:54:19 +02:00
8e58060d01 Implemented Astar 2020-04-22 16:27:01 +02:00
8 changed files with 266 additions and 166 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());
}
}

View file

@ -0,0 +1,30 @@
package org.insa.graphs.algorithm.utils;
import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm;
import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
import java.util.ArrayList;
public class AStarTest extends AlgoTest {
protected void computeValidPathSolutions() {
this.validPathSolutionList = new ArrayList<>();
validPathOptimalSolutionList = new ArrayList<>();
for (ShortestPathData path : validPathList) {
validPathSolutionList.add(new AStarAlgorithm(path).run());
validPathOptimalSolutionList.add(new BellmanFordAlgorithm(path).run());
}
}
protected void computeInvalidPathSolutions() {
invalidPathSolutionList = new ArrayList<>();
invalidPathOptimalSolutionList = new ArrayList<>();
for (ShortestPathData path : invalidPathList) {
invalidPathSolutionList.add(new AStarAlgorithm(path).run());
invalidPathOptimalSolutionList.add(new BellmanFordAlgorithm(path).run());
}
}
}

View file

@ -0,0 +1,163 @@
package org.insa.graphs.algorithm.utils;
import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.algorithm.ArcInspectorFactory;
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.io.BinaryGraphReader;
import org.insa.graphs.model.io.GraphReader;
import org.junit.Before;
import org.junit.Test;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import static org.junit.Assert.*;
public abstract class AlgoTest {
private static ArcInspector filterLength, filterTime;
protected ArrayList<ShortestPathData> validPathList;
protected ArrayList<ShortestPathSolution> validPathSolutionList;
protected ArrayList<ShortestPathSolution> validPathOptimalSolutionList;
protected ArrayList<ShortestPathData> invalidPathList;
protected ArrayList<ShortestPathSolution> invalidPathSolutionList;
protected ArrayList<ShortestPathSolution> invalidPathOptimalSolutionList;
// List of graphs
@SuppressWarnings("unused")
private Graph graphInsa, graphSquare, graphBretagne;
private Graph readGraph(String map) throws IOException {
final GraphReader reader = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(map))));
return reader.read();
}
private void initGraphs() throws IOException {
graphInsa = readGraph("../Maps/insa.mapgr");
graphSquare = readGraph("../Maps/carre.mapgr");
graphBretagne = readGraph("../Maps/bretagne.mapgr");
}
private void initFilters() {
filterLength = ArcInspectorFactory.getAllFilters().get(0);
filterTime = ArcInspectorFactory.getAllFilters().get(2);
}
private void initValidPathList() {
validPathList = new ArrayList<>();
validPathList.add(new ShortestPathData(
graphInsa,
graphInsa.getNodes().get(512),
graphInsa.getNodes().get(526),
filterLength));
validPathList.add(new ShortestPathData(
graphInsa,
graphInsa.getNodes().get(512),
graphInsa.getNodes().get(526),
filterTime));
validPathList.add(new ShortestPathData(
graphSquare,
graphSquare.getNodes().get(21),
graphSquare.getNodes().get(17),
filterLength));
validPathList.add(new ShortestPathData(
graphSquare,
graphSquare.getNodes().get(21),
graphSquare.getNodes().get(17),
filterTime));
}
private void initInvalidPathList() {
invalidPathList = new ArrayList<>();
invalidPathList.add(new ShortestPathData(
graphBretagne,
graphBretagne.getNodes().get(29270),
graphBretagne.getNodes().get(545599),
filterLength));
invalidPathList.add(new ShortestPathData(
graphBretagne,
graphBretagne.getNodes().get(29270),
graphBretagne.getNodes().get(545599),
filterTime));
invalidPathList.add(new ShortestPathData(
graphSquare,
graphSquare.getNodes().get(10),
graphSquare.getNodes().get(10),
filterLength));
invalidPathList.add(new ShortestPathData(
graphSquare,
graphSquare.getNodes().get(10),
graphSquare.getNodes().get(10),
filterTime));
}
@Before
public void init() {
initFilters();
try {
initGraphs();
} catch (IOException e) {
e.printStackTrace();
}
initValidPathList();
initInvalidPathList();
computeValidPathSolutions();
computeInvalidPathSolutions();
}
protected abstract void computeValidPathSolutions();
protected abstract void computeInvalidPathSolutions();
@Test
public void testPathValid() {
for (ShortestPathSolution solution : validPathSolutionList) {
assertTrue(solution.isFeasible());
assertTrue(solution.getPath().isValid());
}
}
@Test
public void testPathInvalid() {
for (ShortestPathSolution solution : invalidPathSolutionList) {
assertFalse(solution.isFeasible());
}
}
@Test
public void testPathOptimalWithOracle() {
for (int i = 0; i < validPathSolutionList.size(); i++) {
System.out.println(i);
assertEquals(
validPathSolutionList.get(i).getPath().getLength(),
validPathOptimalSolutionList.get(i).getPath().getLength(), 0.0
);
assertEquals(
validPathSolutionList.get(i).getPath().getMinimumTravelTime(),
validPathOptimalSolutionList.get(i).getPath().getMinimumTravelTime(), 0.0
);
}
for (int i = 0; i < invalidPathSolutionList.size(); i++) {
assertEquals(validPathSolutionList.get(i).isFeasible(),
validPathOptimalSolutionList.get(i).isFeasible());
}
}
@Test
public void testPathOptimalWithoutOracle() {
// TODO
}
}

View file

@ -1,129 +1,15 @@
package org.insa.graphs.algorithm.utils;
import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.algorithm.ArcInspectorFactory;
import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.io.BinaryGraphReader;
import org.insa.graphs.model.io.GraphReader;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import static org.junit.Assert.*;
public class DijkstraTest extends AlgoTest {
public class DijkstraTest {
private static ArcInspector filterLength, filterTime;
private static ArrayList<ShortestPathData> validPathList;
private static ArrayList<ShortestPathSolution> validPathSolutionList;
private static ArrayList<ShortestPathSolution> validPathOptimalSolutionList;
private static ArrayList<ShortestPathData> invalidPathList;
private static ArrayList<ShortestPathSolution> invalidPathSolutionList;
private static ArrayList<ShortestPathSolution> invalidPathOptimalSolutionList;
// List of graphs
@SuppressWarnings("unused")
private static Graph graphInsa, graphSquare, graphBretagne;
private static Graph readGraph(String map) throws IOException {
final GraphReader reader = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(map))));
return reader.read();
}
private static void initGraphs() throws IOException {
graphInsa = readGraph("../Maps/insa.mapgr");
graphSquare = readGraph("../Maps/carre.mapgr");
graphBretagne = readGraph("../Maps/bretagne.mapgr");
}
private static void initFilters() {
filterLength = ArcInspectorFactory.getAllFilters().get(0);
filterTime = ArcInspectorFactory.getAllFilters().get(2);
}
private static void initValidPathList() {
validPathList = new ArrayList<>();
validPathList.add(new ShortestPathData(
graphInsa,
graphInsa.getNodes().get(512),
graphInsa.getNodes().get(526),
filterLength));
validPathList.add(new ShortestPathData(
graphInsa,
graphInsa.getNodes().get(512),
graphInsa.getNodes().get(526),
filterTime));
validPathList.add(new ShortestPathData(
graphSquare,
graphSquare.getNodes().get(21),
graphSquare.getNodes().get(17),
filterLength));
validPathList.add(new ShortestPathData(
graphSquare,
graphSquare.getNodes().get(21),
graphSquare.getNodes().get(17),
filterTime));
}
private static void initInvalidPathList() {
invalidPathList = new ArrayList<>();
invalidPathList.add(new ShortestPathData(
graphBretagne,
graphBretagne.getNodes().get(29270),
graphBretagne.getNodes().get(545599),
filterLength));
invalidPathList.add(new ShortestPathData(
graphBretagne,
graphBretagne.getNodes().get(29270),
graphBretagne.getNodes().get(545599),
filterTime));
invalidPathList.add(new ShortestPathData(
graphSquare,
graphSquare.getNodes().get(10),
graphSquare.getNodes().get(10),
filterLength));
invalidPathList.add(new ShortestPathData(
graphSquare,
graphSquare.getNodes().get(10),
graphSquare.getNodes().get(10),
filterTime));
}
@BeforeClass
public static void initAll() {
initFilters();
try {
initGraphs();
} catch (IOException e) {
e.printStackTrace();
}
initValidPathList();
initInvalidPathList();
computeValidPathSolutions();
computeInvalidPathSolutions();
}
private static void computeValidPathSolutions() {
validPathSolutionList = new ArrayList<>();
protected void computeValidPathSolutions() {
this.validPathSolutionList = new ArrayList<>();
validPathOptimalSolutionList = new ArrayList<>();
for (ShortestPathData path : validPathList) {
validPathSolutionList.add(new DijkstraAlgorithm(path).run());
@ -131,7 +17,7 @@ public class DijkstraTest {
}
}
private static void computeInvalidPathSolutions() {
protected void computeInvalidPathSolutions() {
invalidPathSolutionList = new ArrayList<>();
invalidPathOptimalSolutionList = new ArrayList<>();
for (ShortestPathData path : invalidPathList) {
@ -140,41 +26,4 @@ public class DijkstraTest {
}
}
@Test
public void testPathValid() {
for (ShortestPathSolution solution : validPathSolutionList) {
assertTrue(solution.isFeasible());
assertTrue(solution.getPath().isValid());
}
}
@Test
public void testPathInvalid() {
for (ShortestPathSolution solution : invalidPathSolutionList) {
assertFalse(solution.isFeasible());
}
}
@Test
public void testPathOptimalWithOracle() {
for (int i = 0; i < validPathSolutionList.size(); i++) {
assertEquals(
validPathSolutionList.get(i).getPath().getLength(),
validPathOptimalSolutionList.get(i).getPath().getLength(), 0.0
);
assertEquals(
validPathSolutionList.get(i).getPath().getMinimumTravelTime(),
validPathOptimalSolutionList.get(i).getPath().getMinimumTravelTime(), 0.0
);
}
for (int i = 0; i < invalidPathSolutionList.size(); i++) {
assertEquals(validPathSolutionList.get(i).isFeasible(),
validPathOptimalSolutionList.get(i).isFeasible());
}
}
@Test
public void testPathOptimalWithoutOracle() {
// TODO
}
}