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.Functions
step()
,steps()
,async_step()
andasync_steps()
are used to advance the simulation.Create a simulation for an existing environment.
- Parameters
env (
Environment
,MultiEnvironment
orDistributedEnvironment
) – An environment.callback (callable) – function to call after each simulation step
log_folder (str) – folder to log simulation information
-
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 an environment, then instantiates agents into it with given 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 simulationenv_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.
-
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
callback
¶ Callable to be called after each simulation step for any extra bookkeeping, etc.. Callback should accept one parameter: cur_step that is the simulation’s current step.
-
property
cur_step
¶ The simulation’s current step as an integer.
-
property
env
¶ Environment for the simulation.
-
property
name
¶ Name of the simulation as a string.
-
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.