2017-06-20 15:45:43 +00:00
|
|
|
from unittest import TestCase
|
|
|
|
|
|
|
|
import os
|
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
|
2022-09-13 16:16:31 +00:00
|
|
|
from soil import (simulation, Environment, agents, network, serialization,
|
2022-09-16 16:13:39 +00:00
|
|
|
utils, config)
|
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
|
|
|
|
2022-09-13 16:16:31 +00:00
|
|
|
class CustomAgent(agents.FSM, agents.NetworkAgent):
|
2019-05-16 17:59:46 +00:00
|
|
|
@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_empty_simulation(self):
|
|
|
|
"""A simulation with a base behaviour should do nothing"""
|
|
|
|
config = {
|
2022-10-06 13:49:10 +00:00
|
|
|
'model_params': {
|
|
|
|
'network_params': {
|
|
|
|
'path': join(ROOT, 'test.gexf')
|
|
|
|
},
|
|
|
|
'agent_class': 'BaseAgent',
|
2017-06-20 15:45:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-06 13:49:10 +00:00
|
|
|
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
|
|
|
|
2022-05-10 14:29:06 +00:00
|
|
|
|
|
|
|
def test_network_agent(self):
|
2017-06-20 15:45:43 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
'num_trials': 1,
|
2022-10-06 13:49:10 +00:00
|
|
|
'max_time': 2,
|
|
|
|
'model_params': {
|
|
|
|
'network_params': {
|
|
|
|
'generator': nx.complete_graph,
|
|
|
|
'n': 2,
|
|
|
|
},
|
|
|
|
'agent_class': 'CounterModel',
|
|
|
|
'states': {
|
|
|
|
0: {'times': 10},
|
|
|
|
1: {'times': 20},
|
|
|
|
},
|
2017-06-20 15:45:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-06 13:49:10 +00:00
|
|
|
s = simulation.from_config(config)
|
2022-09-13 16:16:31 +00:00
|
|
|
|
2022-05-10 14:29:06 +00:00
|
|
|
def test_counter_agent(self):
|
2017-06-20 15:45:43 +00:00
|
|
|
"""
|
2022-05-10 14:29:06 +00:00
|
|
|
The initial states should be applied to the agent and the
|
|
|
|
agent should be able to update its state."""
|
2017-06-20 15:45:43 +00:00
|
|
|
config = {
|
2022-09-13 16:16:31 +00:00
|
|
|
'version': '2',
|
2022-10-06 13:49:10 +00:00
|
|
|
'name': 'CounterAgent',
|
|
|
|
'dry_run': True,
|
|
|
|
'num_trials': 1,
|
|
|
|
'max_time': 2,
|
|
|
|
'model_params': {
|
|
|
|
'topologies': {
|
|
|
|
'default': {
|
|
|
|
'path': join(ROOT, 'test.gexf')
|
|
|
|
}
|
2022-09-13 16:16:31 +00:00
|
|
|
},
|
2022-10-06 13:49:10 +00:00
|
|
|
'agents': {
|
|
|
|
'default': {
|
|
|
|
'agent_class': 'CounterModel',
|
|
|
|
},
|
|
|
|
'counters': {
|
|
|
|
'topology': 'default',
|
|
|
|
'fixed': [{'state': {'times': 10}}, {'state': {'times': 20}}],
|
|
|
|
}
|
2022-09-13 16:16:31 +00:00
|
|
|
}
|
2017-06-20 15:45:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-13 16:16:31 +00:00
|
|
|
s = simulation.from_config(config)
|
|
|
|
env = s.get_env()
|
|
|
|
assert isinstance(env.agents[0], agents.CounterModel)
|
|
|
|
assert env.agents[0].topology == env.topologies['default']
|
|
|
|
assert env.agents[0]['times'] == 10
|
|
|
|
assert env.agents[0]['times'] == 10
|
|
|
|
env.step()
|
|
|
|
assert env.agents[0]['times'] == 11
|
|
|
|
assert env.agents[1]['times'] == 21
|
2017-06-20 15:45:43 +00:00
|
|
|
|
2022-10-06 13:49:10 +00:00
|
|
|
def test_init_and_count_agents(self):
|
|
|
|
"""Agents should be properly initialized and counting should filter them properly"""
|
|
|
|
#TODO: separate this test into two or more test cases
|
2017-06-20 15:45:43 +00:00
|
|
|
config = {
|
|
|
|
'max_time': 10,
|
2022-10-06 13:49:10 +00:00
|
|
|
'model_params': {
|
|
|
|
'agents': [(CustomAgent, {'weight': 1}),
|
|
|
|
(CustomAgent, {'weight': 3}),
|
|
|
|
],
|
|
|
|
'topologies': {
|
|
|
|
'default': {
|
|
|
|
'path': join(ROOT, 'test.gexf')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2017-06-20 15:45:43 +00:00
|
|
|
}
|
2022-10-06 13:49:10 +00:00
|
|
|
s = simulation.from_config(config)
|
2018-02-16 17:04:43 +00:00
|
|
|
env = s.run_simulation(dry_run=True)[0]
|
2022-10-06 13:49:10 +00:00
|
|
|
assert env.agents[0].weight == 1
|
|
|
|
assert env.count_agents() == 2
|
|
|
|
assert env.count_agents(weight=1) == 1
|
|
|
|
assert env.count_agents(weight=3) == 1
|
|
|
|
assert env.count_agents(agent_class=CustomAgent) == 2
|
|
|
|
|
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]
|
2022-10-06 13:49:10 +00:00
|
|
|
config['model_params']['network_params']['path'] = join(EXAMPLES,
|
2017-06-20 15:45:43 +00:00
|
|
|
config['network_params']['path'])
|
2022-10-06 13:49:10 +00:00
|
|
|
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
|
|
|
|
|
2018-12-04 08:54:29 +00:00
|
|
|
def test_serialize_class(self):
|
2022-05-10 14:29:06 +00:00
|
|
|
ser, name = serialization.serialize(agents.BaseAgent, known_modules=[])
|
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
|
|
|
|
2022-05-10 14:29:06 +00:00
|
|
|
ser, name = serialization.serialize(agents.BaseAgent, known_modules=['soil', ])
|
|
|
|
assert name == 'BaseAgent'
|
|
|
|
assert ser == agents.BaseAgent
|
|
|
|
|
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
|
|
|
|
2022-10-06 13:49:10 +00:00
|
|
|
def test_serialize_agent_class(self):
|
2018-12-08 17:53:06 +00:00
|
|
|
'''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 = [
|
|
|
|
{
|
2022-10-06 13:49:10 +00:00
|
|
|
'agent_class': 'CounterModel',
|
2018-12-04 08:54:29 +00:00
|
|
|
'weight': 1
|
|
|
|
},
|
|
|
|
{
|
2022-10-06 13:49:10 +00:00
|
|
|
'agent_class': '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)
|
2022-10-06 13:49:10 +00:00
|
|
|
assert converted[0]['agent_class'] == agents.CounterModel
|
|
|
|
assert converted[1]['agent_class'] == 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 = [
|
|
|
|
{
|
2022-10-06 13:49:10 +00:00
|
|
|
'agent_class': agents.CounterModel,
|
2018-12-04 08:54:29 +00:00
|
|
|
'weight': 1
|
|
|
|
},
|
|
|
|
{
|
2022-10-06 13:49:10 +00:00
|
|
|
'agent_class': 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)
|
2022-10-06 13:49:10 +00:00
|
|
|
assert converted[0]['agent_class'] == 'CounterModel'
|
|
|
|
assert converted[1]['agent_class'] == 'test_main.CustomAgent'
|
2018-12-09 15:38:18 +00:00
|
|
|
pickle.dumps(converted)
|
|
|
|
|
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)
|
2022-10-06 13:49:10 +00:00
|
|
|
distro = agents.calculate_distribution(agent_class=agents.NetworkAgent)
|
2022-09-16 16:13:39 +00:00
|
|
|
distro[0]['topology'] = 'default'
|
|
|
|
aconfig = config.AgentConfig(distribution=distro, topology='default')
|
|
|
|
env = Environment(name='Test', topologies={'default': G}, agents={'network': aconfig})
|
2019-02-19 20:17:19 +00:00
|
|
|
lst = list(env.network_agents)
|
|
|
|
|
2022-09-16 16:13:39 +00:00
|
|
|
a2 = env.find_one(node_id=2)
|
|
|
|
a3 = env.find_one(node_id=3)
|
2019-02-19 20:17:19 +00:00
|
|
|
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
|
2022-10-06 13:49:10 +00:00
|
|
|
assert len(a3.subgraph(agent_class=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',
|
2022-10-06 13:49:10 +00:00
|
|
|
'model_params': {
|
|
|
|
'network_params': {},
|
|
|
|
'agent_class': 'CounterModel',
|
|
|
|
},
|
2020-10-19 11:14:48 +00:00
|
|
|
'max_time': 2,
|
2021-10-15 18:15:17 +00:00
|
|
|
'num_trials': 50,
|
2020-10-19 11:14:48 +00:00
|
|
|
}
|
2022-10-06 13:49:10 +00:00
|
|
|
s = simulation.from_config(config)
|
2020-10-19 11:14:48 +00:00
|
|
|
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)
|