Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements the framework for distance based fare calculation #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<property name="transfers" />
<property name="transferDuration" />
<property name="journeyDuration" />
<property name="distanceMode" />
<property name="distanceUnitPrice" />
<property name="distanceUnitStartOffset" />
</class>

<class name="org.onebusaway.gtfs.model.FareRule" table="gtfs_fare_rules">
Expand All @@ -61,6 +64,7 @@
<property name="originId" />
<property name="destinationId" />
<property name="containsId" />
<property name="unitsTraveled" />
</class>

<class name="org.onebusaway.gtfs.model.FeedInfo" table="gtfs_feed_info">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ protected boolean entitiesAreIdentical(FareRule fareRuleA, FareRule fareRuleB) {
if (!equals(fareRuleA.getContainsId(), fareRuleB.getContainsId())) {
return false;
}
if (!equals(fareRuleA.getUnitsTraveled(), fareRuleB.getUnitsTraveled())) {
return false;
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public final class FareAttribute extends IdentityBean<AgencyAndId> {
@CsvField(optional = true)
private int journeyDuration = MISSING_VALUE;

@CsvField(name = "distance_mode", optional = true, defaultValue = "0")
private int distanceMode = MISSING_VALUE;

@CsvField(name = "distance_unit_price", optional = true)
private float distanceUnitPrice = MISSING_VALUE;

@CsvField(name = "distance_unit_start_offset", optional = true)
private float distanceUnitStartOffset = MISSING_VALUE;

public FareAttribute() {

}
Expand All @@ -59,6 +68,9 @@ public FareAttribute(FareAttribute fa) {
this.transfers = fa.transfers;
this.transferDuration = fa.transferDuration;
this.journeyDuration = fa.journeyDuration;
this.distanceMode = fa.distanceMode;
this.distanceUnitPrice = fa.distanceUnitPrice;
this.distanceUnitStartOffset = fa.distanceUnitStartOffset;
}

@Override
Expand Down Expand Up @@ -143,6 +155,42 @@ public void clearJourneyDuration() {
this.journeyDuration = MISSING_VALUE;
}

public int getDistanceMode() {
return distanceMode;
}

public void setDistanceMode(int distanceMode) {
this.distanceMode = distanceMode;
}

public void clearDistanceMode() {
this.distanceMode = MISSING_VALUE;
}

public float getDistanceUnitPrice() {
return distanceUnitPrice;
}

public void setDistanceUnitPrice(float distanceUnitPrice) {
this.distanceUnitPrice = distanceUnitPrice;
}

public void clearDistanceUnitPrice() {
this.distanceUnitPrice = MISSING_VALUE;
}

public float getDistanceUnitStartOffset() {
return distanceUnitStartOffset;
}

public void setDistanceUnitStartOffset(float distanceUnitStartOffset) {
this.distanceUnitStartOffset = distanceUnitStartOffset;
}

public void clearDistanceUnitStartOffset() {
this.distanceUnitStartOffset = MISSING_VALUE;
}

public String toString() {
return "<FareAttribute " + getId() + ">";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public final class FareRule extends IdentityBean<Integer> {
@CsvField(optional = true)
private String containsId;

@CsvField(name = "units_traveled", optional = true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this field? It's not included in the original proposal. The "units_traveled" value mentioned in the spec doc is just a place-holder variable that refers to a computed value from the actual trip itinerary (distance traveled in km, zones, etc)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is your suggestion that shape_dist_traveled is used for this? Given that these things never line up with operator values, my interpretation was that you would give a origin_id and destination_id and mention the number of units_traveled. In such way it would be possible to unambigiously port a A/B matrix into GTFS.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the distance calculation depends on the distance_mode enum:

1 - per-km - distance values will be computed by computing the length of the shape between the two stops. shape_dist_taveled values will be ignored (since they are unit-less) but instead the distance between lat-lon points belonging to the shape will be computed directly. This is necessarily a bit approximate, but should hopefully give the rider a reasonably close value.
2 - per-zone - compute the number of zones traversed by an intinerary
3 - per-stop - compute the number of stops traversed by an itinerary

I'm not sure it makes sense to include a "units_traveled" for each pair of origin_id and destination_id values. If you are going to be including the entire stop-to-stop fare matrix in the GTFS, why would you need distance-based pricing at that point? You could just specify the price as a fare_attribute directly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This 'approximate' will not work for many agencies. Firstly because some do not even publish shapes, secondly because the shape distance is not used in fare calculation, thirdly rounding errors.

The main reason why we can't publish the price is because of a "startup cost" encoded in "price". The distance-based price is a surplus to that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you describe when "startup cost" is used? If I take a trip from A=>B, with a transfer, followed by B=>C, do I pay the startup cost for the entire trip or just the first leg?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that you are using the same fare system, only the first leg given that your transfer is, within the transfer_duration period.

In the current allows to describe linear relations with respect to distance traveled, but not degenerative relations. The latter (Dutch Railways) we did implement with a cross-product operation on all stops.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, I'd propose the following:

Introduce an additional "distance_mode" enum value:

4 - user-defined distance unit is used, as defined in fare_distances.txt

And introduce a separate fare_distances.txt file with fields:

from_zone_id, to_zone_id, distance_traveled

which would be used to define the A/B matrix of distances.

The main issue here is that I think of fare_rules.txt as defining rules for matching legs and itineraries, while distance_traveled is more of a property of a leg to be matched.

private float unitsTraveled;

public FareRule() {

}
Expand All @@ -53,6 +56,7 @@ public FareRule(FareRule fr) {
this.originId = fr.originId;
this.destinationId = fr.destinationId;
this.containsId = fr.containsId;
this.unitsTraveled = fr.unitsTraveled;
}

@Override
Expand Down Expand Up @@ -105,6 +109,14 @@ public void setContainsId(String containsId) {
this.containsId = containsId;
}

public float getUnitsTraveled() {
return unitsTraveled;
}

public void setUnitsTraveled(float unitsTraveled) {
this.unitsTraveled = unitsTraveled;
}

public String toString() {
return "<FareRule " + getId() + ">";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ public void testAllFields() throws IOException {
"WEEK,20120304,2");
gtfs.putLines(
"fare_attributes.txt",
"fare_id,price,currency_type,payment_method,transfers,transfer_duration,journey_duration",
"FA1,2.0,USD,1,2,60,61");
"fare_id,price,currency_type,payment_method,transfers,transfer_duration,journey_duration,distance_mode,distance_unit_price,distance_unit_start_offset",
"FA1,2.0,USD,1,2,60,61,1,2.00,3.0");
gtfs.putLines("fare_rules.txt",
"fare_id,route_id,origin_id,destination_id,contains_id",
"FA1,R1,Z1,Z2,Z3");
"fare_id,route_id,origin_id,destination_id,contains_id,units_traveled",
"FA1,R1,Z1,Z2,Z3,1.0");
gtfs.putLines(
"shapes.txt",
"shape_id,shape_pt_sequence,shape_pt_lat,shape_pt_lon,shape_dist_traveled",
Expand Down Expand Up @@ -219,6 +219,9 @@ public void testAllFields() throws IOException {
assertEquals(2, fareAttribute.getTransfers());
assertEquals(60, fareAttribute.getTransferDuration());
assertEquals(61, fareAttribute.getJourneyDuration());
assertEquals(1, fareAttribute.getDistanceMode());
assertEquals(2.00, fareAttribute.getDistanceUnitPrice(), 1.0);
assertEquals(3.0, fareAttribute.getDistanceUnitStartOffset(), 1.0);

List<FareRule> rules = dao.getFareRulesForFareAttribute(fareAttribute);
FareRule fareRule = rules.get(0);
Expand All @@ -227,6 +230,7 @@ public void testAllFields() throws IOException {
assertEquals("Z1", fareRule.getOriginId());
assertEquals("Z2", fareRule.getDestinationId());
assertEquals("Z3", fareRule.getContainsId());
assertEquals(1.0, fareRule.getUnitsTraveled(), 1.0);

List<ShapePoint> shapePoints = dao.getShapePointsForShapeId(new AgencyAndId(
"1", "SHP1"));
Expand Down