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
|
|
|
"""
|
2022-03-07 09:48:59 +00:00
|
|
|
from soil import Simulation, agents
|
|
|
|
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))
|
|
|
|
|
|
|
|
|
|
|
|
s = Simulation(
|
|
|
|
name="Programmatic",
|
|
|
|
network_agents=[{"agent_class": MyAgent, "id": 0}],
|
|
|
|
topology={"nodes": [{"id": 0}], "links": []},
|
|
|
|
num_trials=1,
|
|
|
|
max_time=100,
|
|
|
|
agent_class=MyAgent,
|
|
|
|
dry_run=True,
|
|
|
|
)
|
2022-03-07 09:48:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
envs = s.run()
|