can accept a mechanism config with at least a single state

This commit is contained in:
Joshua E. Jodesty 2018-11-06 09:22:21 -05:00
parent f2bf23cc37
commit 1d820486ae
3 changed files with 50 additions and 34 deletions

View File

@ -2,14 +2,12 @@ import pandas as pd
from functools import partial, reduce from functools import partial, reduce
def state_identity(k): def state_identity(k):
def identity(step, sL, s, _input): return lambda step, sL, s, _input: (k, s[k])
return (k, s[k])
return identity
def b_identity(step, sL, s):
return 0
def behavior_identity(k): def behavior_identity(k):
def identity(step, sL, s): return b_identity
return 0
return identity
def key_filter(mechanisms, keyname): def key_filter(mechanisms, keyname):
return [ v[keyname] for k, v in mechanisms.items() ] return [ v[keyname] for k, v in mechanisms.items() ]
@ -27,10 +25,24 @@ def create_matrix_field(mechanisms, key):
identity = behavior_identity identity = behavior_identity
df = pd.DataFrame(key_filter(mechanisms, key)) df = pd.DataFrame(key_filter(mechanisms, key))
col_list = apply_identity_funcs(identity, df, list(df.columns)) col_list = apply_identity_funcs(identity, df, list(df.columns))
return reduce((lambda x, y: pd.concat([x, y], axis=1)), col_list) if len(col_list) != 0:
return reduce((lambda x, y: pd.concat([x, y], axis=1)), col_list)
else:
return pd.DataFrame({'empty' : []})
def generate_config(mechanisms, env_poc): def generate_config(mechanisms, env_poc):
bdf = create_matrix_field(mechanisms,'behaviors') def no_behavior_handler(bdf, sdf):
sdf = create_matrix_field(mechanisms,'states') if bdf.empty == False:
zipped_list = list(zip(sdf.values.tolist(), bdf.values.tolist())) sdf_values, bdf_values = sdf.values.tolist(), bdf.values.tolist()
else:
sdf_values = sdf.values.tolist()
bdf_values = [[b_identity] * len(sdf_values)]
return sdf_values, bdf_values
bdf = create_matrix_field(mechanisms, 'behaviors')
sdf = create_matrix_field(mechanisms, 'states')
sdf_values, bdf_values = no_behavior_handler(bdf, sdf)
zipped_list = list(zip(sdf_values, bdf_values))
return list(map(lambda x: (x[0] + env_poc, x[1]), zipped_list)) return list(map(lambda x: (x[0] + env_poc, x[1]), zipped_list))

View File

@ -45,7 +45,11 @@ def mech_step(m_step, sL, state_funcs, behavior_funcs, env_processes, t_step):
def block_gen(states_list, configs, env_processes, t_step): def block_gen(states_list, configs, env_processes, t_step):
m_step = 0 m_step = 0
print("states_list")
print(states_list)
states_list_copy = deepcopy(states_list) states_list_copy = deepcopy(states_list)
print("states_list_copy")
print(states_list_copy)
genesis_states = states_list_copy[-1] genesis_states = states_list_copy[-1]
genesis_states['mech_step'], genesis_states['time_step'] = m_step, t_step genesis_states['mech_step'], genesis_states['time_step'] = m_step, t_step
states_list = [genesis_states] states_list = [genesis_states]
@ -54,7 +58,7 @@ def block_gen(states_list, configs, env_processes, t_step):
for config in configs: for config in configs:
s_conf, b_conf = config[0], config[1] s_conf, b_conf = config[0], config[1]
states_list = mech_step(m_step, states_list, s_conf, b_conf, env_processes, t_step) states_list = mech_step(m_step, states_list, s_conf, b_conf, env_processes, t_step)
print(b_conf) # print(b_conf)
m_step += 1 m_step += 1
t_step += 1 t_step += 1

View File

@ -105,34 +105,34 @@ env_processes = {
mechanisms = { mechanisms = {
"m1": { "m1": {
"behaviors": { "behaviors": {
"b1": b1m1, # lambda step, sL, s: s['s1'] + 1, # "b1": b1m1, # lambda step, sL, s: s['s1'] + 1,
"b2": b2m1 "b2": b2m1
}, },
"states": { # exclude only. TypeError: reduce() of empty sequence with no initial value "states": { # exclude only. TypeError: reduce() of empty sequence with no initial value
"s1": s1m1, # "s1": s1m1,
"s2": s2m1 # "s2": s2m1
} }
}, },
"m2": { # "m2": {
"behaviors": { # "behaviors": {
"b1": b1m2, # "b1": b1m2,
"b2": b2m2 # "b2": b2m2
}, # },
"states": { # "states": {
"s1": s1m2, # "s1": s1m2,
"s2": s2m2 # "s2": s2m2
} # }
}, # },
"m3": { # "m3": {
"behaviors": { # "behaviors": {
"b1": b1m3, # "b1": b1m3,
"b2": b2m3 #toggle for error # "b2": b2m3 #toggle for error
}, # },
"states": { # "states": {
"s1": s1m3, # "s1": s1m3,
"s2": s2m3 # "s2": s2m3
} # }
} # }
} }
sim_config = { sim_config = {