1
0
mirror of https://github.com/gsi-upm/soil synced 2024-09-21 07:41:43 +00:00
soil/examples/random_delays/random_delays_sim.py

48 lines
940 B
Python
Raw Normal View History

2022-10-17 18:23:57 +00:00
"""
2022-03-07 09:48:59 +00:00
Example of setting a
Example of a fully programmatic simulation, without definition files.
2022-10-17 18:23:57 +00:00
"""
from soil import Simulation, agents, Environment
2022-03-07 09:48:59 +00:00
from soil.time import Delta
class MyAgent(agents.FSM):
2022-10-17 18:23:57 +00:00
"""
2022-03-07 10:17:27 +00:00
An agent that first does a ping
2022-10-17 18:23:57 +00:00
"""
2022-03-07 10:17:27 +00:00
2022-10-17 18:23:57 +00:00
defaults = {"pong_counts": 2}
2022-03-07 09:48:59 +00:00
@agents.default_state
@agents.state
2022-03-07 10:17:27 +00:00
def ping(self):
2022-10-17 18:23:57 +00:00
self.info("Ping")
return self.pong, Delta(self.random.expovariate(1 / 16))
2022-03-07 10:17:27 +00:00
@agents.state
def pong(self):
2022-10-17 18:23:57 +00:00
self.info("Pong")
2022-03-07 10:17:27 +00:00
self.pong_counts -= 1
self.info(str(self.pong_counts))
if self.pong_counts < 1:
return self.die()
2022-10-17 18:23:57 +00:00
return None, Delta(self.random.expovariate(1 / 16))
class RandomEnv(Environment):
def init(self):
self.add_agent(agent_class=MyAgent)
2022-10-17 18:23:57 +00:00
s = Simulation(
name="Programmatic",
model=RandomEnv,
2022-10-17 18:23:57 +00:00
num_trials=1,
max_time=100,
dump=False,
2022-10-17 18:23:57 +00:00
)
2022-03-07 09:48:59 +00:00
envs = s.run()