2018-12-20 18:25:33 +00:00
|
|
|
from networkx import Graph
|
2022-10-13 20:43:16 +00:00
|
|
|
import random
|
2018-12-20 18:25:33 +00:00
|
|
|
import networkx as nx
|
2023-04-09 02:19:24 +00:00
|
|
|
from soil import Simulation, Environment, CounterModel, parameters
|
2018-12-20 18:25:33 +00:00
|
|
|
|
2022-10-17 18:23:57 +00:00
|
|
|
|
2018-12-20 18:25:33 +00:00
|
|
|
def mygenerator(n=5, n_edges=5):
|
2022-10-17 18:23:57 +00:00
|
|
|
"""
|
2018-12-20 18:25:33 +00:00
|
|
|
Just a simple generator that creates a network with n nodes and
|
|
|
|
n_edges edges. Edges are assigned randomly, only avoiding self loops.
|
2022-10-17 18:23:57 +00:00
|
|
|
"""
|
2018-12-20 18:25:33 +00:00
|
|
|
G = nx.Graph()
|
|
|
|
|
|
|
|
for i in range(n):
|
|
|
|
G.add_node(i)
|
2022-10-17 18:23:57 +00:00
|
|
|
|
2018-12-20 18:25:33 +00:00
|
|
|
for i in range(n_edges):
|
|
|
|
nodes = list(G.nodes)
|
2022-10-13 20:43:16 +00:00
|
|
|
n_in = random.choice(nodes)
|
2018-12-20 18:25:33 +00:00
|
|
|
nodes.remove(n_in) # Avoid loops
|
2022-10-13 20:43:16 +00:00
|
|
|
n_out = random.choice(nodes)
|
2018-12-20 18:25:33 +00:00
|
|
|
G.add_edge(n_in, n_out)
|
|
|
|
return G
|
2023-04-09 02:19:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GeneratorEnv(Environment):
|
|
|
|
"""Using a custom generator for the network"""
|
|
|
|
|
|
|
|
generator: parameters.function = mygenerator
|
|
|
|
|
|
|
|
def init(self):
|
|
|
|
self.create_network(network_generator=self.generator, n=10, n_edges=5)
|
|
|
|
self.init_agents(CounterModel)
|
|
|
|
|
|
|
|
|
|
|
|
sim = Simulation(model=GeneratorEnv, max_steps=10, interval=1)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sim.run(dry_run=True)
|