OTP Travel time and cost Matrix

170 views
Skip to first unread message

Rafael Pereira

unread,
Feb 2, 2016, 10:26:10 AM2/2/16
to OpenTripPlanner Users


Hi all,

as some of you will remember, I've created a simple and reproducible example of how to use OTP to compute a travel time matrix using a python (here: https://github.com/rafapereirabr/otp-travel-time-matrix). I'm now facing two questions that I'm sure you've faced before:

1. How can I set a limit to the number of connections?
2. How can I add to the csv output the travel cost based on transit fare ?

Here is the snippet of the python script where I believe I should add another column. I haven't found a manual for these code conventions used in OTP, but I assume it would be something like r.getFare().

  # Add a new row of result in the CSV output
  for r in result:
    matrixCsv.addRow([ origin.getStringData('GEOID'), r.getIndividual().getStringData('GEOID'), r.getWalkDistance() , r.getTime(), r.getFare() ])

Best,

Rafael Pereira

Laurent GRÉGOIRE

unread,
Feb 4, 2016, 3:32:39 AM2/4/16
to Rafael Pereira, OpenTripPlanner Users
Hi Rafael,

For reference, copying my old message in this thread.

On 2 February 2016 at 16:26, Rafael Pereira <correiod...@gmail.com> wrote:
> 1. How can I set a limit to the number of connections?

There is no such limit on OTP per se (not anymore anyway). Instead
there is a "transferPenalty" parameter (in seconds), which can be used
to make connections (=transfers) less preferable. See
RoutingRequest.java L~147.

Having said that, there is currently no method on the scripting API to
set this parameter, but this would be trivial to add:

OtpsRoutingRequest.java:

public void setTransferPenalty(int transferPenalty) {
req.transferPenalty = transferPenalty
}

> 2. How can I add to the csv output the travel cost based on transit fare ?

Currently it's not possible, there is no fare information on the SPT
itself. Fare computation is rather complex and a bit expensive
CPU-wise, so it is not computed on the fly during the search (and
anyway due to the nature of the fare computation itself it would not
be always possible). Having said that nothing is impossible, a
function to compute the fare for a given state could be possible to
implement.

See FareService.java (and various implementation), and
GraphPathToTripPlanConverter::generateItinerary() to see where it is
called. You can see there that in order to compute fare you need a
"GraphPath" which is a bit expensive to build if you want to do it for
each final state in a search, but doable for evaluating a limited set
of destination in scripting. You can build a GraphPath from a final
state in a SPT. Pseudo-quickly-hacked-not-tested-code:

public Fare Sample::evalFare(ShortestPathTree spt, Graph graph) {
FareService fareService = graph.fareService;
State s0 = spt.getState(v0);
State s1 = spt.getState(v1);
Fare f0 = null;
Fare f1 = null;
if (s0 != null)
f0 = computeFare(s0);
if (s1 != null)
f1 = computeFare(s1);
// Fare averaging function is left as an exercice to the reader :)
return averaging(f0, d0, f1, d1);
}

private Fare Sample::computeFare(State s, FareService fareService) {
GraphPath path = new GraphPath(s, true);
return fareService.getCost(path);
}

And adding the relevant fare information in OtpEvaluatedIndividual.

HTH,

--Laurent

Rafael Pereira

unread,
Feb 4, 2016, 1:17:20 PM2/4/16
to Laurent GRÉGOIRE, OpenTripPlanner Users
Thank you again Laurent. 

That was very helpful.  I hope these issues might incorporated into OTP in future versions as I wouldn't be capable of making those changes myself ! 

best,

Rafael Pereira
Reply all
Reply to author
Forward
0 new messages