Creamas contains easy to use iterative simulation to perform experiments on a single computer set up. Each agent wishing to use simulation has to implement ~creamas.core.agent.CreativeAgent.act
:
creamas.core.agent.CreativeAgent.act
Simulation calls act
for each agent in each simulation step. What agents do in their turn is up to you!
Good news! Creating a simple simulation is made easy with :py~creamas.core.simulation.Simulation.create
. Observe.
from mymodule import MyAgent
from creamas.core.simulation import Simulation
env_kwargs={'addr': ('localhost', 5555)}
agent_kwargs = {'foo': 'bar', 'Cthulhu': 'rises'}
# Create initial simulation with default parameters using MyAgents-class
# and passing agent_kwargs to each agent at initialization time.
sim = Simulation.create(env_kwargs=env_kwargs,
agent_cls=MyAgent,
agent_kwargs=agent_kwargs,
n_agents=20)
# Advance simulation by single step
sim.step()
# Close the simulation and it's environment.
sim.close()
create
offers few arguments to modify simulation initialization:
- You can create simulation with multiple agent classes each with its own keyword arguments and number of agents.
from mymodule import MyAgent, CthulhuAgent
from.creamas.core.simulation import Simulation
env_kwargs={'addr': ('localhost', 5555)}
myagent_kwargs = {'foo': 'bar', 'Cthulhu': 'rises'}
cthulhu_kwargs = {"R'lyeh": 'sunken'}
agent_kwargs=[myagent_kwargs,cthulhu_kwargs]
agent_cls = [MyAgent, CthulhuAgent]
n_agents = [10, 1]
sim = Simulation.create(env_kwargs=env_kwargs,
agent_cls=agent_cls,
n_agents=n_agents,
agent_kwargs=agent_kwargs)
- You can create a simulation with your own environment, which is automatically passed down to the agents at their initialization time.
from mymodule import StarSpawnAgent
from myenv import InnsmouthEnvironment
from creamas.core.simulation import Simulation
env_kwargs = {'addr': ('localhost', 5555),
'weather': 'drizzle, slight wind',
'atmosphere': 'gloomy'}
sim = Simulation.create(agent_cls=StarSpawnAgent
env_cls=InnsmouthEnvironment,
env_kwargs=env_kwargs)
If you need more control on creating the environment and agents, you can create your environment directly and then create your agents. After you have fully initialized the environment, you can then pass it to the ~creamas.core.simulation.Simulation
at initialization time.
from mymodule import StarSpawnAgent
from creamas.core.enviroment import Environment
from creamas.core.simulation import Simulation
env = Environment.create(('localhost', 5555))
for i in range(10):
# do some complex calculation
# ...
StarSpawnAgent(env, cause_havoc=True, non_euclidian_angle=mystery)
sim = Simulation(env=env)
Simulation holds a few different ways to advance it.
# Advance simulation by a single step (executing all agents once)
# or advance simulation to the end of the current step.
sim.step()
# Advance simulation by executing a single agent.
sim.next()
# Advance simulation by 10 steps
sim.steps(10)
TODO: Log the logging of logger.