2022-10-17 18:23:57 +00:00
|
|
|
"""
|
2018-12-20 18:25:33 +00:00
|
|
|
Example of a fully programmatic simulation, without definition files.
|
2022-10-17 18:23:57 +00:00
|
|
|
"""
|
2023-04-09 02:19:24 +00:00
|
|
|
from soil import Simulation, Environment, agents
|
2018-12-20 18:25:33 +00:00
|
|
|
from networkx import Graph
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
def mygenerator():
|
|
|
|
# Add only a node
|
|
|
|
G = Graph()
|
|
|
|
G.add_node(1)
|
2022-11-13 19:31:05 +00:00
|
|
|
G.add_node(2)
|
2018-12-20 18:25:33 +00:00
|
|
|
return G
|
|
|
|
|
|
|
|
|
2023-04-14 17:41:24 +00:00
|
|
|
class MyAgent(agents.NetworkAgent, agents.FSM):
|
2022-11-13 19:31:05 +00:00
|
|
|
times_run = 0
|
2018-12-20 18:25:33 +00:00
|
|
|
@agents.default_state
|
|
|
|
@agents.state
|
|
|
|
def neutral(self):
|
2022-10-17 18:23:57 +00:00
|
|
|
self.debug("I am running")
|
2022-11-13 19:31:05 +00:00
|
|
|
if self.prob(0.2):
|
|
|
|
self.times_run += 1
|
2022-10-17 18:23:57 +00:00
|
|
|
self.info("This runs 2/10 times on average")
|
2018-12-20 18:25:33 +00:00
|
|
|
|
|
|
|
|
2023-04-09 02:19:24 +00:00
|
|
|
class ProgrammaticEnv(Environment):
|
|
|
|
|
|
|
|
def init(self):
|
|
|
|
self.create_network(generator=mygenerator)
|
2023-04-14 17:41:24 +00:00
|
|
|
assert len(self.G)
|
2023-04-09 02:19:24 +00:00
|
|
|
self.populate_network(agent_class=MyAgent)
|
|
|
|
self.add_agent_reporter('times_run')
|
|
|
|
|
|
|
|
|
2022-11-13 19:31:05 +00:00
|
|
|
simulation = Simulation(
|
2022-10-17 18:23:57 +00:00
|
|
|
name="Programmatic",
|
2023-04-09 02:19:24 +00:00
|
|
|
model=ProgrammaticEnv,
|
2022-11-13 19:31:05 +00:00
|
|
|
seed='Program',
|
2023-04-20 15:56:44 +00:00
|
|
|
iterations=1,
|
2022-10-17 18:23:57 +00:00
|
|
|
max_time=100,
|
2023-04-14 17:41:24 +00:00
|
|
|
dump=False,
|
2022-10-17 18:23:57 +00:00
|
|
|
)
|
2018-12-20 18:25:33 +00:00
|
|
|
|
2022-11-13 19:31:05 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
# By default, logging will only print WARNING logs (and above).
|
|
|
|
# You need to choose a lower logging level to get INFO/DEBUG traces
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
envs = simulation.run()
|
2018-12-20 18:25:33 +00:00
|
|
|
|
2022-11-13 19:31:05 +00:00
|
|
|
for agent in envs[0].agents:
|
|
|
|
print(agent.times_run)
|