diff --git a/.vscode/launch.json b/.vscode/launch.json index 1f4257f..cdfdbaf 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -30,6 +30,13 @@ "request": "launch", "mainClass": "org.insa.graphs.gui.simple.Launch", "projectName": "be-graphes-gui" + }, + { + "type": "java", + "name": "RunTimeTrial", + "request": "launch", + "mainClass": "org.insa.graphs.algorithm.shortestpath.RunTimeTrial", + "projectName": "be-graphes-gui" } ] } \ No newline at end of file diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/RunTimeTrial.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/RunTimeTrial.java new file mode 100644 index 0000000..9b3ef59 --- /dev/null +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/RunTimeTrial.java @@ -0,0 +1,75 @@ +package org.insa.graphs.algorithm.shortestpath; + +import java.io.BufferedInputStream; +import java.io.DataInputStream; +import java.io.FileInputStream; +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.model.Graph; +import org.insa.graphs.model.Node; +import org.insa.graphs.model.io.BinaryGraphReader; +import org.insa.graphs.model.io.GraphReader; + +public class RunTimeTrial { + final static int TEST_COUNT = 10; + + public static void main(String[] args) throws Exception { + final Graph graph = readGraph("./Maps/france.mapgr"); + + long totalDijkstraTime = 0; + long totalAStarTime = 0; + long totalBellmanTime = 0; + + for (int i = 0; i < TEST_COUNT; i++) { + System.out.println("Running time trial " + i + "/" + TEST_COUNT); + final ShortestPathData data = getRandomShortestPathData(graph); + totalDijkstraTime += new DijkstraAlgorithm(data).run().getSolvingTime().toMillis(); + totalAStarTime += new AStarAlgorithm(data).run().getSolvingTime().toMillis(); + //totalBellmanTime += new BellmanFordAlgorithm(data).run().getSolvingTime().toMillis(); + } + + System.out.println("Dijkstra took " + totalDijkstraTime + " ms"); + System.out.println("A* took " + totalAStarTime + " ms"); + System.out.println("Bellman took " + totalBellmanTime + " ms"); + } + + + private static Node getRandomNode(Graph graph) { + final Random rand = new Random(); + final int graph_size = graph.getNodes().size(); + return graph.get(Math.abs(rand.nextInt()) % graph_size); + } + + private static ArcInspector getRandomArcFilter() { + final Random rand = new Random(); + final List filters = ArcInspectorFactory.getAllFilters(); + return filters.get(Math.abs(rand.nextInt()) % filters.size()); + } + + private static ShortestPathData getRandomShortestPathData(Graph graph) { + final Node origin = getRandomNode(graph); + final Node destination = getRandomNode(graph); + final ArcInspector filter = getRandomArcFilter(); + return new ShortestPathData(graph, origin, destination, filter); + } + + private static Graph readGraph(String mapName) throws IOException { + // Visit these directory to see the list of available files on Commetud. + // When running with VSC, paths are relative to the BE_Graphes directory. + // Create a graph reader. + final GraphReader reader = new BinaryGraphReader( + new DataInputStream(new BufferedInputStream(new FileInputStream(mapName)))); + + // Read the graph. X + final Graph graph = reader.read(); + + // Close resources + reader.close(); + + return graph; + } +}