Compare commits

..

No commits in common. "master" and "param-sweep-multi-threaded" have entirely different histories.

161 changed files with 713614 additions and 16963 deletions

26
.gitignore vendored
View File

@ -1,30 +1,12 @@
.idea
jupyter notebook
.ipynb_checkpoints .ipynb_checkpoints
.DS_Store .DS_Store
.idea .idea
.pytest_cache/ notebooks/.ipynb_checkpoints
notebooks notebooks/multithreading.ipynb
*.egg-info SimCAD.egg-info
__pycache__ __pycache__
Pipfile Pipfile
Pipfile.lock Pipfile.lock
results results
.mypy_cache .mypy_cache
*.csv simulations/scrapbox
simulations/.ipynb_checkpoints
simulations/validation/config3.py
cadCAD.egg-info
build
cadCAD.egg-info
testing/example.py
testing/example2.py
testing/multi_config_test.py
testing/udo.py
testing/udo_test.py
Simulation.md
monkeytype.sqlite3

View File

@ -1,29 +0,0 @@
Authors
=======
cadCAD was originally implemented by Joshua E. Jodesty and designed by Michael Zargham, Markus B. Koch, and
Matthew V. Barlin from 2018 to 2019.
Project Maintainers:
- Joshua E. Jodesty <joshua@block.science, joshua.jodesty@gmail.com>
- Markus B. Koch <markus@block.science>
Contributors:
- Joshua E. Jodesty
- Markus B. Koch
- Matthew V. Barlin
- Michael Zargham
- Zixuan Zhang
- Charles Rice
Wed also like to thank:
- Andrew Clark
- Nikhil Jamdade
- Nick Hirannet
- Jonathan Gabler
- Chris Frazier
- Harry Goodnight
- Charlie Hoppes

View File

@ -1,21 +0,0 @@
# Contributing to cadCAD (Draft)
:+1::tada: First off, thanks for taking the time to contribute! :tada::+1:
The following is a set of guidelines for contributing to cadCAD. These are mostly guidelines, not rules.
Use your best judgment, and feel free to propose changes to this document in a pull request.
### Pull Requests:
Pull Request (PR) presented as "->".
General Template:
fork/branch -> BlockScience/staging
Contributing a new feature:
fork/feature -> BlockScience/staging
Contributing to an existing feature:
fork/feature -> BlockScience/feature
Thanks! :heart:

View File

@ -1,21 +0,0 @@
MIT License
Copyright (c) 2018-2019 BlockScience
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

119
README.md
View File

