-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
param_sweep.py
113 lines (83 loc) · 2.78 KB
/
param_sweep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import pprint
import pandas as pd
from tabulate import tabulate
from cadCAD.configuration.utils import env_trigger, var_substep_trigger, config_sim, psub_list
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from cadCAD.configuration import Experiment
pp = pprint.PrettyPrinter(indent=4)
def some_function(x):
return x
g: Dict[str, List[int]] = {
'alpha': [1],
'beta': [2, 5],
'gamma': [3, 4],
'omega': [some_function]
}
psu_steps = ['1', '2', '3']
system_substeps = len(psu_steps)
var_timestep_trigger = var_substep_trigger([0, system_substeps])
env_timestep_trigger = env_trigger(system_substeps)
env_process = {}
# Policies
def gamma(_params, step, sH, s):
return {'gamma': _params['gamma']}
def omega(_params, step, sH, s):
return {'omega': _params['omega'](7)}
# Internal States
def alpha(_params, step, sH, s, _input):
return 'alpha', _params['alpha']
def alpha_plus_gamma(_params, step, sH, s, _input):
return 'alpha_plus_gamma', _params['alpha'] + _params['gamma']
def beta(_params, step, sH, s, _input):
return 'beta', _params['beta']
def policies(_params, step, sH, s, _input):
return 'policies', _input
def sweeped(_params, step, sH, s, _input):
return 'sweeped', {'beta': _params['beta'], 'gamma': _params['gamma']}
genesis_states = {
'alpha_plus_gamma': 0,
'alpha': 0,
'beta': 0,
'policies': {},
'sweeped': {}
}
env_process['sweeped'] = env_timestep_trigger(trigger_field='timestep', trigger_vals=[5], funct_list=[lambda _g, x: _g['beta']])
sim_config = config_sim(
{
"N": 2,
"T": range(5),
"M": g,
}
)
psu_block = {k: {"policies": {}, "variables": {}} for k in psu_steps}
for m in psu_steps:
psu_block[m]['policies']['gamma'] = gamma
psu_block[m]['policies']['omega'] = omega
psu_block[m]["variables"]['alpha'] = alpha_plus_gamma
psu_block[m]["variables"]['alpha_plus_gamma'] = alpha
psu_block[m]["variables"]['beta'] = beta
psu_block[m]['variables']['policies'] = policies
psu_block[m]["variables"]['sweeped'] = var_timestep_trigger(y='sweeped', f=sweeped)
psubs = psub_list(psu_block, psu_steps)
print()
pp.pprint(psu_block)
print()
exp = Experiment()
exp.append_model(
model_id='param_sweep',
sim_configs=sim_config,
initial_state=genesis_states,
env_processes=env_process,
partial_state_update_blocks=psubs
)
exec_mode = ExecutionMode()
local_proc_ctx = ExecutionContext(context=exec_mode.local_mode)
run = Executor(exec_context=local_proc_ctx, configs=exp.configs)
raw_result, tensor_field, sessions = run.execute()
result = pd.DataFrame(raw_result)
print()
print("Tensor Field:")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()