Reafactor Pt. 4: Improved Runtime Env / ux Pt.2
This commit is contained in:
parent
d29658ecbe
commit
cee90b564c
|
|
@ -1,6 +1,6 @@
|
||||||
configs = []
|
configs = []
|
||||||
|
|
||||||
class Config(object):
|
class Configuration(object):
|
||||||
def __init__(self, sim_config, state_dict, seed, exogenous_states, env_processes, behavior_ops, mechanisms):
|
def __init__(self, sim_config, state_dict, seed, exogenous_states, env_processes, behavior_ops, mechanisms):
|
||||||
self.sim_config = sim_config
|
self.sim_config = sim_config
|
||||||
self.state_dict = state_dict
|
self.state_dict = state_dict
|
||||||
|
|
@ -1,13 +1,11 @@
|
||||||
|
|
||||||
|
|
||||||
from pathos.multiprocessing import ProcessingPool as Pool
|
from pathos.multiprocessing import ProcessingPool as Pool
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from tabulate import tabulate
|
from tabulate import tabulate
|
||||||
|
|
||||||
from utils import flatten
|
from SimCAD.utils import flatten
|
||||||
from utils.ui import create_tensor_field
|
from SimCAD.utils.ui import create_tensor_field
|
||||||
from utils.configProcessor import generate_config
|
from SimCAD.utils.configProcessor import generate_config
|
||||||
|
|
||||||
class ExecutionContext(object):
|
class ExecutionContext(object):
|
||||||
|
|
||||||
|
|
@ -25,7 +23,7 @@ class ExecutionContext(object):
|
||||||
class Executor(object):
|
class Executor(object):
|
||||||
|
|
||||||
def __init__(self, ExecutionContext, configs):
|
def __init__(self, ExecutionContext, configs):
|
||||||
from engine.simulation import Executor
|
from SimCAD.engine.simulation import Executor
|
||||||
|
|
||||||
def execute():
|
def execute():
|
||||||
ec = ExecutionContext()
|
ec = ExecutionContext()
|
||||||
|
|
@ -18,22 +18,3 @@ def last_index(l):
|
||||||
|
|
||||||
def retrieve_state(l, offset):
|
def retrieve_state(l, offset):
|
||||||
return l[last_index(l) + offset + 1]
|
return l[last_index(l) + offset + 1]
|
||||||
|
|
||||||
# def exo_proc_trigger(mech_step, update_f, y):
|
|
||||||
# if mech_step == 1:
|
|
||||||
# return update_f
|
|
||||||
# else:
|
|
||||||
# return lambda step, sL, s, _input: (y, s[y])
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# def apply_exo_proc(s, x, y):
|
|
||||||
# if s['mech_step'] == 1:
|
|
||||||
# return x
|
|
||||||
# else:
|
|
||||||
# return s[y]
|
|
||||||
|
|
||||||
# def es5p2(step, sL, s, _input): # accept timedelta instead of timedelta params
|
|
||||||
# y = 'timestamp'
|
|
||||||
# x = ep_time_step(s, s['timestamp'], seconds=1)
|
|
||||||
# return (y, x)
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from utils.configProcessor import create_matrix_field
|
from SimCAD.utils.configProcessor import create_matrix_field
|
||||||
|
|
||||||
def create_tensor_field(mechanisms, exo_proc, keys=['behaviors', 'states']):
|
def create_tensor_field(mechanisms, exo_proc, keys=['behaviors', 'states']):
|
||||||
dfs = [ create_matrix_field(mechanisms, k) for k in keys ]
|
dfs = [create_matrix_field(mechanisms, 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))):
|
||||||
df['es'+str(i+1)] = es
|
df['es'+str(i+1)] = es
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
|
|
||||||
from engine import ExecutionContext, Executor
|
|
||||||
from ui import config1, config2
|
|
||||||
|
|
||||||
configs = [config1, config2]
|
|
||||||
run = Executor(ExecutionContext, configs)
|
|
||||||
result = run.main()
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
from pathos.multiprocessing import ProcessingPool as Pool
|
|
||||||
|
|
||||||
|
|
||||||
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:
|
|
||||||
results = p.map(lambda t: t[0](t[1], t[2], t[3], t[4], t[5]), l)
|
|
||||||
|
|
||||||
return results
|
|
||||||
|
|
@ -1,50 +0,0 @@
|
||||||
import pandas as pd
|
|
||||||
from tabulate import tabulate
|
|
||||||
from configuration import configs
|
|
||||||
from utils import flatten
|
|
||||||
from utils.ui import create_tensor_field
|
|
||||||
from utils.configProcessor import generate_config
|
|
||||||
from engine.simulation import Executor
|
|
||||||
from runtime.multiproc import parallelize_simulations
|
|
||||||
|
|
||||||
# from ui import config1, config2
|
|
||||||
#
|
|
||||||
|
|
||||||
def main():
|
|
||||||
print(configs)
|
|
||||||
states_lists, Ts, Ns, eps, configs_struct, env_processes, mechanisms, simulation_execs = \
|
|
||||||
[], [], [], [], [], [], [], []
|
|
||||||
config_idx = 0
|
|
||||||
for x in configs:
|
|
||||||
|
|
||||||
states_lists.append([x.state_dict])
|
|
||||||
Ts.append(x.sim_config['T'])
|
|
||||||
Ns.append(x.sim_config['N'])
|
|
||||||
eps.append(list(x.exogenous_states.values()))
|
|
||||||
configs_struct.append(generate_config(x.state_dict, x.mechanisms, eps[config_idx]))
|
|
||||||
env_processes.append(x.env_processes)
|
|
||||||
mechanisms.append(x.mechanisms)
|
|
||||||
simulation_execs.append(Executor(x.behavior_ops).simulation)
|
|
||||||
|
|
||||||
config_idx += 1
|
|
||||||
|
|
||||||
# Dimensions: N x r x mechs
|
|
||||||
|
|
||||||
|
|
||||||
if len(configs) > 1:
|
|
||||||
simulations = parallelize_simulations(simulation_execs, states_lists, configs_struct, env_processes, Ts, Ns)
|
|
||||||
|
|
||||||
for result, mechanism, ep in list(zip(simulations, mechanisms, eps)):
|
|
||||||
print(tabulate(create_tensor_field(mechanism, ep), headers='keys', tablefmt='psql'))
|
|
||||||
print(tabulate(pd.DataFrame(flatten(result)), headers='keys', tablefmt='psql'))
|
|
||||||
else:
|
|
||||||
print('single note')
|
|
||||||
simulation, states_list, config = simulation_execs.pop(), states_lists.pop(), configs_struct.pop()
|
|
||||||
env_process = env_processes.pop()
|
|
||||||
# simulations = [simulation(states_list, config, env_processes, T, N)]
|
|
||||||
|
|
||||||
# behavior_ops, states_list, configs, env_processes, time_seq, runs
|
|
||||||
# result = simulation(states_list1, config1, conf1.env_processes, T, N)
|
|
||||||
# return pd.DataFrame(flatten(result))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
from configuration import Config, configs
|
|
||||||
from utils.configuration import *
|
|
||||||
from fn.op import foldr
|
|
||||||
import numpy as np
|
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
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, \
|
||||||
|
ep_time_step
|
||||||
|
|
||||||
seed = {
|
seed = {
|
||||||
'z': np.random.RandomState(1),
|
'z': np.random.RandomState(1),
|
||||||
|
|
@ -159,4 +161,4 @@ sim_config = {
|
||||||
"T": range(5)
|
"T": range(5)
|
||||||
}
|
}
|
||||||
|
|
||||||
configs.append(Config(sim_config, state_dict, seed, exogenous_states, env_processes, behavior_ops, mechanisms))
|
configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, behavior_ops, mechanisms))
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
from configuration import Config, configs
|
|
||||||
from utils.configuration import *
|
|
||||||
from fn.op import foldr
|
|
||||||
import numpy as np
|
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
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, \
|
||||||
|
ep_time_step
|
||||||
|
|
||||||
|
|
||||||
seed = {
|
seed = {
|
||||||
|
|
@ -161,4 +163,4 @@ sim_config = {
|
||||||
"T": range(5)
|
"T": range(5)
|
||||||
}
|
}
|
||||||
|
|
||||||
configs.append(Config(sim_config, state_dict, seed, exogenous_states, env_processes, behavior_ops, mechanisms))
|
configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, behavior_ops, mechanisms))
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
|
||||||
|
from SimCAD.engine import ExecutionContext, Executor
|
||||||
|
from sandboxUX import config1, config2
|
||||||
|
|
||||||
|
configs = [config1, config2]
|
||||||
|
run = Executor(ExecutionContext, configs)
|
||||||
|
result = run.main()
|
||||||
Loading…
Reference in New Issue