@ -1,50 +1,89 @@
``` # SimCad
__________ ____
________ __ _____/ ____/ | / __ \
/ ___/ __` / __ / / / /| | / / / /
/ /__/ /_/ / /_/ / /___/ ___ |/ /_/ /
\___/\__,_/\__,_/\____/_/ |_/_____/
by BlockScience
======================================
Complex Adaptive Dynamics
o i e
m d s
p e i
u d g
t n
e
r
```
***cadCAD*** is a Python package that assists in the processes of designing, testing and validating complex systems through simulation, with support for Monte Carlo methods, A/B testing and parameter sweeping.
# Getting Started **Dependencies:**
## 1. Installation:
Requires [Python 3](https://www.python.org/downloads/)
**Option A: Install Using [pip](https://pypi.org/project/cadCAD/)**
```bash ```bash
pip3 install cadCAD pip install -r requirements.txt
``` ```
**Option B:** Build From Source **Project:**
```
pip3 install -r requirements.txt Example Runs:
python3 setup.py sdist bdist_wheel `/simulations/sim_test.py`
pip3 install dist/*.whl
Example Configurations:
`/simulations/validation/`
**User Interface: Simulation Configuration**
Configurations:
```bash
/DiffyQ-SimCAD/ui/config.py
``` ```
**Build Tool & Package Import:**
## 2. Learn the basics Step 1. Build & Install Package locally:
**Tutorials:** available both as [Jupyter Notebooks](tutorials) ```bash
and [videos](https://www.youtube.com/watch?v=uJEiYHRWA9g&list=PLmWm8ksQq4YKtdRV-SoinhV6LbQMgX1we) pip install .
pip install -e .
```
* [Package Creation Tutorial](https://python-packaging.readthedocs.io/en/latest/minimal.html)
Familiarize yourself with some system modelling concepts and cadCAD terminology. Step 2. Import Package & Run:
```python
import pandas as pd
from tabulate import tabulate
## 3. Documentation: # The following imports NEED to be in the exact same order
* [System Model Configuration](documentation) from SimCAD.engine import ExecutionMode, ExecutionContext, Executor
* [System Simulation Execution](documentation/Simulation_Execution.md) from simulations.validation import config1, config2
* [Policy Aggregation](documentation/Policy_Aggregation.md) from SimCAD import configs
* [System Model Parameter Sweep](documentation/System_Model_Parameter_Sweep.md)
## 4. Connect # ToDo: pass ExecutionContext with execution method as ExecutionContext input
Find other cadCAD users at our [Discourse](https://community.cadcad.org/). We are a small but rapidly growing community.
exec_mode = ExecutionMode()
exec_mode = ExecutionMode()
print("Simulation Execution 1")
print()
first_config = [configs[0]] # from config1
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
run1 = Executor(exec_context=single_proc_ctx, configs=first_config)
run1_raw_result, tensor_field = run1.main()
result = pd.DataFrame(run1_raw_result)
# result.to_csv('~/Projects/DiffyQ-SimCAD/results/config4.csv', sep=',')
print()
print("Tensor Field:")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()
print("Simulation Execution 2: Pairwise Execution")
print()
multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc)
run2 = Executor(exec_context=multi_proc_ctx, configs=configs)
for raw_result, tensor_field in run2.main():
result = pd.DataFrame(raw_result)
print()
print("Tensor Field:")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()
```
Same can be run in Jupyter .
```bash
jupyter notebook
```
Notebooks Directory:
`/DiffyQ-SimCAD/notebooks/`
**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**

1
SimCAD/__init__.py Normal file
View File

@ -0,0 +1 @@
configs = []

View File

@ -0,0 +1,101 @@
from functools import reduce
from fn.op import foldr
import pandas as pd
from SimCAD.utils import key_filter
from SimCAD.configuration.utils.behaviorAggregation import dict_elemwise_sum
class Configuration:
def __init__(self, sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms, behavior_ops=[foldr(dict_elemwise_sum())]):
self.sim_config = sim_config
self.state_dict = state_dict
self.seed = seed
self.exogenous_states = exogenous_states
self.env_processes = env_processes
self.behavior_ops = behavior_ops
self.mechanisms = mechanisms
class Identity:
def __init__(self, behavior_id={'identity': 0}):
self.beh_id_return_val = behavior_id
def b_identity(self, step, sL, s):
return self.beh_id_return_val
def behavior_identity(self, k):
return self.b_identity
def no_state_identity(self, step, sL, s, _input):
return None
def state_identity(self, k):
return lambda step, sL, s, _input: (k, s[k])
def apply_identity_funcs(self, identity, df, cols):
def fillna_with_id_func(identity, df, col):
return df[[col]].fillna(value=identity(col))
return list(map(lambda col: fillna_with_id_func(identity, df, col), cols))
class Processor:
def __init__(self, id=Identity()):
self.id = id
self.b_identity = id.b_identity
self.behavior_identity = id.behavior_identity
self.no_state_identity = id.no_state_identity
self.state_identity = id.state_identity
self.apply_identity_funcs = id.apply_identity_funcs
# Make returntype chosen by user.
def create_matrix_field(self, mechanisms, key):
if key == 'states':
identity = self.state_identity
elif key == 'behaviors':
identity = self.behavior_identity
df = pd.DataFrame(key_filter(mechanisms, key))
col_list = self.apply_identity_funcs(identity, df, list(df.columns))
if len(col_list) != 0:
return reduce((lambda x, y: pd.concat([x, y], axis=1)), col_list)
else:
return pd.DataFrame({'empty': []})
# Maybe Refactor to only use dictionary BUT I used dfs to fill NAs. Perhaps fill
def generate_config(self, state_dict, mechanisms, exo_proc):
# ToDo: include False / False case
# ToDo: Use Range multiplier instead for loop iterator
def no_update_handler(bdf, sdf):
if (bdf.empty == False) and (sdf.empty == True):
bdf_values = bdf.values.tolist()
sdf_values = [[self.no_state_identity] * len(bdf_values) for m in range(len(mechanisms))]
return sdf_values, bdf_values
elif (bdf.empty == True) and (sdf.empty == False):
sdf_values = sdf.values.tolist()
bdf_values = [[self.b_identity] * len(sdf_values) for m in range(len(mechanisms))]
return sdf_values, bdf_values
else:
sdf_values = sdf.values.tolist()
bdf_values = bdf.values.tolist()
return sdf_values, bdf_values
def only_ep_handler(state_dict):
sdf_functions = [
lambda step, sL, s, _input: (k, v) for k, v in zip(state_dict.keys(), state_dict.values())
]
sdf_values = [sdf_functions]
bdf_values = [[self.b_identity] * len(sdf_values)]
return sdf_values, bdf_values
if len(mechanisms) != 0:
bdf = self.create_matrix_field(mechanisms, 'behaviors')
sdf = self.create_matrix_field(mechanisms, 'states')
sdf_values, bdf_values = no_update_handler(bdf, sdf)
zipped_list = list(zip(sdf_values, bdf_values))
else:
sdf_values, bdf_values = only_ep_handler(state_dict)
zipped_list = list(zip(sdf_values, bdf_values))
return list(map(lambda x: (x[0] + exo_proc, x[1]), zipped_list))

View File

@ -0,0 +1,77 @@
from datetime import datetime, timedelta
from decimal import Decimal
from fn.func import curried
import pandas as pd
from pathos.threading import ThreadPool
from SimCAD.utils import groupByKey
class TensorFieldReport:
def __init__(self, config_proc):
self.config_proc = config_proc
# ??? dont for-loop to apply exo_procs, use exo_proc struct
def create_tensor_field(self, mechanisms, exo_proc, keys=['behaviors', 'states']):
dfs = [self.config_proc.create_matrix_field(mechanisms, k) for k in keys]
df = pd.concat(dfs, axis=1)
for es, i in zip(exo_proc, range(len(exo_proc))):
df['es' + str(i + 1)] = es
df['m'] = df.index + 1
return df
# def s_update(y, x):
# return lambda step, sL, s, _input: (y, x)
#
#
def state_update(y, x):
return lambda step, sL, s, _input: (y, x)
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)
@curried
def proc_trigger(trigger_step, update_f, step):
if step == trigger_step:
return update_f
else:
return lambda x: x
# accept timedelta instead of timedelta params
t_delta = timedelta(days=0, minutes=0, seconds=30)
def time_step(dt_str, dt_format='%Y-%m-%d %H:%M:%S', _timedelta = t_delta):
dt = datetime.strptime(dt_str, dt_format)
t = dt + _timedelta
return t.strftime(dt_format)
# accept timedelta instead of timedelta params
t_delta = timedelta(days=0, minutes=0, seconds=1)
def ep_time_step(s, dt_str, fromat_str='%Y-%m-%d %H:%M:%S', _timedelta = t_delta):
if s['mech_step'] == 0:
return time_step(dt_str, fromat_str, _timedelta)
else:
return dt_str
def exo_update_per_ts(ep):
@curried
def ep_decorator(fs, y, step, sL, s, _input):
# print(s)
if s['mech_step'] + 1 == 1: # inside f body to reduce performance costs
if isinstance(fs, list):
pool = ThreadPool(nodes=len(fs))
fx = pool.map(lambda f: f(step, sL, s, _input), fs)
return groupByKey(fx)
else:
return fs(step, sL, s, _input)
else:
return (y, s[y])
return {es: ep_decorator(f, es) for es, f in ep.items()}

View File

@ -13,8 +13,8 @@ def get_base_value(x):
return 0 return 0
def policy_to_dict(v): def behavior_to_dict(v):
return dict(list(zip(map(lambda n: 'p' + str(n + 1), list(range(len(v)))), v))) return dict(list(zip(map(lambda n: 'b' + str(n + 1), list(range(len(v)))), v)))
add = lambda a, b: a + b add = lambda a, b: a + b
@ -28,7 +28,8 @@ def foldr_dict_vals(f, d):
def sum_dict_values(): def sum_dict_values():
return foldr_dict_vals(add) return foldr_dict_vals(add)
# AttributeError: 'int' object has no attribute 'keys'
# config7c
@curried @curried
def dict_op(f, d1, d2): def dict_op(f, d1, d2):
def set_base_value(target_dict, source_dict, key): def set_base_value(target_dict, source_dict, key):
@ -41,5 +42,9 @@ def dict_op(f, d1, d2):
return {k: f(set_base_value(d1, d2, k), set_base_value(d2, d1, k)) for k in key_set} return {k: f(set_base_value(d1, d2, k), set_base_value(d2, d1, k)) for k in key_set}
def dict_elemwise_sum(): def dict_elemwise_sum():
return dict_op(add) return dict_op(add)
# class BehaviorAggregation:

80
SimCAD/engine/__init__.py Normal file
View File

@ -0,0 +1,80 @@
from pathos.multiprocessing import ProcessingPool as Pool
from SimCAD.utils import flatten
from SimCAD.configuration import Processor
from SimCAD.configuration.utils import TensorFieldReport
from SimCAD.engine.simulation import Executor as SimExecutor
class ExecutionMode:
single_proc = 'single_proc'
multi_proc = 'multi_proc'
# ToDo: switch / rename self.name & context ??
class ExecutionContext:
def __init__(self, context=ExecutionMode.multi_proc):
self.name = context
self.method = None
def single_proc_exec(simulation_execs, states_lists, configs_structs, env_processes_list, Ts, Ns):
l = [simulation_execs, states_lists, configs_structs, env_processes_list, Ts, Ns]
simulation, states_list, config, env_processes, T, N = list(map(lambda x: x.pop(), l))
result = simulation(states_list, config, env_processes, T, N)
return flatten(result)
def parallelize_simulations(fs, states_list, configs, env_processes, Ts, Ns):
l = list(zip(fs, states_list, configs, env_processes, Ts, Ns))
with Pool(len(configs)) as p:
results = p.map(lambda t: t[0](t[1], t[2], t[3], t[4], t[5]), l)
return results
if context == 'single_proc':
self.method = single_proc_exec
elif context == 'multi_proc':
self.method = parallelize_simulations
class Executor:
def __init__(self, exec_context, configs):
self.SimExecutor = SimExecutor
self.exec_method = exec_context.method
self.exec_context = exec_context.name
self.configs = configs
self.main = self.execute
def execute(self):
config_proc = Processor()
create_tensor_field = TensorFieldReport(config_proc).create_tensor_field
print(self.exec_context+": "+str(self.configs))
states_lists, Ts, Ns, eps, configs_structs, env_processes_list, mechanisms, simulation_execs = \
[], [], [], [], [], [], [], []
config_idx = 0
for x in self.configs:
states_lists.append([x.state_dict])
Ts.append(x.sim_config['T'])
Ns.append(x.sim_config['N'])
eps.append(list(x.exogenous_states.values()))
configs_structs.append(config_proc.generate_config(x.state_dict, x.mechanisms, eps[config_idx]))
env_processes_list.append(x.env_processes)
mechanisms.append(x.mechanisms)
simulation_execs.append(SimExecutor(x.behavior_ops).simulation)
config_idx += 1
# Dimensions: N x r x mechs
if self.exec_context == ExecutionMode.single_proc:
tensor_field = create_tensor_field(mechanisms.pop(), eps.pop())
result = self.exec_method(simulation_execs, states_lists, configs_structs, env_processes_list, Ts, Ns)
return result, tensor_field
elif self.exec_context == ExecutionMode.multi_proc:
if len(self.configs) > 1:
simulations = self.exec_method(simulation_execs, states_lists, configs_structs, env_processes_list, Ts, Ns)
results = []
for result, mechanism, ep in list(zip(simulations, mechanisms, eps)):
results.append((flatten(result), create_tensor_field(mechanism, ep)))
return results

174
SimCAD/engine/simulation.py Normal file
View File

@ -0,0 +1,174 @@
from pathos.threading import ThreadPool
from copy import deepcopy
from fn.op import foldr, call
import numpy as np
import pprint
pp = pprint.PrettyPrinter(indent=4)
from SimCAD.utils import groupByKey, flatten, drop_right
from SimCAD.engine.utils import engine_exception
id_exception = engine_exception(KeyError, KeyError, None)
class Executor:
def __init__(self, behavior_ops, behavior_update_exception=id_exception, state_update_exception=id_exception):
self.behavior_ops = behavior_ops
self.state_update_exception = state_update_exception
self.behavior_update_exception = behavior_update_exception
# Data Type reduction
def get_behavior_input(self, step, sL, s, funcs):
ops = self.behavior_ops[::-1]
def get_col_results(step, sL, s, funcs):
return list(map(lambda f: f(step, sL, s), funcs))
return foldr(call, get_col_results(step, sL, s, funcs))(ops)
def xthreaded_env_proc(self, f, s_valx):
if isinstance(s_valx, list):
pool = ThreadPool(nodes=len(s_valx)) # ToDo: Optimize
return pool.map(lambda f: f(s_valx), s_valx)
else:
return f(s_valx)
def apply_env_proc(self, env_processes, state_dict, step):
for state in state_dict.keys():
if state in list(env_processes.keys()):
env_state = env_processes[state]
if (env_state.__name__ == '_curried') or (env_state.__name__ == 'proc_trigger'): # might want to change
state_dict[state] = self.xthreaded_env_proc(env_state(step), state_dict[state])
else:
state_dict[state] = self.xthreaded_env_proc(env_state, state_dict[state])
def xthreaded_state_update(self, fs, m_step, sL, last_in_obj, _input):
if isinstance(fs, list):
pool = ThreadPool(nodes=len(fs)) # ToDo: Optimize
fx = pool.map(lambda f: f(m_step, sL, last_in_obj, _input), fs)
return groupByKey(fx)
else:
return fs(m_step, sL, last_in_obj, _input)
def mech_step(self, m_step, sL, state_funcs, behavior_funcs, env_processes, t_step, run):
last_in_obj = sL[-1]
_input = self.state_update_exception(self.get_behavior_input(m_step, sL, last_in_obj, behavior_funcs))
# ToDo: add env_proc generator to `last_in_copy` iterator as wrapper function
last_in_copy = dict([
self.behavior_update_exception(
self.xthreaded_state_update(f, m_step, sL, last_in_obj, _input)
) for f in state_funcs
])
for k in last_in_obj:
if k not in last_in_copy:
last_in_copy[k] = last_in_obj[k]
del last_in_obj
# make env proc trigger field agnostic
self.apply_env_proc(env_processes, last_in_copy, last_in_copy['timestamp']) # mutating last_in_copy
print()
print(last_in_copy)
print()
def set_sys_metrics(state_dict, m_step, t_step, run):
state_dict["mech_step"], state_dict["time_step"], state_dict['run'] = m_step, t_step, run
if any(isinstance(x, list) for x in last_in_copy.values()):
last_in_copies = flatten(last_in_copy)
for last_in_copy in last_in_copies:
set_sys_metrics(last_in_copy, m_step, t_step, run)
sL.append(last_in_copies)
else:
set_sys_metrics(last_in_copy, m_step, t_step, run)
sL.append(last_in_copy)
print()
pp.pprint(last_in_copies)
print()
del last_in_copy
return sL
def mech_pipeline(self, states_list, configs, env_processes, t_step, run):
m_step = 0
states_list_copy = deepcopy(states_list)
# print(states_list_copy)
# remove copy
genesis_states = states_list_copy[-1]
genesis_states['mech_step'], genesis_states['time_step'] = m_step, t_step
states_list = [genesis_states]
# print(genesis_states)
m_step += 1
for config in configs:
s_conf, b_conf = config[0], config[1]
last_states = states_list[-1]
if isinstance(last_states, list):
pool = ThreadPool(nodes=len(last_states)) # ToDo: Optimize
dropped_right_sL = drop_right(states_list, 1)
def multithreaded_mech_step(mod_states_list):
return self.mech_step(m_step, mod_states_list, s_conf, b_conf, env_processes, t_step, run)
states_lists = pool.map(
lambda last_state_dict: dropped_right_sL + [last_state_dict],
last_states
)
print()
# pp.pprint(configs)
else:
states_lists = self.mech_step(m_step, states_list, s_conf, b_conf, env_processes, t_step, run)
m_step += 1
t_step += 1
exit()
return states_list
# rename pipe
def block_pipeline(self, states_list, configs, env_processes, time_seq, run):
time_seq = [x + 1 for x in time_seq]
simulation_list = [states_list]
# print(len(configs))
for time_step in time_seq:
# print(simulation_list)
if len(simulation_list) == 1:
pipe_run = self.mech_pipeline(simulation_list[-1], configs, env_processes, time_step, run)
exit()
# elif np.array(pipe_run[-1]) == 2:
# pipe_run = self.mech_pipeline(simulation_list[-1], configs, env_processes, time_step, run)
# print(pipe_run)
_, *pipe_run = pipe_run
# print(pipe_run)
simulation_list.append(pipe_run)
return simulation_list
# Del _ / head
def simulation(self, states_list, configs, env_processes, time_seq, runs):
pipe_run = []
for run in range(runs):
run += 1
states_list_copy = deepcopy(states_list) # WHY ???
head, *tail = self.block_pipeline(states_list_copy, configs, env_processes, time_seq, run)
genesis = head.pop()
genesis['mech_step'], genesis['time_step'], genesis['run'] = 0, 0, run
first_timestep_per_run = [genesis] + tail.pop(0)
pipe_run += [first_timestep_per_run] + tail
del states_list_copy
return pipe_run

View File

@ -1,5 +1,7 @@
from datetime import datetime from datetime import datetime
from fn.func import curried from fn.func import curried
from SimCAD.utils import rename
# from SimCAD.configuration.utils import s_update
def datetime_range(start, end, delta, dt_format='%Y-%m-%d %H:%M:%S'): def datetime_range(start, end, delta, dt_format='%Y-%m-%d %H:%M:%S'):
@ -24,6 +26,8 @@ def retrieve_state(l, offset):
return l[last_index(l) + offset + 1] return l[last_index(l) + offset + 1]
# exception_function = f(m_step, sL, sL[-2], _input)
# try_function = f(m_step, sL, last_mut_obj, _input)
@curried @curried
def engine_exception(ErrorType, error_message, exception_function, try_function): def engine_exception(ErrorType, error_message, exception_function, try_function):
try: try:
@ -36,3 +40,8 @@ def engine_exception(ErrorType, error_message, exception_function, try_function)
@curried @curried
def fit_param(param, x): def fit_param(param, x):
return x + param return x + param
# fit_param = lambda param: lambda x: x + param
def sweep(params, sweep_f):
return [rename('sweep', sweep_f(param)) for param in params]

63
SimCAD/utils/__init__.py Normal file
View File

@ -0,0 +1,63 @@
from collections import defaultdict
from itertools import product
# from fn.func import curried
def pipe(x):
return x
def print_pipe(x):
print(x)
return x
def flattenDict(l):
def tupalize(k, vs):
l = []
if isinstance(vs, list):
for v in vs:
l.append((k, v))
else:
l.append((k, vs))
return l
flat_list = [tupalize(k, vs) for k, vs in l.items()]
flat_dict = [dict(items) for items in product(*flat_list)]
return flat_dict
def flatten(l):
if isinstance(l, list):
return [item for sublist in l for item in sublist]
elif isinstance(l, dict):
return flattenDict(l)
def drop_right(l, n):
return l[:len(l)-n]
# def flatmap(f, items):
# return list(map(f, items))
def key_filter(l, keyname):
return [v[keyname] for k, v in l.items()]
def groupByKey(l):
d = defaultdict(list)
for key, value in l:
d[key].append(value)
return list(dict(d).items()).pop()
# @curried
def rename(new_name, f):
f.__name__ = new_name
return f
#
# def rename(newname):
# def decorator(f):
# f.__name__ = newname
# return f
# return decorator

View File

@ -1,15 +0,0 @@
Complex Adaptive Dynamics
o i e
m d s
p e i
u d g
t n
e
r
__________ ____
________ __ _____/ ____/ | / __ \
/ ___/ __` / __ / / / /| | / / / /
/ /__/ /_/ / /_/ / /___/ ___ |/ /_/ /
\___/\__,_/\__,_/\____/_/ |_/_____/
by BlockScience

View File

@ -1,2 +0,0 @@
name = "cadCAD"
configs = []

View File

@ -1,137 +0,0 @@
from typing import Dict, Callable, List, Tuple
from functools import reduce
import pandas as pd
from pandas.core.frame import DataFrame
from cadCAD import configs
from cadCAD.utils import key_filter
from cadCAD.configuration.utils import exo_update_per_ts
from cadCAD.configuration.utils.policyAggregation import dict_elemwise_sum
from cadCAD.configuration.utils.depreciationHandler import sanitize_partial_state_updates, sanitize_config
class Configuration(object):
def __init__(self, sim_config={}, initial_state={}, seeds={}, env_processes={},
exogenous_states={}, partial_state_update_blocks={}, policy_ops=[lambda a, b: a + b],
**kwargs) -> None:
# print(exogenous_states)
self.sim_config = sim_config
self.initial_state = initial_state
self.seeds = seeds
self.env_processes = env_processes
self.exogenous_states = exogenous_states
self.partial_state_updates = partial_state_update_blocks
self.policy_ops = policy_ops
self.kwargs = kwargs
sanitize_config(self)
def append_configs(sim_configs={}, initial_state={}, seeds={}, raw_exogenous_states={}, env_processes={},
partial_state_update_blocks={}, policy_ops=[lambda a, b: a + b], _exo_update_per_ts: bool = True) -> None:
if _exo_update_per_ts is True:
exogenous_states = exo_update_per_ts(raw_exogenous_states)
else:
exogenous_states = raw_exogenous_states
if isinstance(sim_configs, dict):
sim_configs = [sim_configs]
for sim_config in sim_configs:
config = Configuration(
sim_config=sim_config,
initial_state=initial_state,
seeds=seeds,
exogenous_states=exogenous_states,
env_processes=env_processes,
partial_state_update_blocks=partial_state_update_blocks,
policy_ops=policy_ops
)
print(sim_configs)
#for each sim config create new config
configs.append(config)
class Identity:
def __init__(self, policy_id: Dict[str, int] = {'identity': 0}) -> None:
self.beh_id_return_val = policy_id
def p_identity(self, var_dict, sub_step, sL, s):
return self.beh_id_return_val
def policy_identity(self, k: str) -> Callable:
return self.p_identity
def no_state_identity(self, var_dict, sub_step, sL, s, _input):
return None
def state_identity(self, k: str) -> Callable:
return lambda var_dict, sub_step, sL, s, _input: (k, s[k])
def apply_identity_funcs(self, identity: Callable, df: DataFrame, cols: List[str]) -> List[DataFrame]:
def fillna_with_id_func(identity, df, col):
return df[[col]].fillna(value=identity(col))
return list(map(lambda col: fillna_with_id_func(identity, df, col), cols))
class Processor:
def __init__(self, id: Identity = Identity()) -> None:
self.id = id
self.p_identity = id.p_identity
self.policy_identity = id.policy_identity
self.no_state_identity = id.no_state_identity
self.state_identity = id.state_identity
self.apply_identity_funcs = id.apply_identity_funcs
def create_matrix_field(self, partial_state_updates, key: str) -> DataFrame:
if key == 'variables':
identity = self.state_identity
elif key == 'policies':
identity = self.policy_identity
df = pd.DataFrame(key_filter(partial_state_updates, key))
col_list = self.apply_identity_funcs(identity, df, list(df.columns))
if len(col_list) != 0:
return reduce((lambda x, y: pd.concat([x, y], axis=1)), col_list)
else:
return pd.DataFrame({'empty': []})
def generate_config(self, initial_state, partial_state_updates, exo_proc
) -> List[Tuple[List[Callable], List[Callable]]]:
def no_update_handler(bdf, sdf):
if (bdf.empty == False) and (sdf.empty == True):
bdf_values = bdf.values.tolist()
sdf_values = [[self.no_state_identity] * len(bdf_values) for m in range(len(partial_state_updates))]
return sdf_values, bdf_values
elif (bdf.empty == True) and (sdf.empty == False):
sdf_values = sdf.values.tolist()
bdf_values = [[self.p_identity] * len(sdf_values) for m in range(len(partial_state_updates))]
return sdf_values, bdf_values
else:
sdf_values = sdf.values.tolist()
bdf_values = bdf.values.tolist()
return sdf_values, bdf_values
def only_ep_handler(state_dict):
sdf_functions = [
lambda var_dict, sub_step, sL, s, _input: (k, v) for k, v in zip(state_dict.keys(), state_dict.values())
]
sdf_values = [sdf_functions]
bdf_values = [[self.p_identity] * len(sdf_values)]
return sdf_values, bdf_values
if len(partial_state_updates) != 0:
# backwards compatibility # ToDo: Move this
partial_state_updates = sanitize_partial_state_updates(partial_state_updates)
bdf = self.create_matrix_field(partial_state_updates, 'policies')
sdf = self.create_matrix_field(partial_state_updates, 'variables')
sdf_values, bdf_values = no_update_handler(bdf, sdf)
zipped_list = list(zip(sdf_values, bdf_values))
else:
sdf_values, bdf_values = only_ep_handler(initial_state)
zipped_list = list(zip(sdf_values, bdf_values))
return list(map(lambda x: (x[0] + exo_proc, x[1]), zipped_list))

View File

@ -1,223 +0,0 @@
from datetime import datetime, timedelta
from copy import deepcopy
from functools import reduce
from fn.func import curried
from funcy import curry
import pandas as pd
from cadCAD.configuration.utils.depreciationHandler import sanitize_partial_state_updates
from cadCAD.utils import dict_filter, contains_type, flatten_tabulated_dict, tabulate_dict
class TensorFieldReport:
def __init__(self, config_proc):
self.config_proc = config_proc
# ToDo: backwards compatibility
def create_tensor_field(self, partial_state_updates, exo_proc, keys = ['policies', 'variables']):
partial_state_updates = sanitize_partial_state_updates(partial_state_updates) # Temporary
dfs = [self.config_proc.create_matrix_field(partial_state_updates, k) for k in keys]
df = pd.concat(dfs, axis=1)
for es, i in zip(exo_proc, range(len(exo_proc))):
df['es' + str(i + 1)] = es
df['m'] = df.index + 1
return df
def state_update(y, x):
return lambda var_dict, sub_step, sL, s, _input: (y, x)
def bound_norm_random(rng, low, high):
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)
return float(res)
@curried
def env_proc_trigger(timestep, f, time):
if time == timestep:
return f
else:
return lambda x: x
tstep_delta = timedelta(days=0, minutes=0, seconds=30)
def time_step(dt_str, dt_format='%Y-%m-%d %H:%M:%S', _timedelta = tstep_delta):
# print(dt_str)
dt = datetime.strptime(dt_str, dt_format)
t = dt + _timedelta
return t.strftime(dt_format)
ep_t_delta = timedelta(days=0, minutes=0, seconds=1)
def ep_time_step(s_condition, dt_str, fromat_str='%Y-%m-%d %H:%M:%S', _timedelta = ep_t_delta):
# print(dt_str)
if s_condition:
return time_step(dt_str, fromat_str, _timedelta)
else:
return dt_str
def partial_state_sweep_filter(state_field, partial_state_updates):
partial_state_dict = dict([(k, v[state_field]) for k, v in partial_state_updates.items()])
return dict([
(k, dict_filter(v, lambda v: isinstance(v, list))) for k, v in partial_state_dict.items()
if contains_type(list(v.values()), list)
])
def state_sweep_filter(raw_exogenous_states):
return dict([(k, v) for k, v in raw_exogenous_states.items() if isinstance(v, list)])
@curried
def sweep_partial_states(_type, in_config):
configs = []
# filtered_mech_states
filtered_partial_states = partial_state_sweep_filter(_type, in_config.partial_state_updates)
if len(filtered_partial_states) > 0:
for partial_state, state_dict in filtered_partial_states.items():
for state, state_funcs in state_dict.items():
for f in state_funcs:
config = deepcopy(in_config)
config.partial_state_updates[partial_state][_type][state] = f
configs.append(config)
del config
else:
configs = [in_config]
return configs
@curried
def sweep_states(state_type, states, in_config):
configs = []
filtered_states = state_sweep_filter(states)
if len(filtered_states) > 0:
for state, state_funcs in filtered_states.items():
for f in state_funcs:
config = deepcopy(in_config)
exploded_states = deepcopy(states)
exploded_states[state] = f
if state_type == 'exogenous':
config.exogenous_states = exploded_states
elif state_type == 'environmental':
config.env_processes = exploded_states
configs.append(config)
del config, exploded_states
else:
configs = [in_config]
return configs
def exo_update_per_ts(ep):
@curried
def ep_decorator(f, y, var_dict, sub_step, sL, s, _input):
if s['substep'] + 1 == 1:
return f(var_dict, sub_step, sL, s, _input)
else:
return y, s[y]
return {es: ep_decorator(f, es) for es, f in ep.items()}
def trigger_condition(s, pre_conditions, cond_opp):
condition_bools = [s[field] in precondition_values for field, precondition_values in pre_conditions.items()]
return reduce(cond_opp, condition_bools)
def apply_state_condition(pre_conditions, cond_opp, y, f, _g, step, sL, s, _input):
if trigger_condition(s, pre_conditions, cond_opp):
return f(_g, step, sL, s, _input)
else:
return y, s[y]
def var_trigger(y, f, pre_conditions, cond_op):
return lambda _g, step, sL, s, _input: apply_state_condition(pre_conditions, cond_op, y, f, _g, step, sL, s, _input)
def var_substep_trigger(substeps):
def trigger(end_substep, y, f):
pre_conditions = {'substep': substeps}
cond_opp = lambda a, b: a and b
return var_trigger(y, f, pre_conditions, cond_opp)
return lambda y, f: curry(trigger)(substeps)(y)(f)
def env_trigger(end_substep):
def trigger(end_substep, trigger_field, trigger_vals, funct_list):
def env_update(state_dict, sweep_dict, target_value):
state_dict_copy = deepcopy(state_dict)
# Use supstep to simulate current sysMetrics
if state_dict_copy['substep'] == end_substep:
state_dict_copy['timestep'] = state_dict_copy['timestep'] + 1
if state_dict_copy[trigger_field] in trigger_vals:
for g in funct_list:
target_value = g(sweep_dict, target_value)
del state_dict_copy
return target_value
return env_update
return lambda trigger_field, trigger_vals, funct_list: \
curry(trigger)(end_substep)(trigger_field)(trigger_vals)(funct_list)
def config_sim(d):
def process_variables(d):
return flatten_tabulated_dict(tabulate_dict(d))
if "M" in d:
return [{"N": d["N"], "T": d["T"], "M": M} for M in process_variables(d["M"])]
else:
d["M"] = [{}]
return d
def psub_list(psu_block, psu_steps):
return [psu_block[psu] for psu in psu_steps]
def psub(policies, state_updates):
return {
'policies': policies,
'states': state_updates
}
def genereate_psubs(policy_grid, states_grid, policies, state_updates):
PSUBS = []
for policy_ids, state_list in zip(policy_grid, states_grid):
filtered_policies = {k: v for (k, v) in policies.items() if k in policy_ids}
filtered_state_updates = {k: v for (k, v) in state_updates.items() if k in state_list}
PSUBS.append(psub(filtered_policies, filtered_state_updates))
return PSUBS
def access_block(state_history, target_field, psu_block_offset, exculsion_list=[]):
exculsion_list += [target_field]
def filter_history(key_list, sH):
filter = lambda key_list: \
lambda d: {k: v for k, v in d.items() if k not in key_list}
return list(map(filter(key_list), sH))
if psu_block_offset < -1:
if len(state_history) >= abs(psu_block_offset):
return filter_history(exculsion_list, state_history[psu_block_offset])
else:
return []
elif psu_block_offset == -1:
return filter_history(exculsion_list, state_history[psu_block_offset])
else:
return []

View File

@ -1,35 +0,0 @@
from copy import deepcopy
def sanitize_config(config):
for key, value in config.kwargs.items():
if key == 'state_dict':
config.initial_state = value
elif key == 'seed':
config.seeds = value
elif key == 'mechanisms':
config.partial_state_updates = value
if config.initial_state == {}:
raise Exception('The initial conditions of the system have not been set')
def sanitize_partial_state_updates(partial_state_updates):
new_partial_state_updates = deepcopy(partial_state_updates)
def rename_keys(d):
if 'behaviors' in d:
d['policies'] = d.pop('behaviors')
if 'states' in d:
d['variables'] = d.pop('states')
if isinstance(new_partial_state_updates, list):
for v in new_partial_state_updates:
rename_keys(v)
elif isinstance(new_partial_state_updates, dict):
for k, v in new_partial_state_updates.items():
rename_keys(v)
del partial_state_updates
return new_partial_state_updates

View File

@ -1,59 +0,0 @@
from collections import namedtuple
from inspect import getmembers, ismethod
from pandas.core.frame import DataFrame
from cadCAD.utils import SilentDF
def val_switch(v):
if isinstance(v, DataFrame) is True:
return SilentDF(v)
else:
return v
class udcView(object):
def __init__(self, d, masked_members):
self.__dict__ = d
self.masked_members = masked_members
def __repr__(self):
members = {}
variables = {
k: val_switch(v) for k, v in self.__dict__.items()
if str(type(v)) != "<class 'method'>" and k not in self.masked_members # and isinstance(v, DataFrame) is not True
}
members['methods'] = [k for k, v in self.__dict__.items() if str(type(v)) == "<class 'method'>"]
members.update(variables)
return f"{members}"
class udcBroker(object):
def __init__(self, obj, function_filter=['__init__']):
d = {}
funcs = dict(getmembers(obj, ismethod))
filtered_functions = {k: v for k, v in funcs.items() if k not in function_filter}
d['obj'] = obj
# d.update(deepcopy(vars(obj))) # somehow is enough
d.update(vars(obj)) # somehow is enough
d.update(filtered_functions)
self.members_dict = d
def get_members(self):
return self.members_dict
def get_view(self, masked_members):
return udcView(self.members_dict, masked_members)
def get_namedtuple(self):
return namedtuple("Hydra", self.members_dict.keys())(*self.members_dict.values())
def UDO(udo, masked_members=['obj']):
return udcBroker(udo).get_view(masked_members)
def udoPipe(obj_view):
return UDO(obj_view.obj, obj_view.masked_members)

View File

@ -1,119 +0,0 @@
from typing import Callable, Dict, List, Any, Tuple
from pathos.multiprocessing import ProcessingPool as PPool
from pandas.core.frame import DataFrame
from cadCAD.utils import flatten
from cadCAD.configuration import Configuration, Processor
from cadCAD.configuration.utils import TensorFieldReport
from cadCAD.engine.simulation import Executor as SimExecutor
VarDictType = Dict[str, List[Any]]
StatesListsType = List[Dict[str, Any]]
ConfigsType = List[Tuple[List[Callable], List[Callable]]]
EnvProcessesType = Dict[str, Callable]
class ExecutionMode:
single_proc = 'single_proc'
multi_proc = 'multi_proc'
def single_proc_exec(
simulation_execs: List[Callable],
var_dict_list: List[VarDictType],
states_lists: List[StatesListsType],
configs_structs: List[ConfigsType],
env_processes_list: List[EnvProcessesType],
Ts: List[range],
Ns: List[int]
):
l = [simulation_execs, states_lists, configs_structs, env_processes_list, Ts, Ns]
simulation_exec, states_list, config, env_processes, T, N = list(map(lambda x: x.pop(), l))
result = simulation_exec(var_dict_list, states_list, config, env_processes, T, N)
return flatten(result)
def parallelize_simulations(
simulation_execs: List[Callable],
var_dict_list: List[VarDictType],
states_lists: List[StatesListsType],
configs_structs: List[ConfigsType],
env_processes_list: List[EnvProcessesType],
Ts: List[range],
Ns: List[int]
):
l = list(zip(simulation_execs, var_dict_list, states_lists, configs_structs, env_processes_list, Ts, Ns))
with PPool(len(configs_structs)) as p:
results = p.map(lambda t: t[0](t[1], t[2], t[3], t[4], t[5], t[6]), l)
return results
class ExecutionContext:
def __init__(self, context: str = ExecutionMode.multi_proc) -> None:
self.name = context
self.method = None
if context == 'single_proc':
self.method = single_proc_exec
elif context == 'multi_proc':
self.method = parallelize_simulations
class Executor:
def __init__(self, exec_context: ExecutionContext, configs: List[Configuration]) -> None:
self.SimExecutor = SimExecutor
self.exec_method = exec_context.method
self.exec_context = exec_context.name
self.configs = configs
def execute(self) -> Tuple[List[Dict[str, Any]], DataFrame]:
config_proc = Processor()
create_tensor_field = TensorFieldReport(config_proc).create_tensor_field
print(r'''
__________ ____
________ __ _____/ ____/ | / __ \
/ ___/ __` / __ / / / /| | / / / /
/ /__/ /_/ / /_/ / /___/ ___ |/ /_/ /
\___/\__,_/\__,_/\____/_/ |_/_____/
by BlockScience
''')
print(f'Execution Mode: {self.exec_context + ": " + str(self.configs)}')
print(f'Configurations: {self.configs}')
var_dict_list, states_lists, Ts, Ns, eps, configs_structs, env_processes_list, partial_state_updates, simulation_execs = \
[], [], [], [], [], [], [], [], []
config_idx = 0
for x in self.configs:
Ts.append(x.sim_config['T'])
Ns.append(x.sim_config['N'])
var_dict_list.append(x.sim_config['M'])
states_lists.append([x.initial_state])
eps.append(list(x.exogenous_states.values()))
configs_structs.append(config_proc.generate_config(x.initial_state, x.partial_state_updates, eps[config_idx]))
# print(env_processes_list)
env_processes_list.append(x.env_processes)
partial_state_updates.append(x.partial_state_updates)
simulation_execs.append(SimExecutor(x.policy_ops).simulation)
config_idx += 1
final_result = None
if self.exec_context == ExecutionMode.single_proc:
tensor_field = create_tensor_field(partial_state_updates.pop(), eps.pop())
result = self.exec_method(simulation_execs, var_dict_list, states_lists, configs_structs, env_processes_list, Ts, Ns)
final_result = result, tensor_field
elif self.exec_context == ExecutionMode.multi_proc:
# if len(self.configs) > 1:
simulations = self.exec_method(simulation_execs, var_dict_list, states_lists, configs_structs, env_processes_list, Ts, Ns)
results = []
for result, partial_state_updates, ep in list(zip(simulations, partial_state_updates, eps)):
results.append((flatten(result), create_tensor_field(partial_state_updates, ep)))
final_result = results
return final_result

View File

@ -1,234 +0,0 @@
from typing import Any, Callable, Dict, List, Tuple
from pathos.pools import ThreadPool as TPool
from copy import deepcopy
from functools import reduce
from cadCAD.engine.utils import engine_exception
from cadCAD.utils import flatten
id_exception: Callable = engine_exception(KeyError, KeyError, None)
class Executor:
def __init__(
self,
policy_ops: List[Callable],
policy_update_exception: Callable = id_exception,
state_update_exception: Callable = id_exception
) -> None:
self.policy_ops = policy_ops
self.state_update_exception = state_update_exception
self.policy_update_exception = policy_update_exception
def get_policy_input(
self,
sweep_dict: Dict[str, List[Any]],
sub_step: int,
sL: List[Dict[str, Any]],
s: Dict[str, Any],
funcs: List[Callable]
) -> Dict[str, Any]:
ops = self.policy_ops
def get_col_results(sweep_dict, sub_step, sL, s, funcs):
return list(map(lambda f: f(sweep_dict, sub_step, sL, s), funcs))
def compose(init_reduction_funct, funct_list, val_list):
result, i = None, 0
composition = lambda x: [reduce(init_reduction_funct, x)] + funct_list
for g in composition(val_list):
if i == 0:
result = g
i = 1
else:
result = g(result)
return result
col_results = get_col_results(sweep_dict, sub_step, sL, s, funcs)
key_set = list(set(list(reduce(lambda a, b: a + b, list(map(lambda x: list(x.keys()), col_results))))))
new_dict = {k: [] for k in key_set}
for d in col_results:
for k in d.keys():
new_dict[k].append(d[k])
ops_head, *ops_tail = ops
return {
k: compose(
init_reduction_funct=ops_head, # func executed on value list
funct_list=ops_tail,
val_list=val_list
) for k, val_list in new_dict.items()
}
def apply_env_proc(
self,
sweep_dict,
env_processes: Dict[str, Callable],
state_dict: Dict[str, Any],
) -> Dict[str, Any]:
def env_composition(target_field, state_dict, target_value):
function_type = type(lambda x: x)
env_update = env_processes[target_field]
if isinstance(env_update, list):
for f in env_update:
target_value = f(sweep_dict, target_value)
elif isinstance(env_update, function_type):
target_value = env_update(state_dict, sweep_dict, target_value)
else:
target_value = env_update
return target_value
filtered_state_dict = {k: v for k, v in state_dict.items() if k in env_processes.keys()}
env_proc_dict = {
target_field: env_composition(target_field, state_dict, target_value)
for target_field, target_value in filtered_state_dict.items()
}
for k, v in env_proc_dict.items():
state_dict[k] = v
return state_dict
# mech_step
def partial_state_update(
self,
sweep_dict: Dict[str, List[Any]],
sub_step: int,
sL: Any,
sH: Any,
state_funcs: List[Callable],
policy_funcs: List[Callable],
env_processes: Dict[str, Callable],
time_step: int,
run: int
) -> List[Dict[str, Any]]:
last_in_obj: Dict[str, Any] = deepcopy(sL[-1])
_input: Dict[str, Any] = self.policy_update_exception(
self.get_policy_input(sweep_dict, sub_step, sH, last_in_obj, policy_funcs)
)
def generate_record(state_funcs):
for f in state_funcs:
yield self.state_update_exception(f(sweep_dict, sub_step, sH, last_in_obj, _input))
def transfer_missing_fields(source, destination):
for k in source:
if k not in destination:
destination[k] = source[k]
del source # last_in_obj
return destination
last_in_copy: Dict[str, Any] = transfer_missing_fields(last_in_obj, dict(generate_record(state_funcs)))
last_in_copy: Dict[str, Any] = self.apply_env_proc(sweep_dict, env_processes, last_in_copy)
last_in_copy['substep'], last_in_copy['timestep'], last_in_copy['run'] = sub_step, time_step, run
sL.append(last_in_copy)
del last_in_copy
return sL
# mech_pipeline - state_update_block
def state_update_pipeline(
self,
sweep_dict: Dict[str, List[Any]],
simulation_list, #states_list: List[Dict[str, Any]],
configs: List[Tuple[List[Callable], List[Callable]]],
env_processes: Dict[str, Callable],
time_step: int,
run: int
) -> List[Dict[str, Any]]:
sub_step = 0
states_list_copy: List[Dict[str, Any]] = deepcopy(simulation_list[-1])
genesis_states: Dict[str, Any] = states_list_copy[-1]
if len(states_list_copy) == 1:
genesis_states['substep'] = sub_step
# genesis_states['timestep'] = 0
# else:
# genesis_states['timestep'] = time_step
del states_list_copy
states_list: List[Dict[str, Any]] = [genesis_states]
sub_step += 1
for [s_conf, p_conf] in configs: # tensor field
states_list: List[Dict[str, Any]] = self.partial_state_update(
sweep_dict, sub_step, states_list, simulation_list, s_conf, p_conf, env_processes, time_step, run
)
sub_step += 1
time_step += 1
return states_list
# state_update_pipeline
def run_pipeline(
self,
sweep_dict: Dict[str, List[Any]],
states_list: List[Dict[str, Any]],
configs: List[Tuple[List[Callable], List[Callable]]],
env_processes: Dict[str, Callable],
time_seq: range,
run: int
) -> List[List[Dict[str, Any]]]:
time_seq: List[int] = [x + 1 for x in time_seq]
simulation_list: List[List[Dict[str, Any]]] = [states_list]
for time_step in time_seq:
pipe_run: List[Dict[str, Any]] = self.state_update_pipeline(
sweep_dict, simulation_list, configs, env_processes, time_step, run
)
_, *pipe_run = pipe_run
simulation_list.append(pipe_run)
return simulation_list
def simulation(
self,
sweep_dict: Dict[str, List[Any]],
states_list: List[Dict[str, Any]],
configs: List[Tuple[List[Callable], List[Callable]]],
env_processes: Dict[str, Callable],
time_seq: range,
runs: int
) -> List[List[Dict[str, Any]]]:
def execute_run(sweep_dict, states_list, configs, env_processes, time_seq, run) -> List[Dict[str, Any]]:
run += 1
def generate_init_sys_metrics(genesis_states_list):
for d in genesis_states_list:
d['run'], d['substep'], d['timestep'] = run, 0, 0
yield d
states_list_copy: List[Dict[str, Any]] = list(generate_init_sys_metrics(deepcopy(states_list)))
first_timestep_per_run: List[Dict[str, Any]] = self.run_pipeline(
sweep_dict, states_list_copy, configs, env_processes, time_seq, run
)
del states_list_copy
return first_timestep_per_run
tp = TPool(runs)
pipe_run: List[List[Dict[str, Any]]] = flatten(
tp.map(
lambda run: execute_run(sweep_dict, states_list, configs, env_processes, time_seq, run),
list(range(runs))
)
)
tp.clear()
return pipe_run

View File

@ -1,142 +0,0 @@
from functools import reduce
from typing import Dict, List
from collections import defaultdict, Counter
from itertools import product
import warnings
from pandas import DataFrame
class SilentDF(DataFrame):
def __repr__(self):
return str(hex(id(DataFrame))) #"pandas.core.frame.DataFrame"
def append_dict(dict, new_dict):
dict.update(new_dict)
return dict
class IndexCounter:
def __init__(self):
self.i = 0
def __call__(self):
self.i += 1
return self.i
def compose(*functions):
return reduce(lambda f, g: lambda x: f(g(x)), functions, lambda x: x)
def pipe(x):
return x
def print_pipe(x):
print(x)
return x
def flattenDict(l):
def tupalize(k, vs):
l = []
if isinstance(vs, list):
for v in vs:
l.append((k, v))
else:
l.append((k, vs))
return l
flat_list = [tupalize(k, vs) for k, vs in l.items()]
flat_dict = [dict(items) for items in product(*flat_list)]
return flat_dict
def flatten(l):
if isinstance(l, list):
return [item for sublist in l for item in sublist]
elif isinstance(l, dict):
return flattenDict(l)
def flatMap(f, collection):
return flatten(list(map(f, collection)))
def dict_filter(dictionary, condition):
return dict([(k, v) for k, v in dictionary.items() if condition(v)])
def get_max_dict_val_len(g: Dict[str, List[int]]) -> int:
return len(max(g.values(), key=len))
def tabulate_dict(d: Dict[str, List[int]]) -> Dict[str, List[int]]:
max_len = get_max_dict_val_len(d)
_d = {}
for k, vl in d.items():
if len(vl) != max_len:
_d[k] = vl + list([vl[-1]] * (max_len-1))
else:
_d[k] = vl
return _d
def flatten_tabulated_dict(d: Dict[str, List[int]]) -> List[Dict[str, int]]:
max_len = get_max_dict_val_len(d)
dl = [{} for i in range(max_len)]
for k, vl in d.items():
for v, i in zip(vl, list(range(len(vl)))):
dl[i][k] = v
return dl
def contains_type(_collection, type):
return any(isinstance(x, type) for x in _collection)
def drop_right(l, n):
return l[:len(l) - n]
def key_filter(l, keyname):
if (type(l) == list):
return [v[keyname] for v in l]
# Keeping support to dictionaries for backwards compatibility
# Should be removed in the future
warnings.warn(
"The use of a dictionary to describe Partial State Update Blocks will be deprecated. Use a list instead.",
FutureWarning)
return [v[keyname] for k, v in l.items()]
def groupByKey(l):
d = defaultdict(list)
for key, value in l:
d[key].append(value)
return list(dict(d).items()).pop()
# @curried
def rename(new_name, f):
f.__name__ = new_name
return f
def curry_pot(f, *argv):
sweep_ind = f.__name__[0:5] == 'sweep'
arg_len = len(argv)
if sweep_ind is True and arg_len == 4:
return f(argv[0])(argv[1])(argv[2])(argv[3])
elif sweep_ind is False and arg_len == 4:
return f(argv[0], argv[1], argv[2], argv[3])
elif sweep_ind is True and arg_len == 3:
return f(argv[0])(argv[1])(argv[2])
elif sweep_ind is False and arg_len == 3:
return f(argv[0], argv[1], argv[2])
else:
raise TypeError('curry_pot() needs 3 or 4 positional arguments')

View File

@ -1,51 +0,0 @@
from funcy import curry
from cadCAD.configuration.utils import ep_time_step, time_step
def increment(y, incr_by):
return lambda _g, step, sL, s, _input: (y, s[y] + incr_by)
def track(y):
return lambda _g, step, sL, s, _input: (y, s[y].x)
def simple_state_update(y, x):
return lambda _g, step, sH, s, _input: (y, x)
def simple_policy_update(y):
return lambda _g, step, sH, s: y
def update_timestamp(y, timedelta, format):
return lambda _g, step, sL, s, _input: (
y,
ep_time_step(s, dt_str=s[y], fromat_str=format, _timedelta=timedelta)
)
def apply(f, y: str, incr_by: int):
return lambda _g, step, sL, s, _input: (y, curry(f)(s[y])(incr_by))
def add(y: str, incr_by):
return apply(lambda a, b: a + b, y, incr_by)
def increment_state_by_int(y: str, incr_by: int):
return lambda _g, step, sL, s, _input: (y, s[y] + incr_by)
def s(y, x):
return lambda _g, step, sH, s, _input: (y, x)
def time_model(y, substeps, time_delta, ts_format='%Y-%m-%d %H:%M:%S'):
def apply_incriment_condition(s):
if s['substep'] == 0 or s['substep'] == substeps:
return y, time_step(dt_str=s[y], dt_format=ts_format, _timedelta=time_delta)
else:
return y, s[y]
return lambda _g, step, sL, s, _input: apply_incriment_condition(s)

Binary file not shown.

Binary file not shown.

View File

@ -1,91 +0,0 @@
Historical State Access
==
#### Motivation
The current state (values of state variables) is accessed through the `s` list. When the user requires previous state variable values, they may be accessed through the state history list, `sH`. Accessing the state history should be implemented without creating unintended feedback loops on the current state.
The 3rd parameter of state and policy update functions (labeled as `sH` of type `List[List[dict]]`) provides access to past Partial State Update Block (PSUB) given a negative offset number. `access_block` is used to access past PSUBs (`List[dict]`) from `sH`. For example, an offset of `-2` denotes the second to last PSUB.
#### Exclusion List
Create a list of states to exclude from the reported PSU.
```python
exclusion_list = [
'nonexistent', 'last_x', '2nd_to_last_x', '3rd_to_last_x', '4th_to_last_x'
]
```
##### Example Policy Updates
###### Last partial state update
```python
def last_update(_params, substep, sH, s):
return {"last_x": access_block(
state_history=sH,
target_field="last_x", # Add a field to the exclusion list
psu_block_offset=-1,
exculsion_list=exclusion_list
)
}
```
* Note: Although `target_field` adding a field to the exclusion may seem redundant, it is useful in the case of the exclusion list being empty while the `target_field` is assigned to a state or a policy key.
##### Define State Updates
###### 2nd to last partial state update
```python
def second2last_update(_params, substep, sH, s):
return {"2nd_to_last_x": access_block(sH, "2nd_to_last_x", -2, exclusion_list)}
```
###### 3rd to last partial state update
```python
def third_to_last_x(_params, substep, sH, s, _input):
return '3rd_to_last_x', access_block(sH, "3rd_to_last_x", -3, exclusion_list)
```
###### 4rd to last partial state update
```python
def fourth_to_last_x(_params, substep, sH, s, _input):
return '4th_to_last_x', access_block(sH, "4th_to_last_x", -4, exclusion_list)
```
###### Non-exsistent partial state update
* `psu_block_offset >= 0` doesn't exist
```python
def nonexistent(_params, substep, sH, s, _input):
return 'nonexistent', access_block(sH, "nonexistent", 0, exclusion_list)
```
#### [Example Simulation:](examples/historical_state_access.py)
#### Example Output:
###### State History
```
+----+-------+-----------+------------+-----+
| | run | substep | timestep | x |
|----+-------+-----------+------------+-----|
| 0 | 1 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 1 | 2 |
| 3 | 1 | 3 | 1 | 3 |
| 4 | 1 | 1 | 2 | 4 |
| 5 | 1 | 2 | 2 | 5 |
| 6 | 1 | 3 | 2 | 6 |
| 7 | 1 | 1 | 3 | 7 |
| 8 | 1 | 2 | 3 | 8 |
| 9 | 1 | 3 | 3 | 9 |
+----+-------+-----------+------------+-----+
```
###### Accessed State History:
Example: `last_x`
```
+----+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| | last_x |
|----+-----------------------------------------------------------------------------------------------------------------------------------------------------|
| 0 | [] |
| 1 | [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}] |
| 2 | [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}] |
| 3 | [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}] |
| 4 | [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}] |
| 5 | [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}] |
| 6 | [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}] |
| 7 | [{'x': 4, 'run': 1, 'substep': 1, 'timestep': 2}, {'x': 5, 'run': 1, 'substep': 2, 'timestep': 2}, {'x': 6, 'run': 1, 'substep': 3, 'timestep': 2}] |
| 8 | [{'x': 4, 'run': 1, 'substep': 1, 'timestep': 2}, {'x': 5, 'run': 1, 'substep': 2, 'timestep': 2}, {'x': 6, 'run': 1, 'substep': 3, 'timestep': 2}] |
| 9 | [{'x': 4, 'run': 1, 'substep': 1, 'timestep': 2}, {'x': 5, 'run': 1, 'substep': 2, 'timestep': 2}, {'x': 6, 'run': 1, 'substep': 3, 'timestep': 2}] |
+----+-----------------------------------------------------------------------------------------------------------------------------------------------------+
```

View File

@ -1,77 +0,0 @@
Policy Aggregation
==
For each Partial State Update, multiple policy dictionaries are aggregated into a single dictionary to be imputted into
all state functions using an initial reduction function and optional subsequent map functions.
#### Aggregate Function Composition:
```python
# Reduce Function
add = lambda a, b: a + b # Used to add policy values of the same key
# Map Function
mult_by_2 = lambda y: y * 2 # Used to multiply all policy values by 2
policy_ops=[add, mult_by_2]
```
##### Example Policy Updates per Partial State Update (PSU)
```python
def p1_psu1(_params, step, sH, s):
return {'policy1': 1}
def p2_psu1(_params, step, sH, s):
return {'policy2': 2}
```
* `add` not applicable due to lack of redundant policies
* `mult_by_2` applied to all policies
* Result: `{'policy1': 2, 'policy2': 4}`
```python
def p1_psu2(_params, step, sH, s):
return {'policy1': 2, 'policy2': 2}
def p2_psu2(_params, step, sH, s):
return {'policy1': 2, 'policy2': 2}
```
* `add` applicable due to redundant policies
* `mult_by_2` applied to all policies
* Result: `{'policy1': 8, 'policy2': 8}`
```python
def p1_psu3(_params, step, sH, s):
return {'policy1': 1, 'policy2': 2, 'policy3': 3}
def p2_psu3(_params, step, sH, s):
return {'policy1': 1, 'policy2': 2, 'policy3': 3}
```
* `add` applicable due to redundant policies
* `mult_by_2` applied to all policies
* Result: `{'policy1': 4, 'policy2': 8, 'policy3': 12}`
#### Aggregate Policies using functions
```python
from cadCAD.configuration import append_configs
append_configs(
sim_configs=???,
initial_state=???,
partial_state_update_blocks=???,
policy_ops=[add, mult_by_2] # Default: [lambda a, b: a + b]
)
```
#### Example
##### * [System Model Configuration](examples/policy_aggregation.py)
##### * Simulation Results:
```
+----+---------------------------------------------+-------+------+-----------+------------+
| | policies | run | s1 | substep | timestep |
|----+---------------------------------------------+-------+------+-----------+------------|
| 0 | {} | 1 | 0 | 0 | 0 |
| 1 | {'policy1': 2, 'policy2': 4} | 1 | 1 | 1 | 1 |
| 2 | {'policy1': 8, 'policy2': 8} | 1 | 2 | 2 | 1 |
| 3 | {'policy3': 12, 'policy1': 4, 'policy2': 8} | 1 | 3 | 3 | 1 |
| 4 | {'policy1': 2, 'policy2': 4} | 1 | 4 | 1 | 2 |
| 5 | {'policy1': 8, 'policy2': 8} | 1 | 5 | 2 | 2 |
| 6 | {'policy3': 12, 'policy1': 4, 'policy2': 8} | 1 | 6 | 3 | 2 |
| 7 | {'policy1': 2, 'policy2': 4} | 1 | 7 | 1 | 3 |
| 8 | {'policy1': 8, 'policy2': 8} | 1 | 8 | 2 | 3 |
| 9 | {'policy3': 12, 'policy1': 4, 'policy2': 8} | 1 | 9 | 3 | 3 |
+----+---------------------------------------------+-------+------+-----------+------------+
```

View File

@ -1,241 +0,0 @@
Simulation Configuration
==
## Introduction
Given a **Simulation Configuration**, cadCAD produces datasets that represent the evolution of the state of a system
over [discrete time](https://en.wikipedia.org/wiki/Discrete_time_and_continuous_time#Discrete_time). The state of the
system is described by a set of [State Variables](#State-Variables). The dynamic of the system is described by
[Policy Functions](#Policy-Functions) and [State Update Functions](#State-Update-Functions), which are evaluated by
cadCAD according to the definitions set by the user in [Partial State Update Blocks](#Partial-State-Update-Blocks).
A Simulation Configuration is comprised of a [System Model](#System-Model) and a set of
[Simulation Properties](#Simulation-Properties)
`append_configs`, stores a **Simulation Configuration** to be [Executed](/JS4Q9oayQASihxHBJzz4Ug) by cadCAD
```python
from cadCAD.configuration import append_configs
append_configs(
initial_state = ..., # System Model
partial_state_update_blocks = .., # System Model
policy_ops = ..., # System Model
sim_configs = ... # Simulation Properties
)
```
Parameters:
* **initial_state** : _dict_ - [State Variables](#State-Variables) and their initial values
* **partial_state_update_blocks** : List[dict[dict]] - List of [Partial State Update Blocks](#Partial-State-Update-Blocks)
* **policy_ops** : List[functions] - See [Policy Aggregation](/63k2ncjITuqOPCUHzK7Viw)
* **sim_configs** - See [System Model Parameter Sweep](/4oJ_GT6zRWW8AO3yMhFKrg)
## Simulation Properties
Simulation properties are passed to `append_configs` in the `sim_configs` parameter. To construct this parameter, we
use the `config_sim` function in `cadCAD.configuration.utils`
```python
from cadCAD.configuration.utils import config_sim
c = config_sim({
"N": ...,
"T": range(...),
"M": ...
})
append_configs(
...
sim_configs = c # Simulation Properties
)
```
### T - Simulation Length
Computer simulations run in discrete time:
>Discrete time views values of variables as occurring at distinct, separate "points in time", or equivalently as being
unchanged throughout each non-zero region of time ("time period")—that is, time is viewed as a discrete variable. (...)
This view of time corresponds to a digital clock that gives a fixed reading of 10:37 for a while, and then jumps to a
new fixed reading of 10:38, etc.
([source: Wikipedia](https://en.wikipedia.org/wiki/Discrete_time_and_continuous_time#Discrete_time))
As is common in many simulation tools, in cadCAD too we refer to each discrete unit of time as a **timestep**. cadCAD
increments a "time counter", and at each step it updates the state variables according to the equations that describe
the system.
The main simulation property that the user must set when creating a Simulation Configuration is the number of timesteps
in the simulation. In other words, for how long do they want to simulate the system that has been modeled.
### N - Number of Runs
cadCAD facilitates running multiple simulations of the same system sequentially, reporting the results of all those
runs in a single dataset. This is especially helpful for running
[Monte Carlo Simulations](../tutorials/robot-marbles-part-4/robot-marbles-part-4.ipynb).
### M - Parameters of the System
Parameters of the system, passed to the state update functions and the policy functions in the `params` parameter are
defined here. See [System Model Parameter Sweep](/4oJ_GT6zRWW8AO3yMhFKrg) for more information.
## System Model
The System Model describes the system that will be simulated in cadCAD. It is comprised of a set of
[State Variables](###Sate-Variables) and the [State Update Functions](#State-Update-Functions) that determine the
evolution of the state of the system over time. [Policy Functions](#Policy-Functions) (representations of user policies
or internal system control policies) may also be part of a System Model.
### State Variables
>A state variable is one of the set of variables that are used to describe the mathematical "state" of a dynamical
system. Intuitively, the state of a system describes enough about the system to determine its future behaviour in the
absence of any external forces affecting the system. ([source: Wikipedia](https://en.wikipedia.org/wiki/State_variable))
cadCAD can handle state variables of any Python data type, including custom classes. It is up to the user of cadCAD to
determine the state variables needed to **sufficiently and accurately** describe the system they are interested in.
State Variables are passed to `append_configs` along with its initial values, as a Python `dict` where the `dict_keys`
are the names of the variables and the `dict_values` are their initial values.
```python
from cadCAD.configuration import append_configs
genesis_states = {
'state_variable_1': 0,
'state_variable_2': 0,
'state_variable_3': 1.5,
'timestamp': '2019-01-01 00:00:00'
}
append_configs(
initial_state = genesis_states,
...
)
```
### State Update Functions
State Update Functions represent equations according to which the state variables change over time. Each state update
function must return a tuple containing a string with the name of the state variable being updated and its new value.
Each state update function can only modify a single state variable. The general structure of a state update function is:
```python
def state_update_function_A(_params, substep, sH, s, _input):
...
return 'state_variable_name', new_value
```
Parameters:
* **_params** : _dict_ - [System parameters](/4oJ_GT6zRWW8AO3yMhFKrg)
* **substep** : _int_ - Current [substep](#Substep)
* **sH** : _list[list[dict_]] - Historical values of all state variables for the simulation. See
[Historical State Access](/smiyQTnATtC9xPwvF8KbBQ) for details
* **s** : _dict_ - Current state of the system, where the `dict_keys` are the names of the state variables and the
`dict_values` are their current values.
* **_input** : _dict_ - Aggregation of the signals of all policy functions in the current
[Partial State Update Block](#Partial-State-Update-Block)
Return:
* _tuple_ containing a string with the name of the state variable being updated and its new value.
State update functions should not modify any of the parameters passed to it, as those are mutable Python objects that
cadCAD relies on in order to run the simulation according to the specifications.
### Policy Functions
A Policy Function computes one or more signals to be passed to [State Update Functions](#State-Update-Functions)
(via the _\_input_ parameter). Read
[this article](../tutorials/robot-marbles-part-2/robot-marbles-part-2.ipynb)
for details on why and when to use policy functions.
<!-- We would then expand the tutorials with these kind of concepts
#### Policies
Policies consist of the potential action made available through mechanisms. The action taken is expected to be the
result of a conditional determination of the past state.
While executed the same, the modeller can approach policies dependent on the availability of a mechanism to a population.
- ***Control Policy***
When the controlling or deploying entity has the ability to act in order to affect some aspect of the system, this is a
control policy.
- ***User Policy*** model agent behaviors in reaction to state variables and exogenous variables. The resulted user
action will become an input to PSUs. Note that user behaviors should not directly update value of state variables.
The action taken, as well as the potential to act, through a mechanism is a behavior. -->
The general structure of a policy function is:
```python
def policy_function_1(_params, substep, sH, s):
...
return {'signal_1': value_1, ..., 'signal_N': value_N}
```
Parameters:
* **_params** : _dict_ - [System parameters](/4oJ_GT6zRWW8AO3yMhFKrg)
* **substep** : _int_ - Current [substep](#Substep)
* **sH** : _list[list[dict_]] - Historical values of all state variables for the simulation. See
[Historical State Access](/smiyQTnATtC9xPwvF8KbBQ) for details
* **s** : _dict_ - Current state of the system, where the `dict_keys` are the names of the state variables and the
`dict_values` are their current values.
Return:
* _dict_ of signals to be passed to the state update functions in the same
[Partial State Update Block](#Partial-State-Update-Blocks)
Policy functions should not modify any of the parameters passed to it, as those are mutable Python objects that cadCAD
relies on in order to run the simulation according to the specifications.
At each [Partial State Update Block](#Partial-State-Update-Blocks) (PSUB), the `dicts` returned by all policy functions
within that PSUB dictionaries are aggregated into a single `dict` using an initial reduction function
(a key-wise operation, default: `dic1['keyA'] + dic2['keyA']`) and optional subsequent map functions. The resulting
aggregated `dict` is then passed as the `_input` parameter to the state update functions in that PSUB. For more
information on how to modify the aggregation method, see [Policy Aggregation](/63k2ncjITuqOPCUHzK7Viw).
### Partial State Update Blocks
A **Partial State Update Block** (PSUB) is a set of State Update Functions and Policy Functions such that State Update
Functions in the set are independent from each other and Policies in the set are independent from each other and from
the State Update Functions in the set. In other words, if a state variable is updated in a PSUB, its new value cannot
impact the State Update Functions and Policy Functions in that PSUB - only those in the next PSUB.
![](https://i.imgur.com/9rlX9TG.png)
Partial State Update Blocks are passed to `append_configs` as a List of Python `dicts` where the `dict_keys` are named
`"policies"` and `"variables"` and the values are also Python `dicts` where the keys are the names of the policy and
state update functions and the values are the functions.
```python
PSUBs = [
{
"policies": {
"b_1": policy_function_1,
...
"b_J": policy_function_J
},
"variables": {
"s_1": state_update_function_1,
...
"s_K": state_update_function_K
}
}, #PSUB_1,
{...}, #PSUB_2,
...
{...} #PSUB_M
]
append_configs(
...
partial_state_update_blocks = PSUBs,
...
)
```
#### Substep
At each timestep, cadCAD iterates over the `partial_state_update_blocks` list. For each Partial State Update Block,
cadCAD returns a record containing the state of the system at the end of that PSUB. We refer to that subdivision of a
timestep as a `substep`.
## Result Dataset
cadCAD returns a dataset containing the evolution of the state variables defined by the user over time, with three `int`
indexes:
* `run` - id of the [run](#N-Number-of-Runs)
* `timestep` - discrete unit of time (the total number of timesteps is defined by the user in the
[T Simulation Parameter](#T-Simulation-Length))
* `substep` - subdivision of timestep (the number of [substeps](#Substeps) is the same as the number of Partial State
Update Blocks)
Therefore, the total number of records in the resulting dataset is `N` x `T` x `len(partial_state_update_blocks)`
#### [System Simulation Execution](Simulation_Execution.md)

View File

@ -1,160 +0,0 @@
Simulation Execution
==
System Simulations are executed with the execution engine executor (`cadCAD.engine.Executor`) given System Model
Configurations. There are multiple simulation Execution Modes and Execution Contexts.
### Steps:
1. #### *Choose Execution Mode*:
* ##### Simulation Execution Modes:
`cadCAD` executes a process per System Model Configuration and a thread per System Simulation.
##### Class: `cadCAD.engine.ExecutionMode`
##### Attributes:
* **Single Process:** A single process Execution Mode for a single System Model Configuration (Example:
`cadCAD.engine.ExecutionMode().single_proc`).
* **Multi-Process:** Multiple process Execution Mode for System Model Simulations which executes on a thread per
given System Model Configuration (Example: `cadCAD.engine.ExecutionMode().multi_proc`).
2. #### *Create Execution Context using Execution Mode:*
```python
from cadCAD.engine import ExecutionMode, ExecutionContext
exec_mode = ExecutionMode()
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
```
3. #### *Create Simulation Executor*
```python
from cadCAD.engine import Executor
from cadCAD import configs
simulation = Executor(exec_context=single_proc_ctx, configs=configs)
```
4. #### *Execute Simulation: Produce System Event Dataset*
A Simulation execution produces a System Event Dataset and the Tensor Field applied to initial states used to create it.
```python
import pandas as pd
raw_system_events, tensor_field = simulation.execute()
# Simulation Result Types:
# raw_system_events: List[dict]
# tensor_field: pd.DataFrame
# Result System Events DataFrame
simulation_result = pd.DataFrame(raw_system_events)
```
##### Example Tensor Field
```
+----+-----+--------------------------------+--------------------------------+
| | m | b1 | s1 |
|----+-----+--------------------------------+--------------------------------|
| 0 | 1 | <function p1m1 at 0x10c458ea0> | <function s1m1 at 0x10c464510> |
| 1 | 2 | <function p1m2 at 0x10c464048> | <function s1m2 at 0x10c464620> |
| 2 | 3 | <function p1m3 at 0x10c464400> | <function s1m3 at 0x10c464730> |
+----+-----+--------------------------------+--------------------------------+
```
##### Example Result: System Events DataFrame
```
+----+-------+------------+-----------+------+-----------+
| | run | timestep | substep | s1 | s2 |
|----+-------+------------+-----------+------+-----------|
| 0 | 1 | 0 | 0 | 0 | 0.0 |
| 1 | 1 | 1 | 1 | 1 | 4 |
| 2 | 1 | 1 | 2 | 2 | 6 |
| 3 | 1 | 1 | 3 | 3 | [ 30 300] |
| 4 | 2 | 0 | 0 | 0 | 0.0 |
| 5 | 2 | 1 | 1 | 1 | 4 |
| 6 | 2 | 1 | 2 | 2 | 6 |
| 7 | 2 | 1 | 3 | 3 | [ 30 300] |
+----+-------+------------+-----------+------+-----------+
```
### Execution Examples:
##### Single Simulation Execution (Single Process Execution)
Example System Model Configurations:
* [System Model A](examples/sys_model_A.py): `/documentation/examples/sys_model_A.py`
* [System Model B](examples/sys_model_B.py): `/documentation/examples/sys_model_B.py`
Example Simulation Executions:
* [System Model A](examples/sys_model_A_exec.py): `/documentation/examples/sys_model_A_exec.py`
* [System Model B](examples/sys_model_B_exec.py): `/documentation/examples/sys_model_B_exec.py`
```python
import pandas as pd
from tabulate import tabulate
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from documentation.examples import sys_model_A
from cadCAD import configs
exec_mode = ExecutionMode()
# Single Process Execution using a Single System Model Configuration:
# sys_model_A
sys_model_A = [configs[0]] # sys_model_A
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
sys_model_A_simulation = Executor(exec_context=single_proc_ctx, configs=sys_model_A)
sys_model_A_raw_result, sys_model_A_tensor_field = sys_model_A_simulation.execute()
sys_model_A_result = pd.DataFrame(sys_model_A_raw_result)
print()
print("Tensor Field: sys_model_A")
print(tabulate(sys_model_A_tensor_field, headers='keys', tablefmt='psql'))
print("Result: System Events DataFrame")
print(tabulate(sys_model_A_result, headers='keys', tablefmt='psql'))
print()
```
##### Multiple Simulation Execution
* ##### *Multi Process Execution*
Documentation: Simulation Execution
[Example Simulation Executions::](examples/sys_model_AB_exec.py) `/documentation/examples/sys_model_AB_exec.py`
Example System Model Configurations:
* [System Model A](examples/sys_model_A.py): `/documentation/examples/sys_model_A.py`
* [System Model B](examples/sys_model_B.py): `/documentation/examples/sys_model_B.py`
```python
import pandas as pd
from tabulate import tabulate
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from documentation.examples import sys_model_A, sys_model_B
from cadCAD import configs
exec_mode = ExecutionMode()
# # Multiple Processes Execution using Multiple System Model Configurations:
# # sys_model_A & sys_model_B
multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc)
sys_model_AB_simulation = Executor(exec_context=multi_proc_ctx, configs=configs)
i = 0
config_names = ['sys_model_A', 'sys_model_B']
for sys_model_AB_raw_result, sys_model_AB_tensor_field in sys_model_AB_simulation.execute():
sys_model_AB_result = pd.DataFrame(sys_model_AB_raw_result)
print()
print(f"Tensor Field: {config_names[i]}")
print(tabulate(sys_model_AB_tensor_field, headers='keys', tablefmt='psql'))
print("Result: System Events DataFrame:")
print(tabulate(sys_model_AB_result, headers='keys', tablefmt='psql'))
print()
i += 1
```
* ##### [*System Model Parameter Sweep*](System_Model_Parameter_Sweep.md)
[Example:](examples/param_sweep.py) `/documentation/examples/param_sweep.py`
```python
import pandas as pd
from tabulate import tabulate
# The following imports NEED to be in the exact order
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from documentation.examples import param_sweep
from cadCAD import configs
exec_mode = ExecutionMode()
multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc)
run = Executor(exec_context=multi_proc_ctx, configs=configs)
for raw_result, tensor_field in run.execute():
result = pd.DataFrame(raw_result)
print()
print("Tensor Field:")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()
```

View File

@ -1,72 +0,0 @@
System Model Parameter Sweep
==
Parametrization of a System Model configuration that produces multiple configurations.
##### Set Parameters
```python
params = {
'alpha': [1],
'beta': [2, 5],
'gamma': [3, 4],
'omega': [7]
}
```
The parameters above produce 2 simulations.
* Simulation 1:
* `alpha = 1`
* `beta = 2`
* `gamma = 3`
* `omega = 7`
* Simulation 2:
* `alpha = 1`
* `beta = 5`
* `gamma = 4`
* `omega = 7`
All parameters can also be set to include a single parameter each, which will result in a single simulation.
##### Example State Updates
Previous State:
`y = 0`
```python
def state_update(_params, step, sH, s, _input):
y = 'state'
x = s['state'] + _params['alpha'] + _params['gamma']
return y, x
```
* Updated State:
* Simulation 1: `y = 4 = 0 + 1 + 3`
* Simulation 2: `y = 5 = 0 + 1 + 4`
##### Example Policy Updates
```python
# Internal States per Mechanism
def policies(_params, step, sH, s):
return {'beta': _params['beta'], 'gamma': _params['gamma']}
```
* Simulation 1: `{'beta': 2, 'gamma': 3]}`
* Simulation 2: `{'beta': 5, 'gamma': 4}`
##### Configure Simulation
```python
from cadCAD.configuration.utils import config_sim
g = {
'alpha': [1],
'beta': [2, 5],
'gamma': [3, 4],
'omega': [7]
}
sim_config = config_sim(
{
"N": 2,
"T": range(5),
"M": g,
}
)
```
#### Example
##### * [System Model Configuration](examples/param_sweep.py)

View File

@ -1,45 +0,0 @@
from pprint import pprint
import pandas as pd
from tabulate import tabulate
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from documentation.examples import sys_model_A, sys_model_B
from cadCAD import configs
exec_mode = ExecutionMode()
# Single Process Execution using a Single System Model Configuration:
# sys_model_A
sys_model_A = [configs[0]]
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
sys_model_A_simulation = Executor(exec_context=single_proc_ctx, configs=sys_model_A)
sys_model_A_raw_result, sys_model_A_tensor_field = sys_model_A_simulation.execute()
sys_model_A_result = pd.DataFrame(sys_model_A_raw_result)
print()
print("Tensor Field: sys_model_A")
print(tabulate(sys_model_A_tensor_field, headers='keys', tablefmt='psql'))
print("Result: System Events DataFrame")
print(tabulate(sys_model_A_result, headers='keys', tablefmt='psql'))
print()
# # Multiple Processes Execution using Multiple System Model Configurations:
# # sys_model_A & sys_model_B
multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc)
sys_model_AB_simulation = Executor(exec_context=multi_proc_ctx, configs=configs)
i = 0
config_names = ['sys_model_A', 'sys_model_B']
for sys_model_AB_raw_result, sys_model_AB_tensor_field in sys_model_AB_simulation.execute():
print()
pprint(sys_model_AB_raw_result)
# sys_model_AB_result = pd.DataFrame(sys_model_AB_raw_result)
print()
print(f"Tensor Field: {config_names[i]}")
print(tabulate(sys_model_AB_tensor_field, headers='keys', tablefmt='psql'))
# print("Result: System Events DataFrame:")
# print(tabulate(sys_model_AB_result, headers='keys', tablefmt='psql'))
# print()
i += 1

View File

@ -1,110 +0,0 @@
import pandas as pd
from tabulate import tabulate
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import config_sim, access_block
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from cadCAD import configs
policies, variables = {}, {}
exclusion_list = ['nonexsistant', 'last_x', '2nd_to_last_x', '3rd_to_last_x', '4th_to_last_x']
# Policies per Mechanism
# state_history, target_field, psu_block_offset, exculsion_list
def last_update(_g, substep, sH, s):
return {"last_x": access_block(
state_history=sH,
target_field="last_x",
psu_block_offset=-1,
exculsion_list=exclusion_list
)
}
policies["last_x"] = last_update
def second2last_update(_g, substep, sH, s):
return {"2nd_to_last_x": access_block(sH, "2nd_to_last_x", -2, exclusion_list)}
policies["2nd_to_last_x"] = second2last_update
# Internal States per Mechanism
# WARNING: DO NOT delete elements from sH
def add(y, x):
return lambda _g, substep, sH, s, _input: (y, s[y] + x)
variables['x'] = add('x', 1)
# last_partial_state_update_block
def nonexsistant(_g, substep, sH, s, _input):
return 'nonexsistant', access_block(sH, "nonexsistant", 0, exclusion_list)
variables['nonexsistant'] = nonexsistant
# last_partial_state_update_block
def last_x(_g, substep, sH, s, _input):
return 'last_x', _input["last_x"]
variables['last_x'] = last_x
# 2nd to last partial state update block
def second_to_last_x(_g, substep, sH, s, _input):
return '2nd_to_last_x', _input["2nd_to_last_x"]
variables['2nd_to_last_x'] = second_to_last_x
# 3rd to last partial state update block
def third_to_last_x(_g, substep, sH, s, _input):
return '3rd_to_last_x', access_block(sH, "3rd_to_last_x", -3, exclusion_list)
variables['3rd_to_last_x'] = third_to_last_x
# 4th to last partial state update block
def fourth_to_last_x(_g, substep, sH, s, _input):
return '4th_to_last_x', access_block(sH, "4th_to_last_x", -4, exclusion_list)
variables['4th_to_last_x'] = fourth_to_last_x
genesis_states = {
'x': 0,
'nonexsistant': [],
'last_x': [],
'2nd_to_last_x': [],
'3rd_to_last_x': [],
'4th_to_last_x': []
}
PSUB = {
"policies": policies,
"variables": variables
}
psubs = {
"PSUB1": PSUB,
"PSUB2": PSUB,
"PSUB3": PSUB
}
sim_config = config_sim(
{
"N": 1,
"T": range(3),
}
)
append_configs(
sim_configs=sim_config,
initial_state=genesis_states,
partial_state_update_blocks=psubs
)
exec_mode = ExecutionMode()
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
run = Executor(exec_context=single_proc_ctx, configs=configs)
raw_result, tensor_field = run.execute()
result = pd.DataFrame(raw_result)
cols = ['run','substep','timestep','x','nonexsistant','last_x','2nd_to_last_x','3rd_to_last_x','4th_to_last_x']
result = result[cols]
print()
print("Tensor Field:")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()

View File

@ -1,116 +0,0 @@
import pprint
from typing import Dict, List
import pandas as pd
from tabulate import tabulate
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import env_trigger, var_substep_trigger, config_sim, psub_list
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from cadCAD import configs
pp = pprint.PrettyPrinter(indent=4)
def some_function(x):
return x
g: Dict[str, List[int]] = {
'alpha': [1],
'beta': [2, 5],
'gamma': [3, 4],
'omega': [some_function]
}
psu_steps = ['1', '2', '3']
system_substeps = len(psu_steps)
var_timestep_trigger = var_substep_trigger([0, system_substeps])
env_timestep_trigger = env_trigger(system_substeps)
env_process = {}
# Policies
def gamma(_params, step, sH, s):
return {'gamma': _params['gamma']}
def omega(_params, step, sH, s):
return {'omega': _params['omega'](7)}
# Internal States
def alpha(_params, step, sH, s, _input):
return 'alpha', _params['alpha']
def alpha_plus_gamma(_params, step, sH, s, _input):
return 'alpha_plus_gamma', _params['alpha'] + _params['gamma']
def beta(_params, step, sH, s, _input):
return 'beta', _params['beta']
def policies(_params, step, sH, s, _input):
return 'policies', _input
def sweeped(_params, step, sH, s, _input):
return 'sweeped', {'beta': _params['beta'], 'gamma': _params['gamma']}
genesis_states = {
'alpha_plus_gamma': 0,
'alpha': 0,
'beta': 0,
'policies': {},
'sweeped': {}
}
env_process['sweeped'] = env_timestep_trigger(trigger_field='timestep', trigger_vals=[5], funct_list=[lambda _g, x: _g['beta']])
sim_config = config_sim(
{
"N": 2,
"T": range(5),
"M": g,
}
)
psu_block = {k: {"policies": {}, "variables": {}} for k in psu_steps}
for m in psu_steps:
psu_block[m]['policies']['gamma'] = gamma
psu_block[m]['policies']['omega'] = omega
psu_block[m]["variables"]['alpha'] = alpha_plus_gamma
psu_block[m]["variables"]['alpha_plus_gamma'] = alpha
psu_block[m]["variables"]['beta'] = beta
psu_block[m]['variables']['policies'] = policies
psu_block[m]["variables"]['sweeped'] = var_timestep_trigger(y='sweeped', f=sweeped)
psubs = psub_list(psu_block, psu_steps)
print()
pp.pprint(psu_block)
print()
append_configs(
sim_configs=sim_config,
initial_state=genesis_states,
env_processes=env_process,
partial_state_update_blocks=psubs
)
exec_mode = ExecutionMode()
multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc)
run = Executor(exec_context=multi_proc_ctx, configs=configs)
for raw_result, tensor_field in run.execute():
result = pd.DataFrame(raw_result)
print()
print("Tensor Field:")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()

View File

@ -1,98 +0,0 @@
import pandas as pd
from tabulate import tabulate
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import config_sim
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from cadCAD import configs
# Policies per Mechanism
def p1m1(_g, step, sH, s):
return {'policy1': 1}
def p2m1(_g, step, sH, s):
return {'policy2': 2}
def p1m2(_g, step, sH, s):
return {'policy1': 2, 'policy2': 2}
def p2m2(_g, step, sH, s):
return {'policy1': 2, 'policy2': 2}
def p1m3(_g, step, sH, s):
return {'policy1': 1, 'policy2': 2, 'policy3': 3}
def p2m3(_g, step, sH, s):
return {'policy1': 1, 'policy2': 2, 'policy3': 3}
# Internal States per Mechanism
def add(y, x):
return lambda _g, step, sH, s, _input: (y, s[y] + x)
def policies(_g, step, sH, s, _input):
y = 'policies'
x = _input
return (y, x)
# Genesis States
genesis_states = {
'policies': {},
's1': 0
}
variables = {
's1': add('s1', 1),
"policies": policies
}
psubs = {
"m1": {
"policies": {
"p1": p1m1,
"p2": p2m1
},
"variables": variables
},
"m2": {
"policies": {
"p1": p1m2,
"p2": p2m2
},
"variables": variables
},
"m3": {
"policies": {
"p1": p1m3,
"p2": p2m3
},
"variables": variables
}
}
sim_config = config_sim(
{
"N": 1,
"T": range(3),
}
)
append_configs(
sim_configs=sim_config,
initial_state=genesis_states,
partial_state_update_blocks=psubs,
policy_ops=[lambda a, b: a + b, lambda y: y * 2] # Default: lambda a, b: a + b
)
exec_mode = ExecutionMode()
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
run = Executor(exec_context=single_proc_ctx, configs=configs)
raw_result, tensor_field = run.execute()
result = pd.DataFrame(raw_result)
print()
print("Tensor Field:")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()

View File

@ -1,159 +0,0 @@
import numpy as np
from datetime import timedelta
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import bound_norm_random, config_sim, time_step, env_trigger
seeds = {
'z': np.random.RandomState(1),
'a': np.random.RandomState(2),
'b': np.random.RandomState(3),
'c': np.random.RandomState(4)
}
# Policies per Mechanism
def p1m1(_g, step, sH, s):
return {'param1': 1}
def p2m1(_g, step, sH, s):
return {'param1': 1, 'param2': 4}
def p1m2(_g, step, sH, s):
return {'param1': 'a', 'param2': 2}
def p2m2(_g, step, sH, s):
return {'param1': 'b', 'param2': 4}
def p1m3(_g, step, sH, s):
return {'param1': ['c'], 'param2': np.array([10, 100])}
def p2m3(_g, step, sH, s):
return {'param1': ['d'], 'param2': np.array([20, 200])}
# Internal States per Mechanism
def s1m1(_g, step, sH, s, _input):
y = 's1'
x = s['s1'] + 1
return (y, x)
def s2m1(_g, step, sH, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m2(_g, step, sH, s, _input):
y = 's1'
x = s['s1'] + 1
return (y, x)
def s2m2(_g, step, sH, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m3(_g, step, sH, s, _input):
y = 's1'
x = s['s1'] + 1
return (y, x)
def s2m3(_g, step, sH, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def policies(_g, step, sH, s, _input):
y = 'policies'
x = _input
return (y, x)
# Exogenous States
proc_one_coef_A = 0.7
proc_one_coef_B = 1.3
def es3(_g, step, sH, s, _input):
y = 's3'
x = s['s3'] * bound_norm_random(seeds['a'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
def es4(_g, step, sH, s, _input):
y = 's4'
x = s['s4'] * bound_norm_random(seeds['b'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
def update_timestamp(_g, step, sH, s, _input):
y = 'timestamp'
return y, time_step(dt_str=s[y], dt_format='%Y-%m-%d %H:%M:%S', _timedelta=timedelta(days=0, minutes=0, seconds=1))
# Genesis States
genesis_states = {
's1': 0.0,
's2': 0.0,
's3': 1.0,
's4': 1.0,
'timestamp': '2018-10-01 15:16:24'
}
# Environment Process
# ToDo: Depreciation Waring for env_proc_trigger convention
trigger_timestamps = ['2018-10-01 15:16:25', '2018-10-01 15:16:27', '2018-10-01 15:16:29']
env_processes = {
"s3": [lambda _g, x: 5],
"s4": env_trigger(3)(trigger_field='timestamp', trigger_vals=trigger_timestamps, funct_list=[lambda _g, x: 10])
}
psubs = [
{
"policies": {
"b1": p1m1,
"b2": p2m1
},
"variables": {
"s1": s1m1,
"s2": s2m1,
"s3": es3,
"s4": es4,
"timestamp": update_timestamp
}
},
{
"policies": {
"b1": p1m2,
"b2": p2m2
},
"variables": {
"s1": s1m2,
"s2": s2m2,
# "s3": es3p1,
# "s4": es4p2,
}
},
{
"policies": {
"b1": p1m3,
"b2": p2m3
},
"variables": {
"s1": s1m3,
"s2": s2m3,
# "s3": es3p1,
# "s4": es4p2,
}
}
]
sim_config = config_sim(
{
"N": 2,
"T": range(1),
}
)
append_configs(
sim_configs=sim_config,
initial_state=genesis_states,
env_processes=env_processes,
partial_state_update_blocks=psubs,
policy_ops=[lambda a, b: a + b]
)

View File

@ -1,24 +0,0 @@
import pandas as pd
from tabulate import tabulate
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from documentation.examples import sys_model_A, sys_model_B
from cadCAD import configs
exec_mode = ExecutionMode()
# # Multiple Processes Execution using Multiple System Model Configurations:
# # sys_model_A & sys_model_B
multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc)
sys_model_AB_simulation = Executor(exec_context=multi_proc_ctx, configs=configs)
i = 0
config_names = ['sys_model_A', 'sys_model_B']
for sys_model_AB_raw_result, sys_model_AB_tensor_field in sys_model_AB_simulation.execute():
sys_model_AB_result = pd.DataFrame(sys_model_AB_raw_result)
print()
print(f"Tensor Field: {config_names[i]}")
print(tabulate(sys_model_AB_tensor_field, headers='keys', tablefmt='psql'))
print("Result: System Events DataFrame:")
print(tabulate(sys_model_AB_result, headers='keys', tablefmt='psql'))
print()
i += 1

View File

@ -1,22 +0,0 @@
import pandas as pd
from tabulate import tabulate
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from documentation.examples import sys_model_A
from cadCAD import configs
exec_mode = ExecutionMode()
# Single Process Execution using a Single System Model Configuration:
# sys_model_A
sys_model_A = [configs[0]] # sys_model_A
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
sys_model_A_simulation = Executor(exec_context=single_proc_ctx, configs=sys_model_A)
sys_model_A_raw_result, sys_model_A_tensor_field = sys_model_A_simulation.execute()
sys_model_A_result = pd.DataFrame(sys_model_A_raw_result)
print()
print("Tensor Field: sys_model_A")
print(tabulate(sys_model_A_tensor_field, headers='keys', tablefmt='psql'))
print("Result: System Events DataFrame")
print(tabulate(sys_model_A_result, headers='keys', tablefmt='psql'))
print()

View File

@ -1,147 +0,0 @@
import numpy as np
from datetime import timedelta
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import bound_norm_random, config_sim, env_trigger, time_step
seeds = {
'z': np.random.RandomState(1),
'a': np.random.RandomState(2),
'b': np.random.RandomState(3),
'c': np.random.RandomState(3)
}
# Policies per Mechanism
def p1m1(_g, step, sH, s):
return {'param1': 1}
def p2m1(_g, step, sH, s):
return {'param2': 4}
def p1m2(_g, step, sH, s):
return {'param1': 'a', 'param2': 2}
def p2m2(_g, step, sH, s):
return {'param1': 'b', 'param2': 4}
def p1m3(_g, step, sH, s):
return {'param1': ['c'], 'param2': np.array([10, 100])}
def p2m3(_g, step, sH, s):
return {'param1': ['d'], 'param2': np.array([20, 200])}
# Internal States per Mechanism
def s1m1(_g, step, sH, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m1(_g, step, sH, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m2(_g, step, sH, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m2(_g, step, sH, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m3(_g, step, sH, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m3(_g, step, sH, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
# Exogenous States
proc_one_coef_A = 0.7
proc_one_coef_B = 1.3
def es3(_g, step, sH, s, _input):
y = 's3'
x = s['s3'] * bound_norm_random(seeds['a'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
def es4(_g, step, sH, s, _input):
y = 's4'
x = s['s4'] * bound_norm_random(seeds['b'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
def update_timestamp(_g, step, sH, s, _input):
y = 'timestamp'
return y, time_step(dt_str=s[y], dt_format='%Y-%m-%d %H:%M:%S', _timedelta=timedelta(days=0, minutes=0, seconds=1))
# Genesis States
genesis_states = {
's1': 0,
's2': 0,
's3': 1,
's4': 1,
'timestamp': '2018-10-01 15:16:24'
}
# Environment Process
# ToDo: Depreciation Waring for env_proc_trigger convention
trigger_timestamps = ['2018-10-01 15:16:25', '2018-10-01 15:16:27', '2018-10-01 15:16:29']
env_processes = {
"s3": [lambda _g, x: 5],
"s4": env_trigger(3)(trigger_field='timestamp', trigger_vals=trigger_timestamps, funct_list=[lambda _g, x: 10])
}
psubs = [
{
"policies": {
"b1": p1m1,
# "b2": p2m1
},
"states": {
"s1": s1m1,
# "s2": s2m1
"s3": es3,
"s4": es4,
"timestep": update_timestamp
}
},
{
"policies": {
"b1": p1m2,
# "b2": p2m2
},
"states": {
"s1": s1m2,
# "s2": s2m2
}
},
{
"policies": {
"b1": p1m3,
"b2": p2m3
},
"states": {
"s1": s1m3,
"s2": s2m3
}
}
]
sim_config = config_sim(
{
"N": 2,
"T": range(5),
}
)
append_configs(
sim_configs=sim_config,
initial_state=genesis_states,
env_processes=env_processes,
partial_state_update_blocks=psubs
)

View File

@ -1,23 +0,0 @@
import pandas as pd
from tabulate import tabulate
# The following imports NEED to be in the exact order
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from documentation.examples import sys_model_B
from cadCAD import configs
exec_mode = ExecutionMode()
print("Simulation Execution: Single Configuration")
print()
first_config = configs # only contains sys_model_B
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
run = Executor(exec_context=single_proc_ctx, configs=first_config)
raw_result, tensor_field = run.execute()
result = pd.DataFrame(raw_result)
print()
print("Tensor Field: sys_model_B")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,42 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"assert pd.__version__ == '0.23.4'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,55 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'ui'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-a6e895c51fc0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mengine\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mrun\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mrun\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\engine\\run.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mui\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconfig\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mstate_dict\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmechanisms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mexogenous_states\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msim_config\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mengine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconfigProcessor\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mgenerate_config\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mengine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmechanismExecutor\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0msimulation\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mengine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mutils\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mflatten\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'ui'"
]
}
],
"source": [
"from engine import run\n",
"run.main()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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": 1
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,482 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## SimCAD Application Notebook\n",
"## Experiment Type 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Name of Config File or System Description\n",
"#### 20 MonteCarlo Runs \n",
"#### Behaviors: EMHers, Herders, HODLers, EIUers, and Human EIUers"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Simulation Run 1\n",
"single_proc: [<SimCAD.Configuration object at 0x000001EA1AAA6630>]\n"
]
},
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for *: 'float' and 'decimal.Decimal'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-0d9ea96d7f5c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[0msingle_proc_ctx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mExecutionContext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mexec_mode\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msingle_proc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[0mrun1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mExecutor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msingle_proc_ctx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msingle_config\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m \u001b[0mrun1_raw_result\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrun1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 18\u001b[0m \u001b[0mdf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrun1_raw_result\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;31m# print(tabulate(result, headers='keys', tablefmt='psql'))\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\__init__.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 71\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 72\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexec_context\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mExecutionMode\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msingle_proc\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 73\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0msingle_proc_exec\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msimulation_execs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstates_lists\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconfigs_structs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menv_processes_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mTs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mNs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 74\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexec_context\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mExecutionMode\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmulti_proc\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 75\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconfigs\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\__init__.py\u001b[0m in \u001b[0;36msingle_proc_exec\u001b[1;34m(simulation_execs, states_lists, configs_structs, env_processes_list, Ts, Ns)\u001b[0m\n\u001b[0;32m 67\u001b[0m \u001b[0msimulation\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstates_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconfig\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mT\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mN\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ml\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 68\u001b[0m \u001b[1;31m# print(states_list)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 69\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msimulation\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstates_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconfig\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mT\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mN\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 70\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mflatten\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 71\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\simulation.py\u001b[0m in \u001b[0;36msimulation\u001b[1;34m(self, states_list, configs, env_processes, time_seq, runs)\u001b[0m\n\u001b[0;32m 100\u001b[0m \u001b[1;31m# print(\"Run: \"+str(run))\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 101\u001b[0m \u001b[0mstates_list_copy\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdeepcopy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstates_list\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# WHY ???\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 102\u001b[1;33m \u001b[0mhead\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0mtail\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpipe\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstates_list_copy\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconfigs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtime_seq\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrun\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 103\u001b[0m \u001b[0mgenesis\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mhead\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 104\u001b[0m \u001b[0mgenesis\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'mech_step'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgenesis\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'time_step'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgenesis\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'run'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrun\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\simulation.py\u001b[0m in \u001b[0;36mpipe\u001b[1;34m(self, states_list, configs, env_processes, time_seq, run)\u001b[0m\n\u001b[0;32m 86\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mtime_step\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mtime_seq\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 87\u001b[0m \u001b[1;31m# print(run)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 88\u001b[1;33m \u001b[0mpipe_run\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mblock_gen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msimulation_list\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconfigs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtime_step\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrun\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 89\u001b[0m \u001b[0m_\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0mpipe_run\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpipe_run\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 90\u001b[0m \u001b[0msimulation_list\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpipe_run\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\simulation.py\u001b[0m in \u001b[0;36mblock_gen\u001b[1;34m(self, states_list, configs, env_processes, t_step, run)\u001b[0m\n\u001b[0;32m 72\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mconfig\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mconfigs\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 73\u001b[0m \u001b[0ms_conf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb_conf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mconfig\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconfig\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 74\u001b[1;33m \u001b[0mstates_list\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmech_step\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm_step\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstates_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ms_conf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb_conf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mt_step\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrun\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 75\u001b[0m \u001b[0mm_step\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 76\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\simulation.py\u001b[0m in \u001b[0;36mmech_step\u001b[1;34m(self, m_step, sL, state_funcs, behavior_funcs, env_processes, t_step, run)\u001b[0m\n\u001b[0;32m 42\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 43\u001b[0m \u001b[1;31m# *** add env_proc value here as wrapper function ***\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 44\u001b[1;33m \u001b[0mlast_in_copy\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexception_handler\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mm_step\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msL\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlast_in_obj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_input\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mf\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mstate_funcs\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 45\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 46\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mlast_in_obj\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\simulation.py\u001b[0m in \u001b[0;36m<listcomp>\u001b[1;34m(.0)\u001b[0m\n\u001b[0;32m 42\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 43\u001b[0m \u001b[1;31m# *** add env_proc value here as wrapper function ***\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 44\u001b[1;33m \u001b[0mlast_in_copy\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexception_handler\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mm_step\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msL\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlast_in_obj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_input\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mf\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mstate_funcs\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 45\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 46\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mlast_in_obj\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\simulation.py\u001b[0m in \u001b[0;36mexception_handler\u001b[1;34m(self, f, m_step, sL, last_mut_obj, _input)\u001b[0m\n\u001b[0;32m 28\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mexception_handler\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mm_step\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msL\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlast_mut_obj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_input\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 29\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 30\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm_step\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msL\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlast_mut_obj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_input\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 31\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 32\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Exception\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\staging\\21f1155\\sandboxUX\\config6b.py\u001b[0m in \u001b[0;36ms2m3\u001b[1;34m(step, sL, s, _input)\u001b[0m\n\u001b[0;32m 179\u001b[0m \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'Price'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 180\u001b[0m \u001b[1;31m#var1 = Decimal.from_float(s['Buy_Log'])\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 181\u001b[1;33m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Price'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Buy_Log'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m/\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Z'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m-\u001b[0m \u001b[1;36m0.1\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Sell_Log'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Z'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m+\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Buy_Log'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Sell_Log'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 182\u001b[0m \u001b[1;31m#+ np.divide(s['Buy_Log'],s['Z']) - np.divide() # / Psignal_int\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 183\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for *: 'float' and 'decimal.Decimal'"
]
}
],
"source": [
"import pandas as pd\n",
"from tabulate import tabulate\n",
"\n",
"from SimCAD.engine import ExecutionMode, ExecutionContext, Executor\n",
"from sandboxUX import config6b #, config2\n",
"from SimCAD import configs\n",
"\n",
"# ToDo: pass ExecutionContext with execution method as ExecutionContext input\n",
"\n",
"exec_mode = ExecutionMode()\n",
"\n",
"print(\"Simulation Run 1\")\n",
"# print()\n",
"single_config = [configs[0]]\n",
"single_proc_ctx = ExecutionContext(exec_mode.single_proc)\n",
"run1 = Executor(single_proc_ctx, single_config)\n",
"run1_raw_result = run1.main()\n",
"df = pd.DataFrame(run1_raw_result)\n",
"# print(tabulate(result, headers='keys', tablefmt='psql'))\n",
"# print()\n",
"\n",
"# print(\"Simulation Run 2: Pairwise Execution\")\n",
"# print()\n",
"# multi_proc_ctx = ExecutionContext(exec_mode.multi_proc)\n",
"# run2 = Executor(multi_proc_ctx, configs)\n",
"# run2_raw_results = run2.main()\n",
"# for raw_result in run2_raw_results:\n",
"# result = pd.DataFrame(raw_result)\n",
"# print(tabulate(result, headers='keys', tablefmt='psql'))\n",
"# print()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#df = pd.DataFrame(run1_raw_result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Standard Library Imports\n",
"#import pandas as pd\n",
"import numpy as np\n",
"import matplotlib as mpl\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"#from tabulate import tabulate\n",
"\n",
"sns.set_style('whitegrid')\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# SimCAD Specific\n",
"# from SimCAD.engine import ExecutionMode, ExecutionContext, Executor\n",
"# from sandboxUX import config1 , config2\n",
"# from SimCAD import configs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Convert data type of output to float. MPL works OK with strings, seaborn does not\n",
"names = df.keys()[:-3] # [:-3] only affects state variables\n",
"for n in names:\n",
" df[n]=df[n].apply(float)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Check\n",
"df.head(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.iloc[2995:3005]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.tail(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.corr()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"aggregate_dimension = 'time_step'\n",
"\n",
"mean_df = df.groupby(aggregate_dimension).mean().reset_index()\n",
"median_df = df.groupby(aggregate_dimension).median().reset_index()\n",
"std_df = df.groupby(aggregate_dimension).std().reset_index()\n",
"min_df = df.groupby(aggregate_dimension).min().reset_index()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mean_df.head(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"mean_df.tail(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def dist_plot(x, y,lx=False,ly=False, suppMin=False): \n",
" plt.figure(figsize=(12,8))\n",
" if not(suppMin):\n",
" plt.plot(mean_df[x].values, mean_df[y].values,\n",
" mean_df[x].values,median_df[y].values,\n",
" mean_df[x].values,mean_df[y].values+std_df[y].values,\n",
" mean_df[x].values,min_df[y].values)\n",
" plt.legend(['mean', 'median', 'mean+ 1*std', 'min'],bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)\n",
" \n",
" else:\n",
" plt.plot(mean_df[x].values, mean_df[y].values,\n",
" mean_df[x].values,median_df[y].values,\n",
" mean_df[x].values,mean_df[y].values+std_df[y].values,\n",
" mean_df[x].values,mean_df[y].values-std_df[y].values)\n",
" plt.legend(['mean', 'median', 'mean+ 1*std', 'mean - 1*std'],bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)\n",
"\n",
" plt.xlabel(x)\n",
" plt.ylabel(y)\n",
" if lx:\n",
" plt.xscale('log')\n",
" \n",
" if ly:\n",
" plt.yscale('log')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dist_plot('time_step', 'P_Ext_Markets',suppMin=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dist_plot('time_step', 'Price',suppMin=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(12,8))\n",
"plt.plot(mean_df['time_step'][1:],mean_df['Price'][1:]) #, df['Zeus_LT']], figsize=(15,10)) #, logy=True)\n",
"plt.plot(mean_df['time_step'][1:],(1/250)*mean_df['P_Ext_Markets'][1:])\n",
"#plt.plot(df['time_step'],df['Zeus_LT'])\n",
"plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(np.std(mean_df))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(12,8))\n",
"plt.plot(mean_df['time_step'][1:],mean_df['Buy_Log'][1:]) #, df['Zeus_LT']], figsize=(15,10)) #, logy=True)\n",
"plt.plot(mean_df['time_step'][1:],mean_df['Sell_Log'][1:])\n",
"#plt.plot(df['time_step'],df['Zeus_LT'])\n",
"plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"buy_delta = mean_df['Buy_Log'].diff()\n",
"sell_delta = mean_df['Sell_Log'].diff()\n",
"ext_delta = mean_df['P_Ext_Markets'].diff()\n",
"# df_delta['Buy_Log'] = buy_delta\n",
"# df_delta['Sell_Log'] = sell_delta\n",
"# df_delta = df_delta.append(ext_delta)\n",
"# df_delta.head()\n",
"sell_delta.head(20)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(12,8))\n",
"plt.plot(mean_df['time_step'][1:],buy_delta[1:]) #, df['Zeus_LT']], figsize=(15,10)) #, logy=True)\n",
"plt.plot(mean_df['time_step'][1:],sell_delta[1:])\n",
"plt.plot(mean_df['time_step'][1:],ext_delta[1:])\n",
"plt.ylim(-400,400)\n",
"#plt.plot(df['time_step'],df['Zeus_LT'])\n",
"plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sns.pairplot(mean_df)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(12,8))\n",
"plt.plot(mean_df['time_step'],mean_df['Z']/mean_df['P_Ext_Markets'])\n",
"plt.title('Z per External Stock Market Price')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# plt.figure(figsize=(12,8))\n",
"# plt.plot(df['time_step'],(df['TDR_Int']-df['TDR_Ext'])/df['TDR_Ext'])\n",
"# plt.title('Availability of TDR arbitrage opportunity')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# plt.figure(figsize=(12,8))\n",
"# plt.plot(df['time_step'],(df['Zeus_LT']/df['Zeus_ST']-1))\n",
"# plt.title('Availability of LT vs ST arbitrage opportunity')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# vol_df = df.rolling(window = 21).mean()\n",
"vol_df = pd.DataFrame()\n",
"rolling_days = 63 # days = number * mechanisms\n",
"for n in names:\n",
" vol_df[n] = mean_df[n].rolling(rolling_days).mean().shift()\n",
" \n",
"vol_df = vol_df.dropna() #(vol_df.iloc[0:rolling_days])\n",
"# vol_df[n].iloc[:rolling_days], axis=1)\n",
"vol_df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(12,8))\n",
"plt.plot(vol_df['Z'])\n",
"plt.title('Rolling Average of Z')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(12,8))\n",
"plt.plot(vol_df['P_Ext_Markets'])\n",
"plt.title('Rolling Average of External Stock Market Price')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(12,8))\n",
"plt.plot(vol_df['Price'])\n",
"plt.plot(vol_df['P_Ext_Markets']/250)\n",
"plt.legend()\n",
"plt.title('Rolling Average of Zeus Price')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df[\"Price\"].min()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df[\"Price\"].max()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}

View File

@ -0,0 +1,576 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import _thread\n",
"import time\n",
"from fn.func import curried"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Define a function for the thread\n",
"def f(threadName, delay):\n",
" count = 0\n",
" print(count)\n",
" # while count < 5:\n",
" # time.sleep(delay)\n",
" # count += 1\n",
" # print(count)\n",
" \n",
"def pipe(x):\n",
" print(x)\n",
" return x"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"00\n",
"\n"
]
}
],
"source": [
"# Create two threads as follows\n",
"try:\n",
" _thread.start_new_thread( f, (\"Thread-1\", 2, ) )\n",
" _thread.start_new_thread( f, (\"Thread-2\", 4, ) )\n",
"except:\n",
" print (\"Error: unable to start thread\")\n",
"\n",
"while 1:\n",
" pass\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2]\n",
"('s2', <function fit_param.<locals>.<lambda> at 0x1099efae8>)\n",
"('s2', <function fit_param.<locals>.<lambda> at 0x1099ef9d8>)\n"
]
}
],
"source": [
"from SimCAD.engine.utils import sweep\n",
"from SimCAD.utils import rename\n",
"from SimCAD.configuration.utils import s_update\n",
"\n",
"# @curried\n",
"def fit_param(param):\n",
" return lambda x: x + param\n",
"\n",
"# xf = lambda param: lambda x: x + param\n",
"\n",
"def sweep(params, y, xf):\n",
" op = [rename('sweep', s_update(y, xf(param))) for param in params]\n",
" print(params)\n",
" # print()\n",
" return op\n",
"\n",
"for f in sweep([1,2], 's2', fit_param):\n",
" print(f(1,2,3,4))\n",
"# sweep([1,2], 's2', xf)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 64, 2187, 65536]\n"
]
}
],
"source": [
"# instantiate and configure the worker pool\n",
"from pathos.threading import ThreadPool\n",
"pool = ThreadPool(nodes=4)\n",
"\n",
"# do a blocking map on the chosen function\n",
"print(pool.map(pow, [1,2,3,4], [5,6,7,8]))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-2-6e999d313015>, line 3)",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-2-6e999d313015>\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m [for f in fs]\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
],
"output_type": "error"
}
],
"source": [
"with Pool(len(configs)) as p:\n",
" results = p.map(lambda t: t[0](t[1], t[2], t[3], t[4], t[5]), l)\n",
" \n",
"\n",
"def state_multithreading(self, fs, m_step, sL, last_in_obj, _input):\n",
" if type(fs) == 'list':\n",
" pool.map(f(m_step, sL, last_in_obj, _input), fs)\n",
" else:\n",
" f(m_step, sL, last_in_obj, _input)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('s2', [11, 23])]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from itertools import groupby\n",
"l = [('s2', 11), ('s2', 23)]\n",
"l.sort(key = lambda i : i[0])\n",
"[(key, [i[1] for i in values]) for key, values in groupby(l, lambda i: i[0])]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def groupByKV(l):\n",
" l.sort(key = lambda i : i[0])\n",
" return [(key, [i[1] for i in values]) for key, values in groupby(l, lambda i: i[0])]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('s2', [11, 23])]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"groupByKV(l)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-20-fada0ccd8d2a>, line 2)",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-20-fada0ccd8d2a>\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m collect = lambda tuplist: reduce(lambda acc, (k,v): acc[k].append(v) or acc,tuplist, defaultdict(list))\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
],
"output_type": "error"
}
],
"source": [
"from collections import defaultdict \n",
"collect = lambda tuplist: reduce(lambda acc, (k,v): acc[k].append(v) or acc,tuplist, defaultdict(list))"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"from collections import defaultdict\n",
"d = defaultdict(list)\n",
"for key, value in [('s2', 11), ('s2', 23)]:\n",
" d[key].append(value)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on defaultdict object:\n",
"\n",
"class defaultdict(builtins.dict)\n",
" | defaultdict(default_factory[, ...]) --> dict with default factory\n",
" | \n",
" | The default factory is called without arguments to produce\n",
" | a new value when a key is not present, in __getitem__ only.\n",
" | A defaultdict compares equal to a dict with the same items.\n",
" | All remaining arguments are treated the same as if they were\n",
" | passed to the dict constructor, including keyword arguments.\n",
" | \n",
" | Method resolution order:\n",
" | defaultdict\n",
" | builtins.dict\n",
" | builtins.object\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | __copy__(...)\n",
" | D.copy() -> a shallow copy of D.\n",
" | \n",
" | __getattribute__(self, name, /)\n",
" | Return getattr(self, name).\n",
" | \n",
" | __init__(self, /, *args, **kwargs)\n",
" | Initialize self. See help(type(self)) for accurate signature.\n",
" | \n",
" | __missing__(...)\n",
" | __missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n",
" | if self.default_factory is None: raise KeyError((key,))\n",
" | self[key] = value = self.default_factory()\n",
" | return value\n",
" | \n",
" | __reduce__(...)\n",
" | Return state information for pickling.\n",
" | \n",
" | __repr__(self, /)\n",
" | Return repr(self).\n",
" | \n",
" | copy(...)\n",
" | D.copy() -> a shallow copy of D.\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Data descriptors defined here:\n",
" | \n",
" | default_factory\n",
" | Factory for default value called by __missing__().\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Methods inherited from builtins.dict:\n",
" | \n",
" | __contains__(self, key, /)\n",
" | True if D has a key k, else False.\n",
" | \n",
" | __delitem__(self, key, /)\n",
" | Delete self[key].\n",
" | \n",
" | __eq__(self, value, /)\n",
" | Return self==value.\n",
" | \n",
" | __ge__(self, value, /)\n",
" | Return self>=value.\n",
" | \n",
" | __getitem__(...)\n",
" | x.__getitem__(y) <==> x[y]\n",
" | \n",
" | __gt__(self, value, /)\n",
" | Return self>value.\n",
" | \n",
" | __iter__(self, /)\n",
" | Implement iter(self).\n",
" | \n",
" | __le__(self, value, /)\n",
" | Return self<=value.\n",
" | \n",
" | __len__(self, /)\n",
" | Return len(self).\n",
" | \n",
" | __lt__(self, value, /)\n",
" | Return self<value.\n",
" | \n",
" | __ne__(self, value, /)\n",
" | Return self!=value.\n",
" | \n",
" | __new__(*args, **kwargs) from builtins.type\n",
" | Create and return a new object. See help(type) for accurate signature.\n",
" | \n",
" | __setitem__(self, key, value, /)\n",
" | Set self[key] to value.\n",
" | \n",
" | __sizeof__(...)\n",
" | D.__sizeof__() -> size of D in memory, in bytes\n",
" | \n",
" | clear(...)\n",
" | D.clear() -> None. Remove all items from D.\n",
" | \n",
" | fromkeys(iterable, value=None, /) from builtins.type\n",
" | Returns a new dict with keys from iterable and values equal to value.\n",
" | \n",
" | get(...)\n",
" | D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.\n",
" | \n",
" | items(...)\n",
" | D.items() -> a set-like object providing a view on D's items\n",
" | \n",
" | keys(...)\n",
" | D.keys() -> a set-like object providing a view on D's keys\n",
" | \n",
" | pop(...)\n",
" | D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
" | If key is not found, d is returned if given, otherwise KeyError is raised\n",
" | \n",
" | popitem(...)\n",
" | D.popitem() -> (k, v), remove and return some (key, value) pair as a\n",
" | 2-tuple; but raise KeyError if D is empty.\n",
" | \n",
" | setdefault(...)\n",
" | D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D\n",
" | \n",
" | update(...)\n",
" | D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n",
" | If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n",
" | If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n",
" | In either case, this is followed by: for k in F: D[k] = F[k]\n",
" | \n",
" | values(...)\n",
" | D.values() -> an object providing a view on D's values\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Data and other attributes inherited from builtins.dict:\n",
" | \n",
" | __hash__ = None\n",
"\n"
]
}
],
"source": [
"help(d)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'s2': [11, 23]}"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dict(d)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"def groupByKey(l):\n",
" d = defaultdict(list)\n",
" for key, value in l:\n",
" d[key].append(value)\n",
" return list(dict(d).items()).pop()"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('s2', [11, 23])"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r = groupByKey([('s2', 11), ('s2', 23)])\n",
"r"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# xf = lambda param: 1.0 + param\n",
"# def xf(y, param, s):\n",
"# return s[y] + param\n",
"\n",
"# def fit_param(param):\n",
"# y = 's2'\n",
"# x = 1 + param\n",
"# return lambda step, sL, s, _input: (y, x)\n",
"#\n",
"# def fit_param(param):\n",
"# return lambda step, sL, s, _input: (\n",
"# 's2',\n",
"# s['s2'] + param\n",
"# )\n",
"#\n",
"# s2m1 = sweep(\n",
"# params = [Decimal(11.0), Decimal(22.0)],\n",
"# sweep_f = fit_param\n",
"# )"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"from decimal import Decimal\n",
"from itertools import product\n",
"\n",
"# def \n",
"\n",
"l = {\n",
" 's1': 1, \n",
" 's2': [Decimal('11'), Decimal('22')], \n",
" 's3': [Decimal('12'), Decimal('23')], \n",
" 's4': 10, \n",
" 'timestamp': '2018-10-01 15:16:25', \n",
" 'mech_step': 0, \n",
" 'time_step': 1\n",
"}\n",
"\n",
"def flattenDict(l):\n",
" def tupalize(k, vs):\n",
" l = []\n",
" if isinstance(vs, list):\n",
" for v in vs:\n",
" l.append((k, v))\n",
" else:\n",
" l.append((k, vs))\n",
" return l\n",
"\n",
" flat_list = [tupalize(k, vs) for k, vs in l.items()]\n",
" flat_dict = [dict(items) for items in product(*flat_list)]\n",
" return flat_dict"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'s1': 1,\n",
" 's2': Decimal('11'),\n",
" 's3': Decimal('12'),\n",
" 's4': 10,\n",
" 'timestamp': '2018-10-01 15:16:25',\n",
" 'mech_step': 0,\n",
" 'time_step': 1},\n",
" {'s1': 1,\n",
" 's2': Decimal('11'),\n",
" 's3': Decimal('23'),\n",
" 's4': 10,\n",
" 'timestamp': '2018-10-01 15:16:25',\n",
" 'mech_step': 0,\n",
" 'time_step': 1},\n",
" {'s1': 1,\n",
" 's2': Decimal('22'),\n",
" 's3': Decimal('12'),\n",
" 's4': 10,\n",
" 'timestamp': '2018-10-01 15:16:25',\n",
" 'mech_step': 0,\n",
" 'time_step': 1},\n",
" {'s1': 1,\n",
" 's2': Decimal('22'),\n",
" 's3': Decimal('23'),\n",
" 's4': 10,\n",
" 'timestamp': '2018-10-01 15:16:25',\n",
" 'mech_step': 0,\n",
" 'time_step': 1}]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"flattenDict(l)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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": 1
}

202
notebooks/test.ipynb Normal file
View File

@ -0,0 +1,202 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Simulation Run 1\n",
"\n",
"single_proc: [<SimCAD.Configuration object at 0x10fc1a8d0>]\n",
"+----+-------------+-------+------------+-----------+----------+----------+-------------+---------------------+\n",
"| | mech_step | run | s1 | s2 | s3 | s4 | time_step | timestamp |\n",
"|----+-------------+-------+------------+-----------+----------+----------+-------------+---------------------|\n",
"| 0 | 0 | 2 | 0 | 0 | 1 | 1 | 0 | 2018-10-01 15:16:24 |\n",
"| 1 | 1 | 1 | 1 | 4 | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 2 | 2 | 1 | ab | 6 | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 3 | 3 | 1 | ['c', 'd'] | [ 30 300] | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 4 | 1 | 1 | 1 | 4 | 9.94373 | 10.4365 | 2 | 2018-10-01 15:16:26 |\n",
"| 5 | 2 | 1 | ab | 6 | 9.94373 | 10.4365 | 2 | 2018-10-01 15:16:26 |\n",
"| 6 | 3 | 1 | ['c', 'd'] | [ 30 300] | 9.94373 | 10.4365 | 2 | 2018-10-01 15:16:26 |\n",
"| 7 | 1 | 1 | 1 | 4 | 7.81956 | 10.5372 | 3 | 2018-10-01 15:16:27 |\n",
"| 8 | 2 | 1 | ab | 6 | 7.81956 | 10.5372 | 3 | 2018-10-01 15:16:27 |\n",
"| 9 | 3 | 1 | ['c', 'd'] | [ 30 300] | 7.81956 | 10.5372 | 3 | 2018-10-01 15:16:27 |\n",
"| 10 | 1 | 1 | 1 | 4 | 9.10218 | 8.57362 | 4 | 2018-10-01 15:16:28 |\n",
"| 11 | 2 | 1 | ab | 6 | 9.10218 | 8.57362 | 4 | 2018-10-01 15:16:28 |\n",
"| 12 | 3 | 1 | ['c', 'd'] | [ 30 300] | 9.10218 | 8.57362 | 4 | 2018-10-01 15:16:28 |\n",
"| 13 | 1 | 1 | 1 | 4 | 7.46976 | 8.33579 | 5 | 2018-10-01 15:16:29 |\n",
"| 14 | 2 | 1 | ab | 6 | 7.46976 | 8.33579 | 5 | 2018-10-01 15:16:29 |\n",
"| 15 | 3 | 1 | ['c', 'd'] | [ 30 300] | 7.46976 | 8.33579 | 5 | 2018-10-01 15:16:29 |\n",
"| 16 | 0 | 2 | 0 | 0 | 1 | 1 | 0 | 2018-10-01 15:16:24 |\n",
"| 17 | 1 | 2 | 1 | 4 | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 18 | 2 | 2 | ab | 6 | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 19 | 3 | 2 | ['c', 'd'] | [ 30 300] | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 20 | 1 | 2 | 1 | 4 | 10.5029 | 9.91726 | 2 | 2018-10-01 15:16:26 |\n",
"| 21 | 2 | 2 | ab | 6 | 10.5029 | 9.91726 | 2 | 2018-10-01 15:16:26 |\n",
"| 22 | 3 | 2 | ['c', 'd'] | [ 30 300] | 10.5029 | 9.91726 | 2 | 2018-10-01 15:16:26 |\n",
"| 23 | 1 | 2 | 1 | 4 | 9.19497 | 9.29545 | 3 | 2018-10-01 15:16:27 |\n",
"| 24 | 2 | 2 | ab | 6 | 9.19497 | 9.29545 | 3 | 2018-10-01 15:16:27 |\n",
"| 25 | 3 | 2 | ['c', 'd'] | [ 30 300] | 9.19497 | 9.29545 | 3 | 2018-10-01 15:16:27 |\n",
"| 26 | 1 | 2 | 1 | 4 | 8.22219 | 9.25471 | 4 | 2018-10-01 15:16:28 |\n",
"| 27 | 2 | 2 | ab | 6 | 8.22219 | 9.25471 | 4 | 2018-10-01 15:16:28 |\n",
"| 28 | 3 | 2 | ['c', 'd'] | [ 30 300] | 8.22219 | 9.25471 | 4 | 2018-10-01 15:16:28 |\n",
"| 29 | 1 | 2 | 1 | 4 | 7.47478 | 8.81306 | 5 | 2018-10-01 15:16:29 |\n",
"| 30 | 2 | 2 | ab | 6 | 7.47478 | 8.81306 | 5 | 2018-10-01 15:16:29 |\n",
"| 31 | 3 | 2 | ['c', 'd'] | [ 30 300] | 7.47478 | 8.81306 | 5 | 2018-10-01 15:16:29 |\n",
"+----+-------------+-------+------------+-----------+----------+----------+-------------+---------------------+\n",
"\n",
"Simulation Run 2: Pairwise Execution\n",
"\n",
"multi_proc: [<SimCAD.Configuration object at 0x10fc1a8d0>, <SimCAD.Configuration object at 0x10fc1aeb8>]\n",
"+----+--------------------------------+--------------------------------+--------------------------------+--------------------------------+-----------------------------------------------------+-----------------------------------------------------+-----------------------------------------------------+-----+\n",
"| | b1 | b2 | s1 | s2 | es1 | es2 | es3 | m |\n",
"|----+--------------------------------+--------------------------------+--------------------------------+--------------------------------+-----------------------------------------------------+-----------------------------------------------------+-----------------------------------------------------+-----|\n",
"| 0 | <function b1m1 at 0x10faedd08> | <function b2m1 at 0x10fc230d0> | <function s1m1 at 0x10fc23378> | <function s2m1 at 0x10fc23400> | <function curried.<locals>._curried at 0x10fc23ae8> | <function curried.<locals>._curried at 0x10fc23b70> | <function curried.<locals>._curried at 0x10fc23bf8> | 1 |\n",
"| 1 | <function b1m2 at 0x10fc23158> | <function b2m2 at 0x10fc231e0> | <function s1m2 at 0x10fc23488> | <function s2m2 at 0x10fc23510> | <function curried.<locals>._curried at 0x10fc23ae8> | <function curried.<locals>._curried at 0x10fc23b70> | <function curried.<locals>._curried at 0x10fc23bf8> | 2 |\n",
"| 2 | <function b1m3 at 0x10fc23268> | <function b2m3 at 0x10fc232f0> | <function s1m3 at 0x10fc23598> | <function s2m3 at 0x10fc23620> | <function curried.<locals>._curried at 0x10fc23ae8> | <function curried.<locals>._curried at 0x10fc23b70> | <function curried.<locals>._curried at 0x10fc23bf8> | 3 |\n",
"+----+--------------------------------+--------------------------------+--------------------------------+--------------------------------+-----------------------------------------------------+-----------------------------------------------------+-----------------------------------------------------+-----+\n",
"+----+--------------------------------+--------------------------------+--------------------------------+------------------------------------------------------------+-----------------------------------------------------+-----------------------------------------------------+-----------------------------------------------------+-----+\n",
"| | b1 | b2 | s1 | s2 | es1 | es2 | es3 | m |\n",
"|----+--------------------------------+--------------------------------+--------------------------------+------------------------------------------------------------+-----------------------------------------------------+-----------------------------------------------------+-----------------------------------------------------+-----|\n",
"| 0 | <function b1m1 at 0x10fc23d08> | <function b2m1 at 0x10fc23d90> | <function s1m1 at 0x10fc290d0> | <function state_identity.<locals>.<lambda> at 0x10d4f6598> | <function curried.<locals>._curried at 0x10fc29840> | <function curried.<locals>._curried at 0x10fc298c8> | <function curried.<locals>._curried at 0x10fc29950> | 1 |\n",
"| 1 | <function b1m2 at 0x10fc23e18> | <function b2m2 at 0x10fc23ea0> | <function s1m2 at 0x10fc291e0> | <function state_identity.<locals>.<lambda> at 0x10d4f6598> | <function curried.<locals>._curried at 0x10fc29840> | <function curried.<locals>._curried at 0x10fc298c8> | <function curried.<locals>._curried at 0x10fc29950> | 2 |\n",
"| 2 | <function b1m3 at 0x10fc23f28> | <function b2m3 at 0x10fc29048> | <function s1m3 at 0x10fc292f0> | <function s2m3 at 0x10fc29378> | <function curried.<locals>._curried at 0x10fc29840> | <function curried.<locals>._curried at 0x10fc298c8> | <function curried.<locals>._curried at 0x10fc29950> | 3 |\n",
"+----+--------------------------------+--------------------------------+--------------------------------+------------------------------------------------------------+-----------------------------------------------------+-----------------------------------------------------+-----------------------------------------------------+-----+\n",
"+----+-------------+-------+------------+-----------+----------+----------+-------------+---------------------+\n",
"| | mech_step | run | s1 | s2 | s3 | s4 | time_step | timestamp |\n",
"|----+-------------+-------+------------+-----------+----------+----------+-------------+---------------------|\n",
"| 0 | 0 | 2 | 0 | 0 | 1 | 1 | 0 | 2018-10-01 15:16:24 |\n",
"| 1 | 1 | 1 | 1 | 4 | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 2 | 2 | 1 | ab | 6 | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 3 | 3 | 1 | ['c', 'd'] | [ 30 300] | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 4 | 1 | 1 | 1 | 4 | 12.2922 | 10.8846 | 2 | 2018-10-01 15:16:26 |\n",
"| 5 | 2 | 1 | ab | 6 | 12.2922 | 10.8846 | 2 | 2018-10-01 15:16:26 |\n",
"| 6 | 3 | 1 | ['c', 'd'] | [ 30 300] | 12.2922 | 10.8846 | 2 | 2018-10-01 15:16:26 |\n",
"| 7 | 1 | 1 | 1 | 4 | 12.3433 | 11.8439 | 3 | 2018-10-01 15:16:27 |\n",
"| 8 | 2 | 1 | ab | 6 | 12.3433 | 11.8439 | 3 | 2018-10-01 15:16:27 |\n",
"| 9 | 3 | 1 | ['c', 'd'] | [ 30 300] | 12.3433 | 11.8439 | 3 | 2018-10-01 15:16:27 |\n",
"| 10 | 1 | 1 | 1 | 4 | 10.9634 | 13.8687 | 4 | 2018-10-01 15:16:28 |\n",
"| 11 | 2 | 1 | ab | 6 | 10.9634 | 13.8687 | 4 | 2018-10-01 15:16:28 |\n",
"| 12 | 3 | 1 | ['c', 'd'] | [ 30 300] | 10.9634 | 13.8687 | 4 | 2018-10-01 15:16:28 |\n",
"| 13 | 1 | 1 | 1 | 4 | 11.5544 | 13.9381 | 5 | 2018-10-01 15:16:29 |\n",
"| 14 | 2 | 1 | ab | 6 | 11.5544 | 13.9381 | 5 | 2018-10-01 15:16:29 |\n",
"| 15 | 3 | 1 | ['c', 'd'] | [ 30 300] | 11.5544 | 13.9381 | 5 | 2018-10-01 15:16:29 |\n",
"| 16 | 0 | 2 | 0 | 0 | 1 | 1 | 0 | 2018-10-01 15:16:24 |\n",
"| 17 | 1 | 2 | 1 | 4 | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 18 | 2 | 2 | ab | 6 | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 19 | 3 | 2 | ['c', 'd'] | [ 30 300] | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 20 | 1 | 2 | 1 | 4 | 9.98087 | 9.45464 | 2 | 2018-10-01 15:16:26 |\n",
"| 21 | 2 | 2 | ab | 6 | 9.98087 | 9.45464 | 2 | 2018-10-01 15:16:26 |\n",
"| 22 | 3 | 2 | ['c', 'd'] | [ 30 300] | 9.98087 | 9.45464 | 2 | 2018-10-01 15:16:26 |\n",
"| 23 | 1 | 2 | 1 | 4 | 11.1536 | 7.9925 | 3 | 2018-10-01 15:16:27 |\n",
"| 24 | 2 | 2 | ab | 6 | 11.1536 | 7.9925 | 3 | 2018-10-01 15:16:27 |\n",
"| 25 | 3 | 2 | ['c', 'd'] | [ 30 300] | 11.1536 | 7.9925 | 3 | 2018-10-01 15:16:27 |\n",
"| 26 | 1 | 2 | 1 | 4 | 10.3195 | 8.77766 | 4 | 2018-10-01 15:16:28 |\n",
"| 27 | 2 | 2 | ab | 6 | 10.3195 | 8.77766 | 4 | 2018-10-01 15:16:28 |\n",
"| 28 | 3 | 2 | ['c', 'd'] | [ 30 300] | 10.3195 | 8.77766 | 4 | 2018-10-01 15:16:28 |\n",
"| 29 | 1 | 2 | 1 | 4 | 10.3288 | 7.81118 | 5 | 2018-10-01 15:16:29 |\n",
"| 30 | 2 | 2 | ab | 6 | 10.3288 | 7.81118 | 5 | 2018-10-01 15:16:29 |\n",
"| 31 | 3 | 2 | ['c', 'd'] | [ 30 300] | 10.3288 | 7.81118 | 5 | 2018-10-01 15:16:29 |\n",
"+----+-------------+-------+------------+-----------+----------+----------+-------------+---------------------+\n",
"+----+-------------+-------+------------+-----------+----------+----------+-------------+---------------------+\n",
"| | mech_step | run | s1 | s2 | s3 | s4 | time_step | timestamp |\n",
"|----+-------------+-------+------------+-----------+----------+----------+-------------+---------------------|\n",
"| 0 | 0 | 2 | 0 | 0 | 1 | 1 | 0 | 2018-10-01 15:16:24 |\n",
"| 1 | 1 | 1 | 1 | 0 | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 2 | 2 | 1 | ab | 0 | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 3 | 3 | 1 | ['c', 'd'] | [ 30 300] | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 4 | 1 | 1 | 1 | [ 30 300] | 9.94373 | 10.4365 | 2 | 2018-10-01 15:16:26 |\n",
"| 5 | 2 | 1 | ab | [ 30 300] | 9.94373 | 10.4365 | 2 | 2018-10-01 15:16:26 |\n",
"| 6 | 3 | 1 | ['c', 'd'] | [ 30 300] | 9.94373 | 10.4365 | 2 | 2018-10-01 15:16:26 |\n",
"| 7 | 1 | 1 | 1 | [ 30 300] | 7.81956 | 10.5372 | 3 | 2018-10-01 15:16:27 |\n",
"| 8 | 2 | 1 | ab | [ 30 300] | 7.81956 | 10.5372 | 3 | 2018-10-01 15:16:27 |\n",
"| 9 | 3 | 1 | ['c', 'd'] | [ 30 300] | 7.81956 | 10.5372 | 3 | 2018-10-01 15:16:27 |\n",
"| 10 | 1 | 1 | 1 | [ 30 300] | 9.10218 | 8.57362 | 4 | 2018-10-01 15:16:28 |\n",
"| 11 | 2 | 1 | ab | [ 30 300] | 9.10218 | 8.57362 | 4 | 2018-10-01 15:16:28 |\n",
"| 12 | 3 | 1 | ['c', 'd'] | [ 30 300] | 9.10218 | 8.57362 | 4 | 2018-10-01 15:16:28 |\n",
"| 13 | 1 | 1 | 1 | [ 30 300] | 7.46976 | 8.33579 | 5 | 2018-10-01 15:16:29 |\n",
"| 14 | 2 | 1 | ab | [ 30 300] | 7.46976 | 8.33579 | 5 | 2018-10-01 15:16:29 |\n",
"| 15 | 3 | 1 | ['c', 'd'] | [ 30 300] | 7.46976 | 8.33579 | 5 | 2018-10-01 15:16:29 |\n",
"| 16 | 0 | 2 | 0 | 0 | 1 | 1 | 0 | 2018-10-01 15:16:24 |\n",
"| 17 | 1 | 2 | 1 | 0 | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 18 | 2 | 2 | ab | 0 | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 19 | 3 | 2 | ['c', 'd'] | [ 30 300] | 10 | 10 | 1 | 2018-10-01 15:16:25 |\n",
"| 20 | 1 | 2 | 1 | [ 30 300] | 10.5029 | 9.91726 | 2 | 2018-10-01 15:16:26 |\n",
"| 21 | 2 | 2 | ab | [ 30 300] | 10.5029 | 9.91726 | 2 | 2018-10-01 15:16:26 |\n",
"| 22 | 3 | 2 | ['c', 'd'] | [ 30 300] | 10.5029 | 9.91726 | 2 | 2018-10-01 15:16:26 |\n",
"| 23 | 1 | 2 | 1 | [ 30 300] | 9.19497 | 9.29545 | 3 | 2018-10-01 15:16:27 |\n",
"| 24 | 2 | 2 | ab | [ 30 300] | 9.19497 | 9.29545 | 3 | 2018-10-01 15:16:27 |\n",
"| 25 | 3 | 2 | ['c', 'd'] | [ 30 300] | 9.19497 | 9.29545 | 3 | 2018-10-01 15:16:27 |\n",
"| 26 | 1 | 2 | 1 | [ 30 300] | 8.22219 | 9.25471 | 4 | 2018-10-01 15:16:28 |\n",
"| 27 | 2 | 2 | ab | [ 30 300] | 8.22219 | 9.25471 | 4 | 2018-10-01 15:16:28 |\n",
"| 28 | 3 | 2 | ['c', 'd'] | [ 30 300] | 8.22219 | 9.25471 | 4 | 2018-10-01 15:16:28 |\n",
"| 29 | 1 | 2 | 1 | [ 30 300] | 7.47478 | 8.81306 | 5 | 2018-10-01 15:16:29 |\n",
"| 30 | 2 | 2 | ab | [ 30 300] | 7.47478 | 8.81306 | 5 | 2018-10-01 15:16:29 |\n",
"| 31 | 3 | 2 | ['c', 'd'] | [ 30 300] | 7.47478 | 8.81306 | 5 | 2018-10-01 15:16:29 |\n",
"+----+-------------+-------+------------+-----------+----------+----------+-------------+---------------------+\n",
"\n"
]
}
],
"source": [
"import pandas as pd\n",
"from tabulate import tabulate\n",
"\n",
"from SimCAD.engine import ExecutionMode, ExecutionContext, Executor\n",
"from sandboxUX import config1, config2\n",
"from SimCAD import configs\n",
"\n",
"# ToDo: pass ExecutionContext with execution method as ExecutionContext input\n",
"\n",
"exec_mode = ExecutionMode()\n",
"\n",
"print(\"Simulation Run 1\")\n",
"print()\n",
"single_config = [configs[0]]\n",
"single_proc_ctx = ExecutionContext(exec_mode.single_proc)\n",
"run1 = Executor(single_proc_ctx, single_config)\n",
"run1_raw_result = run1.main()\n",
"result = pd.DataFrame(run1_raw_result)\n",
"print(tabulate(result, headers='keys', tablefmt='psql'))\n",
"print()\n",
"\n",
"print(\"Simulation Run 2: Pairwise Execution\")\n",
"print()\n",
"multi_proc_ctx = ExecutionContext(exec_mode.multi_proc)\n",
"run2 = Executor(multi_proc_ctx, configs)\n",
"run2_raw_results = run2.main()\n",
"for raw_result in run2_raw_results:\n",
" result = pd.DataFrame(raw_result)\n",
" print(tabulate(result, headers='keys', tablefmt='psql'))\n",
"print()"
]
}
],
"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
}

View File

@ -1,6 +1,4 @@
pandas
wheel
pathos pathos
pipenv
fn fn
tabulate tabulate
funcy

View File

@ -1,38 +1,11 @@
from setuptools import setup, find_packages from setuptools import setup
long_description = """ setup(name='SimCAD',
cadCAD (complex adaptive systems computer-aided design) is a python based, unified modeling framework for stochastic version='0.1',
dynamical systems and differential games for research, validation, and Computer Aided Design of economic systems created description='Sim-Cad Enigne',
by BlockScience. It is capable of modeling systems at all levels of abstraction from Agent Based Modeling (ABM) to url='https://github.com/BlockScience/DiffyQ-SimCAD',
System Dynamics (SD), and enabling smooth integration of computational social science simulations with empirical data
science workflows.
An economic system is treated as a state-based model and defined through a set of endogenous and exogenous state
variables which are updated through mechanisms and environmental processes, respectively. Behavioral models, which may
be deterministic or stochastic, provide the evolution of the system within the action space of the mechanisms.
Mathematical formulations of these economic games treat agent utility as derived from the state rather than direct from
an action, creating a rich, dynamic modeling framework. Simulations may be run with a range of initial conditions and
parameters for states, behaviors, mechanisms, and environmental processes to understand and visualize network behavior
under various conditions. Support for A/B testing policies, Monte Carlo analysis, and other common numerical methods is
provided.
"""
setup(name='cadCAD',
version='0.3.1',
description="cadCAD: a differential games based simulation software package for research, validation, and \
Computer Aided Design of economic systems",
long_description=long_description,
url='https://github.com/BlockScience/cadCAD',
author='Joshua E. Jodesty', author='Joshua E. Jodesty',
author_email='joshua@block.science, joshua.jodesty@gmail.com', author_email='joshua@block.science',
license='LICENSE.txt', # license='?????',
packages=find_packages(), packages=['SimCAD'],
install_requires=[ zip_safe=False)
"pandas",
"wheel",
"pathos",
"fn",
"tabulate",
"funcy"
]
)

View File

@ -0,0 +1,220 @@
from decimal import Decimal
import numpy as np
from datetime import timedelta
from SimCAD import configs
from SimCAD.configuration import Configuration
from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \
ep_time_step
seed = {
'z': np.random.RandomState(1)
}
# Signals
# Pr_signal
beta = Decimal('0.25') # agent response gain
beta_LT = Decimal('0.1') # LT agent response gain
alpha = Decimal('0.091') # 21 day EMA forgetfullness between 0 and 1, closer to 1 discounts older obs quicker, should be 2/(N+1)
max_withdraw_factor = Decimal('0.9')
external_draw = Decimal('0.01') # between 0 and 1 to draw Buy_Log to external
# Stochastic process factors
correction_factor = Decimal('0.01')
volatility = Decimal('5.0')
# Buy_Log_signal =
# Z_signal =
# Price_signal =
# TDR_draw_signal =
# P_Ext_Markets_signal =
# Behaviors per Mechanism
# BEHAVIOR 1: EMH Trader
EMH_portion = Decimal('0.250')
EMH_Ext_Hold = Decimal('42000.0')
def b1m1(step, sL, s):
print('b1m1')
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
buy = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
return {'buy_order1': buy}
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
return {'buy_order1': 0}
else:
return {'buy_order1': 0}
def b1m2(step, sL, s):
print('b1m2')
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
return {'sell_order1': 0}
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
sell = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
return {'sell_order1': sell}
else:
return {'sell_order1': 0}
# BEHAVIOR 3: Herding
# BEHAVIOR 4: HODLers
HODL_belief = Decimal('10.0')
HODL_portion = Decimal('0.250')
HODL_Ext_Hold = Decimal('4200.0')
def b4m2(step, sL, s):
print('b4m2')
theta = (s['Z']*HODL_portion*s['Price'])/(s['Z']*HODL_portion*s['Price'] + HODL_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < 1/HODL_belief*(theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
sell = beta * theta*HODL_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HODL_portion*(1-theta))
return {'sell_order2': sell}
elif s['Price'] > (theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
return {'sell_order2': 0}
else:
return {'sell_order2': 0}
# STATES
# ZEUS Fixed Supply
def s1m1(step, sL, s, _input):
y = 'Z'
x = s['Z'] #+ _input # / Psignal_int
return (y, x)
def s2m1(step, sL, s, _input):
y = 'Price'
x = (s['P_Ext_Markets'] - _input['buy_order1']) / s['Z'] * 10000
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
return (y, x)
def s3m1(step, sL, s, _input):
y = 'Buy_Log'
x = _input['buy_order1'] # / Psignal_int
return (y, x)
def s4m2(step, sL, s, _input):
y = 'Sell_Log'
x = _input['sell_order1'] + _input['sell_order2'] # / Psignal_int
return (y, x)
def s3m3(step, sL, s, _input):
y = 'Buy_Log'
x = s['Buy_Log'] + _input # / Psignal_int
return (y, x)
# Price Update
def s2m3(step, sL, s, _input):
y = 'Price'
#var1 = Decimal.from_float(s['Buy_Log'])
x = s['Price'] + s['Buy_Log'] * 1/s['Z'] - s['Sell_Log']/s['Z']
#+ np.divide(s['Buy_Log'],s['Z']) - np.divide() # / Psignal_int
return (y, x)
def s6m1(step, sL, s, _input):
y = 'P_Ext_Markets'
x = s['P_Ext_Markets'] - _input
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
return (y, x)
def s2m2(step, sL, s, _input):
y = 'Price'
x = (s['P_Ext_Markets'] - _input) /s['Z'] *10000
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
return (y, x)
# Exogenous States
proc_one_coef_A = -125
proc_one_coef_B = 125
# A change in belief of actual price, passed onto behaviors to make action
def es4p2(step, sL, s, _input):
y = 'P_Ext_Markets'
x = s['P_Ext_Markets'] + bound_norm_random(seed['z'], proc_one_coef_A, proc_one_coef_B)
return (y,x)
ts_format = '%Y-%m-%d %H:%M:%S'
t_delta = timedelta(days=0, minutes=0, seconds=1)
def es5p2(step, sL, s, _input):
y = 'timestamp'
x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta)
return (y, x)
#Environment States
# NONE
# Genesis States
state_dict = {
'Z': Decimal(21000000.0),
'Price': Decimal(100.0), # Initialize = Z for EMA
'Buy_Log': Decimal(0.0),
'Sell_Log': Decimal(0.0),
'Trans': Decimal(0.0),
'P_Ext_Markets': Decimal(25000.0),
'timestamp': '2018-10-01 15:16:24'
}
def env_proc_id(x):
return x
env_processes = {
# "P_Ext_Markets": env_proc_id
}
exogenous_states = exo_update_per_ts(
{
"P_Ext_Markets": es4p2,
"timestamp": es5p2
}
)
sim_config = {
"N": 1,
"T": range(1000)
}
# test return vs. non-return functions as lambdas
# test fully defined functions
mechanisms = {
"m1": {
"behaviors": {
"b1": b1m1
},
"states": {
"Z": s1m1,
"Buy_Log": s3m1
}
},
"m2": {
"behaviors": {
"b1": b1m2,
"b4": b4m2
},
"states": {
"Sell_Log": s4m2
}
},
"m3": {
"behaviors": {
},
"states": {
"Price": s2m3
}
}
}
configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms))

View File

@ -0,0 +1,247 @@
from decimal import Decimal
import numpy as np
from datetime import timedelta
from SimCAD import configs
from SimCAD.configuration import Configuration
from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \
ep_time_step
seed = {
'z': np.random.RandomState(1)
}
# Signals
# Pr_signal
beta = Decimal('0.25') # agent response gain
beta_LT = Decimal('0.1') # LT agent response gain
# alpha = .67, 2 block moving average
alpha = Decimal('0.67') # 21 day EMA forgetfullness between 0 and 1, closer to 1 discounts older obs quicker, should be 2/(N+1)
max_withdraw_factor = Decimal('0.9')
external_draw = Decimal('0.01') # between 0 and 1 to draw Buy_Log to external
#alpha * s['Zeus_ST'] + (1 - alpha)*s['Zeus_LT']
# Stochastic process factors
correction_factor = Decimal('0.01')
volatility = Decimal('5.0')
# Buy_Log_signal =
# Z_signal =
# Price_signal =
# TDR_draw_signal =
# P_Ext_Markets_signal =
# Behaviors per Mechanism
# BEHAVIOR 1: EMH Trader
EMH_portion = Decimal('0.250')
EMH_Ext_Hold = Decimal('42000.0')
def b1m1(step, sL, s):
# print('b1m1')
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
buy = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
return {'buy_order1': buy}
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
return {'buy_order1': 0}
else:
return {'buy_order1': 0}
def b1m2(step, sL, s):
# print('b1m2')
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
return {'sell_order1': 0}
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
sell = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
return {'sell_order1': sell}
else:
return {'sell_order1': 0}
# BEHAVIOR 3: Herding
Herd_portion = Decimal('0.250')
Herd_Ext_Hold = Decimal('42000.0')
Herd_UB = Decimal('0.10') # UPPER BOUND
Herd_LB = Decimal('0.10') # LOWER BOUND
def b3m2(step, sL, s):
theta = (s['Z']*Herd_portion*s['Price'])/(s['Z']*Herd_portion*s['Price'] + Herd_Ext_Hold * s['P_Ext_Markets'])
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
if (s['Price'] - s['Price_Signal']) < - Herd_LB:
sell = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
return {'herd_sell': sell, 'herd_buy': 0}
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
elif (s['Price'] - s['Price_Signal']) > Herd_UB:
buy = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
return {'herd_sell': 0, 'herd_buy': buy}
else:
return {'herd_sell': 0, 'herd_buy': 0}
# BEHAVIOR 4: HODLers
HODL_belief = Decimal('10.0')
HODL_portion = Decimal('0.250')
HODL_Ext_Hold = Decimal('4200.0')
def b4m2(step, sL, s):
# print('b4m2')
theta = (s['Z']*HODL_portion*s['Price'])/(s['Z']*HODL_portion*s['Price'] + HODL_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < 1/HODL_belief*(theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
sell = beta * theta*HODL_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HODL_portion*(1-theta))
return {'sell_order2': sell}
elif s['Price'] > (theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
return {'sell_order2': 0}
else:
return {'sell_order2': 0}
# STATES
# ZEUS Fixed Supply
def s1m1(step, sL, s, _input):
y = 'Z'
x = s['Z'] #+ _input # / Psignal_int
return (y, x)
# def s2m1(step, sL, s, _input):
# y = 'Price'
# x = (s['P_Ext_Markets'] - _input['buy_order1']) / s['Z'] * 10000
# #x= alpha * s['Z'] + (1 - alpha)*s['Price']
# return (y, x)
def s3m1(step, sL, s, _input):
y = 'Buy_Log'
x = _input['buy_order1'] + _input['herd_buy'] # / Psignal_int
return (y, x)
def s4m2(step, sL, s, _input):
y = 'Sell_Log'
x = _input['sell_order1'] + _input['sell_order2'] + _input['herd_sell'] # / Psignal_int
return (y, x)
def s3m3(step, sL, s, _input):
y = 'Buy_Log'
x = s['Buy_Log'] + _input # / Psignal_int
return (y, x)
# Price Update
def s2m3(step, sL, s, _input):
y = 'Price'
#var1 = Decimal.from_float(s['Buy_Log'])
x = s['Price'] + s['Buy_Log'] /s['Z'] - s['Sell_Log']/s['Z']
#+ np.divide(s['Buy_Log'],s['Z']) - np.divide() # / Psignal_int
return (y, x)
def s5m3(step, sL, s, _input):
y = 'Price_Signal'
x = alpha * s['Price'] + (1 - alpha)*s['Price_Signal']
return (y, x)
def s6m1(step, sL, s, _input):
y = 'P_Ext_Markets'
x = s['P_Ext_Markets'] - _input
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
return (y, x)
def s2m2(step, sL, s, _input):
y = 'Price'
x = (s['P_Ext_Markets'] - _input) /s['Z'] *10000
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
return (y, x)
# Exogenous States
proc_one_coef_A = -125
proc_one_coef_B = 125
# A change in belief of actual price, passed onto behaviors to make action
def es4p2(step, sL, s, _input):
y = 'P_Ext_Markets'
x = s['P_Ext_Markets'] + bound_norm_random(seed['z'], proc_one_coef_A, proc_one_coef_B)
return (y,x)
ts_format = '%Y-%m-%d %H:%M:%S'
t_delta = timedelta(days=0, minutes=0, seconds=1)
def es5p2(step, sL, s, _input):
y = 'timestamp'
x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta)
return (y, x)
#Environment States
# NONE
# Genesis States
state_dict = {
'Z': Decimal(21000000.0),
'Price': Decimal(100.0), # Initialize = Z for EMA
'Buy_Log': Decimal(0.0),
'Sell_Log': Decimal(0.0),
'Price_Signal': Decimal(100.0),
'Trans': Decimal(0.0),
'P_Ext_Markets': Decimal(25000.0),
'timestamp': '2018-10-01 15:16:24'
}
def env_proc_id(x):
return x
env_processes = {}
exogenous_states = exo_update_per_ts(
{
"P_Ext_Markets": es4p2,
"timestamp": es5p2
}
)
sim_config = {
"N": 20,
"T": range(1000)
}
# test return vs. non-return functions as lambdas
# test fully defined functions
mechanisms = {
"m1": {
"behaviors": {
"b1": b1m1,
"b3": b3m2
},
"states": {
"Z": s1m1,
"Buy_Log": s3m1
}
},
"m2": {
"behaviors": {
"b1": b1m2,
"b3": b3m2,
"b4": b4m2
},
"states": {
"Sell_Log": s4m2
}
},
"m3": {
"behaviors": {
},
"states": {
"Price": s2m3,
"Price_Signal": s5m3
}
}
}
configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms))

View File

@ -0,0 +1,267 @@
from decimal import Decimal
import numpy as np
from datetime import timedelta
from SimCAD import configs
from SimCAD.configuration import Configuration
from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \
ep_time_step
seed = {
'z': np.random.RandomState(1)
}
# Signals
# Pr_signal
beta = Decimal('0.25') # agent response gain
beta_LT = Decimal('0.1') # LT agent response gain
# alpha = .67, 2 block moving average
alpha = Decimal('0.67') # 21 day EMA forgetfullness between 0 and 1, closer to 1 discounts older obs quicker, should be 2/(N+1)
max_withdraw_factor = Decimal('0.9')
external_draw = Decimal('0.01') # between 0 and 1 to draw Buy_Log to external
#alpha * s['Zeus_ST'] + (1 - alpha)*s['Zeus_LT']
# Stochastic process factors
correction_factor = Decimal('0.01')
volatility = Decimal('5.0')
# Buy_Log_signal =
# Z_signal =
# Price_signal =
# TDR_draw_signal =
# P_Ext_Markets_signal =
# Behaviors per Mechanism
# BEHAVIOR 1: EMH Trader
EMH_portion = Decimal('0.250')
EMH_Ext_Hold = Decimal('42000.0')
def b1m1(step, sL, s):
# print('b1m1')
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
buy = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
return {'buy_order1': buy}
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
return {'buy_order1': 0}
else:
return {'buy_order1': 0}
def b1m2(step, sL, s):
# print('b1m2')
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
return {'sell_order1': 0}
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
sell = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
return {'sell_order1': sell}
else:
return {'sell_order1': 0}
# BEHAVIOR 3: Herding
Herd_portion = Decimal('0.250')
Herd_Ext_Hold = Decimal('42000.0')
Herd_UB = Decimal('0.10') # UPPER BOUND
Herd_LB = Decimal('0.10') # LOWER BOUND
def b3m2(step, sL, s):
theta = (s['Z']*Herd_portion*s['Price'])/(s['Z']*Herd_portion*s['Price'] + Herd_Ext_Hold * s['P_Ext_Markets'])
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
if (s['Price'] - s['Price_Signal']) < - Herd_LB:
sell = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
return {'herd_sell': sell, 'herd_buy': 0}
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
elif (s['Price'] - s['Price_Signal']) > Herd_UB:
buy = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
return {'herd_sell': 0, 'herd_buy': buy}
else:
return {'herd_sell': 0, 'herd_buy': 0}
# BEHAVIOR 4: HODLers
HODL_belief = Decimal('10.0')
HODL_portion = Decimal('0.250')
HODL_Ext_Hold = Decimal('4200.0')
def b4m2(step, sL, s):
# print('b4m2')
theta = (s['Z']*HODL_portion*s['Price'])/(s['Z']*HODL_portion*s['Price'] + HODL_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < 1/HODL_belief*(theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
sell = beta * theta*HODL_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HODL_portion*(1-theta))
return {'sell_order2': sell}
elif s['Price'] > (theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
return {'sell_order2': 0}
else:
return {'sell_order2': 0}
# BEHAVIOR 7: Endogenous Information Updating (EIU)
EIU_portion = Decimal('0.250')
EIU_Ext_Hold = Decimal('42000.0')
EIU_UB = Decimal('0.50') # UPPER BOUND
EIU_LB = Decimal('0.50') # LOWER BOUND
def b7m2(step, sL, s):
theta = (s['Z']*EIU_portion*s['Price'])/(s['Z']*EIU_portion*s['Price'] + EIU_Ext_Hold * s['P_Ext_Markets'])
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
if (s['Price'] - s['Price_Signal']) < - EIU_LB:
sell = beta * theta*EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EIU_portion*(1-theta))
return {'EIU_sell': sell, 'EIU_buy': 0}
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
elif (s['Price'] - s['Price_Signal']) > EIU_UB:
buy = beta * theta* EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']* EIU_portion*(1-theta))
return {'EIU_sell': 0, 'EIU_buy': buy}
else:
return {'EIU_sell': 0, 'EIU_buy': 0}
# STATES
# ZEUS Fixed Supply
def s1m1(step, sL, s, _input):
y = 'Z'
x = s['Z'] #+ _input # / Psignal_int
return (y, x)
# def s2m1(step, sL, s, _input):
# y = 'Price'
# x = (s['P_Ext_Markets'] - _input['buy_order1']) / s['Z'] * 10000
# #x= alpha * s['Z'] + (1 - alpha)*s['Price']
# return (y, x)
def s3m1(step, sL, s, _input):
y = 'Buy_Log'
x = _input['buy_order1'] + _input['herd_buy'] + _input['EIU_buy'] # / Psignal_int
return (y, x)
def s4m2(step, sL, s, _input):
y = 'Sell_Log'
x = _input['sell_order1'] + _input['sell_order2'] + _input['herd_sell'] + _input['EIU_sell'] # / Psignal_int
return (y, x)
# def s3m3(step, sL, s, _input):
# y = 'Buy_Log'
# x = s['Buy_Log'] + _input # / Psignal_int
# return (y, x)
# Price Update
def s2m3(step, sL, s, _input):
y = 'Price'
#var1 = Decimal.from_float(s['Buy_Log'])
x = s['Price'] + s['Buy_Log'] /s['Z'] /(Decimal('0.10') * s['Price']) - s['Sell_Log'] / s['Z'] / (Decimal('0.10')*s['Price'])
#+ np.divide(s['Buy_Log'],s['Z']) - np.divide() # / Psignal_int
return (y, x)
def s5m3(step, sL, s, _input):
y = 'Price_Signal'
x = alpha * s['Price'] + (1 - alpha)*s['Price_Signal']
return (y, x)
def s6m1(step, sL, s, _input):
y = 'P_Ext_Markets'
x = s['P_Ext_Markets'] - _input
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
return (y, x)
def s2m2(step, sL, s, _input):
y = 'Price'
x = (s['P_Ext_Markets'] - _input) /s['Z'] *10000
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
return (y, x)
# Exogenous States
proc_one_coef_A = -125
proc_one_coef_B = 125
# A change in belief of actual price, passed onto behaviors to make action
def es4p2(step, sL, s, _input):
y = 'P_Ext_Markets'
x = s['P_Ext_Markets'] + bound_norm_random(seed['z'], proc_one_coef_A, proc_one_coef_B)
return (y,x)
def es5p2(step, sL, s, _input): # accept timedelta instead of timedelta params
y = 'timestamp'
x = ep_time_step(s, s['timestamp'], seconds=1)
return (y, x)
#Environment States
# NONE
# Genesis States
state_dict = {
'Z': Decimal(21000000.0),
'Price': Decimal(100.0), # Initialize = Z for EMA
'Buy_Log': Decimal(0.0),
'Sell_Log': Decimal(0.0),
'Price_Signal': Decimal(100.0),
'Trans': Decimal(0.0),
'P_Ext_Markets': Decimal(25000.0),
'timestamp': '2018-10-01 15:16:24'
}
def env_proc_id(x):
return x
env_processes = {
# "P_Ext_Markets": env_proc_id
}
exogenous_states = exo_update_per_ts(
{
"P_Ext_Markets": es4p2,
"timestamp": es5p2
}
)
sim_config = {
"N": 100,
"T": range(1000)
}
# test return vs. non-return functions as lambdas
# test fully defined functions
mechanisms = {
"m1": {
"behaviors": {
"b1": b1m1,
"b3": b3m2,
"b7": b7m2
},
"states": {
"Z": s1m1,
"Buy_Log": s3m1
}
},
"m2": {
"behaviors": {
"b1": b1m2,
"b3": b3m2,
"b4": b4m2,
"b7": b7m2
},
"states": {
"Sell_Log": s4m2
}
},
"m3": {
"behaviors": {
},
"states": {
"Price": s2m3,
"Price_Signal": s5m3
}
}
}
configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms))

View File

@ -0,0 +1,300 @@
from decimal import Decimal
import numpy as np
from datetime import timedelta
from SimCAD import configs
from SimCAD.configuration import Configuration
from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \
ep_time_step
seed = {
'z': np.random.RandomState(1)
}
# Signals
# Pr_signal
beta = Decimal('0.25') # agent response gain
beta_LT = Decimal('0.1') # LT agent response gain
# alpha = .67, 2 block moving average
alpha = Decimal('0.67')
# 21 day EMA forgetfullness between 0 and 1, closer to 1 discounts older obs quicker, should be 2/(N+1)
# 21 * 3 mech steps, 2/64 = 0.03125
alpha_2 = Decimal('0.03125')
max_withdraw_factor = Decimal('0.9')
external_draw = Decimal('0.01') # between 0 and 1 to draw Buy_Log to external
#alpha * s['Zeus_ST'] + (1 - alpha)*s['Zeus_LT']
# Stochastic process factors
correction_factor = Decimal('0.01')
volatility = Decimal('5.0')
# Buy_Log_signal =
# Z_signal =
# Price_signal =
# TDR_draw_signal =
# P_Ext_Markets_signal =
# Behaviors per Mechanism
# BEHAVIOR 1: EMH Trader
EMH_portion = Decimal('0.20')
EMH_Ext_Hold = Decimal('42000.0')
def b1m1(step, sL, s):
# print('b1m1')
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
buy = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
return {'buy_order1': buy}
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
return {'buy_order1': 0}
else:
return {'buy_order1': 0}
def b1m2(step, sL, s):
# print('b1m2')
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
return {'sell_order1': 0}
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
sell = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
return {'sell_order1': sell}
else:
return {'sell_order1': 0}
# BEHAVIOR 3: Herding
Herd_portion = Decimal('0.20')
Herd_Ext_Hold = Decimal('42000.0')
Herd_UB = Decimal('0.10') # UPPER BOUND
Herd_LB = Decimal('0.10') # LOWER BOUND
def b3m2(step, sL, s):
theta = (s['Z']*Herd_portion*s['Price'])/(s['Z']*Herd_portion*s['Price'] + Herd_Ext_Hold * s['P_Ext_Markets'])
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
if (s['Price'] - s['Price_Signal']) < - Herd_LB:
sell = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
return {'herd_sell': sell, 'herd_buy': 0}
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
elif (s['Price'] - s['Price_Signal']) > Herd_UB:
buy = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
return {'herd_sell': 0, 'herd_buy': buy}
else:
return {'herd_sell': 0, 'herd_buy': 0}
# BEHAVIOR 4: HODLers
HODL_belief = Decimal('10.0')
HODL_portion = Decimal('0.20')
HODL_Ext_Hold = Decimal('4200.0')
def b4m2(step, sL, s):
# print('b4m2')
theta = (s['Z']*HODL_portion*s['Price'])/(s['Z']*HODL_portion*s['Price'] + HODL_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < 1/HODL_belief*(theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
sell = beta * theta*HODL_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HODL_portion*(1-theta))
return {'sell_order2': sell}
elif s['Price'] > (theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
return {'sell_order2': 0}
else:
return {'sell_order2': 0}
# BEHAVIOR 7: Endogenous Information Updating (EIU)
# Short Term Price Signal, Lower Threshold = BOT-like
EIU_portion = Decimal('0.20')
EIU_Ext_Hold = Decimal('42000.0')
EIU_UB = Decimal('0.50') # UPPER BOUND
EIU_LB = Decimal('0.50') # LOWER BOUND
def b7m2(step, sL, s):
theta = (s['Z']*EIU_portion*s['Price'])/(s['Z']*EIU_portion*s['Price'] + EIU_Ext_Hold * s['P_Ext_Markets'])
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
if (s['Price'] - s['Price_Signal']) < - EIU_LB:
sell = beta * theta*EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EIU_portion*(1-theta))
return {'EIU_sell': sell, 'EIU_buy': 0}
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
elif (s['Price'] - s['Price_Signal']) > EIU_UB:
buy = beta * theta* EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']* EIU_portion*(1-theta))
return {'EIU_sell': 0, 'EIU_buy': buy}
else:
return {'EIU_sell': 0, 'EIU_buy': 0}
# BEHAVIOR 7b: Endogenous Information Updating (EIU)
# Longer Term Price Signal, Higher Threshold = Human-Like
HEIU_portion = Decimal('0.20')
HEIU_Ext_Hold = Decimal('42000.0')
HEIU_UB = Decimal('2.0') # UPPER BOUND
HEIU_LB = Decimal('2.0') # LOWER BOUND
def b7hm2(step, sL, s):
theta = (s['Z']*HEIU_portion*s['Price'])/(s['Z']*HEIU_portion*s['Price'] + HEIU_Ext_Hold * s['P_Ext_Markets'])
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
if (s['Price'] - s['Price_Signal_2']) < - HEIU_LB:
sell = beta * theta* HEIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HEIU_portion*(1-theta))
return {'HEIU_sell': sell, 'HEIU_buy': 0}
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
elif (s['Price'] - s['Price_Signal_2']) > HEIU_UB:
buy = beta * theta* HEIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']* HEIU_portion*(1-theta))
return {'HEIU_sell': 0, 'HEIU_buy': buy}
else:
return {'HEIU_sell': 0, 'HEIU_buy': 0}
# STATES
# ZEUS Fixed Supply
def s1m1(step, sL, s, _input):
y = 'Z'
x = s['Z'] #+ _input # / Psignal_int
return (y, x)
# def s2m1(step, sL, s, _input):
# y = 'Price'
# x = (s['P_Ext_Markets'] - _input['buy_order1']) / s['Z'] * 10000
# #x= alpha * s['Z'] + (1 - alpha)*s['Price']
# return (y, x)
def s3m1(step, sL, s, _input):
y = 'Buy_Log'
x = _input['buy_order1'] + _input['herd_buy'] + _input['EIU_buy'] + _input['HEIU_buy'] # / Psignal_int
return (y, x)
def s4m2(step, sL, s, _input):
y = 'Sell_Log'
x = _input['sell_order1'] + _input['sell_order2'] + _input['herd_sell'] + _input['EIU_sell'] + _input['HEIU_sell'] # / Psignal_int
return (y, x)
# def s3m3(step, sL, s, _input):
# y = 'Buy_Log'
# x = s['Buy_Log'] + _input # / Psignal_int
# return (y, x)
# Price Update
def s2m3(step, sL, s, _input):
y = 'Price'
#var1 = Decimal.from_float(s['Buy_Log'])
x = s['Price'] + s['Buy_Log'] /s['Z']/(Decimal('1.25') ) - s['Sell_Log']/s['Z']/(Decimal('1.25') )
#+ np.divide(s['Buy_Log'],s['Z']) - np.divide() # / Psignal_int
return (y, x)
def s5m3(step, sL, s, _input):
y = 'Price_Signal'
x = alpha * s['Price'] + (1 - alpha)*s['Price_Signal']
return (y, x)
def s6m3(step, sL, s, _input):
y = 'Price_Signal_2'
x = alpha_2 * s['Price'] + (1 - alpha_2)*s['Price_Signal_2']
return (y, x)
def s6m1(step, sL, s, _input):
y = 'P_Ext_Markets'
x = s['P_Ext_Markets'] - _input
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
return (y, x)
def s2m2(step, sL, s, _input):
y = 'Price'
x = (s['P_Ext_Markets'] - _input) /s['Z'] *10000
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
return (y, x)
# Exogenous States
proc_one_coef_A = -125
proc_one_coef_B = 125
# A change in belief of actual price, passed onto behaviors to make action
def es4p2(step, sL, s, _input):
y = 'P_Ext_Markets'
x = s['P_Ext_Markets'] + bound_norm_random(seed['z'], proc_one_coef_A, proc_one_coef_B)
return (y,x)
def es5p2(step, sL, s, _input): # accept timedelta instead of timedelta params
y = 'timestamp'
x = ep_time_step(s, s['timestamp'], seconds=1)
return (y, x)
#Environment States
# NONE
# Genesis States
state_dict = {
'Z': Decimal(21000000.0),
'Price': Decimal(100.0), # Initialize = Z for EMA
'Buy_Log': Decimal(0.0),
'Sell_Log': Decimal(0.0),
'Price_Signal': Decimal(100.0),
'Price_Signal_2': Decimal(100.0),
'Trans': Decimal(0.0),
'P_Ext_Markets': Decimal(25000.0),
'timestamp': '2018-10-01 15:16:24'
}
def env_proc_id(x):
return x
env_processes = {
# "P_Ext_Markets": env_proc_id
}
exogenous_states = exo_update_per_ts(
{
"P_Ext_Markets": es4p2,
"timestamp": es5p2
}
)
sim_config = {
"N": 100,
"T": range(1000)
}
# test return vs. non-return functions as lambdas
# test fully defined functions
mechanisms = {
"m1": {
"behaviors": {
"b1": b1m1,
"b3": b3m2,
"b7": b7m2,
"b7h": b7hm2
},
"states": {
"Z": s1m1,
"Buy_Log": s3m1
}
},
"m2": {
"behaviors": {
"b1": b1m2,
"b3": b3m2,
"b4": b4m2,
"b7": b7m2,
"b7h": b7hm2
},
"states": {
"Sell_Log": s4m2
}
},
"m3": {
"behaviors": {
},
"states": {
"Price": s2m3,
"Price_Signal": s5m3,
"Price_Signal_2": s6m3,
}
}
}
configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms))

View File

@ -0,0 +1,309 @@
from decimal import Decimal
import numpy as np
from datetime import timedelta
from SimCAD import configs
from SimCAD.configuration import Configuration
from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \
ep_time_step
seed = {
'z': np.random.RandomState(1)
}
# Signals
# Pr_signal
beta = Decimal('0.25') # agent response gain
beta_LT = Decimal('0.1') # LT agent response gain
# alpha = .67, 2 block moving average
alpha = Decimal('0.67')
# 21 day EMA forgetfullness between 0 and 1, closer to 1 discounts older obs quicker, should be 2/(N+1)
# 21 * 3 mech steps, 2/64 = 0.03125
alpha_2 = Decimal('0.03125')
max_withdraw_factor = Decimal('0.9')
external_draw = Decimal('0.01') # between 0 and 1 to draw Buy_Log to external
#alpha * s['Zeus_ST'] + (1 - alpha)*s['Zeus_LT']
# Stochastic process factors
correction_factor = Decimal('0.01')
volatility = Decimal('5.0')
# Buy_Log_signal =
# Z_signal =
# Price_signal =
# TDR_draw_signal =
# P_Ext_Markets_signal =
# Behaviors per Mechanism
# BEHAVIOR 1: EMH Trader
EMH_portion = Decimal('0.20')
EMH_Ext_Hold = Decimal('42000.0')
def b1m1(step, sL, s):
# print('b1m1')
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
buy = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
price = s['Price']
return {'EMH_buy': buy, 'EMH_buy_P': price}
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
return {'EMH_buy': 0}
else:
return {'EMH_buy': 0}
def b1m2(step, sL, s):
# print('b1m2')
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
return {'EMH_sell': 0}
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
sell = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
price = s['Price']
return {'EMH_sell': sell, 'EMH_sell_P': price}
else:
return {'EMH_sell': 0}
# BEHAVIOR 3: Herding
Herd_portion = Decimal('0.20')
Herd_Ext_Hold = Decimal('42000.0')
Herd_UB = Decimal('0.10') # UPPER BOUND
Herd_LB = Decimal('0.10') # LOWER BOUND
def b3m2(step, sL, s):
theta = (s['Z']*Herd_portion*s['Price'])/(s['Z']*Herd_portion*s['Price'] + Herd_Ext_Hold * s['P_Ext_Markets'])
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
if (s['Price'] - s['Price_Signal']) < - Herd_LB:
sell = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
price = s['Price'] - (s['Price_Signal'] / s['Price'])
return {'herd_sell': sell, 'herd_buy': 0, 'herd_sell_P': price}
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
elif (s['Price'] - s['Price_Signal']) > Herd_UB:
buy = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
price = s['Price'] + (s['Price'] / s['Price_Signal'])
return {'herd_sell': 0, 'herd_buy': buy, 'herd_buy_P': price}
else:
return {'herd_sell': 0, 'herd_buy': 0}
# BEHAVIOR 4: HODLers
HODL_belief = Decimal('10.0')
HODL_portion = Decimal('0.20')
HODL_Ext_Hold = Decimal('4200.0')
def b4m2(step, sL, s):
# print('b4m2')
theta = (s['Z']*HODL_portion*s['Price'])/(s['Z']*HODL_portion*s['Price'] + HODL_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < 1/HODL_belief*(theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
sell = beta * theta*HODL_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HODL_portion*(1-theta))
price = s['Price']
return {'HODL_sell': sell, 'HODL_sell_P': price}
elif s['Price'] > (theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
return {'HODL_sell': 0}
else:
return {'HODL_sell': 0}
# BEHAVIOR 7: Endogenous Information Updating (EIU)
# Short Term Price Signal, Lower Threshold = BOT-like
EIU_portion = Decimal('0.20')
EIU_Ext_Hold = Decimal('42000.0')
EIU_UB = Decimal('0.50') # UPPER BOUND
EIU_LB = Decimal('0.50') # LOWER BOUND
def b7m2(step, sL, s):
theta = (s['Z']*EIU_portion*s['Price'])/(s['Z']*EIU_portion*s['Price'] + EIU_Ext_Hold * s['P_Ext_Markets'])
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
if (s['Price'] - s['Price_Signal']) < - EIU_LB:
sell = beta * theta*EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EIU_portion*(1-theta))
price = s['Price'] + (s['Price_Signal'] / s['Price'])
return {'EIU_sell': sell, 'EIU_buy': 0, 'EIU_sell_P': price}
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
elif (s['Price'] - s['Price_Signal']) > EIU_UB:
buy = beta * theta* EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']* EIU_portion*(1-theta))
price = s['Price'] - (s['Price'] / s['Price_Signal'])
return {'EIU_sell': 0, 'EIU_buy': buy, 'EIU_buy_P': price}
else:
return {'EIU_sell': 0, 'EIU_buy': 0}
# BEHAVIOR 7b: Endogenous Information Updating (EIU)
# Longer Term Price Signal, Higher Threshold = Human-Like
HEIU_portion = Decimal('0.20')
HEIU_Ext_Hold = Decimal('42000.0')
HEIU_UB = Decimal('2.0') # UPPER BOUND
HEIU_LB = Decimal('2.0') # LOWER BOUND
def b7hm2(step, sL, s):
theta = (s['Z']*HEIU_portion*s['Price'])/(s['Z']*HEIU_portion*s['Price'] + HEIU_Ext_Hold * s['P_Ext_Markets'])
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
if (s['Price'] - s['Price_Signal_2']) < - HEIU_LB:
sell = beta * theta* HEIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HEIU_portion*(1-theta))
price = s['Price'] + (s['Price_Signal_2'] / s['Price'])
return {'HEIU_sell': sell, 'HEIU_buy': 0, 'HEIU_sell_P': price}
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
elif (s['Price'] - s['Price_Signal_2']) > HEIU_UB:
buy = beta * theta* HEIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']* HEIU_portion*(1-theta))
price = s['Price'] - (s['Price'] / s['Price_Signal_2'])
return {'HEIU_sell': 0, 'HEIU_buy': buy, 'HEIU_buy_P': price}
else:
return {'HEIU_sell': 0, 'HEIU_buy': 0}
# STATES
# ZEUS Fixed Supply
def s1m1(step, sL, s, _input):
y = 'Z'
x = s['Z'] #+ _input # / Psignal_int
return (y, x)
# def s2m1(step, sL, s, _input):
# y = 'Price'
# x = (s['P_Ext_Markets'] - _input['EMH_buy']) / s['Z'] * 10000
# #x= alpha * s['Z'] + (1 - alpha)*s['Price']
# return (y, x)
def s3m1(step, sL, s, _input):
y = 'Buy_Log'
x = _input['EMH_buy'] + _input['herd_buy'] + _input['EIU_buy'] + _input['HEIU_buy'] # / Psignal_int
return (y, x)
def s4m2(step, sL, s, _input):
y = 'Sell_Log'
x = _input['EMH_sell'] + _input['HODL_sell'] + _input['herd_sell'] + _input['EIU_sell'] + _input['HEIU_sell'] # / Psignal_int
return (y, x)
# def s3m3(step, sL, s, _input):
# y = 'Buy_Log'
# x = s['Buy_Log'] + _input # / Psignal_int
# return (y, x)
# Price Update
def s2m3(step, sL, s, _input):
y = 'Price'
#var1 = Decimal.from_float(s['Buy_Log'])
x = s['Price'] + (s['Buy_Log'] /s['Z'] ) - (s['Sell_Log']/s['Z'] )
#+ np.divide(s['Buy_Log'],s['Z']) - np.divide() # / Psignal_int
return (y, x)
def s5m3(step, sL, s, _input):
y = 'Price_Signal'
x = alpha * s['Price'] + (1 - alpha)*s['Price_Signal']
return (y, x)
def s6m3(step, sL, s, _input):
y = 'Price_Signal_2'
x = alpha_2 * s['Price'] + (1 - alpha_2)*s['Price_Signal_2']
return (y, x)
def s6m1(step, sL, s, _input):
y = 'P_Ext_Markets'
x = s['P_Ext_Markets'] - _input
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
return (y, x)
# def s2m2(step, sL, s, _input):
# y = 'Price'
# x = (s['P_Ext_Markets'] - _input) /s['Z'] *10000
# x= alpha * s['Z'] + (1 - alpha)*s['Price']
# return (y, x)
# Exogenous States
proc_one_coef_A = -125
proc_one_coef_B = 125
# A change in belief of actual price, passed onto behaviors to make action
def es4p2(step, sL, s, _input):
y = 'P_Ext_Markets'
x = s['P_Ext_Markets'] + bound_norm_random(seed['z'], proc_one_coef_A, proc_one_coef_B)
return (y,x)
def es5p2(step, sL, s, _input): # accept timedelta instead of timedelta params
y = 'timestamp'
x = ep_time_step(s, s['timestamp'], seconds=1)
return (y, x)
#Environment States
# NONE
# Genesis States
state_dict = {
'Z': Decimal(21000000.0),
'Price': Decimal(100.0), # Initialize = Z for EMA
'Buy_Log': Decimal(0.0),
'Sell_Log': Decimal(0.0),
'Price_Signal': Decimal(100.0),
'Price_Signal_2': Decimal(100.0),
'Trans': Decimal(0.0),
'P_Ext_Markets': Decimal(25000.0),
'timestamp': '2018-10-01 15:16:24'
}
def env_proc_id(x):
return x
env_processes = {
# "P_Ext_Markets": env_proc_id
}
exogenous_states = exo_update_per_ts(
{
"P_Ext_Markets": es4p2,
"timestamp": es5p2
}
)
sim_config = {
"N": 1,
"T": range(1000)
}
# test return vs. non-return functions as lambdas
# test fully defined functions
mechanisms = {
"m1": {
"behaviors": {
"b1": b1m1,
"b3": b3m2,
"b7": b7m2,
"b7h": b7hm2
},
"states": {
"Z": s1m1,
"Buy_Log": s3m1
}
},
"m2": {
"behaviors": {
"b1": b1m2,
"b3": b3m2,
"b4": b4m2,
"b7": b7m2,
"b7h": b7hm2
},
"states": {
"Sell_Log": s4m2
}
},
"m3": {
"behaviors": {
},
"states": {
"Price": s2m3,
"Price_Signal": s5m3,
"Price_Signal_2": s6m3,
}
}
}
configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms))

View File

@ -0,0 +1,319 @@
from decimal import Decimal
import numpy as np
from datetime import timedelta
from SimCAD import configs
from SimCAD.configuration import Configuration
from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \
ep_time_step
seed = {
'z': np.random.RandomState(1)
}
# Signals
# Pr_signal
beta = Decimal('0.25') # agent response gain
beta_LT = Decimal('0.1') # LT agent response gain
# alpha = .67, 2 block moving average
alpha = Decimal('0.67')
# 21 day EMA forgetfullness between 0 and 1, closer to 1 discounts older obs quicker, should be 2/(N+1)
# 21 * 3 mech steps, 2/64 = 0.03125
alpha_2 = Decimal('0.03125')
max_withdraw_factor = Decimal('0.9')
external_draw = Decimal('0.01') # between 0 and 1 to draw Buy_Log to external
#alpha * s['Zeus_ST'] + (1 - alpha)*s['Zeus_LT']
# Stochastic process factors
correction_factor = Decimal('0.01')
volatility = Decimal('5.0')
# Buy_Log_signal =
# Z_signal =
# Price_signal =
# TDR_draw_signal =
# P_Ext_Markets_signal =
# Behaviors per Mechanism
# BEHAVIOR 1: EMH Trader
EMH_portion = Decimal('0.20')
EMH_Ext_Hold = Decimal('42000.0')
def b1m1(step, sL, s):
# print('b1m1')
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
buy = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
price = s['Price']
return {'EMH_buy': buy, 'EMH_buy_P': price}
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
price = 0
return {'EMH_buy': 0, 'EMH_buy_P': price}
else:
price = 0
return {'EMH_buy': 0, 'EMH_buy_P': price}
def b1m2(step, sL, s):
# print('b1m2')
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
return {'EMH_sell': 0}
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
sell = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
price = s['Price']
return {'EMH_sell': sell, 'EMH_sell_P': price}
else:
return {'EMH_sell': 0}
# BEHAVIOR 3: Herding
Herd_portion = Decimal('0.20')
Herd_Ext_Hold = Decimal('42000.0')
Herd_UB = Decimal('0.10') # UPPER BOUND
Herd_LB = Decimal('0.10') # LOWER BOUND
def b3m2(step, sL, s):
theta = (s['Z']*Herd_portion*s['Price'])/(s['Z']*Herd_portion*s['Price'] + Herd_Ext_Hold * s['P_Ext_Markets'])
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
if (s['Price'] - s['Price_Signal']) < - Herd_LB:
sell = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
price = s['Price'] - (s['Price_Signal'] / s['Price'])
return {'herd_sell': sell, 'herd_buy': 0, 'herd_sell_P': price}
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
elif (s['Price'] - s['Price_Signal']) > Herd_UB:
buy = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
price = s['Price'] + (s['Price'] / s['Price_Signal'])
return {'herd_sell': 0, 'herd_buy': buy, 'herd_buy_P': price}
else:
return {'herd_sell': 0, 'herd_buy': 0, 'herd_buy_P':0}
# BEHAVIOR 4: HODLers
HODL_belief = Decimal('10.0')
HODL_portion = Decimal('0.20')
HODL_Ext_Hold = Decimal('4200.0')
def b4m2(step, sL, s):
# print('b4m2')
theta = (s['Z']*HODL_portion*s['Price'])/(s['Z']*HODL_portion*s['Price'] + HODL_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < 1/HODL_belief*(theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
sell = beta * theta*HODL_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HODL_portion*(1-theta))
price = s['Price']
return {'HODL_sell': sell, 'HODL_sell_P': price}
elif s['Price'] > (theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
return {'HODL_sell': 0}
else:
return {'HODL_sell': 0}
# BEHAVIOR 7: Endogenous Information Updating (EIU)
# Short Term Price Signal, Lower Threshold = BOT-like
EIU_portion = Decimal('0.20')
EIU_Ext_Hold = Decimal('42000.0')
EIU_UB = Decimal('0.50') # UPPER BOUND
EIU_LB = Decimal('0.50') # LOWER BOUND
def b7m2(step, sL, s):
theta = (s['Z']*EIU_portion*s['Price'])/(s['Z']*EIU_portion*s['Price'] + EIU_Ext_Hold * s['P_Ext_Markets'])
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
if (s['Price'] - s['Price_Signal']) < - EIU_LB:
sell = beta * theta*EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EIU_portion*(1-theta))
price = s['Price'] + (s['Price_Signal'] / s['Price'])
return {'EIU_sell': sell, 'EIU_buy': 0, 'EIU_sell_P': price}
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
elif (s['Price'] - s['Price_Signal']) > EIU_UB:
buy = beta * theta* EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']* EIU_portion*(1-theta))
price = s['Price'] - (s['Price'] / s['Price_Signal'])
return {'EIU_sell': 0, 'EIU_buy': buy, 'EIU_buy_P': price}
else:
return {'EIU_sell': 0, 'EIU_buy': 0}
# BEHAVIOR 7b: Endogenous Information Updating (EIU)
# Longer Term Price Signal, Higher Threshold = Human-Like
HEIU_portion = Decimal('0.20')
HEIU_Ext_Hold = Decimal('42000.0')
HEIU_UB = Decimal('2.0') # UPPER BOUND
HEIU_LB = Decimal('2.0') # LOWER BOUND
def b7hm2(step, sL, s):
theta = (s['Z']*HEIU_portion*s['Price'])/(s['Z']*HEIU_portion*s['Price'] + HEIU_Ext_Hold * s['P_Ext_Markets'])
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
if (s['Price'] - s['Price_Signal_2']) < - HEIU_LB:
sell = beta * theta* HEIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HEIU_portion*(1-theta))
price = s['Price'] + (s['Price_Signal_2'] / s['Price'])
return {'HEIU_sell': sell, 'HEIU_buy': 0, 'HEIU_sell_P': price}
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
elif (s['Price'] - s['Price_Signal_2']) > HEIU_UB:
buy = beta * theta* HEIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']* HEIU_portion*(1-theta))
price = s['Price'] - (s['Price'] / s['Price_Signal_2'])
return {'HEIU_sell': 0, 'HEIU_buy': buy, 'HEIU_buy_P': price}
else:
return {'HEIU_sell': 0, 'HEIU_buy': 0}
# STATES
# ZEUS Fixed Supply
def s1m1(step, sL, s, _input):
y = 'Z'
x = s['Z'] #+ _input # / Psignal_int
return (y, x)
# def s2m1(step, sL, s, _input):
# y = 'Price'
# x = (s['P_Ext_Markets'] - _input['EMH_buy']) / s['Z'] * 10000
# #x= alpha * s['Z'] + (1 - alpha)*s['Price']
# return (y, x)
def s3m1(step, sL, s, _input):
y = 'Buy_Log'
x = np.zeros(4)
x[0] = _input['EMH_buy']
x[1] = _input['EMH_buy_P']
x[2] = _input['herd_buy']
x[3] = _input['herd_buy_P']
# = _input['EMH_buy'] + _input['herd_buy'] + _input['EIU_buy'] + _input['HEIU_buy'] # / Psignal_int
return (y, x) #[0], x[1])
def s4m2(step, sL, s, _input):
y = 'Sell_Log'
x = _input['EMH_sell'] + _input['HODL_sell'] + _input['herd_sell'] + _input['EIU_sell'] + _input['HEIU_sell'] # / Psignal_int
return (y, x)
# def s3m3(step, sL, s, _input):
# y = 'Buy_Log'
# x = s['Buy_Log'] + _input # / Psignal_int
# return (y, x)
# Price Update
def s2m3(step, sL, s, _input):
y = 'Price'
#var1 = Decimal.from_float(s['Buy_Log'])
x = s['Price'] + (Decimal(s['Buy_Log'][0])) / s['Z'] # - (s['Sell_Log']/s['Z'] ) # for buy log term /s['Z'] )
#+ np.divide(s['Buy_Log'],s['Z']) - np.divide() # / Psignal_int
return (y, x)
def s5m3(step, sL, s, _input):
y = 'Price_Signal'
x = alpha * s['Price'] + (1 - alpha)*s['Price_Signal']
return (y, x)
def s6m3(step, sL, s, _input):
y = 'Price_Signal_2'
x = alpha_2 * s['Price'] + (1 - alpha_2)*s['Price_Signal_2']
return (y, x)
def s6m1(step, sL, s, _input):
y = 'P_Ext_Markets'
x = s['P_Ext_Markets'] - _input
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
return (y, x)
# def s2m2(step, sL, s, _input):
# y = 'Price'
# x = (s['P_Ext_Markets'] - _input) /s['Z'] *10000
# x= alpha * s['Z'] + (1 - alpha)*s['Price']
# return (y, x)
# Exogenous States
proc_one_coef_A = -125
proc_one_coef_B = 125
# A change in belief of actual price, passed onto behaviors to make action
def es4p2(step, sL, s, _input):
y = 'P_Ext_Markets'
x = s['P_Ext_Markets'] + bound_norm_random(seed['z'], proc_one_coef_A, proc_one_coef_B)
return (y,x)
ts_format = '%Y-%m-%d %H:%M:%S'
t_delta = timedelta(days=0, minutes=0, seconds=1)
def es5p2(step, sL, s, _input):
y = 'timestamp'
x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta)
return (y, x)
#Environment States
# NONE
# Genesis States
state_dict = {
'Z': Decimal(21000000.0),
'Price': Decimal(100.0), # Initialize = Z for EMA
'Buy_Log': Decimal(0.0),
'Sell_Log': Decimal(0.0),
'Price_Signal': Decimal(100.0),
'Price_Signal_2': Decimal(100.0),
'Trans': Decimal(0.0),
'P_Ext_Markets': Decimal(25000.0),
'timestamp': '2018-10-01 15:16:24'
}
def env_proc_id(x):
return x
env_processes = {
# "P_Ext_Markets": env_proc_id
}
exogenous_states = exo_update_per_ts(
{
"P_Ext_Markets": es4p2,
"timestamp": es5p2
}
)
sim_config = {
"N": 1,
"T": range(1000)
}
# test return vs. non-return functions as lambdas
# test fully defined functions
mechanisms = {
"m1": {
"behaviors": {
"b1": b1m1,
"b3": b3m2,
"b7": b7m2,
"b7h": b7hm2
},
"states": {
"Z": s1m1,
"Buy_Log": s3m1
}
},
"m2": {
"behaviors": {
"b1": b1m2,
"b3": b3m2,
"b4": b4m2,
"b7": b7m2,
"b7h": b7hm2
},
"states": {
"Sell_Log": s4m2
}
},
"m3": {
"behaviors": {
},
"states": {
"Price": s2m3,
"Price_Signal": s5m3,
"Price_Signal_2": s6m3,
}
}
}
configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms))

View File

@ -0,0 +1,319 @@
from decimal import Decimal
import numpy as np
from datetime import timedelta
from SimCAD import configs
from SimCAD.configuration import Configuration
from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \
ep_time_step
seed = {
'z': np.random.RandomState(1)
}
# Signals
# Pr_signal
beta = Decimal('0.25') # agent response gain
beta_LT = Decimal('0.1') # LT agent response gain
# alpha = .67, 2 block moving average
alpha = Decimal('0.67')
# 21 day EMA forgetfullness between 0 and 1, closer to 1 discounts older obs quicker, should be 2/(N+1)
# 21 * 3 mech steps, 2/64 = 0.03125
alpha_2 = Decimal('0.03125')
max_withdraw_factor = Decimal('0.9')
external_draw = Decimal('0.01') # between 0 and 1 to draw Buy_Log to external
#alpha * s['Zeus_ST'] + (1 - alpha)*s['Zeus_LT']
# Stochastic process factors
correction_factor = Decimal('0.01')
volatility = Decimal('5.0')
# Buy_Log_signal =
# Z_signal =
# Price_signal =
# TDR_draw_signal =
# P_Ext_Markets_signal =
# Behaviors per Mechanism
# BEHAVIOR 1: EMH Trader
EMH_portion = Decimal('0.20')
EMH_Ext_Hold = Decimal('42000.0')
def b1m1(step, sL, s):
# print('b1m1')
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
buy = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
price = s['Price']
return {'EMH_buy': buy, 'EMH_buy_P': price}
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
price = 0
return {'EMH_buy': 0, 'EMH_buy_P': price}
else:
price = 0
return {'EMH_buy': 0, 'EMH_buy_P': price}
def b1m2(step, sL, s):
# print('b1m2')
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
return {'EMH_sell': 0}
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
sell = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
price = s['Price']
return {'EMH_sell': sell, 'EMH_sell_P': price}
else:
return {'EMH_sell': 0}
# BEHAVIOR 3: Herding
Herd_portion = Decimal('0.20')
Herd_Ext_Hold = Decimal('42000.0')
Herd_UB = Decimal('0.10') # UPPER BOUND
Herd_LB = Decimal('0.10') # LOWER BOUND
def b3m2(step, sL, s):
theta = (s['Z']*Herd_portion*s['Price'])/(s['Z']*Herd_portion*s['Price'] + Herd_Ext_Hold * s['P_Ext_Markets'])
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
if (s['Price'] - s['Price_Signal']) < - Herd_LB:
sell = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
price = s['Price'] - (s['Price_Signal'] / s['Price'])
return {'herd_sell': sell, 'herd_buy': 0, 'herd_sell_P': price}
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
elif (s['Price'] - s['Price_Signal']) > Herd_UB:
buy = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
price = s['Price'] + (s['Price'] / s['Price_Signal'])
return {'herd_sell': 0, 'herd_buy': buy, 'herd_buy_P': price}
else:
return {'herd_sell': 0, 'herd_buy': 0, 'herd_buy_P':0}
# BEHAVIOR 4: HODLers
HODL_belief = Decimal('10.0')
HODL_portion = Decimal('0.20')
HODL_Ext_Hold = Decimal('4200.0')
def b4m2(step, sL, s):
# print('b4m2')
theta = (s['Z']*HODL_portion*s['Price'])/(s['Z']*HODL_portion*s['Price'] + HODL_Ext_Hold * s['P_Ext_Markets'])
if s['Price'] < 1/HODL_belief*(theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
sell = beta * theta*HODL_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HODL_portion*(1-theta))
price = s['Price']
return {'HODL_sell': sell, 'HODL_sell_P': price}
elif s['Price'] > (theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
return {'HODL_sell': 0}
else:
return {'HODL_sell': 0}
# BEHAVIOR 7: Endogenous Information Updating (EIU)
# Short Term Price Signal, Lower Threshold = BOT-like
EIU_portion = Decimal('0.20')
EIU_Ext_Hold = Decimal('42000.0')
EIU_UB = Decimal('0.50') # UPPER BOUND
EIU_LB = Decimal('0.50') # LOWER BOUND
def b7m2(step, sL, s):
theta = (s['Z']*EIU_portion*s['Price'])/(s['Z']*EIU_portion*s['Price'] + EIU_Ext_Hold * s['P_Ext_Markets'])
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
if (s['Price'] - s['Price_Signal']) < - EIU_LB:
sell = beta * theta*EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EIU_portion*(1-theta))
price = s['Price'] + (s['Price_Signal'] / s['Price'])
return {'EIU_sell': sell, 'EIU_buy': 0, 'EIU_sell_P': price}
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
elif (s['Price'] - s['Price_Signal']) > EIU_UB:
buy = beta * theta* EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']* EIU_portion*(1-theta))
price = s['Price'] - (s['Price'] / s['Price_Signal'])
return {'EIU_sell': 0, 'EIU_buy': buy, 'EIU_buy_P': price}
else:
return {'EIU_sell': 0, 'EIU_buy': 0}
# BEHAVIOR 7b: Endogenous Information Updating (EIU)
# Longer Term Price Signal, Higher Threshold = Human-Like
HEIU_portion = Decimal('0.20')
HEIU_Ext_Hold = Decimal('42000.0')
HEIU_UB = Decimal('2.0') # UPPER BOUND
HEIU_LB = Decimal('2.0') # LOWER BOUND
def b7hm2(step, sL, s):
theta = (s['Z']*HEIU_portion*s['Price'])/(s['Z']*HEIU_portion*s['Price'] + HEIU_Ext_Hold * s['P_Ext_Markets'])
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
if (s['Price'] - s['Price_Signal_2']) < - HEIU_LB:
sell = beta * theta* HEIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HEIU_portion*(1-theta))
price = s['Price'] + (s['Price_Signal_2'] / s['Price'])
return {'HEIU_sell': sell, 'HEIU_buy': 0, 'HEIU_sell_P': price}
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
elif (s['Price'] - s['Price_Signal_2']) > HEIU_UB:
buy = beta * theta* HEIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']* HEIU_portion*(1-theta))
price = s['Price'] - (s['Price'] / s['Price_Signal_2'])
return {'HEIU_sell': 0, 'HEIU_buy': buy, 'HEIU_buy_P': price}
else:
return {'HEIU_sell': 0, 'HEIU_buy': 0}
# STATES
# ZEUS Fixed Supply
def s1m1(step, sL, s, _input):
y = 'Z'
x = s['Z'] #+ _input # / Psignal_int
return (y, x)
# def s2m1(step, sL, s, _input):
# y = 'Price'
# x = (s['P_Ext_Markets'] - _input['EMH_buy']) / s['Z'] * 10000
# #x= alpha * s['Z'] + (1 - alpha)*s['Price']
# return (y, x)
def s3m1(step, sL, s, _input):
y = 'Buy_Log'
x = np.zeros(4)
x[0] = _input['EMH_buy']
x[1] = _input['EMH_buy_P']
x[2] = _input['herd_buy']
x[3] = _input['herd_buy_P']
# = _input['EMH_buy'] + _input['herd_buy'] + _input['EIU_buy'] + _input['HEIU_buy'] # / Psignal_int
return (y, x) #[0], x[1])
def s4m2(step, sL, s, _input):
y = 'Sell_Log'
x = _input['EMH_sell'] + _input['HODL_sell'] + _input['herd_sell'] + _input['EIU_sell'] + _input['HEIU_sell'] # / Psignal_int
return (y, x)
# def s3m3(step, sL, s, _input):
# y = 'Buy_Log'
# x = s['Buy_Log'] + _input # / Psignal_int
# return (y, x)
# Price Update
def s2m3(step, sL, s, _input):
y = 'Price'
#var1 = Decimal.from_float(s['Buy_Log'])
x = s['Price'] + (Decimal(s['Buy_Log'][0] )) /s['Z'] # - (s['Sell_Log']/s['Z'] ) # for buy log term /s['Z'] )
#+ np.divide(s['Buy_Log'],s['Z']) - np.divide() # / Psignal_int
return (y, x)
def s5m3(step, sL, s, _input):
y = 'Price_Signal'
x = alpha * s['Price'] + (1 - alpha)*s['Price_Signal']
return (y, x)
def s6m3(step, sL, s, _input):
y = 'Price_Signal_2'
x = alpha_2 * s['Price'] + (1 - alpha_2)*s['Price_Signal_2']
return (y, x)
def s6m1(step, sL, s, _input):
y = 'P_Ext_Markets'
x = s['P_Ext_Markets'] - _input
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
return (y, x)
# def s2m2(step, sL, s, _input):
# y = 'Price'
# x = (s['P_Ext_Markets'] - _input) /s['Z'] *10000
# x= alpha * s['Z'] + (1 - alpha)*s['Price']
# return (y, x)
# Exogenous States
proc_one_coef_A = -125
proc_one_coef_B = 125
# A change in belief of actual price, passed onto behaviors to make action
def es4p2(step, sL, s, _input):
y = 'P_Ext_Markets'
x = s['P_Ext_Markets'] + bound_norm_random(seed['z'], proc_one_coef_A, proc_one_coef_B)
return (y,x)
ts_format = '%Y-%m-%d %H:%M:%S'
t_delta = timedelta(days=0, minutes=0, seconds=1)
def es5p2(step, sL, s, _input):
y = 'timestamp'
x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta)
return (y, x)
#Environment States
# NONE
# Genesis States
state_dict = {
'Z': Decimal(21000000.0),
'Price': Decimal(100.0), # Initialize = Z for EMA
'Buy_Log': Decimal(0.0),
'Sell_Log': Decimal(0.0),
'Price_Signal': Decimal(100.0),
'Price_Signal_2': Decimal(100.0),
'Trans': Decimal(0.0),
'P_Ext_Markets': Decimal(25000.0),
'timestamp': '2018-10-01 15:16:24'
}
def env_proc_id(x):
return x
env_processes = {
# "P_Ext_Markets": env_proc_id
}
exogenous_states = exo_update_per_ts(
{
"P_Ext_Markets": es4p2,
"timestamp": es5p2
}
)
sim_config = {
"N": 1,
"T": range(1000)
}
# test return vs. non-return functions as lambdas
# test fully defined functions
mechanisms = {
"m1": {
"behaviors": {
"b1": b1m1,
"b3": b3m2,
"b7": b7m2,
"b7h": b7hm2
},
"states": {
"Z": s1m1,
"Buy_Log": s3m1
}
},
"m2": {
"behaviors": {
"b1": b1m2,
"b3": b3m2,
"b4": b4m2,
"b7": b7m2,
"b7h": b7hm2
},
"states": {
"Sell_Log": s4m2
}
},
"m3": {
"behaviors": {
},
"states": {
"Price": s2m3,
"Price_Signal": s5m3,
"Price_Signal_2": s6m3,
}
}
}
configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms))

File diff suppressed because one or more lines are too long

View File

@ -1,27 +0,0 @@
ds1,ds2,ds3,run,substep,timestep
0,0,1,1,0,0
1,40,5,1,1,1
2,40,5,1,2,1
3,40,5,1,3,1
4,40,5,1,1,2
5,40,5,1,2,2
6,40,5,1,3,2
7,40,5,1,1,3
8,40,5,1,2,3
9,40,5,1,3,3
10,40,5,1,1,4
11,40,5,1,2,4
12,40,5,1,3,4
0,0,1,2,0,0
1,40,5,2,1,1
2,40,5,2,2,1
3,40,5,2,3,1
4,40,5,2,1,2
5,40,5,2,2,2
6,40,5,2,3,2
7,40,5,2,1,3
8,40,5,2,2,3
9,40,5,2,3,3
10,40,5,2,1,4
11,40,5,2,2,4
12,40,5,2,3,4
1 ds1 ds2 ds3 run substep timestep
2 0 0 1 1 0 0
3 1 40 5 1 1 1
4 2 40 5 1 2 1
5 3 40 5 1 3 1
6 4 40 5 1 1 2
7 5 40 5 1 2 2
8 6 40 5 1 3 2
9 7 40 5 1 1 3
10 8 40 5 1 2 3
11 9 40 5 1 3 3
12 10 40 5 1 1 4
13 11 40 5 1 2 4
14 12 40 5 1 3 4
15 0 0 1 2 0 0
16 1 40 5 2 1 1
17 2 40 5 2 2 1
18 3 40 5 2 3 1
19 4 40 5 2 1 2
20 5 40 5 2 2 2
21 6 40 5 2 3 2
22 7 40 5 2 1 3
23 8 40 5 2 2 3
24 9 40 5 2 3 3
25 10 40 5 2 1 4
26 11 40 5 2 2 4
27 12 40 5 2 3 4

View File

@ -1,24 +0,0 @@
import pandas as pd
from tabulate import tabulate
# The following imports NEED to be in the exact order
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
# from simulations.validation import config1_test_pipe
# from simulations.validation import config1
from simulations.validation import write_simulation
from cadCAD import configs
exec_mode = ExecutionMode()
print("Simulation Execution: Single Configuration")
print()
first_config = configs # only contains config1
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
run = Executor(exec_context=single_proc_ctx, configs=first_config)
raw_result, _ = run.main()
result = pd.DataFrame(raw_result)
result.to_csv('/Users/jjodesty/Projects/DiffyQ-SimCAD/simulations/external_data/output.csv', index=False)
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()

View File

@ -1,24 +0,0 @@
import pandas as pd
from tabulate import tabulate
# The following imports NEED to be in the exact order
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from simulations.validation import sweep_config
from cadCAD import configs
exec_mode = ExecutionMode()
print("Simulation Execution: Concurrent Execution")
multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc)
run = Executor(exec_context=multi_proc_ctx, configs=configs)
i = 0
config_names = ['sweep_config_A', 'sweep_config_B']
for raw_result, tensor_field in run.execute():
result = pd.DataFrame(raw_result)
print()
print("Tensor Field: " + config_names[i])
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()
i += 1

View File

@ -1,160 +0,0 @@
import numpy as np
from datetime import timedelta
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import bound_norm_random, config_sim, time_step, env_trigger
seeds = {
'z': np.random.RandomState(1),
'a': np.random.RandomState(2),
'b': np.random.RandomState(3),
'c': np.random.RandomState(4)
}
# Policies per Mechanism
def p1m1(_g, step, sL, s):
return {'param1': 1}
def p2m1(_g, step, sL, s):
return {'param1': 1, 'param2': 4}
def p1m2(_g, step, sL, s):
return {'param1': 'a', 'param2': 2}
def p2m2(_g, step, sL, s):
return {'param1': 'b', 'param2': 4}
def p1m3(_g, step, sL, s):
return {'param1': ['c'], 'param2': np.array([10, 100])}
def p2m3(_g, step, sL, s):
return {'param1': ['d'], 'param2': np.array([20, 200])}
# Internal States per Mechanism
def s1m1(_g, step, sL, s, _input):
y = 's1'
x = s['s1'] + 1
return (y, x)
def s2m1(_g, step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m2(_g, step, sL, s, _input):
y = 's1'
x = s['s1'] + 1
return (y, x)
def s2m2(_g, step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m3(_g, step, sL, s, _input):
y = 's1'
x = s['s1'] + 1
return (y, x)
def s2m3(_g, step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def policies(_g, step, sL, s, _input):
y = 'policies'
x = _input
return (y, x)
# Exogenous States
proc_one_coef_A = 0.7
proc_one_coef_B = 1.3
def es3(_g, step, sL, s, _input):
y = 's3'
x = s['s3'] * bound_norm_random(seeds['a'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
def es4(_g, step, sL, s, _input):
y = 's4'
x = s['s4'] * bound_norm_random(seeds['b'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
def update_timestamp(_g, step, sL, s, _input):
y = 'timestamp'
return y, time_step(dt_str=s[y], dt_format='%Y-%m-%d %H:%M:%S', _timedelta=timedelta(days=0, minutes=0, seconds=1))
# Genesis States
genesis_states = {
's1': 0.0,
's2': 0.0,
's3': 1.0,
's4': 1.0,
'timestamp': '2018-10-01 15:16:24'
}
# Environment Process
# ToDo: Depreciation Waring for env_proc_trigger convention
trigger_timestamps = ['2018-10-01 15:16:25', '2018-10-01 15:16:27', '2018-10-01 15:16:29']
env_processes = {
"s3": [lambda _g, x: 5],
"s4": env_trigger(3)(trigger_field='timestamp', trigger_vals=trigger_timestamps, funct_list=[lambda _g, x: 10])
}
partial_state_update_block = [
{
"policies": {
"b1": p1m1,
"b2": p2m1
},
"variables": {
"s1": s1m1,
"s2": s2m1,
"s3": es3,
"s4": es4,
"timestamp": update_timestamp
}
},
{
"policies": {
"b1": p1m2,
"b2": p2m2
},
"variables": {
"s1": s1m2,
"s2": s2m2,
# "s3": es3p1,
# "s4": es4p2,
}
},
{
"policies": {
"b1": p1m3,
"b2": p2m3
},
"variables": {
"s1": s1m3,
"s2": s2m3,
# "s3": es3p1,
# "s4": es4p2,
}
}
]
sim_config = config_sim(
{
"N": 1,
# "N": 5,
"T": range(5),
}
)
append_configs(
sim_configs=sim_config,
initial_state=genesis_states,
env_processes=env_processes,
partial_state_update_blocks=partial_state_update_block,
policy_ops=[lambda a, b: a + b]
)

View File

@ -1,147 +0,0 @@
import numpy as np
from datetime import timedelta
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import bound_norm_random, config_sim, env_trigger, time_step
seeds = {
'z': np.random.RandomState(1),
'a': np.random.RandomState(2),
'b': np.random.RandomState(3),
'c': np.random.RandomState(3)
}
# Policies per Mechanism
def p1m1(_g, step, sL, s):
return {'param1': 1}
def p2m1(_g, step, sL, s):
return {'param2': 4}
def p1m2(_g, step, sL, s):
return {'param1': 'a', 'param2': 2}
def p2m2(_g, step, sL, s):
return {'param1': 'b', 'param2': 4}
def p1m3(_g, step, sL, s):
return {'param1': ['c'], 'param2': np.array([10, 100])}
def p2m3(_g, step, sL, s):
return {'param1': ['d'], 'param2': np.array([20, 200])}
# Internal States per Mechanism
def s1m1(_g, step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m1(_g, step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m2(_g, step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m2(_g, step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m3(_g, step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m3(_g, step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
# Exogenous States
proc_one_coef_A = 0.7
proc_one_coef_B = 1.3
def es3(_g, step, sL, s, _input):
y = 's3'
x = s['s3'] * bound_norm_random(seeds['a'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
def es4(_g, step, sL, s, _input):
y = 's4'
x = s['s4'] * bound_norm_random(seeds['b'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
def update_timestamp(_g, step, sL, s, _input):
y = 'timestamp'
return y, time_step(dt_str=s[y], dt_format='%Y-%m-%d %H:%M:%S', _timedelta=timedelta(days=0, minutes=0, seconds=1))
# Genesis States
genesis_states = {
's1': 0,
's2': 0,
's3': 1,
's4': 1,
'timestamp': '2018-10-01 15:16:24'
}
# Environment Process
# ToDo: Depreciation Waring for env_proc_trigger convention
trigger_timestamps = ['2018-10-01 15:16:25', '2018-10-01 15:16:27', '2018-10-01 15:16:29']
env_processes = {
"s3": [lambda _g, x: 5],
"s4": env_trigger(3)(trigger_field='timestamp', trigger_vals=trigger_timestamps, funct_list=[lambda _g, x: 10])
}
partial_state_update_block = {
"m1": {
"policies": {
"b1": p1m1,
# "b2": p2m1
},
"states": {
"s1": s1m1,
# "s2": s2m1
"s3": es3,
"s4": es4,
"timestep": update_timestamp
}
},
"m2": {
"policies": {
"b1": p1m2,
# "b2": p2m2
},
"states": {
"s1": s1m2,
# "s2": s2m2
}
},
"m3": {
"policies": {
"b1": p1m3,
"b2": p2m3
},
"states": {
"s1": s1m3,
"s2": s2m3
}
}
}
sim_config = config_sim(
{
"N": 2,
"T": range(5),
}
)
append_configs(
sim_configs=sim_config,
initial_state=genesis_states,
env_processes=env_processes,
partial_state_update_blocks=partial_state_update_block
)

View File

@ -1,67 +0,0 @@
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import config_sim
import pandas as pd
from cadCAD.utils import SilentDF
df = SilentDF(pd.read_csv('/Users/jjodesty/Projects/DiffyQ-SimCAD/simulations/external_data/output.csv'))
def query(s, df):
return df[
(df['run'] == s['run']) & (df['substep'] == s['substep']) & (df['timestep'] == s['timestep'])
].drop(columns=['run', 'substep', "timestep"])
def p1(_g, substep, sL, s):
result_dict = query(s, df).to_dict()
del result_dict["ds3"]
return {k: list(v.values()).pop() for k, v in result_dict.items()}
def p2(_g, substep, sL, s):
result_dict = query(s, df).to_dict()
del result_dict["ds1"], result_dict["ds2"]
return {k: list(v.values()).pop() for k, v in result_dict.items()}
# ToDo: SilentDF(df) wont work
#integrate_ext_dataset
def integrate_ext_dataset(_g, step, sL, s, _input):
result_dict = query(s, df).to_dict()
return 'external_data', {k: list(v.values()).pop() for k, v in result_dict.items()}
def increment(y, incr_by):
return lambda _g, step, sL, s, _input: (y, s[y] + incr_by)
increment = increment('increment', 1)
def view_policies(_g, step, sL, s, _input):
return 'policies', _input
external_data = {'ds1': None, 'ds2': None, 'ds3': None}
state_dict = {
'increment': 0,
'external_data': external_data,
'policies': external_data
}
policies = {"p1": p1, "p2": p2}
states = {'increment': increment, 'external_data': integrate_ext_dataset, 'policies': view_policies}
PSUB = {'policies': policies, 'states': states}
# needs M1&2 need behaviors
partial_state_update_blocks = {
'PSUB1': PSUB,
'PSUB2': PSUB,
'PSUB3': PSUB
}
sim_config = config_sim({
"N": 2,
"T": range(4)
})
append_configs(
sim_configs=sim_config,
initial_state=state_dict,
partial_state_update_blocks=partial_state_update_blocks,
policy_ops=[lambda a, b: {**a, **b}]
)

View File

@ -1,91 +0,0 @@
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import config_sim, access_block
policies, variables = {}, {}
exclusion_list = ['nonexsistant', 'last_x', '2nd_to_last_x', '3rd_to_last_x', '4th_to_last_x']
# Policies per Mechanism
# WARNING: DO NOT delete elements from sH
# state_history, target_field, psu_block_offset, exculsion_list
def last_update(_g, substep, sH, s):
return {"last_x": access_block(
state_history=sH,
target_field="last_x",
psu_block_offset=-1,
exculsion_list=exclusion_list
)
}
policies["last_x"] = last_update
def second2last_update(_g, substep, sH, s):
return {"2nd_to_last_x": access_block(sH, "2nd_to_last_x", -2, exclusion_list)}
policies["2nd_to_last_x"] = second2last_update
# Internal States per Mechanism
# WARNING: DO NOT delete elements from sH
def add(y, x):
return lambda _g, substep, sH, s, _input: (y, s[y] + x)
variables['x'] = add('x', 1)
# last_partial_state_update_block
def nonexsistant(_g, substep, sH, s, _input):
return 'nonexsistant', access_block(sH, "nonexsistant", 0, exclusion_list)
variables['nonexsistant'] = nonexsistant
# last_partial_state_update_block
def last_x(_g, substep, sH, s, _input):
return 'last_x', _input["last_x"]
variables['last_x'] = last_x
# 2nd to last partial state update block
def second_to_last_x(_g, substep, sH, s, _input):
return '2nd_to_last_x', _input["2nd_to_last_x"]
variables['2nd_to_last_x'] = second_to_last_x
# 3rd to last partial state update block
def third_to_last_x(_g, substep, sH, s, _input):
return '3rd_to_last_x', access_block(sH, "3rd_to_last_x", -3, exclusion_list)
variables['3rd_to_last_x'] = third_to_last_x
# 4th to last partial state update block
def fourth_to_last_x(_g, substep, sH, s, _input):
return '4th_to_last_x', access_block(sH, "4th_to_last_x", -4, exclusion_list)
variables['4th_to_last_x'] = fourth_to_last_x
genesis_states = {
'x': 0,
'nonexsistant': [],
'last_x': [],
'2nd_to_last_x': [],
'3rd_to_last_x': [],
'4th_to_last_x': []
}
PSUB = {
"policies": policies,
"variables": variables
}
partial_state_update_block = {
"PSUB1": PSUB,
"PSUB2": PSUB,
"PSUB3": PSUB
}
sim_config = config_sim(
{
"N": 1,
"T": range(3),
}
)
append_configs(
sim_configs=sim_config,
initial_state=genesis_states,
partial_state_update_blocks=partial_state_update_block
)

View File

@ -1,83 +0,0 @@
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import config_sim
# Policies per Mechanism
def p1m1(_g, step, sL, s):
return {'policy1': 1}
def p2m1(_g, step, sL, s):
return {'policy2': 2}
def p1m2(_g, step, sL, s):
return {'policy1': 2, 'policy2': 2}
def p2m2(_g, step, sL, s):
return {'policy1': 2, 'policy2': 2}
def p1m3(_g, step, sL, s):
return {'policy1': 1, 'policy2': 2, 'policy3': 3}
def p2m3(_g, step, sL, s):
return {'policy1': 1, 'policy2': 2, 'policy3': 3}
# Internal States per Mechanism
def add(y, x):
return lambda _g, step, sH, s, _input: (y, s[y] + x)
def policies(_g, step, sH, s, _input):
y = 'policies'
x = _input
return (y, x)
# Genesis States
genesis_states = {
'policies': {},
's1': 0
}
variables = {
's1': add('s1', 1),
"policies": policies
}
partial_state_update_block = {
"m1": {
"policies": {
"p1": p1m1,
"p2": p2m1
},
"variables": variables
},
"m2": {
"policies": {
"p1": p1m2,
"p2": p2m2
},
"variables": variables
},
"m3": {
"policies": {
"p1": p1m3,
"p2": p2m3
},
"variables": variables
}
}
sim_config = config_sim(
{
"N": 1,
"T": range(3),
}
)
# Aggregation == Reduce Map / Reduce Map Aggregation
# using env functions (include in reg test using / for env proc)
append_configs(
sim_configs=sim_config,
initial_state=genesis_states,
partial_state_update_blocks=partial_state_update_block,
# ToDo: subsequent functions should include policy dict for access to each policy (i.e shouldnt be a map)
policy_ops=[lambda a, b: a + b, lambda y: y * 2] # Default: lambda a, b: a + b ToDO: reduction function requires high lvl explanation
)

View File

@ -1,159 +0,0 @@
import numpy as np
from datetime import timedelta
import pprint
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import env_trigger, var_substep_trigger, config_sim, time_step, psub_list
from typing import Dict, List
pp = pprint.PrettyPrinter(indent=4)
seeds = {
'z': np.random.RandomState(1),
'a': np.random.RandomState(2),
'b': np.random.RandomState(3),
'c': np.random.RandomState(3)
}
# Optional
g: Dict[str, List[int]] = {
'alpha': [1],
# 'beta': [2],
# 'gamma': [3],
'beta': [2, 5],
'gamma': [3, 4],
'omega': [7]
}
psu_steps = ['m1', 'm2', 'm3']
system_substeps = len(psu_steps)
var_timestep_trigger = var_substep_trigger([0, system_substeps])
env_timestep_trigger = env_trigger(system_substeps)
env_process = {}
psu_block = {k: {"policies": {}, "variables": {}} for k in psu_steps}
# ['s1', 's2', 's3', 's4']
# Policies per Mechanism
def p1m1(_g, step, sL, s):
return {'param1': 1}
psu_block['m1']['policies']['p1'] = p1m1
def p2m1(_g, step, sL, s):
return {'param2': 4}
psu_block['m1']['policies']['p2'] = p2m1
def p1m2(_g, step, sL, s):
return {'param1': 'a', 'param2': _g['beta']}
psu_block['m2']['policies']['p1'] = p1m2
def p2m2(_g, step, sL, s):
return {'param1': 'b', 'param2': 0}
psu_block['m2']['policies']['p2'] = p2m2
def p1m3(_g, step, sL, s):
return {'param1': np.array([10, 100])}
psu_block['m3']['policies']['p1'] = p1m3
def p2m3(_g, step, sL, s):
return {'param1': np.array([20, 200])}
psu_block['m3']['policies']['p2'] = p2m3
# Internal States per Mechanism
def s1m1(_g, step, sL, s, _input):
return 's1', 0
psu_block['m1']["variables"]['s1'] = s1m1
def s2m1(_g, step, sL, s, _input):
return 's2', _g['beta']
psu_block['m1']["variables"]['s2'] = s2m1
def s1m2(_g, step, sL, s, _input):
return 's1', _input['param2']
psu_block['m2']["variables"]['s1'] = s1m2
def s2m2(_g, step, sL, s, _input):
return 's2', _input['param2']
psu_block['m2']["variables"]['s2'] = s2m2
def s1m3(_g, step, sL, s, _input):
return 's1', 0
psu_block['m3']["variables"]['s1'] = s1m3
def s2m3(_g, step, sL, s, _input):
return 's2', 0
psu_block['m3']["variables"]['s2'] = s2m3
# Exogenous States
def update_timestamp(_g, step, sL, s, _input):
y = 'timestamp'
return y, time_step(dt_str=s[y], dt_format='%Y-%m-%d %H:%M:%S', _timedelta=timedelta(days=0, minutes=0, seconds=1))
for m in ['m1','m2','m3']:
# psu_block[m]["variables"]['timestamp'] = update_timestamp
psu_block[m]["variables"]['timestamp'] = var_timestep_trigger(y='timestamp', f=update_timestamp)
# psu_block[m]["variables"]['timestamp'] = var_trigger(
# y='timestamp', f=update_timestamp, pre_conditions={'substep': [0, system_substeps]}, cond_op=lambda a, b: a and b
# )
proc_one_coef = 0.7
def es3(_g, step, sL, s, _input):
return 's3', s['s3'] + proc_one_coef
# use `timestep_trigger` to update every ts
for m in ['m1','m2','m3']:
psu_block[m]["variables"]['s3'] = var_timestep_trigger(y='s3', f=es3)
def es4(_g, step, sL, s, _input):
return 's4', s['s4'] + _g['gamma']
for m in ['m1','m2','m3']:
psu_block[m]["variables"]['s4'] = var_timestep_trigger(y='s4', f=es4)
# ToDo: The number of values entered in sweep should be the # of config objs created,
# not dependent on the # of times the sweep is applied
# sweep exo_state func and point to exo-state in every other funtion
# param sweep on genesis states
# Genesis States
genesis_states = {
's1': 0.0,
's2': 0.0,
's3': 1.0,
's4': 1.0,
'timestamp': '2018-10-01 15:16:24'
}
# Environment Process
# ToDo: Validate - make env proc trigger field agnostic
env_process["s3"] = [lambda _g, x: _g['beta'], lambda _g, x: x + 1]
env_process["s4"] = env_timestep_trigger(trigger_field='timestep', trigger_vals=[5], funct_list=[lambda _g, x: _g['beta']])
# config_sim Necessary
sim_config = config_sim(
{
"N": 2,
"T": range(5),
"M": g, # Optional
}
)
# New Convention
partial_state_update_blocks = psub_list(psu_block, psu_steps)
append_configs(
sim_configs=sim_config,
initial_state=genesis_states,
seeds=seeds,
env_processes=env_process,
partial_state_update_blocks=partial_state_update_blocks
)
print()
print("Policie State Update Block:")
pp.pprint(partial_state_update_blocks)
print()
print()

View File

@ -1,36 +0,0 @@
import unittest
import pandas as pd
# from tabulate import tabulate
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from simulations.regression_tests import policy_aggregation
from cadCAD import configs
exec_mode = ExecutionMode()
first_config = configs # only contains config1
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
run = Executor(exec_context=single_proc_ctx, configs=first_config)
raw_result, tensor_field = run.execute()
result = pd.DataFrame(raw_result)
class TestStringMethods(unittest.TestCase):
def __init__(self, result: pd.DataFrame, tensor_field: pd.DataFrame) -> None:
self.result = result
self.tensor_field = tensor_field
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()

View File

@ -1,183 +0,0 @@
from copy import deepcopy
import pandas as pd
from fn.func import curried
from datetime import timedelta
import pprint as pp
from cadCAD.utils import SilentDF #, val_switch
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import time_step, config_sim, var_trigger, var_substep_trigger, env_trigger, psub_list
from cadCAD.configuration.utils.userDefinedObject import udoPipe, UDO
DF = SilentDF(pd.read_csv('/Users/jjodesty/Projects/DiffyQ-SimCAD/simulations/external_data/output.csv'))
class udoExample(object):
def __init__(self, x, dataset=None):
self.x = x
self.mem_id = str(hex(id(self)))
self.ds = dataset # for setting ds initially or querying
self.perception = {}
def anon(self, f):
return f(self)
def updateX(self):
self.x += 1
return self
def updateDS(self):
self.ds.iloc[0,0] -= 10
# pp.pprint(self.ds)
return self
def perceive(self, s):
self.perception = self.ds[
(self.ds['run'] == s['run']) & (self.ds['substep'] == s['substep']) & (self.ds['timestep'] == s['timestep'])
].drop(columns=['run', 'substep']).to_dict()
return self
def read(self, ds_uri):
self.ds = SilentDF(pd.read_csv(ds_uri))
return self
def write(self, ds_uri):
pd.to_csv(ds_uri)
# ToDo: Generic update function
pass
state_udo = UDO(udo=udoExample(0, DF), masked_members=['obj', 'perception'])
policy_udoA = UDO(udo=udoExample(0, DF), masked_members=['obj', 'perception'])
policy_udoB = UDO(udo=udoExample(0, DF), masked_members=['obj', 'perception'])
sim_config = config_sim({
"N": 2,
"T": range(4)
})
# ToDo: DataFrame Column order
state_dict = {
'increment': 0,
'state_udo': state_udo, 'state_udo_tracker': 0,
'state_udo_perception_tracker': {"ds1": None, "ds2": None, "ds3": None, "timestep": None},
'udo_policies': {'udo_A': policy_udoA, 'udo_B': policy_udoB},
'udo_policy_tracker': (0, 0),
'timestamp': '2019-01-01 00:00:00'
}
psu_steps = ['m1', 'm2', 'm3']
system_substeps = len(psu_steps)
var_timestep_trigger = var_substep_trigger([0, system_substeps])
env_timestep_trigger = env_trigger(system_substeps)
psu_block = {k: {"policies": {}, "variables": {}} for k in psu_steps}
def udo_policyA(_g, step, sL, s):
s['udo_policies']['udo_A'].updateX()
return {'udo_A': udoPipe(s['udo_policies']['udo_A'])}
# policies['a'] = udo_policyA
for m in psu_steps:
psu_block[m]['policies']['a'] = udo_policyA
def udo_policyB(_g, step, sL, s):
s['udo_policies']['udo_B'].updateX()
return {'udo_B': udoPipe(s['udo_policies']['udo_B'])}
# policies['b'] = udo_policyB
for m in psu_steps:
psu_block[m]['policies']['b'] = udo_policyB
# policies = {"p1": udo_policyA, "p2": udo_policyB}
# policies = {"A": udo_policyA, "B": udo_policyB}
def add(y: str, added_val):
return lambda _g, step, sL, s, _input: (y, s[y] + added_val)
# state_updates['increment'] = add('increment', 1)
for m in psu_steps:
psu_block[m]["variables"]['increment'] = add('increment', 1)
@curried
def perceive(s, self):
self.perception = self.ds[
(self.ds['run'] == s['run']) & (self.ds['substep'] == s['substep']) & (self.ds['timestep'] == s['timestep'])
].drop(columns=['run', 'substep']).to_dict()
return self
def state_udo_update(_g, step, sL, s, _input):
y = 'state_udo'
# s['hydra_state'].updateX().anon(perceive(s))
s['state_udo'].updateX().perceive(s).updateDS()
x = udoPipe(s['state_udo'])
return y, x
for m in psu_steps:
psu_block[m]["variables"]['state_udo'] = state_udo_update
def track(destination, source):
return lambda _g, step, sL, s, _input: (destination, s[source].x)
state_udo_tracker = track('state_udo_tracker', 'state_udo')
for m in psu_steps:
psu_block[m]["variables"]['state_udo_tracker'] = state_udo_tracker
def track_state_udo_perception(destination, source):
def id(past_perception):
if len(past_perception) == 0:
return state_dict['state_udo_perception_tracker']
else:
return past_perception
return lambda _g, step, sL, s, _input: (destination, id(s[source].perception))
state_udo_perception_tracker = track_state_udo_perception('state_udo_perception_tracker', 'state_udo')
for m in psu_steps:
psu_block[m]["variables"]['state_udo_perception_tracker'] = state_udo_perception_tracker
def view_udo_policy(_g, step, sL, s, _input):
return 'udo_policies', _input
for m in psu_steps:
psu_block[m]["variables"]['udo_policies'] = view_udo_policy
def track_udo_policy(destination, source):
def val_switch(v):
if isinstance(v, pd.DataFrame) is True or isinstance(v, SilentDF) is True:
return SilentDF(v)
else:
return v.x
return lambda _g, step, sL, s, _input: (destination, tuple(val_switch(v) for _, v in s[source].items()))
udo_policy_tracker = track_udo_policy('udo_policy_tracker', 'udo_policies')
for m in psu_steps:
psu_block[m]["variables"]['udo_policy_tracker'] = udo_policy_tracker
def update_timestamp(_g, step, sL, s, _input):
y = 'timestamp'
return y, time_step(dt_str=s[y], dt_format='%Y-%m-%d %H:%M:%S', _timedelta=timedelta(days=0, minutes=0, seconds=1))
for m in psu_steps:
psu_block[m]["variables"]['timestamp'] = var_timestep_trigger(y='timestamp', f=update_timestamp)
# psu_block[m]["variables"]['timestamp'] = var_trigger(
# y='timestamp', f=update_timestamp,
# pre_conditions={'substep': [0, system_substeps]}, cond_op=lambda a, b: a and b
# )
# psu_block[m]["variables"]['timestamp'] = update_timestamp
# ToDo: Bug without specifying parameters
# New Convention
partial_state_update_blocks = psub_list(psu_block, psu_steps)
append_configs(
sim_configs=sim_config,
initial_state=state_dict,
partial_state_update_blocks=partial_state_update_blocks
)
print()
print("State Updates:")
pp.pprint(partial_state_update_blocks)
print()

View File

@ -1,169 +0,0 @@
import pandas as pd
import pprint as pp
from fn.func import curried
from datetime import timedelta
from cadCAD.utils import SilentDF #, val_switch
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import time_step, config_sim
from cadCAD.configuration.utils.userDefinedObject import udoPipe, UDO
DF = SilentDF(pd.read_csv('/Users/jjodesty/Projects/DiffyQ-SimCAD/simulations/external_data/output.csv'))
class udoExample(object):
def __init__(self, x, dataset=None):
self.x = x
self.mem_id = str(hex(id(self)))
self.ds = dataset # for setting ds initially or querying
self.perception = {}
def anon(self, f):
return f(self)
def updateX(self):
self.x += 1
return self
def perceive(self, s):
self.perception = self.ds[
(self.ds['run'] == s['run']) & (self.ds['substep'] == s['substep']) & (self.ds['timestep'] == s['timestep'])
].drop(columns=['run', 'substep']).to_dict()
return self
def read(self, ds_uri):
self.ds = SilentDF(pd.read_csv(ds_uri))
return self
def write(self, ds_uri):
pd.to_csv(ds_uri)
# ToDo: Generic update function
pass
# can be accessed after an update within the same substep and timestep
state_udo = UDO(udo=udoExample(0, DF), masked_members=['obj', 'perception'])
policy_udoA = UDO(udo=udoExample(0, DF), masked_members=['obj', 'perception'])
policy_udoB = UDO(udo=udoExample(0, DF), masked_members=['obj', 'perception'])
def udo_policyA(_g, step, sL, s):
s['udo_policies']['udo_A'].updateX()
return {'udo_A': udoPipe(s['udo_policies']['udo_A'])}
def udo_policyB(_g, step, sL, s):
s['udo_policies']['udo_B'].updateX()
return {'udo_B': udoPipe(s['udo_policies']['udo_B'])}
policies = {"p1": udo_policyA, "p2": udo_policyB}
# ToDo: DataFrame Column order
state_dict = {
'increment': 0,
'state_udo': state_udo, 'state_udo_tracker_a': 0, 'state_udo_tracker_b': 0,
'state_udo_perception_tracker': {"ds1": None, "ds2": None, "ds3": None, "timestep": None},
'udo_policies': {'udo_A': policy_udoA, 'udo_B': policy_udoB},
'udo_policy_tracker_a': (0, 0), 'udo_policy_tracker_b': (0, 0),
'timestamp': '2019-01-01 00:00:00'
}
@curried
def perceive(s, self):
self.perception = self.ds[
(self.ds['run'] == s['run']) & (self.ds['substep'] == s['substep']) & (self.ds['timestep'] == s['timestep'])
].drop(columns=['run', 'substep']).to_dict()
return self
def view_udo_policy(_g, step, sL, s, _input):
return 'udo_policies', _input
def state_udo_update(_g, step, sL, s, _input):
y = 'state_udo'
# s['hydra_state'].updateX().anon(perceive(s))
s['state_udo'].updateX().perceive(s)
x = udoPipe(s['state_udo'])
return y, x
def increment(y, incr_by):
return lambda _g, step, sL, s, _input: (y, s[y] + incr_by)
def track(destination, source):
return lambda _g, step, sL, s, _input: (destination, s[source].x)
def track_udo_policy(destination, source):
def val_switch(v):
if isinstance(v, pd.DataFrame) is True or isinstance(v, SilentDF) is True:
return SilentDF(v)
else:
return v.x
return lambda _g, step, sL, s, _input: (destination, tuple(val_switch(v) for _, v in s[source].items()))
def track_state_udo_perception(destination, source):
def id(past_perception):
if len(past_perception) == 0:
return state_dict['state_udo_perception_tracker']
else:
return past_perception
return lambda _g, step, sL, s, _input: (destination, id(s[source].perception))
def time_model(y, substeps, time_delta, ts_format='%Y-%m-%d %H:%M:%S'):
def apply_incriment_condition(s):
if s['substep'] == 0 or s['substep'] == substeps:
return y, time_step(dt_str=s[y], dt_format=ts_format, _timedelta=time_delta)
else:
return y, s[y]
return lambda _g, step, sL, s, _input: apply_incriment_condition(s)
states = {
'increment': increment('increment', 1),
'state_udo_tracker_a': track('state_udo_tracker_a', 'state_udo'),
'state_udo': state_udo_update,
'state_udo_perception_tracker': track_state_udo_perception('state_udo_perception_tracker', 'state_udo'),
'state_udo_tracker_b': track('state_udo_tracker_b', 'state_udo'),
'udo_policy_tracker_a': track_udo_policy('udo_policy_tracker_a', 'udo_policies'),
'udo_policies': view_udo_policy,
'udo_policy_tracker_b': track_udo_policy('udo_policy_tracker_b', 'udo_policies')
}
substeps=3
update_timestamp = time_model(
'timestamp',
substeps=3,
time_delta=timedelta(days=0, minutes=0, seconds=1),
ts_format='%Y-%m-%d %H:%M:%S'
)
states['timestamp'] = update_timestamp
PSUB = {
'policies': policies,
'states': states
}
# needs M1&2 need behaviors
partial_state_update_blocks = [PSUB] * substeps
# pp.pprint(partial_state_update_blocks)
sim_config = config_sim({
"N": 2,
"T": range(4)
})
# ToDo: Bug without specifying parameters
append_configs(
sim_configs=sim_config,
initial_state=state_dict,
seeds={},
raw_exogenous_states={},
env_processes={},
partial_state_update_blocks=partial_state_update_blocks,
# policy_ops=[lambda a, b: {**a, **b}]
)
print()
print("State Updates:")
pp.pprint(partial_state_update_blocks)
print()

44
simulations/sim_test.py Normal file
View File

@ -0,0 +1,44 @@
import pandas as pd
from tabulate import tabulate
# The following imports NEED to be in the exact same order
from SimCAD.engine import ExecutionMode, ExecutionContext, Executor
from simulations.validation import config1, config2
# from simulations.validation import base_config1, base_config2
# from simulations.barlin import config4
# from simulations.zx import config_zx
# from simulations.barlin import config6atemp #config6aworks,
from SimCAD import configs
# ToDo: pass ExecutionContext with execution method as ExecutionContext input
exec_mode = ExecutionMode()
print("Simulation Execution 1")
print()
first_config = [configs[0]] # from config1
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
run1 = Executor(exec_context=single_proc_ctx, configs=first_config)
run1_raw_result, tensor_field = run1.main()
result = pd.DataFrame(run1_raw_result)
# result.to_csv('~/Projects/DiffyQ-SimCAD/results/config4.csv', sep=',')
print()
print("Tensor Field:")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()
#
# print("Simulation Execution 2: Pairwise Execution")
# print()
# multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc)
# run2 = Executor(exec_context=multi_proc_ctx, configs=configs)
# for raw_result, tensor_field in run2.main():
# result = pd.DataFrame(raw_result)
# print()
# print("Tensor Field:")
# print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
# print("Output:")
# print(tabulate(result, headers='keys', tablefmt='psql'))
# print()

View File

@ -1,25 +0,0 @@
import pandas as pd
from typing import List
from tabulate import tabulate
# The following imports NEED to be in the exact order
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from simulations.regression_tests import config1
from cadCAD import configs
exec_mode = ExecutionMode()
print("Simulation Execution: Single Configuration")
print()
first_config = configs # only contains config1
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
run = Executor(exec_context=single_proc_ctx, configs=first_config)
raw_result, tensor_field = run.execute()
result = pd.DataFrame(raw_result)
print()
print("Tensor Field: config1")
# print(raw_result)
print(tabulate(tensor_field[['m', 'b1', 's1', 's2']], headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()

View File

@ -1,23 +0,0 @@
import pandas as pd
from tabulate import tabulate
# The following imports NEED to be in the exact order
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from simulations.regression_tests import config2
from cadCAD import configs
exec_mode = ExecutionMode()
print("Simulation Execution: Single Configuration")
print()
first_config = configs # only contains config2
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
run = Executor(exec_context=single_proc_ctx, configs=first_config)
raw_result, tensor_field = run.execute()
result = pd.DataFrame(raw_result)
print()
print("Tensor Field: config1")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()

View File

@ -1,26 +0,0 @@
import pandas as pd
from tabulate import tabulate
# The following imports NEED to be in the exact order
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from simulations.regression_tests import external_dataset
from cadCAD import configs
exec_mode = ExecutionMode()
print("Simulation Execution: Single Configuration")
print()
first_config = configs # only contains config1
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
run = Executor(exec_context=single_proc_ctx, configs=first_config)
raw_result, tensor_field = run.execute()
result = pd.DataFrame(raw_result)
result = pd.concat([result, result['external_data'].apply(pd.Series)], axis=1)[
['run', 'substep', 'timestep', 'increment', 'external_data', 'policies', 'ds1', 'ds2', 'ds3', ]
]
print()
print("Tensor Field: config1")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()

View File

@ -1,27 +0,0 @@
import pandas as pd
from tabulate import tabulate
# The following imports NEED to be in the exact order
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from simulations.regression_tests import historical_state_access
from cadCAD import configs
exec_mode = ExecutionMode()
print("Simulation Execution: Single Configuration")
print()
first_config = configs # only contains config1
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
run = Executor(exec_context=single_proc_ctx, configs=first_config)
raw_result, tensor_field = run.execute()
result = pd.DataFrame(raw_result)
# cols = ['run','substep','timestep','x','nonexsistant','last_x','2nd_to_last_x','3rd_to_last_x','4th_to_last_x']
cols = ['last_x']
result = result[cols]
print()
print("Tensor Field: config1")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()

View File

@ -1,25 +0,0 @@
import pandas as pd
from tabulate import tabulate
# The following imports NEED to be in the exact order
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from simulations.regression_tests import config1, config2
from cadCAD import configs
exec_mode = ExecutionMode()
print("Simulation Execution: Concurrent Execution")
multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc)
run = Executor(exec_context=multi_proc_ctx, configs=configs)
# print(configs)
i = 0
config_names = ['config1', 'config2']
for raw_result, tensor_field in run.execute():
result = pd.DataFrame(raw_result)
print()
print(f"Tensor Field: {config_names[i]}")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()
i += 1

View File

@ -1,26 +0,0 @@
import pandas as pd
from tabulate import tabulate
# The following imports NEED to be in the exact order
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from simulations.regression_tests import sweep_config
from cadCAD import configs
# pprint(configs)
exec_mode = ExecutionMode()
print("Simulation Execution: Concurrent Execution")
multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc)
run = Executor(exec_context=multi_proc_ctx, configs=configs)
i = 0
config_names = ['sweep_config_A', 'sweep_config_B']
for raw_result, tensor_field in run.execute():
result = pd.DataFrame(raw_result)
print()
print("Tensor Field: " + config_names[i])
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()
i += 1

View File

@ -1,25 +0,0 @@
from pprint import pprint
import pandas as pd
from tabulate import tabulate
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from simulations.regression_tests import policy_aggregation
from cadCAD import configs
exec_mode = ExecutionMode()
print("Simulation Execution: Single Configuration")
print()
first_config = configs # only contains config1
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
run = Executor(exec_context=single_proc_ctx, configs=first_config)
raw_result, tensor_field = run.execute()
result = pd.DataFrame(raw_result)
print()
print("Tensor Field: config1")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()

View File

@ -1,48 +0,0 @@
import pandas as pd
from tabulate import tabulate
# The following imports NEED to be in the exact order
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from simulations.regression_tests import udo
from cadCAD import configs
exec_mode = ExecutionMode()
print("Simulation Execution: Single Configuration")
print()
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
run = Executor(exec_context=single_proc_ctx, configs=configs)
# cols = configs[0].initial_state.keys()
cols = [
'increment',
'state_udo_tracker', 'state_udo', 'state_udo_perception_tracker',
'udo_policies', 'udo_policy_tracker',
'timestamp'
]
raw_result, tensor_field = run.execute()
result = pd.DataFrame(raw_result)[['run', 'substep', 'timestep'] + cols]
# result = pd.concat([result.drop(['c'], axis=1), result['c'].apply(pd.Series)], axis=1)
# print(list(result['c']))
# print(tabulate(result['c'].apply(pd.Series), headers='keys', tablefmt='psql'))
# print(result.iloc[8,:]['state_udo'].ds)
# ctypes.cast(id(v['state_udo']['mem_id']), ctypes.py_object).value
print()
print("Tensor Field: config1")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()
print(result.info(verbose=True))
# def f(df, col):
# for k in df[col].iloc[0].keys():
# df[k] = None
# for index, row in df.iterrows():
# # df.apply(lambda row:, axis=1)

View File

@ -1,44 +0,0 @@
import pandas as pd
from tabulate import tabulate
# The following imports NEED to be in the exact order
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from simulations.regression_tests import udo_inter_substep_update
from cadCAD import configs
exec_mode = ExecutionMode()
print("Simulation Execution: Single Configuration")
print()
first_config = configs # only contains config1
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
run = Executor(exec_context=single_proc_ctx, configs=first_config)
# cols = configs[0].initial_state.keys()
cols = [
'increment',
'state_udo_tracker_a', 'state_udo', 'state_udo_perception_tracker', 'state_udo_tracker_b',
'udo_policy_tracker_a', 'udo_policies', 'udo_policy_tracker_b',
'timestamp'
]
raw_result, tensor_field = run.execute()
result = pd.DataFrame(raw_result)[['run', 'substep', 'timestep'] + cols]
# result = pd.concat([result.drop(['c'], axis=1), result['c'].apply(pd.Series)], axis=1)
# print(list(result['c']))
# print(tabulate(result['c'].apply(pd.Series), headers='keys', tablefmt='psql'))
print()
print("Tensor Field: config1")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print()
print(result.info(verbose=True))
# def f(df, col):
# for k in df[col].iloc[0].keys():
# df[k] = None
# for index, row in df.iterrows():
# # df.apply(lambda row:, axis=1)

View File

@ -1,166 +0,0 @@
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from cadCAD.configuration import Configuration
from cadCAD.configuration.utils.userDefinedObject import udoPipe, UDO
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pprint as pp
T = 50 #iterations in our simulation
n = 3 #number of boxes in our network
m = 2 #for barabasi graph type number of edges is (n-2)*m
G = nx.barabasi_albert_graph(n, m)
k = len(G.edges)
# class udoExample(object):
# def __init__(self, G):
# self.G = G
# self.mem_id = str(hex(id(self)))
g = UDO(udo=G)
print()
# print(g.edges)
# print(G.edges)
# pp.pprint(f"{type(g)}: {g}")
# next
balls = np.zeros(n,)
for node in g.nodes:
rv = np.random.randint(1,25)
g.nodes[node]['initial_balls'] = rv
balls[node] = rv
# pp.pprint(balls)
# next
scale=100
nx.draw_kamada_kawai(G, node_size=balls*scale,labels=nx.get_node_attributes(G,'initial_balls'))
# next
initial_conditions = {'balls':balls, 'network':G}
print(initial_conditions)
# next
def update_balls(params, step, sL, s, _input):
delta_balls = _input['delta']
new_balls = s['balls']
for e in G.edges:
move_ball = delta_balls[e]
src = e[0]
dst = e[1]
if (new_balls[src] >= move_ball) and (new_balls[dst] >= -move_ball):
new_balls[src] = new_balls[src] - move_ball
new_balls[dst] = new_balls[dst] + move_ball
key = 'balls'
value = new_balls
return (key, value)
def update_network(params, step, sL, s, _input):
new_nodes = _input['nodes']
new_edges = _input['edges']
new_balls = _input['quantity']
graph = s['network']
for node in new_nodes:
graph.add_node(node)
graph.nodes[node]['initial_balls'] = new_balls[node]
graph.nodes[node]['strat'] = _input['node_strats'][node]
for edge in new_edges:
graph.add_edge(edge[0], edge[1])
graph.edges[edge]['strat'] = _input['edge_strats'][edge]
key = 'network'
value = graph
return (key, value)
def update_network_balls(params, step, sL, s, _input):
new_nodes = _input['nodes']
new_balls = _input['quantity']
balls = np.zeros(len(s['balls']) + len(new_nodes))
for node in s['network'].nodes:
balls[node] = s['balls'][node]
for node in new_nodes:
balls[node] = new_balls[node]
key = 'balls'
value = balls
return (key, value)
# next
def greedy_robot(src_balls, dst_balls):
# robot wishes to accumlate balls at its source
# takes half of its neighbors balls
if src_balls < dst_balls:
delta = -np.floor(dst_balls / 2)
else:
delta = 0
return delta
def fair_robot(src_balls, dst_balls):
# robot follows the simple balancing rule
delta = np.sign(src_balls - dst_balls)
return delta
def giving_robot(src_balls, dst_balls):
# robot wishes to gice away balls one at a time
if src_balls > 0:
delta = 1
else:
delta = 0
return delta
# next
robot_strategies = [greedy_robot,fair_robot, giving_robot]
for node in G.nodes:
nstrats = len(robot_strategies)
rv = np.random.randint(0,nstrats)
G.nodes[node]['strat'] = robot_strategies[rv]
for e in G.edges:
owner_node = e[0]
G.edges[e]['strat'] = G.nodes[owner_node]['strat']
# next
def robotic_network(params, step, sL, s):
graph = s['network']
delta_balls = {}
for e in graph.edges:
src = e[0]
src_balls = s['balls'][src]
dst = e[1]
dst_balls = s['balls'][dst]
# transfer balls according to specific robot strat
strat = graph.edges[e]['strat']
delta_balls[e] = strat(src_balls, dst_balls)
return_dict = {'nodes': [], 'edges': {}, 'quantity': {}, 'node_strats': {}, 'edge_strats': {}, 'delta': delta_balls}
return (return_dict)

View File

@ -1,221 +0,0 @@
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils.userDefinedObject import udoPipe, UDO
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
T = 50 #iterations in our simulation
n = 3 #number of boxes in our network
m = 2 #for barabasi graph type number of edges is (n-2)*m
G = nx.barabasi_albert_graph(n, m)
k = len(G.edges)
balls = np.zeros(n,)
for node in G.nodes:
rv = np.random.randint(1,25)
G.nodes[node]['initial_balls'] = rv
balls[node] = rv
scale=100
nx.draw_kamada_kawai(G, node_size=balls*scale,labels=nx.get_node_attributes(G,'initial_balls'))
def greedy_robot(src_balls, dst_balls):
# robot wishes to accumlate balls at its source
# takes half of its neighbors balls
if src_balls < dst_balls:
return -np.floor(dst_balls / 2)
else:
return 0
def fair_robot(src_balls, dst_balls):
# robot follows the simple balancing rule
return np.sign(src_balls - dst_balls)
def giving_robot(src_balls, dst_balls):
# robot wishes to gice away balls one at a time
if src_balls > 0:
return 1
else:
return 0
robot_strategies = [greedy_robot,fair_robot, giving_robot]
for node in G.nodes:
nstrats = len(robot_strategies)
rv = np.random.randint(0,nstrats)
G.nodes[node]['strat'] = robot_strategies[rv]
for e in G.edges:
owner_node = e[0]
G.edges[e]['strat'] = G.nodes[owner_node]['strat']
default_policy = {'nodes': [], 'edges': {}, 'quantity': {}, 'node_strats': {}, 'edge_strats': {}, 'delta': {}}
class robot(object):
def __init__(self, graph, balls, internal_policy=default_policy):
self.mem_id = str(hex(id(self)))
self.internal_policy = internal_policy
self.graph = graph
self.balls = balls
def robotic_network(self, graph, balls): # move balls
self.graph, self.balls = graph, balls
delta_balls = {}
for e in self.graph.edges:
src = e[0]
src_balls = self.balls[src]
dst = e[1]
dst_balls = self.balls[dst]
# transfer balls according to specific robot strat
strat = self.graph.edges[e]['strat']
delta_balls[e] = strat(src_balls, dst_balls)
self.internal_policy = {'nodes': [], 'edges': {}, 'quantity': {}, 'node_strats': {}, 'edge_strats': {}, 'delta': delta_balls}
return self
def agent_arrival(self, graph, balls): # add node
self.graph, self.balls = graph, balls
node = len(self.graph.nodes)
edge_list = self.graph.edges
# choose a m random edges without replacement
# new = np.random.choose(edgelist,m)
new = [0, 1] # tester
nodes = [node]
edges = [(node, new_node) for new_node in new]
initial_balls = {node: np.random.randint(1, 25)}
rv = np.random.randint(0, nstrats)
node_strat = {node: robot_strategies[rv]}
edge_strats = {e: robot_strategies[rv] for e in edges}
self.internal_policy = {'nodes': nodes,
'edges': edges,
'quantity': initial_balls,
'node_strats': node_strat,
'edge_strats': edge_strats,
'delta': np.zeros(node + 1)
}
return self
robot_udo = UDO(udo=robot(G, balls), masked_members=['obj'])
initial_conditions = {'balls': balls, 'network': G, 'robot': robot_udo}
def update_balls(params, step, sL, s, _input):
delta_balls = _input['robot'].internal_policy['delta']
new_balls = s['balls']
for e in G.edges:
move_ball = delta_balls[e]
src = e[0]
dst = e[1]
if (new_balls[src] >= move_ball) and (new_balls[dst] >= -move_ball):
new_balls[src] = new_balls[src] - move_ball
new_balls[dst] = new_balls[dst] + move_ball
key = 'balls'
value = new_balls
return (key, value)
def update_network(params, step, sL, s, _input):
new_nodes = _input['robot'].internal_policy['nodes']
new_edges = _input['robot'].internal_policy['edges']
new_balls = _input['robot'].internal_policy['quantity']
graph = s['network']
for node in new_nodes:
graph.add_node(node)
graph.nodes[node]['initial_balls'] = new_balls[node]
graph.nodes[node]['strat'] = _input['robot'].internal_policy['node_strats'][node]
for edge in new_edges:
graph.add_edge(edge[0], edge[1])
graph.edges[edge]['strat'] = _input['robot'].internal_policy['edge_strats'][edge]
key = 'network'
value = graph
return (key, value)
def update_network_balls(params, step, sL, s, _input):
new_nodes = _input['robot'].internal_policy['nodes']
new_balls = _input['robot'].internal_policy['quantity']
balls = np.zeros(len(s['balls']) + len(new_nodes))
for node in s['network'].nodes:
balls[node] = s['balls'][node]
for node in new_nodes:
balls[node] = new_balls[node]
key = 'balls'
value = balls
return (key, value)
def robotic_network(params, step, sL, s):
s['robot'].robotic_network(s['network'], s['balls'])
return {'robot': udoPipe(s['robot'])}
def agent_arrival(params, step, sL, s):
s['robot'].agent_arrival(s['network'], s['balls'])
return {'robot': udoPipe(s['robot'])}
def get_robot(params, step, sL, s, _input):
return 'robot', _input['robot']
partial_state_update_blocks = [
{
'policies': {
# The following policy functions will be evaluated and their returns will be passed to the state update functions
'p1': robotic_network
},
'variables': { # The following state variables will be updated simultaneously
'balls': update_balls,
'robot': get_robot
}
},
{
'policies': {
# The following policy functions will be evaluated and their returns will be passed to the state update functions
'p1': agent_arrival
},
'variables': { # The following state variables will be updated simultaneously
'network': update_network,
'balls': update_network_balls,
'robot': get_robot
}
}
]
simulation_parameters = {
'T': range(T),
'N': 1,
'M': {}
}
append_configs(
sim_configs=simulation_parameters, #dict containing state update functions
initial_state=initial_conditions, #dict containing variable names and initial values
partial_state_update_blocks= partial_state_update_blocks #, #dict containing state update functions
# policy_ops=[lambda a, b: {**a, **b}]
)
# config = Configuration(initial_state=initial_conditions, #dict containing variable names and initial values
# partial_state_update_blocks=partial_state_update_blocks, #dict containing state update functions
# sim_config=simulation_parameters #dict containing simulation parameters
# )

View File

@ -1,25 +0,0 @@
import pandas as pd
from tabulate import tabulate
# The following imports NEED to be in the exact order
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from cadCAD import configs
exec_mode = ExecutionMode()
print("Simulation Execution: Single Configuration")
print()
first_config = configs # only contains config1
single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)
run = Executor(exec_context=single_proc_ctx, configs=first_config)
raw_result, tensor_field = run.main()
result = pd.DataFrame(raw_result)
print()
print("Tensor Field: config1")
print(tabulate(tensor_field, headers='keys', tablefmt='psql'))
print("Output:")
print(tabulate(result, headers='keys', tablefmt='psql'))
print(result[['network']])
print()
print(result[['network', 'substep']])

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,171 @@
from decimal import Decimal
import numpy as np
from datetime import timedelta
from SimCAD import configs
from SimCAD.configuration import Configuration
from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \
ep_time_step
seed = {
'z': np.random.RandomState(1),
'a': np.random.RandomState(2),
'b': np.random.RandomState(3),
'c': np.random.RandomState(3)
}
# Behaviors per Mechanism
# Different return types per mechanism ?? *** No ***
def b1m1(step, sL, s):
return {'param1': 1}
def b2m1(step, sL, s):
return {'param1': 1}
def b1m2(step, sL, s):
return {'param1': 1, 'param2': 2}
def b2m2(step, sL, s):
return {'param1': 1, 'param2': 4}
def b1m3(step, sL, s):
return {'param1': 1, 'param2': np.array([10, 100])}
def b2m3(step, sL, s):
return {'param1': 1, 'param2': np.array([20, 200])}
# deff not more than 2
# Internal States per Mechanism
def s1m1(step, sL, s, _input):
y = 's1'
x = s['s1'] + _input['param1']
return (y, x)
def s2m1(step, sL, s, _input):
y = 's2'
x = s['s2'] + _input['param1']
return (y, x)
def s1m2(step, sL, s, _input):
y = 's1'
x = s['s1'] + _input['param1']
return (y, x)
def s2m2(step, sL, s, _input):
y = 's2'
x = s['s2'] + _input['param1']
return (y, x)
def s1m3(step, sL, s, _input):
y = 's1'
x = s['s1'] + _input['param1']
return (y, x)
def s2m3(step, sL, s, _input):
y = 's2'
x = s['s2'] + _input['param1']
return (y, x)
# Exogenous States
proc_one_coef_A = 0.7
proc_one_coef_B = 1.3
def es3p1(step, sL, s, _input):
y = 's3'
x = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
def es4p2(step, sL, s, _input):
y = 's4'
x = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
ts_format = '%Y-%m-%d %H:%M:%S'
t_delta = timedelta(days=0, minutes=0, seconds=1)
def es5p2(step, sL, s, _input):
y = 'timestamp'
x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta)
return (y, x)
# Environment States
def env_a(x):
return 10
def env_b(x):
return 10
# def what_ever(x):
# return x + 1
# Genesis States
genesis_states = {
's1': Decimal(0.0),
's2': Decimal(0.0),
's3': Decimal(1.0),
's4': Decimal(1.0),
'timestamp': '2018-10-01 15:16:24'
}
# remove `exo_update_per_ts` to update every ts
exogenous_states = exo_update_per_ts(
{
"s3": es3p1,
"s4": es4p2,
"timestamp": es5p2
}
)
# make env proc trigger field agnostic
# ToDo: Bug - Can't use environments without proc_trigger. TypeError: 'int' object is not callable
# "/Users/jjodesty/Projects/DiffyQ-SimCAD/SimCAD/engine/simulation.py"
env_processes = {
# "s3": env_a,
# "s4": env_b
"s3": proc_trigger('2018-10-01 15:16:25', env_a),
"s4": proc_trigger('2018-10-01 15:16:25', env_b)
}
# need at least 1 behaviour and 1 state function for the 1st mech with behaviors
# mechanisms = {}
mechanisms = {
"m1": {
"behaviors": {
"b1": b1m1, # lambda step, sL, s: s['s1'] + 1,
"b2": b2m1
},
"states": { # exclude only. TypeError: reduce() of empty sequence with no initial value
"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,
"T": range(5)
}
configs.append(
Configuration(
sim_config=sim_config,
state_dict=genesis_states,
seed=seed,
exogenous_states=exogenous_states,
env_processes=env_processes,
mechanisms=mechanisms
)
)

View File

@ -0,0 +1,180 @@
from decimal import Decimal
import numpy as np
from datetime import timedelta
from SimCAD import configs
from SimCAD.configuration import Configuration
from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \
ep_time_step
seed = {
'z': np.random.RandomState(1),
'a': np.random.RandomState(2),
'b': np.random.RandomState(3),
'c': np.random.RandomState(3)
}
# Behaviors per Mechanism
# Different return types per mechanism ?? *** No ***
def b1m1(step, sL, s):
return {'param1': 1}
def b2m1(step, sL, s):
return {'param2': 4}
def b1m2(step, sL, s):
return {'param1': 'a', 'param2': 2}
def b2m2(step, sL, s):
return {'param1': 'b', 'param2': 4}
def b1m3(step, sL, s):
return {'param1': ['c'], 'param2': np.array([10, 100])}
def b2m3(step, sL, s):
return {'param1': ['d'], 'param2': np.array([20, 200])}
# Internal States per Mechanism
def s1m1(step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m1(step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m2(step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m2(step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m3(step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m3(step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
# Exogenous States
proc_one_coef_A = 0.7
proc_one_coef_B = 1.3
def es3p1(step, sL, s, _input):
y = 's3'
x = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
def es4p2(step, sL, s, _input):
y = 's4'
x = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
ts_format = '%Y-%m-%d %H:%M:%S'
t_delta = timedelta(days=0, minutes=0, seconds=1)
def es5p2(step, sL, s, _input):
y = 'timestamp'
x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta)
return (y, x)
# Environment States
def env_a(x):
return 10
def env_b(x):
return 10
# def what_ever(x):
# return x + 1
# Genesis States
genesis_states = {
's1': Decimal(0.0),
's2': Decimal(0.0),
's3': Decimal(1.0),
's4': Decimal(1.0),
'timestamp': '2018-10-01 15:16:24'
}
# remove `exo_update_per_ts` to update every ts
# why `exo_update_per_ts` here instead of `env_processes`
exogenous_states = exo_update_per_ts(
{
"s3": es3p1,
"s4": es4p2,
"timestamp": es5p2
}
)
# make env proc trigger field agnostic
env_processes = {
"s3": proc_trigger('2018-10-01 15:16:25', env_a),
"s4": proc_trigger('2018-10-01 15:16:25', env_b)
}
# lambdas
# genesis Sites should always be there
# [1, 2]
# behavior_ops = [ foldr(_ + _), lambda x: x + 0 ]
# [1, 2] = {'b1': ['a'], 'b2', [1]} =
# behavior_ops = [behavior_to_dict, print_fwd, sum_dict_values]
# behavior_ops = [foldr(dict_elemwise_sum())]
# behavior_ops = []
# need at least 1 behaviour and 1 state function for the 1st mech with behaviors
# mechanisms = {}
mechanisms = {
"m1": {
"behaviors": {
"b1": b1m1, # lambda step, sL, s: s['s1'] + 1,
# "b2": b2m1
},
"states": { # exclude only. TypeError: reduce() of empty sequence with no initial value
"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,
"T": range(5)
}
configs.append(
Configuration(
sim_config=sim_config,
state_dict=genesis_states,
seed=seed,
exogenous_states=exogenous_states,
env_processes=env_processes,
mechanisms=mechanisms
)
)

View File

@ -0,0 +1,210 @@
from decimal import Decimal
import numpy as np
from datetime import timedelta
from fn.func import curried
from SimCAD import configs
from SimCAD.configuration import Configuration
from SimCAD.configuration.utils import state_update, exo_update_per_ts, proc_trigger, bound_norm_random, \
ep_time_step
from SimCAD.engine.utils import sweep
seed = {
'z': np.random.RandomState(1),
'a': np.random.RandomState(2),
'b': np.random.RandomState(3),
'c': np.random.RandomState(3)
}
# Behaviors per Mechanism
# Different return types per mechanism ?? *** No ***
def b1m1(step, sL, s):
return {'param1': 1}
def b2m1(step, sL, s):
return {'param2': 4}
def b1m2(step, sL, s):
return {'param1': 'a', 'param2': 2}
def b2m2(step, sL, s):
return {'param1': 'b', 'param2': 4}
def b1m3(step, sL, s):
return {'param1': ['c'], 'param2': np.array([10, 100])}
def b2m3(step, sL, s):
return {'param1': ['d'], 'param2': np.array([20, 200])}
# deff not more than 2
# Internal States per Mechanism
def s1m1(step, sL, s, _input):
y = 's1'
x = _input['param1'] #+ [Coef1 x 5]
return (y, x)
# curry to give sweep_f s2m1 and returning a s2m1 sweep_f(s2m1)(param)
# decorator
param = Decimal(11.0)
def s2m1(step, sL, s, _input):
y = 's2'
x = _input['param2'] + param
return (y, x)
s2m1_params =[Decimal(11.0), Decimal(22.0)]
@curried
def s2m1(param, step, sL, s, _input):
y = 's2'
x = _input['param2'] + param
return (y, x)
# s2m1_sweep = s2m1(param=s2m1_params)
#
# s2m1_sweep = sweep(
# params = s2m1_params,
# sweep_f = s2m1_sweep
# )
def s1m2(step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m2(step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m3(step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m3(step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
# Exogenous States
proc_one_coef_A = 0.7
proc_one_coef_B = 1.3
# def es3p1(step, sL, s, _input):
# y = 's3'
# x = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B)
# return (y, x)
es3p1 = sweep(
params = [Decimal(11.0), Decimal(22.0)],
sweep_f = lambda param: lambda step, sL, s, _input: (
's3',
s['s3'] + param
)
)
def es4p2(step, sL, s, _input):
y = 's4'
x = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
ts_format = '%Y-%m-%d %H:%M:%S'
t_delta = timedelta(days=0, minutes=0, seconds=1)
def es5p2(step, sL, s, _input):
y = 'timestamp'
x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta)
return (y, x)
# Environment States
def env_a(x):
return 5
def env_b(x):
return 10
# def what_ever(x):
# return x + 1
# Genesis States
genesis_states = {
's1': Decimal(0.0),
's2': Decimal(0.0),
's3': Decimal(1.0),
's4': Decimal(1.0),
'timestamp': '2018-10-01 15:16:24'
}
# remove `exo_update_per_ts` to update every ts
exogenous_states = exo_update_per_ts(
{
"s3": es3p1,
"s4": es4p2,
"timestamp": es5p2
}
)
# ToDo: make env proc trigger field agnostic
# ToDo: input json into function renaming __name__
env_processes = {
# "s3": env_a,
"s4": proc_trigger('2018-10-01 15:16:25', env_b)
}
# lambdas
# genesis Sites should always be there
# [1, 2]
# behavior_ops = [ foldr(_ + _), lambda x: x + 0 ]
# [1, 2] = {'b1': ['a'], 'b2', [1]} =
# behavior_ops = [ behavior_to_dict, print_fwd, sum_dict_values ]
# behavior_ops = [foldr(dict_elemwise_sum())]
# behavior_ops = [foldr(lambda a, b: a + b)]
# need at least 1 behaviour and 1 state function for the 1st mech with behaviors
# mechanisms = {}
mechanisms = {
"m1": {
"behaviors": {
"b1": b1m1, # lambda step, sL, s: s['s1'] + 1,
"b2": b2m1
},
"states": { # exclude only. TypeError: reduce() of empty sequence with no initial value
"s1": s1m1,
"s2": sweep(s2m1_params, 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,
"T": range(5)
}
configs.append(
Configuration(
sim_config=sim_config,
state_dict=genesis_states,
seed=seed,
exogenous_states=exogenous_states,
env_processes=env_processes,
mechanisms=mechanisms
)
)

View File

@ -0,0 +1,180 @@
from decimal import Decimal
import numpy as np
from datetime import timedelta
from SimCAD import configs
from SimCAD.configuration import Configuration
from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \
ep_time_step
seed = {
'z': np.random.RandomState(1),
'a': np.random.RandomState(2),
'b': np.random.RandomState(3),
'c': np.random.RandomState(3)
}
# Behaviors per Mechanism
# Different return types per mechanism ?? *** No ***
def b1m1(step, sL, s):
return {'param1': 1}
def b2m1(step, sL, s):
return {'param2': 4}
def b1m2(step, sL, s):
return {'param1': 'a', 'param2': 2}
def b2m2(step, sL, s):
return {'param1': 'b', 'param2': 4}
def b1m3(step, sL, s):
return {'param1': ['c'], 'param2': np.array([10, 100])}
def b2m3(step, sL, s):
return {'param1': ['d'], 'param2': np.array([20, 200])}
# Internal States per Mechanism
def s1m1(step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m1(step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m2(step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m2(step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m3(step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m3(step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
# Exogenous States
proc_one_coef_A = 0.7
proc_one_coef_B = 1.3
def es3p1(step, sL, s, _input):
y = 's3'
x = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
def es4p2(step, sL, s, _input):
y = 's4'
x = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
ts_format = '%Y-%m-%d %H:%M:%S'
t_delta = timedelta(days=0, minutes=0, seconds=1)
def es5p2(step, sL, s, _input):
y = 'timestamp'
x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta)
return (y, x)
# Environment States
def env_a(x):
return 10
def env_b(x):
return 10
# def what_ever(x):
# return x + 1
# Genesis States
genesis_states = {
's1': Decimal(0.0),
's2': Decimal(0.0),
's3': Decimal(1.0),
's4': Decimal(1.0),
'timestamp': '2018-10-01 15:16:24'
}
# remove `exo_update_per_ts` to update every ts
# why `exo_update_per_ts` here instead of `env_processes`
exogenous_states = exo_update_per_ts(
{
"s3": es3p1,
"s4": es4p2,
"timestamp": es5p2
}
)
# make env proc trigger field agnostic
env_processes = {
"s3": proc_trigger('2018-10-01 15:16:25', env_a),
"s4": proc_trigger('2018-10-01 15:16:25', env_b)
}
# lambdas
# genesis Sites should always be there
# [1, 2]
# behavior_ops = [ foldr(_ + _), lambda x: x + 0 ]
# [1, 2] = {'b1': ['a'], 'b2', [1]} =
# behavior_ops = [behavior_to_dict, print_fwd, sum_dict_values]
# behavior_ops = [foldr(dict_elemwise_sum())]
# behavior_ops = []
# need at least 1 behaviour and 1 state function for the 1st mech with behaviors
# mechanisms = {}
mechanisms = {
"m1": {
"behaviors": {
"b1": b1m1, # lambda step, sL, s: s['s1'] + 1,
# "b2": b2m1
},
"states": { # exclude only. TypeError: reduce() of empty sequence with no initial value
"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,
"T": range(5)
}
configs.append(
Configuration(
sim_config=sim_config,
state_dict=genesis_states,
seed=seed,
exogenous_states=exogenous_states,
env_processes=env_processes,
mechanisms=mechanisms
)
)

View File

@ -1,139 +0,0 @@
from decimal import Decimal
import numpy as np
from datetime import timedelta
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import env_proc_trigger, bound_norm_random, ep_time_step, config_sim
seeds = {
'z': np.random.RandomState(1),
'a': np.random.RandomState(2),
'b': np.random.RandomState(3),
'c': np.random.RandomState(3)
}
# Policies per Mechanism
def p1m1(_g, step, sL, s):
return {'param1': 1}
def p2m1(_g, step, sL, s):
return {'param2': 4}
def p1m2(_g, step, sL, s):
return {'param1': 'a', 'param2': 2}
def p2m2(_g, step, sL, s):
return {'param1': 'b', 'param2': 4}
def p1m3(_g, step, sL, s):
return {'param1': ['c'], 'param2': np.array([10, 100])}
def p2m3(_g, step, sL, s):
return {'param1': ['d'], 'param2': np.array([20, 200])}
# Internal States per Mechanism
def s1m1(_g, step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m1(_g, step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m2(_g, step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m2(_g, step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m3(_g, step, sL, s, _input):
y = 's1'
x = _input['param1']
return (y, x)
def s2m3(_g, step, sL, s, _input):
y = 's2'
x = _input['param2']
return (y, x)
def s1m4(_g, step, sL, s, _input):
y = 's1'
x = [1]
return (y, x)
# Exogenous States
proc_one_coef_A = 0.7
proc_one_coef_B = 1.3
def es3p1(_g, step, sL, s, _input):
y = 's3'
x = s['s3'] * bound_norm_random(seeds['a'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
def es4p2(_g, step, sL, s, _input):
y = 's4'
x = s['s4'] * bound_norm_random(seeds['b'], proc_one_coef_A, proc_one_coef_B)
return (y, x)
ts_format = '%Y-%m-%d %H:%M:%S'
t_delta = timedelta(days=0, minutes=0, seconds=1)
def es5p2(_g, step, sL, s, _input):
y = 'timestamp'
x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta)
return (y, x)
# Environment States
def env_a(x):
return 5
def env_b(x):
return 10
# Genesis States
genesis_states = {
's1': Decimal(0.0),
's2': Decimal(0.0),
's3': Decimal(1.0),
's4': Decimal(1.0),
'timestamp': '2018-10-01 15:16:24'
}
raw_exogenous_states = {
"s3": es3p1,
"s4": es4p2,
"timestamp": es5p2
}
env_processes = {
"s3": env_a,
"s4": env_proc_trigger('2018-10-01 15:16:25', env_b)
}
partial_state_update_block = [
]
sim_config = config_sim(
{
"N": 2,
"T": range(5),
}
)
append_configs(
sim_configs=sim_config,
initial_state=genesis_states,
seeds={},
raw_exogenous_states={},
env_processes={},
partial_state_update_blocks=partial_state_update_block
)

File diff suppressed because one or more lines are too long

Some files were not shown because too many files have changed in this diff Show More