849 lines
28 KiB
Plaintext
849 lines
28 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": 24,
|
|
"metadata": {
|
|
"scrolled": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"2\n",
|
|
"28.93333333333333333333333333\n",
|
|
"20478.13710603775397375912083\n",
|
|
"2099481908.456319610782972145\n",
|
|
"20278528742469494677.57634624\n",
|
|
"1.877325257150165424550244514E+39\n",
|
|
"1.607776799724788040183428838E+79\n",
|
|
"1.179145751689312862767420288E+159\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"([{'s1': Decimal('2'),\n",
|
|
" 's2': Decimal('4'),\n",
|
|
" 's3': Decimal('0'),\n",
|
|
" 's4': Decimal('0'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:24',\n",
|
|
" 'mech_step': 0,\n",
|
|
" 'time_step': 0}],\n",
|
|
" [{'s1': Decimal('14'),\n",
|
|
" 's2': Decimal('15'),\n",
|
|
" 's3': Decimal('1.050839624268343097668321207'),\n",
|
|
" 's4': Decimal('2.441733141317365962663643586'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:25',\n",
|
|
" 'mech_step': 1,\n",
|
|
" 'time_step': 1},\n",
|
|
" {'s1': Decimal('28.93333333333333333333333333'),\n",
|
|
" 's2': Decimal('15'),\n",
|
|
" 's3': Decimal('2.095161551176761634794792378'),\n",
|
|
" 's4': Decimal('5.113659156229101397052573169'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:25',\n",
|
|
" 'mech_step': 2,\n",
|
|
" 'time_step': 1},\n",
|
|
" {'s1': Decimal('28.93333333333333333333333333'),\n",
|
|
" 's2': Decimal('323.9333333333333333333333333'),\n",
|
|
" 's3': Decimal('2.519890991569726736568136605'),\n",
|
|
" 's4': Decimal('6.560321793796678631321742758'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:25',\n",
|
|
" 'mech_step': 3,\n",
|
|
" 'time_step': 1}],\n",
|
|
" [{'s1': Decimal('10238.54222222222222222222222'),\n",
|
|
" 's2': Decimal('9726.337777777777777777777775'),\n",
|
|
" 's3': Decimal('2.414227808970399960946867000'),\n",
|
|
" 's4': Decimal('7.247940290566966932609850879'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:26',\n",
|
|
" 'mech_step': 1,\n",
|
|
" 'time_step': 2},\n",
|
|
" {'s1': Decimal('20478.13710603775397375912083'),\n",
|
|
" 's2': Decimal('9726.337777777777777777777775'),\n",
|
|
" 's3': Decimal('2.382280598316759442817818771'),\n",
|
|
" 's4': Decimal('8.214625498425979768450058718'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:26',\n",
|
|
" 'mech_step': 2,\n",
|
|
" 'time_step': 2},\n",
|
|
" {'s1': Decimal('20478.13710603775397375912083'),\n",
|
|
" 's2': Decimal('30782.40821714886508487023194'),\n",
|
|
" 's3': Decimal('2.372853856887477324389517831'),\n",
|
|
" 's4': Decimal('8.052456732708285471678171806'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:26',\n",
|
|
" 'mech_step': 3,\n",
|
|
" 'time_step': 2}],\n",
|
|
" [{'s1': Decimal('1049740953.395583992944188109'),\n",
|
|
" 's2': Decimal('630417637.4701208244714003488'),\n",
|
|
" 's3': Decimal('2.450209707314563093303496856'),\n",
|
|
" 's4': Decimal('9.481481965839488087100214227'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:27',\n",
|
|
" 'mech_step': 1,\n",
|
|
" 'time_step': 3},\n",
|
|
" {'s1': Decimal('2099481908.456319610782972145'),\n",
|
|
" 's2': Decimal('630417637.4701208244714003488'),\n",
|
|
" 's3': Decimal('1.950287773309288650377474974'),\n",
|
|
" 's4': Decimal('8.841740293751859296135111327'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:27',\n",
|
|
" 'mech_step': 2,\n",
|
|
" 'time_step': 3},\n",
|
|
" {'s1': Decimal('2099481908.456319610782972145'),\n",
|
|
" 's2': Decimal('2729930685.334657584119457364'),\n",
|
|
" 's3': Decimal('1.959308931432416130120888268'),\n",
|
|
" 's4': Decimal('8.974911814493511615884310915'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:27',\n",
|
|
" 'mech_step': 3,\n",
|
|
" 'time_step': 3}],\n",
|
|
" [{'s1': Decimal('10139264371234747337.90364286'),\n",
|
|
" 's2': Decimal('5731440090029288025.821169394'),\n",
|
|
" 's3': Decimal('1.826531350305170994714191335'),\n",
|
|
" 's4': Decimal('9.112179233963412364421569386'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:28',\n",
|
|
" 'mech_step': 1,\n",
|
|
" 'time_step': 4},\n",
|
|
" {'s1': Decimal('20278528742469494677.57634624'),\n",
|
|
" 's2': Decimal('5731440090029288025.821169394'),\n",
|
|
" 's3': Decimal('1.563613299386086065297790265'),\n",
|
|
" 's4': Decimal('8.142465426358771934611814063'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:28',\n",
|
|
" 'mech_step': 2,\n",
|
|
" 'time_step': 4},\n",
|
|
" {'s1': Decimal('20278528742469494677.57634624'),\n",
|
|
" 's2': Decimal('26009968835228714302.73217321'),\n",
|
|
" 's3': Decimal('1.645592986464568891773823198'),\n",
|
|
" 's4': Decimal('8.499061940452677383673350698'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:28',\n",
|
|
" 'mech_step': 3,\n",
|
|
" 'time_step': 4}],\n",
|
|
" [{'s1': Decimal('9.386626285750827122751222570E+38'),\n",
|
|
" 's2': Decimal('5.274439006159212871100158803E+38'),\n",
|
|
" 's3': Decimal('1.766590077810922682661611754'),\n",
|
|
" 's4': Decimal('10.14701174406741548094852163'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:29',\n",
|
|
" 'mech_step': 1,\n",
|
|
" 'time_step': 5},\n",
|
|
" {'s1': Decimal('1.877325257150165424550244514E+39'),\n",
|
|
" 's2': Decimal('5.274439006159212871100158803E+38'),\n",
|
|
" 's3': Decimal('1.651187533670048789111573805'),\n",
|
|
" 's4': Decimal('9.107013181916861331280610696'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:29',\n",
|
|
" 'mech_step': 2,\n",
|
|
" 'time_step': 5},\n",
|
|
" {'s1': Decimal('1.877325257150165424550244514E+39'),\n",
|
|
" 's2': Decimal('2.404769157766086711686270363E+39'),\n",
|
|
" 's3': Decimal('1.790292864649310878443840252'),\n",
|
|
" 's4': Decimal('9.926041890044552780550681046'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:29',\n",
|
|
" 'mech_step': 3,\n",
|
|
" 'time_step': 5}],\n",
|
|
" [{'s1': Decimal('8.038883998623940200917144191E+78'),\n",
|
|
" 's2': Decimal('4.514533877490005463421990753E+78'),\n",
|
|
" 's3': Decimal('1.721990241144296597165634041'),\n",
|
|
" 's4': Decimal('9.772677741619428942163366001'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:30',\n",
|
|
" 'mech_step': 1,\n",
|
|
" 'time_step': 6},\n",
|
|
" {'s1': Decimal('1.607776799724788040183428838E+79'),\n",
|
|
" 's2': Decimal('4.514533877490005463421990753E+78'),\n",
|
|
" 's3': Decimal('1.733439583633598073067015745'),\n",
|
|
" 's4': Decimal('11.50207747740550731873114995'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:30',\n",
|
|
" 'mech_step': 2,\n",
|
|
" 'time_step': 6},\n",
|
|
" {'s1': Decimal('1.607776799724788040183428838E+79'),\n",
|
|
" 's2': Decimal('2.059230187473788586525627913E+79'),\n",
|
|
" 's3': Decimal('1.542979825353311903430476680'),\n",
|
|
" 's4': Decimal('12.05853458308223690328902863'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:30',\n",
|
|
" 'mech_step': 3,\n",
|
|
" 'time_step': 6}],\n",
|
|
" [{'s1': Decimal('5.895728758446564313837101442E+158'),\n",
|
|
" 's2': Decimal('3.310782520713283121912590559E+158'),\n",
|
|
" 's3': Decimal('1.787462981508510228113852341'),\n",
|
|
" 's4': Decimal('12.87395246742184837067844430'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:31',\n",
|
|
" 'mech_step': 1,\n",
|
|
" 'time_step': 7},\n",
|
|
" {'s1': Decimal('1.179145751689312862767420288E+159'),\n",
|
|
" 's2': Decimal('3.310782520713283121912590559E+158'),\n",
|
|
" 's3': Decimal('1.312096236060668399279110012'),\n",
|
|
" 's4': Decimal('13.70195781773988542185573409'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:31',\n",
|
|
" 'mech_step': 2,\n",
|
|
" 'time_step': 7},\n",
|
|
" {'s1': Decimal('1.179145751689312862767420288E+159'),\n",
|
|
" 's2': Decimal('1.510224003760641174958679344E+159'),\n",
|
|
" 's3': Decimal('1.300096771833397499282040225'),\n",
|
|
" 's4': Decimal('14.04325537259159774263686231'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:31',\n",
|
|
" 'mech_step': 3,\n",
|
|
" 'time_step': 7}],\n",
|
|
" [{'s1': Decimal('3.171158921860539561899348910E+318'),\n",
|
|
" 's2': Decimal('1.780774218133584893940497220E+318'),\n",
|
|
" 's3': Decimal('1.390469047289367567385951761'),\n",
|
|
" 's4': Decimal('12.08314904185900429022556786'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:32',\n",
|
|
" 'mech_step': 1,\n",
|
|
" 'time_step': 8},\n",
|
|
" {'s1': Decimal('6.342317843721079123798697820E+318'),\n",
|
|
" 's2': Decimal('1.780774218133584893940497220E+318'),\n",
|
|
" 's3': Decimal('1.107721818181176344138493920'),\n",
|
|
" 's4': Decimal('13.76471607488974402351461499'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:32',\n",
|
|
" 'mech_step': 2,\n",
|
|
" 'time_step': 8},\n",
|
|
" {'s1': Decimal('6.342317843721079123798697820E+318'),\n",
|
|
" 's2': Decimal('8.123092061854664017739195040E+318'),\n",
|
|
" 's3': Decimal('1.086733894341511439510583758'),\n",
|
|
" 's4': Decimal('11.87802909584694378791861784'),\n",
|
|
" 'timestamp': '2018-10-01 15:16:32',\n",
|
|
" 'mech_step': 3,\n",
|
|
" 'time_step': 8}])"
|
|
]
|
|
},
|
|
"execution_count": 24,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"configs = generate_config(mechanisms, exogenous_states)\n",
|
|
"\n",
|
|
"pipeline(states_list, configs, env_processes, range(8))\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
|
|
}
|