From 7fb764056f39a5280edb66aadcd030858a1d7fd3 Mon Sep 17 00:00:00 2001 From: "Joshua E. Jodesty" Date: Wed, 20 Feb 2019 11:51:55 -0500 Subject: [PATCH] renameing hell pt. 4 --- README.md | 79 ++++++++++++++++++++++++-------- Simulation.md | 20 ++++---- requirements.txt | 2 +- setup.py | 2 +- simulations/example_run.py | 34 -------------- simulations/multi_config run.py | 24 ++++++++++ simulations/param_sweep_run.py | 24 ++++++++++ simulations/single_config_run.py | 22 +++++++++ 8 files changed, 141 insertions(+), 66 deletions(-) delete mode 100644 simulations/example_run.py create mode 100644 simulations/multi_config run.py create mode 100644 simulations/param_sweep_run.py create mode 100644 simulations/single_config_run.py diff --git a/README.md b/README.md index fc9ae66..1906130 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -# SimCad +# cadCAD **Warning**: **Do not** publish this package / software to **any** software repository **except** one permitted by BlockScience. **Description:** -SimCAD is a differential games based simulation software package for research, validation, and Computer \ +cadCAD is a differential games based simulation software package for research, validation, and Computer \ Aided Design of economic systems. 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 \ @@ -15,11 +15,9 @@ Simulations may be run with a range of initial conditions and parameters for sta 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. -SimCAD is written in Python 3. - **1. Install Dependencies:** ```bash -pip3 install -r requirements.txt +pip install -r requirements.txt python3 setup.py sdist bdist_wheel pip3 install dist/*.whl ``` @@ -32,51 +30,92 @@ Intructions: Examples: `/simulations/validation/*` -**3. Import SimCAD & Run Simulation:** +**3. Import cadCAD & Run Simulations:** -Examples: `/simulations/example_run.py` or `/simulations/example_run.ipynb` +Examples: `/simulations/*.py` or `/simulations/*.ipynb` -`/simulations/example_run.py`: +Single Simulation Run: `/simulations/single_config_run.py` ```python -import pandas as pd from tabulate import tabulate - # The following imports NEED to be in the exact order -from SimCAD.engine import ExecutionMode, ExecutionContext, Executor -from validation import config1, config2 -from SimCAD import configs +from cadCAD.engine import ExecutionMode, ExecutionContext, Executor +from simulations.validation import config1 +from cadCAD import configs exec_mode = ExecutionMode() -print("Simulation Execution 1") +print("Simulation Execution: Single Configuration") print() -first_config = [configs[0]] # from config1 +first_config = configs # only contains 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) print() -print("Tensor Field:") +print("Tensor Field: config1") 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() +Parameter Sweep Simulation Run (Concurrent): `/simulations/param_sweep_run.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 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) run2 = Executor(exec_context=multi_proc_ctx, configs=configs) + +i = 0 +config_names = ['sweep_config_A', 'sweep_config_B'] for raw_result, tensor_field in run2.main(): result = pd.DataFrame(raw_result) print() - print("Tensor Field:") + 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 +``` + +Multiple Simulation Runs (Concurrent): `/simulations/multi_config run.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 simulations.validation import config1, config2 +from cadCAD import configs + +exec_mode = ExecutionMode() + +print("Simulation Execution: Concurrent Execution") +multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc) +run2 = Executor(exec_context=multi_proc_ctx, configs=configs) + +i = 0 +config_names = ['config1', 'config2'] +for raw_result, tensor_field in run2.main(): + 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 ``` The above can be run in Jupyter. ```bash jupyter notebook -``` +``` \ No newline at end of file diff --git a/Simulation.md b/Simulation.md index a6e1347..8c1d3cf 100644 --- a/Simulation.md +++ b/Simulation.md @@ -1,10 +1,10 @@ -# SimCAD Documentation +# cadCAD Documentation ## Introduction A blockchain is a distributed ledger with economic agents transacting in a network. The state of the network evolves with every new transaction, which can be a result of user behaviors, protocol-defined system mechanisms, or external processes. -It is not uncommon today for blockchain projects to announce a set of rules for their network and make claims about their system level behavior. However, the validity of those claims is hardly validated. Furthermore, it is difficult to know the potential system-level impact when the network is considering an upgrade to their system rules and parameters. +It is not uncommon today for blockchain projects to announce a set of rules for their network and make claims about their system level behvaior. However, the validity of those claims is hardly validated. Furthermore, it is difficult to know the potential system-level impact when the network is considering an upgrade to their system rules and prameters. To rigorously and reliably analyze, design, and improve cryptoeconomic networks, we are introducing this Computer Aided Design Engine where we define a cryptoeconomic network with its state and exogneous variables, model transactions as a result of agent behaviors, state mechanisms, and environmental processes. We can then run simulations with different initial states, mechanisms, environmental processes to understand and visualize network behavior under different conditions. @@ -31,13 +31,13 @@ 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, \ +from cadCAD import configs +from cadCAD.configuration import Configuration +from cadCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \ ep_time_step ``` -State variables and their initial values can be defined as follows. Note that `timestamp` is a required field for this iteration of SimCAD for `env_proc` to work. Future iterations will strive to make this more generic and timestamp optional. +State variables and their initial values can be defined as follows. Note that `timestamp` is a required field for this iteration of cadCAD for `env_proc` to work. Future iterations will strive to make this more generic and timestamp optional. ```python genesis_dict = { 's1': Decimal(0.0), @@ -63,7 +63,7 @@ transitions = { "m2": {...} } ``` -Every behavior per transition should return a dictionary as actions taken by the agents. They will then be aggregated through addition in this version of SimCAD. Some examples of behaviors per transition are as follows. More flexible and user-defined aggregation functions will be introduced in future iterations but no example is provided at this point. +Every behavior per transition should return a dictionary as actions taken by the agents. They will then be aggregated through addition in this version of cadCAD. Some examples of behaviors per transition are as follows. More flexible and user-defined aggregation functions will be introduced in future iterations but no example is provided at this point. ```python def b1m1(step, sL, s): return {'param1': 1} @@ -105,7 +105,7 @@ seed = { 'c': np.random.RandomState(3) } ``` -SimCAD currently supports generating random number from a normal distribution through `bound_norm_random` with `min` and `max` values specified. Examples of environmental processes with randomness are as follows. We also define timestamp format with `ts_format` and timestamp changes with `t_delta`. Users can define other distributions to update exogenous variables. +cadCAD currently supports generating random number from a normal distribution through `bound_norm_random` with `min` and `max` values specified. Examples of environmental processes with randomness are as follows. We also define timestamp format with `ts_format` and timestamp changes with `t_delta`. Users can define other distributions to update exogenous variables. ```python proc_one_coef_A = 0.7 proc_one_coef_B = 1.3 @@ -127,7 +127,7 @@ def es5p2(step, sL, s, _input): x = ep_time_step(s, s['timestamp'], fromat_str=ts_format, _timedelta=t_delta) return (y, x) ``` -User can also define specific external events such as market shocks at specific timestamps through `env_processes` with `proc_trigger`. An environmental process with no `proc_trigger` will be called at every timestamp. In the example below, it will return the value of `s3` at every timestamp. Logical event triggers, such as a big draw down in exogenous variables, will be supported in a later version of SimCAD. +User can also define specific external events such as market shocks at specific timestamps through `env_processes` with `proc_trigger`. An environmental process with no `proc_trigger` will be called at every timestamp. In the example below, it will return the value of `s3` at every timestamp. Logical event triggers, such as a big draw down in exogenous variables, will be supported in a later version of cadCAD. ```python def env_a(x): return x @@ -148,4 +148,4 @@ sim_config = { } configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms)) -``` +``` \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 0418284..70c7dfe 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ -wheel pandas +wheel pathos fn tabulate \ No newline at end of file diff --git a/setup.py b/setup.py index deef949..a01f39c 100644 --- a/setup.py +++ b/setup.py @@ -20,4 +20,4 @@ setup(name='cadCAD', author_email='joshua@block.science', # license='LICENSE', packages=find_packages() -) +) \ No newline at end of file diff --git a/simulations/example_run.py b/simulations/example_run.py deleted file mode 100644 index 634ad4e..0000000 --- a/simulations/example_run.py +++ /dev/null @@ -1,34 +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 #, config1, config2 -from cadCAD import configs - -exec_mode = ExecutionMode() - -# print("Simulation Execution 1") -# print() -# first_config = [configs[0]] # FOR non-sweep configs ONLY -# 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) -# 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: Concurrent Execution") -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() diff --git a/simulations/multi_config run.py b/simulations/multi_config run.py new file mode 100644 index 0000000..c81e1f0 --- /dev/null +++ b/simulations/multi_config run.py @@ -0,0 +1,24 @@ +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, config2 +from cadCAD import configs + +exec_mode = ExecutionMode() + +print("Simulation Execution: Concurrent Execution") +multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc) +run2 = Executor(exec_context=multi_proc_ctx, configs=configs) + +i = 0 +config_names = ['config1', 'config2'] +for raw_result, tensor_field in run2.main(): + 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 \ No newline at end of file diff --git a/simulations/param_sweep_run.py b/simulations/param_sweep_run.py new file mode 100644 index 0000000..ecf620f --- /dev/null +++ b/simulations/param_sweep_run.py @@ -0,0 +1,24 @@ +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) +run2 = Executor(exec_context=multi_proc_ctx, configs=configs) + +i = 0 +config_names = ['sweep_config_A', 'sweep_config_B'] +for raw_result, tensor_field in run2.main(): + 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 \ No newline at end of file diff --git a/simulations/single_config_run.py b/simulations/single_config_run.py new file mode 100644 index 0000000..38b1307 --- /dev/null +++ b/simulations/single_config_run.py @@ -0,0 +1,22 @@ +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 +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) +run1 = Executor(exec_context=single_proc_ctx, configs=first_config) +run1_raw_result, tensor_field = run1.main() +result = pd.DataFrame(run1_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() \ No newline at end of file