refactored 2

This commit is contained in:
Joshua E. Jodesty 2018-09-25 18:01:16 -04:00
parent c19226246a
commit 6fd6dbe251
1 changed files with 139 additions and 39 deletions

View File

@ -21,7 +21,17 @@
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": []
"source": [
"def last_index(l):\n",
" return len(l)-1\n",
"def retrieve_state(l, offset):\n",
" return l[last_index(l) + offset + 1]\n",
"def bound_norm_random(low, high):\n",
" res = np.random.normal((high+low)/2,(high-low)/6)\n",
" if (res<low or res>high):\n",
" res = bound_norm_random(low, high)\n",
" return res"
]
},
{
"cell_type": "code",
@ -42,7 +52,8 @@
"outputs": [],
"source": [
"# ToDo:\n",
"# Handle case where Mechanisms have no input. Perhaps the sentinel value of 0"
"# Handle case where Mechanisms have no input. Perhaps the sentinel value of 0\n",
"# Mock Simulation SimCADUI Lucid chart "
]
},
{
@ -51,32 +62,52 @@
"metadata": {},
"outputs": [],
"source": [
"# UI Behavior Mechanisms per Mechanism\n",
"def b1m1(s):\n",
"# UI Behaviors per Mechanism\n",
"def b1m1(step, sL, s):\n",
" return s['s1']\n",
"def b2m1(s):\n",
"def b2m1(step, sL, s):\n",
" return s['s1'] * s['s2']\n",
"\n",
"def b1m2(s):\n",
"def b1m2(step, sL, s):\n",
" return s['s1']\n",
"def b2m2(s):\n",
"def b2m2(step, sL, s):\n",
" return s['s1'] / s['s2']\n",
"\n",
"# UI State Mechanisms per Mechanism\n",
"def s1m1(s, _input):\n",
"def b1m3(step, sL, s):\n",
" return s['s1']\n",
"def b2m3(step, sL, s):\n",
" ps = retrieve_state(sL, -3)\n",
" return ps['s2']\n",
"\n",
"# UI States per Mechanism\n",
"def s1m1(step, sL, s, _input):\n",
" s['s1'] = s['s1']**2 + _input\n",
"def s2m1(s, _input):\n",
" s['s2'] = s['s2'] + 1 + _input \n",
"def s3m1(s, _input):\n",
"def s2m1(step, sL, s, _input):\n",
" s['s2'] = s['s2'] + 1 + _input\n",
"\n",
"\n",
"def s3m1(step, sL, s, _input):\n",
" s['s3'] = s['s3']\n",
"\n",
"def s1m2(s, _input):\n",
"def s1m2(step, sL, s, _input):\n",
" s['s1'] = s['s1'] + _input\n",
"def s2m2(s, _input):\n",
"def s2m2(step, sL, s, _input):\n",
" s['s2'] = s['s2']\n",
"def s3m2(s, _input):\n",
" \n",
"def s3m2(step, sL, s, _input):\n",
" s['s3'] = s['s3'] + 1\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",
"state_dict = {\n",
" 's1': 2,\n",
" 's2': 4,\n",
" 's3': 300\n",
"}\n",
" \n",
"j = {\n",
" \"m1\": {\n",
" \"behaviors\": {\n",
@ -99,6 +130,16 @@
" \"s2\": s2m2,\n",
" \"s3\": s3m2\n",
" }\n",
" },\n",
" \"m3\": {\n",
" \"behaviors\": {\n",
" \"b1\": b1m3,\n",
" \"b2\": b2m3\n",
" },\n",
" \"states\": {\n",
" \"s1\": s1m3,\n",
" \"s2\": s2m3\n",
" }\n",
" }\n",
"}"
]
@ -120,28 +161,41 @@
" )\n",
" )\n",
"\n",
"def getColResults(s, funcs):\n",
" return list(map(lambda f: f(s), funcs))\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(s, funcs): \n",
" return op.foldr(_ + _)(getColResults(s, funcs))\n",
"def getBehaviorInput(step, sL, s, funcs): \n",
" return op.foldr(_ + _)(getColResults(step, sL, s, funcs))\n",
"\n",
"def mech_step(sL, state_funcs, behavior_funcs):\n",
" in_copy, out_copy, mutatable_copy = deepcopy(sL), deepcopy(sL), deepcopy(sL)\n",
"def mech_step(step, sL, state_funcs, behavior_funcs):\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",
" _input = getBehaviorInput(last_in_obj, behavior_funcs)\n",
" _input = getBehaviorInput(step, sL, last_in_obj, behavior_funcs)\n",
"\n",
" for f in state_funcs:\n",
" f(last_mut_obj, _input)\n",
" \n",
" out_copy.append(last_mut_obj)\n",
" f(step, sL, last_mut_obj, _input)\n",
" \n",
" last_mut_obj[\"step\"] = step\n",
" out_copy.append(last_mut_obj) \n",
" return out_copy\n",
"\n",
"def pipeline(states_list, configs):\n",
"# isolate ALL step logic within 'step_func'\n",
"def pipeline(states_list, configs, step_func):\n",
" step = 0\n",
" genesis_states = states_list[-1]\n",
" genesis_states['step'] = step\n",
" states_list = [genesis_states]\n",
" \n",
" step = step_func(step, 1)\n",
" for config in configs:\n",
" s_conf, b_conf = config[0], config[1]\n",
" states_list = mech_step(states_list, s_conf, b_conf)\n",
" states_list = mech_step(step, states_list, s_conf, b_conf)\n",
" \n",
" step = step_func(step, 1)\n",
" return states_list"
]
},
@ -153,9 +207,7 @@
{
"data": {
"text/plain": [
"[{'s1': 2, 's2': 4, 's3': 300},\n",
" {'s1': 14, 's2': 15, 's3': 300},\n",
" {'s1': 28.933333333333334, 's2': 15, 's3': 301}]"
"[{'s1': 2, 's2': 4, 's3': 300}]"
]
},
"execution_count": 7,
@ -164,22 +216,70 @@
}
],
"source": [
"state_dict = {\n",
" 's1': 2,\n",
" 's2': 4,\n",
" 's3': 300\n",
"}\n",
"states_list = [state_dict]\n",
"configs = generate_configs(j)\n",
"pipeline(states_list, configs)"
"states_list"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'s1': 2, 's2': 4, 's3': 300, 'step': 0},\n",
" {'s1': 14, 's2': 15, 's3': 300, 'step': 1},\n",
" {'s1': 28.933333333333334, 's2': 15, 's3': 301, 'step': 2},\n",
" {'s1': 28.933333333333334, 's2': 47.93333333333334, 's3': 301, 'step': 3}]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"configs = generate_configs(j)\n",
"def step_func(x, y):\n",
" return x + y\n",
"pipeline(states_list, configs, step_func)\n",
"# categorize by simulation"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"47.93333333333334"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"15 + 28.933333333333334 + 4"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": []
"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": {