feat(time-trial): add time trial
This commit is contained in:
parent
d7c938cc76
commit
e4c6f656f9
2 changed files with 82 additions and 0 deletions
7
.vscode/launch.json
vendored
7
.vscode/launch.json
vendored
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -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<ArcInspector> 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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue