diff --git a/Scripts/demand/trips.py b/Scripts/demand/trips.py index dab28ed1..da825f62 100644 --- a/Scripts/demand/trips.py +++ b/Scripts/demand/trips.py @@ -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]