Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add a script that generates a benchmark plan from a graph rather than…

… GTFS. Formatting fixes too.
  • Loading branch information...
commit e005a575c92a87a8f06769e4aba0c9b01fe2b3ea 1 parent e2c219e
@fakeholz fakeholz authored
View
179 ...-integration/src/main/java/org/opentripplanner/integration/benchmark/GenerateGraphBenchmarkPlanMain.java
@@ -0,0 +1,179 @@
+/* This program is free software: you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public License
+ as published by the Free Software Foundation, either version 3 of
+ the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+package org.opentripplanner.integration.benchmark;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Random;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.GnuParser;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.Parser;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.node.ObjectNode;
+import org.onebusaway.gtfs.model.Stop;
+import org.onebusaway.gtfs.serialization.GtfsReader;
+import org.onebusaway.gtfs.services.GenericMutableDao;
+import org.opentripplanner.routing.graph.Graph;
+import org.opentripplanner.routing.graph.Graph.LoadLevel;
+import org.opentripplanner.routing.graph.Vertex;
+
+import com.vividsolutions.jts.geom.Coordinate;
+import com.vividsolutions.jts.geom.Envelope;
+
+/**
+ * Generate a benchmark plan (see {@link RunBenchmarkPlanMain} from a Graph randomly picking pairs of points near intersections and generating from-to
+ * trip requests between the pairs. The resulting trip plan file can be fed into {@link RunBenchmarkPlanMain} to run a benchmark of those plans.
+ *
+ * @author flamholz
+ */
+public class GenerateGraphBenchmarkPlanMain {
+
+ private static final String ARG_TIME = "time";
+
+ private static final String ARG_SAMPLES = "samples";
+
+ private static final String ARG_LAT_NOISE = "latNoise";
+
+ private static final String ARG_LON_NOISE = "lonNoise";
+
+ public static void main(String[] args) throws Exception {
+
+ Options options = new Options();
+ options.addOption(ARG_TIME, true, "time to plan trip");
+ options.addOption(ARG_SAMPLES, true, "number of samples to generate");
+ options.addOption(ARG_LAT_NOISE, true, "lat noise");
+ options.addOption(ARG_LON_NOISE, true, "lon noise");
+
+ Parser parser = new GnuParser();
+ CommandLine cli = parser.parse(options, args);
+ args = cli.getArgs();
+
+ if (args.length != 2) {
+ System.err.println("usage: input_graph output_file");
+ System.exit(-1);
+ }
+
+ GenerateGraphBenchmarkPlanMain task = new GenerateGraphBenchmarkPlanMain();
+ task.setInputPath(new File(args[0]));
+ task.setOutputPath(new File(args[1]));
+
+ if (cli.hasOption(ARG_TIME)) {
+ Date time = DateLibrary.getIso8601StringAsDate(cli.getOptionValue(ARG_TIME));
+ task.setTime(time);
+ }
+
+ if (cli.hasOption(ARG_SAMPLES)) {
+ int samples = Integer.parseInt(cli.getOptionValue(ARG_SAMPLES));
+ task.setSamples(samples);
+ }
+
+ if (cli.hasOption(ARG_LAT_NOISE)) {
+ task.setLatNoise(Double.parseDouble(cli.getOptionValue(ARG_LAT_NOISE)));
+ }
+
+ if (cli.hasOption(ARG_LON_NOISE)) {
+ task.setLatNoise(Double.parseDouble(cli.getOptionValue(ARG_LON_NOISE)));
+ }
+
+ task.run();
+ }
+
+ private static Random _random = new Random();
+
+ private File _inputPath;
+
+ private File _outputPath;
+
+ private Date _time = new Date();
+
+ private int _samples = 100;
+
+ private double _latNoise = 0.0002;
+
+ private double _lonNoise = 0.002;
+
+ ObjectMapper mapper = new ObjectMapper();
+
+ public void setInputPath(File file) {
+ _inputPath = file;
+ }
+
+ public void setOutputPath(File file) {
+ _outputPath = file;
+ }
+
+ public void setTime(Date time) {
+ _time = time;
+ }
+
+ public void setSamples(int samples) {
+ _samples = samples;
+ }
+
+ public void setLatNoise(double latNoise) {
+ _latNoise = latNoise;
+ }
+
+ public void setLonNoise(double lonNoise) {
+ _lonNoise = lonNoise;
+ }
+
+ public void run() throws IOException, ClassNotFoundException {
+
+ Graph g = readGraphFromFile();
+ List<Vertex> vertices = new ArrayList<Vertex>(g.getVertices());
+
+ String timeAsString = DateLibrary.getTimeAsIso8601String(_time);
+
+ PrintWriter out = new PrintWriter(_outputPath);
+
+ for (int i = 0; i < _samples; i++) {
+ ObjectNode from = getRandomLocationNearVertex(vertices);
+ ObjectNode to = getRandomLocationNearVertex(vertices);
+ ObjectNode row = mapper.createObjectNode();
+ row.put("from", from);
+ row.put("to", to);
+ row.put("time", timeAsString);
+ out.println(row.toString());
+ }
+
+ out.close();
+ }
+
+ private Graph readGraphFromFile() throws IOException, ClassNotFoundException {
+ Graph g = Graph.load(_inputPath, LoadLevel.FULL);
+ return g;
+ }
+
+ private ObjectNode getRandomLocationNearVertex(List<Vertex> vertices) {
+
+ int index = _random.nextInt(vertices.size());
+ Vertex v = vertices.get(index);
+
+ double lat = v.getY() + _random.nextGaussian() * _latNoise;
+ double lon = v.getX() + _random.nextGaussian() * _lonNoise;
+
+ ObjectNode obj = mapper.createObjectNode();
+ obj.put("lat", lat);
+ obj.put("lon", lon);
+ return obj;
+ }
+}
View
228 ...ripplanner-integration/src/main/java/org/opentripplanner/integration/benchmark/RunBenchmarkPlanMain.java
@@ -34,6 +34,8 @@
import org.opentripplanner.model.GraphBundle;
import org.opentripplanner.routing.core.RoutingRequest;
import org.opentripplanner.routing.error.VertexNotFoundException;
+import org.opentripplanner.routing.graph.Graph;
+import org.opentripplanner.routing.graph.Graph.LoadLevel;
import org.opentripplanner.routing.impl.SerializedGraphFactoryBean;
import org.opentripplanner.routing.services.PathService;
import org.springframework.beans.factory.config.BeanDefinition;
@@ -44,149 +46,155 @@
public class RunBenchmarkPlanMain {
- private static final String ARG_REPETITIONS = "repetitions";
+ private static final String ARG_REPETITIONS = "repetitions";
- public static void main(String[] args) throws Exception {
+ public static void main(String[] args) throws Exception {
- Options options = new Options();
- options.addOption(ARG_REPETITIONS, true, "repetitions");
+ Options options = new Options();
+ options.addOption(ARG_REPETITIONS, true, "repetitions");
- Parser parser = new GnuParser();
- CommandLine cli = parser.parse(options, args);
- args = cli.getArgs();
+ Parser parser = new GnuParser();
+ CommandLine cli = parser.parse(options, args);
+ args = cli.getArgs();
- if (args.length != 2) {
- System.err.println("usage: graph_path benchmark_path");
- System.exit(-1);
- }
+ if (args.length != 2) {
+ System.err.println("usage: graph_path benchmark_path");
+ System.exit(-1);
+ }
- RunBenchmarkPlanMain task = new RunBenchmarkPlanMain();
- task.setGraphPath(new File(args[0]));
- task.setBenchmarkPlan(new File(args[1]));
+ RunBenchmarkPlanMain task = new RunBenchmarkPlanMain();
+ task.setGraphPath(new File(args[0]));
+ task.setBenchmarkPlan(new File(args[1]));
- if (cli.hasOption(ARG_REPETITIONS))
- task.setRepetitions(Integer.parseInt(cli.getOptionValue(ARG_REPETITIONS)));
+ if (cli.hasOption(ARG_REPETITIONS))
+ task.setRepetitions(Integer.parseInt(cli.getOptionValue(ARG_REPETITIONS)));
+
+ task.run();
+ }
- task.run();
- }
+ private File _graphPath;
- private File _graphPath;
+ private File _benchmarkPlan;
- private File _benchmarkPlan;
+ private int _repetitions = 1;
- private int _repetitions = 1;
+ public void setGraphPath(File path) {
+ _graphPath = path;
+ }
- public void setGraphPath(File path) {
- _graphPath = path;
- }
+ public void setBenchmarkPlan(File benchmarkPlan) {
+ _benchmarkPlan = benchmarkPlan;
+ }
- public void setBenchmarkPlan(File benchmarkPlan) {
- _benchmarkPlan = benchmarkPlan;
- }
+ public void setRepetitions(int repetitions) {
+ _repetitions = repetitions;
+ }
- public void setRepetitions(int repetitions) {
- _repetitions = repetitions;
- }
+ public void run() throws Exception {
+ GenericApplicationContext context = getApplicationContext();
+ PathService service = (PathService) context.getBean("pathServiceImpl");
+
+ List<Plan> plans = readPlans();
+
+ long tTotal = 0;
+ for (int i = 0; i < _repetitions; i++) {
+ for (Plan plan : plans) {
+
+ String from = plan.latFrom + " " + plan.lonFrom;
+ String to = plan.latTo + " " + plan.lonTo;
+
+ try {
+ RoutingRequest opt = new RoutingRequest();
+ opt.setDateTime(plan.time);
+ opt.setFrom(from);
+ opt.setTo(to);
+
+ long t0 = System.currentTimeMillis();
+ service.getPaths(opt);
+ long t1 = System.currentTimeMillis();
+
+ System.out.println("t=" + (t1 - t0));
+ tTotal += t1 - t0;
+ } catch (VertexNotFoundException ex) {
+ System.out.println("no vertex: from=" + from + " to=" + to);
+ }
+ }
+ }
+ System.out.println("totalTime=" + tTotal);
+ }
- public void run() throws Exception {
- GenericApplicationContext context = getApplicationContext();
- PathService service = (PathService) context.getBean("pathServiceImpl");
+ private List<Plan> readPlans() throws FileNotFoundException, IOException, ParseException {
+ BufferedReader reader = new BufferedReader(new FileReader(_benchmarkPlan));
+ String line = null;
+ List<Plan> plans = new ArrayList<Plan>();
- List<Plan> plans = readPlans();
+ while ((line = reader.readLine()) != null) {
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode obj = mapper.readTree(line);
+ plans.add(getJsonObjectAsPlan(obj));
+ }
+ reader.close();
+ return plans;
+ }
- long tTotal = 0;
- for (int i = 0; i < _repetitions; i++) {
- for (Plan plan : plans) {
+ private GenericApplicationContext getApplicationContext() {
- String from = plan.latFrom + " " + plan.lonFrom;
- String to = plan.latTo + " " + plan.lonTo;
+ GenericApplicationContext ctx = new GenericApplicationContext();
+ XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
+ xmlReader.loadBeanDefinitions(new ClassPathResource(
+ "org/opentripplanner/integration/application-context.xml"));
- try {
- RoutingRequest opt = new RoutingRequest();
- opt.setDateTime(plan.time);
- opt.setFrom(from);
- opt.setTo(to);
- long t0 = System.currentTimeMillis();
- service.getPaths(opt);
- long t1 = System.currentTimeMillis();
+ Map<String, BeanDefinition> additionalBeans = getAdditionalBeans();
+ for (Map.Entry<String, BeanDefinition> entry : additionalBeans.entrySet())
+ ctx.registerBeanDefinition(entry.getKey(), entry.getValue());
- System.out.println("t=" + (t1 - t0));
- tTotal += t1 - t0;
- } catch (VertexNotFoundException ex) {
- System.out.println("no vertex: from=" + from + " to=" + to);
- }
- }
+ ctx.refresh();
+ ctx.registerShutdownHook();
+ return ctx;
}
- System.out.println("totalTime=" + tTotal);
- }
-
- private List<Plan> readPlans() throws FileNotFoundException, IOException,
- ParseException {
- BufferedReader reader = new BufferedReader(new FileReader(_benchmarkPlan));
- String line = null;
- List<Plan> plans = new ArrayList<Plan>();
-
- while ((line = reader.readLine()) != null) {
- ObjectMapper mapper = new ObjectMapper();
- JsonNode obj = mapper.readTree(line);
- plans.add(getJsonObjectAsPlan(obj));
- }
- reader.close();
- return plans;
- }
- private GenericApplicationContext getApplicationContext() {
+ private Map<String, BeanDefinition> getAdditionalBeans() {
- GenericApplicationContext ctx = new GenericApplicationContext();
- XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
- xmlReader.loadBeanDefinitions(new ClassPathResource(
- "org/opentripplanner/application-context.xml"));
+ Map<String, BeanDefinition> additionalBeans = new HashMap<String, BeanDefinition>();
- Map<String, BeanDefinition> additionalBeans = getAdditionalBeans();
- for (Map.Entry<String, BeanDefinition> entry : additionalBeans.entrySet())
- ctx.registerBeanDefinition(entry.getKey(), entry.getValue());
+ BeanDefinitionBuilder bundlePath = BeanDefinitionBuilder
+ .genericBeanDefinition(GraphBundle.class);
+ bundlePath.addPropertyValue("path", _graphPath);
+ additionalBeans.put("graphBundle", bundlePath.getBeanDefinition());
- ctx.refresh();
- ctx.registerShutdownHook();
- return ctx;
- }
+ BeanDefinitionBuilder graph = BeanDefinitionBuilder
+ .genericBeanDefinition(SerializedGraphFactoryBean.class);
+ graph.addPropertyReference("graphBundle", "graphBundle");
+ additionalBeans.put("graph", graph.getBeanDefinition());
- private Map<String, BeanDefinition> getAdditionalBeans() {
+ return additionalBeans;
+ }
- Map<String, BeanDefinition> additionalBeans = new HashMap<String, BeanDefinition>();
+ private Plan getJsonObjectAsPlan(JsonNode obj) throws ParseException {
- BeanDefinitionBuilder bundlePath = BeanDefinitionBuilder.genericBeanDefinition(GraphBundle.class);
- bundlePath.addPropertyValue("path", _graphPath);
- additionalBeans.put("graphBundle", bundlePath.getBeanDefinition());
+ Plan plan = new Plan();
- BeanDefinitionBuilder graph = BeanDefinitionBuilder.genericBeanDefinition(SerializedGraphFactoryBean.class);
- graph.addPropertyReference("graphBundle", "graphBundle");
- additionalBeans.put("graph", graph.getBeanDefinition());
+ JsonNode from = obj.get("from");
+ plan.latFrom = from.get("lat").getValueAsDouble();
+ plan.lonFrom = from.get("lon").getValueAsDouble();
- return additionalBeans;
- }
+ JsonNode to = obj.get("to");
+ plan.latTo = to.get("lat").getValueAsDouble();
+ plan.lonTo = to.get("lon").getValueAsDouble();
- private Plan getJsonObjectAsPlan(JsonNode obj) throws ParseException {
+ plan.time = DateLibrary.getIso8601StringAsDate(obj.get("time").getValueAsText());
+ return plan;
+ }
- Plan plan = new Plan();
+ private static class Plan {
+ public double latFrom;
- JsonNode from = obj.get("from");
- plan.latFrom = from.get("lat").getValueAsDouble();
- plan.lonFrom = from.get("lon").getValueAsDouble();
+ public double lonFrom;
- JsonNode to = obj.get("to");
- plan.latTo = to.get("lat").getValueAsDouble();
- plan.lonTo = to.get("lon").getValueAsDouble();
+ public double latTo;
- plan.time = DateLibrary.getIso8601StringAsDate(obj.get("time").getValueAsText());
- return plan;
- }
+ public double lonTo;
- private static class Plan {
- public double latFrom;
- public double lonFrom;
- public double latTo;
- public double lonTo;
- public Date time;
- }
+ public Date time;
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.