mirror of
https://github.com/gsi-upm/soil
synced 2024-11-24 11:52:29 +00:00
Add generator and programmatic examples
This commit is contained in:
parent
8593ac999d
commit
09e14c6e84
@ -26,7 +26,7 @@ But before that, let's import the soil module and networkx.
|
|||||||
%autoreload 2
|
%autoreload 2
|
||||||
|
|
||||||
%pylab inline
|
%pylab inline
|
||||||
# To display plots in the notebooed_
|
# To display plots in the notebook_
|
||||||
|
|
||||||
|
|
||||||
.. parsed-literal::
|
.. parsed-literal::
|
||||||
@ -2531,7 +2531,7 @@ Dealing with bigger data
|
|||||||
|
|
||||||
.. parsed-literal::
|
.. parsed-literal::
|
||||||
|
|
||||||
267M ../rabbits/soil_output/rabbits_example/
|
267M ../rabbits/soil_output/rabbits_example/
|
||||||
|
|
||||||
|
|
||||||
If we tried to load the entire history, we would probably run out of
|
If we tried to load the entire history, we would probably run out of
|
||||||
|
17
examples/custom_generator/custom_generator.yml
Normal file
17
examples/custom_generator/custom_generator.yml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
---
|
||||||
|
name: custom-generator
|
||||||
|
description: Using a custom generator for the network
|
||||||
|
num_trials: 3
|
||||||
|
dry_run: True
|
||||||
|
max_time: 100
|
||||||
|
interval: 1
|
||||||
|
network_params:
|
||||||
|
generator: mymodule.mygenerator
|
||||||
|
# These are custom parameters
|
||||||
|
n: 10
|
||||||
|
n_edges: 5
|
||||||
|
network_agents:
|
||||||
|
- agent_type: CounterModel
|
||||||
|
weight: 1
|
||||||
|
state:
|
||||||
|
id: 0
|
27
examples/custom_generator/mymodule.py
Normal file
27
examples/custom_generator/mymodule.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
from networkx import Graph
|
||||||
|
import networkx as nx
|
||||||
|
from random import choice
|
||||||
|
|
||||||
|
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 = choice(nodes)
|
||||||
|
nodes.remove(n_in) # Avoid loops
|
||||||
|
n_out = choice(nodes)
|
||||||
|
G.add_edge(n_in, n_out)
|
||||||
|
return G
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
1
examples/programmatic/.gitignore
vendored
Normal file
1
examples/programmatic/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
Programmatic*
|
38
examples/programmatic/programmatic.py
Normal file
38
examples/programmatic/programmatic.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
'''
|
||||||
|
Example of a fully programmatic simulation, without definition files.
|
||||||
|
'''
|
||||||
|
from soil import Simulation, agents
|
||||||
|
from networkx import Graph
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
def mygenerator():
|
||||||
|
# Add only a node
|
||||||
|
G = Graph()
|
||||||
|
G.add_node(1)
|
||||||
|
return G
|
||||||
|
|
||||||
|
|
||||||
|
class MyAgent(agents.FSM):
|
||||||
|
|
||||||
|
@agents.default_state
|
||||||
|
@agents.state
|
||||||
|
def neutral(self):
|
||||||
|
self.info('I am running')
|
||||||
|
|
||||||
|
|
||||||
|
s = Simulation(name='Programmatic',
|
||||||
|
network_params={'generator': mygenerator},
|
||||||
|
num_trials=1,
|
||||||
|
max_time=100,
|
||||||
|
agent_type=MyAgent,
|
||||||
|
dry_run=True)
|
||||||
|
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
envs = s.run()
|
||||||
|
|
||||||
|
s.dump_yaml()
|
||||||
|
|
||||||
|
for env in envs:
|
||||||
|
env.dump_csv()
|
@ -1 +1 @@
|
|||||||
0.13.4
|
0.13.6
|
||||||
|
@ -88,14 +88,8 @@ class Simulation(NetworkSimulation):
|
|||||||
environment_agents=None, environment_params=None,
|
environment_agents=None, environment_params=None,
|
||||||
environment_class=None, **kwargs):
|
environment_class=None, **kwargs):
|
||||||
|
|
||||||
if topology is None:
|
self.seed = str(seed) or str(time.time())
|
||||||
topology = utils.load_network(network_params,
|
|
||||||
dir_path=dir_path)
|
|
||||||
elif isinstance(topology, basestring) or isinstance(topology, dict):
|
|
||||||
topology = json_graph.node_link_graph(topology)
|
|
||||||
|
|
||||||
self.load_module = load_module
|
self.load_module = load_module
|
||||||
self.topology = nx.Graph(topology)
|
|
||||||
self.network_params = network_params
|
self.network_params = network_params
|
||||||
self.name = name or 'UnnamedSimulation'
|
self.name = name or 'UnnamedSimulation'
|
||||||
self.num_trials = num_trials
|
self.num_trials = num_trials
|
||||||
@ -103,12 +97,19 @@ class Simulation(NetworkSimulation):
|
|||||||
self.default_state = default_state or {}
|
self.default_state = default_state or {}
|
||||||
self.dir_path = dir_path or os.getcwd()
|
self.dir_path = dir_path or os.getcwd()
|
||||||
self.interval = interval
|
self.interval = interval
|
||||||
self.seed = str(seed) or str(time.time())
|
|
||||||
self.dump = dump
|
self.dump = dump
|
||||||
self.dry_run = dry_run
|
self.dry_run = dry_run
|
||||||
|
|
||||||
sys.path += [self.dir_path, os.getcwd()]
|
sys.path += [self.dir_path, os.getcwd()]
|
||||||
|
|
||||||
|
if topology is None:
|
||||||
|
topology = utils.load_network(network_params,
|
||||||
|
dir_path=self.dir_path)
|
||||||
|
elif isinstance(topology, basestring) or isinstance(topology, dict):
|
||||||
|
topology = json_graph.node_link_graph(topology)
|
||||||
|
self.topology = nx.Graph(topology)
|
||||||
|
|
||||||
|
|
||||||
self.environment_params = environment_params or {}
|
self.environment_params = environment_params or {}
|
||||||
self.environment_class = utils.deserialize(environment_class,
|
self.environment_class = utils.deserialize(environment_class,
|
||||||
known_modules=['soil.environment', ]) or Environment
|
known_modules=['soil.environment', ]) or Environment
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import ast
|
import ast
|
||||||
|
import sys
|
||||||
import yaml
|
import yaml
|
||||||
import logging
|
import logging
|
||||||
import importlib
|
import importlib
|
||||||
@ -36,9 +37,14 @@ def load_network(network_params, dir_path=None):
|
|||||||
return method(path, **kwargs)
|
return method(path, **kwargs)
|
||||||
|
|
||||||
net_args = network_params.copy()
|
net_args = network_params.copy()
|
||||||
net_type = net_args.pop('generator')
|
net_gen = net_args.pop('generator')
|
||||||
|
|
||||||
|
if dir_path not in sys.path:
|
||||||
|
sys.path.append(dir_path)
|
||||||
|
|
||||||
|
method = deserializer(net_gen,
|
||||||
|
known_modules=['networkx.generators',])
|
||||||
|
|
||||||
method = getattr(nx.generators, net_type)
|
|
||||||
return method(**net_args)
|
return method(**net_args)
|
||||||
|
|
||||||
|
|
||||||
@ -114,6 +120,8 @@ def serialize(v, known_modules=[]):
|
|||||||
return func(v), tname
|
return func(v), tname
|
||||||
|
|
||||||
def deserializer(type_, known_modules=[]):
|
def deserializer(type_, known_modules=[]):
|
||||||
|
if type(type_) != str: # Already deserialized
|
||||||
|
return type_
|
||||||
if type_ == 'str':
|
if type_ == 'str':
|
||||||
return lambda x='': x
|
return lambda x='': x
|
||||||
if type_ == 'None':
|
if type_ == 'None':
|
||||||
|
Loading…
Reference in New Issue
Block a user