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

Generalize sub-bound handling (#469) #475

Merged
merged 1 commit into from
Oct 28, 2022
Merged
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
29 changes: 15 additions & 14 deletions Scripts/datahandling/zonedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,30 @@ class ZoneData:
def __init__(self, data_dir, zone_numbers):
self._values = {}
self.share = ShareChecker(self)
zone_numbers = numpy.array(zone_numbers)
all_zone_numbers = numpy.array(zone_numbers)
self.all_zone_numbers = all_zone_numbers
surrounding = param.areas["surrounding"]
peripheral = param.areas["peripheral"]
external = param.areas["external"]
first_extra = numpy.searchsorted(zone_numbers, peripheral[1], "right")
self.zone_numbers = zone_numbers[:first_extra]
self.zone_numbers = all_zone_numbers[:all_zone_numbers.searchsorted(
peripheral[1], "right")]
Zone.counter = 0
self.zones = {number: Zone(number) for number in self.zone_numbers}
self.first_not_helsinki_zone = numpy.searchsorted(
self.zone_numbers, param.municipalities["Espoo"][0])
self.first_surrounding_zone = numpy.searchsorted(
self.zone_numbers, surrounding[0])
first_peripheral = numpy.searchsorted(self.zone_numbers, peripheral[0])
self.first_not_helsinki_zone = self.zone_numbers.searchsorted(
param.municipalities["Espoo"][0])
self.first_surrounding_zone = self.zone_numbers.searchsorted(
surrounding[0])
first_peripheral = self.zone_numbers.searchsorted(peripheral[0])
self.first_peripheral_zone = first_peripheral
first_external = numpy.searchsorted(zone_numbers, external[0])
self.first_external_zone = first_external
external_zones = zone_numbers[first_external:]
popdata = read_csv_file(data_dir, ".pop", self.zone_numbers, float)
workdata = read_csv_file(data_dir, ".wrk", self.zone_numbers, float)
schooldata = read_csv_file(data_dir, ".edu", self.zone_numbers, float)
landdata = read_csv_file(data_dir, ".lnd", self.zone_numbers, float)
parkdata = read_csv_file(data_dir, ".prk", self.zone_numbers, float)
self.externalgrowth = read_csv_file(data_dir, ".ext", external_zones, float)
self.externalgrowth = read_csv_file(
data_dir, ".ext",
all_zone_numbers[all_zone_numbers.searchsorted(external[0]):],
float)
transit = read_csv_file(data_dir, ".tco")
try:
transit["fare"] = transit["fare"].astype(
Expand Down Expand Up @@ -69,8 +70,8 @@ def __init__(self, data_dir, zone_numbers):
+ self["share_age_65-99"])
self.share["share_age_18-99"] = (self["share_age_7-99"]
-self["share_age_7-17"])
self.share["share_female"] = pandas.Series(0.5, zone_numbers)
self.share["share_male"] = pandas.Series(0.5, zone_numbers)
self.share["share_female"] = pandas.Series(0.5, self.zone_numbers)
self.share["share_male"] = pandas.Series(0.5, self.zone_numbers)
self.nr_zones = len(self.zone_numbers)
self["population_density"] = pop / landdata["builtar"]
wp = workdata["total"]
Expand Down
3 changes: 2 additions & 1 deletion Scripts/datatypes/purpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ def __init__(self, specification, zone_data, resultdata=None):
self.dest = specification["dest"]
self.area = specification["area"]
self.sources = []
zone_numbers = zone_data.zone_numbers
zone_numbers = zone_data.all_zone_numbers
zone_intervals = param.purpose_areas[self.area]
self.bounds = slice(*zone_numbers.searchsorted(
[zone_intervals[0], zone_intervals[-1]]))
sub_intervals = zone_numbers[self.bounds].searchsorted(zone_intervals)
self.sub_bounds = [slice(sub_intervals[i-1], sub_intervals[i])
for i in range(1, len(sub_intervals))]
self.sub_intervals = sub_intervals[1:]
self.zone_data = zone_data
self.resultdata = resultdata
self.model = None
Expand Down
5 changes: 2 additions & 3 deletions Scripts/datatypes/tour.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,9 @@ def _get_cost(self, impedance, mtx_type):
pass
# scale transit costs from month to day
if self.mode == "transit" and mtx_type == "cost":
idx = numpy.searchsorted(
[bounds.stop for bounds in self.purpose.sub_bounds],
i = self.purpose.sub_intervals.searchsorted(
self.position[0], side="right")
cost /= transit_trips_per_month[self.purpose.area][demand_type][idx]
cost /= transit_trips_per_month[self.purpose.area][demand_type][i]
return cost

def __str__(self):
Expand Down
12 changes: 6 additions & 6 deletions Scripts/models/logit.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,10 @@ def calc_individual_mode_prob(self, is_car_user, zone):
try:
mode_exps[mode] *= math.exp(b["car_users"])
except TypeError:
if zone < self.zone_data.first_surrounding_zone:
mode_exps[mode] *= math.exp(b["car_users"][0])
else:
mode_exps[mode] *= math.exp(b["car_users"][1])
# Separate sub-region parameters
i = self.purpose.sub_intervals.searchsorted(
zone, side="right")
mode_exps[mode] *= math.exp(b["car_users"][i])
mode_expsum += mode_exps[mode]
probs = numpy.empty(len(modes))
for i, mode in enumerate(modes):
Expand All @@ -395,8 +395,8 @@ def calc_individual_mode_prob(self, is_car_user, zone):
money_utility = 1 / b
except TypeError:
# Separate sub-region parameters
money_utility = (1 / b[0] if self.sub_bounds[0].stop < zone
else 1 / b[1])
i = self.purpose.sub_intervals.searchsorted(zone, side="right")
money_utility = 1 / b[i]
money_utility /= self.mode_choice_param["car"]["log"]["logsum"]
accessibility = -money_utility * logsum
return probs, accessibility
Expand Down