agent_urban_planning.AgentPopulation¶
- class AgentPopulation(agents)[source]¶
Bases:
objectCollection of weighted representative agent types.
Holds the full list of
Agentinstances comprising the simulation’s population, indexed by integer position. Each agent carries aweightthat represents its population share; weights must sum to 1.0 across the population. Construct viafrom_config()from a parsed agent YAML, which handles distributional sampling (per-zone Census or single national) or explicit per-agent records.- Parameters:
agents (
list[Agent]) – List ofAgentinstances. The constructor validates that their weights sum to 1.0 within tolerance.- Raises:
ValueError – If
agentsweights do not sum to 1 within1e-6.
Examples
>>> import agent_urban_planning as aup >>> # config = aup.data.builtin.load_agents("singapore_real_v2") >>> # pop = aup.AgentPopulation.from_config(config) >>> # len(pop) # number of representative types >>> # for agent in pop: >>> # ... # iterate over the population
- classmethod from_config(config, rng=None, strict=True)[source]¶
Generate an agent population from a configuration object.
Dispatches on
config.mode:"distributional"samples agents from per-zone Census distributions (or, for unit-test configs withstrict=False, a single national distribution), otherwise loads explicitly declared agent records.- Parameters:
config (
AgentDistributionalConfig) – ParsedAgentDistributionalConfigfrom a YAML file.rng (
Optional[RandomState]) – Optionalnumpy.random.RandomStatefor reproducible sampling. IfNone, a fresh state is created.strict (
bool) – IfTrue(default), reject configs without per-zone Census data. Set toFalseonly in unit tests that use small synthetic configs.
- Return type:
- Returns:
A new
AgentPopulation. Weights are normalized to sum to 1.- Raises:
RuntimeError – When
strict=Trueand the config lacks per-zonezone_distributions.
Examples
>>> import agent_urban_planning as aup >>> # config = aup.data.builtin.load_agents("singapore_real_v2") >>> # pop = aup.AgentPopulation.from_config(config, strict=True)