1
0
mirror of https://github.com/gsi-upm/soil synced 2024-09-21 15:51:43 +00:00
soil/examples/custom_timeouts/custom_timeouts.py

36 lines
996 B
Python
Raw Normal View History

2019-02-01 18:05:07 +00:00
from soil.agents import FSM, state, default_state
class Fibonacci(FSM):
'''Agent that only executes in t_steps that are Fibonacci numbers'''
defaults = {
'prev': 1
}
@default_state
@state
def counting(self):
self.log('Stopping at {}'.format(self.now))
prev, self['prev'] = self['prev'], max([self.now, self['prev']])
return None, self.env.timeout(prev)
2019-02-01 18:05:07 +00:00
class Odds(FSM):
'''Agent that only executes in odd t_steps'''
@default_state
@state
def odds(self):
self.log('Stopping at {}'.format(self.now))
return None, self.env.timeout(1+self.now%2)
2019-02-01 18:05:07 +00:00
if __name__ == '__main__':
from soil import Simulation
2022-10-06 13:49:10 +00:00
s = Simulation(network_agents=[{'ids': [0], 'agent_class': Fibonacci},
{'ids': [1], 'agent_class': Odds}],
2019-02-01 18:05:07 +00:00
network_params={"generator": "complete_graph", "n": 2},
max_time=100,
)
2019-04-29 16:47:15 +00:00
s.run(dry_run=True)