123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- 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));
- }
-
-
-
-
- }
|