1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-14 15:32:29 +00:00
soil/examples/custom_timeouts/custom_timeouts.py

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