694 lines
31 KiB
Plaintext
694 lines
31 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from fn import op, _, F\n",
|
|
"from operator import add, mul\n",
|
|
"from itertools import repeat, chain\n",
|
|
"from functools import reduce\n",
|
|
"# from objproxies import LazyProxy\n",
|
|
"import json\n",
|
|
"from copy import deepcopy, copy\n",
|
|
"from pipetools import pipe\n",
|
|
"from functools import partial\n",
|
|
"import numpy as np\n",
|
|
"from datetime import datetime, timedelta\n",
|
|
"from decimal import Decimal, MAX_EMAX"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"flatten = lambda l: [item for sublist in l for item in sublist]\n",
|
|
"\n",
|
|
"def flatmap(f, items):\n",
|
|
" return list(map(f, items))\n",
|
|
" \n",
|
|
"def last_index(l):\n",
|
|
" return len(l)-1\n",
|
|
"\n",
|
|
"def retrieve_state(l, offset):\n",
|
|
" return l[last_index(l) + offset + 1]\n",
|
|
"# Stochastic Process\n",
|
|
"# Input RNG Seed\n",
|
|
"def bound_norm_random(rng, low, high):\n",
|
|
" # Add RNG Seed\n",
|
|
" res = rng.normal((high+low)/2,(high-low)/6)\n",
|
|
" if (res<low or res>high):\n",
|
|
" res = bound_norm_random(rng, low, high)\n",
|
|
" return Decimal(res)\n",
|
|
"\n",
|
|
"def env_proc(trigger_step, update_f):\n",
|
|
" def env_step_trigger(trigger_step, update_f, step):\n",
|
|
" if step == trigger_step:\n",
|
|
" return update_f\n",
|
|
" else:\n",
|
|
" return lambda x: x\n",
|
|
" return partial(env_step_trigger, trigger_step, update_f)\n",
|
|
"\n",
|
|
"def env_proc(trigger_step, update_f):\n",
|
|
" def env_step_trigger(trigger_step, update_f, step):\n",
|
|
" if step == trigger_step:\n",
|
|
" return update_f\n",
|
|
" else:\n",
|
|
" return lambda x: x\n",
|
|
" return partial(env_step_trigger, trigger_step, update_f)\n",
|
|
"\n",
|
|
"def 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",
|
|
" dt = datetime.strptime(dt_str, fromat_str)\n",
|
|
" t = dt + timedelta(days=days, minutes=minutes, seconds=seconds)\n",
|
|
" return t.strftime(fromat_str)\n",
|
|
" else: \n",
|
|
" return dt_str"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 14,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# def pipeline(*steps):\n",
|
|
"# return reduce(lambda x, y: y(x), list(steps))\n",
|
|
"# def compose(*funcs):\n",
|
|
"# return lambda x: reduce(lambda f, g: g(f), list(funcs), x)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 15,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# ToDo:\n",
|
|
"# Handle case where Mechanisms have no input. Perhaps the sentinel value of 0\n",
|
|
"# Mock Simulation SimCADUI Lucid chart \n",
|
|
"\n",
|
|
"# Filter by states at steps (in runtime)\n",
|
|
"\n",
|
|
"# No historical access beyond previous step\n",
|
|
"# problem, inconsistence between what you fillter and access\n",
|
|
"\n",
|
|
"# highlight / directly ref. steps within b, s, es mech func. defs\n",
|
|
"\n",
|
|
"# Can behaviors & state read steps / time\n",
|
|
"\n",
|
|
"# Play with this\n",
|
|
"# Time: Exogenous state and the Env Process of it is (prev. state + duration of step)\n",
|
|
"# Sim config contains \"duration of step\"\n",
|
|
"\n",
|
|
"# Env Proc\n",
|
|
"\n",
|
|
"# only need states to run the enigine (exclude everything else in config gen. func.)\n",
|
|
"# validation of config: mechs, behaviors, & eps (optional) can be null / 0\n",
|
|
"\n",
|
|
"# simlulation category mapping\n",
|
|
"\n",
|
|
"# RNG Seed Config:\n",
|
|
"# * 1 seed per exo_proc (Manditory)\n",
|
|
"# * 1 seed per behavior_mech (optional)\n",
|
|
"# init RNG obj with seed during config, then use RNG object during each state mutation\n",
|
|
"# RNG Seed = output field\n",
|
|
"\n",
|
|
"# define UI generated switch funct for behavior <> seed reporting\n",
|
|
"\n",
|
|
"# remove objs from mem **\n",
|
|
"# *** large numbers"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 16,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"seed = {\n",
|
|
" 'z': np.random.RandomState(1),\n",
|
|
" 'a': np.random.RandomState(2),\n",
|
|
" 'b': np.random.RandomState(3),\n",
|
|
" 'c': np.random.RandomState(3)\n",
|
|
"}\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 17,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"a = np.random.RandomState(1)\n",
|
|
"proc_one_coef_B = 1.3\n",
|
|
"b = np.random.RandomState(2)\n",
|
|
"\n",
|
|
"def pipe_f(x):\n",
|
|
" return x"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 19,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# UI Behaviors per Mechanism\n",
|
|
"def b1m1(step, sL, s):\n",
|
|
" return s['s1']\n",
|
|
"def b2m1(step, sL, s):\n",
|
|
" return s['s1'] * s['s2']\n",
|
|
"\n",
|
|
"def b1m2(step, sL, s):\n",
|
|
" return s['s1']\n",
|
|
"def b2m2(step, sL, s):\n",
|
|
" return s['s1'] / s['s2']\n",
|
|
"\n",
|
|
"def b1m3(step, sL, s):\n",
|
|
" return s['s1'] + seed['z'].randint(1000)\n",
|
|
"def b2m3(step, sL, s):\n",
|
|
" ps = retrieve_state(sL, -3)\n",
|
|
" return ps['s2']\n",
|
|
"\n",
|
|
"# UI Internal States per Mechanism\n",
|
|
"def s1m1(step, sL, s, _input):\n",
|
|
" print(s['s1'])\n",
|
|
" s['s1'] = s['s1']**2 + _input\n",
|
|
"def s2m1(step, sL, s, _input):\n",
|
|
" s['s2'] = s['s2'] + 1 + _input\n",
|
|
"\n",
|
|
"def s1m2(step, sL, s, _input):\n",
|
|
" s['s1'] = s['s1'] + _input\n",
|
|
"def s2m2(step, sL, s, _input):\n",
|
|
" s['s2'] = s['s2']\n",
|
|
" \n",
|
|
"def s1m3(step, sL, s, _input):\n",
|
|
" s['s1'] = s['s1']\n",
|
|
"def s2m3(step, sL, s, _input):\n",
|
|
" s['s2'] = s['s2'] + _input\n",
|
|
"\n",
|
|
"# UI Exogenous States //per Mechanism \n",
|
|
"# ??? Refactor: The difference between mech and step need to be re ???\n",
|
|
"\n",
|
|
"proc_one_coef_A = 0.7\n",
|
|
"proc_one_coef_B = 1.3\n",
|
|
"def es3p1(step, sL, s, _input):\n",
|
|
" s['s3'] = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B)\n",
|
|
"def es4p2(step, sL, s, _input):\n",
|
|
" s['s4'] = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B) \n",
|
|
"def es5p2(step, sL, s, _input):\n",
|
|
" s['timestamp'] = time_step(s, s['timestamp'], seconds=1)\n",
|
|
"\n",
|
|
"\n",
|
|
"#add env process f(s) read from time es\n",
|
|
"# funcs execute in order\n",
|
|
"def env_a(x): \n",
|
|
" return x + 1\n",
|
|
"def env_b(x): \n",
|
|
" return x + 2\n",
|
|
" \n",
|
|
"def what_ever(x): \n",
|
|
" return x + 1\n",
|
|
"\n",
|
|
"# Genesis States \n",
|
|
"state_dict = {\n",
|
|
" 's1': Decimal(2.0),\n",
|
|
" 's2': Decimal(4.0),\n",
|
|
" 's3': Decimal(0.0),\n",
|
|
" 's4': Decimal(0.0),\n",
|
|
" 'timestamp': '2018-10-01 15:16:24'\n",
|
|
"}\n",
|
|
"\n",
|
|
"exogenous_states = {\n",
|
|
" \"s3\": es3p1,\n",
|
|
" \"s4\": es4p2,\n",
|
|
" \"timestamp\": es5p2\n",
|
|
"}\n",
|
|
"\n",
|
|
"# update time at the end of each pipeline (once per 'block')\n",
|
|
"env_processes = {\n",
|
|
" \"s3\": env_proc(1, env_a),\n",
|
|
" \"s4\": env_proc(1, pipe | env_b | what_ever)\n",
|
|
"}\n",
|
|
" \n",
|
|
"mechanisms = {\n",
|
|
" \"m1\": {\n",
|
|
" \"behaviors\": {\n",
|
|
" \"b1\": b1m1,\n",
|
|
" \"b2\": b2m1\n",
|
|
" },\n",
|
|
" \"states\": {\n",
|
|
" \"s1\": s1m1,\n",
|
|
" \"s2\": s2m1,\n",
|
|
" }\n",
|
|
" },\n",
|
|
" \"m2\": {\n",
|
|
" \"behaviors\": {\n",
|
|
" \"b1\": b1m2,\n",
|
|
" \"b2\": b2m2\n",
|
|
" },\n",
|
|
" \"states\": {\n",
|
|
" \"s1\": s1m2,\n",
|
|
" \"s2\": s2m2,\n",
|
|
" }\n",
|
|
" },\n",
|
|
" \"m3\": {\n",
|
|
" \"behaviors\": {\n",
|
|
" \"b1\": b1m3,\n",
|
|
" \"b2\": b2m3\n",
|
|
" },\n",
|
|
" \"states\": {\n",
|
|
" \"s1\": s1m3,\n",
|
|
" \"s2\": s2m3,\n",
|
|
" }\n",
|
|
" }\n",
|
|
"}"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 20,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# if beh list empty, repeat 0 x n_states in list \n",
|
|
"def generate_config(mechanisms, exogenous_states):\n",
|
|
" es_funcs = [exogenous_states[state] for state in list(exogenous_states.keys())]\n",
|
|
" config = list(\n",
|
|
" map(\n",
|
|
" lambda m: (\n",
|
|
" list(mechanisms[m][\"states\"].values()) + es_funcs,\n",
|
|
" list(mechanisms[m][\"behaviors\"].values())\n",
|
|
" ), \n",
|
|
" list(mechanisms.keys())\n",
|
|
" )\n",
|
|
" )\n",
|
|
" return config\n",
|
|
"\n",
|
|
"# partials = list(map(lambda f: partial(f, step, sL), funcs))\n",
|
|
"def getColResults(step, sL, s, funcs):\n",
|
|
" return list(map(lambda f: f(step, sL, s), funcs))\n",
|
|
"\n",
|
|
"def getBehaviorInput(step, sL, s, funcs): \n",
|
|
" return op.foldr(_ + _)(getColResults(step, sL, s, funcs))\n",
|
|
"\n",
|
|
"def apply_env_proc(env_processes, state_dict, step):\n",
|
|
" for state in state_dict.keys(): \n",
|
|
" if state in list(env_processes.keys()):\n",
|
|
" state_dict[state] = env_processes[state](step)(state_dict[state])\n",
|
|
"# return state_dict\n",
|
|
" \n",
|
|
"def mech_step(m_step, sL, state_funcs, behavior_funcs, env_processes, t_step):\n",
|
|
" # Purge Memory accociated with certain objs\n",
|
|
" # Truncate reffs\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",
|
|
" \n",
|
|
" # * force eval of _input withing state functions\n",
|
|
" _input = getBehaviorInput(m_step, sL, last_in_obj, behavior_funcs)\n",
|
|
"\n",
|
|
" apply_env_proc(env_processes, last_mut_obj, t_step)\n",
|
|
" \n",
|
|
" for f in state_funcs:\n",
|
|
" f(m_step, sL, last_mut_obj, _input)\n",
|
|
" \n",
|
|
" last_mut_obj[\"mech_step\"], last_mut_obj[\"time_step\"] = m_step, t_step\n",
|
|
" out_copy.append(last_mut_obj)\n",
|
|
" \n",
|
|
" del last_in_obj, last_mut_obj, in_copy, mutatable_copy,\n",
|
|
" return out_copy\n",
|
|
"\n",
|
|
"# isolate ALL step logic within 'step_func'\n",
|
|
"def block_gen(states_list, configs, env_processes, t_step):\n",
|
|
" m_step = 0\n",
|
|
" states_list_copy = deepcopy(states_list) #[-1]\n",
|
|
" genesis_states = states_list_copy[-1]\n",
|
|
" genesis_states['mech_step'], genesis_states['time_step'] = m_step, t_step\n",
|
|
" states_list = [genesis_states]\n",
|
|
" \n",
|
|
" m_step += 1\n",
|
|
" for config in configs:\n",
|
|
" s_conf, b_conf = config[0], config[1]\n",
|
|
" states_list = mech_step(m_step, states_list, s_conf, b_conf, env_processes, t_step) \n",
|
|
" m_step += 1\n",
|
|
" \n",
|
|
" t_step += 1 \n",
|
|
" \n",
|
|
" return states_list\n",
|
|
"\n",
|
|
"def pipeline(states_list, configs, env_processes, time_seq):\n",
|
|
" time_seq = [x + 1 for x in time_seq]\n",
|
|
" simulation_list = [states_list]\n",
|
|
" for time_step in time_seq:\n",
|
|
" pipeline_run = block_gen(simulation_list[-1], configs, env_processes, time_step)\n",
|
|
" head, *pipeline_run = pipeline_run\n",
|
|
" simulation_list.append(pipeline_run)\n",
|
|
" \n",
|
|
" # should only occur once for genesis state, placed before loop\n",
|
|
" head, *tail = simulation_list\n",
|
|
" head[-1]['mech_step'], head[-1]['time_step'] = 0, 0\n",
|
|
" simulation_list = head, *tail\n",
|
|
" return simulation_list\n",
|
|
"\n",
|
|
"# Simulation layer\n",
|
|
"# pipeline(states_list, configs, env_processes, range(1))\n",
|
|
"\n",
|
|
"# Parameter sweep layer (single config param? For now ;) )\n",
|
|
" \n",
|
|
"# n = T (time) <> Pipeline run \n",
|
|
"# multiple runs of Pipeline per Montercarlo Simulation\n",
|
|
"# Montercarlo Simulation creates a new dataset initialized by the same genesis states"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 21,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'s1': Decimal('2.221384236472411425810660220E+330'),\n",
|
|
" 's2': Decimal('2.845096899831187764579913612E+330'),\n",
|
|
" 's3': Decimal('3.622519693611211445932083740'),\n",
|
|
" 's4': Decimal('5.161487081026327858663750787'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:32',\n",
|
|
" 'mech_step': 3,\n",
|
|
" 'time_step': 8}"
|
|
]
|
|
},
|
|
"execution_count": 21,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"states_list = [state_dict]\n",
|
|
"states_list\n",
|
|
"{'s1': 8.742503764992283e+170,\n",
|
|
" 's2': 1.119720780594405e+171,\n",
|
|
" 's3': 4.251811100418906,\n",
|
|
" 's4': 6.92728042985628,\n",
|
|
" 'timestamp': '2018-10-01 15:16:31',\n",
|
|
" 'mech_step': 3,\n",
|
|
" 'time_step': 7}\n",
|
|
"{'s1': Decimal('2.221384236472411425810660220E+330'),\n",
|
|
" 's2': Decimal('2.845096899831187764579913612E+330'),\n",
|
|
" 's3': Decimal('3.622519693611211445932083740'),\n",
|
|
" 's4': Decimal('5.161487081026327858663750787'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:32',\n",
|
|
" 'mech_step': 3,\n",
|
|
" 'time_step': 8}"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 22,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"Decimal('Infinity')"
|
|
]
|
|
},
|
|
"execution_count": 22,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"Decimal(1.180039338585514253573643575E+354663)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 23,
|
|
"metadata": {
|
|
"scrolled": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"2\n",
|
|
"28.93333333333333333333333333\n",
|
|
"6648.243150484876793318447126\n",
|
|
"215266002.9098566044894792728\n",
|
|
"212676723296002516.0223878870\n",
|
|
"2.064462977716089278043496414E+35\n",
|
|
"1.944246638657598357070784299E+71\n",
|
|
"1.724319625268384335555851407E+143\n",
|
|
"1.356277245443530770557350375E+287\n",
|
|
"8.390921927247213674080072180E+574\n",
|
|
"3.211678541196335645996776724E+1150\n",
|
|
"4.705186557507909314007212924E+2301\n",
|
|
"1.009872166532254449900559698E+4604\n",
|
|
"4.652062198297258244018932170E+9208\n",
|
|
"9.871967857966477618498629472E+18417\n",
|
|
"4.445495477485404825150822930E+36836\n",
|
|
"9.014736833846553341620594734E+73673\n",
|
|
"3.706967797153494335559907166E+147348\n",
|
|
"6.268308088445542763967769694E+294697\n"
|
|
]
|
|
},
|
|
{
|
|
"ename": "Overflow",
|
|
"evalue": "[<class 'decimal.Overflow'>]",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[0;31m-------------------------------------------------\u001b[0m",
|
|
"\u001b[0;31mOverflow\u001b[0m Traceback (most recent call last)",
|
|
"\u001b[0;32m<ipython-input-23-900ef90e9c0a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mconfigs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgenerate_config\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmechanisms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexogenous_states\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mpipeline\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstates_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconfigs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;31m# a = block_gen(states_list, configs, env_processes, 1)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m# b = block_gen(a, configs, env_processes, 2)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32m<ipython-input-20-b19b5b411056>\u001b[0m in \u001b[0;36mpipeline\u001b[0;34m(states_list, configs, env_processes, time_seq)\u001b[0m\n\u001b[1;32m 68\u001b[0m \u001b[0msimulation_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mstates_list\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 69\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mtime_step\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mtime_seq\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 70\u001b[0;31m \u001b[0mpipeline_run\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mblock_gen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msimulation_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconfigs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtime_step\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 71\u001b[0m \u001b[0mhead\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0mpipeline_run\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpipeline_run\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 72\u001b[0m \u001b[0msimulation_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpipeline_run\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32m<ipython-input-20-b19b5b411056>\u001b[0m in \u001b[0;36mblock_gen\u001b[0;34m(states_list, configs, env_processes, t_step)\u001b[0m\n\u001b[1;32m 57\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mconfig\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mconfigs\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 58\u001b[0m \u001b[0ms_conf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb_conf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mconfig\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconfig\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 59\u001b[0;31m \u001b[0mstates_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmech_step\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mm_step\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstates_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms_conf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb_conf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mt_step\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 60\u001b[0m \u001b[0mm_step\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 61\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32m<ipython-input-20-b19b5b411056>\u001b[0m in \u001b[0;36mmech_step\u001b[0;34m(m_step, sL, state_funcs, behavior_funcs, env_processes, t_step)\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 34\u001b[0m \u001b[0;31m# * force eval of _input withing state functions\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 35\u001b[0;31m \u001b[0m_input\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgetBehaviorInput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mm_step\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msL\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlast_in_obj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbehavior_funcs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 36\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 37\u001b[0m \u001b[0mapply_env_proc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0menv_processes\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlast_mut_obj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mt_step\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32m<ipython-input-20-b19b5b411056>\u001b[0m in \u001b[0;36mgetBehaviorInput\u001b[0;34m(step, sL, s, funcs)\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mgetBehaviorInput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msL\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfuncs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 20\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mop\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfoldr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgetColResults\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msL\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfuncs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 21\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mapply_env_proc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0menv_processes\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstate_dict\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstep\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32m<ipython-input-20-b19b5b411056>\u001b[0m in \u001b[0;36mgetColResults\u001b[0;34m(step, sL, s, funcs)\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;31m# partials = list(map(lambda f: partial(f, step, sL), funcs))\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mgetColResults\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msL\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfuncs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msL\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfuncs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mgetBehaviorInput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msL\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfuncs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32m<ipython-input-20-b19b5b411056>\u001b[0m in \u001b[0;36m<lambda>\u001b[0;34m(f)\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;31m# partials = list(map(lambda f: partial(f, step, sL), funcs))\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mgetColResults\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msL\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfuncs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msL\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfuncs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mgetBehaviorInput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msL\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfuncs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32m<ipython-input-19-da6201518c93>\u001b[0m in \u001b[0;36mb2m1\u001b[0;34m(step, sL, s)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m's1'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mb2m1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msL\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m's1'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m's2'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mb1m2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msL\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;31mOverflow\u001b[0m: [<class 'decimal.Overflow'>]"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"configs = generate_config(mechanisms, exogenous_states)\n",
|
|
"\n",
|
|
"pipeline(states_list, configs, env_processes, range(20))\n",
|
|
"# a = block_gen(states_list, configs, env_processes, 1)\n",
|
|
"# b = block_gen(a, configs, env_processes, 2)\n",
|
|
"# b\n",
|
|
"# categorize by simulation\n",
|
|
"\n",
|
|
"# Pipeline : exec of mech steps\n",
|
|
"# T (Time Step) per Pipeline runs\n",
|
|
"\n",
|
|
"# Simulatiom: N Pipline runs per Simulation at different T progressively"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from decimal import Decimal, MAX_EMAX\n",
|
|
"# Decimal(2.5347751615574597e+167)**2\n",
|
|
"MAX_EMAX**5"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"scrolled": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"1000 * 1000"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"scrolled": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from datetime import datetime, timedelta\n",
|
|
"# from datetime import timedelta \n",
|
|
"# st = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')\n",
|
|
"st = datetime.now()\n",
|
|
"print(st)\n",
|
|
"x = st + timedelta(seconds=60)\n",
|
|
"print(x)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"s = datetime.now().strftime('%Y-%m-%d %H:%M:%S')\n",
|
|
"datetime_object = datetime.strptime(s, '%Y-%m-%d %H:%M:%S')\n",
|
|
"datetime_object"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"datetime.fromtimestamp()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from datetime import datetime, timedelta\n",
|
|
"\n",
|
|
"# def time_step_func(year, month, day, hour, minute, second):\n",
|
|
"a = datetime(2016, 11, 15, 9, 59, 25)#.strftime(\"%Y-%m-%d %H:%M:%S\")\n",
|
|
"b = a + timedelta(seconds=60) # days, seconds, then other fields.\n",
|
|
"print(a)\n",
|
|
"print(b)\n",
|
|
"st = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')\n",
|
|
"def time_step_func(dt_str, fromat_str='%Y-%m-%d %H:%M:%S', days=0, minutes=0, seconds=1):\n",
|
|
" dt = datetime.strptime(dt_str, fromat_str)\n",
|
|
" t = dt + timedelta(days=days, minutes=minutes, seconds=seconds)\n",
|
|
" return t.strftime(fromat_str)\n",
|
|
"\n",
|
|
"time_step_func('2018-10-01 15:16:24')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def time_step_func(x, y):\n",
|
|
" return x + y"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"for m in M: #M is the parameter sweep array\n",
|
|
" # for Pairwise, run below separately\n",
|
|
" for n in N: #N is the total number of Montecarlo runs\n",
|
|
" for t in T: #T[len(T)]-T[0] is the duration of each simulation\n",
|
|
" # Pipeline\n",
|
|
" for b in Behaviors:\n",
|
|
" for M in Mechanisms:\n",
|
|
" for s in States:\n",
|
|
" update(s)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"[{'s1': 2}] + [{'s1': 3}]\n",
|
|
"list(zip([block_gen] * 2, range(2)))\n",
|
|
"len([0,1])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"states_list = [state_dict]\n",
|
|
"configs = generate_config(mechanisms, exogenous_states)\n",
|
|
"def step_func(x, y):\n",
|
|
" return x + y\n",
|
|
"pipeline(states_list, configs, env_processes, step_func)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# def s3step1(step, sL, s, _input):\n",
|
|
"# s['s3'] = s['s3'] \n",
|
|
"# # def s3step2(step, sL, s, _input):\n",
|
|
"# s['s3'] = s['s3'] + 1\n",
|
|
"# def s3step3(step, sL, s, _input):\n",
|
|
"# s['s3'] = s['s3'] * bound_norm_random(proc_one_coef_A, proc_one_coef_B)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# 15 + 28.933333333333334 + 4"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# def multiply(x,y,z):\n",
|
|
"# return x * y * z\n",
|
|
"\n",
|
|
"# # create a new function that multiplies by 2\n",
|
|
"# dbl = partial(partial(multiply,2), multiply, 2)\n",
|
|
"# print(dbl(2))"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.6.5"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|