2018-05-04 08:01:49 +00:00
|
|
|
from unittest import TestCase
|
|
|
|
|
|
|
|
import os
|
|
|
|
import pandas as pd
|
|
|
|
import yaml
|
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
from os.path import join
|
|
|
|
from soil import simulation, analysis, agents
|
|
|
|
|
|
|
|
|
|
|
|
ROOT = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
|
|
|
|
|
|
|
class Ping(agents.FSM):
|
|
|
|
|
|
|
|
defaults = {
|
|
|
|
'count': 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
@agents.default_state
|
|
|
|
@agents.state
|
|
|
|
def even(self):
|
2021-10-14 15:37:06 +00:00
|
|
|
self.debug(f'Even {self["count"]}')
|
2018-05-04 08:01:49 +00:00
|
|
|
self['count'] += 1
|
|
|
|
return self.odd
|
|
|
|
|
|
|
|
@agents.state
|
|
|
|
def odd(self):
|
2021-10-14 15:37:06 +00:00
|
|
|
self.debug(f'Odd {self["count"]}')
|
2018-05-04 08:01:49 +00:00
|
|
|
self['count'] += 1
|
|
|
|
return self.even
|
|
|
|
|
|
|
|
|
|
|
|
class TestAnalysis(TestCase):
|
|
|
|
|
|
|
|
# Code to generate a simple sqlite history
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
The initial states should be applied to the agent and the
|
|
|
|
agent should be able to update its state."""
|
|
|
|
config = {
|
|
|
|
'name': 'analysis',
|
|
|
|
'seed': 'seed',
|
|
|
|
'network_params': {
|
|
|
|
'generator': 'complete_graph',
|
|
|
|
'n': 2
|
|
|
|
},
|
|
|
|
'agent_type': Ping,
|
|
|
|
'states': [{'interval': 1}, {'interval': 2}],
|
|
|
|
'max_time': 30,
|
|
|
|
'num_trials': 1,
|
|
|
|
'environment_params': {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = simulation.from_config(config)
|
2019-04-29 16:47:15 +00:00
|
|
|
self.env = s.run_simulation(dry_run=True)[0]
|
2018-05-04 08:01:49 +00:00
|
|
|
|
|
|
|
def test_saved(self):
|
|
|
|
env = self.env
|
|
|
|
assert env.get_agent(0)['count', 0] == 1
|
|
|
|
assert env.get_agent(0)['count', 29] == 30
|
|
|
|
assert env.get_agent(1)['count', 0] == 1
|
|
|
|
assert env.get_agent(1)['count', 29] == 15
|
|
|
|
assert env['env', 29, None]['SEED'] == env['env', 29, 'SEED']
|
|
|
|
|
|
|
|
def test_count(self):
|
|
|
|
env = self.env
|
2019-04-29 16:47:15 +00:00
|
|
|
df = analysis.read_sql(env._history.db_path)
|
2021-10-15 18:15:17 +00:00
|
|
|
res = analysis.get_count(df, 'SEED', 'state_id')
|
2020-10-19 11:14:48 +00:00
|
|
|
assert res['SEED'][self.env['SEED']].iloc[0] == 1
|
|
|
|
assert res['SEED'][self.env['SEED']].iloc[-1] == 1
|
2021-10-15 18:15:17 +00:00
|
|
|
assert res['state_id']['odd'].iloc[0] == 2
|
|
|
|
assert res['state_id']['even'].iloc[0] == 0
|
|
|
|
assert res['state_id']['odd'].iloc[-1] == 1
|
|
|
|
assert res['state_id']['even'].iloc[-1] == 1
|
2018-05-04 08:01:49 +00:00
|
|
|
|
|
|
|
def test_value(self):
|
|
|
|
env = self.env
|
2020-10-19 11:14:48 +00:00
|
|
|
df = analysis.read_sql(env._history.db_path)
|
2018-05-04 08:01:49 +00:00
|
|
|
res_sum = analysis.get_value(df, 'count')
|
|
|
|
|
|
|
|
assert res_sum['count'].iloc[0] == 2
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
res_mean = analysis.get_value(df, 'count', aggfunc=np.mean)
|
2021-10-14 15:37:06 +00:00
|
|
|
assert res_mean['count'].iloc[15] == (16+8)/2
|
2018-05-04 08:01:49 +00:00
|
|
|
|
2021-10-14 15:37:06 +00:00
|
|
|
res_total = analysis.get_majority(df)
|
2020-10-19 11:14:48 +00:00
|
|
|
res_total['SEED'].iloc[0] == self.env['SEED']
|