mirror of
https://github.com/gsi-upm/soil
synced 2025-08-23 19:52:19 +00:00
Pre-release version of v1.0
This commit is contained in:
@@ -54,8 +54,7 @@ class TestAgents(TestCase):
|
||||
class MyAgent(agents.FSM):
|
||||
run = 0
|
||||
|
||||
@agents.default_state
|
||||
@agents.state("original")
|
||||
@agents.state("original", default=True)
|
||||
def root(self):
|
||||
self.run += 1
|
||||
return self.other
|
||||
@@ -65,10 +64,11 @@ class TestAgents(TestCase):
|
||||
self.run += 1
|
||||
|
||||
e = environment.Environment()
|
||||
a = MyAgent(model=e, unique_id=e.next_id())
|
||||
a.step()
|
||||
a = e.add_agent(MyAgent)
|
||||
e.step()
|
||||
assert a.run == 1
|
||||
a.step()
|
||||
print("DONE")
|
||||
|
||||
def test_broadcast(self):
|
||||
"""
|
||||
|
@@ -28,13 +28,16 @@ class TestConfig(TestCase):
|
||||
|
||||
def test_torvalds_config(self):
|
||||
sim = simulation.from_config(os.path.join(ROOT, "test_config.yml"))
|
||||
assert sim.interval == 2
|
||||
MAX_STEPS = 10
|
||||
INTERVAL = 2
|
||||
assert sim.interval == INTERVAL
|
||||
assert sim.max_steps == MAX_STEPS
|
||||
envs = sim.run()
|
||||
assert len(envs) == 1
|
||||
env = envs[0]
|
||||
assert env.interval == 2
|
||||
assert env.count_agents() == 3
|
||||
assert env.now == 20
|
||||
assert env.now == INTERVAL * MAX_STEPS
|
||||
|
||||
|
||||
def make_example_test(path, cfg):
|
||||
@@ -42,10 +45,10 @@ def make_example_test(path, cfg):
|
||||
root = os.getcwd()
|
||||
print(path)
|
||||
s = simulation.from_config(cfg)
|
||||
iterations = s.max_time * s.num_trials
|
||||
iterations = s.max_time * s.iterations
|
||||
if iterations > 1000:
|
||||
s.max_time = 100
|
||||
s.num_trials = 1
|
||||
s.iterations = 1
|
||||
if cfg.skip_test and not FORCE_TESTS:
|
||||
self.skipTest('Example ignored.')
|
||||
envs = s.run_simulation(dump=False)
|
||||
@@ -53,7 +56,7 @@ def make_example_test(path, cfg):
|
||||
for env in envs:
|
||||
assert env
|
||||
try:
|
||||
n = cfg.model_params['topology']['params']['n']
|
||||
n = cfg.parameters['topology']['params']['n']
|
||||
assert len(list(env.network_agents)) == n
|
||||
assert env.now > 0 # It has run
|
||||
assert env.now <= cfg.max_time # But not further than allowed
|
||||
|
@@ -28,17 +28,22 @@ def get_test_for_sims(sims, path):
|
||||
if sim.skip_test and not FORCE_TESTS:
|
||||
continue
|
||||
run = True
|
||||
iterations = sim.max_steps * sim.num_trials
|
||||
|
||||
if sim.max_steps is None:
|
||||
sim.max_steps = 100
|
||||
|
||||
iterations = sim.max_steps * sim.iterations
|
||||
if iterations < 0 or iterations > 1000:
|
||||
sim.max_steps = 100
|
||||
sim.num_trials = 1
|
||||
envs = sim.run_simulation(dump=False)
|
||||
sim.iterations = 1
|
||||
|
||||
envs = sim.run(dump=False)
|
||||
assert envs
|
||||
for env in envs:
|
||||
assert env
|
||||
assert env.now > 0
|
||||
try:
|
||||
n = sim.model_params["network_params"]["n"]
|
||||
n = sim.parameters["network_params"]["n"]
|
||||
assert len(list(env.network_agents)) == n
|
||||
except KeyError:
|
||||
pass
|
||||
|
@@ -9,28 +9,29 @@ from soil import exporters
|
||||
from soil import environment
|
||||
from soil import simulation
|
||||
from soil import agents
|
||||
from soil import decorators
|
||||
|
||||
from mesa import Agent as MesaAgent
|
||||
|
||||
|
||||
class Dummy(exporters.Exporter):
|
||||
started = False
|
||||
trials = 0
|
||||
iterations = 0
|
||||
ended = False
|
||||
total_time = 0
|
||||
called_start = 0
|
||||
called_trial = 0
|
||||
called_iteration = 0
|
||||
called_end = 0
|
||||
|
||||
def sim_start(self):
|
||||
self.__class__.called_start += 1
|
||||
self.__class__.started = True
|
||||
|
||||
def trial_end(self, env):
|
||||
def iteration_end(self, env, *args, **kwargs):
|
||||
assert env
|
||||
self.__class__.trials += 1
|
||||
self.__class__.iterations += 1
|
||||
self.__class__.total_time += env.now
|
||||
self.__class__.called_trial += 1
|
||||
self.__class__.called_iteration += 1
|
||||
|
||||
def sim_end(self):
|
||||
self.__class__.ended = True
|
||||
@@ -44,77 +45,78 @@ class Exporters(TestCase):
|
||||
class SimpleEnv(environment.Environment):
|
||||
def init(self):
|
||||
self.add_agent(agent_class=MesaAgent)
|
||||
|
||||
|
||||
num_trials = 5
|
||||
iterations = 5
|
||||
max_time = 2
|
||||
s = simulation.Simulation(num_trials=num_trials, max_time=max_time, name="exporter_sim",
|
||||
dump=False, model=SimpleEnv)
|
||||
s = simulation.Simulation(iterations=iterations,
|
||||
max_time=max_time, name="exporter_sim",
|
||||
exporters=[Dummy], dump=False, model=SimpleEnv)
|
||||
|
||||
for env in s.run_simulation(exporters=[Dummy], dump=False):
|
||||
for env in s.run():
|
||||
assert len(env.agents) == 1
|
||||
|
||||
assert Dummy.started
|
||||
assert Dummy.ended
|
||||
assert Dummy.called_start == 1
|
||||
assert Dummy.called_end == 1
|
||||
assert Dummy.called_trial == num_trials
|
||||
assert Dummy.trials == num_trials
|
||||
assert Dummy.total_time == max_time * num_trials
|
||||
assert Dummy.called_iteration == iterations
|
||||
assert Dummy.iterations == iterations
|
||||
assert Dummy.total_time == max_time * iterations
|
||||
|
||||
def test_writing(self):
|
||||
"""Try to write CSV, sqlite and YAML (without no_dump)"""
|
||||
n_trials = 5
|
||||
n_iterations = 5
|
||||
n_nodes = 4
|
||||
max_time = 2
|
||||
config = {
|
||||
"name": "exporter_sim",
|
||||
"model_params": {
|
||||
"network_generator": "complete_graph",
|
||||
"network_params": {"n": n_nodes},
|
||||
"agent_class": "CounterModel",
|
||||
},
|
||||
"max_time": max_time,
|
||||
"num_trials": n_trials,
|
||||
"dump": True,
|
||||
}
|
||||
output = io.StringIO()
|
||||
s = simulation.from_config(config)
|
||||
tmpdir = tempfile.mkdtemp()
|
||||
envs = s.run_simulation(
|
||||
|
||||
class ConstantEnv(environment.Environment):
|
||||
@decorators.report
|
||||
@property
|
||||
def constant(self):
|
||||
return 1
|
||||
|
||||
s = simulation.Simulation(
|
||||
model=ConstantEnv,
|
||||
name="exporter_sim",
|
||||
exporters=[
|
||||
exporters.default,
|
||||
exporters.csv,
|
||||
],
|
||||
model_params={
|
||||
"agent_reporters": {"times": "times"},
|
||||
"model_reporters": {
|
||||
"constant": lambda x: 1,
|
||||
},
|
||||
},
|
||||
dump=True,
|
||||
outdir=tmpdir,
|
||||
exporter_params={"copy_to": output},
|
||||
parameters=dict(
|
||||
network_generator="complete_graph",
|
||||
network_params={"n": n_nodes},
|
||||
agent_class="CounterModel",
|
||||
agent_reporters={"times": "times"},
|
||||
),
|
||||
max_time=max_time,
|
||||
outdir=tmpdir,
|
||||
iterations=n_iterations,
|
||||
dump=True,
|
||||
)
|
||||
envs = s.run()
|
||||
result = output.getvalue()
|
||||
|
||||
simdir = os.path.join(tmpdir, s.group or "", s.name)
|
||||
with open(os.path.join(simdir, "{}.dumped.yml".format(s.name))) as f:
|
||||
with open(os.path.join(simdir, "{}.dumped.yml".format(s.id))) as f:
|
||||
result = f.read()
|
||||
assert result
|
||||
|
||||
try:
|
||||
for e in envs:
|
||||
dbpath = os.path.join(simdir, f"{s.name}.sqlite")
|
||||
db = sqlite3.connect(dbpath)
|
||||
cur = db.cursor()
|
||||
agent_entries = cur.execute("SELECT times FROM agents WHERE times > 0").fetchall()
|
||||
env_entries = cur.execute("SELECT constant from env WHERE constant == 1").fetchall()
|
||||
assert len(agent_entries) == n_nodes * n_trials * max_time
|
||||
assert len(env_entries) == n_trials * max_time
|
||||
dbpath = os.path.join(simdir, f"{s.name}.sqlite")
|
||||
db = sqlite3.connect(dbpath)
|
||||
cur = db.cursor()
|
||||
agent_entries = cur.execute("SELECT times FROM agents WHERE times > 0").fetchall()
|
||||
env_entries = cur.execute("SELECT constant from env WHERE constant == 1").fetchall()
|
||||
assert len(agent_entries) == n_nodes * n_iterations * max_time
|
||||
assert len(env_entries) == n_iterations * (max_time + 1) # +1 for the initial state
|
||||
|
||||
for e in envs:
|
||||
with open(os.path.join(simdir, "{}.env.csv".format(e.id))) as f:
|
||||
result = f.read()
|
||||
assert result
|
||||
|
||||
finally:
|
||||
shutil.rmtree(tmpdir)
|
||||
|
@@ -30,14 +30,14 @@ class TestMain(TestCase):
|
||||
def test_empty_simulation(self):
|
||||
"""A simulation with a base behaviour should do nothing"""
|
||||
config = {
|
||||
"model_params": {
|
||||
"parameters": {
|
||||
"topology": join(ROOT, "test.gexf"),
|
||||
"agent_class": MesaAgent,
|
||||
},
|
||||
"max_time": 1
|
||||
}
|
||||
s = simulation.from_config(config)
|
||||
s.run_simulation(dump=False)
|
||||
s.run(dump=False)
|
||||
|
||||
def test_network_agent(self):
|
||||
"""
|
||||
@@ -45,9 +45,9 @@ class TestMain(TestCase):
|
||||
agent should be able to update its state."""
|
||||
config = {
|
||||
"name": "CounterAgent",
|
||||
"num_trials": 1,
|
||||
"iterations": 1,
|
||||
"max_time": 2,
|
||||
"model_params": {
|
||||
"parameters": {
|
||||
"network_params": {
|
||||
"generator": nx.complete_graph,
|
||||
"n": 2,
|
||||
@@ -93,7 +93,7 @@ class TestMain(TestCase):
|
||||
try:
|
||||
os.chdir(os.path.dirname(pyfile))
|
||||
s = simulation.from_py(pyfile)
|
||||
env = s.run_simulation(dump=False)[0]
|
||||
env = s.run(dump=False)[0]
|
||||
for a in env.network_agents:
|
||||
skill_level = a["skill_level"]
|
||||
if a.node_id == "Torvalds":
|
||||
@@ -157,11 +157,11 @@ class TestMain(TestCase):
|
||||
n_trials = 50
|
||||
max_time = 2
|
||||
s = simulation.Simulation(
|
||||
model_params=dict(agents=dict(agent_classes=[CheckRun], k=1)),
|
||||
num_trials=n_trials,
|
||||
parameters=dict(agents=dict(agent_classes=[CheckRun], k=1)),
|
||||
iterations=n_trials,
|
||||
max_time=max_time,
|
||||
)
|
||||
runs = list(s.run_simulation(dump=False))
|
||||
runs = list(s.run(dump=False))
|
||||
over = list(x.now for x in runs if x.now > 2)
|
||||
assert len(runs) == n_trials
|
||||
assert len(over) == 0
|
||||
@@ -212,13 +212,24 @@ class TestMain(TestCase):
|
||||
for sim in sims:
|
||||
assert sim
|
||||
assert sim.name == "newspread_sim"
|
||||
assert sim.num_trials == 5
|
||||
assert sim.iterations == 5
|
||||
assert sim.max_steps == 300
|
||||
assert not sim.dump
|
||||
assert sim.model_params
|
||||
assert "ratio_dumb" in sim.model_params
|
||||
assert "ratio_herd" in sim.model_params
|
||||
assert "ratio_wise" in sim.model_params
|
||||
assert "network_generator" in sim.model_params
|
||||
assert "network_params" in sim.model_params
|
||||
assert "prob_neighbor_spread" in sim.model_params
|
||||
assert sim.parameters
|
||||
assert "ratio_dumb" in sim.parameters
|
||||
assert "ratio_herd" in sim.parameters
|
||||
assert "ratio_wise" in sim.parameters
|
||||
assert "network_generator" in sim.parameters
|
||||
assert "network_params" in sim.parameters
|
||||
assert "prob_neighbor_spread" in sim.parameters
|
||||
|
||||
def test_config_matrix(self):
|
||||
"""It should be possible to specify a matrix of parameters"""
|
||||
a = [1, 2]
|
||||
b = [3, 4]
|
||||
sim = simulation.Simulation(matrix=dict(a=a, b=b))
|
||||
configs = sim._collect_params()
|
||||
assert len(configs) == len(a) * len(b)
|
||||
for i in a:
|
||||
for j in b:
|
||||
assert {"a": i, "b": j} in configs
|
Reference in New Issue
Block a user