jython-2.7opentripplanner

Open trip planner script slower on days other than today


I'm making use of open trip planner using the jython scripting method explained here: http://docs.opentripplanner.org/en/latest/Scripting/ (specifically 'Using OTP as a library') and am using a script very similar to their example script

For testing purposes I have two csv files containing 40 locations each. The locations are inside the Netherlands and I have loaded both the dutch gtfs and map. The strange thing is that the code that calculates the public transport trip times (line 32 in the example script: res = spt.eval(colleges), using modes WALK,TRANSIT) takes longer when I specify a day other than today.

An example:

req.setDateTime(2018, 12, 8, 16, 00, 00) # today
spt.eval(my_data) # -> takes ~7 - 10 seconds

req.setDateTime(2018, 12, 7, 16, 00, 00) # yesterday
spt.eval(my_data) # -> takes ~30 - 40 seconds

When not setting req.setDateTime(), spt.eval() is even faster. Note that I ran the script on the 6th, for the 6th, as well, and it was fast then too, so it's certainly related to "today" and not specifically the 8th.

Of course my primary question is, how do I make it fast for days other than today? (my main interest is actually tomorrow)

Is it related to when the OTP instance is started or is it some internal optimization? I don't think it's related to the building of the graph because that was built a couple of days ago. I was looking into providing a day or datetime setting when initializing OTP but am unable to find that in the docs.

(I haven't tried messing with my system time yet, but that's also an option I'm not very fond of). Any ideas or comments are welcome. If necessary I will provide a reproducible sample tomorrow.


Solution

  • This problem was actually caused because of how I used req.setDateTime() in combination with req.setMaxTimeSec().

    Basically, setMaxTimeSec() uses the date set by setDateTime() as a starting point, and defines a worstTime (aka the last possible time) to that date time + the maxTimeSec. However, if setDateTime() was not yet set when calling setMaxTimeSec(), the current date time is used instead. This will consequently cause problems when you happen to call setDateTime() AFTERWARDS. Example:

    setMaxTimeSec(60*60) # Sets worst time to now + 1 hour
    setDateTime(yesterday) # Sets departure time to yesterday
    

    This example has a very long time window to search for solutions! Instead of only looking within an hour time, we are now looking in a window of 25 hours!

    Anyway, a simple solution is to first call setDateTime(), and then setMaxTimeSec():

    setDateTime(yesterday) # Sets departure time to yesterday
    setMaxTimeSec(60*60) # Sets worst time to yesterday + 1 hour
    

    Alternatively, if, for some reason, you can't switch these methods, you can always correct the setMaxTimeSec() with the time difference between now and your setDateTime()-value:

    date = datetime.strptime('2019-01-08 21:00', '%Y-%m-%d %H:%M')
    date_seconds = time.mktime(date.timetuple())
    now_seconds = time.mktime(datetime.now().timetuple())
    date_diff_seconds = int(round(date_seconds - now_seconds))
    req.setMaxTimeSec(60*60 + date_diff_seconds) 
    req.setDateTime(date.year, date.month, date.day, date.hour, date.minute, 00)