2019-02-01 18:05:07 +00:00
|
|
|
from soil.agents import FSM, state, default_state
|
2022-11-13 19:31:05 +00:00
|
|
|
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"""
|
2023-04-09 02:19:24 +00:00
|
|
|
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"]])
|
2022-11-13 19:31:05 +00:00
|
|
|
return None, Delta(prev)
|
2019-02-01 18:05:07 +00:00
|
|
|
|
2022-10-16 15:54:03 +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))
|
2022-11-13 19:31:05 +00:00
|
|
|
return None, Delta(1 + self.now % 2)
|
2019-02-01 18:05:07 +00:00
|
|
|
|
2022-10-16 15:54:03 +00:00
|
|
|
|
2023-04-09 02:19:24 +00:00
|
|
|
from soil import Environment, Simulation
|
|
|
|
from networkx import complete_graph
|
|
|
|
|
|
|
|
|
|
|
|
class TimeoutsEnv(Environment):
|
|
|
|
def init(self):
|
2023-04-14 17:41:24 +00:00
|
|
|
self.create_network(generator=complete_graph, n=2)
|
2023-04-09 02:19:24 +00:00
|
|
|
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)
|
2022-11-13 19:31:05 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-04-14 17:41:24 +00:00
|
|
|
sim.run(dump=False)
|