pyLEAFS.simulation module

The simulation: a grid, a list of fields, a list of populations, a clock.

Simulation owns the shared Grid, a list of fields, and a list of populations, and advances them together. Both are lists from the start so that adding a pheromone field or a predator population later is an append, not a rewrite. v1 has one resource field and one greedy forager population; the forager() factory builds that world from the LEAFS applet parameters.

class pyLEAFS.simulation.Simulation(grid, fields, populations, dt, rng=None)[source]

Bases: object

A clocked collection of fields and populations on a shared grid.

Parameters:
  • grid (Grid) – Shared spatial grid.

  • fields (list) – Fields to step each timestep (e.g. a ResourceField).

  • populations (list of Population) – Populations to step each timestep.

  • dt (float) – Timestep.

  • rng (numpy.random.Generator, optional) – Random source; defaults to a fresh unseeded generator.

step_count

Number of timesteps advanced.

Type:

int

time

Physical time elapsed (step_count * dt).

Type:

float

Examples

>>> sim = Simulation.forager(seed=0)
>>> sim.run(50)
>>> sim.populations[0].count >= 0
True
classmethod forager(seed=None, shape=(10, 10), n_agents=5, Xi=0.5)[source]

Build the v1 greedy-forager world from forager_applet.js.

The environment homogeneity Xi is the primary control: the energy per resource is derived from it as in the applet,

\[\epsilon = \Gamma / \big((\Xi / r_\mathrm{col})^2\,\gamma\big),\]

so that the equilibrium resource density rises as Xi falls (a more homogeneous field). Remaining parameters match the applet: mu0=0.1, dt=0.02, v=20, R_sense=6, r_collect=1, s_max=1, reproduction at 0.8 with the daughter budded r_collect away.

Parameters:
  • seed (int, optional) – RNG seed for reproducibility.

  • shape (tuple of int, optional) – Regions per axis; length 2 or 3 selects the dimension (default (10, 10), the applet’s 100x100 domain at L=10).

  • n_agents (int, optional) – Initial number of foragers (default 5).

  • Xi (float, optional) – Environment homogeneity parameter (default 0.5).

Returns:

A seeded simulation with one resource field and one forager population, both populated near their initial conditions.

Return type:

Simulation

run(n_steps, stop_on_extinction=True)[source]

Advance n_steps timesteps.

Parameters:
  • n_steps (int) – Number of steps to advance.

  • stop_on_extinction (bool, optional) – Stop early if every population is empty (default True).

Returns:

Number of steps actually taken.

Return type:

int

step()[source]

Advance every field then every population by one timestep.