dante's inferno

This commit is contained in:
Joshua E. Jodesty 2018-10-10 21:02:59 -04:00
commit 185bef3910
19 changed files with 2435 additions and 23 deletions

View File

@ -1,11 +1,61 @@
# DiffyQ-SimCAD
# SimCad
If you don't already have it installed, install [pipenv](https://pypi.org/project/pipenv/)
**Dependencies:**
Clone this repo, then
Install [pipenv](https://pypi.org/project/pipenv/)
**Project:**
Example Run File:
`/DiffyQ-SimCAD/test.py`
**User Interface: Simulation Configuration**
Configurations:
```bash
/DiffyQ-SimCAD/ui/config.py
```
**Build Tool & Package Import:**
Build & Install Package locally:
```bash
pip install .
```
* [Package Creation Tutorial](https://python-packaging.readthedocs.io/en/latest/minimal.html)
Import Package & Run:
```python
from engine import run
run.main()
```
**Warning**:
**Do Not** publish this package / software to **Any** software repository **except** [DiffyQ-SimCAD's staging branch](https://github.com/BlockScience/DiffyQ-SimCAD/tree/staging) or its **Fork**
**Jupyter Setup:**
Step 1. Create Virtual Environment:
```bash
cd DiffyQ-SimCAD
pipenv run python -m ipykernel install --user --name DiffyQ-SimCAD --display-name "DiffyQ-SimCAD Env"
```
Step 2. Run Jupter Notebook:
```bash
pipenv run jupyter notebook
```
When you open `TestNotebook.ipynb`, you should see it running on the `DiffyQ-SimCAD Env` Kernel
Step 3. Notebook Management:
Notebook Directory:
`/DiffyQ-SimCAD/notebooks/`
Note:
Notebooks should run on the `DiffyQ-SimCAD Env` kernel.

0
engine/__init__.py Normal file
View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

13
engine/configProcessor.py Normal file
View File

@ -0,0 +1,13 @@
# if beh list empty, repeat 0 x n_states in list
def generate_config(mechanisms, exogenous_states):
es_funcs = [exogenous_states[state] for state in list(exogenous_states.keys())]
config = list(
map(
lambda m: (
list(mechanisms[m]["states"].values()) + es_funcs,
list(mechanisms[m]["behaviors"].values())
),
list(mechanisms.keys())
)
)
return config

View File

@ -0,0 +1,79 @@
from copy import deepcopy
from fn import op, _
def getColResults(step, sL, s, funcs):
return list(map(lambda f: f(step, sL, s), funcs))
def getBehaviorInput(step, sL, s, funcs):
return op.foldr(_ + _)(getColResults(step, sL, s, funcs))
def apply_env_proc(env_processes, state_dict, step):
for state in state_dict.keys():
if state in list(env_processes.keys()):
state_dict[state] = env_processes[state](step)(state_dict[state])
def mech_step(m_step, sL, state_funcs, behavior_funcs, env_processes, t_step):
in_copy, mutatable_copy, out_copy = deepcopy(sL), deepcopy(sL), deepcopy(sL)
last_in_obj, last_mut_obj = in_copy[-1], mutatable_copy[-1]
_input = getBehaviorInput(m_step, sL, last_in_obj, behavior_funcs)
for f in state_funcs:
f(m_step, sL, last_mut_obj, _input)
apply_env_proc(env_processes, last_mut_obj, last_mut_obj['timestamp'])
last_mut_obj["mech_step"], last_mut_obj["time_step"] = m_step, t_step
out_copy.append(last_mut_obj)
del last_in_obj, last_mut_obj, in_copy, mutatable_copy,
return out_copy
def block_gen(states_list, configs, env_processes, t_step):
m_step = 0
states_list_copy = deepcopy(states_list)
genesis_states = states_list_copy[-1]
genesis_states['mech_step'], genesis_states['time_step'] = m_step, t_step
states_list = [genesis_states]
m_step += 1
for config in configs:
s_conf, b_conf = config[0], config[1]
states_list = mech_step(m_step, states_list, s_conf, b_conf, env_processes, t_step)
m_step += 1
t_step += 1
return states_list
def pipeline(states_list, configs, env_processes, time_seq):
time_seq = [x + 1 for x in time_seq]
simulation_list = [states_list]
for time_step in time_seq:
pipeline_run = block_gen(simulation_list[-1], configs, env_processes, time_step)
head, *pipeline_run = pipeline_run
simulation_list.append(pipeline_run)
return simulation_list
def simulation(states_list, configs, env_processes, time_seq, runs):
pipeline_run = []
for run in range(runs):
if run == 0:
head, *tail = pipeline(states_list, configs, env_processes, time_seq)
head[-1]['mech_step'], head[-1]['time_step'] = 0, 0
simulation_list = [head] + tail
pipeline_run += simulation_list
else:
transient_states_list = [pipeline_run[-1][-1]]
head, *tail = pipeline(transient_states_list, configs, env_processes, time_seq)
pipeline_run += tail
return pipeline_run

18
engine/run.py Normal file
View File

@ -0,0 +1,18 @@
from ui.config import state_dict, mechanisms, exogenous_states, env_processes, sim_config
from engine.configProcessor import generate_config
from engine.mechanismExecutor import simulation
from engine.utils import flatten
from tabulate import tabulate
import pandas as pd
def main():
states_list = [state_dict]
configs = generate_config(mechanisms, exogenous_states)
# p = pipeline(states_list, configs, env_processes, range(10))
N = sim_config['N']
r = range(5)
# Dimentions: N x r x mechs
s = simulation(states_list, configs, env_processes, r, N)
result = pd.DataFrame(flatten(s))
print(tabulate(result, headers='keys', tablefmt='psql'))

57
engine/utils.py Normal file
View File

@ -0,0 +1,57 @@
from datetime import datetime, timedelta
from decimal import Decimal
from functools import partial
flatten = lambda l: [item for sublist in l for item in sublist]
def flatmap(f, items):
return list(map(f, items))
def datetime_range(start, end, delta, dt_format='%Y-%m-%d %H:%M:%S'):
reverse_head = end
[start, end] = [datetime.strptime(x, dt_format) for x in [start, end]]
def _datetime_range(start, end, delta):
current = start
while current < end:
yield current
current += delta
reverse_tail = [dt.strftime(dt_format) for dt in _datetime_range(start, end, delta)]
return reverse_tail + [reverse_head]
def last_index(l):
return len(l)-1
def retrieve_state(l, offset):
return l[last_index(l) + offset + 1]
def bound_norm_random(rng, low, high):
# Add RNG Seed
res = rng.normal((high+low)/2,(high-low)/6)
if (res<low or res>high):
res = bound_norm_random(rng, low, high)
return Decimal(res)
def env_proc(trigger_step, update_f):
def env_step_trigger(trigger_step, update_f, step):
if step == trigger_step:
return update_f
else:
return lambda x: x
return partial(env_step_trigger, trigger_step, update_f)
# accept timedelta instead of timedelta params
def time_step(dt_str, dt_format='%Y-%m-%d %H:%M:%S', days=0, minutes=0, seconds=30):
dt = datetime.strptime(dt_str, dt_format)
t = dt + timedelta(days=days, minutes=minutes, seconds=seconds)
return t.strftime(dt_format)
# accept timedelta instead of timedelta params
def ep_time_step(s, dt_str, fromat_str='%Y-%m-%d %H:%M:%S', days=0, minutes=0, seconds=1):
if s['mech_step'] == 0:
return time_step(dt_str, fromat_str, days, minutes, seconds)
else:
return dt_str

View File

@ -29,6 +29,7 @@
"cell_type": "code",
"execution_count": 48,
"metadata": {},
<<<<<<< HEAD:CAD_Engine.ipynb
"outputs": [],
"source": [
"# datetime_range('2018-10-01 01:00:00', '2018-10-01 02:00:00', timedelta(minutes=15))\n",
@ -43,6 +44,8 @@
"cell_type": "code",
"execution_count": 49,
"metadata": {},
=======
>>>>>>> detached-work:notebooks/.ipynb_checkpoints/CAD_Engine-checkpoint.ipynb
"outputs": [],
"source": [
"flatten = lambda l: [item for sublist in l for item in sublist]\n",
@ -70,8 +73,7 @@
"\n",
"def retrieve_state(l, offset):\n",
" return l[last_index(l) + offset + 1]\n",
"# Stochastic Process\n",
"# Input RNG Seed\n",
"\n",
"def bound_norm_random(rng, low, high):\n",
" # Add RNG Seed\n",
" res = rng.normal((high+low)/2,(high-low)/6)\n",
@ -97,6 +99,7 @@
" return time_step(dt_str, dt_format, t_delta)\n",
" else: \n",
" return dt_str\n",
<<<<<<< HEAD:CAD_Engine.ipynb
" \n",
"def shift_time(time_seq, t_delta, dt_format='%Y-%m-%d %H:%M:%S'):\n",
" time_seq = [\n",
@ -105,24 +108,34 @@
" time_seq = time_seq[:-1]\n",
"# x = (datetime.strptime(time_seq[0], dt_format) - t_delta).strftime(dt_format)\n",
" return time_seq"
=======
"\n",
"def round_down(x, fp=TWOPLACES):\n",
" return x.quantize(TWOPLACES, rounding=ROUND_DOWN)"
>>>>>>> detached-work:notebooks/.ipynb_checkpoints/CAD_Engine-checkpoint.ipynb
]
},
{
"cell_type": "code",
<<<<<<< HEAD:CAD_Engine.ipynb
"execution_count": 50,
=======
"execution_count": 3,
>>>>>>> detached-work:notebooks/.ipynb_checkpoints/CAD_Engine-checkpoint.ipynb
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"\n",
"# esceptions:\n",
"# point to line giving problems and pass a sentinel"
]
},
{
"cell_type": "code",
<<<<<<< HEAD:CAD_Engine.ipynb
"execution_count": 51,
=======
"execution_count": 4,
>>>>>>> detached-work:notebooks/.ipynb_checkpoints/CAD_Engine-checkpoint.ipynb
"metadata": {},
"outputs": [],
"source": [
@ -175,14 +188,10 @@
"def es5p2(step, sL, s, _input):\n",
" s['timestamp'] = ep_time_step(s, s['timestamp'], t_delta=timedelta(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 10\n",
"def env_b(x): \n",
" return 10\n",
" \n",
"def what_ever(x): \n",
" return x + 1\n",
"\n",
@ -195,9 +204,9 @@
" 'timestamp': '2018-10-01 15:16:24'\n",
"}\n",
"\n",
"# simulation = {\n",
"# \"s3\": es3p1\n",
"# }\n",
"sim_config = {\n",
" \"N\": 2\n",
"}\n",
"\n",
"exogenous_states = {\n",
" \"s3\": es3p1,\n",
@ -205,8 +214,6 @@
" \"timestamp\": es5p2\n",
"}\n",
"\n",
"# Update time at the end of each pipeline (once per 'block')\n",
"# Enable the acceptance of user defined time\n",
"env_processes = {\n",
" \"s3\": env_proc('2018-10-01 15:16:25', env_a),\n",
" \"s4\": env_proc('2018-10-01 15:16:25', env_b)\n",
@ -248,7 +255,11 @@
},
{
"cell_type": "code",
<<<<<<< HEAD:CAD_Engine.ipynb
"execution_count": 52,
=======
"execution_count": 5,
>>>>>>> detached-work:notebooks/.ipynb_checkpoints/CAD_Engine-checkpoint.ipynb
"metadata": {},
"outputs": [],
"source": [
@ -280,11 +291,9 @@
"# return state_dict\n",
" \n",
"def mech_step(m_step, sL, state_funcs, behavior_funcs, env_processes, t_step):\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",
" for f in state_funcs:\n",
@ -340,12 +349,17 @@
},
{
"cell_type": "code",
<<<<<<< HEAD:CAD_Engine.ipynb
"execution_count": 53,
=======
"execution_count": 6,
>>>>>>> detached-work:notebooks/.ipynb_checkpoints/CAD_Engine-checkpoint.ipynb
"metadata": {},
"outputs": [],
"source": [
"states_list = [state_dict]\n",
"configs = generate_config(mechanisms, exogenous_states)\n",
<<<<<<< HEAD:CAD_Engine.ipynb
"t_delta = timedelta(seconds=1)\n",
"dt_format = '%Y-%m-%d %H:%M:%S'\n",
"time_seq = datetime_range('2018-10-01 15:16:25', '2018-10-01 15:16:30', t_delta)\n",
@ -353,17 +367,30 @@
"# t_step_func = partial(time_step, dt_format='%Y-%m-%d %H:%M:%S', t_delta=timedelta(seconds=1))\n",
"p = pipeline(states_list, configs, env_processes, time_seq, shift_time)\n",
"s = simulation(states_list, configs, env_processes, time_seq, shift_time, 2)\n",
=======
"p = pipeline(states_list, configs, env_processes, range(10))\n",
"N = sim_config['N']\n",
"s = simulation(states_list, configs, env_processes, range(5), N)\n",
>>>>>>> detached-work:notebooks/.ipynb_checkpoints/CAD_Engine-checkpoint.ipynb
"# x = flatten(s)\n",
"# transient_state = p[-1][1]\n",
"# transient_state['mech_step'], transient_state['time_step'] = 0, 0\n",
"# head, *tail = pipeline([transient_state], configs, env_processes, range(1))\n",
<<<<<<< HEAD:CAD_Engine.ipynb
"# head\n",
"# datetime_range('2018-10-01 15:16:24', '2018-10-01 15:16:34', timedelta(seconds=1))"
=======
"# head"
>>>>>>> detached-work:notebooks/.ipynb_checkpoints/CAD_Engine-checkpoint.ipynb
]
},
{
"cell_type": "code",
<<<<<<< HEAD:CAD_Engine.ipynb
"execution_count": 54,
=======
"execution_count": 7,
>>>>>>> detached-work:notebooks/.ipynb_checkpoints/CAD_Engine-checkpoint.ipynb
"metadata": {},
"outputs": [
{
@ -812,7 +839,11 @@
"30 2018-10-01 15:16:30 2018-10-01 15:16:34 "
]
},
<<<<<<< HEAD:CAD_Engine.ipynb
"execution_count": 54,
=======
"execution_count": 7,
>>>>>>> detached-work:notebooks/.ipynb_checkpoints/CAD_Engine-checkpoint.ipynb
"metadata": {},
"output_type": "execute_result"
}
@ -830,7 +861,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
@ -839,7 +870,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
@ -848,10 +879,11 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"metadata": {},
"outputs": [
{
<<<<<<< HEAD:CAD_Engine.ipynb
"ename": "TypeError",
"evalue": "strptime() argument 1 must be str, not int",
"output_type": "error",
@ -865,6 +897,456 @@
"\u001b[0;32m<ipython-input-3-53307d3b1d89>\u001b[0m in \u001b[0;36mtime_step\u001b[0;34m(dt_str, dt_format, t_delta)\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mtime_step\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdt_str\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdt_format\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'%Y-%m-%d %H:%M:%S'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mt_delta\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtimedelta\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mseconds\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 41\u001b[0;31m \u001b[0mdt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstrptime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdt_str\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdt_format\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 42\u001b[0m \u001b[0mt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdt\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mt_delta\u001b[0m \u001b[0;31m#(days=days, minutes=minutes, seconds=seconds)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 43\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstrftime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdt_format\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: strptime() argument 1 must be str, not int"
]
=======
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>mech_step</th>\n",
" <th>s1</th>\n",
" <th>s2</th>\n",
" <th>s3</th>\n",
" <th>s4</th>\n",
" <th>time_step</th>\n",
" <th>timestamp</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>2018-10-01 15:16:24</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>10</td>\n",
" <td>10</td>\n",
" <td>1</td>\n",
" <td>2018-10-01 15:16:25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" <td>8</td>\n",
" <td>8</td>\n",
" <td>10</td>\n",
" <td>10</td>\n",
" <td>1</td>\n",
" <td>2018-10-01 15:16:25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>3</td>\n",
" <td>26</td>\n",
" <td>26</td>\n",
" <td>10</td>\n",
" <td>10</td>\n",
" <td>1</td>\n",
" <td>2018-10-01 15:16:25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1</td>\n",
" <td>80</td>\n",
" <td>80</td>\n",
" <td>10.32600343338698101192107970</td>\n",
" <td>11.77464503140593476970821030</td>\n",
" <td>2</td>\n",
" <td>2018-10-01 15:16:26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>2</td>\n",
" <td>242</td>\n",
" <td>242</td>\n",
" <td>8.219165152747849552053712991</td>\n",
" <td>10.98017733872146348043050973</td>\n",
" <td>2</td>\n",
" <td>2018-10-01 15:16:26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>3</td>\n",
" <td>728</td>\n",
" <td>728</td>\n",
" <td>8.257183331140684599959797063</td>\n",
" <td>11.14555732791249226295457795</td>\n",
" <td>2</td>\n",
" <td>2018-10-01 15:16:26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>1</td>\n",
" <td>2186</td>\n",
" <td>2186</td>\n",
" <td>7.697614183037259884157485079</td>\n",
" <td>11.31602383773220234209463432</td>\n",
" <td>3</td>\n",
" <td>2018-10-01 15:16:27</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>2</td>\n",
" <td>6560</td>\n",
" <td>6560</td>\n",
" <td>6.589589556253206242453558457</td>\n",
" <td>10.11177792894543602078060757</td>\n",
" <td>3</td>\n",
" <td>2018-10-01 15:16:27</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>3</td>\n",
" <td>19682</td>\n",
" <td>19682</td>\n",
" <td>6.935079384210909962341072506</td>\n",
" <td>10.55461981674527424973269651</td>\n",
" <td>3</td>\n",
" <td>2018-10-01 15:16:27</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>1</td>\n",
" <td>59048</td>\n",
" <td>59048</td>\n",
" <td>7.445001607171021964619602297</td>\n",
" <td>12.60113786498380553274154381</td>\n",
" <td>4</td>\n",
" <td>2018-10-01 15:16:28</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>2</td>\n",
" <td>177146</td>\n",
" <td>177146</td>\n",
" <td>6.958656677811361123011555690</td>\n",
" <td>11.30960833968231274880432784</td>\n",
" <td>4</td>\n",
" <td>2018-10-01 15:16:28</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>3</td>\n",
" <td>531440</td>\n",
" <td>531440</td>\n",
" <td>7.544893080763414390539482619</td>\n",
" <td>12.32672489841013251241265104</td>\n",
" <td>4</td>\n",
" <td>2018-10-01 15:16:28</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>1</td>\n",
" <td>1594322</td>\n",
" <td>1594322</td>\n",
" <td>7.257042974416754341292737066</td>\n",
" <td>12.13626855258194099234747637</td>\n",
" <td>5</td>\n",
" <td>2018-10-01 15:16:29</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>2</td>\n",
" <td>4782968</td>\n",
" <td>4782968</td>\n",
" <td>7.305294334086750288753115659</td>\n",
" <td>14.28393577165736567187337373</td>\n",
" <td>5</td>\n",
" <td>2018-10-01 15:16:29</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>3</td>\n",
" <td>14348906</td>\n",
" <td>14348906</td>\n",
" <td>6.502633193673677105757998498</td>\n",
" <td>14.97497594007758912299650180</td>\n",
" <td>5</td>\n",
" <td>2018-10-01 15:16:29</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>1</td>\n",
" <td>43046720</td>\n",
" <td>43046720</td>\n",
" <td>7.532967006460158866830248622</td>\n",
" <td>15.98760837190111498137591899</td>\n",
" <td>6</td>\n",
" <td>2018-10-01 15:16:30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>2</td>\n",
" <td>129140162</td>\n",
" <td>129140162</td>\n",
" <td>5.529612505431635938711521609</td>\n",
" <td>17.01587263683626320687941714</td>\n",
" <td>6</td>\n",
" <td>2018-10-01 15:16:30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>3</td>\n",
" <td>387420488</td>\n",
" <td>387420488</td>\n",
" <td>5.479042748712564957046046498</td>\n",
" <td>17.43971540455384099481071599</td>\n",
" <td>6</td>\n",
" <td>2018-10-01 15:16:30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>1</td>\n",
" <td>1162261466</td>\n",
" <td>1162261466</td>\n",
" <td>5.859901751864631374048367148</td>\n",
" <td>15.00554357874217937378653300</td>\n",
" <td>7</td>\n",
" <td>2018-10-01 15:16:31</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>2</td>\n",
" <td>3486784400</td>\n",
" <td>3486784400</td>\n",
" <td>4.668310334266428533266666371</td>\n",
" <td>17.09380941965055731858258171</td>\n",
" <td>7</td>\n",
" <td>2018-10-01 15:16:31</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>3</td>\n",
" <td>10460353202</td>\n",
" <td>10460353202</td>\n",
" <td>4.579860201618161652723257217</td>\n",
" <td>14.75081393185207749785986453</td>\n",
" <td>7</td>\n",
" <td>2018-10-01 15:16:31</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>1</td>\n",
" <td>31381059608</td>\n",
" <td>31381059608</td>\n",
" <td>4.544495132390953668869140844</td>\n",
" <td>15.10271405806888416660025395</td>\n",
" <td>8</td>\n",
" <td>2018-10-01 15:16:32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>2</td>\n",
" <td>94143178826</td>\n",
" <td>94143178826</td>\n",
" <td>4.919281011778609131178883770</td>\n",
" <td>16.03013712466506066440454624</td>\n",
" <td>8</td>\n",
" <td>2018-10-01 15:16:32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>3</td>\n",
" <td>282429536480</td>\n",
" <td>282429536480</td>\n",
" <td>5.533312023673580051763457041</td>\n",
" <td>14.68695226167930775054172372</td>\n",
" <td>8</td>\n",
" <td>2018-10-01 15:16:32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25</th>\n",
" <td>1</td>\n",
" <td>847288609442</td>\n",
" <td>847288609442</td>\n",
" <td>5.309825828653936981622956333</td>\n",
" <td>14.90000591172573408234847192</td>\n",
" <td>9</td>\n",
" <td>2018-10-01 15:16:33</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>2</td>\n",
" <td>2541865828328</td>\n",
" <td>2541865828328</td>\n",
" <td>4.574670531071372379176513606</td>\n",
" <td>16.64015120873515800905796433</td>\n",
" <td>9</td>\n",
" <td>2018-10-01 15:16:33</td>\n",
" </tr>\n",
" <tr>\n",
" <th>27</th>\n",
" <td>3</td>\n",
" <td>7625597484986</td>\n",
" <td>7625597484986</td>\n",
" <td>5.200135691359001534911972922</td>\n",
" <td>16.60004100605598806616905229</td>\n",
" <td>9</td>\n",
" <td>2018-10-01 15:16:33</td>\n",
" </tr>\n",
" <tr>\n",
" <th>28</th>\n",
" <td>1</td>\n",
" <td>22876792454960</td>\n",
" <td>22876792454960</td>\n",
" <td>5.833452746203938319991329665</td>\n",
" <td>15.12486604838650902185343711</td>\n",
" <td>10</td>\n",
" <td>2018-10-01 15:16:34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>29</th>\n",
" <td>2</td>\n",
" <td>68630377364882</td>\n",
" <td>68630377364882</td>\n",
" <td>5.563944109509313999489022256</td>\n",
" <td>10.71485175557766753591769425</td>\n",
" <td>10</td>\n",
" <td>2018-10-01 15:16:34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>30</th>\n",
" <td>3</td>\n",
" <td>205891132094648</td>\n",
" <td>205891132094648</td>\n",
" <td>5.759176506486725134678752362</td>\n",
" <td>9.673539064064506702922033631</td>\n",
" <td>10</td>\n",
" <td>2018-10-01 15:16:34</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" mech_step s1 s2 \\\n",
"0 0 0 0 \n",
"1 1 2 2 \n",
"2 2 8 8 \n",
"3 3 26 26 \n",
"4 1 80 80 \n",
"5 2 242 242 \n",
"6 3 728 728 \n",
"7 1 2186 2186 \n",
"8 2 6560 6560 \n",
"9 3 19682 19682 \n",
"10 1 59048 59048 \n",
"11 2 177146 177146 \n",
"12 3 531440 531440 \n",
"13 1 1594322 1594322 \n",
"14 2 4782968 4782968 \n",
"15 3 14348906 14348906 \n",
"16 1 43046720 43046720 \n",
"17 2 129140162 129140162 \n",
"18 3 387420488 387420488 \n",
"19 1 1162261466 1162261466 \n",
"20 2 3486784400 3486784400 \n",
"21 3 10460353202 10460353202 \n",
"22 1 31381059608 31381059608 \n",
"23 2 94143178826 94143178826 \n",
"24 3 282429536480 282429536480 \n",
"25 1 847288609442 847288609442 \n",
"26 2 2541865828328 2541865828328 \n",
"27 3 7625597484986 7625597484986 \n",
"28 1 22876792454960 22876792454960 \n",
"29 2 68630377364882 68630377364882 \n",
"30 3 205891132094648 205891132094648 \n",
"\n",
" s3 s4 time_step \\\n",
"0 1 1 0 \n",
"1 10 10 1 \n",
"2 10 10 1 \n",
"3 10 10 1 \n",
"4 10.32600343338698101192107970 11.77464503140593476970821030 2 \n",
"5 8.219165152747849552053712991 10.98017733872146348043050973 2 \n",
"6 8.257183331140684599959797063 11.14555732791249226295457795 2 \n",
"7 7.697614183037259884157485079 11.31602383773220234209463432 3 \n",
"8 6.589589556253206242453558457 10.11177792894543602078060757 3 \n",
"9 6.935079384210909962341072506 10.55461981674527424973269651 3 \n",
"10 7.445001607171021964619602297 12.60113786498380553274154381 4 \n",
"11 6.958656677811361123011555690 11.30960833968231274880432784 4 \n",
"12 7.544893080763414390539482619 12.32672489841013251241265104 4 \n",
"13 7.257042974416754341292737066 12.13626855258194099234747637 5 \n",
"14 7.305294334086750288753115659 14.28393577165736567187337373 5 \n",
"15 6.502633193673677105757998498 14.97497594007758912299650180 5 \n",
"16 7.532967006460158866830248622 15.98760837190111498137591899 6 \n",
"17 5.529612505431635938711521609 17.01587263683626320687941714 6 \n",
"18 5.479042748712564957046046498 17.43971540455384099481071599 6 \n",
"19 5.859901751864631374048367148 15.00554357874217937378653300 7 \n",
"20 4.668310334266428533266666371 17.09380941965055731858258171 7 \n",
"21 4.579860201618161652723257217 14.75081393185207749785986453 7 \n",
"22 4.544495132390953668869140844 15.10271405806888416660025395 8 \n",
"23 4.919281011778609131178883770 16.03013712466506066440454624 8 \n",
"24 5.533312023673580051763457041 14.68695226167930775054172372 8 \n",
"25 5.309825828653936981622956333 14.90000591172573408234847192 9 \n",
"26 4.574670531071372379176513606 16.64015120873515800905796433 9 \n",
"27 5.200135691359001534911972922 16.60004100605598806616905229 9 \n",
"28 5.833452746203938319991329665 15.12486604838650902185343711 10 \n",
"29 5.563944109509313999489022256 10.71485175557766753591769425 10 \n",
"30 5.759176506486725134678752362 9.673539064064506702922033631 10 \n",
"\n",
" timestamp \n",
"0 2018-10-01 15:16:24 \n",
"1 2018-10-01 15:16:25 \n",
"2 2018-10-01 15:16:25 \n",
"3 2018-10-01 15:16:25 \n",
"4 2018-10-01 15:16:26 \n",
"5 2018-10-01 15:16:26 \n",
"6 2018-10-01 15:16:26 \n",
"7 2018-10-01 15:16:27 \n",
"8 2018-10-01 15:16:27 \n",
"9 2018-10-01 15:16:27 \n",
"10 2018-10-01 15:16:28 \n",
"11 2018-10-01 15:16:28 \n",
"12 2018-10-01 15:16:28 \n",
"13 2018-10-01 15:16:29 \n",
"14 2018-10-01 15:16:29 \n",
"15 2018-10-01 15:16:29 \n",
"16 2018-10-01 15:16:30 \n",
"17 2018-10-01 15:16:30 \n",
"18 2018-10-01 15:16:30 \n",
"19 2018-10-01 15:16:31 \n",
"20 2018-10-01 15:16:31 \n",
"21 2018-10-01 15:16:31 \n",
"22 2018-10-01 15:16:32 \n",
"23 2018-10-01 15:16:32 \n",
"24 2018-10-01 15:16:32 \n",
"25 2018-10-01 15:16:33 \n",
"26 2018-10-01 15:16:33 \n",
"27 2018-10-01 15:16:33 \n",
"28 2018-10-01 15:16:34 \n",
"29 2018-10-01 15:16:34 \n",
"30 2018-10-01 15:16:34 "
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
>>>>>>> detached-work:notebooks/.ipynb_checkpoints/CAD_Engine-checkpoint.ipynb
}
],
"source": [
@ -876,18 +1358,53 @@
},
{
"cell_type": "code",
<<<<<<< HEAD:CAD_Engine.ipynb
"execution_count": null,
"metadata": {},
"outputs": [],
=======
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
>>>>>>> detached-work:notebooks/.ipynb_checkpoints/CAD_Engine-checkpoint.ipynb
"source": [
"list(range(2))"
]
},
{
"cell_type": "code",
<<<<<<< HEAD:CAD_Engine.ipynb
"execution_count": null,
"metadata": {},
"outputs": [],
=======
"execution_count": 12,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'non_numeric' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-12-9786f8c4c4ba>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mstate_dict\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m's1'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mDecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'3323'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m's2'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mDecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'2572'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m's3'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mDecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'2.657'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m's4'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mDecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'7.914'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'timestamp'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'2018-10-01 15:16:26'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'mech_step'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'time_step'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mstate_dict\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mquantize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mTWOPLACES\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mROUND_DOWN\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mstate_dict\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mDecimal\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mstate_dict\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnon_numeric\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mstate_dict\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'non_numeric' is not defined"
]
}
],
>>>>>>> detached-work:notebooks/.ipynb_checkpoints/CAD_Engine-checkpoint.ipynb
"source": [
"# Decimal('240.0').quantize(Decimal('0.01'), ROUND_DOWN)\n",
"# round(Decimal('240.01'), 2)\n",

1544
notebooks/CAD_Engine.ipynb Normal file

File diff suppressed because it is too large Load Diff

11
setup.py Normal file
View File

@ -0,0 +1,11 @@
from setuptools import setup
setup(name='SimCAD',
version='0.1',
description='Sim-Cad Enigne',
url='https://github.com/BlockScience/DiffyQ-SimCAD',
author='Joshua E. Jodesty',
author_email='joshua@block.science',
license='MIT',
packages=['engine'],
zip_safe=False)

2
test.py Normal file
View File

@ -0,0 +1,2 @@
from engine import run
run.main()

Binary file not shown.

121
ui/config.py Normal file
View File

@ -0,0 +1,121 @@
from engine.utils import bound_norm_random, ep_time_step, env_proc
import numpy as np
from decimal import Decimal
seed = {
'z': np.random.RandomState(1),
'a': np.random.RandomState(2),
'b': np.random.RandomState(3),
'c': np.random.RandomState(3)
}
# Behaviors per Mechanism
def b1m1(step, sL, s):
return s['s1'] + 1
def b2m1(step, sL, s):
return s['s1'] + 1
def b1m2(step, sL, s):
return s['s1'] + 1
def b2m2(step, sL, s):
return s['s1'] + 1
def b1m3(step, sL, s):
return s['s1'] + 1
def b2m3(step, sL, s):
return s['s2'] + 1
# Internal States per Mechanism
def s1m1(step, sL, s, _input):
s['s1'] = s['s1'] + _input
def s2m1(step, sL, s, _input):
s['s2'] = s['s2'] + _input
def s1m2(step, sL, s, _input):
s['s1'] = s['s1'] + _input
def s2m2(step, sL, s, _input):
s['s2'] = s['s2'] + _input
def s1m3(step, sL, s, _input):
s['s1'] = s['s1'] + _input
def s2m3(step, sL, s, _input):
s['s2'] = s['s2'] + _input
# Exogenous States
proc_one_coef_A = 0.7
proc_one_coef_B = 1.3
def es3p1(step, sL, s, _input):
s['s3'] = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B)
def es4p2(step, sL, s, _input):
s['s4'] = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B)
def es5p2(step, sL, s, _input): # accept timedelta instead of timedelta params
s['timestamp'] = ep_time_step(s, s['timestamp'], seconds=1)
# Environment States
def env_a(x):
return 10
def env_b(x):
return 10
# def what_ever(x):
# return x + 1
# Genesis States
state_dict = {
's1': Decimal(0.0),
's2': Decimal(0.0),
's3': Decimal(1.0),
's4': Decimal(1.0),
'timestamp': '2018-10-01 15:16:24'
}
exogenous_states = {
"s3": es3p1,
"s4": es4p2,
"timestamp": es5p2
}
env_processes = {
"s3": env_proc('2018-10-01 15:16:25', env_a),
"s4": env_proc('2018-10-01 15:16:25', env_b)
}
# test return vs. non-return functions as lambdas
# test fully defined functions
mechanisms = {
"m1": {
"behaviors": {
"b1": b1m1, # lambda step, sL, s: s['s1'] + 1,
"b2": b2m1
},
"states": {
"s1": s1m1,
"s2": s2m1,
}
},
"m2": {
"behaviors": {
"b1": b1m2,
"b2": b2m2
},
"states": {
"s1": s1m2,
"s2": s2m2,
}
},
"m3": {
"behaviors": {
"b1": b1m3,
"b2": b2m3
},
"states": {
"s1": s1m3,
"s2": s2m3,
}
}
}
sim_config = {
"N": 2
}