cadCAD/CAD_Engine.ipynb

307 lines
7.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from fn import op, _\n",
"from itertools import repeat\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"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"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",
"execution_count": 3,
"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": 4,
"metadata": {},
"outputs": [],
"source": [
"# ToDo:\n",
"# Handle case where Mechanisms have no input. Perhaps the sentinel value of 0\n",
"# Mock Simulation SimCADUI Lucid chart "
]
},
{
"cell_type": "code",
"execution_count": 5,
"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']\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(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(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 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",
" \"b1\": b1m1,\n",
" \"b2\": b2m1\n",
" },\n",
" \"states\": {\n",
" \"s1\": s1m1,\n",
" \"s2\": s2m1,\n",
" \"s3\": s3m1\n",
" }\n",
" },\n",
" \"m2\": {\n",
" \"behaviors\": {\n",
" \"b1\": b1m2,\n",
" \"b2\": b2m2\n",
" },\n",
" \"states\": {\n",
" \"s1\": s1m2,\n",
" \"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",
"}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def generate_configs(j):\n",
" return list(\n",
" map(\n",
" lambda x: (\n",
" list(j[x][\"states\"].values()),\n",
" list(j[x][\"behaviors\"].values())\n",
" ), \n",
" j.keys()\n",
" )\n",
" )\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 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(step, sL, last_in_obj, behavior_funcs)\n",
"\n",
" for f in state_funcs:\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",
"# 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(step, states_list, s_conf, b_conf)\n",
" \n",
" step = step_func(step, 1)\n",
" return states_list"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'s1': 2, 's2': 4, 's3': 300}]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"states_list = [state_dict]\n",
"states_list"
]
},
{
"cell_type": "code",
"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": [
"# 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
}