mirror of
https://github.com/gsi-upm/soil
synced 2024-11-14 15:32:29 +00:00
2869b1e1e6
* Removed old/unnecessary models * Added a `simulation.{iter_}from_py` method to load simulations from python files * Changed tests of examples to run programmatic simulations * Fixed programmatic examples
44 lines
883 B
Python
44 lines
883 B
Python
"""
|
|
Example of setting a
|
|
Example of a fully programmatic simulation, without definition files.
|
|
"""
|
|
from soil import Simulation, agents
|
|
from soil.time import Delta
|
|
|
|
|
|
class MyAgent(agents.FSM):
|
|
"""
|
|
An agent that first does a ping
|
|
"""
|
|
|
|
defaults = {"pong_counts": 2}
|
|
|
|
@agents.default_state
|
|
@agents.state
|
|
def ping(self):
|
|
self.info("Ping")
|
|
return self.pong, Delta(self.random.expovariate(1 / 16))
|
|
|
|
@agents.state
|
|
def pong(self):
|
|
self.info("Pong")
|
|
self.pong_counts -= 1
|
|
self.info(str(self.pong_counts))
|
|
if self.pong_counts < 1:
|
|
return self.die()
|
|
return None, Delta(self.random.expovariate(1 / 16))
|
|
|
|
|
|
s = Simulation(
|
|
name="Programmatic",
|
|
model_params={
|
|
'agents': [{'agent_class': MyAgent}],
|
|
},
|
|
num_trials=1,
|
|
max_time=100,
|
|
dry_run=True,
|
|
)
|
|
|
|
|
|
envs = s.run()
|