Merge branch 'param-sweep-multi-proc' of https://github.com/BlockScience/DiffyQ-SimCAD into param-sweep-multi-proc

This commit is contained in:
Joshua E. Jodesty 2019-02-18 12:22:50 -05:00
commit e00605c073
9 changed files with 405 additions and 1847 deletions

View File

@ -10,17 +10,17 @@ from cadCAD.configuration.utils import exo_update_per_ts
class Configuration(object): class Configuration(object):
def __init__(self, sim_config={}, initial_state={}, seeds={}, env_processes={}, def __init__(self, sim_config={}, initial_state={}, seeds={}, env_processes={},
exogenous_states={}, partial_state_updates={}, policy_ops=[foldr(dict_elemwise_sum())], **kwargs): exogenous_states={}, partial_state_update_blocks={}, policy_ops=[foldr(dict_elemwise_sum())], **kwargs):
self.sim_config = sim_config self.sim_config = sim_config
self.initial_state = initial_state self.initial_state = initial_state
self.seeds = seeds self.seeds = seeds
self.env_processes = env_processes self.env_processes = env_processes
self.exogenous_states = exogenous_states self.exogenous_states = exogenous_states
self.partial_state_updates = partial_state_updates self.partial_state_updates = partial_state_update_blocks
self.policy_ops = policy_ops self.policy_ops = policy_ops
def append_configs(sim_configs, initial_state, seeds, raw_exogenous_states, env_processes, partial_state_updates, _exo_update_per_ts=True): def append_configs(sim_configs, initial_state, seeds, raw_exogenous_states, env_processes, partial_state_update_blocks, _exo_update_per_ts=True):
if _exo_update_per_ts is True: if _exo_update_per_ts is True:
exogenous_states = exo_update_per_ts(raw_exogenous_states) exogenous_states = exo_update_per_ts(raw_exogenous_states)
else: else:
@ -35,7 +35,7 @@ def append_configs(sim_configs, initial_state, seeds, raw_exogenous_states, env_
seeds=seeds, seeds=seeds,
exogenous_states=exogenous_states, exogenous_states=exogenous_states,
env_processes=env_processes, env_processes=env_processes,
partial_state_updates=partial_state_updates partial_state_update_blocks=partial_state_update_blocks
) )
) )
elif isinstance(sim_configs, dict): elif isinstance(sim_configs, dict):
@ -46,7 +46,7 @@ def append_configs(sim_configs, initial_state, seeds, raw_exogenous_states, env_
seeds=seeds, seeds=seeds,
exogenous_states=exogenous_states, exogenous_states=exogenous_states,
env_processes=env_processes, env_processes=env_processes,
partial_state_updates=partial_state_updates partial_state_update_blocks=partial_state_update_blocks
) )
) )
@ -84,7 +84,7 @@ class Processor:
self.apply_identity_funcs = id.apply_identity_funcs self.apply_identity_funcs = id.apply_identity_funcs
def create_matrix_field(self, partial_state_updates, key): def create_matrix_field(self, partial_state_updates, key):
if key == 'states': if key == 'variables':
identity = self.state_identity identity = self.state_identity
elif key == 'policies': elif key == 'policies':
identity = self.policy_identity identity = self.policy_identity
@ -113,7 +113,7 @@ class Processor:
def only_ep_handler(state_dict): def only_ep_handler(state_dict):
sdf_functions = [ sdf_functions = [
lambda sub_step, sL, s, _input: (k, v) for k, v in zip(state_dict.keys(), state_dict.values()) lambda var_dict, sub_step, sL, s, _input: (k, v) for k, v in zip(state_dict.keys(), state_dict.values())
] ]
sdf_values = [sdf_functions] sdf_values = [sdf_functions]
bdf_values = [[self.p_identity] * len(sdf_values)] bdf_values = [[self.p_identity] * len(sdf_values)]
@ -121,7 +121,7 @@ class Processor:
if len(partial_state_updates) != 0: if len(partial_state_updates) != 0:
bdf = self.create_matrix_field(partial_state_updates, 'policies') bdf = self.create_matrix_field(partial_state_updates, 'policies')
sdf = self.create_matrix_field(partial_state_updates, 'states') sdf = self.create_matrix_field(partial_state_updates, 'variables')
sdf_values, bdf_values = no_update_handler(bdf, sdf) sdf_values, bdf_values = no_update_handler(bdf, sdf)
zipped_list = list(zip(sdf_values, bdf_values)) zipped_list = list(zip(sdf_values, bdf_values))
else: else:

View File

@ -11,7 +11,7 @@ class TensorFieldReport:
def __init__(self, config_proc): def __init__(self, config_proc):
self.config_proc = config_proc self.config_proc = config_proc
def create_tensor_field(self, partial_state_updates, exo_proc, keys=['policies', 'states']): def create_tensor_field(self, partial_state_updates, exo_proc, keys=['policies', 'variables']):
dfs = [self.config_proc.create_matrix_field(partial_state_updates, k) for k in keys] dfs = [self.config_proc.create_matrix_field(partial_state_updates, k) for k in keys]
df = pd.concat(dfs, axis=1) df = pd.concat(dfs, axis=1)
for es, i in zip(exo_proc, range(len(exo_proc))): for es, i in zip(exo_proc, range(len(exo_proc))):
@ -52,7 +52,7 @@ def time_step(dt_str, dt_format='%Y-%m-%d %H:%M:%S', _timedelta = tstep_delta):
ep_t_delta = timedelta(days=0, minutes=0, seconds=1) ep_t_delta = timedelta(days=0, minutes=0, seconds=1)
def ep_time_step(s, dt_str, fromat_str='%Y-%m-%d %H:%M:%S', _timedelta = ep_t_delta): def ep_time_step(s, dt_str, fromat_str='%Y-%m-%d %H:%M:%S', _timedelta = ep_t_delta):
if s['sub_step'] == 0: if s['substep'] == 0:
return time_step(dt_str, fromat_str, _timedelta) return time_step(dt_str, fromat_str, _timedelta)
else: else:
return dt_str return dt_str
@ -114,7 +114,7 @@ def sweep_states(state_type, states, in_config):
def exo_update_per_ts(ep): def exo_update_per_ts(ep):
@curried @curried
def ep_decorator(f, y, var_dict, sub_step, sL, s, _input): def ep_decorator(f, y, var_dict, sub_step, sL, s, _input):
if s['sub_step'] + 1 == 1: if s['substep'] + 1 == 1:
return f(var_dict, sub_step, sL, s, _input) return f(var_dict, sub_step, sL, s, _input)
else: else:
return y, s[y] return y, s[y]

View File

@ -51,7 +51,7 @@ class Executor:
self.apply_env_proc(env_processes, last_in_copy, last_in_copy['timestep']) # not time_step self.apply_env_proc(env_processes, last_in_copy, last_in_copy['timestep']) # not time_step
last_in_copy["sub_step"], last_in_copy["time_step"], last_in_copy['run'] = sub_step, time_step, run last_in_copy['substep'], last_in_copy['timestep'], last_in_copy['run'] = sub_step, time_step, run
sL.append(last_in_copy) sL.append(last_in_copy)
del last_in_copy del last_in_copy
@ -62,7 +62,7 @@ class Executor:
sub_step = 0 sub_step = 0
states_list_copy = deepcopy(states_list) states_list_copy = deepcopy(states_list)
genesis_states = states_list_copy[-1] genesis_states = states_list_copy[-1]
genesis_states['sub_step'], genesis_states['time_step'] = sub_step, time_step genesis_states['substep'], genesis_states['timestep'] = sub_step, time_step
states_list = [genesis_states] states_list = [genesis_states]
sub_step += 1 sub_step += 1
@ -93,7 +93,7 @@ class Executor:
states_list_copy = deepcopy(states_list) states_list_copy = deepcopy(states_list)
head, *tail = self.run_pipeline(var_dict, states_list_copy, configs, env_processes, time_seq, run) head, *tail = self.run_pipeline(var_dict, states_list_copy, configs, env_processes, time_seq, run)
genesis = head.pop() genesis = head.pop()
genesis['sub_step'], genesis['time_step'], genesis['run'] = 0, 0, run genesis['substep'], genesis['timestep'], genesis['run'] = 0, 0, run
first_timestep_per_run = [genesis] + tail.pop(0) first_timestep_per_run = [genesis] + tail.pop(0)
pipe_run += [first_timestep_per_run] + tail pipe_run += [first_timestep_per_run] + tail
del states_list_copy del states_list_copy

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@ -98,20 +98,20 @@ genesis_states = {
's2': Decimal(0.0), 's2': Decimal(0.0),
's3': Decimal(1.0), 's3': Decimal(1.0),
's4': Decimal(1.0), 's4': Decimal(1.0),
'timestep': '2018-10-01 15:16:24' # 'timestep': '2018-10-01 15:16:24'
} }
raw_exogenous_states = { raw_exogenous_states = {
"s3": es3p1, "s3": es3p1,
"s4": es4p2, "s4": es4p2,
"timestep": es5p2 # "timestep": es5p2
} }
env_processes = { env_processes = {
"s3": env_a, "s3": env_a,
"s4": proc_trigger('2018-10-01 15:16:25', env_b) "s4": proc_trigger(1, env_b)
} }
@ -121,7 +121,7 @@ partial_state_update_block = {
"b1": p1m1, "b1": p1m1,
"b2": p2m1 "b2": p2m1
}, },
"states": { "variables": {
"s1": s1m1, "s1": s1m1,
"s2": s2m1 "s2": s2m1
} }
@ -131,7 +131,7 @@ partial_state_update_block = {
"b1": p1m2, "b1": p1m2,
"b2": p2m2 "b2": p2m2
}, },
"states": { "variables": {
"s1": s1m2, "s1": s1m2,
"s2": s2m2 "s2": s2m2
} }
@ -141,7 +141,7 @@ partial_state_update_block = {
"b1": p1m3, "b1": p1m3,
"b2": p2m3 "b2": p2m3
}, },
"states": { "variables": {
"s1": s1m3, "s1": s1m3,
"s2": s2m3 "s2": s2m3
} }
@ -163,5 +163,5 @@ append_configs(
seeds=seeds, seeds=seeds,
raw_exogenous_states=raw_exogenous_states, raw_exogenous_states=raw_exogenous_states,
env_processes=env_processes, env_processes=env_processes,
partial_state_updates=partial_state_update_block partial_state_update_blocks=partial_state_update_block
) )

View File

@ -97,20 +97,20 @@ genesis_states = {
's2': Decimal(0.0), 's2': Decimal(0.0),
's3': Decimal(1.0), 's3': Decimal(1.0),
's4': Decimal(1.0), 's4': Decimal(1.0),
'timestep': '2018-10-01 15:16:24' # 'timestep': '2018-10-01 15:16:24'
} }
raw_exogenous_states = { raw_exogenous_states = {
"s3": es3p1, "s3": es3p1,
"s4": es4p2, "s4": es4p2,
"timestep": es5p2 # "timestep": es5p2
} }
env_processes = { env_processes = {
"s3": proc_trigger('2018-10-01 15:16:25', env_a), "s3": proc_trigger(1, env_a),
"s4": proc_trigger('2018-10-01 15:16:25', env_b) "s4": proc_trigger(1, env_b)
} }
@ -120,7 +120,7 @@ partial_state_update_block = {
"b1": p1m1, "b1": p1m1,
# "b2": p2m1 # "b2": p2m1
}, },
"states": { "variables": {
"s1": s1m1, "s1": s1m1,
# "s2": s2m1 # "s2": s2m1
} }
@ -130,7 +130,7 @@ partial_state_update_block = {
"b1": p1m2, "b1": p1m2,
# "b2": p2m2 # "b2": p2m2
}, },
"states": { "variables": {
"s1": s1m2, "s1": s1m2,
# "s2": s2m2 # "s2": s2m2
} }
@ -140,7 +140,7 @@ partial_state_update_block = {
"b1": p1m3, "b1": p1m3,
"b2": p2m3 "b2": p2m3
}, },
"states": { "variables": {
"s1": s1m3, "s1": s1m3,
"s2": s2m3 "s2": s2m3
} }
@ -162,5 +162,5 @@ append_configs(
seeds=seeds, seeds=seeds,
raw_exogenous_states=raw_exogenous_states, raw_exogenous_states=raw_exogenous_states,
env_processes=env_processes, env_processes=env_processes,
partial_state_updates=partial_state_update_block partial_state_update_blocks=partial_state_update_block
) )

View File

@ -0,0 +1,142 @@
from decimal import Decimal
import numpy as np
from datetime import timedelta
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import proc_trigger, bound_norm_random, ep_time_step
from cadCAD.configuration.utils.parameterSweep import config_sim
seeds = {
'z': np.random.RandomState(1),
'a': np.random.RandomState(2),
'b': np.random.RandomState(3),
'c': np.random.RandomState(3)
}
# Policies per Mechanism
def p1m1(_g, step, sL, s):
return {'param1': 1}
def p2m1(_g, step, sL, s):
return {'param2': 4}
def p1m2(_g, step, sL, s):
return {'param1': 'a', 'param2': 2}
def p2m2(_g, step, sL, s):
return {'param1': 'b', 'param2': 4}
def p1m3(_g, step, sL, s):
return {'param1': ['c'], 'param2': np.array([10, 100])}
def p2m3(_g, step, sL, s):
return {'param1': ['d'], 'param2': np.array([20, 200])}
# Internal States per Mechanism
def s1m1(_g, step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m1(_g, step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m2(_g, step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m2(_g, step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m3(_g, step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m3(_g, step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m4(_g, step, sL, s, _input):
y = 's1'
x = [1]
return (y, x)
# Exogenous States
proc_one_coef_A = 0.7
proc_one_coef_B = 1.3
def es3p1(_g, step, sL, s, _input):
y = 's3'
x = s['s3'] * bound_norm_random(seeds['a'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
def es4p2(_g, step, sL, s, _input):
y = 's4'
x = s['s4'] * bound_norm_random(seeds['b'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
ts_format = '%Y-%m-%d %H:%M:%S'
t_delta = timedelta(days=0, minutes=0, seconds=1)
def es5p2(_g, step, sL, s, _input):
y = 'timestamp'
x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta)
return (y, x)
# Environment States
def env_a(x):
return 5
def env_b(x):
return 10
# def what_ever(x):
# return x + 1
# Genesis States
genesis_states = {
's1': Decimal(0.0),
's2': Decimal(0.0),
's3': Decimal(1.0),
's4': Decimal(1.0),
'timestamp': '2018-10-01 15:16:24'
}
raw_exogenous_states = {
"s3": es3p1,
"s4": es4p2,
"timestamp": es5p2
}
env_processes = {
"s3": env_a,
"s4": proc_trigger('2018-10-01 15:16:25', env_b)
}
partial_state_update_block = [
]
sim_config = config_sim(
{
"N": 2,
"T": range(5),
}
)
append_configs(
sim_configs=sim_config,
initial_state=genesis_states,
seeds={},
raw_exogenous_states={},
env_processes={},
partial_state_update_blocks=partial_state_update_block
)

View File

@ -114,7 +114,7 @@ genesis_states = {
's2': Decimal(0.0), 's2': Decimal(0.0),
's3': Decimal(1.0), 's3': Decimal(1.0),
's4': Decimal(1.0), 's4': Decimal(1.0),
'timestep': '2018-10-01 15:16:24' # 'timestep': '2018-10-01 15:16:24'
} }
@ -122,13 +122,13 @@ genesis_states = {
raw_exogenous_states = { raw_exogenous_states = {
"s3": es3p1, "s3": es3p1,
"s4": es4p2, "s4": es4p2,
"timestep": es5p2 # "timestep": es5p2
} }
# ToDo: make env proc trigger field agnostic # ToDo: make env proc trigger field agnostic
# ToDo: input json into function renaming __name__ # ToDo: input json into function renaming __name__
triggered_env_b = proc_trigger('2018-10-01 15:16:25', env_b) triggered_env_b = proc_trigger(1, env_b)
env_processes = { env_processes = {
"s3": env_a, #sweep(beta, env_a), "s3": env_a, #sweep(beta, env_a),
"s4": triggered_env_b #rename('parameterized', triggered_env_b) #sweep(beta, triggered_env_b) "s4": triggered_env_b #rename('parameterized', triggered_env_b) #sweep(beta, triggered_env_b)
@ -149,7 +149,7 @@ partial_state_update_block = {
"b1": p1m1, "b1": p1m1,
"b2": p2m1 "b2": p2m1
}, },
"states": { "variables": {
"s1": s1m1, "s1": s1m1,
"s2": s2m1 "s2": s2m1
} }
@ -159,7 +159,7 @@ partial_state_update_block = {
"b1": p1m2, "b1": p1m2,
"b2": p2m2, "b2": p2m2,
}, },
"states": { "variables": {
"s1": s1m2, "s1": s1m2,
"s2": s2m2 "s2": s2m2
} }
@ -169,7 +169,7 @@ partial_state_update_block = {
"b1": p1m3, "b1": p1m3,
"b2": p2m3 "b2": p2m3
}, },
"states": { "variables": {
"s1": s1m3, "s1": s1m3,
"s2": s2m3 "s2": s2m3
} }
@ -192,5 +192,5 @@ append_configs(
seeds=seeds, seeds=seeds,
raw_exogenous_states=raw_exogenous_states, raw_exogenous_states=raw_exogenous_states,
env_processes=env_processes, env_processes=env_processes,
partial_state_updates=partial_state_update_block partial_state_update_blocks=partial_state_update_block
) )