reset to Genesis value between runs

This commit is contained in:
Joshua E. Jodesty 2018-11-21 13:45:00 -05:00
parent 7a28a9095a
commit d7c423b8be
7 changed files with 32 additions and 18 deletions

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -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:

View File

@ -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

View File

@ -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))

View File

@ -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)

View File

@ -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]

View File

@ -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))
configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms))