Skip to content

Commit

Permalink
Use random.Generator.choice() to create all age samples at once in De…
Browse files Browse the repository at this point in the history
…mandModel.create_population()
  • Loading branch information
samakinen committed Oct 26, 2024
1 parent 1d68ab8 commit a6e7ec0
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions Scripts/demand/trips.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Dict, Tuple, Union, cast
from typing import TYPE_CHECKING, Dict, List, Tuple, cast
import numpy # type: ignore
import pandas
if TYPE_CHECKING:
Expand Down Expand Up @@ -34,7 +34,9 @@ def __init__(self,
is_agent_model: bool=False):
self.resultdata = resultdata
self.zone_data = zone_data
self.tour_purposes = []
self.population: List[Person] = []
self.zone_population = pandas.Series(0, zone_data.zone_numbers)
self.tour_purposes: List[Purpose] = []
self.purpose_dict: Dict[str,Purpose] = {}
for purpose_spec in param.tour_purposes:
args = (purpose_spec, zone_data, resultdata)
Expand All @@ -48,7 +50,7 @@ def __init__(self,
for source in purpose_spec["source"]:
purpose.sources.append(self.purpose_dict[source])
if "sec_dest" in purpose_spec:
tour_purpose = cast(TourPurpose, self.purpose_dict[source]) #type checker hint
tour_purpose = self.purpose_dict[source]
tour_purpose.sec_dest_purpose = purpose
bounds = param.purpose_areas["metropolitan"]
self.bounds = slice(*zone_data.all_zone_numbers.searchsorted(
Expand Down Expand Up @@ -103,7 +105,7 @@ def create_population(self):
Store list of `Person` instances in `self.population`.
"""
numpy.random.seed(param.population_draw)
rng = numpy.random.default_rng(param.population_draw)
self.population = []
zone_numbers = self.zone_data.zone_numbers[self.bounds]
self.zone_population = pandas.Series(0, zone_numbers)
Expand All @@ -124,15 +126,12 @@ def create_population(self):
* param.agent_demand_fraction))
zone = self.zone_data.zones[zone_number]
incmod = self._income_models[zone.municipality == "Helsinki"]
for _ in range(zone_pop):
i = numpy.random.choice(a=age_range, p=weights)
if i != -1:
self.population.append(Person(
zone, param.age_groups[i], self.tour_generation_model,
self.car_use_model, incmod))
self.zone_population[zone_number] += 1
numpy.random.seed(None)

age_groups = [Person(zone, param.age_groups[i],
self.tour_generation_model, self.car_use_model, incmod)
for i in rng.choice(a=age_range, size=zone_pop, p=weights) if i != -1]
self.population.extend(age_groups)
self.zone_population[zone_number] = len(age_groups)

def predict_income(self):
for model in self._income_models:
model.predict()
Expand Down

0 comments on commit a6e7ec0

Please sign in to comment.