Implemented A* tests
This commit is contained in:
parent
8e58060d01
commit
f658b678af
3 changed files with 197 additions and 155 deletions
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,129 +1,15 @@
|
||||||
package org.insa.graphs.algorithm.utils;
|
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.BellmanFordAlgorithm;
|
||||||
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
|
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
|
||||||
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
|
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 java.util.ArrayList;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
public class DijkstraTest extends AlgoTest {
|
||||||
|
|
||||||
public class DijkstraTest {
|
protected void computeValidPathSolutions() {
|
||||||
|
this.validPathSolutionList = new ArrayList<>();
|
||||||
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<>();
|
|
||||||
validPathOptimalSolutionList = new ArrayList<>();
|
validPathOptimalSolutionList = new ArrayList<>();
|
||||||
for (ShortestPathData path : validPathList) {
|
for (ShortestPathData path : validPathList) {
|
||||||
validPathSolutionList.add(new DijkstraAlgorithm(path).run());
|
validPathSolutionList.add(new DijkstraAlgorithm(path).run());
|
||||||
|
@ -131,7 +17,7 @@ public class DijkstraTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void computeInvalidPathSolutions() {
|
protected void computeInvalidPathSolutions() {
|
||||||
invalidPathSolutionList = new ArrayList<>();
|
invalidPathSolutionList = new ArrayList<>();
|
||||||
invalidPathOptimalSolutionList = new ArrayList<>();
|
invalidPathOptimalSolutionList = new ArrayList<>();
|
||||||
for (ShortestPathData path : invalidPathList) {
|
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue