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
|
|
|
"""
|
2023-04-09 02:19:24 +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))
|
|
|
|
|
|
|
|
|
2023-04-09 02:19:24 +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",
|
2023-04-09 02:19:24 +00:00
|
|
|
model=RandomEnv,
|
2023-04-20 15:56:44 +00:00
|
|
|
iterations=1,
|
2022-10-17 18:23:57 +00:00
|
|
|
max_time=100,
|
2023-04-14 17:41:24 +00:00
|
|
|
dump=False,
|
2022-10-17 18:23:57 +00:00
|
|
|
)
|
2022-03-07 09:48:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
envs = s.run()
|