1
0
mirror of https://github.com/gsi-upm/soil synced 2025-08-23 19:52:19 +00:00

Make py3 compatibility explicit

This commit is contained in:
J. Fernando Sánchez
2017-10-09 11:38:04 +02:00
parent e8988015e2
commit eafecc9e5e
8 changed files with 51 additions and 726 deletions

View File

@@ -2,7 +2,7 @@ import importlib
import sys
import os
__version__ = "0.9.4"
__version__ = "0.9.6"
try:
basestring

32
soil/__main__.py Normal file
View File

@@ -0,0 +1,32 @@
import importlib
import sys
import argparse
from . import simulation
def main():
parser = argparse.ArgumentParser(description='Run a SOIL simulation')
parser.add_argument('file', type=str,
nargs="?",
default='simulation.yml',
help='python module containing the simulation configuration.')
parser.add_argument('--module', '-m', type=str,
help='file containing the code of any custom agents.')
parser.add_argument('--dry-run', '--dry', action='store_true',
help='Do not store the results of the simulation.')
parser.add_argument('--output', '-o', type=str,
help='folder to write results to. It defaults to the current directory.')
args = parser.parse_args()
if args.module:
sys.path.append(os.getcwd())
importlib.import_module(args.module)
print('Loading config file: {}'.format(args.file, args.output))
simulation.run_from_config(args.file, dump=not args.dry_run, results_dir=args.output)
if __name__ == '__main__':
main()

View File

@@ -1,123 +0,0 @@
import nxsim
from collections import OrderedDict
from copy import deepcopy
import json
from functools import wraps
class BaseAgent(nxsim.BaseAgent):
"""
A special simpy BaseAgent that keeps track of its state history.
"""
def __init__(self, *args, **kwargs):
self._history = OrderedDict()
self._neighbors = None
super().__init__(*args, **kwargs)
self._history[None] = deepcopy(self.state)
@property
def now(self):
try:
return self.env.now
except AttributeError:
# No environment
return None
def run(self):
while True:
res = self.step()
self._history[self.env.now] = deepcopy(self.state)
yield res or self.env.timeout(self.env.interval)
def step(self):
pass
def to_json(self):
return json.dumps(self._history)
class NetworkAgent(BaseAgent, nxsim.BaseNetworkAgent):
def count_agents(self, state_id=None, limit_neighbors=False):
if limit_neighbors:
agents = self.global_topology.neighbors(self.id)
else:
agents = self.global_topology.nodes()
count = 0
for agent in agents:
if state_id and state_id != self.global_topology.node[agent]['agent'].state['id']:
continue
count += 1
return count
def count_neighboring_agents(self, state_id=None):
return self.count_agents(state_id, limit_neighbors=True)
def state(func):
@wraps(func)
def func_wrapper(self):
when = None
next_state = func(self)
try:
next_state, when = next_state
except TypeError:
pass
if next_state:
try:
self.state['id'] = next_state.id
except AttributeError:
raise NotImplemented('State id %s is not valid.' % next_state)
return when
func_wrapper.id = func.__name__
func_wrapper.is_default = False
return func_wrapper
def default_state(func):
func.is_default = True
return func
class MetaFSM(type):
def __init__(cls, name, bases, nmspc):
super(MetaFSM, cls).__init__(name, bases, nmspc)
states = {}
# Re-use states from inherited classes
default_state = None
for i in bases:
if isinstance(i, MetaFSM):
for state_id, state in i.states.items():
if state.is_default:
default_state = state
states[state_id] = state
# Add new states
for name, func in nmspc.items():
if hasattr(func, 'id'):
if func.is_default:
default_state = func
states[func.id] = func
cls.default_state = default_state
cls.states = states
class FSM(BaseAgent, metaclass=MetaFSM):
def __init__(self, *args, **kwargs):
super(FSM, self).__init__(*args, **kwargs)
if 'id' not in self.state:
self.state['id'] = self.default_state.id
def step(self):
if 'id' in self.state:
next_state = self.state['id']
elif self.default_state:
next_state = self.default_state.id
else:
raise Exception('{} has no valid state id or default state'.format(self))
if next_state not in self.states:
raise Exception('{} is not a valid id for {}'.format(next_state, self))
self.states[next_state](self)

View File

@@ -1,9 +1,9 @@
import random
import numpy as np
from . import FSM, state
from . import FSM, NetworkAgent, state
class SISaModel(FSM):
class SISaModel(FSM, NetworkAgent):
"""
Settings:
neutral_discontent_spon_prob