1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-22 19:22:29 +00:00
soil/examples/custom_timeouts/custom_timeouts_sim.py

41 lines
1.0 KiB
Python
Raw Normal View History

2019-02-01 18:05:07 +00:00
from soil.agents import FSM, state, default_state
from soil.time import Delta
2019-02-01 18:05:07 +00:00
class Fibonacci(FSM):
2022-10-17 18:23:57 +00:00
"""Agent that only executes in t_steps that are Fibonacci numbers"""
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"]])
return None, Delta(prev)
2019-02-01 18:05:07 +00:00
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, Delta(1 + self.now % 2)
2019-02-01 18:05:07 +00:00
from soil import Environment, Simulation
from networkx import complete_graph
class TimeoutsEnv(Environment):
def init(self):
self.create_network(generator=complete_graph, n=2)
self.add_agent(agent_class=Fibonacci, node_id=0)
self.add_agent(agent_class=Odds, node_id=1)
sim = Simulation(model=TimeoutsEnv, max_steps=10, interval=1)
if __name__ == "__main__":
sim.run(dump=False)