Skip to content

Commit

Permalink
Normalize weights in DemandModel.create_population() to fix exception…
Browse files Browse the repository at this point in the history
… in random.choice()
  • Loading branch information
samakinen committed Oct 26, 2024
1 parent d0ee4be commit 1d68ab8
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions Scripts/demand/trips.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,16 @@ def create_population(self):
# Group -1 is under-7-year-olds
age_range = numpy.arange(-1, len(param.age_groups))
for zone_number in zone_numbers:
weights = [self.zone_data[f"share_{age}"][zone_number]
for age in self._age_strings()]
weights = numpy.array([self.zone_data[f"share_{age}"][zone_number]
for age in self._age_strings()])
# Append under-7 weight
weights = [max(1 - sum(weights), 0)] + weights
if sum(weights) > 1:
if sum(weights) > 1.005:
msg = "Sum of age group shares for zone {} is {}".format(
zone_number, sum(weights))
log.error(msg)
raise ValueError(msg)
else:
weights = numpy.array(weights)
rebalance = 1 / sum(weights)
weights = rebalance * weights
weights = numpy.insert(weights, 0, max(1.0 - weights.sum(), 0.0))
if sum(weights) > 1.005:
msg = "Sum of age group shares for zone {} is {}".format(
zone_number, sum(weights))
log.error(msg)
raise ValueError(msg)
weights *= 1.0/weights.sum() # Normalize weights
zone_pop = int(round(self.zone_data["population"][zone_number]
* param.agent_demand_fraction))
zone = self.zone_data.zones[zone_number]
Expand Down

0 comments on commit 1d68ab8

Please sign in to comment.