{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from functools import partial, reduce" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Behaviors per Mechanism\n", "def b1m1(step, sL, s):\n", " return s['s1'] + 1\n", "def b2m1(step, sL, s):\n", " return s['s1'] + 1\n", "\n", "def b1m2(step, sL, s):\n", " return s['s1'] + 1\n", "def b2m2(step, sL, s):\n", " return s['s1'] + 1\n", "\n", "def b1m3(step, sL, s):\n", " return s['s1'] + 1\n", "def b2m3(step, sL, s):\n", " return s['s2'] + 1\n", "\n", "\n", "# Internal States per Mechanism\n", "def s1m1(step, sL, s, _input):\n", " y = 's1'\n", " x = s['s1'] + _input\n", " return (y, x)\n", "def s2m1(step, sL, s, _input):\n", " y = 's2'\n", " x = s['s2'] + _input\n", " return (y, x)\n", "\n", "def s1m2(step, sL, s, _input):\n", " y = 's1'\n", " x = s['s1'] + _input\n", " return (y, x)\n", "def s2m2(step, sL, s, _input):\n", " y = 's2'\n", " x = s['s2'] + _input\n", " return (y, x)\n", "\n", "def s1m3(step, sL, s, _input):\n", " y = 's1'\n", " x = s['s1'] + _input\n", " return (y, x)\n", "def s2m3(step, sL, s, _input):\n", " y = 's2'\n", " x = s['s2'] + _input\n", " return (y, x)\n", "\n", "# Exogenous States\n", "proc_one_coef_A = 0.7\n", "proc_one_coef_B = 1.3\n", "def es3p1(step, sL, s, _input):\n", " y = 's3'\n", " x = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B)\n", " return (y, x)\n", "def es4p2(step, sL, s, _input):\n", " y = 's4'\n", " x = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B)\n", " return (y, x)\n", "def es5p2(step, sL, s, _input): # accept timedelta instead of timedelta params\n", " y = 'timestamp'\n", " x = ep_time_step(s, s['timestamp'], seconds=1)\n", " return (y, x)\n", "\n", "\n", "\n", "# Environment States\n", "def env_a(x):\n", " return 10\n", "def env_b(x):\n", " return 10\n", "\n", "\n", "exogenous_states = {\n", " \"s3\": es3p1,\n", " \"s4\": es4p2,\n", " \"timestamp\": es5p2\n", "}\n", "\n", "ep = list(exogenous_states.values())\n", "\n", "mechanisms = {\n", " \"m1\": {\n", " \"behaviors\": {\n", " \"b1\": b1m1, # lambda step, sL, s: s['s1'] + 1,\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", "}\n", "\n", "\n", "def state_identity(k):\n", " def identity(step, sL, s, _input):\n", " return (k, s[k])\n", " return identity\n", "\n", "def behavior_identity(k):\n", " def identity(step, sL, s):\n", " return s[k]\n", " return identity\n", "\n", "def key_filter(mechanisms, keyname): \n", " return [ v[keyname] for k, v in mechanisms.items() ]\n", "\n", "def fillna_with_id(identity, df, col):\n", " return df[[col]].fillna(value=identity(col))\n", "\n", "def apply_identity_funcs(identity, df, cols):\n", " return list(map(lambda col: fillna_with_id(identity, df, col), cols))\n", "\n", "def create_matrix_field(mechanisms, key):\n", " if key == 'states':\n", " identity = state_identity\n", " else:\n", " identity = behavior_identity\n", " df = pd.DataFrame(key_filter(mechanisms, key))\n", " col_list = apply_identity_funcs(identity, df, list(df.columns))\n", " return reduce((lambda x, y: pd.concat([x, y], axis=1)), col_list)\n", "\n", "def create_tensor_field(mechanisms, env_poc=ep, keys=['behaviors', 'states']):\n", " dfs = [ create_matrix_field(mechanisms, k) for k in keys ]\n", " df = pd.concat(dfs, axis=1)\n", " for es, i in zip(env_poc, range(len(env_poc))):\n", " df['es'+str(i)] = es\n", " df['m'] = df.index + 1\n", " return df\n", "\n", "def generate_config(mechanisms, env_poc=ep):\n", " bdf = create_matrix_field(mechanisms,'behaviors')\n", " sdf = create_matrix_field(mechanisms,'states')\n", " zipped_list = list(zip(sdf.values.tolist(), bdf.values.tolist()))\n", " return list(map(lambda x: (x[0] + env_poc, x[1]), zipped_list))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[,\n", " ],\n", " [,\n", " ],\n", " [.identity(step, sL, s)>,\n", " .identity(step, sL, s)>]]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "create_matrix_field(mechanisms,'behaviors').values.tolist()\n", " \n", "# exdf[exdf.index == 0].values.tolist() #['b1','b2']" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
b1b2s1s2es0es1es2m
0<function b1m1 at 0x10af42158><function b2m1 at 0x10af42268><function s1m1 at 0x10af42510><function s2m1 at 0x10af42598><function es3p1 at 0x10af42840><function es4p2 at 0x10af428c8><function es5p2 at 0x10af42950>1
1<function b1m2 at 0x10af422f0><function b2m2 at 0x10af42400><function s1m2 at 0x10af42620><function state_identity.<locals>.identity at ...<function es3p1 at 0x10af42840><function es4p2 at 0x10af428c8><function es5p2 at 0x10af42950>2
2<function behavior_identity.<locals>.identity ...<function behavior_identity.<locals>.identity ...<function s1m3 at 0x10af42730><function state_identity.<locals>.identity at ...<function es3p1 at 0x10af42840><function es4p2 at 0x10af428c8><function es5p2 at 0x10af42950>3
\n", "
" ], "text/plain": [ " b1 \\\n", "0 \n", "1 \n", "2 .identity ... \n", "\n", " b2 \\\n", "0 \n", "1 \n", "2 .identity ... \n", "\n", " s1 \\\n", "0 \n", "1 \n", "2 \n", "\n", " s2 \\\n", "0 \n", "1 .identity at ... \n", "2 .identity at ... \n", "\n", " es0 es1 \\\n", "0 \n", "1 \n", "2 \n", "\n", " es2 m \n", "0 1 \n", "1 2 \n", "2 3 " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# generate_config(mechanisms, ep)\n", "create_tensor_field(mechanisms)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[([,\n", " ,\n", " ,\n", " ,\n", " ],\n", " [,\n", " ]),\n", " ([,\n", " ,\n", " ,\n", " ],\n", " [,\n", " ]),\n", " ([,\n", " ,\n", " ,\n", " ],\n", " [])]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# def generate_config(mechanisms, exogenous_states):\n", "# es_funcs = list(exogenous_states.values())\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", "# generate_config(mechanisms, exogenous_states)" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
b1b2s1s2es0es1es2m
0<function b1m1 at 0x118771e18><function b2m1 at 0x1188d7d90><function s1m1 at 0x1188d7730><function s2m1 at 0x1188ed730><function es3p1 at 0x1188ed400><function es4p2 at 0x1188ed378><function es5p2 at 0x1188ed158>1
1<function b1m2 at 0x1188d70d0><function b2m2 at 0x1188d7840><function s1m2 at 0x1188ed620><function state_identity.<locals>.identity at ...<function es3p1 at 0x1188ed400><function es4p2 at 0x1188ed378><function es5p2 at 0x1188ed158>2
2<function b1m3 at 0x1188d7ea0><function behavior_identity.<locals>.identity ...<function s1m3 at 0x1188d7a60><function s2m3 at 0x1188ed268><function es3p1 at 0x1188ed400><function es4p2 at 0x1188ed378><function es5p2 at 0x1188ed158>3
\n", "
" ], "text/plain": [ " b1 \\\n", "0 \n", "1 \n", "2 \n", "\n", " b2 \\\n", "0 \n", "1 \n", "2 .identity ... \n", "\n", " s1 \\\n", "0 \n", "1 \n", "2 \n", "\n", " s2 \\\n", "0 \n", "1 .identity at ... \n", "2 \n", "\n", " es0 es1 \\\n", "0 \n", "1 \n", "2 \n", "\n", " es2 m \n", "0 1 \n", "1 2 \n", "2 3 " ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "create_tensor_field(mechanisms)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
b1b2
0<function b1m1 at 0x1108c88c8><function b2m1 at 0x1108c89d8>
1<function b1m2 at 0x1108c8d90><function b2m2 at 0x1108c8a60>
2<function b1m3 at 0x1108c8c80><function behavior_identity.<locals>.identity ...
\n", "
" ], "text/plain": [ " b1 \\\n", "0 \n", "1 \n", "2 \n", "\n", " b2 \n", "0 \n", "1 \n", "2 .identity ... " ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "create_matrix_field(mechanisms,'behaviors')\n", "create_matrix_field(mechanisms,'states')" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
b1b2s1s2es0es1es2m
0<function b1m1 at 0x1105ed9d8><function b2m1 at 0x1105edbf8><function s1m1 at 0x1105c69d8><function s2m1 at 0x1105ede18><function es3p1 at 0x1187710d0><function es4p2 at 0x118771158><function es5p2 at 0x1187711e0>1
1<function b1m2 at 0x1105edc80><function b2m2 at 0x1105edd08><function s1m2 at 0x1105edea0><function state_identity.<locals>.identity at ...<function es3p1 at 0x1187710d0><function es4p2 at 0x118771158><function es5p2 at 0x1187711e0>2
2<function b1m3 at 0x1105edb70><function behavior_identity.<locals>.identity ...<function s1m3 at 0x1105ed0d0><function s2m3 at 0x118771048><function es3p1 at 0x1187710d0><function es4p2 at 0x118771158><function es5p2 at 0x1187711e0>3
\n", "
" ], "text/plain": [ " b1 \\\n", "0 \n", "1 \n", "2 \n", "\n", " b2 \\\n", "0 \n", "1 \n", "2 .identity ... \n", "\n", " s1 \\\n", "0 \n", "1 \n", "2 \n", "\n", " s2 \\\n", "0 \n", "1 .identity at ... \n", "2 \n", "\n", " es0 es1 \\\n", "0 \n", "1 \n", "2 \n", "\n", " es2 m \n", "0 1 \n", "1 2 \n", "2 3 " ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tensor_field_report(mechanisms)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('a', 0)\n", "('b', 1)\n", "('c', 2)\n" ] } ], "source": [ "\n", "for x, i in zip(['a', 'b', 'c'], range(3)):\n", " print((x, i))" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(env_poc)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def create_tensor_field2(mechanisms, env_poc=ep):\n", " beh_df = create_matrix_field(mechanisms, 'behaviors')\n", " state_df = create_matrix_field(mechanisms, 'states')\n", " ep_df = pd.DataFrame({'m' : range(len(env_poc))})\n", " for es, i in zip(env_poc, range(len(env_poc))):\n", " ep_df['es'+str(i)] = es\n", " \n", " return beh_df" ] } ], "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 }