1
0
mirror of https://github.com/gsi-upm/soil synced 2024-12-23 08:28:13 +00:00
soil/examples/random_delays/random_delays_sim.py

46 lines
885 B
Python
Raw Normal View History

2022-10-17 18:23:57 +00:00
"""
2022-03-07 09:48:59 +00:00
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
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
2023-05-03 10:14:49 +00:00
max_pongs = 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")
2023-05-03 10:14:49 +00:00
return self.pong.delay(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")
2023-05-03 10:14:49 +00:00
self.max_pongs -= 1
self.info(str(self.max_pongs), "pongs remaining")
if self.max_pongs < 1:
2022-03-07 10:17:27 +00:00
return self.die()
2023-05-03 10:14:49 +00:00
return self.delay(self.random.expovariate(1 / 16))
2022-10-17 18:23:57 +00:00
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,
2023-04-20 15:56:44 +00:00
iterations=1,
2022-10-17 18:23:57 +00:00
max_time=100,
dump=False,
2022-10-17 18:23:57 +00:00
)
2022-03-07 09:48:59 +00:00
envs = s.run()