Merge pull request #38 from BlockScience/refactor_terminology

Refactor terminology
This commit is contained in:
Joshua E. Jodesty 2019-02-18 15:15:42 -05:00 committed by GitHub
commit 19feab55e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 500 additions and 1886 deletions

3
.gitignore vendored
View File

@ -17,4 +17,5 @@ dist/*.gz
cadCAD.egg-info
build
SimCAD.egg-info
cadCAD.egg-info
SimCAD.egg-info

View File

@ -9,7 +9,7 @@ Aided Design of economic systems. An economic system is treated as a state based
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 state rather than direct from action, creating a rich dynamic modeling framework.
treat agent utility as derived from state rather than direct from 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 \
@ -21,7 +21,7 @@ SimCAD is written in Python 3.
```bash
pip3 install -r requirements.txt
python3 setup.py sdist bdist_wheel
pip3 install dist/cadCAD-0.1-py3-none-any.whl
pip3 install dist/*.whl
```
**2. Configure Simulation:**
@ -76,7 +76,7 @@ for raw_result, tensor_field in run2.main():
print()
```
The above can be run in Jupyter.
The above can be run in Jupyter.
```bash
jupyter notebook
```

View File

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

View File

@ -3,24 +3,29 @@ from fn.op import foldr
import pandas as pd
from cadCAD import configs
from cadCAD.utils import key_filter
from cadCAD.configuration.utils.policyAggregation import dict_elemwise_sum
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_updates={}, policy_ops=[foldr(dict_elemwise_sum())], **kwargs):
exogenous_states={}, partial_state_update_blocks={}, policy_ops=[foldr(dict_elemwise_sum())], **kwargs):
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_updates
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_updates, _exo_update_per_ts=True):
def append_configs(sim_configs={}, initial_state={}, seeds={}, raw_exogenous_states={}, env_processes={}, partial_state_update_blocks={}, _exo_update_per_ts=True):
if _exo_update_per_ts is True:
exogenous_states = exo_update_per_ts(raw_exogenous_states)
else:
@ -28,27 +33,25 @@ def append_configs(sim_configs, initial_state, seeds, raw_exogenous_states, env_
if isinstance(sim_configs, list):
for sim_config in sim_configs:
configs.append(
Configuration(
sim_config=sim_config,
initial_state=initial_state,
seeds=seeds,
exogenous_states=exogenous_states,
env_processes=env_processes,
partial_state_updates=partial_state_updates
)
)
elif isinstance(sim_configs, dict):
configs.append(
Configuration(
sim_config=sim_configs,
config = Configuration(
sim_config=sim_config,
initial_state=initial_state,
seeds=seeds,
exogenous_states=exogenous_states,
env_processes=env_processes,
partial_state_updates=partial_state_updates
partial_state_update_blocks=partial_state_update_blocks
)
configs.append(config)
elif isinstance(sim_configs, dict):
config = Configuration(
sim_config=sim_configs,
initial_state=initial_state,
seeds=seeds,
exogenous_states=exogenous_states,
env_processes=env_processes,
partial_state_update_blocks=partial_state_update_blocks
)
configs.append(config)
class Identity:
@ -84,10 +87,11 @@ class Processor:
self.apply_identity_funcs = id.apply_identity_funcs
def create_matrix_field(self, partial_state_updates, key):
if key == 'states':
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:
@ -113,15 +117,18 @@ class Processor:
def only_ep_handler(state_dict):
sdf_functions = [
lambda sub_step, sL, s, _input: (k, v) for k, v in zip(state_dict.keys(), state_dict.values())
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, 'states')
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:

View File

@ -4,14 +4,21 @@ from copy import deepcopy
from fn.func import curried
import pandas as pd
# Temporary
from cadCAD.configuration.utils.depreciationHandler import sanitize_partial_state_updates
from cadCAD.utils import dict_filter, contains_type
# ToDo: Fix - Returns empty when partial_state_update is missing in Configuration
class TensorFieldReport:
def __init__(self, config_proc):
self.config_proc = config_proc
def create_tensor_field(self, partial_state_updates, exo_proc, keys=['policies', 'states']):
# 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))):
@ -20,12 +27,8 @@ class TensorFieldReport:
return df
# def s_update(y, x):
# return lambda step, sL, s, _input: (y, x)
#
#
def state_update(y, x):
return lambda sub_step, sL, s, _input: (y, x)
return lambda var_dict, sub_step, sL, s, _input: (y, x)
def bound_norm_random(rng, low, high):
@ -52,7 +55,7 @@ def time_step(dt_str, dt_format='%Y-%m-%d %H:%M:%S', _timedelta = tstep_delta):
ep_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 = ep_t_delta):
if s['sub_step'] == 0:
if s['substep'] == 0:
return time_step(dt_str, fromat_str, _timedelta)
else:
return dt_str
@ -114,7 +117,7 @@ def sweep_states(state_type, states, in_config):
def exo_update_per_ts(ep):
@curried
def ep_decorator(f, y, var_dict, sub_step, sL, s, _input):
if s['sub_step'] + 1 == 1:
if s['substep'] + 1 == 1:
return f(var_dict, sub_step, sL, s, _input)
else:
return y, s[y]

View File

@ -0,0 +1,41 @@
from copy import deepcopy
def sanitize_config(config):
# for backwards compatibility, we accept old arguments via **kwargs
# TODO: raise specific deprecation warnings for key == 'state_dict', key == 'seed', key == 'mechanisms'
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)
# for backwards compatibility we accept the old keys
# ('behaviors' and 'states') and rename them
def rename_keys(d):
if 'behaviors' in d:
d['policies'] = d.pop('behaviors')
if 'states' in d:
d['variables'] = d.pop('states')
# Also for backwards compatibility, we accept partial state update blocks both as list or dict
# No need for a deprecation warning as it's already raised by cadCAD.utils.key_filter
if (type(new_partial_state_updates)==list):
for v in new_partial_state_updates:
rename_keys(v)
elif (type(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

@ -65,6 +65,7 @@ class Executor:
config_idx += 1
if self.exec_context == ExecutionMode.single_proc:
# ToDO: Deprication Handler - "sanitize" in appropriate place
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)
return result, tensor_field

View File

@ -7,6 +7,7 @@ id_exception = engine_exception(KeyError, KeyError, None)
class Executor:
def __init__(self, policy_ops, policy_update_exception=id_exception, state_update_exception=id_exception):
self.policy_ops = policy_ops # behavior_ops
self.state_update_exception = state_update_exception
@ -49,20 +50,21 @@ class Executor:
del last_in_obj
self.apply_env_proc(env_processes, last_in_copy, last_in_copy['timestep']) # not time_step
self.apply_env_proc(env_processes, last_in_copy, last_in_copy['timestep'])
last_in_copy["sub_step"], last_in_copy["time_step"], last_in_copy['run'] = sub_step, time_step, run
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
def state_update_pipeline(self, var_dict, states_list, configs, env_processes, time_step, run):
sub_step = 0
states_list_copy = deepcopy(states_list)
genesis_states = states_list_copy[-1]
genesis_states['sub_step'], genesis_states['time_step'] = sub_step, time_step
genesis_states['substep'], genesis_states['timestep'] = sub_step, time_step
states_list = [genesis_states]
sub_step += 1
@ -93,7 +95,7 @@ class Executor:
states_list_copy = deepcopy(states_list)
head, *tail = self.run_pipeline(var_dict, states_list_copy, configs, env_processes, time_seq, run)
genesis = head.pop()
genesis['sub_step'], genesis['time_step'], genesis['run'] = 0, 0, run
genesis['substep'], genesis['timestep'], genesis['run'] = 0, 0, run
first_timestep_per_run = [genesis] + tail.pop(0)
pipe_run += [first_timestep_per_run] + tail
del states_list_copy

View File

@ -75,7 +75,8 @@ def contains_type(_collection, type):
def drop_right(l, n):
return l[:len(l) - n]
# backwards compatibility
# ToDo: Encapsulate in function
def key_filter(l, keyname):
if (type(l) == list):
return [v[keyname] for v in l]
@ -132,4 +133,4 @@ def curry_pot(f, *argv):
# def decorator(f):
# f.__name__ = newname
# return f
# return decorator
# return decorator

Binary file not shown.

BIN
dist/cadCAD-0.2-py3-none-any.whl vendored Normal file

Binary file not shown.

View File

@ -11,11 +11,11 @@ long_description = "cadCAD is a differential games based simulation software pac
monte carlo analysis and other common numerical methods is provided."
setup(name='cadCAD',
version='0.1',
version='0.2',
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/DiffyQ-cadCAD',
long_description=long_description,
url='https://github.com/BlockScience/DiffyQ-SimCAD',
author='Joshua E. Jodesty',
author_email='joshua@block.science',
# license='LICENSE',

File diff suppressed because one or more lines are too long

View File

@ -2,7 +2,7 @@ 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 simulations.validation import config2 #sweep_config, config1, config2, config4
from cadCAD import configs
exec_mode = ExecutionMode()
@ -21,14 +21,14 @@ 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()
# 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()

View File

@ -98,20 +98,20 @@ genesis_states = {
's2': Decimal(0.0),
's3': Decimal(1.0),
's4': Decimal(1.0),
'timestep': '2018-10-01 15:16:24'
# 'timestep': '2018-10-01 15:16:24'
}
raw_exogenous_states = {
"s3": es3p1,
"s4": es4p2,
"timestep": es5p2
# "timestep": es5p2
}
env_processes = {
"s3": env_a,
"s4": proc_trigger('2018-10-01 15:16:25', env_b)
"s4": proc_trigger(1, env_b)
}
@ -121,7 +121,7 @@ partial_state_update_block = {
"b1": p1m1,
"b2": p2m1
},
"states": {
"variables": {
"s1": s1m1,
"s2": s2m1
}
@ -131,7 +131,7 @@ partial_state_update_block = {
"b1": p1m2,
"b2": p2m2
},
"states": {
"variables": {
"s1": s1m2,
"s2": s2m2
}
@ -141,7 +141,7 @@ partial_state_update_block = {
"b1": p1m3,
"b2": p2m3
},
"states": {
"variables": {
"s1": s1m3,
"s2": s2m3
}
@ -163,5 +163,5 @@ append_configs(
seeds=seeds,
raw_exogenous_states=raw_exogenous_states,
env_processes=env_processes,
partial_state_updates=partial_state_update_block
partial_state_update_blocks=partial_state_update_block
)

View File

@ -97,20 +97,20 @@ genesis_states = {
's2': Decimal(0.0),
's3': Decimal(1.0),
's4': Decimal(1.0),
'timestep': '2018-10-01 15:16:24'
# 'timestep': '2018-10-01 15:16:24'
}
raw_exogenous_states = {
"s3": es3p1,
"s4": es4p2,
"timestep": es5p2
# "timestep": es5p2
}
env_processes = {
"s3": proc_trigger('2018-10-01 15:16:25', env_a),
"s4": proc_trigger('2018-10-01 15:16:25', env_b)
"s3": proc_trigger(1, env_a),
"s4": proc_trigger(1, env_b)
}
@ -162,5 +162,5 @@ append_configs(
seeds=seeds,
raw_exogenous_states=raw_exogenous_states,
env_processes=env_processes,
partial_state_updates=partial_state_update_block
partial_state_update_blocks=partial_state_update_block
)

View File

@ -0,0 +1,142 @@
from decimal import Decimal
import numpy as np
from datetime import timedelta
from cadCAD.configuration import append_configs
from cadCAD.configuration.utils import proc_trigger, bound_norm_random, ep_time_step
from cadCAD.configuration.utils.parameterSweep import 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
# 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'
}
raw_exogenous_states = {
"s3": es3p1,
"s4": es4p2,
"timestamp": es5p2
}
env_processes = {
"s3": env_a,
"s4": 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
)

View File

@ -114,7 +114,7 @@ genesis_states = {
's2': Decimal(0.0),
's3': Decimal(1.0),
's4': Decimal(1.0),
'timestep': '2018-10-01 15:16:24'
# 'timestep': '2018-10-01 15:16:24'
}
@ -122,13 +122,13 @@ genesis_states = {
raw_exogenous_states = {
"s3": es3p1,
"s4": es4p2,
"timestep": es5p2
# "timestep": es5p2
}
# ToDo: make env proc trigger field agnostic
# ToDo: input json into function renaming __name__
triggered_env_b = proc_trigger('2018-10-01 15:16:25', env_b)
triggered_env_b = proc_trigger(1, env_b)
env_processes = {
"s3": env_a, #sweep(beta, env_a),
"s4": triggered_env_b #rename('parameterized', triggered_env_b) #sweep(beta, triggered_env_b)
@ -149,7 +149,7 @@ partial_state_update_block = {
"b1": p1m1,
"b2": p2m1
},
"states": {
"variables": {
"s1": s1m1,
"s2": s2m1
}
@ -159,7 +159,7 @@ partial_state_update_block = {
"b1": p1m2,
"b2": p2m2,
},
"states": {
"variables": {
"s1": s1m2,
"s2": s2m2
}
@ -169,7 +169,7 @@ partial_state_update_block = {
"b1": p1m3,
"b2": p2m3
},
"states": {
"variables": {
"s1": s1m3,
"s2": s2m3
}
@ -192,5 +192,5 @@ append_configs(
seeds=seeds,
raw_exogenous_states=raw_exogenous_states,
env_processes=env_processes,
partial_state_updates=partial_state_update_block
partial_state_update_blocks=partial_state_update_block
)