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

46 lines
1.0 KiB
Python
Raw Normal View History

2022-03-07 09:48:59 +00:00
'''
Example of setting a
Example of a fully programmatic simulation, without definition files.
'''
from soil import Simulation, agents
from soil.time import Delta
from random import expovariate
import logging
class MyAgent(agents.FSM):
2022-03-07 10:17:27 +00:00
'''
An agent that first does a ping
'''
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):
self.info('Ping')
return self.pong, Delta(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()
2022-03-07 09:48:59 +00:00
return None, Delta(expovariate(1/16))
2022-03-07 10:17:27 +00:00
2022-03-07 09:48:59 +00:00
s = Simulation(name='Programmatic',
network_agents=[{'agent_type': MyAgent, 'id': 0}],
topology={'nodes': [{'id': 0}], 'links': []},
num_trials=1,
max_time=100,
agent_type=MyAgent,
dry_run=True)
logging.basicConfig(level=logging.INFO)
envs = s.run()