2017-06-20 15:45:43 +00:00
|
|
|
from unittest import TestCase
|
|
|
|
|
|
|
|
import os
|
2019-04-30 14:16:46 +00:00
|
|
|
import io
|
2017-06-20 15:45:43 +00:00
|
|
|
import yaml
|
2018-12-09 15:38:18 +00:00
|
|
|
import pickle
|
2018-02-16 17:04:43 +00:00
|
|
|
import networkx as nx
|
2017-06-20 15:45:43 +00:00
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
from os.path import join
|
2019-04-26 17:22:45 +00:00
|
|
|
from soil import (simulation, Environment, agents, serialization,
|
2021-10-15 18:15:17 +00:00
|
|
|
utils)
|
2021-10-15 11:36:39 +00:00
|
|
|
from soil.time import Delta
|
2017-06-20 15:45:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
ROOT = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
EXAMPLES = join(ROOT, '..', 'examples')
|
|
|
|
|
2018-12-08 17:53:06 +00:00
|
|
|
|
2019-05-16 17:59:46 +00:00
|
|
|
class CustomAgent(agents.FSM):
|
|
|
|
@agents.default_state
|
|
|
|
@agents.state
|
|
|
|
def normal(self):
|
2021-10-15 18:15:17 +00:00
|
|
|
self.neighbors = self.count_agents(state_id='normal',
|
|
|
|
limit_neighbors=True)
|
2019-05-16 17:59:46 +00:00
|
|
|
@agents.state
|
|
|
|
def unreachable(self):
|
|
|
|
return
|
2018-12-08 17:53:06 +00:00
|
|
|
|
2017-06-20 15:45:43 +00:00
|
|
|
class TestMain(TestCase):
|
|
|
|
|
|
|
|
def test_load_graph(self):
|
|
|
|
"""
|
|
|
|
Load a graph from file if the extension is known.
|
|
|
|
Raise an exception otherwise.
|
|
|
|
"""
|
|
|
|
config = {
|
|
|
|
'network_params': {
|
|
|
|
'path': join(ROOT, 'test.gexf')
|
|
|
|
}
|
|
|
|
}
|
2019-04-26 17:22:45 +00:00
|
|
|
G = serialization.load_network(config['network_params'])
|
2017-06-20 15:45:43 +00:00
|
|
|
assert G
|
|
|
|
assert len(G) == 2
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
config = {
|
|
|
|
'network_params': {
|
|
|
|
'path': join(ROOT, 'unknown.extension')
|
|
|
|
}
|
|
|
|
}
|
2019-04-26 17:22:45 +00:00
|
|
|
G = serialization.load_network(config['network_params'])
|
2017-06-20 15:45:43 +00:00
|
|
|
print(G)
|
|
|
|
|
|
|
|
def test_generate_barabasi(self):
|
|
|
|
"""
|
|
|
|
If no path is given, a generator and network parameters
|
|
|
|
should be used to generate a network
|
|
|
|
"""
|
|
|
|
config = {
|
|
|
|
'network_params': {
|
|
|
|
'generator': 'barabasi_albert_graph'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
with self.assertRaises(TypeError):
|
2019-04-26 17:22:45 +00:00
|
|
|
G = serialization.load_network(config['network_params'])
|
2017-06-20 15:45:43 +00:00
|
|
|
config['network_params']['n'] = 100
|
|
|
|
config['network_params']['m'] = 10
|
2019-04-26 17:22:45 +00:00
|
|
|
G = serialization.load_network(config['network_params'])
|
2017-06-20 15:45:43 +00:00
|
|
|
assert len(G) == 100
|
|
|
|
|
|
|
|
def test_empty_simulation(self):
|
|
|
|
"""A simulation with a base behaviour should do nothing"""
|
|
|
|
config = {
|
|
|
|
'network_params': {
|
|
|
|
'path': join(ROOT, 'test.gexf')
|
|
|
|
},
|
2017-10-18 18:28:42 +00:00
|
|
|
'agent_type': 'BaseAgent',
|
2017-06-20 15:45:43 +00:00
|
|
|
'environment_params': {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = simulation.from_config(config)
|
2018-02-16 17:04:43 +00:00
|
|
|
s.run_simulation(dry_run=True)
|
2017-06-20 15:45:43 +00:00
|
|
|
|
|
|
|
def test_counter_agent(self):
|
|
|
|
"""
|
|
|
|
The initial states should be applied to the agent and the
|
|
|
|
agent should be able to update its state."""
|
|
|
|
config = {
|
2018-02-16 17:04:43 +00:00
|
|
|
'name': 'CounterAgent',
|
2017-06-20 15:45:43 +00:00
|
|
|
'network_params': {
|
|
|
|
'path': join(ROOT, 'test.gexf')
|
|
|
|
},
|
|
|
|
'agent_type': 'CounterModel',
|
2018-05-04 08:01:49 +00:00
|
|
|
'states': [{'times': 10}, {'times': 20}],
|
2017-06-20 15:45:43 +00:00
|
|
|
'max_time': 2,
|
|
|
|
'num_trials': 1,
|
|
|
|
'environment_params': {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = simulation.from_config(config)
|
2018-02-16 17:04:43 +00:00
|
|
|
env = s.run_simulation(dry_run=True)[0]
|
2018-05-04 08:01:49 +00:00
|
|
|
assert env.get_agent(0)['times', 0] == 11
|
|
|
|
assert env.get_agent(0)['times', 1] == 12
|
|
|
|
assert env.get_agent(1)['times', 0] == 21
|
|
|
|
assert env.get_agent(1)['times', 1] == 22
|
2017-06-20 15:45:43 +00:00
|
|
|
|
|
|
|
def test_counter_agent_history(self):
|
|
|
|
"""
|
|
|
|
The evolution of the state should be recorded in the logging agent
|
|
|
|
"""
|
|
|
|
config = {
|
2018-02-16 17:04:43 +00:00
|
|
|
'name': 'CounterAgent',
|
2017-06-20 15:45:43 +00:00
|
|
|
'network_params': {
|
|
|
|
'path': join(ROOT, 'test.gexf')
|
|
|
|
},
|
|
|
|
'network_agents': [{
|
|
|
|
'agent_type': 'AggregatedCounter',
|
|
|
|
'weight': 1,
|
2021-10-15 18:15:17 +00:00
|
|
|
'state': {'state_id': 0}
|
2017-06-20 15:45:43 +00:00
|
|
|
|
|
|
|
}],
|
|
|
|
'max_time': 10,
|
|
|
|
'environment_params': {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = simulation.from_config(config)
|
2018-02-16 17:04:43 +00:00
|
|
|
env = s.run_simulation(dry_run=True)[0]
|
2017-06-20 15:45:43 +00:00
|
|
|
for agent in env.network_agents:
|
|
|
|
last = 0
|
2022-05-12 14:14:47 +00:00
|
|
|
assert len(agent[None, None]) == 10
|
2018-05-04 08:01:49 +00:00
|
|
|
for step, total in sorted(agent['total', None]):
|
|
|
|
assert total == last + 2
|
|
|
|
last = total
|
2017-06-20 15:45:43 +00:00
|
|
|
|
|
|
|
def test_custom_agent(self):
|
|
|
|
"""Allow for search of neighbors with a certain state_id"""
|
|
|
|
config = {
|
|
|
|
'network_params': {
|
|
|
|
'path': join(ROOT, 'test.gexf')
|
|
|
|
},
|
|
|
|
'network_agents': [{
|
|
|
|
'agent_type': CustomAgent,
|
2019-05-16 17:59:46 +00:00
|
|
|
'weight': 1
|
2017-06-20 15:45:43 +00:00
|
|
|
|
|
|
|
}],
|
|
|
|
'max_time': 10,
|
|
|
|
'environment_params': {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = simulation.from_config(config)
|
2018-02-16 17:04:43 +00:00
|
|
|
env = s.run_simulation(dry_run=True)[0]
|
2019-05-16 17:59:46 +00:00
|
|
|
assert env.get_agent(1).count_agents(state_id='normal') == 2
|
|
|
|
assert env.get_agent(1).count_agents(state_id='normal', limit_neighbors=True) == 1
|
2021-10-15 18:15:17 +00:00
|
|
|
assert env.get_agent(0).neighbors == 1
|
2017-06-20 15:45:43 +00:00
|
|
|
|
|
|
|
def test_torvalds_example(self):
|
|
|
|
"""A complete example from a documentation should work."""
|
2019-04-26 17:22:45 +00:00
|
|
|
config = serialization.load_file(join(EXAMPLES, 'torvalds.yml'))[0]
|
2017-06-20 15:45:43 +00:00
|
|
|
config['network_params']['path'] = join(EXAMPLES,
|
|
|
|
config['network_params']['path'])
|
|
|
|
s = simulation.from_config(config)
|
2019-04-29 16:47:15 +00:00
|
|
|
env = s.run_simulation(dry_run=True)[0]
|
2017-06-20 15:45:43 +00:00
|
|
|
for a in env.network_agents:
|
|
|
|
skill_level = a.state['skill_level']
|
|
|
|
if a.id == 'Torvalds':
|
|
|
|
assert skill_level == 'God'
|
|
|
|
assert a.state['total'] == 3
|
|
|
|
assert a.state['neighbors'] == 2
|
|
|
|
elif a.id == 'balkian':
|
|
|
|
assert skill_level == 'developer'
|
|
|
|
assert a.state['total'] == 3
|
|
|
|
assert a.state['neighbors'] == 1
|
|
|
|
else:
|
|
|
|
assert skill_level == 'beginner'
|
|
|
|
assert a.state['total'] == 3
|
|
|
|
assert a.state['neighbors'] == 1
|
|
|
|
|
|
|
|
def test_yaml(self):
|
|
|
|
"""
|
|
|
|
The YAML version of a newly created simulation
|
|
|
|
should be equivalent to the configuration file used
|
|
|
|
"""
|
|
|
|
with utils.timer('loading'):
|
2019-04-26 17:22:45 +00:00
|
|
|
config = serialization.load_file(join(EXAMPLES, 'complete.yml'))[0]
|
2017-06-20 15:45:43 +00:00
|
|
|
s = simulation.from_config(config)
|
|
|
|
with utils.timer('serializing'):
|
|
|
|
serial = s.to_yaml()
|
|
|
|
with utils.timer('recovering'):
|
2020-03-11 15:17:14 +00:00
|
|
|
recovered = yaml.load(serial, Loader=yaml.SafeLoader)
|
2017-06-20 15:45:43 +00:00
|
|
|
with utils.timer('deleting'):
|
|
|
|
del recovered['topology']
|
|
|
|
assert config == recovered
|
|
|
|
|
|
|
|
def test_configuration_changes(self):
|
|
|
|
"""
|
|
|
|
The configuration should not change after running
|
|
|
|
the simulation.
|
|
|
|
"""
|
2019-04-26 17:22:45 +00:00
|
|
|
config = serialization.load_file(join(EXAMPLES, 'complete.yml'))[0]
|
2017-06-20 15:45:43 +00:00
|
|
|
s = simulation.from_config(config)
|
2021-10-14 15:37:06 +00:00
|
|
|
|
|
|
|
s.run_simulation(dry_run=True)
|
|
|
|
nconfig = s.to_dict()
|
|
|
|
del nconfig['topology']
|
|
|
|
assert config == nconfig
|
2017-06-20 15:45:43 +00:00
|
|
|
|
2017-10-17 17:48:56 +00:00
|
|
|
def test_row_conversion(self):
|
2019-04-29 16:47:15 +00:00
|
|
|
env = Environment()
|
2017-10-17 17:48:56 +00:00
|
|
|
env['test'] = 'test_value'
|
|
|
|
|
|
|
|
res = list(env.history_to_tuples())
|
|
|
|
assert len(res) == len(env.environment_params)
|
|
|
|
|
2021-10-14 15:37:06 +00:00
|
|
|
env.schedule.time = 1
|
2017-10-17 17:48:56 +00:00
|
|
|
env['test'] = 'second_value'
|
|
|
|
res = list(env.history_to_tuples())
|
|
|
|
|
|
|
|
assert env['env', 0, 'test' ] == 'test_value'
|
|
|
|
assert env['env', 1, 'test' ] == 'second_value'
|
|
|
|
|
2018-02-16 17:04:43 +00:00
|
|
|
def test_save_geometric(self):
|
|
|
|
"""
|
|
|
|
There is a bug in networkx that prevents it from creating a GEXF file
|
|
|
|
from geometric models. We should work around it.
|
|
|
|
"""
|
2018-05-07 16:57:41 +00:00
|
|
|
G = nx.random_geometric_graph(20, 0.1)
|
2019-04-29 16:47:15 +00:00
|
|
|
env = Environment(topology=G)
|
2019-04-30 14:16:46 +00:00
|
|
|
f = io.BytesIO()
|
|
|
|
env.dump_gexf(f)
|
2017-06-20 15:45:43 +00:00
|
|
|
|
2018-05-07 16:57:41 +00:00
|
|
|
def test_save_graph(self):
|
|
|
|
'''
|
|
|
|
The history_to_graph method should return a valid networkx graph.
|
|
|
|
|
|
|
|
The state of the agent should be encoded as intervals in the nx graph.
|
|
|
|
'''
|
|
|
|
G = nx.cycle_graph(5)
|
|
|
|
distribution = agents.calculate_distribution(None, agents.BaseAgent)
|
2019-04-29 16:47:15 +00:00
|
|
|
env = Environment(topology=G, network_agents=distribution)
|
2018-05-07 16:57:41 +00:00
|
|
|
env[0, 0, 'testvalue'] = 'start'
|
|
|
|
env[0, 10, 'testvalue'] = 'finish'
|
|
|
|
nG = env.history_to_graph()
|
2020-03-11 15:17:14 +00:00
|
|
|
values = nG.nodes[0]['attr_testvalue']
|
2018-05-07 16:57:41 +00:00
|
|
|
assert ('start', 0, 10) in values
|
|
|
|
assert ('finish', 10, None) in values
|
|
|
|
|
2018-12-04 08:54:29 +00:00
|
|
|
def test_serialize_class(self):
|
2019-04-26 17:22:45 +00:00
|
|
|
ser, name = serialization.serialize(agents.BaseAgent)
|
2018-12-04 08:54:29 +00:00
|
|
|
assert name == 'soil.agents.BaseAgent'
|
|
|
|
assert ser == agents.BaseAgent
|
2017-06-20 15:45:43 +00:00
|
|
|
|
2019-04-26 17:22:45 +00:00
|
|
|
ser, name = serialization.serialize(CustomAgent)
|
2018-12-04 08:54:29 +00:00
|
|
|
assert name == 'test_main.CustomAgent'
|
|
|
|
assert ser == CustomAgent
|
2018-12-09 15:38:18 +00:00
|
|
|
pickle.dumps(ser)
|
2017-06-20 15:45:43 +00:00
|
|
|
|
2018-12-04 08:54:29 +00:00
|
|
|
def test_serialize_builtin_types(self):
|
2017-06-20 15:45:43 +00:00
|
|
|
|
2018-12-04 08:54:29 +00:00
|
|
|
for i in [1, None, True, False, {}, [], list(), dict()]:
|
2019-04-26 17:22:45 +00:00
|
|
|
ser, name = serialization.serialize(i)
|
2018-12-04 08:54:29 +00:00
|
|
|
assert type(ser) == str
|
2019-04-26 17:22:45 +00:00
|
|
|
des = serialization.deserialize(name, ser)
|
2018-12-04 08:54:29 +00:00
|
|
|
assert i == des
|
2017-06-20 15:45:43 +00:00
|
|
|
|
2018-12-08 17:53:06 +00:00
|
|
|
def test_serialize_agent_type(self):
|
|
|
|
'''A class from soil.agents should be serialized without the module part'''
|
|
|
|
ser = agents.serialize_type(CustomAgent)
|
|
|
|
assert ser == 'test_main.CustomAgent'
|
|
|
|
ser = agents.serialize_type(agents.BaseAgent)
|
|
|
|
assert ser == 'BaseAgent'
|
2018-12-09 15:38:18 +00:00
|
|
|
pickle.dumps(ser)
|
|
|
|
|
2018-12-04 08:54:29 +00:00
|
|
|
def test_deserialize_agent_distribution(self):
|
|
|
|
agent_distro = [
|
|
|
|
{
|
|
|
|
'agent_type': 'CounterModel',
|
|
|
|
'weight': 1
|
|
|
|
},
|
|
|
|
{
|
2018-12-08 17:53:06 +00:00
|
|
|
'agent_type': 'test_main.CustomAgent',
|
2018-12-04 08:54:29 +00:00
|
|
|
'weight': 2
|
|
|
|
},
|
|
|
|
]
|
2021-10-14 15:37:06 +00:00
|
|
|
converted = agents.deserialize_definition(agent_distro)
|
2018-12-04 08:54:29 +00:00
|
|
|
assert converted[0]['agent_type'] == agents.CounterModel
|
2018-12-08 17:53:06 +00:00
|
|
|
assert converted[1]['agent_type'] == CustomAgent
|
2018-12-09 15:38:18 +00:00
|
|
|
pickle.dumps(converted)
|
2018-12-04 08:54:29 +00:00
|
|
|
|
|
|
|
def test_serialize_agent_distribution(self):
|
|
|
|
agent_distro = [
|
|
|
|
{
|
|
|
|
'agent_type': agents.CounterModel,
|
|
|
|
'weight': 1
|
|
|
|
},
|
|
|
|
{
|
2018-12-08 17:53:06 +00:00
|
|
|
'agent_type': CustomAgent,
|
2018-12-04 08:54:29 +00:00
|
|
|
'weight': 2
|
|
|
|
},
|
|
|
|
]
|
2021-10-14 15:37:06 +00:00
|
|
|
converted = agents.serialize_definition(agent_distro)
|
2018-12-04 08:54:29 +00:00
|
|
|
assert converted[0]['agent_type'] == 'CounterModel'
|
2018-12-08 17:53:06 +00:00
|
|
|
assert converted[1]['agent_type'] == 'test_main.CustomAgent'
|
2018-12-09 15:38:18 +00:00
|
|
|
pickle.dumps(converted)
|
|
|
|
|
|
|
|
def test_pickle_agent_environment(self):
|
|
|
|
env = Environment(name='Test')
|
2021-10-14 15:37:06 +00:00
|
|
|
a = agents.BaseAgent(model=env, unique_id=25)
|
2018-12-09 15:38:18 +00:00
|
|
|
|
|
|
|
a['key'] = 'test'
|
|
|
|
|
|
|
|
pickled = pickle.dumps(a)
|
|
|
|
recovered = pickle.loads(pickled)
|
|
|
|
|
|
|
|
assert recovered.env.name == 'Test'
|
2019-04-29 16:47:15 +00:00
|
|
|
assert list(recovered.env._history.to_tuples())
|
2018-12-09 15:38:18 +00:00
|
|
|
assert recovered['key', 0] == 'test'
|
2019-04-29 16:47:15 +00:00
|
|
|
assert recovered['key'] == 'test'
|
2018-12-04 08:54:29 +00:00
|
|
|
|
2019-02-19 20:17:19 +00:00
|
|
|
def test_subgraph(self):
|
|
|
|
'''An agent should be able to subgraph the global topology'''
|
|
|
|
G = nx.Graph()
|
|
|
|
G.add_node(3)
|
|
|
|
G.add_edge(1, 2)
|
|
|
|
distro = agents.calculate_distribution(agent_type=agents.NetworkAgent)
|
|
|
|
env = Environment(name='Test', topology=G, network_agents=distro)
|
|
|
|
lst = list(env.network_agents)
|
|
|
|
|
|
|
|
a2 = env.get_agent(2)
|
|
|
|
a3 = env.get_agent(3)
|
|
|
|
assert len(a2.subgraph(limit_neighbors=True)) == 2
|
|
|
|
assert len(a3.subgraph(limit_neighbors=True)) == 1
|
|
|
|
assert len(a3.subgraph(limit_neighbors=True, center=False)) == 0
|
|
|
|
assert len(a3.subgraph(agent_type=agents.NetworkAgent)) == 3
|
2019-04-26 17:22:45 +00:00
|
|
|
|
|
|
|
def test_templates(self):
|
|
|
|
'''Loading a template should result in several configs'''
|
|
|
|
configs = serialization.load_file(join(EXAMPLES, 'template.yml'))
|
|
|
|
assert len(configs) > 0
|
|
|
|
|
2020-10-19 11:14:48 +00:00
|
|
|
def test_until(self):
|
|
|
|
config = {
|
2021-10-14 15:37:06 +00:00
|
|
|
'name': 'until_sim',
|
2020-10-19 11:14:48 +00:00
|
|
|
'network_params': {},
|
|
|
|
'agent_type': 'CounterModel',
|
|
|
|
'max_time': 2,
|
2021-10-15 18:15:17 +00:00
|
|
|
'num_trials': 50,
|
2020-10-19 11:14:48 +00:00
|
|
|
'environment_params': {}
|
|
|
|
}
|
|
|
|
s = simulation.from_config(config)
|
|
|
|
runs = list(s.run_simulation(dry_run=True))
|
|
|
|
over = list(x.now for x in runs if x.now>2)
|
2021-10-15 18:15:17 +00:00
|
|
|
assert len(runs) == config['num_trials']
|
2020-10-19 11:14:48 +00:00
|
|
|
assert len(over) == 0
|
2021-10-15 11:36:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_fsm(self):
|
|
|
|
'''Basic state change'''
|
|
|
|
class ToggleAgent(agents.FSM):
|
|
|
|
@agents.default_state
|
|
|
|
@agents.state
|
|
|
|
def ping(self):
|
|
|
|
return self.pong
|
|
|
|
|
|
|
|
@agents.state
|
|
|
|
def pong(self):
|
|
|
|
return self.ping
|
|
|
|
|
|
|
|
a = ToggleAgent(unique_id=1, model=Environment())
|
2021-10-15 18:15:17 +00:00
|
|
|
assert a.state_id == a.ping.id
|
2021-10-15 11:36:39 +00:00
|
|
|
a.step()
|
2021-10-15 18:15:17 +00:00
|
|
|
assert a.state_id == a.pong.id
|
2021-10-15 11:36:39 +00:00
|
|
|
a.step()
|
2021-10-15 18:15:17 +00:00
|
|
|
assert a.state_id == a.ping.id
|
2021-10-15 11:36:39 +00:00
|
|
|
|
|
|
|
def test_fsm_when(self):
|
|
|
|
'''Basic state change'''
|
|
|
|
class ToggleAgent(agents.FSM):
|
|
|
|
@agents.default_state
|
|
|
|
@agents.state
|
|
|
|
def ping(self):
|
|
|
|
return self.pong, 2
|
|
|
|
|
|
|
|
@agents.state
|
|
|
|
def pong(self):
|
|
|
|
return self.ping
|
|
|
|
|
|
|
|
a = ToggleAgent(unique_id=1, model=Environment())
|
|
|
|
when = a.step()
|
|
|
|
assert when == 2
|
|
|
|
when = a.step()
|
|
|
|
assert when == Delta(a.interval)
|