Added Junit tests
This commit is contained in:
parent
dc987176d1
commit
be0cd90c69
3 changed files with 189 additions and 0 deletions
|
@ -0,0 +1,20 @@
|
||||||
|
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.ShortestPathAlgorithm;
|
||||||
|
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
|
||||||
|
|
||||||
|
public class AstarAlgorithmTests extends ShortestPathTests{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ShortestPathAlgorithm getOracleAlgo(ShortestPathData data) {
|
||||||
|
return new BellmanFordAlgorithm(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ShortestPathAlgorithm getTesterAlgo(ShortestPathData data) {
|
||||||
|
return new AStarAlgorithm(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package org.insa.graphs.algorithm.utils;
|
||||||
|
|
||||||
|
import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
|
||||||
|
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
|
||||||
|
import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
|
||||||
|
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
|
||||||
|
|
||||||
|
public class DijkstraAlgorithmTests extends ShortestPathTests{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ShortestPathAlgorithm getOracleAlgo(ShortestPathData data) {
|
||||||
|
return new BellmanFordAlgorithm(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ShortestPathAlgorithm getTesterAlgo(ShortestPathData data) {
|
||||||
|
return new DijkstraAlgorithm(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,147 @@
|
||||||
|
package org.insa.graphs.algorithm.utils;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import org.insa.graphs.algorithm.ArcInspector;
|
||||||
|
import org.insa.graphs.algorithm.ArcInspectorFactory;
|
||||||
|
import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
|
||||||
|
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.Node;
|
||||||
|
import org.insa.graphs.model.io.BinaryGraphReader;
|
||||||
|
import org.insa.graphs.model.io.GraphReader;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
||||||
|
public abstract class ShortestPathTests {
|
||||||
|
|
||||||
|
|
||||||
|
Graph testGraph;
|
||||||
|
|
||||||
|
//oracle is the algorithm that has authority to determine the shortestPath
|
||||||
|
protected abstract ShortestPathAlgorithm getOracleAlgo(ShortestPathData data);
|
||||||
|
|
||||||
|
protected abstract ShortestPathAlgorithm getTesterAlgo(ShortestPathData data);
|
||||||
|
|
||||||
|
|
||||||
|
class Journey {
|
||||||
|
Node start, end;
|
||||||
|
|
||||||
|
Journey(Node start, Node end){
|
||||||
|
this.start = start;
|
||||||
|
this.end = end;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node getStart() {
|
||||||
|
return this.start;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node getEnd() {
|
||||||
|
return this.end;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() throws IOException {
|
||||||
|
// we read a graph
|
||||||
|
String testMap = "/home/adrien/git/be-graphes/maps/haute-garonne.mapgr";
|
||||||
|
GraphReader readerMap = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(testMap))));
|
||||||
|
this.testGraph = readerMap.read();
|
||||||
|
readerMap.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to get some pair of random point in the graph in order to find a path between them.
|
||||||
|
* @param quantity the number of random pair to generate in the graph
|
||||||
|
* @return Journey a Journey array object that contain start and end Node.
|
||||||
|
*/
|
||||||
|
protected Journey[] getRandomJourneys(int quantity) {
|
||||||
|
|
||||||
|
Random rand = new Random();
|
||||||
|
|
||||||
|
Journey [] allJourneys = new Journey[quantity];
|
||||||
|
|
||||||
|
List<Node> nodes = testGraph.getNodes();
|
||||||
|
int size = nodes.size();
|
||||||
|
|
||||||
|
for(int i=0; i < quantity; i++) {
|
||||||
|
Node start = nodes.get(rand.nextInt(size));
|
||||||
|
Node end;
|
||||||
|
|
||||||
|
do {
|
||||||
|
end = nodes.get(rand.nextInt(size));
|
||||||
|
} while(start.equals(end));
|
||||||
|
|
||||||
|
allJourneys[i] = new Journey(start, end);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return allJourneys;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void compareAlgorithm(int numberOfRandomTests, ArcInspector mode) {
|
||||||
|
// We are determining 10 random start/end points on the graph
|
||||||
|
Journey[] allJourneys = getRandomJourneys(numberOfRandomTests);
|
||||||
|
|
||||||
|
// We now ask to oracle the best path time based
|
||||||
|
ShortestPathSolution oracleSolutions[] = new ShortestPathSolution[numberOfRandomTests];
|
||||||
|
|
||||||
|
ShortestPathSolution testAlgoSolutions[] = new ShortestPathSolution[numberOfRandomTests];
|
||||||
|
|
||||||
|
|
||||||
|
for(int i = 0 ; i < numberOfRandomTests ; i++) {
|
||||||
|
System.out.println("Running test n°" + i + " mode = " + mode.toString());
|
||||||
|
ShortestPathData parameters = new ShortestPathData(testGraph,
|
||||||
|
allJourneys[i].getStart(), allJourneys[i].getEnd(), mode);
|
||||||
|
oracleSolutions[i] = getOracleAlgo(parameters).run();
|
||||||
|
testAlgoSolutions[i] = getTesterAlgo(parameters).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now we have all the journey, let's compare.
|
||||||
|
for(int i = 0 ; i < numberOfRandomTests ; i++) {
|
||||||
|
/**System.out.println("Comparing solution " + i + "...");
|
||||||
|
System.out.println("===================");
|
||||||
|
System.out.println("Oracle says : ");
|
||||||
|
System.out.println(oracleSolutions[i]);
|
||||||
|
System.out.println("===================");
|
||||||
|
System.out.println("Test algo says : ");
|
||||||
|
System.out.println(testAlgoSolutions[i]);
|
||||||
|
System.out.println("===================");**/
|
||||||
|
|
||||||
|
if(!oracleSolutions[i].isFeasible()) {
|
||||||
|
assertTrue(!testAlgoSolutions[i].isFeasible());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
assertTrue(oracleSolutions[i].getPath().isSamePath(testAlgoSolutions[i].getPath()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testShortestPathAlgorithmTimeBasedRandom() {
|
||||||
|
compareAlgorithm(2, ArcInspectorFactory.getAllFilters().get(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testShortestPathAlgorithmLengthBasedRandom() {
|
||||||
|
compareAlgorithm(2, ArcInspectorFactory.getAllFilters().get(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue