init packaging
This commit is contained in:
parent
596a75d7b7
commit
577ac24b96
|
|
@ -0,0 +1,13 @@
|
||||||
|
# if beh list empty, repeat 0 x n_states in list
|
||||||
|
def generate_config(mechanisms, exogenous_states):
|
||||||
|
es_funcs = [exogenous_states[state] for state in list(exogenous_states.keys())]
|
||||||
|
config = list(
|
||||||
|
map(
|
||||||
|
lambda m: (
|
||||||
|
list(mechanisms[m]["states"].values()) + es_funcs,
|
||||||
|
list(mechanisms[m]["behaviors"].values())
|
||||||
|
),
|
||||||
|
list(mechanisms.keys())
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return config
|
||||||
|
|
@ -0,0 +1,79 @@
|
||||||
|
from copy import deepcopy
|
||||||
|
from fn import op, _
|
||||||
|
|
||||||
|
def getColResults(step, sL, s, funcs):
|
||||||
|
return list(map(lambda f: f(step, sL, s), funcs))
|
||||||
|
|
||||||
|
|
||||||
|
def getBehaviorInput(step, sL, s, funcs):
|
||||||
|
return op.foldr(_ + _)(getColResults(step, sL, s, funcs))
|
||||||
|
|
||||||
|
|
||||||
|
def apply_env_proc(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])
|
||||||
|
|
||||||
|
|
||||||
|
def mech_step(m_step, sL, state_funcs, behavior_funcs, env_processes, t_step):
|
||||||
|
in_copy, mutatable_copy, out_copy = deepcopy(sL), deepcopy(sL), deepcopy(sL)
|
||||||
|
last_in_obj, last_mut_obj = in_copy[-1], mutatable_copy[-1]
|
||||||
|
|
||||||
|
_input = getBehaviorInput(m_step, sL, last_in_obj, behavior_funcs)
|
||||||
|
|
||||||
|
for f in state_funcs:
|
||||||
|
f(m_step, sL, last_mut_obj, _input)
|
||||||
|
|
||||||
|
apply_env_proc(env_processes, last_mut_obj, last_mut_obj['timestamp'])
|
||||||
|
|
||||||
|
last_mut_obj["mech_step"], last_mut_obj["time_step"] = m_step, t_step
|
||||||
|
out_copy.append(last_mut_obj)
|
||||||
|
|
||||||
|
del last_in_obj, last_mut_obj, in_copy, mutatable_copy,
|
||||||
|
|
||||||
|
return out_copy
|
||||||
|
|
||||||
|
|
||||||
|
def block_gen(states_list, configs, env_processes, t_step):
|
||||||
|
m_step = 0
|
||||||
|
states_list_copy = deepcopy(states_list)
|
||||||
|
genesis_states = states_list_copy[-1]
|
||||||
|
genesis_states['mech_step'], genesis_states['time_step'] = m_step, t_step
|
||||||
|
states_list = [genesis_states]
|
||||||
|
|
||||||
|
m_step += 1
|
||||||
|
for config in configs:
|
||||||
|
s_conf, b_conf = config[0], config[1]
|
||||||
|
states_list = mech_step(m_step, states_list, s_conf, b_conf, env_processes, t_step)
|
||||||
|
m_step += 1
|
||||||
|
|
||||||
|
t_step += 1
|
||||||
|
|
||||||
|
return states_list
|
||||||
|
|
||||||
|
|
||||||
|
def pipeline(states_list, configs, env_processes, time_seq):
|
||||||
|
time_seq = [x + 1 for x in time_seq]
|
||||||
|
simulation_list = [states_list]
|
||||||
|
for time_step in time_seq:
|
||||||
|
pipeline_run = block_gen(simulation_list[-1], configs, env_processes, time_step)
|
||||||
|
head, *pipeline_run = pipeline_run
|
||||||
|
simulation_list.append(pipeline_run)
|
||||||
|
|
||||||
|
return simulation_list
|
||||||
|
|
||||||
|
|
||||||
|
def simulation(states_list, configs, env_processes, time_seq, runs):
|
||||||
|
pipeline_run = []
|
||||||
|
for run in range(runs):
|
||||||
|
if run == 0:
|
||||||
|
head, *tail = pipeline(states_list, configs, env_processes, time_seq)
|
||||||
|
head[-1]['mech_step'], head[-1]['time_step'] = 0, 0
|
||||||
|
simulation_list = [head] + tail
|
||||||
|
pipeline_run += simulation_list
|
||||||
|
else:
|
||||||
|
transient_states_list = [pipeline_run[-1][-1]]
|
||||||
|
head, *tail = pipeline(transient_states_list, configs, env_processes, time_seq)
|
||||||
|
pipeline_run += tail
|
||||||
|
|
||||||
|
return pipeline_run
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
from ui.config import state_dict, mechanisms, exogenous_states, env_processes, sim_config
|
||||||
|
from engine.configProcessor import generate_config
|
||||||
|
from engine.mechanismExecutor import simulation
|
||||||
|
from engine.utils import flatten
|
||||||
|
|
||||||
|
from tabulate import tabulate
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
def main():
|
||||||
|
states_list = [state_dict]
|
||||||
|
configs = generate_config(mechanisms, exogenous_states)
|
||||||
|
# p = pipeline(states_list, configs, env_processes, range(10))
|
||||||
|
N = sim_config['N']
|
||||||
|
s = simulation(states_list, configs, env_processes, range(5), N)
|
||||||
|
result = pd.DataFrame(flatten(s))
|
||||||
|
print(tabulate(result, headers='keys', tablefmt='psql'))
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from decimal import Decimal
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
|
flatten = lambda l: [item for sublist in l for item in sublist]
|
||||||
|
|
||||||
|
def flatmap(f, items):
|
||||||
|
return list(map(f, items))
|
||||||
|
|
||||||
|
|
||||||
|
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]]
|
||||||
|
|
||||||
|
def _datetime_range(start, end, delta):
|
||||||
|
current = start
|
||||||
|
while current < end:
|
||||||
|
yield current
|
||||||
|
current += delta
|
||||||
|
|
||||||
|
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]
|
||||||
|
|
||||||
|
def bound_norm_random(rng, low, high):
|
||||||
|
# Add RNG Seed
|
||||||
|
res = rng.normal((high+low)/2,(high-low)/6)
|
||||||
|
if (res<low or res>high):
|
||||||
|
res = bound_norm_random(rng, low, high)
|
||||||
|
return Decimal(res)
|
||||||
|
|
||||||
|
def env_proc(trigger_step, update_f):
|
||||||
|
def env_step_trigger(trigger_step, update_f, step):
|
||||||
|
if step == trigger_step:
|
||||||
|
return update_f
|
||||||
|
else:
|
||||||
|
return lambda x: x
|
||||||
|
return partial(env_step_trigger, trigger_step, update_f)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
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:
|
||||||
|
return time_step(dt_str, fromat_str, days, minutes, seconds)
|
||||||
|
else:
|
||||||
|
return dt_str
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -29,59 +29,6 @@
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"['2018-10-01 01:00:00',\n",
|
|
||||||
" '2018-10-01 01:15:00',\n",
|
|
||||||
" '2018-10-01 01:30:00',\n",
|
|
||||||
" '2018-10-01 01:45:00',\n",
|
|
||||||
" '2018-10-01 02:00:00']"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 2,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
|
||||||
"def datetime_range(start, end, delta, dt_format='%Y-%m-%d %H:%M:%S'):\n",
|
|
||||||
" reverse_head = end\n",
|
|
||||||
" [start, end] = [datetime.strptime(x, dt_format) for x in [start, end]]\n",
|
|
||||||
" def _datetime_range(start, end, delta):\n",
|
|
||||||
" current = start\n",
|
|
||||||
" while current < end:\n",
|
|
||||||
" yield current\n",
|
|
||||||
" current += delta\n",
|
|
||||||
" \n",
|
|
||||||
" reverse_tail = [dt.strftime(dt_format) for dt in _datetime_range(start, end, delta)]\n",
|
|
||||||
" return reverse_tail + [reverse_head]\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"datetime_range('2018-10-01 01:00:00', '2018-10-01 02:00:00', timedelta(minutes=15))\n",
|
|
||||||
"\n",
|
|
||||||
"# # datetime(2018, 10, 1, 1, 0, 0) + timedelta(seconds=30)\n",
|
|
||||||
"# # datetime.strptime('2018-10-01 15:16:24', '%Y-%m-%d %H:%M:%S')\n",
|
|
||||||
"\n",
|
|
||||||
"# def time_step(dt_str, dt_format='%Y-%m-%d %H:%M:%S', days=0, minutes=0, seconds=30):\n",
|
|
||||||
"# dt = datetime.strptime(dt_str, dt_format)\n",
|
|
||||||
"# t = dt + timedelta(days=days, minutes=minutes, seconds=seconds)\n",
|
|
||||||
"# return t.strftime(dt_format)\n",
|
|
||||||
"\n",
|
|
||||||
"# def es_time_step(s, dt_str, fromat_str='%Y-%m-%d %H:%M:%S', days=0, minutes=0, seconds=1):\n",
|
|
||||||
"# if s['mech_step'] == 0:\n",
|
|
||||||
"# return time_step(dt_str, fromat_str, days, minutes, seconds)\n",
|
|
||||||
"# else: \n",
|
|
||||||
"# return dt_str\n",
|
|
||||||
"\n",
|
|
||||||
"# time_step('2018-10-01 15:16:24')"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 3,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"flatten = lambda l: [item for sublist in l for item in sublist]\n",
|
"flatten = lambda l: [item for sublist in l for item in sublist]\n",
|
||||||
|
|
@ -106,8 +53,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"def retrieve_state(l, offset):\n",
|
"def retrieve_state(l, offset):\n",
|
||||||
" return l[last_index(l) + offset + 1]\n",
|
" return l[last_index(l) + offset + 1]\n",
|
||||||
"# Stochastic Process\n",
|
"\n",
|
||||||
"# Input RNG Seed\n",
|
|
||||||
"def bound_norm_random(rng, low, high):\n",
|
"def bound_norm_random(rng, low, high):\n",
|
||||||
" # Add RNG Seed\n",
|
" # Add RNG Seed\n",
|
||||||
" res = rng.normal((high+low)/2,(high-low)/6)\n",
|
" res = rng.normal((high+low)/2,(high-low)/6)\n",
|
||||||
|
|
@ -118,10 +64,8 @@
|
||||||
"def env_proc(trigger_step, update_f):\n",
|
"def env_proc(trigger_step, update_f):\n",
|
||||||
" def env_step_trigger(trigger_step, update_f, step):\n",
|
" def env_step_trigger(trigger_step, update_f, step):\n",
|
||||||
" if step == trigger_step:\n",
|
" if step == trigger_step:\n",
|
||||||
"# print('trigered: '+step)\n",
|
|
||||||
" return update_f\n",
|
" return update_f\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
"# print('NOT trigered: '+step)\n",
|
|
||||||
" return lambda x: x\n",
|
" return lambda x: x\n",
|
||||||
" return partial(env_step_trigger, trigger_step, update_f)\n",
|
" return partial(env_step_trigger, trigger_step, update_f)\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -137,28 +81,22 @@
|
||||||
" return dt_str\n",
|
" return dt_str\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def round_down(x, fp=TWOPLACES):\n",
|
"def round_down(x, fp=TWOPLACES):\n",
|
||||||
" return x.quantize(TWOPLACES, rounding=ROUND_DOWN)\n",
|
" return x.quantize(TWOPLACES, rounding=ROUND_DOWN)"
|
||||||
" \n",
|
|
||||||
"# def round_down(f, fp=TWOPLACES):\n",
|
|
||||||
"# return (f).quantize(fp, ROUND_DOWN)"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 3,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"# esceptions:\n",
|
"# esceptions:\n",
|
||||||
"# point to line giving problems and pass a sentinel"
|
"# point to line giving problems and pass a sentinel"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 4,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
|
@ -211,14 +149,10 @@
|
||||||
"def es5p2(step, sL, s, _input):\n",
|
"def es5p2(step, sL, s, _input):\n",
|
||||||
" s['timestamp'] = ep_time_step(s, s['timestamp'], seconds=1)\n",
|
" s['timestamp'] = ep_time_step(s, s['timestamp'], seconds=1)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
|
||||||
"# add env process f(s) read from time es\n",
|
|
||||||
"# funcs execute in order\n",
|
|
||||||
"def env_a(x): \n",
|
"def env_a(x): \n",
|
||||||
" return 10\n",
|
" return 10\n",
|
||||||
"def env_b(x): \n",
|
"def env_b(x): \n",
|
||||||
" return 10\n",
|
" return 10\n",
|
||||||
" \n",
|
|
||||||
"def what_ever(x): \n",
|
"def what_ever(x): \n",
|
||||||
" return x + 1\n",
|
" return x + 1\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -231,9 +165,9 @@
|
||||||
" 'timestamp': '2018-10-01 15:16:24'\n",
|
" 'timestamp': '2018-10-01 15:16:24'\n",
|
||||||
"}\n",
|
"}\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# simulation = {\n",
|
"sim_config = {\n",
|
||||||
"# \"s3\": es3p1\n",
|
" \"N\": 2\n",
|
||||||
"# }\n",
|
"}\n",
|
||||||
"\n",
|
"\n",
|
||||||
"exogenous_states = {\n",
|
"exogenous_states = {\n",
|
||||||
" \"s3\": es3p1,\n",
|
" \"s3\": es3p1,\n",
|
||||||
|
|
@ -241,8 +175,6 @@
|
||||||
" \"timestamp\": es5p2\n",
|
" \"timestamp\": es5p2\n",
|
||||||
"}\n",
|
"}\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Update time at the end of each pipeline (once per 'block')\n",
|
|
||||||
"# Enable the acceptance of user defined time\n",
|
|
||||||
"env_processes = {\n",
|
"env_processes = {\n",
|
||||||
" \"s3\": env_proc('2018-10-01 15:16:25', env_a),\n",
|
" \"s3\": env_proc('2018-10-01 15:16:25', env_a),\n",
|
||||||
" \"s4\": env_proc('2018-10-01 15:16:25', env_b)\n",
|
" \"s4\": env_proc('2018-10-01 15:16:25', env_b)\n",
|
||||||
|
|
@ -284,7 +216,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": 5,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
|
@ -316,16 +248,11 @@
|
||||||
"# return state_dict\n",
|
"# return state_dict\n",
|
||||||
" \n",
|
" \n",
|
||||||
"def mech_step(m_step, sL, state_funcs, behavior_funcs, env_processes, t_step):\n",
|
"def mech_step(m_step, sL, state_funcs, behavior_funcs, env_processes, t_step):\n",
|
||||||
" # Truncate reffs\n",
|
|
||||||
" in_copy, mutatable_copy, out_copy = deepcopy(sL), deepcopy(sL), deepcopy(sL)\n",
|
" in_copy, mutatable_copy, out_copy = deepcopy(sL), deepcopy(sL), deepcopy(sL)\n",
|
||||||
" last_in_obj, last_mut_obj = in_copy[-1], mutatable_copy[-1]\n",
|
" last_in_obj, last_mut_obj = in_copy[-1], mutatable_copy[-1]\n",
|
||||||
" \n",
|
" \n",
|
||||||
" # * force eval of _input withing state functions\n",
|
|
||||||
" _input = getBehaviorInput(m_step, sL, last_in_obj, behavior_funcs)\n",
|
" _input = getBehaviorInput(m_step, sL, last_in_obj, behavior_funcs)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" \n",
|
|
||||||
"# apply_env_proc(env_processes, last_mut_obj, t_step)\n",
|
|
||||||
"\n",
|
|
||||||
" for f in state_funcs:\n",
|
" for f in state_funcs:\n",
|
||||||
" f(m_step, sL, last_mut_obj, _input)\n",
|
" f(m_step, sL, last_mut_obj, _input)\n",
|
||||||
" \n",
|
" \n",
|
||||||
|
|
@ -382,34 +309,25 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 15,
|
"execution_count": 6,
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"time_seq = datetime_range('2018-10-01 15:16:24', '2018-10-01 15:16:34', timedelta(seconds=1))"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 7,
|
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"states_list = [state_dict]\n",
|
"states_list = [state_dict]\n",
|
||||||
"configs = generate_config(mechanisms, exogenous_states)\n",
|
"configs = generate_config(mechanisms, exogenous_states)\n",
|
||||||
"p = pipeline(states_list, configs, env_processes, range(10))\n",
|
"p = pipeline(states_list, configs, env_processes, range(10))\n",
|
||||||
"s = simulation(states_list, configs, env_processes, range(5), 2)\n",
|
"N = sim_config['N']\n",
|
||||||
"x = flatten(s)\n",
|
"s = simulation(states_list, configs, env_processes, range(5), N)\n",
|
||||||
|
"# x = flatten(s)\n",
|
||||||
"# transient_state = p[-1][1]\n",
|
"# transient_state = p[-1][1]\n",
|
||||||
"# transient_state['mech_step'], transient_state['time_step'] = 0, 0\n",
|
"# transient_state['mech_step'], transient_state['time_step'] = 0, 0\n",
|
||||||
"# head, *tail = pipeline([transient_state], configs, env_processes, range(1))\n",
|
"# head, *tail = pipeline([transient_state], configs, env_processes, range(1))\n",
|
||||||
"# head\n",
|
"# head"
|
||||||
"datetime_range('2018-10-01 15:16:24', '2018-10-01 15:16:34', timedelta(seconds=1))"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 8,
|
"execution_count": 7,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
|
|
@ -858,7 +776,7 @@
|
||||||
"30 2018-10-01 15:16:34 "
|
"30 2018-10-01 15:16:34 "
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 8,
|
"execution_count": 7,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
|
|
@ -876,7 +794,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 9,
|
"execution_count": 8,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
|
@ -885,7 +803,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 10,
|
"execution_count": 9,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
|
@ -894,7 +812,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 11,
|
"execution_count": 10,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
|
|
@ -1343,7 +1261,7 @@
|
||||||
"30 2018-10-01 15:16:34 "
|
"30 2018-10-01 15:16:34 "
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 11,
|
"execution_count": 10,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
|
|
@ -1357,7 +1275,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 12,
|
"execution_count": 11,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
|
|
@ -1366,7 +1284,7 @@
|
||||||
"[0, 1]"
|
"[0, 1]"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 12,
|
"execution_count": 11,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
|
|
@ -1377,7 +1295,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 13,
|
"execution_count": 12,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
|
|
@ -1385,9 +1303,9 @@
|
||||||
"evalue": "name 'non_numeric' is not defined",
|
"evalue": "name 'non_numeric' is not defined",
|
||||||
"output_type": "error",
|
"output_type": "error",
|
||||||
"traceback": [
|
"traceback": [
|
||||||
"\u001b[0;31m--------------------------------------------------------------------------\u001b[0m",
|
"\u001b[0;31m---------------------------------------------------------------\u001b[0m",
|
||||||
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
|
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
|
||||||
"\u001b[0;32m<ipython-input-13-9786f8c4c4ba>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mstate_dict\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m's1'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mDecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'3323'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m's2'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mDecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'2572'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m's3'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mDecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'2.657'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m's4'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mDecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'7.914'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'timestamp'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'2018-10-01 15:16:26'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'mech_step'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'time_step'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mstate_dict\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mquantize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mTWOPLACES\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mROUND_DOWN\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mstate_dict\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mDecimal\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mstate_dict\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnon_numeric\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mstate_dict\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
"\u001b[0;32m<ipython-input-12-9786f8c4c4ba>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mstate_dict\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m's1'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mDecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'3323'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m's2'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mDecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'2572'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m's3'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mDecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'2.657'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m's4'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mDecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'7.914'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'timestamp'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'2018-10-01 15:16:26'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'mech_step'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'time_step'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mstate_dict\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mquantize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mTWOPLACES\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mROUND_DOWN\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mstate_dict\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mDecimal\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mstate_dict\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnon_numeric\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mstate_dict\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
"\u001b[0;31mNameError\u001b[0m: name 'non_numeric' is not defined"
|
"\u001b[0;31mNameError\u001b[0m: name 'non_numeric' is not defined"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Binary file not shown.
|
|
@ -0,0 +1,120 @@
|
||||||
|
from engine.utils import bound_norm_random, ep_time_step, env_proc
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
|
seed = {
|
||||||
|
'z': np.random.RandomState(1),
|
||||||
|
'a': np.random.RandomState(2),
|
||||||
|
'b': np.random.RandomState(3),
|
||||||
|
'c': np.random.RandomState(3)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Behaviors per Mechanism
|
||||||
|
def b1m1(step, sL, s):
|
||||||
|
return s['s1'] + 1
|
||||||
|
def b2m1(step, sL, s):
|
||||||
|
return s['s1'] + 1
|
||||||
|
|
||||||
|
def b1m2(step, sL, s):
|
||||||
|
return s['s1'] + 1
|
||||||
|
def b2m2(step, sL, s):
|
||||||
|
return s['s1'] + 1
|
||||||
|
|
||||||
|
def b1m3(step, sL, s):
|
||||||
|
return s['s1'] + 1
|
||||||
|
def b2m3(step, sL, s):
|
||||||
|
return s['s2'] + 1
|
||||||
|
|
||||||
|
|
||||||
|
# Internal States per Mechanism
|
||||||
|
def s1m1(step, sL, s, _input):
|
||||||
|
s['s1'] = s['s1'] + _input
|
||||||
|
def s2m1(step, sL, s, _input):
|
||||||
|
s['s2'] = s['s2'] + _input
|
||||||
|
|
||||||
|
|
||||||
|
def s1m2(step, sL, s, _input):
|
||||||
|
s['s1'] = s['s1'] + _input
|
||||||
|
def s2m2(step, sL, s, _input):
|
||||||
|
s['s2'] = s['s2'] + _input
|
||||||
|
|
||||||
|
def s1m3(step, sL, s, _input):
|
||||||
|
s['s1'] = s['s1'] + _input
|
||||||
|
def s2m3(step, sL, s, _input):
|
||||||
|
s['s2'] = s['s2'] + _input
|
||||||
|
|
||||||
|
# Exogenous States
|
||||||
|
proc_one_coef_A = 0.7
|
||||||
|
proc_one_coef_B = 1.3
|
||||||
|
def es3p1(step, sL, s, _input):
|
||||||
|
s['s3'] = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B)
|
||||||
|
def es4p2(step, sL, s, _input):
|
||||||
|
s['s4'] = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B)
|
||||||
|
def es5p2(step, sL, s, _input):
|
||||||
|
s['timestamp'] = ep_time_step(s, s['timestamp'], seconds=1)
|
||||||
|
|
||||||
|
# Environment States
|
||||||
|
def env_a(x):
|
||||||
|
return x
|
||||||
|
def env_b(x):
|
||||||
|
return x
|
||||||
|
def what_ever(x):
|
||||||
|
return x + 1
|
||||||
|
|
||||||
|
# Genesis States
|
||||||
|
state_dict = {
|
||||||
|
's1': Decimal(0.0),
|
||||||
|
's2': Decimal(0.0),
|
||||||
|
's3': Decimal(1.0),
|
||||||
|
's4': Decimal(1.0),
|
||||||
|
'timestamp': '2018-10-01 15:16:24'
|
||||||
|
}
|
||||||
|
|
||||||
|
exogenous_states = {
|
||||||
|
"s3": es3p1,
|
||||||
|
"s4": es4p2,
|
||||||
|
"timestamp": es5p2
|
||||||
|
}
|
||||||
|
|
||||||
|
env_processes = {
|
||||||
|
"s3": env_proc('2018-10-01 15:16:25', env_a),
|
||||||
|
"s4": env_proc('2018-10-01 15:16:25', env_b)
|
||||||
|
}
|
||||||
|
|
||||||
|
mechanisms = {
|
||||||
|
"m1": {
|
||||||
|
"behaviors": {
|
||||||
|
"b1": b1m1,
|
||||||
|
"b2": b2m1
|
||||||
|
},
|
||||||
|
"states": {
|
||||||
|
"s1": s1m1,
|
||||||
|
"s2": s2m1,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"m2": {
|
||||||
|
"behaviors": {
|
||||||
|
"b1": b1m2,
|
||||||
|
"b2": b2m2
|
||||||
|
},
|
||||||
|
"states": {
|
||||||
|
"s1": s1m2,
|
||||||
|
"s2": s2m2,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"m3": {
|
||||||
|
"behaviors": {
|
||||||
|
"b1": b1m3,
|
||||||
|
"b2": b2m3
|
||||||
|
},
|
||||||
|
"states": {
|
||||||
|
"s1": s1m3,
|
||||||
|
"s2": s2m3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sim_config = {
|
||||||
|
"N": 2
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
from engine import run
|
||||||
|
run.main()
|
||||||
Loading…
Reference in New Issue