mirror of
https://github.com/gsi-upm/soil
synced 2025-09-18 22:22:20 +00:00
Pre-release version of v1.0
This commit is contained in:
@@ -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)
|
||||
|
Reference in New Issue
Block a user