mirror of
https://github.com/gsi-upm/soil
synced 2024-11-22 11:12:29 +00:00
feab0ba79e
The examples weren't being properly tested in the last commit. When we fixed that a lot of bugs in the new implementation of environment and agent were found, which accounts for most of these changes. The main difference is the mechanism to load simulations from a configuration file. For that to work, we had to rework our module loading code in `serialization` and add a `source_file` attribute to configurations (and simulations, for that matter).
39 lines
1004 B
Python
39 lines
1004 B
Python
from networkx import Graph
|
|
import random
|
|
import networkx as nx
|
|
from soil import Simulation, Environment, CounterModel, parameters
|
|
|
|
|
|
def mygenerator(n=5, n_edges=5):
|
|
"""
|
|
Just a simple generator that creates a network with n nodes and
|
|
n_edges edges. Edges are assigned randomly, only avoiding self loops.
|
|
"""
|
|
G = nx.Graph()
|
|
|
|
for i in range(n):
|
|
G.add_node(i)
|
|
|
|
for i in range(n_edges):
|
|
nodes = list(G.nodes)
|
|
n_in = random.choice(nodes)
|
|
nodes.remove(n_in) # Avoid loops
|
|
n_out = random.choice(nodes)
|
|
G.add_edge(n_in, n_out)
|
|
return G
|
|
|
|
|
|
class GeneratorEnv(Environment):
|
|
"""Using a custom generator for the network"""
|
|
|
|
generator: parameters.function = staticmethod(mygenerator)
|
|
|
|
def init(self):
|
|
self.create_network(generator=self.generator, n=10, n_edges=5)
|
|
self.add_agents(CounterModel)
|
|
|
|
|
|
sim = Simulation(model=GeneratorEnv, max_steps=10, interval=1)
|
|
|
|
if __name__ == '__main__':
|
|
sim.run(dump=False) |