From d7c423b8bef2fc1305414a98d74ae5a3affd5f0e Mon Sep 17 00:00:00 2001 From: "Joshua E. Jodesty" Date: Wed, 21 Nov 2018 13:45:00 -0500 Subject: [PATCH] reset to Genesis value between runs --- .idea/vcs.xml | 6 ------ SimCAD/engine/__init__.py | 7 +++++-- SimCAD/engine/simulation.py | 7 ++++--- SimCAD/utils/__init__.py | 1 + SimCAD/utils/configuration.py | 14 +++++++++++++- SimCAD/utils/engine.py | 3 +++ sandboxUX/config1.py | 12 ++++++------ 7 files changed, 32 insertions(+), 18 deletions(-) delete mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/SimCAD/engine/__init__.py b/SimCAD/engine/__init__.py index d534bca..2f61691 100644 --- a/SimCAD/engine/__init__.py +++ b/SimCAD/engine/__init__.py @@ -6,16 +6,19 @@ from SimCAD.utils.ui import create_tensor_field from SimCAD.utils.configProcessor import generate_config from SimCAD.engine.simulation import Executor as SimExecutor -class ExecutionMode(object): + +class ExecutionMode: single_proc = 'single_proc' multi_proc = 'multi_proc' -class ExecutionContext(object): + +class ExecutionContext: def __init__(self, context=ExecutionMode.multi_proc): self.name = context self.method = None + def parallelize_simulations(fs, states_list, configs, env_processes, Ts, Ns): l = list(zip(fs, states_list, configs, env_processes, Ts, Ns)) with Pool(len(configs)) as p: diff --git a/SimCAD/engine/simulation.py b/SimCAD/engine/simulation.py index f798096..317a87b 100644 --- a/SimCAD/engine/simulation.py +++ b/SimCAD/engine/simulation.py @@ -1,5 +1,4 @@ from copy import deepcopy -from fn import _ from fn.op import foldr, call @@ -7,6 +6,7 @@ class Executor: def __init__(self, behavior_ops): self.behavior_ops = behavior_ops + # Data Type reduction def getBehaviorInput(self, step, sL, s, funcs): @@ -16,11 +16,13 @@ class Executor: return foldr(call, getColResults(step, sL, s, funcs))(ops) + def apply_env_proc(self, env_processes, state_dict, step): for state in state_dict.keys(): if state in list(env_processes.keys()): state_dict[state] = env_processes[state](step)(state_dict[state]) + # remove / modify def exception_handler(self, f, m_step, sL, last_mut_obj, _input): try: @@ -98,8 +100,7 @@ class Executor: simulation_list = [head] + tail pipe_run += simulation_list else: - transient_states_list = [pipe_run[-1][-1]] - _, *tail = self.pipe(transient_states_list, configs, env_processes, time_seq, run) + _, *tail = self.pipe(states_list, configs, env_processes, time_seq, run) pipe_run += tail return pipe_run \ No newline at end of file diff --git a/SimCAD/utils/__init__.py b/SimCAD/utils/__init__.py index 71ebbdd..60a491b 100644 --- a/SimCAD/utils/__init__.py +++ b/SimCAD/utils/__init__.py @@ -1,4 +1,5 @@ flatten = lambda l: [item for sublist in l for item in sublist] + def flatmap(f, items): return list(map(f, items)) \ No newline at end of file diff --git a/SimCAD/utils/configuration.py b/SimCAD/utils/configuration.py index fba3af6..a134fd4 100644 --- a/SimCAD/utils/configuration.py +++ b/SimCAD/utils/configuration.py @@ -1,9 +1,9 @@ from datetime import datetime, timedelta from decimal import Decimal -from fn import _ from fn.func import curried from fn.op import foldr + def bound_norm_random(rng, low, high): # Add RNG Seed res = rng.normal((high+low)/2,(high-low)/6) @@ -11,6 +11,7 @@ def bound_norm_random(rng, low, high): res = bound_norm_random(rng, low, high) return Decimal(res) + @curried def proc_trigger(trigger_step, update_f, step): if step == trigger_step: @@ -18,12 +19,14 @@ def proc_trigger(trigger_step, update_f, step): else: return lambda x: x + # accept timedelta instead of timedelta params def time_step(dt_str, dt_format='%Y-%m-%d %H:%M:%S', days=0, minutes=0, seconds=30): dt = datetime.strptime(dt_str, dt_format) t = dt + timedelta(days=days, minutes=minutes, seconds=seconds) return t.strftime(dt_format) + # accept timedelta instead of timedelta params def ep_time_step(s, dt_str, fromat_str='%Y-%m-%d %H:%M:%S', days=0, minutes=0, seconds=1): if s['mech_step'] == 0: @@ -31,6 +34,7 @@ def ep_time_step(s, dt_str, fromat_str='%Y-%m-%d %H:%M:%S', days=0, minutes=0, s else: return dt_str + def exo_update_per_ts(ep): @curried def ep_decorator(f, y, step, sL, s, _input): @@ -40,10 +44,12 @@ def exo_update_per_ts(ep): return (y, s[y]) return {es: ep_decorator(f, es) for es, f in ep.items()} + def print_fwd(x): print(x) return x + def get_base_value(datatype): if datatype is str: return '' @@ -53,18 +59,23 @@ def get_base_value(datatype): return [] return 0 + def behavior_to_dict(v): return dict(list(zip(map(lambda n: 'b' + str(n + 1), list(range(len(v)))), v))) + add = lambda a, b: a + b + @curried def foldr_dict_vals(f, d): return foldr(f)(list(d.values())) + def sum_dict_values(): return foldr_dict_vals(add) + @curried def dict_op(f, d1, d2): @@ -78,5 +89,6 @@ def dict_op(f, d1, d2): return {k: f(set_base_value(d1, d2, k), set_base_value(d2, d1, k)) for k in key_set} + def dict_elemwise_sum(): return dict_op(add) \ No newline at end of file diff --git a/SimCAD/utils/engine.py b/SimCAD/utils/engine.py index 4d68ab2..ad7cae0 100644 --- a/SimCAD/utils/engine.py +++ b/SimCAD/utils/engine.py @@ -1,5 +1,6 @@ from datetime import datetime + def datetime_range(start, end, delta, dt_format='%Y-%m-%d %H:%M:%S'): reverse_head = end [start, end] = [datetime.strptime(x, dt_format) for x in [start, end]] @@ -13,8 +14,10 @@ def datetime_range(start, end, delta, dt_format='%Y-%m-%d %H:%M:%S'): reverse_tail = [dt.strftime(dt_format) for dt in _datetime_range(start, end, delta)] return reverse_tail + [reverse_head] + def last_index(l): return len(l)-1 + def retrieve_state(l, offset): return l[last_index(l) + offset + 1] \ No newline at end of file diff --git a/sandboxUX/config1.py b/sandboxUX/config1.py index c3a8e8b..a0ba9df 100644 --- a/sandboxUX/config1.py +++ b/sandboxUX/config1.py @@ -3,7 +3,7 @@ import numpy as np from fn.op import foldr from SimCAD import Configuration, configs -from SimCAD.utils.configuration import exo_update_per_ts, proc_trigger, dict_elemwise_sum, bound_norm_random, \ +from SimCAD.utils.configuration import exo_update_per_ts, proc_trigger, bound_norm_random, \ ep_time_step seed = { @@ -30,15 +30,15 @@ def b1m3(step, sL, s): def b2m3(step, sL, s): return {'param1': ['d'], 'param2': np.array([20, 200])} - +# deff not more than 2 # Internal States per Mechanism def s1m1(step, sL, s, _input): y = 's1' - x = _input['param1'] + x = _input['param1'] #+ [Coef1 x 5] return (y, x) def s2m1(step, sL, s, _input): y = 's2' - x = _input['param2'] + x = _input['param2'] #+ [Coef2 x 5] return (y, x) def s1m2(step, sL, s, _input): @@ -118,7 +118,7 @@ env_processes = { # [1, 2] = {'b1': ['a'], 'b2', [1]} = # behavior_ops = [ behavior_to_dict, print_fwd, sum_dict_values ] -behavior_ops = [foldr(dict_elemwise_sum())] +# behavior_ops = [foldr(dict_elemwise_sum())] # behavior_ops = [] # need at least 1 behaviour and 1 state function for the 1st mech with behaviors @@ -161,4 +161,4 @@ sim_config = { "T": range(5) } -configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms, behavior_ops)) \ No newline at end of file +configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms)) \ No newline at end of file