Simulation

Basic simulation implementation where agents in the same environment can be run in an iterative manner.

class creamas.core.simulation.Simulation(env, callback=None, log_folder=None)[source]

A base class for iterative simulations.

In each step the simulation calls act() for each agent in the simulation.

Create simulation for previously set up environment.

Parameters
async_step()[source]

Progress simulation by running all agents asynchronously once.

async_steps(n)[source]

Progress simulation by running all agents n times asynchronously.

close(folder=None)[source]

Close the simulation and the current simulation environment.

classmethod create(agent_cls=None, n_agents=10, agent_kwargs={}, env_cls=<class 'creamas.core.environment.Environment'>, env_kwargs={}, callback=None, conns=0, log_folder=None)[source]

A convenience function to create simple simulations.

Method first creates environment, then instantiates agents into it with give arguments, and finally creates simulation for the environment.

Parameters
  • agent_cls – class for agents, or list of classes. If list, then n_agents and agent_kwargs are expected to be lists also.

  • n_agents – amount of agents for simulation, or list of amounts

  • agent_kwargs – keyword arguments passed to agents at creation time, or list of keyword arguments.

  • env_cls (Environment) – environment class for simulation

  • env_kwargs (dict) – keyword arguments passed to environment at creation time

  • callback (callable) – optional callable to call after each simulation step

  • conns – Create conns amount of initial (random) connections for agents in the simulation environment.

  • log_folder (str) – folder for possible logging. This overwrites log_folder keyword argument from agent_kwargs and env_kwargs.

end(folder=None)[source]

Close the simulation and the current simulation environment.

Deprecated since version 0.4.0: Use func:close instead.

finish_step()[source]

Progress simulation to the end of the current step.

Deprecated since version 0.4.0: Use step() instead.

step()[source]

Progress simulation by a single step.

steps(n)[source]

Progress simulation with given amount of steps.

Can not be called when some of the agents have not acted for the current step.

Parameters

n (int) – amount of steps to run

property age

Age of the simulation.

property callback

Callable to be called after each simulation step for any extra bookkeeping, etc.. Should accept one parameter: age that is current simulation age.

property env

Environment for the simulation.

property name

Name of the simulation.

property order

Order in which agents are run. Order is not enforced for asynchronous executions.

Possible values:

  • alphabetical: agents are sorted by name

  • random: agents are shuffled

Changing the order while iteration is unfinished will take place in the next iteration.