From 964e3f7bc11a1479987c7bd791ab580f0cb15d8c Mon Sep 17 00:00:00 2001 From: "Joshua E. Jodesty" Date: Fri, 7 Jun 2019 10:40:45 -0400 Subject: [PATCH 1/9] test partially done --- cadCAD/configuration/utils/__init__.py | 13 +-- .../historical_state_access.py | 10 +- .../regression_tests/policy_aggregation.py | 5 +- simulations/regression_tests/tests.py | 36 +++++++ .../test_executions/policy_agg_test.py | 4 + testing/__init__.py | 0 testing/example.py | 20 ++++ testing/example2.py | 71 ++++++++++++++ testing/generic_test.py | 39 ++++++++ .../system_models/historical_state_access.py | 97 +++++++++++++++++++ testing/system_models/policy_aggregation.py | 90 +++++++++++++++++ testing/tests/__init__.py | 0 testing/tests/historical_state_access.py | 84 ++++++++++++++++ testing/tests/policy_aggregation.py | 32 ++++++ testing/utils.py | 28 ++++++ 15 files changed, 517 insertions(+), 12 deletions(-) create mode 100644 simulations/regression_tests/tests.py create mode 100644 testing/__init__.py create mode 100644 testing/example.py create mode 100644 testing/example2.py create mode 100644 testing/generic_test.py create mode 100644 testing/system_models/historical_state_access.py create mode 100644 testing/system_models/policy_aggregation.py create mode 100644 testing/tests/__init__.py create mode 100644 testing/tests/historical_state_access.py create mode 100644 testing/tests/policy_aggregation.py create mode 100644 testing/utils.py diff --git a/cadCAD/configuration/utils/__init__.py b/cadCAD/configuration/utils/__init__.py index 3efdcc3..8c16b8c 100644 --- a/cadCAD/configuration/utils/__init__.py +++ b/cadCAD/configuration/utils/__init__.py @@ -202,19 +202,20 @@ def genereate_psubs(policy_grid, states_grid, policies, state_updates): return PSUBS -def access_block(sH, y, psu_block_offset, exculsion_list=[]): - exculsion_list += [y] +# ToDo: DO NOT filter sH for every state/policy update. Requires a consumable sH (new sH) +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(sH) >= abs(psu_block_offset): - return filter_history(exculsion_list, sH[psu_block_offset]) + if len(state_history) >= abs(psu_block_offset): + return filter_history(exculsion_list, state_history[psu_block_offset]) else: return [] - elif psu_block_offset < 0: - return filter_history(exculsion_list, sH[psu_block_offset]) + elif psu_block_offset == -1: + return filter_history(exculsion_list, state_history[psu_block_offset]) else: return [] \ No newline at end of file diff --git a/simulations/regression_tests/historical_state_access.py b/simulations/regression_tests/historical_state_access.py index 67dce76..08382ee 100644 --- a/simulations/regression_tests/historical_state_access.py +++ b/simulations/regression_tests/historical_state_access.py @@ -7,9 +7,15 @@ exclusion_list = ['nonexsistant', 'last_x', '2nd_to_last_x', '3rd_to_last_x', '4 # 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(sH, "last_x", -1, exclusion_list)} + 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): diff --git a/simulations/regression_tests/policy_aggregation.py b/simulations/regression_tests/policy_aggregation.py index f5e3916..a81ac07 100644 --- a/simulations/regression_tests/policy_aggregation.py +++ b/simulations/regression_tests/policy_aggregation.py @@ -1,4 +1,3 @@ -import numpy as np from cadCAD.configuration import append_configs from cadCAD.configuration.utils import config_sim @@ -73,14 +72,12 @@ sim_config = config_sim( } ) - - # Aggregation == Reduce Map / Reduce Map Aggregation -# ToDo: subsequent functions should accept the entire datastructure # 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 ) \ No newline at end of file diff --git a/simulations/regression_tests/tests.py b/simulations/regression_tests/tests.py new file mode 100644 index 0000000..3226c62 --- /dev/null +++ b/simulations/regression_tests/tests.py @@ -0,0 +1,36 @@ +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() \ No newline at end of file diff --git a/simulations/test_executions/policy_agg_test.py b/simulations/test_executions/policy_agg_test.py index ec6d564..f1da13f 100644 --- a/simulations/test_executions/policy_agg_test.py +++ b/simulations/test_executions/policy_agg_test.py @@ -1,9 +1,12 @@ +from pprint import pprint + 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 policy_aggregation from cadCAD import configs +from testing.utils import generate_assertions exec_mode = ExecutionMode() @@ -15,6 +18,7 @@ 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')) diff --git a/testing/__init__.py b/testing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/example.py b/testing/example.py new file mode 100644 index 0000000..09ee3aa --- /dev/null +++ b/testing/example.py @@ -0,0 +1,20 @@ +import unittest + +class TestStringMethods(unittest.TestCase): + + 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() \ No newline at end of file diff --git a/testing/example2.py b/testing/example2.py new file mode 100644 index 0000000..081ec32 --- /dev/null +++ b/testing/example2.py @@ -0,0 +1,71 @@ +from functools import reduce + +import pandas as pd +import unittest +from parameterized import parameterized +from tabulate import tabulate + +from testing.system_models.policy_aggregation import run +from testing.generic_test import make_generic_test +from testing.utils import generate_assertions_df + +raw_result, tensor_field = run.execute() +result = pd.DataFrame(raw_result) + +expected_results = { + (1, 0, 0): {'policies': {}, 's1': 0}, + (1, 1, 1): {'policies': {'policy1': 2, 'policy2': 4}, 's1': 500}, + (1, 1, 2): {'policies': {'policy1': 8, 'policy2': 8}, 's1': 2}, + (1, 1, 3): {'policies': {'policy1': 4, 'policy2': 8, 'policy3': 12}, 's1': 3}, + (1, 2, 1): {'policies': {'policy1': 2, 'policy2': 4}, 's1': 4}, + (1, 2, 2): {'policies': {'policy1': 8, 'policy2': 8}, 's1': 5}, + (1, 2, 3): {'policies': {'policy1': 4, 'policy2': 8, 'policy3': 12}, 's1': 6}, + (1, 3, 1): {'policies': {'policy1': 2, 'policy2': 4}, 's1': 7}, + (1, 3, 2): {'policies': {'policy1': 8, 'policy2': 8}, 's1': 8}, + (1, 3, 3): {'policies': {'policy1': 4, 'policy2': 8, 'policy3': 12}, 's1': 9} +} + +params = [["policy_aggregation", result, expected_results, ['policies', 's1']]] + + +class TestSequence(unittest.TestCase): + @parameterized.expand(params) + def test_validate_results(self, name, result_df, expected_reults, target_cols): + # alt for (*) Exec Debug mode + tested_df = generate_assertions_df(result_df, expected_reults, target_cols) + + erroneous = tested_df[(tested_df['test'] == False)] + for index, row in erroneous.iterrows(): + expected = expected_reults[(row['run'], row['timestep'], row['substep'])] + unexpected = {k: expected[k] for k in expected if k in row and expected[k] != row[k]} + for key in unexpected.keys(): + erroneous[f"invalid_{key}"] = unexpected[key] + # etc. + + # def etc. + + print() + print(tabulate(erroneous, headers='keys', tablefmt='psql')) + + self.assertEqual(reduce(lambda a, b: a and b, tested_df['test']), True) + + s = 'hello world' + # self.assertEqual(s.split(), 1) + # # check that s.split fails when the separator is not a string + # with self.assertRaises(AssertionError): + # tested_df[(tested_df['test'] == False)] + # erroneous = tested_df[(tested_df['test'] == False)] + # for index, row in erroneous.iterrows(): + # expected = expected_reults[(row['run'], row['timestep'], row['substep'])] + # unexpected = {k: expected[k] for k in expected if k in row and expected[k] != row[k]} + # for key in unexpected.keys(): + # erroneous[f"invalid_{key}"] = unexpected[key] + # # etc. + # + # # def etc. + # + # print() + # print(tabulate(erroneous, headers='keys', tablefmt='psql')) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/testing/generic_test.py b/testing/generic_test.py new file mode 100644 index 0000000..19b562c --- /dev/null +++ b/testing/generic_test.py @@ -0,0 +1,39 @@ +import unittest +from parameterized import parameterized +from functools import reduce +from tabulate import tabulate +from testing.utils import generate_assertions_df + +# ToDo: Exec Debug mode (*) for which state and policy updates are validated during runtime using `expected_results` +# EXAMPLE: ('state_test' T/F, 'policy_test' T/F) +# ToDo: (Sys Model Config) give `expected_results to` `Configuration` for Exec Debug mode (*) +# ToDo: (expected_results) Function to generate sys metrics keys using system model config +# ToDo: (expected_results) Function to generate target_vals given user input (apply fancy validation lib later on) + + +# ToDo: Use self.assertRaises(AssertionError) + + +def make_generic_test(params): + class TestSequence(unittest.TestCase): + @parameterized.expand(params) + def test_validate_results(self, name, result_df, expected_reults, target_cols): + # alt for (*) Exec Debug mode + tested_df = generate_assertions_df(result_df, expected_reults, target_cols) + erroneous = tested_df[(tested_df['test'] == False)] + if erroneous.empty is False: + for index, row in erroneous.iterrows(): + expected = expected_reults[(row['run'], row['timestep'], row['substep'])] + unexpected = {k: expected[k] for k in expected if k in row and expected[k] != row[k]} + for key in unexpected.keys(): + erroneous[f"invalid_{key}"] = unexpected[key] + # etc. + + print() + print(tabulate(erroneous, headers='keys', tablefmt='psql')) + + self.assertTrue(reduce(lambda a, b: a and b, tested_df['test'])) + + # def etc. + + return TestSequence diff --git a/testing/system_models/historical_state_access.py b/testing/system_models/historical_state_access.py new file mode 100644 index 0000000..839a826 --- /dev/null +++ b/testing/system_models/historical_state_access.py @@ -0,0 +1,97 @@ +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 + +# 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 +) + +exec_mode = ExecutionMode() +single_proc_ctx = ExecutionContext(context=exec_mode.single_proc) +run = Executor(exec_context=single_proc_ctx, configs=configs) diff --git a/testing/system_models/policy_aggregation.py b/testing/system_models/policy_aggregation.py new file mode 100644 index 0000000..aae9234 --- /dev/null +++ b/testing/system_models/policy_aggregation.py @@ -0,0 +1,90 @@ +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, 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 +# ToDo: subsequent functions should accept the entire datastructure +# 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, + policy_ops=[lambda a, b: a + b, lambda y: y * 2] # Default: lambda a, b: a + b ToDO: reduction function requires high lvl explanation +) + +exec_mode = ExecutionMode() +single_proc_ctx = ExecutionContext(context=exec_mode.single_proc) +run = Executor(exec_context=single_proc_ctx, configs=configs) diff --git a/testing/tests/__init__.py b/testing/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/tests/historical_state_access.py b/testing/tests/historical_state_access.py new file mode 100644 index 0000000..4ab145f --- /dev/null +++ b/testing/tests/historical_state_access.py @@ -0,0 +1,84 @@ +import unittest +import pandas as pd +from tabulate import tabulate + +from testing.generic_test import make_generic_test +from testing.system_models.historical_state_access import run +from testing.utils import generate_assertions_df + +raw_result, tensor_field = run.execute() +result = pd.DataFrame(raw_result) + +expected_results = { + (1, 0, 0): {'x': 0, 'nonexsistant': [], 'last_x': [], '2nd_to_last_x': [], '3rd_to_last_x': [], '4th_to_last_x': []}, + (1, 1, 1): {'x': 1, + 'nonexsistant': [], + 'last_x': [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}], + '2nd_to_last_x': [], + '3rd_to_last_x': [], + '4th_to_last_x': []}, + (1, 1, 2): {'x': 2, + 'nonexsistant': [], + 'last_x': [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}], + '2nd_to_last_x': [], + '3rd_to_last_x': [], + '4th_to_last_x': []}, + (1, 1, 3): {'x': 3, + 'nonexsistant': [], + 'last_x': [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}], + '2nd_to_last_x': [], + '3rd_to_last_x': [], + '4th_to_last_x': []}, + (1, 2, 1): {'x': 4, + 'nonexsistant': [], + 'last_x': [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}], + '2nd_to_last_x': [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}], + '3rd_to_last_x': [], + '4th_to_last_x': []}, + (1, 2, 2): {'x': 5, + 'nonexsistant': [], + 'last_x': [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}], + '2nd_to_last_x': [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}], + '3rd_to_last_x': [], + '4th_to_last_x': []}, + (1, 2, 3): {'x': 6, + 'nonexsistant': [], + 'last_x': [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}], + '2nd_to_last_x': [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}], + '3rd_to_last_x': [], + '4th_to_last_x': []}, + (1, 3, 1): {'x': 7, + 'nonexsistant': [], + 'last_x': [{'x': 4, 'run': 1, 'substep': 1, 'timestep': 2}, {'x': 5, 'run': 1, 'substep': 2, 'timestep': 2}, {'x': 6, 'run': 1, 'substep': 3, 'timestep': 2}], + '2nd_to_last_x': [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}], + '3rd_to_last_x': [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}], + '4th_to_last_x': []}, + (1, 3, 2): {'x': 8, + 'nonexsistant': [], + 'last_x': [{'x': 4, 'run': 1, 'substep': 1, 'timestep': 2}, {'x': 5, 'run': 1, 'substep': 2, 'timestep': 2}, {'x': 6, 'run': 1, 'substep': 3, 'timestep': 2}], + '2nd_to_last_x': [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}], + '3rd_to_last_x': [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}], + '4th_to_last_x': []}, + (1, 3, 3): {'x': 9, + 'nonexsistant': [], + 'last_x': [{'x': 4, 'run': 1, 'substep': 1, 'timestep': 2}, {'x': 5, 'run': 1, 'substep': 2, 'timestep': 2}, {'x': 6, 'run': 1, 'substep': 3, 'timestep': 2}], + '2nd_to_last_x': [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}], + '3rd_to_last_x': [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}], + '4th_to_last_x': []} +} + +params = [["historical_state_access", result, expected_results, + ['x', 'nonexsistant', 'last_x', '2nd_to_last_x', '3rd_to_last_x', '4th_to_last_x']] + ] +# df = generate_assertions_df(result, expected_results, +# ['x', 'nonexsistant', 'last_x', '2nd_to_last_x', '3rd_to_last_x', '4th_to_last_x'] +# ) +# print(tabulate(df, headers='keys', tablefmt='psql')) + + +class GenericTest(make_generic_test(params)): + pass + + +if __name__ == '__main__': + unittest.main() diff --git a/testing/tests/policy_aggregation.py b/testing/tests/policy_aggregation.py new file mode 100644 index 0000000..4be0da4 --- /dev/null +++ b/testing/tests/policy_aggregation.py @@ -0,0 +1,32 @@ +import unittest +import pandas as pd +from testing.generic_test import make_generic_test +from testing.system_models.policy_aggregation import run + +raw_result, tensor_field = run.execute() +result = pd.DataFrame(raw_result) + +expected_results = { + (1, 0, 0): {'policies': {}, 's1': 0}, + (1, 1, 1): {'policies': {'policy1': 2, 'policy2': 4}, 's1': 500}, + (1, 1, 2): {'policies': {'policy1': 8, 'policy2': 8}, 's1': 2}, + (1, 1, 3): {'policies': {'policy1': 4, 'policy2': 8, 'policy3': 12}, 's1': 3}, + (1, 2, 1): {'policies': {'policy1': 2, 'policy2': 4}, 's1': 4}, + (1, 2, 2): {'policies': {'policy1': 8, 'policy2': 8}, 's1': 5}, + (1, 2, 3): {'policies': {'policy1': 4, 'policy2': 8, 'policy3': 12}, 's1': 6}, + (1, 3, 1): {'policies': {'policy1': 2, 'policy2': 4}, 's1': 7}, + (1, 3, 2): {'policies': {'policy1': 8, 'policy2': 8}, 's1': 8}, + (1, 3, 3): {'policies': {'policy1': 4, 'policy2': 8, 'policy3': 12}, 's1': 9} +} + +params = [["policy_aggregation", result, expected_results, ['policies', 's1']]] +# df = generate_assertions_df(result, expected_results, ['policies', 's1']) +# print(tabulate(df, headers='keys', tablefmt='psql')) + + +class GenericTest(make_generic_test(params)): + pass + + +if __name__ == '__main__': + unittest.main() diff --git a/testing/utils.py b/testing/utils.py new file mode 100644 index 0000000..62dc439 --- /dev/null +++ b/testing/utils.py @@ -0,0 +1,28 @@ +def gen_metric_row(row): + return ((row['run'], row['timestep'], row['substep']), {'s1': row['s1'], 'policies': row['policies']}) + +def gen_metric_row(row): + return { + 'run': row['run'], + 'timestep': row['timestep'], + 'substep': row['substep'], + 's1': row['s1'], + 'policies': row['policies'] + } + +def gen_metric_dict(df): + return [gen_metric_row(row) for index, row in df.iterrows()] + +def generate_assertions_df(df, expected_results, target_cols): + def df_filter(run, timestep, substep): + return df[ + (df['run'] == run) & (df['timestep'] == timestep) & (df['substep'] == substep) + ][target_cols].to_dict(orient='records')[0] + + df['test'] = df.apply( + lambda x: \ + df_filter(x['run'], x['timestep'], x['substep']) == expected_results[(x['run'], x['timestep'], x['substep'])] + , axis=1 + ) + + return df \ No newline at end of file From c55e43392026ff99919f54953879c30e8c73e3e0 Mon Sep 17 00:00:00 2001 From: "Joshua E. Jodesty" Date: Fri, 19 Jul 2019 10:59:05 -0400 Subject: [PATCH 2/9] docs pending review --- README.md | 38 ++- .../configuration/utils/userDefinedObject.py | 18 +- cadCAD/engine/simulation.py | 36 +++ cadCAD/utils/__init__.py | 2 - documentation/examples/__init__.py | 0 documentation/examples/example_1.py | 45 ++++ .../examples/historical_state_access.py | 111 +++++++++ documentation/examples/param_sweep.py | 116 +++++++++ documentation/examples/policy_aggregation.py | 98 ++++++++ documentation/examples/sys_model_A.py | 159 +++++++++++++ documentation/examples/sys_model_AB_exec.py | 24 ++ documentation/examples/sys_model_A_exec.py | 22 ++ documentation/examples/sys_model_B.py | 147 ++++++++++++ documentation/examples/sys_model_B_exec.py | 23 ++ documentation/execution.md | 71 ++++++ documentation/historical_state_access.md | 97 ++++++++ documentation/param_sweep.md | 68 ++++++ documentation/policy_agg.md | 60 +++++ documentation/sys_model_config.md | 220 ++++++++++++++++++ simulations/regression_tests/config1.py | 17 +- simulations/regression_tests/config2.py | 5 +- simulations/regression_tests/udo.py | 9 +- simulations/test_executions/config1_test.py | 4 +- .../historical_state_access_test.py | 4 +- .../test_executions/policy_agg_test.py | 2 - simulations/test_executions/udo_test.py | 8 +- testing/generic_test.py | 60 +++-- testing/system_models/__init__.py | 0 testing/system_models/external_dataset.py | 67 ++++++ .../system_models/historical_state_access.py | 4 +- testing/system_models/param_sweep.py | 110 +++++++++ testing/system_models/policy_aggregation.py | 6 +- testing/system_models/udo.py | 185 +++++++++++++++ testing/tests/external_test.py | 127 ++++++++++ testing/tests/historical_state_access.py | 84 +++++-- testing/tests/multi_config_test.py | 56 +++++ testing/tests/param_sweep.py | 85 +++++++ testing/tests/policy_aggregation.py | 19 +- testing/tests/udo.py | 39 ++++ testing/utils.py | 41 ++-- 40 files changed, 2180 insertions(+), 107 deletions(-) create mode 100644 documentation/examples/__init__.py create mode 100644 documentation/examples/example_1.py create mode 100644 documentation/examples/historical_state_access.py create mode 100644 documentation/examples/param_sweep.py create mode 100644 documentation/examples/policy_aggregation.py create mode 100644 documentation/examples/sys_model_A.py create mode 100644 documentation/examples/sys_model_AB_exec.py create mode 100644 documentation/examples/sys_model_A_exec.py create mode 100644 documentation/examples/sys_model_B.py create mode 100644 documentation/examples/sys_model_B_exec.py create mode 100644 documentation/execution.md create mode 100644 documentation/historical_state_access.md create mode 100644 documentation/param_sweep.md create mode 100644 documentation/policy_agg.md create mode 100644 documentation/sys_model_config.md create mode 100644 testing/system_models/__init__.py create mode 100644 testing/system_models/external_dataset.py create mode 100644 testing/system_models/param_sweep.py create mode 100644 testing/system_models/udo.py create mode 100644 testing/tests/external_test.py create mode 100644 testing/tests/multi_config_test.py create mode 100644 testing/tests/param_sweep.py create mode 100644 testing/tests/udo.py diff --git a/README.md b/README.md index 0719875..f919ca9 100644 --- a/README.md +++ b/README.md @@ -4,25 +4,41 @@ **Description:** -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 \ -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. - -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. +cadCAD (complex adaptive systems computer-aided design) is a python based, unified modeling framework for stochastic +dynamical systems and differential games for research, validation, and Computer Aided Design of economic systems created +by BlockScience. It is capable of modeling systems at all levels of abstraction from Agent Based Modeling (ABM) to +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. + + +In essence, cadCAD tool allows us to represent a company’s or community’s current business model along with a desired +future state and helps make informed, rigorously tested decisions on how to get from today’s stage to the future state. +It allows us to use code to solidify our conceptualized ideas and see if the outcome meets our expectations. We can +iteratively refine our work until we have constructed a model that closely reflects reality at the start of the model, +and see how it evolves. We can then use these results to inform business decisions. + +#### Simulation Instructional: +* ##### [System Model Configuration](link) +* ##### [System Simulation Execution](link) + +#### Installation: **1. Install Dependencies:** **Option A:** Package Repository Access ***IMPORTANT NOTE:*** Tokens are issued to and meant to be used by trial users and BlockScience employees **ONLY**. Replace \ with an issued token in the script below. ```bash -pip3 install pandas pathos fn tabulate +pip3 install pandas pathos fn funcy tabulate pip3 install cadCAD --extra-index-url https://@repo.fury.io/blockscience/ ``` diff --git a/cadCAD/configuration/utils/userDefinedObject.py b/cadCAD/configuration/utils/userDefinedObject.py index c1f532c..4ced71f 100644 --- a/cadCAD/configuration/utils/userDefinedObject.py +++ b/cadCAD/configuration/utils/userDefinedObject.py @@ -5,7 +5,6 @@ from pandas.core.frame import DataFrame from cadCAD.utils import SilentDF - def val_switch(v): if isinstance(v, DataFrame) is True: return SilentDF(v) @@ -18,7 +17,7 @@ class udcView(object): self.masked_members = masked_members # returns dict to dataframe - # def __repr__(self): + def __repr__(self): members = {} variables = { @@ -26,9 +25,20 @@ class udcView(object): if str(type(v)) != "" 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)) == ""] + members.update(variables) return f"{members}" #[1:-1] + # def __repr__(self): + # members = {} + # variables = { + # k: val_switch(v) for k, v in self.__dict__.items() + # if str(type(v)) != "" and k not in self.masked_members and k == 'x' # and isinstance(v, DataFrame) is not True + # } + # + # members.update(variables) + # return f"{members}" #[1:-1] + class udcBroker(object): def __init__(self, obj, function_filter=['__init__']): @@ -36,7 +46,8 @@ class udcBroker(object): 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(deepcopy(vars(obj))) # somehow is enough + d.update(vars(obj)) # somehow is enough d.update(filtered_functions) self.members_dict = d @@ -57,4 +68,3 @@ def UDO(udo, masked_members=['obj']): def udoPipe(obj_view): return UDO(obj_view.obj, obj_view.masked_members) - diff --git a/cadCAD/engine/simulation.py b/cadCAD/engine/simulation.py index 8a5c5df..1823a34 100644 --- a/cadCAD/engine/simulation.py +++ b/cadCAD/engine/simulation.py @@ -115,12 +115,48 @@ class Executor: 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)) + # ToDo: add env_proc generator to `last_in_copy` iterator as wrapper function # ToDo: Can be multithreaded ?? 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 generate_record(state_funcs): + # for f in state_funcs: + # tmp_last_in_copy = deepcopy(last_in_obj) + # new_kv = self.state_update_exception(f(sweep_dict, sub_step, sH, tmp_last_in_copy, _input)) + # del tmp_last_in_copy + # yield new_kv + # + # # get `state` from last_in_obj.keys() + # # vals = last_in_obj.values() + # def generate_record(state_funcs): + # for state, v, f in zip(states, vals, state_funcs): + # v_copy = deepcopy(v) + # last_in_obj[state] = v_copy + # new_kv = self.state_update_exception(f(sweep_dict, sub_step, sH, last_in_copy, _input)) + # del v + # yield new_kv + + # {k: v for k, v in l} + + # r() - r(a') -> r(a',b') -> r(a',b',c') + + # r(f(a),b,c) -> r(a'f(b),c) -> r(a',b',f(c)) => r(a',b',c') + # r(a',b.update(),c) + # r1(f(a1),b1,c1) -> r2(a2,f(b1),c1) -> r3(a3,b1,f(c1)) => r(a',b',c') + + # r1(f(a1),b,c) -> r2(a,f(b1),c) -> r3(a,b,f(c1)) => r(a',b',c') + + # r1(f(a1),b1,c1) -> r(a2',b2.update(),c2) -> r3(a3,b1,f(c1)) => r(a',b',c') + + + # r1(f(a1),b1,c1) -> r2(a2,f(b1),c1) -> r3(a3,b1,f(c1)) => r(a',b',c') + + + # reduce(lambda r: F(r), [r2(f(a),b,c), r2(a,f(b),c), r3(a,b,f(c))]) => R(a',b',c') + def transfer_missing_fields(source, destination): for k in source: if k not in destination: diff --git a/cadCAD/utils/__init__.py b/cadCAD/utils/__init__.py index 44717b5..ccb5f9d 100644 --- a/cadCAD/utils/__init__.py +++ b/cadCAD/utils/__init__.py @@ -15,14 +15,12 @@ def append_dict(dict, new_dict): dict.update(new_dict) return dict - # def val_switch(v): # if isinstance(v, DataFrame) is True or isinstance(v, SilentDF) is True: # return SilentDF(v) # else: # return v.x - class IndexCounter: def __init__(self): self.i = 0 diff --git a/documentation/examples/__init__.py b/documentation/examples/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/documentation/examples/example_1.py b/documentation/examples/example_1.py new file mode 100644 index 0000000..ed1edfc --- /dev/null +++ b/documentation/examples/example_1.py @@ -0,0 +1,45 @@ +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 \ No newline at end of file diff --git a/documentation/examples/historical_state_access.py b/documentation/examples/historical_state_access.py new file mode 100644 index 0000000..5079988 --- /dev/null +++ b/documentation/examples/historical_state_access.py @@ -0,0 +1,111 @@ +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 + +# 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 +) + +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() \ No newline at end of file diff --git a/documentation/examples/param_sweep.py b/documentation/examples/param_sweep.py new file mode 100644 index 0000000..a118966 --- /dev/null +++ b/documentation/examples/param_sweep.py @@ -0,0 +1,116 @@ +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, sL, s): + return {'gamma': _params['gamma']} + + +def omega(_params, step, sL, s): + return {'omega': _params['omega'](7)} + + +# Internal States +def alpha(_params, step, sL, s, _input): + return 'alpha', _params['alpha'] + +def alpha_plus_gamma(_params, step, sL, s, _input): + return 'alpha_plus_gamma', _params['alpha'] + _params['gamma'] + + +def beta(_params, step, sL, s, _input): + return 'beta', _params['beta'] + + +def policies(_params, step, sL, s, _input): + return 'policies', _input + + +def sweeped(_params, step, sL, 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) + +partial_state_update_blocks = 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=partial_state_update_blocks +) + +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() \ No newline at end of file diff --git a/documentation/examples/policy_aggregation.py b/documentation/examples/policy_aggregation.py new file mode 100644 index 0000000..86313e7 --- /dev/null +++ b/documentation/examples/policy_aggregation.py @@ -0,0 +1,98 @@ +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, 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), + } +) + +append_configs( + sim_configs=sim_config, + initial_state=genesis_states, + partial_state_update_blocks=partial_state_update_block, + 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() \ No newline at end of file diff --git a/documentation/examples/sys_model_A.py b/documentation/examples/sys_model_A.py new file mode 100644 index 0000000..92a7fe3 --- /dev/null +++ b/documentation/examples/sys_model_A.py @@ -0,0 +1,159 @@ +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": 2, + "T": range(1), + } +) + +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] +) \ No newline at end of file diff --git a/documentation/examples/sys_model_AB_exec.py b/documentation/examples/sys_model_AB_exec.py new file mode 100644 index 0000000..95067b3 --- /dev/null +++ b/documentation/examples/sys_model_AB_exec.py @@ -0,0 +1,24 @@ +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 \ No newline at end of file diff --git a/documentation/examples/sys_model_A_exec.py b/documentation/examples/sys_model_A_exec.py new file mode 100644 index 0000000..8a630d9 --- /dev/null +++ b/documentation/examples/sys_model_A_exec.py @@ -0,0 +1,22 @@ +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: config1") +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() \ No newline at end of file diff --git a/documentation/examples/sys_model_B.py b/documentation/examples/sys_model_B.py new file mode 100644 index 0000000..7298d0b --- /dev/null +++ b/documentation/examples/sys_model_B.py @@ -0,0 +1,147 @@ +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 = [ + { + "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=partial_state_update_block +) \ No newline at end of file diff --git a/documentation/examples/sys_model_B_exec.py b/documentation/examples/sys_model_B_exec.py new file mode 100644 index 0000000..53eef37 --- /dev/null +++ b/documentation/examples/sys_model_B_exec.py @@ -0,0 +1,23 @@ +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 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() diff --git a/documentation/execution.md b/documentation/execution.md new file mode 100644 index 0000000..f34064b --- /dev/null +++ b/documentation/execution.md @@ -0,0 +1,71 @@ +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 | | | +| 1 | 2 | | | +| 2 | 3 | | | ++----+-----+--------------------------------+--------------------------------+ +``` + +##### Example Result: System Events DataFrame +```python ++----+-------+------------+-----------+------+-----------+ +| | 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] | ++----+-------+------------+-----------+------+-----------+ +``` + +##### [Single Process Example Execution](link) + +##### [Multiple Process Example Execution](link) diff --git a/documentation/historical_state_access.md b/documentation/historical_state_access.md new file mode 100644 index 0000000..944fc8a --- /dev/null +++ b/documentation/historical_state_access.md @@ -0,0 +1,97 @@ +Historical State Access +== +The 3rd parameter of state and policy update functions (labels as `sH` of type `List[List[dict]]`) provides access to +past Partial State Updates (PSU) given a negative offset number. `access_block` is used to access past PSUs +(`List[dict]`) from `sH`. + +Example: `-2` denotes to second to last PSU + +##### Exclusion List +Create a list of states to exclude from the reported PSU. +```python +exclusion_list = [ + 'nonexsistant', 'last_x', '2nd_to_last_x', '3rd_to_last_x', '4th_to_last_x' +] +``` +##### Example Policy Updates +###### Last partial state update +```python +from cadCAD.configuration.utils import config_sim, access_block + +def last_update(_g, 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. +###### 2nd to last partial state update +```python +def second2last_update(_g, substep, sH, s): + return {"2nd_to_last_x": access_block(sH, "2nd_to_last_x", -2, exclusion_list)} +``` + +##### Define State Updates +###### 3rd to last partial state update +```python +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) +``` +###### 4rd to last partial state update +```python +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) +``` +###### Non-exsistant partial state update +* `psu_block_offset >= 0` doesn't exsist +```python +def nonexsistant(_g, substep, sH, s, _input): + return 'nonexsistant', access_block(sH, "nonexsistant", 0, exclusion_list) +``` + +#### Example Simulation +link + +#### 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}] | ++----+-----------------------------------------------------------------------------------------------------------------------------------------------------+ +``` + +#### [Example Configuration](link) +#### [Example Results](link) \ No newline at end of file diff --git a/documentation/param_sweep.md b/documentation/param_sweep.md new file mode 100644 index 0000000..3822369 --- /dev/null +++ b/documentation/param_sweep.md @@ -0,0 +1,68 @@ +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, sL, 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(_g, step, sL, s): + return {'beta': _g['beta'], 'gamma': _g['gamma']} +``` +* Simulation 1: `{'beta': 2, 'gamma': 3]}` +* Simulation 2: `{'beta': 5, 'gamma': 4}` + +##### Configure Simulation +```python +from cadCAD.configuration.utils import config_sim + +sim_config = config_sim( + { + "N": 2, + "T": range(5), + "M": g, + } +) +``` + +#### [Example Configuration](link) +#### [Example Results](link) + + diff --git a/documentation/policy_agg.md b/documentation/policy_agg.md new file mode 100644 index 0000000..7ddaa50 --- /dev/null +++ b/documentation/policy_agg.md @@ -0,0 +1,60 @@ +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(_g, step, sL, s): + return {'policy1': 1} +def p2_psu1(_g, step, sL, 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(_g, step, sL, s): + return {'policy1': 2, 'policy2': 2} +def p2_psu2(_g, step, sL, 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(_g, step, sL, s): + return {'policy1': 1, 'policy2': 2, 'policy3': 3} +def p2_psu3(_g, step, sL, 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 Configuration](link) +#### [Example Results](link) \ No newline at end of file diff --git a/documentation/sys_model_config.md b/documentation/sys_model_config.md new file mode 100644 index 0000000..edece69 --- /dev/null +++ b/documentation/sys_model_config.md @@ -0,0 +1,220 @@ +System Model Configuration +== + +#### Introduction + +Given System Model Configurations, cadCAD produces system event datasets that conform to specified system metrics. Each +event / record is of [Enogenous State variables](link) produced by user defined [Partial State Updates](link) (PSU / +functions that update state); A sequence of event / record subsets that comprises the resulting system event dataset is +produced by a [Partial State Update Block](link) (PSUB / a Tensor Field for which State, Policy, and Time are dimensions +and PSU functions are values). + +A **System Model Configuration** is comprised of a simulation configuration, initial endogenous states, Partial State +Update Blocks, environmental process, and a user defined policy aggregation function. + +Execution: + +#### Simulation Properties + +###### System Metrics +The following system metrics determine the size of resulting system event datasets: +* `run` - the number of simulations in the resulting dataset +* `timestep` - the number of timestamps in the resulting dataset +* `substep` - the number of PSUs per `timestep` / within PSUBS +* Number of events / records: `run` x `timestep` x `substep` + +###### Simulation Configuration +For the following dictionary, `T` is assigned a `timestep` range, `N` is assigned the number of simulation runs, and +`params` is assigned the [**Parameter Sweep**](link) dictionary. + +```python +from cadCAD.configuration.utils import config_sim + +sim_config = config_sim({ + "N": 2, + "T": range(5), + "M": params, # Optional +}) +``` + +#### Initial Endogenous States +**Enogenous State variables** are read-only variables defined to capture the shape and property of the network and +represent internal input and signal. + +The PSUB tensor field is applied to the following states to produce a resulting system event +dataset. +```python +genesis_states = { + 's1': 0.0, + 's2': 0.0, + 's3': 1.0, + 'timestamp': '2018-10-01 15:16:24' +} +``` + +#### Partial State Update Block: +- ***Partial State Update Block(PSUB)*** ***(Define ?)*** Tensor Field for which State, Policy, Time are dimensions +and Partial State Update functions are values. +- ***Partial State Update (PSU)*** are user defined functions that encodes state updates and are executed in +a specified order PSUBs. PSUs update states given the most recent set of states and PSU policies. +- ***Mechanism*** ***(Define)*** + + +The PSUBs is a list of PSU dictionaries of the structure within the code block below. PSUB elements (PSU dictionaries) +are listed / defined in order of `substeps` and **identity functions** (returning a previous state's value) are assigned +to unreferenced states within PSUs. The number of records produced produced per `timestep` is the number of `substeps`. + +```python +partial_state_update_block = [ + { + "policies": { + "b1": p1_psu1, + "b2": p2_psu1 + }, + "variables": { + "s1": s1_psu1, + "s2": s2_psu1 + } + }, + { + "policies": { + "b1": p1_psu2, + }, + "variables": { + "s2": s2_psu2 + } + }, + {...} +] +``` +*Notes:* +1. An identity function (returning the previous state value) is assigned to `s1` in the second PSU. +2. Currently the only names that need not correspond to the convention below are `'b1'` and `'b2'`. + +#### Policies +- ***Policies*** ***(Define)*** When are policies behavior ? +- ***Behaviors*** 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. + +Policies accept parameter sweep variables [see link] `_g` (`dict`), the most recent +`substep` integer, the state history[see link] (`sH`), the most recent state record `s` (`dict) as inputs and returns a +set of actions (`dict`). + +Policy functions return dictionaries as actions. Policy functions provide access to parameter sweep variables [see link] +via dictionary `_g`. +```python +def p1_psu1(_g, substep, sH, s): + return {'policy1': 1} +def p2_psu1(_g, substep, sH, s): + return {'policy1': 1, 'policy2': 4} +``` +For each PSU, multiple policy dictionaries are aggregated into a single dictionary to be imputted into +all state functions using an initial reduction function (default: `lambda a, b: a + b`) and optional subsequent map +functions. +Example Result: `{'policy1': 2, 'policy2': 4}` + +#### State Updates +State update functions provide access to parameter sweep variables [see link] `_g` (`dict`), the most recent `substep` +integer, the state history[see link] (`sH`), the most recent state record as a dictionary (`s`), the policies of a +PSU (`_input`), and returns a tuple of the state variable's name and the resulting new value of the variable. + +```python +def state_update(_g, substep, sH, s, _input): + ... + return state, update +``` +**Note:** Each state update function updates one state variable at a time. Changes to multiple state variables requires +separate state update functions. A generic example of a PSU is as follows. + +* ##### Endogenous State Updates +They are only updated by PSUs and can be used as inputs to a PSUs. +```python +def s1_update(_g, substep, sH, s, _input): + x = _input['policy1'] + 1 + return 's1', x + +def s2_update(_g, substep, sH, s, _input): + x = _input['policy2'] + return 's2', x +``` + +* ##### Exogenous State Updates +***Exogenous State variables*** ***(Review)*** are read-only variables that represent external input and signal. They +update endogenous states and are only updated by environmental processes. Exgoneous variables can be used +as an input to a PSU that impacts state variables. ***(Expand upon Exogenous state updates)*** + +```python +from datetime import timedelta +from cadCAD.configuration.utils import time_step +def es3_update(_g, substep, sH, s, _input): + x = ... + return 's3' +def es4_update(_g, substep, sH, s, _input): + x = ... + return 's4', x +def update_timestamp(_g, substep, sH, s, _input): + x = time_step(dt_str=s[y], dt_format='%Y-%m-%d %H:%M:%S', _timedelta=timedelta(days=0, minutes=0, seconds=1)) + return 'timestamp', x +``` +Exogenous state update functions (`es3_update`, `es4_update` and `es5_update`) update once per timestamp and should be +included as a part of the first PSU in the PSUB. +```python +partial_state_update_block['psu1']['variables']['s3'] = es3_update +partial_state_update_block['psu1']['variables']['s4'] = es4_update +partial_state_update_block['psu1']['variables']['timestamp'] = update_timestamp +``` + +* #### Environmental Process +- ***Environmental processes*** model external changes that directly impact exogenous states at given specific +conditions such as market shocks at specific timestamps. + +Create a dictionary like `env_processes` below for which the keys are exogenous states and the values are lists of user +defined **Environment Update** functions to be composed (e.g. `[f(params, x), g(params, x)]` becomes +`f(params, g(params, x))`). + +Environment Updates accept the [**Parameter Sweep**](link) dictionary `params` and a state as a result of a PSU. +```python +def env_update(params, state): + . . . + return updated_state + +# OR + +env_update = lambda params, state: state + 5 +``` + +The `env_trigger` function is used to apply composed environment update functions to a list of specific exogenous state +update results. `env_trigger` accepts the total number of `substeps` for the simulation / `end_substep` and returns a +function accepting `trigger_field`, `trigger_vals`, and `funct_list`. + +In the following example functions are used to add `5` to every `s3` update and assign `10` to `s4` at +`timestamp`s `'2018-10-01 15:16:25'`, `'2018-10-01 15:16:27'`, and `'2018-10-01 15:16:29'`. +```python +from cadCAD.configuration.utils import env_trigger +trigger_timestamps = ['2018-10-01 15:16:25', '2018-10-01 15:16:27', '2018-10-01 15:16:29'] +env_processes = { + "s3": [lambda params, x: x + 5], + "s4": env_trigger(end_substep=3)( + trigger_field='timestamp', trigger_vals=trigger_timestamps, funct_list=[lambda params, x: 10] + ) +} +``` + +#### System Model Configuration +`append_configs`, stores a **System Model Configuration** to be (Executed)[url] as +simulations producing system event dataset(s) + +```python +from cadCAD.configuration import append_configs + +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] +) +``` + +#### [System Simulation Execution](link) diff --git a/simulations/regression_tests/config1.py b/simulations/regression_tests/config1.py index 9ee1979..a677f5e 100644 --- a/simulations/regression_tests/config1.py +++ b/simulations/regression_tests/config1.py @@ -9,7 +9,7 @@ seeds = { 'z': np.random.RandomState(1), 'a': np.random.RandomState(2), 'b': np.random.RandomState(3), - 'c': np.random.RandomState(3) + 'c': np.random.RandomState(4) } @@ -95,14 +95,15 @@ genesis_states = { # 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='timestep', trigger_vals=[1], funct_list=[lambda _g, x: 10]) + "s4": env_trigger(3)(trigger_field='timestamp', trigger_vals=trigger_timestamps, funct_list=[lambda _g, x: 10]) } -partial_state_update_blocks = { - "m1": { +partial_state_update_block = [ + { "policies": { "b1": p1m1, "b2": p2m1 @@ -115,7 +116,7 @@ partial_state_update_blocks = { "timestamp": update_timestamp } }, - "m2": { + { "policies": { "b1": p1m2, "b2": p2m2 @@ -127,7 +128,7 @@ partial_state_update_blocks = { # "s4": es4p2, } }, - "m3": { + { "policies": { "b1": p1m3, "b2": p2m3 @@ -139,7 +140,7 @@ partial_state_update_blocks = { # "s4": es4p2, } } -} +] sim_config = config_sim( @@ -153,6 +154,6 @@ append_configs( sim_configs=sim_config, initial_state=genesis_states, env_processes=env_processes, - partial_state_update_blocks=partial_state_update_blocks, + partial_state_update_blocks=partial_state_update_block, policy_ops=[lambda a, b: a + b] ) \ No newline at end of file diff --git a/simulations/regression_tests/config2.py b/simulations/regression_tests/config2.py index e120285..f8c4981 100644 --- a/simulations/regression_tests/config2.py +++ b/simulations/regression_tests/config2.py @@ -8,7 +8,7 @@ seeds = { 'z': np.random.RandomState(1), 'a': np.random.RandomState(2), 'b': np.random.RandomState(3), - 'c': np.random.RandomState(4) + 'c': np.random.RandomState(3) } @@ -89,9 +89,10 @@ genesis_states = { # 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='timestep', trigger_vals=[2], funct_list=[lambda _g, x: 10]) + "s4": env_trigger(3)(trigger_field='timestamp', trigger_vals=trigger_timestamps, funct_list=[lambda _g, x: 10]) } partial_state_update_block = { diff --git a/simulations/regression_tests/udo.py b/simulations/regression_tests/udo.py index 02647e7..618b86c 100644 --- a/simulations/regression_tests/udo.py +++ b/simulations/regression_tests/udo.py @@ -1,3 +1,5 @@ +from copy import deepcopy + import pandas as pd from fn.func import curried from datetime import timedelta @@ -26,6 +28,11 @@ class udoExample(object): 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']) @@ -106,7 +113,7 @@ def perceive(s, 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) + s['state_udo'].updateX().perceive(s).updateDS() x = udoPipe(s['state_udo']) return y, x for m in psu_steps: diff --git a/simulations/test_executions/config1_test.py b/simulations/test_executions/config1_test.py index 052c602..8807c02 100644 --- a/simulations/test_executions/config1_test.py +++ b/simulations/test_executions/config1_test.py @@ -1,4 +1,5 @@ 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 @@ -17,7 +18,8 @@ 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(raw_result) +print(tabulate(tensor_field[['m', 'b1', 's1', 's2']], headers='keys', tablefmt='psql')) print("Output:") print(tabulate(result, headers='keys', tablefmt='psql')) print() diff --git a/simulations/test_executions/historical_state_access_test.py b/simulations/test_executions/historical_state_access_test.py index 2b3b477..f0229bc 100644 --- a/simulations/test_executions/historical_state_access_test.py +++ b/simulations/test_executions/historical_state_access_test.py @@ -15,10 +15,10 @@ 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 = ['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')) diff --git a/simulations/test_executions/policy_agg_test.py b/simulations/test_executions/policy_agg_test.py index f1da13f..2d86f09 100644 --- a/simulations/test_executions/policy_agg_test.py +++ b/simulations/test_executions/policy_agg_test.py @@ -2,11 +2,9 @@ from pprint import pprint 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 policy_aggregation from cadCAD import configs -from testing.utils import generate_assertions exec_mode = ExecutionMode() diff --git a/simulations/test_executions/udo_test.py b/simulations/test_executions/udo_test.py index f4cf6d9..45c9d55 100644 --- a/simulations/test_executions/udo_test.py +++ b/simulations/test_executions/udo_test.py @@ -11,9 +11,9 @@ 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) +run = Executor(exec_context=single_proc_ctx, configs=configs) # cols = configs[0].initial_state.keys() cols = [ 'increment', @@ -29,6 +29,10 @@ result = pd.DataFrame(raw_result)[['run', 'substep', 'timestep'] + cols] # 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')) diff --git a/testing/generic_test.py b/testing/generic_test.py index 19b562c..796770b 100644 --- a/testing/generic_test.py +++ b/testing/generic_test.py @@ -2,7 +2,6 @@ import unittest from parameterized import parameterized from functools import reduce from tabulate import tabulate -from testing.utils import generate_assertions_df # ToDo: Exec Debug mode (*) for which state and policy updates are validated during runtime using `expected_results` # EXAMPLE: ('state_test' T/F, 'policy_test' T/F) @@ -14,26 +13,61 @@ from testing.utils import generate_assertions_df # ToDo: Use self.assertRaises(AssertionError) +def generate_assertions_df(df, expected_results, target_cols, evaluations): + # cols = ['run', 'timestep', 'substep'] + target_cols + # print(cols) + test_names = [] + for eval_f in evaluations: + def wrapped_eval(a, b): + try: + return eval_f(a, b) + except KeyError: + return True + + test_name = f"{eval_f.__name__}_test" + test_names.append(test_name) + df[test_name] = df.apply( + lambda x: wrapped_eval( + x.filter(items=target_cols).to_dict(), + expected_results[(x['run'], x['timestep'], x['substep'])] + ), + axis=1 + ) + + return df, test_names + + def make_generic_test(params): class TestSequence(unittest.TestCase): - @parameterized.expand(params) - def test_validate_results(self, name, result_df, expected_reults, target_cols): - # alt for (*) Exec Debug mode - tested_df = generate_assertions_df(result_df, expected_reults, target_cols) - erroneous = tested_df[(tested_df['test'] == False)] - if erroneous.empty is False: + + def generic_test(self, tested_df, expected_reults, test_name): + erroneous = tested_df[(tested_df[test_name] == False)] + # print(tabulate(tested_df, headers='keys', tablefmt='psql')) + + if erroneous.empty is False: # Or Entire df IS NOT erroneous for index, row in erroneous.iterrows(): expected = expected_reults[(row['run'], row['timestep'], row['substep'])] - unexpected = {k: expected[k] for k in expected if k in row and expected[k] != row[k]} + unexpected = {f"invalid_{k}": expected[k] for k in expected if k in row and expected[k] != row[k]} + for key in unexpected.keys(): - erroneous[f"invalid_{key}"] = unexpected[key] + erroneous[key] = None + erroneous.at[index, key] = unexpected[key] # etc. - print() - print(tabulate(erroneous, headers='keys', tablefmt='psql')) + # print() + # print(f"TEST: {test_name}") + # print(tabulate(erroneous, headers='keys', tablefmt='psql')) - self.assertTrue(reduce(lambda a, b: a and b, tested_df['test'])) + # ToDo: Condition that will change false to true + self.assertTrue(reduce(lambda a, b: a and b, tested_df[test_name])) - # def etc. + + @parameterized.expand(params) + def test_validation(self, name, result_df, expected_reults, target_cols, evaluations): + # alt for (*) Exec Debug mode + tested_df, test_names = generate_assertions_df(result_df, expected_reults, target_cols, evaluations) + + for test_name in test_names: + self.generic_test(tested_df, expected_reults, test_name) return TestSequence diff --git a/testing/system_models/__init__.py b/testing/system_models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/system_models/external_dataset.py b/testing/system_models/external_dataset.py new file mode 100644 index 0000000..0265288 --- /dev/null +++ b/testing/system_models/external_dataset.py @@ -0,0 +1,67 @@ +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}] +) diff --git a/testing/system_models/historical_state_access.py b/testing/system_models/historical_state_access.py index 839a826..8f88e85 100644 --- a/testing/system_models/historical_state_access.py +++ b/testing/system_models/historical_state_access.py @@ -92,6 +92,4 @@ append_configs( partial_state_update_blocks=partial_state_update_block ) -exec_mode = ExecutionMode() -single_proc_ctx = ExecutionContext(context=exec_mode.single_proc) -run = Executor(exec_context=single_proc_ctx, configs=configs) + diff --git a/testing/system_models/param_sweep.py b/testing/system_models/param_sweep.py new file mode 100644 index 0000000..fabb450 --- /dev/null +++ b/testing/system_models/param_sweep.py @@ -0,0 +1,110 @@ +import pprint +from typing import Dict, List + +from cadCAD.configuration import append_configs +from cadCAD.configuration.utils import env_trigger, var_substep_trigger, config_sim, psub_list + +pp = pprint.PrettyPrinter(indent=4) + +def some_function(x): + return x + +# Optional +# dict must contain lists opf 2 distinct lengths +g: Dict[str, List[int]] = { + 'alpha': [1], + 'beta': [2, some_function], + '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 = {} + + +# ['s1', 's2', 's3', 's4'] +# Policies per Mechanism +def gamma(_g, step, sL, s): + return {'gamma': _g['gamma']} + + +def omega(_g, step, sL, s): + return {'omega': _g['omega']} + + +# Internal States per Mechanism +def alpha(_g, step, sL, s, _input): + return 'alpha', _g['alpha'] + + +def beta(_g, step, sL, s, _input): + return 'beta', _g['beta'] + + +def policies(_g, step, sL, s, _input): + return 'policies', _input + + +def sweeped(_g, step, sL, s, _input): + return 'sweeped', {'beta': _g['beta'], 'gamma': _g['gamma']} + +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 + psu_block[m]["variables"]['beta'] = beta + psu_block[m]['variables']['policies'] = policies + psu_block[m]["variables"]['sweeped'] = var_timestep_trigger(y='sweeped', f=sweeped) + + +# 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 = { + 'alpha': 0, + 'beta': 0, + 'policies': {}, + 'sweeped': {} +} + +# Environment Process +# ToDo: Validate - make env proc trigger field agnostic +env_process['sweeped'] = 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 + } +) +# print() +# pp.pprint(g) +# print() +# pp.pprint(sim_config) + + +# New Convention +partial_state_update_blocks = psub_list(psu_block, psu_steps) +append_configs( + sim_configs=sim_config, + initial_state=genesis_states, + 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() diff --git a/testing/system_models/policy_aggregation.py b/testing/system_models/policy_aggregation.py index aae9234..e2be18b 100644 --- a/testing/system_models/policy_aggregation.py +++ b/testing/system_models/policy_aggregation.py @@ -1,7 +1,5 @@ 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 @@ -85,6 +83,4 @@ append_configs( policy_ops=[lambda a, b: a + b, lambda y: y * 2] # Default: lambda a, b: a + b ToDO: reduction function requires high lvl explanation ) -exec_mode = ExecutionMode() -single_proc_ctx = ExecutionContext(context=exec_mode.single_proc) -run = Executor(exec_context=single_proc_ctx, configs=configs) + diff --git a/testing/system_models/udo.py b/testing/system_models/udo.py new file mode 100644 index 0000000..1415908 --- /dev/null +++ b/testing/system_models/udo.py @@ -0,0 +1,185 @@ +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 + +from cadCAD.engine import ExecutionMode, ExecutionContext, Executor +from cadCAD import configs + + +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 + + +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) + 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() + + +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) diff --git a/testing/tests/external_test.py b/testing/tests/external_test.py new file mode 100644 index 0000000..1d86a3e --- /dev/null +++ b/testing/tests/external_test.py @@ -0,0 +1,127 @@ +import unittest +from pprint import pprint + +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 +from testing.generic_test import make_generic_test +from testing.utils import gen_metric_dict + +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(tabulate(result, headers='keys', tablefmt='psql')) + +# cols = ['run', 'substep', 'timestep', 'increment', 'external_data', 'policies'] +# result = result[cols] +# +# metrics = gen_metric_dict(result, ['increment', 'external_data', 'policies']) +# # +# pprint(metrics) + +def get_expected_results(run): + return { + (run, 0, 0): { + 'external_data': {'ds1': None, 'ds2': None, 'ds3': None}, + 'increment': 0, + 'policies': {'ds1': None, 'ds2': None, 'ds3': None} + }, + (run, 1, 1): { + 'external_data': {'ds1': 0, 'ds2': 0, 'ds3': 1}, + 'increment': 1, + 'policies': {'ds1': 0, 'ds2': 0, 'ds3': 1} + }, + (run, 1, 2): { + 'external_data': {'ds1': 1, 'ds2': 40, 'ds3': 5}, + 'increment': 2, + 'policies': {'ds1': 1, 'ds2': 40, 'ds3': 5} + }, + (run, 1, 3): { + 'external_data': {'ds1': 2, 'ds2': 40, 'ds3': 5}, + 'increment': 3, + 'policies': {'ds1': 2, 'ds2': 40, 'ds3': 5} + }, + (run, 2, 1): { + 'external_data': {'ds1': 3, 'ds2': 40, 'ds3': 5}, + 'increment': 4, + 'policies': {'ds1': 3, 'ds2': 40, 'ds3': 5} + }, + (run, 2, 2): { + 'external_data': {'ds1': 4, 'ds2': 40, 'ds3': 5}, + 'increment': 5, + 'policies': {'ds1': 4, 'ds2': 40, 'ds3': 5} + }, + (run, 2, 3): { + 'external_data': {'ds1': 5, 'ds2': 40, 'ds3': 5}, + 'increment': 6, + 'policies': {'ds1': 5, 'ds2': 40, 'ds3': 5} + }, + (run, 3, 1): { + 'external_data': {'ds1': 6, 'ds2': 40, 'ds3': 5}, + 'increment': 7, + 'policies': {'ds1': 6, 'ds2': 40, 'ds3': 5} + }, + (run, 3, 2): { + 'external_data': {'ds1': 7, 'ds2': 40, 'ds3': 5}, + 'increment': 8, + 'policies': {'ds1': 7, 'ds2': 40, 'ds3': 5} + }, + (run, 3, 3): { + 'external_data': {'ds1': 8, 'ds2': 40, 'ds3': 5}, + 'increment': 9, + 'policies': {'ds1': 8, 'ds2': 40, 'ds3': 5} + }, + (run, 4, 1): { + 'external_data': {'ds1': 9, 'ds2': 40, 'ds3': 5}, + 'increment': 10, + 'policies': {'ds1': 9, 'ds2': 40, 'ds3': 5} + }, + (run, 4, 2): { + 'external_data': {'ds1': 10, 'ds2': 40, 'ds3': 5}, + 'increment': 11, + 'policies': {'ds1': 10, 'ds2': 40, 'ds3': 5} + }, + (run, 4, 3): { + 'external_data': {'ds1': 11, 'ds2': 40, 'ds3': 5}, + 'increment': 12, + 'policies': {'ds1': 11, 'ds2': 40, 'ds3': 5} + } + } + + +expected_results = {} +expected_results_1 = get_expected_results(1) +expected_results_2 = get_expected_results(2) +expected_results.update(expected_results_1) +expected_results.update(expected_results_2) + + +def row(a, b): + return a == b +params = [["external_dataset", result, expected_results, ['increment', 'external_data', 'policies'], [row]]] + + +class GenericTest(make_generic_test(params)): + pass + + +if __name__ == '__main__': + unittest.main() + +# print() +# print("Tensor Field: config1") +# print(tabulate(tensor_field, headers='keys', tablefmt='psql')) +# print("Output:") +# print(tabulate(result, headers='keys', tablefmt='psql')) +# print() diff --git a/testing/tests/historical_state_access.py b/testing/tests/historical_state_access.py index 4ab145f..ffc2d95 100644 --- a/testing/tests/historical_state_access.py +++ b/testing/tests/historical_state_access.py @@ -1,14 +1,20 @@ import unittest import pandas as pd -from tabulate import tabulate + +from cadCAD.engine import ExecutionMode, ExecutionContext, Executor from testing.generic_test import make_generic_test -from testing.system_models.historical_state_access import run -from testing.utils import generate_assertions_df +from testing.system_models import historical_state_access +from cadCAD import configs + + +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) - +# ToDo: Discrepance not reported fot collection values. Needs custom test for collection values expected_results = { (1, 0, 0): {'x': 0, 'nonexsistant': [], 'last_x': [], '2nd_to_last_x': [], '3rd_to_last_x': [], '4th_to_last_x': []}, (1, 1, 1): {'x': 1, @@ -31,49 +37,85 @@ expected_results = { '4th_to_last_x': []}, (1, 2, 1): {'x': 4, 'nonexsistant': [], - 'last_x': [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}], - '2nd_to_last_x': [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}], + 'last_x': [ + {'x': 4, 'run': 1, 'substep': 1, 'timestep': 1}, # x: 1 + {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, + {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1} + ], + '2nd_to_last_x': [{'x': -1, 'run': 1, 'substep': 0, 'timestep': 0}], # x: 0 '3rd_to_last_x': [], '4th_to_last_x': []}, (1, 2, 2): {'x': 5, 'nonexsistant': [], - 'last_x': [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}], + 'last_x': [ + {'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, + {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, + {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1} + ], '2nd_to_last_x': [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}], '3rd_to_last_x': [], '4th_to_last_x': []}, (1, 2, 3): {'x': 6, 'nonexsistant': [], - 'last_x': [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}], + 'last_x': [ + {'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, + {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, + {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1} + ], '2nd_to_last_x': [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}], '3rd_to_last_x': [], '4th_to_last_x': []}, (1, 3, 1): {'x': 7, 'nonexsistant': [], - 'last_x': [{'x': 4, 'run': 1, 'substep': 1, 'timestep': 2}, {'x': 5, 'run': 1, 'substep': 2, 'timestep': 2}, {'x': 6, 'run': 1, 'substep': 3, 'timestep': 2}], - '2nd_to_last_x': [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}], + 'last_x': [ + {'x': 4, 'run': 1, 'substep': 1, 'timestep': 2}, + {'x': 5, 'run': 1, 'substep': 2, 'timestep': 2}, + {'x': 6, 'run': 1, 'substep': 3, 'timestep': 2} + ], + '2nd_to_last_x': [ + {'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, + {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, + {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1} + ], '3rd_to_last_x': [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}], '4th_to_last_x': []}, (1, 3, 2): {'x': 8, 'nonexsistant': [], - 'last_x': [{'x': 4, 'run': 1, 'substep': 1, 'timestep': 2}, {'x': 5, 'run': 1, 'substep': 2, 'timestep': 2}, {'x': 6, 'run': 1, 'substep': 3, 'timestep': 2}], - '2nd_to_last_x': [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}], + 'last_x': [ + {'x': 4, 'run': 1, 'substep': 1, 'timestep': 2}, + {'x': 5, 'run': 1, 'substep': 2, 'timestep': 2}, + {'x': 6, 'run': 1, 'substep': 3, 'timestep': 2} + ], + '2nd_to_last_x': [ + {'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, + {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, + {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1} + ], '3rd_to_last_x': [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}], '4th_to_last_x': []}, (1, 3, 3): {'x': 9, 'nonexsistant': [], - 'last_x': [{'x': 4, 'run': 1, 'substep': 1, 'timestep': 2}, {'x': 5, 'run': 1, 'substep': 2, 'timestep': 2}, {'x': 6, 'run': 1, 'substep': 3, 'timestep': 2}], - '2nd_to_last_x': [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}], + 'last_x': [ + {'x': 4, 'run': 1, 'substep': 1, 'timestep': 2}, + {'x': 5, 'run': 1, 'substep': 2, 'timestep': 2}, + {'x': 6, 'run': 1, 'substep': 3, 'timestep': 2} + ], + '2nd_to_last_x': [ + {'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, + {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, + {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1} + ], '3rd_to_last_x': [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}], '4th_to_last_x': []} } -params = [["historical_state_access", result, expected_results, - ['x', 'nonexsistant', 'last_x', '2nd_to_last_x', '3rd_to_last_x', '4th_to_last_x']] - ] -# df = generate_assertions_df(result, expected_results, -# ['x', 'nonexsistant', 'last_x', '2nd_to_last_x', '3rd_to_last_x', '4th_to_last_x'] -# ) -# print(tabulate(df, headers='keys', tablefmt='psql')) + +def row(a, b): + return a == b +params = [ + ["historical_state_access", result, expected_results, + ['x', 'nonexsistant', 'last_x', '2nd_to_last_x', '3rd_to_last_x', '4th_to_last_x'], [row]] + ] class GenericTest(make_generic_test(params)): diff --git a/testing/tests/multi_config_test.py b/testing/tests/multi_config_test.py new file mode 100644 index 0000000..c668773 --- /dev/null +++ b/testing/tests/multi_config_test.py @@ -0,0 +1,56 @@ +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 +from testing.utils import gen_metric_dict + +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) + + +def get_expected_results_1(run): + return { + (run, 0, 0): {'s1': 0, 's2': 0.0, 's3': 5}, + (run, 1, 1): {'s1': 1, 's2': 4, 's3': 5}, + (run, 1, 2): {'s1': 2, 's2': 6, 's3': 5}, + (run, 1, 3): {'s1': 3, 's2': [30, 300], 's3': 5}, + (run, 2, 1): {'s1': 4, 's2': 4, 's3': 5}, + (run, 2, 2): {'s1': 5, 's2': 6, 's3': 5}, + (run, 2, 3): {'s1': 6, 's2': [30, 300], 's3': 5}, + (run, 3, 1): {'s1': 7, 's2': 4, 's3': 5}, + (run, 3, 2): {'s1': 8, 's2': 6, 's3': 5}, + (run, 3, 3): {'s1': 9, 's2': [30, 300], 's3': 5}, + (run, 4, 1): {'s1': 10, 's2': 4, 's3': 5}, + (run, 4, 2): {'s1': 11, 's2': 6, 's3': 5}, + (run, 4, 3): {'s1': 12, 's2': [30, 300], 's3': 5}, + (run, 5, 1): {'s1': 13, 's2': 4, 's3': 5}, + (run, 5, 2): {'s1': 14, 's2': 6, 's3': 5}, + (run, 5, 3): {'s1': 15, 's2': [30, 300], 's3': 5}, + } + +expected_results_1 = {} +expected_results_A = get_expected_results_1(1) +expected_results_B = get_expected_results_1(2) +expected_results_1.update(expected_results_A) +expected_results_1.update(expected_results_B) + +expected_results_2 = {} + +# 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() + print(gen_metric_dict) + i += 1 diff --git a/testing/tests/param_sweep.py b/testing/tests/param_sweep.py new file mode 100644 index 0000000..a87dab3 --- /dev/null +++ b/testing/tests/param_sweep.py @@ -0,0 +1,85 @@ +import unittest +import pandas as pd + + +from cadCAD.engine import ExecutionMode, ExecutionContext, Executor +from testing.system_models import param_sweep +from cadCAD import configs + +from testing.generic_test import make_generic_test +from testing.system_models.param_sweep import some_function + + +exec_mode = ExecutionMode() +multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc) +run = Executor(exec_context=multi_proc_ctx, configs=configs) + + +def get_expected_results(run, beta, gamma): + return { + (run, 0, 0): {'policies': {}, 'sweeped': {}, 'alpha': 0, 'beta': 0}, + (run, 1, 1): {'policies': {'gamma': gamma, 'omega': 7}, 'sweeped': {'beta': beta, 'gamma': gamma}, 'alpha': 1, 'beta': beta}, + (run, 1, 2): {'policies': {'gamma': gamma, 'omega': 7}, 'sweeped': {'beta': beta, 'gamma': gamma}, 'alpha': 1, 'beta': beta}, + (run, 1, 3): {'policies': {'gamma': gamma, 'omega': 7}, 'sweeped': {'beta': beta, 'gamma': gamma}, 'alpha': 1, 'beta': beta}, + (run, 2, 1): {'policies': {'gamma': gamma, 'omega': 7}, 'sweeped': {'beta': beta, 'gamma': gamma}, 'alpha': 1, 'beta': beta}, + (run, 2, 2): {'policies': {'gamma': gamma, 'omega': 7}, 'sweeped': {'beta': beta, 'gamma': gamma}, 'alpha': 1, 'beta': beta}, + (run, 2, 3): {'policies': {'gamma': gamma, 'omega': 7}, 'sweeped': {'beta': beta, 'gamma': gamma}, 'alpha': 1, 'beta': beta}, + (run, 3, 1): {'policies': {'gamma': gamma, 'omega': 7}, 'sweeped': {'beta': beta, 'gamma': gamma}, 'alpha': 1, 'beta': beta}, + (run, 3, 2): {'policies': {'gamma': gamma, 'omega': 7}, 'sweeped': {'beta': beta, 'gamma': gamma}, 'alpha': 1, 'beta': beta}, + (run, 3, 3): {'policies': {'gamma': gamma, 'omega': 7}, 'sweeped': {'beta': beta, 'gamma': gamma}, 'alpha': 1, 'beta': beta}, + (run, 4, 1): {'policies': {'gamma': gamma, 'omega': 7}, 'sweeped': {'beta': beta, 'gamma': gamma}, 'alpha': 1, 'beta': beta}, + (run, 4, 2): {'policies': {'gamma': gamma, 'omega': 7}, 'sweeped': {'beta': beta, 'gamma': gamma}, 'alpha': 1, 'beta': beta}, + (run, 4, 3): {'policies': {'gamma': gamma, 'omega': 7}, 'sweeped': {'beta': beta, 'gamma': gamma}, 'alpha': 1, 'beta': beta}, + (run, 5, 1): {'policies': {'gamma': gamma, 'omega': 7}, 'sweeped': beta, 'alpha': 1, 'beta': beta}, + (run, 5, 2): {'policies': {'gamma': gamma, 'omega': 7}, 'sweeped': beta, 'alpha': 1, 'beta': beta}, + (run, 5, 3): {'policies': {'gamma': gamma, 'omega': 7}, 'sweeped': beta, 'alpha': 1, 'beta': beta} + } + + +expected_results_1 = {} +expected_results_1a = get_expected_results(1, 2, 3) +expected_results_1b = get_expected_results(2, 2, 3) +expected_results_1.update(expected_results_1a) +expected_results_1.update(expected_results_1b) + +expected_results_2 = {} +expected_results_2a = get_expected_results(1, some_function, 4) +expected_results_2b = get_expected_results(2, some_function, 4) +expected_results_2.update(expected_results_2a) +expected_results_2.update(expected_results_2b) + + +i = 0 +expected_results = [expected_results_1, expected_results_2] +config_names = ['sweep_config_A', 'sweep_config_B'] + +def row(a, b): + return a == b +def create_test_params(feature, fields): + i = 0 + for raw_result, _ in run.execute(): + yield [feature, pd.DataFrame(raw_result), expected_results[i], fields, [row]] + i += 1 + + +params = list(create_test_params("param_sweep", ['alpha', 'beta', 'policies', 'sweeped'])) + + +class GenericTest(make_generic_test(params)): + pass + + +if __name__ == '__main__': + unittest.main() + +# 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 \ No newline at end of file diff --git a/testing/tests/policy_aggregation.py b/testing/tests/policy_aggregation.py index 4be0da4..657b6e6 100644 --- a/testing/tests/policy_aggregation.py +++ b/testing/tests/policy_aggregation.py @@ -1,14 +1,21 @@ import unittest import pandas as pd + +from cadCAD.engine import ExecutionMode, ExecutionContext, Executor from testing.generic_test import make_generic_test -from testing.system_models.policy_aggregation import run +from testing.system_models import policy_aggregation +from cadCAD import configs + +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) expected_results = { (1, 0, 0): {'policies': {}, 's1': 0}, - (1, 1, 1): {'policies': {'policy1': 2, 'policy2': 4}, 's1': 500}, + (1, 1, 1): {'policies': {'policy1': 1, 'policy2': 4}, 's1': 1}, # 'policy1': 2 (1, 1, 2): {'policies': {'policy1': 8, 'policy2': 8}, 's1': 2}, (1, 1, 3): {'policies': {'policy1': 4, 'policy2': 8, 'policy3': 12}, 's1': 3}, (1, 2, 1): {'policies': {'policy1': 2, 'policy2': 4}, 's1': 4}, @@ -19,14 +26,14 @@ expected_results = { (1, 3, 3): {'policies': {'policy1': 4, 'policy2': 8, 'policy3': 12}, 's1': 9} } -params = [["policy_aggregation", result, expected_results, ['policies', 's1']]] -# df = generate_assertions_df(result, expected_results, ['policies', 's1']) -# print(tabulate(df, headers='keys', tablefmt='psql')) +def row(a, b): + return a == b +params = [["policy_aggregation", result, expected_results, ['policies', 's1'], [row]]] class GenericTest(make_generic_test(params)): pass - if __name__ == '__main__': unittest.main() + diff --git a/testing/tests/udo.py b/testing/tests/udo.py new file mode 100644 index 0000000..ea4b42a --- /dev/null +++ b/testing/tests/udo.py @@ -0,0 +1,39 @@ +import unittest +import ctypes +from copy import deepcopy +from pprint import pprint + +import pandas as pd +from tabulate import tabulate + +from testing.generic_test import make_generic_test +from testing.system_models.udo import run +from testing.utils import generate_assertions_df, gen_metric_dict + +raw_result, tensor_field = run.execute() +result = pd.DataFrame(raw_result) + +cols = ['increment', 'state_udo', 'state_udo_perception_tracker', + 'state_udo_tracker', 'timestamp', 'udo_policies', 'udo_policy_tracker'] + + +# print(list(result.columns) +# ctypes.cast(id(a), ctypes.py_object).value +# pprint(gen_metric_dict(result, cols)) +d = gen_metric_dict(result, cols) +pprint(d) + +# for k1, v1 in d: +# print(v1) +# d_copy = deepcopy(d) +# for k, v in d_copy.items(): +# # print(d[k]['state_udo']) # = +# print(ctypes.cast(id(v['state_udo']['mem_id']), ctypes.py_object).value) + + +# pprint(d_copy) + +# df = generate_assertions_df(result, d, cols) +# +# print(tabulate(df, headers='keys', tablefmt='psql')) +# \ No newline at end of file diff --git a/testing/utils.py b/testing/utils.py index 62dc439..0fff73d 100644 --- a/testing/utils.py +++ b/testing/utils.py @@ -1,28 +1,21 @@ -def gen_metric_row(row): - return ((row['run'], row['timestep'], row['substep']), {'s1': row['s1'], 'policies': row['policies']}) +# +# def record_generator(row, cols): +# return {col: row[col] for col in cols} -def gen_metric_row(row): - return { - 'run': row['run'], - 'timestep': row['timestep'], - 'substep': row['substep'], - 's1': row['s1'], - 'policies': row['policies'] - } +def gen_metric_row(row, cols): + return ((row['run'], row['timestep'], row['substep']), {col: row[col] for col in cols}) -def gen_metric_dict(df): - return [gen_metric_row(row) for index, row in df.iterrows()] +# def gen_metric_row(row): +# return ((row['run'], row['timestep'], row['substep']), {'s1': row['s1'], 'policies': row['policies']}) -def generate_assertions_df(df, expected_results, target_cols): - def df_filter(run, timestep, substep): - return df[ - (df['run'] == run) & (df['timestep'] == timestep) & (df['substep'] == substep) - ][target_cols].to_dict(orient='records')[0] +# def gen_metric_row(row): +# return { +# 'run': row['run'], +# 'timestep': row['timestep'], +# 'substep': row['substep'], +# 's1': row['s1'], +# 'policies': row['policies'] +# } - df['test'] = df.apply( - lambda x: \ - df_filter(x['run'], x['timestep'], x['substep']) == expected_results[(x['run'], x['timestep'], x['substep'])] - , axis=1 - ) - - return df \ No newline at end of file +def gen_metric_dict(df, cols): + return dict([gen_metric_row(row, cols) for index, row in df.iterrows()]) From 715e6f9a745f8eea4a71c2e46a63cb461d332d09 Mon Sep 17 00:00:00 2001 From: "Joshua E. Jodesty" Date: Tue, 30 Jul 2019 11:17:49 -0400 Subject: [PATCH 3/9] pre refactor upload --- cadCAD/engine/simulation.py | 13 +- simulations/regression_tests/config1.py | 3 +- simulations/validation/exo_example.ipynb | 763 +++++++++++++++++++++++ 3 files changed, 775 insertions(+), 4 deletions(-) create mode 100644 simulations/validation/exo_example.ipynb diff --git a/cadCAD/engine/simulation.py b/cadCAD/engine/simulation.py index 1823a34..a288658 100644 --- a/cadCAD/engine/simulation.py +++ b/cadCAD/engine/simulation.py @@ -1,3 +1,4 @@ +from pprint import pprint from typing import Any, Callable, Dict, List, Tuple from pathos.pools import ThreadPool as TPool from copy import deepcopy @@ -113,7 +114,9 @@ class Executor: ) -> 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)) + _input: Dict[str, Any] = self.policy_update_exception( + self.get_policy_input(sweep_dict, sub_step, sH, last_in_obj, policy_funcs) + ) # ToDo: add env_proc generator to `last_in_copy` iterator as wrapper function @@ -211,6 +214,9 @@ class Executor: time_step += 1 + pprint(states_list) + print() + return states_list # state_update_pipeline @@ -260,7 +266,9 @@ class Executor: 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) + 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 @@ -271,5 +279,4 @@ class Executor: list(range(runs)) ) ) - return pipe_run diff --git a/simulations/regression_tests/config1.py b/simulations/regression_tests/config1.py index a677f5e..a0eb078 100644 --- a/simulations/regression_tests/config1.py +++ b/simulations/regression_tests/config1.py @@ -145,7 +145,8 @@ partial_state_update_block = [ sim_config = config_sim( { - "N": 2, + "N": 1, + # "N": 5, "T": range(5), } ) diff --git a/simulations/validation/exo_example.ipynb b/simulations/validation/exo_example.ipynb new file mode 100644 index 0000000..f1588c7 --- /dev/null +++ b/simulations/validation/exo_example.ipynb @@ -0,0 +1,763 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Exogenous Example\n", + "## Authored by BlockScience, MV Barlin\n", + "### Updated July-10-2019 \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Key assumptions and space:\n", + "1. Implementation of System Model in cell 2\n", + "2. Timestep = day\n", + "3. Launch simulation, without intervention from changing governance policies" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Library Imports" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from IPython.display import Image\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", + "import math\n", + "#from tabulate import tabulate\n", + "from scipy import stats\n", + "sns.set_style('whitegrid')\n", + "from decimal import Decimal\n", + "from datetime import timedelta\n", + "\n", + "%matplotlib inline" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## cadCAD Setup\n", + "#### ----------------cadCAD LIBRARY IMPORTS------------------------" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "from cadCAD.engine import ExecutionMode, ExecutionContext, Executor\n", + "#from simulations.validation import sweep_config\n", + "from cadCAD import configs\n", + "from cadCAD.configuration import append_configs\n", + "from cadCAD.configuration.utils import proc_trigger, ep_time_step, config_sim" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "#from cadCAD.configuration.utils.parameterSweep import config_sim" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "from typing import Dict, List" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### ----------------Random State Seed-----------------------------" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "seed = {\n", + "# 'z': np.random.RandomState(1)\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Timestamp" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "ts_format = '%Y-%m-%d %H:%M:%S'\n", + "t_delta = timedelta(days=0, minutes=0, seconds=1)\n", + "def set_time(_g, step, sL, s, _input):\n", + " y = 'timestamp'\n", + " x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta)\n", + " return (y, x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# ASSUMED PARAMETERS" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### PRICE LIST" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "# dai_xns_conversion = 1.0 # Assumed for static conversion 'PUBLISHED PRICE LIST' DEPRECATED" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Initial Condition State Variables" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "del_stake_pct = 2\n", + "\n", + "starting_xns = float(10**10) # initial supply of xns tokens\n", + "starting_broker_xns = float(1 * 10**8) # inital holding of xns token by broker app\n", + "starting_broker_fiat = float(1 * 10**5) # inital holding of xns token by broker app\n", + "starting_broker_stable = float(1 * 10**6) # inital holding of stable token by broker app\n", + "starting_deposit_acct = float(100) # inital deposit locked for first month of resources TBD: make function of resource*price\n", + "starting_entrance = float(1 * 10**4) # TBD: make function of entrance fee % * cost * # of initial apps\n", + "starting_app_usage = float(10) # initial fees from app usage \n", + "starting_platform = float(100) # initial platform fees \n", + "starting_resource_fees = float(10) # initial resource fees usage paid by apps \n", + "starting_app_subsidy = float(0.25* 10**9) # initial application subsidy pool\n", + "starting_stake = float(4 * 10**7)\n", + "starting_stake_pool = starting_stake + ((3*10**7)*(del_stake_pct)) # initial staked pool + ((3*10**7)*(del_stake_pct))\n", + "\n", + "#starting_block_reward = float(0) # initial block reward MOVED ABOVE TO POLICY\n", + "starting_capacity_subsidy = float(7.5 * 10**7) # initial capacity subsidy pool\n", + "starting_delegate_holdings = 0.15 * starting_xns\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Initial Condition Composite State Variables" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "# subsidy limit is 30% of the 10B supply\n", + "starting_treasury = float(5.5 * 10**9) \n", + "starting_app_income = float(0) # initial income to application\n", + "starting_resource_income = float(0) # initial income to application\n", + "starting_delegate_income = float(0) # initial income to delegate" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Initial Condition Exogoneous State Variables " + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "starting_xns_fiat = float(0.01) # initial xns per fiat signal\n", + "starting_fiat_ext = float(1) # initial xns per fiat signal\n", + "starting_stable_ext = float(1) # initial stable signal" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Exogenous Price Updates" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "def delta_price(mean,sd):\n", + " '''Returns normal random variable generated by first two central moments of price change of input ticker'''\n", + " rv = np.random.normal(mean, sd)\n", + " return rv" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "def xns_ext_update(_g, step, sL, s, _input):\n", + " key = 'XNS_fiat_external'\n", + " \n", + " value = s['XNS_fiat_external'] * (1 + delta_price(0.000000, 0.005))\n", + " \n", + " return key, value" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "From Currency Analysis of DAI-USD pair \n", + "May-09-2018 through June-10-2019 \n", + "Datasource: BitFinex \n", + "Analysis of daily return percentage performed by BlockScience" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "DAI_mean = 0.0000719\n", + "DAI_sd = 0.006716" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The daily return is computed as: \n", + "$$ r = \\frac{Price_n - Price_{n-1}}{Price_{n-1}} $$ \n", + "Thus, the modelled current price can be as: \n", + "$$ Price_n = Price_{n-1} * r + Price_{n-1} $$" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "def stable_update(_g, step, sL, s, _input):\n", + " key = 'stable_external'\n", + " \n", + " value = s['stable_external'] * (1 + delta_price(DAI_mean, DAI_sd))\n", + " return key, value\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Assumed Parameters" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "apps_deployed = 1 # Make part of test- application deployment model\n", + "\n", + "starting_deposit_acct = float(100) # inital deposit locked for first month of resources TBD: make function of resource*price\n", + "\n", + "app_resource_fee_constant = 10**1 # in STABLE, assumed per day per total nodes \n", + "platform_fee_constant = 10 # in XNS\n", + "# ^^^^^^^^^^^^ MAKE A PERCENTAGE OR FLAT FEE as PART of TESTING" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1000" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "alpha = 100 # Fee Rate\n", + "beta = 0.10 # FIXED Too high because multiplied by constant and resource fees\n", + "app_platform = alpha * platform_fee_constant\n", + "app_platform" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10.0" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "beta_out =beta*100\n", + "beta_out" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.15" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "starting_capacity_subsidy / (5 * 10**7) / 10" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "weight = 0.95 # 0.95 internal weight 5% friction from external markets\n", + "\n", + "def xns_int_update(_g, step, sL, s, _input):\n", + " key = 'XNS_fiat_internal'\n", + "\n", + " internal = s['XNS_fiat_internal'] * weight\n", + " external = s['XNS_fiat_external'] * (1 - weight)\n", + " value = internal + external\n", + " \n", + " return key, value" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### CONFIGURATION DICTIONARY" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "time_step_count = 3652 # days = 10 years\n", + "run_count = 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Genesis States" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "#----------STATE VARIABLE Genesis DICTIONARY---------------------------\n", + "genesis_states = {\n", + " 'XNS_fiat_external' : starting_xns_fiat,\n", + " 'XNS_fiat_internal' : starting_xns_fiat,\n", + " # 'fiat_external' : starting_fiat_ext,\n", + " 'stable_external' : starting_stable_ext,\n", + " 'timestamp': '2018-10-01 15:16:24', #es5\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "#--------------EXOGENOUS STATE MECHANISM DICTIONARY--------------------\n", + "exogenous_states = {\n", + " 'XNS_fiat_external' : xns_ext_update,\n", + "# 'fiat_external' : starting_fiat_ext,\n", + " 'stable_external' : stable_update,\n", + " \"timestamp\": set_time,\n", + " }\n", + "\n", + "#--------------ENVIRONMENTAL PROCESS DICTIONARY------------------------\n", + "env_processes = {\n", + "# \"Poisson\": env_proc_id\n", + "}\n", + "#----------------------SIMULATION RUN SETUP----------------------------\n", + "sim_config = config_sim(\n", + " {\n", + " \"N\": run_count,\n", + " \"T\": range(time_step_count)\n", + "# \"M\": g # for parameter sweep\n", + "}\n", + ")\n", + "#----------------------MECHANISM AND BEHAVIOR DICTIONARY---------------\n", + "partial_state_update_block = {\n", + " \"price\": { \n", + " \"policies\": { \n", + " },\n", + " \"variables\": {\n", + " 'XNS_fiat_internal' : xns_int_update\n", + "# 'app_income' : app_earn,\n", + " }\n", + " },\n", + "}\n", + "\n", + "append_configs(\n", + " sim_configs=sim_config,\n", + " initial_state=genesis_states,\n", + " seeds=seed,\n", + " raw_exogenous_states= exogenous_states,\n", + " env_processes=env_processes,\n", + " partial_state_update_blocks=partial_state_update_block\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Running cadCAD" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Simulation Execution: Single Configuration\n", + "\n", + "single_proc: []\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\mbarl\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\cadCAD\\utils\\__init__.py:89: FutureWarning: The use of a dictionary to describe Partial State Update Blocks will be deprecated. Use a list instead.\n", + " FutureWarning)\n" + ] + } + ], + "source": [ + "exec_mode = ExecutionMode()\n", + "\n", + "print(\"Simulation Execution: Single Configuration\")\n", + "print()\n", + "first_config = configs # only contains config1\n", + "single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)\n", + "run1 = Executor(exec_context=single_proc_ctx, configs=first_config)\n", + "run1_raw_result, tensor_field = run1.main()\n", + "result = pd.DataFrame(run1_raw_result)\n", + "# print()\n", + "# print(\"Tensor Field: config1\")\n", + "# print(tabulate(tensor_field, headers='keys', tablefmt='psql'))\n", + "# print(\"Output:\")\n", + "# print(tabulate(result, headers='keys', tablefmt='psql'))\n", + "# print()" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "df = result" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
XNS_fiat_externalXNS_fiat_internalrunstable_externalsubsteptimestamptimestep
00.0100000.01000011.00000002018-10-01 15:16:240
10.0099440.01000011.00017212018-10-01 15:16:251
20.0098890.00999711.00351612018-10-01 15:16:262
30.0098480.00999210.99065512018-10-01 15:16:273
40.0098140.00998511.00134612018-10-01 15:16:284
50.0097980.00997611.00249512018-10-01 15:16:295
60.0097060.00996710.99491112018-10-01 15:16:306
70.0096250.00995410.99891912018-10-01 15:16:317
80.0096320.00993810.99504712018-10-01 15:16:328
90.0096480.00992210.98078612018-10-01 15:16:339
\n", + "
" + ], + "text/plain": [ + " XNS_fiat_external XNS_fiat_internal run stable_external substep \\\n", + "0 0.010000 0.010000 1 1.000000 0 \n", + "1 0.009944 0.010000 1 1.000172 1 \n", + "2 0.009889 0.009997 1 1.003516 1 \n", + "3 0.009848 0.009992 1 0.990655 1 \n", + "4 0.009814 0.009985 1 1.001346 1 \n", + "5 0.009798 0.009976 1 1.002495 1 \n", + "6 0.009706 0.009967 1 0.994911 1 \n", + "7 0.009625 0.009954 1 0.998919 1 \n", + "8 0.009632 0.009938 1 0.995047 1 \n", + "9 0.009648 0.009922 1 0.980786 1 \n", + "\n", + " timestamp timestep \n", + "0 2018-10-01 15:16:24 0 \n", + "1 2018-10-01 15:16:25 1 \n", + "2 2018-10-01 15:16:26 2 \n", + "3 2018-10-01 15:16:27 3 \n", + "4 2018-10-01 15:16:28 4 \n", + "5 2018-10-01 15:16:29 5 \n", + "6 2018-10-01 15:16:30 6 \n", + "7 2018-10-01 15:16:31 7 \n", + "8 2018-10-01 15:16:32 8 \n", + "9 2018-10-01 15:16:33 9 " + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.head(10)" + ] + }, + { + "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 +} From 176593ae0f9565de2f5c32e5740c3c0c1eb67384 Mon Sep 17 00:00:00 2001 From: "Joshua E. Jodesty" Date: Tue, 30 Jul 2019 12:41:13 -0400 Subject: [PATCH 4/9] included execution in readme --- README.md | 109 ++++----- cadCAD/engine/simulation.py | 35 --- documentation/Execution.md | 160 +++++++++++++ ...access.md => Historically_State_Access.md} | 40 ++-- .../{policy_agg.md => Policy_Aggregation.md} | 12 +- documentation/Simulation_Configuration.md | 201 ++++++++++++++++ ...eep.md => System_Model_Parameter_Sweep.md} | 15 +- .../examples/historical_state_access.py | 4 +- documentation/examples/param_sweep.py | 18 +- documentation/examples/policy_aggregation.py | 16 +- documentation/examples/sys_model_A.py | 36 +-- documentation/examples/sys_model_A_exec.py | 2 +- documentation/examples/sys_model_B.py | 34 +-- documentation/examples/sys_model_B_exec.py | 4 +- documentation/execution.md | 71 ------ documentation/sys_model_config.md | 220 ------------------ 16 files changed, 508 insertions(+), 469 deletions(-) create mode 100644 documentation/Execution.md rename documentation/{historical_state_access.md => Historically_State_Access.md} (75%) rename documentation/{policy_agg.md => Policy_Aggregation.md} (86%) create mode 100644 documentation/Simulation_Configuration.md rename documentation/{param_sweep.md => System_Model_Parameter_Sweep.md} (84%) delete mode 100644 documentation/execution.md delete mode 100644 documentation/sys_model_config.md diff --git a/README.md b/README.md index f919ca9..129a577 100644 --- a/README.md +++ b/README.md @@ -59,90 +59,95 @@ Examples: **3. Import cadCAD & Run Simulations:** -Examples: `/simulations/*.py` or `/simulations/*.ipynb` -Single Simulation: `/simulations/single_config_run.py` -```python -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() -``` - -Parameter Sweep Simulation (Concurrent): `/simulations/param_sweep_run.py` +##### Single Process Execution: +Example [System Model Configurations](link): +* [System Model A](link): `/documentation/examples/sys_model_A.py` +* [System Model B](link): `/documentation/examples/sys_model_B.py` +Execution Examples: +* [System Model A](link): `/documentation/examples/sys_model_A_exec.py` +* [System Model B](link): `/documentation/examples/sys_model_B_exec.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 documentation.examples import sys_model_A from cadCAD import configs exec_mode = ExecutionMode() -print("Simulation Execution: Concurrent Execution") +# 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 Simulations (Concurrent): +##### Multiple Simulation Execution (Multi Process Execution) +Documentation: [Simulation Execution](link) +Example [System Model Configurations](link): +* [System Model A](link): `/documentation/examples/sys_model_A.py` +* [System Model B](link): `/documentation/examples/sys_model_B.py` +[Execution Example:](link) `/documentation/examples/sys_model_AB_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, 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) -run2 = Executor(exec_context=multi_proc_ctx, configs=configs) +sys_model_AB_simulation = 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) +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("Tensor Field: " + config_names[i]) - print(tabulate(tensor_field, headers='keys', tablefmt='psql')) - print("Output:") - print(tabulate(result, headers='keys', tablefmt='psql')) + 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 ``` -Multiple Simulations (Concurrent): `/simulations/multi_config run.py` +### Parameter Sweep Simulation (Concurrent): +Documentation: [System Model Parameter Sweep](link) +[Example:](link) `/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 simulations.validation import config1, config2 +from documentation.examples import param_sweep 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) +run = Executor(exec_context=multi_proc_ctx, configs=configs) -i = 0 -config_names = ['config1', 'config2'] -for raw_result, tensor_field in run2.main(): +for raw_result, tensor_field in run.execute(): result = pd.DataFrame(raw_result) print() - print("Tensor Field: " + config_names[i]) + print("Tensor Field:") 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 -``` diff --git a/cadCAD/engine/simulation.py b/cadCAD/engine/simulation.py index a288658..dd2d45d 100644 --- a/cadCAD/engine/simulation.py +++ b/cadCAD/engine/simulation.py @@ -125,41 +125,6 @@ class Executor: for f in state_funcs: yield self.state_update_exception(f(sweep_dict, sub_step, sH, last_in_obj, _input)) - # def generate_record(state_funcs): - # for f in state_funcs: - # tmp_last_in_copy = deepcopy(last_in_obj) - # new_kv = self.state_update_exception(f(sweep_dict, sub_step, sH, tmp_last_in_copy, _input)) - # del tmp_last_in_copy - # yield new_kv - # - # # get `state` from last_in_obj.keys() - # # vals = last_in_obj.values() - # def generate_record(state_funcs): - # for state, v, f in zip(states, vals, state_funcs): - # v_copy = deepcopy(v) - # last_in_obj[state] = v_copy - # new_kv = self.state_update_exception(f(sweep_dict, sub_step, sH, last_in_copy, _input)) - # del v - # yield new_kv - - # {k: v for k, v in l} - - # r() - r(a') -> r(a',b') -> r(a',b',c') - - # r(f(a),b,c) -> r(a'f(b),c) -> r(a',b',f(c)) => r(a',b',c') - # r(a',b.update(),c) - # r1(f(a1),b1,c1) -> r2(a2,f(b1),c1) -> r3(a3,b1,f(c1)) => r(a',b',c') - - # r1(f(a1),b,c) -> r2(a,f(b1),c) -> r3(a,b,f(c1)) => r(a',b',c') - - # r1(f(a1),b1,c1) -> r(a2',b2.update(),c2) -> r3(a3,b1,f(c1)) => r(a',b',c') - - - # r1(f(a1),b1,c1) -> r2(a2,f(b1),c1) -> r3(a3,b1,f(c1)) => r(a',b',c') - - - # reduce(lambda r: F(r), [r2(f(a),b,c), r2(a,f(b),c), r3(a,b,f(c))]) => R(a',b',c') - def transfer_missing_fields(source, destination): for k in source: if k not in destination: diff --git a/documentation/Execution.md b/documentation/Execution.md new file mode 100644 index 0000000..613c8a6 --- /dev/null +++ b/documentation/Execution.md @@ -0,0 +1,160 @@ +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 | | | +| 1 | 2 | | | +| 2 | 3 | | | ++----+-----+--------------------------------+--------------------------------+ +``` + +##### Example Result: System Events DataFrame +```python ++----+-------+------------+-----------+------+-----------+ +| | 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](link): +* [System Model A](link): `/documentation/examples/sys_model_A.py` +* [System Model B](link): `/documentation/examples/sys_model_B.py` +Execution Examples: +* [System Model A](link): `/documentation/examples/sys_model_A_exec.py` +* [System Model B](link): `/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](link) +[Execution Example:](link) `/documentation/examples/sys_model_AB_exec.py` +Example [System Model Configurations](link): +* [System Model A](link): `/documentation/examples/sys_model_A.py` +* [System Model B](link): `/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 +``` + +* ##### *Parameter Sweep* +Documentation: [System Model Parameter Sweep](link) +[Example:](link) `/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() +``` diff --git a/documentation/historical_state_access.md b/documentation/Historically_State_Access.md similarity index 75% rename from documentation/historical_state_access.md rename to documentation/Historically_State_Access.md index 944fc8a..7d684bf 100644 --- a/documentation/historical_state_access.md +++ b/documentation/Historically_State_Access.md @@ -1,24 +1,21 @@ Historical State Access == -The 3rd parameter of state and policy update functions (labels as `sH` of type `List[List[dict]]`) provides access to -past Partial State Updates (PSU) given a negative offset number. `access_block` is used to access past PSUs -(`List[dict]`) from `sH`. +#### 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. -Example: `-2` denotes to second to last PSU +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 +#### Exclusion List Create a list of states to exclude from the reported PSU. ```python exclusion_list = [ - 'nonexsistant', 'last_x', '2nd_to_last_x', '3rd_to_last_x', '4th_to_last_x' + 'nonexistent', 'last_x', '2nd_to_last_x', '3rd_to_last_x', '4th_to_last_x' ] ``` ##### Example Policy Updates ###### Last partial state update ```python -from cadCAD.configuration.utils import config_sim, access_block - -def last_update(_g, substep, sH, s): +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 @@ -27,30 +24,30 @@ def last_update(_g, substep, sH, s): ) } ``` -* 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. +* 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(_g, substep, sH, s): +def second2last_update(_params, substep, sH, s): return {"2nd_to_last_x": access_block(sH, "2nd_to_last_x", -2, exclusion_list)} ``` -##### Define State Updates + ###### 3rd to last partial state update ```python -def third_to_last_x(_g, substep, sH, s, _input): +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(_g, substep, sH, s, _input): +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-exsistant partial state update -* `psu_block_offset >= 0` doesn't exsist +###### Non-exsistent partial state update +* `psu_block_offset >= 0` doesn't exist ```python -def nonexsistant(_g, substep, sH, s, _input): - return 'nonexsistant', access_block(sH, "nonexsistant", 0, exclusion_list) +def nonexistent(_params, substep, sH, s, _input): + return 'nonexistent', access_block(sH, "nonexistent", 0, exclusion_list) ``` #### Example Simulation @@ -91,7 +88,4 @@ Example: `last_x` | 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}] | +----+-----------------------------------------------------------------------------------------------------------------------------------------------------+ -``` - -#### [Example Configuration](link) -#### [Example Results](link) \ No newline at end of file +``` \ No newline at end of file diff --git a/documentation/policy_agg.md b/documentation/Policy_Aggregation.md similarity index 86% rename from documentation/policy_agg.md rename to documentation/Policy_Aggregation.md index 7ddaa50..64b4b1e 100644 --- a/documentation/policy_agg.md +++ b/documentation/Policy_Aggregation.md @@ -15,9 +15,9 @@ policy_ops=[add, mult_by_2] ##### Example Policy Updates per Partial State Update (PSU) ```python -def p1_psu1(_g, step, sL, s): +def p1_psu1(_params, step, sH, s): return {'policy1': 1} -def p2_psu1(_g, step, sL, s): +def p2_psu1(_params, step, sH, s): return {'policy2': 2} ``` * `add` not applicable due to lack of redundant policies @@ -25,9 +25,9 @@ def p2_psu1(_g, step, sL, s): * Result: `{'policy1': 2, 'policy2': 4}` ```python -def p1_psu2(_g, step, sL, s): +def p1_psu2(_params, step, sH, s): return {'policy1': 2, 'policy2': 2} -def p2_psu2(_g, step, sL, s): +def p2_psu2(_params, step, sH, s): return {'policy1': 2, 'policy2': 2} ``` * `add` applicable due to redundant policies @@ -35,9 +35,9 @@ def p2_psu2(_g, step, sL, s): * Result: `{'policy1': 8, 'policy2': 8}` ```python -def p1_psu3(_g, step, sL, s): +def p1_psu3(_params, step, sH, s): return {'policy1': 1, 'policy2': 2, 'policy3': 3} -def p2_psu3(_g, step, sL, s): +def p2_psu3(_params, step, sH, s): return {'policy1': 1, 'policy2': 2, 'policy3': 3} ``` * `add` applicable due to redundant policies diff --git a/documentation/Simulation_Configuration.md b/documentation/Simulation_Configuration.md new file mode 100644 index 0000000..a49e364 --- /dev/null +++ b/documentation/Simulation_Configuration.md @@ -0,0 +1,201 @@ +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 paramenter, 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](https://github.com/BlockScience/cadCAD-Tutorials/blob/master/01%20Tutorials/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](https://github.com/BlockScience/cadCAD-Tutorials/blob/master/01%20Tutorials/robot-marbles-part-2/robot-marbles-part-2.ipynb) for details on why and when to use policy functions. + + + +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 cannnot 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](link) diff --git a/documentation/param_sweep.md b/documentation/System_Model_Parameter_Sweep.md similarity index 84% rename from documentation/param_sweep.md rename to documentation/System_Model_Parameter_Sweep.md index 3822369..aa47e7b 100644 --- a/documentation/param_sweep.md +++ b/documentation/System_Model_Parameter_Sweep.md @@ -31,7 +31,7 @@ Previous State: `y = 0` ```python -def state_update(_params, step, sL, s, _input): +def state_update(_params, step, sH, s, _input): y = 'state' x = s['state'] + _params['alpha'] + _params['gamma'] return y, x @@ -43,8 +43,8 @@ def state_update(_params, step, sL, s, _input): ##### Example Policy Updates ```python # Internal States per Mechanism -def policies(_g, step, sL, s): - return {'beta': _g['beta'], 'gamma': _g['gamma']} +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}` @@ -53,6 +53,13 @@ def policies(_g, step, sL, s): ```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, @@ -64,5 +71,3 @@ sim_config = config_sim( #### [Example Configuration](link) #### [Example Results](link) - - diff --git a/documentation/examples/historical_state_access.py b/documentation/examples/historical_state_access.py index 5079988..fa038e7 100644 --- a/documentation/examples/historical_state_access.py +++ b/documentation/examples/historical_state_access.py @@ -75,7 +75,7 @@ PSUB = { "variables": variables } -partial_state_update_block = { +psubs = { "PSUB1": PSUB, "PSUB2": PSUB, "PSUB3": PSUB @@ -91,7 +91,7 @@ sim_config = config_sim( append_configs( sim_configs=sim_config, initial_state=genesis_states, - partial_state_update_blocks=partial_state_update_block + partial_state_update_blocks=psubs ) exec_mode = ExecutionMode() diff --git a/documentation/examples/param_sweep.py b/documentation/examples/param_sweep.py index a118966..1157db2 100644 --- a/documentation/examples/param_sweep.py +++ b/documentation/examples/param_sweep.py @@ -31,31 +31,31 @@ env_process = {} # Policies -def gamma(_params, step, sL, s): +def gamma(_params, step, sH, s): return {'gamma': _params['gamma']} -def omega(_params, step, sL, s): +def omega(_params, step, sH, s): return {'omega': _params['omega'](7)} # Internal States -def alpha(_params, step, sL, s, _input): +def alpha(_params, step, sH, s, _input): return 'alpha', _params['alpha'] -def alpha_plus_gamma(_params, step, sL, s, _input): +def alpha_plus_gamma(_params, step, sH, s, _input): return 'alpha_plus_gamma', _params['alpha'] + _params['gamma'] -def beta(_params, step, sL, s, _input): +def beta(_params, step, sH, s, _input): return 'beta', _params['beta'] -def policies(_params, step, sL, s, _input): +def policies(_params, step, sH, s, _input): return 'policies', _input -def sweeped(_params, step, sL, s, _input): +def sweeped(_params, step, sH, s, _input): return 'sweeped', {'beta': _params['beta'], 'gamma': _params['gamma']} @@ -90,7 +90,7 @@ for m in psu_steps: psu_block[m]['variables']['policies'] = policies psu_block[m]["variables"]['sweeped'] = var_timestep_trigger(y='sweeped', f=sweeped) -partial_state_update_blocks = psub_list(psu_block, psu_steps) +psubs = psub_list(psu_block, psu_steps) print() pp.pprint(psu_block) print() @@ -99,7 +99,7 @@ append_configs( sim_configs=sim_config, initial_state=genesis_states, env_processes=env_process, - partial_state_update_blocks=partial_state_update_blocks + partial_state_update_blocks=psubs ) exec_mode = ExecutionMode() diff --git a/documentation/examples/policy_aggregation.py b/documentation/examples/policy_aggregation.py index 86313e7..38865ad 100644 --- a/documentation/examples/policy_aggregation.py +++ b/documentation/examples/policy_aggregation.py @@ -7,19 +7,19 @@ from cadCAD.engine import ExecutionMode, ExecutionContext, Executor from cadCAD import configs # Policies per Mechanism -def p1m1(_g, step, sL, s): +def p1m1(_g, step, sH, s): return {'policy1': 1} -def p2m1(_g, step, sL, s): +def p2m1(_g, step, sH, s): return {'policy2': 2} -def p1m2(_g, step, sL, s): +def p1m2(_g, step, sH, s): return {'policy1': 2, 'policy2': 2} -def p2m2(_g, step, sL, s): +def p2m2(_g, step, sH, s): return {'policy1': 2, 'policy2': 2} -def p1m3(_g, step, sL, s): +def p1m3(_g, step, sH, s): return {'policy1': 1, 'policy2': 2, 'policy3': 3} -def p2m3(_g, step, sL, s): +def p2m3(_g, step, sH, s): return {'policy1': 1, 'policy2': 2, 'policy3': 3} @@ -44,7 +44,7 @@ variables = { "policies": policies } -partial_state_update_block = { +psubs = { "m1": { "policies": { "p1": p1m1, @@ -79,7 +79,7 @@ sim_config = config_sim( append_configs( sim_configs=sim_config, initial_state=genesis_states, - partial_state_update_blocks=partial_state_update_block, + partial_state_update_blocks=psubs, policy_ops=[lambda a, b: a + b, lambda y: y * 2] # Default: lambda a, b: a + b ) diff --git a/documentation/examples/sys_model_A.py b/documentation/examples/sys_model_A.py index 92a7fe3..5c54dbe 100644 --- a/documentation/examples/sys_model_A.py +++ b/documentation/examples/sys_model_A.py @@ -14,51 +14,51 @@ seeds = { # Policies per Mechanism -def p1m1(_g, step, sL, s): +def p1m1(_g, step, sH, s): return {'param1': 1} -def p2m1(_g, step, sL, s): +def p2m1(_g, step, sH, s): return {'param1': 1, 'param2': 4} -def p1m2(_g, step, sL, s): +def p1m2(_g, step, sH, s): return {'param1': 'a', 'param2': 2} -def p2m2(_g, step, sL, s): +def p2m2(_g, step, sH, s): return {'param1': 'b', 'param2': 4} -def p1m3(_g, step, sL, s): +def p1m3(_g, step, sH, s): return {'param1': ['c'], 'param2': np.array([10, 100])} -def p2m3(_g, step, sL, s): +def p2m3(_g, step, sH, s): return {'param1': ['d'], 'param2': np.array([20, 200])} # Internal States per Mechanism -def s1m1(_g, step, sL, s, _input): +def s1m1(_g, step, sH, s, _input): y = 's1' x = s['s1'] + 1 return (y, x) -def s2m1(_g, step, sL, s, _input): +def s2m1(_g, step, sH, s, _input): y = 's2' x = _input['param2'] return (y, x) -def s1m2(_g, step, sL, s, _input): +def s1m2(_g, step, sH, s, _input): y = 's1' x = s['s1'] + 1 return (y, x) -def s2m2(_g, step, sL, s, _input): +def s2m2(_g, step, sH, s, _input): y = 's2' x = _input['param2'] return (y, x) -def s1m3(_g, step, sL, s, _input): +def s1m3(_g, step, sH, s, _input): y = 's1' x = s['s1'] + 1 return (y, x) -def s2m3(_g, step, sL, s, _input): +def s2m3(_g, step, sH, s, _input): y = 's2' x = _input['param2'] return (y, x) -def policies(_g, step, sL, s, _input): +def policies(_g, step, sH, s, _input): y = 'policies' x = _input return (y, x) @@ -68,17 +68,17 @@ def policies(_g, step, sL, s, _input): proc_one_coef_A = 0.7 proc_one_coef_B = 1.3 -def es3(_g, step, sL, s, _input): +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, sL, s, _input): +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, sL, s, _input): +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)) @@ -102,7 +102,7 @@ env_processes = { } -partial_state_update_block = [ +psubs = [ { "policies": { "b1": p1m1, @@ -154,6 +154,6 @@ append_configs( sim_configs=sim_config, initial_state=genesis_states, env_processes=env_processes, - partial_state_update_blocks=partial_state_update_block, + partial_state_update_blocks=psubs, policy_ops=[lambda a, b: a + b] ) \ No newline at end of file diff --git a/documentation/examples/sys_model_A_exec.py b/documentation/examples/sys_model_A_exec.py index 8a630d9..e568482 100644 --- a/documentation/examples/sys_model_A_exec.py +++ b/documentation/examples/sys_model_A_exec.py @@ -15,7 +15,7 @@ sys_model_A_simulation = Executor(exec_context=single_proc_ctx, configs=sys_mode 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: config1") +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')) diff --git a/documentation/examples/sys_model_B.py b/documentation/examples/sys_model_B.py index 7298d0b..2c6ad9e 100644 --- a/documentation/examples/sys_model_B.py +++ b/documentation/examples/sys_model_B.py @@ -13,46 +13,46 @@ seeds = { # Policies per Mechanism -def p1m1(_g, step, sL, s): +def p1m1(_g, step, sH, s): return {'param1': 1} -def p2m1(_g, step, sL, s): +def p2m1(_g, step, sH, s): return {'param2': 4} -def p1m2(_g, step, sL, s): +def p1m2(_g, step, sH, s): return {'param1': 'a', 'param2': 2} -def p2m2(_g, step, sL, s): +def p2m2(_g, step, sH, s): return {'param1': 'b', 'param2': 4} -def p1m3(_g, step, sL, s): +def p1m3(_g, step, sH, s): return {'param1': ['c'], 'param2': np.array([10, 100])} -def p2m3(_g, step, sL, s): +def p2m3(_g, step, sH, s): return {'param1': ['d'], 'param2': np.array([20, 200])} # Internal States per Mechanism -def s1m1(_g, step, sL, s, _input): +def s1m1(_g, step, sH, s, _input): y = 's1' x = _input['param1'] return (y, x) -def s2m1(_g, step, sL, s, _input): +def s2m1(_g, step, sH, s, _input): y = 's2' x = _input['param2'] return (y, x) -def s1m2(_g, step, sL, s, _input): +def s1m2(_g, step, sH, s, _input): y = 's1' x = _input['param1'] return (y, x) -def s2m2(_g, step, sL, s, _input): +def s2m2(_g, step, sH, s, _input): y = 's2' x = _input['param2'] return (y, x) -def s1m3(_g, step, sL, s, _input): +def s1m3(_g, step, sH, s, _input): y = 's1' x = _input['param1'] return (y, x) -def s2m3(_g, step, sL, s, _input): +def s2m3(_g, step, sH, s, _input): y = 's2' x = _input['param2'] return (y, x) @@ -62,17 +62,17 @@ def s2m3(_g, step, sL, s, _input): proc_one_coef_A = 0.7 proc_one_coef_B = 1.3 -def es3(_g, step, sL, s, _input): +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, sL, s, _input): +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, sL, s, _input): +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)) @@ -95,7 +95,7 @@ env_processes = { "s4": env_trigger(3)(trigger_field='timestamp', trigger_vals=trigger_timestamps, funct_list=[lambda _g, x: 10]) } -partial_state_update_block = [ +psubs = [ { "policies": { "b1": p1m1, @@ -143,5 +143,5 @@ append_configs( sim_configs=sim_config, initial_state=genesis_states, env_processes=env_processes, - partial_state_update_blocks=partial_state_update_block + partial_state_update_blocks=psubs ) \ No newline at end of file diff --git a/documentation/examples/sys_model_B_exec.py b/documentation/examples/sys_model_B_exec.py index 53eef37..75a339a 100644 --- a/documentation/examples/sys_model_B_exec.py +++ b/documentation/examples/sys_model_B_exec.py @@ -9,14 +9,14 @@ exec_mode = ExecutionMode() print("Simulation Execution: Single Configuration") print() -first_config = configs # only contains config2 +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: config1") +print("Tensor Field: sys_model_B") print(tabulate(tensor_field, headers='keys', tablefmt='psql')) print("Output:") print(tabulate(result, headers='keys', tablefmt='psql')) diff --git a/documentation/execution.md b/documentation/execution.md deleted file mode 100644 index f34064b..0000000 --- a/documentation/execution.md +++ /dev/null @@ -1,71 +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 | | | -| 1 | 2 | | | -| 2 | 3 | | | -+----+-----+--------------------------------+--------------------------------+ -``` - -##### Example Result: System Events DataFrame -```python -+----+-------+------------+-----------+------+-----------+ -| | 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] | -+----+-------+------------+-----------+------+-----------+ -``` - -##### [Single Process Example Execution](link) - -##### [Multiple Process Example Execution](link) diff --git a/documentation/sys_model_config.md b/documentation/sys_model_config.md deleted file mode 100644 index edece69..0000000 --- a/documentation/sys_model_config.md +++ /dev/null @@ -1,220 +0,0 @@ -System Model Configuration -== - -#### Introduction - -Given System Model Configurations, cadCAD produces system event datasets that conform to specified system metrics. Each -event / record is of [Enogenous State variables](link) produced by user defined [Partial State Updates](link) (PSU / -functions that update state); A sequence of event / record subsets that comprises the resulting system event dataset is -produced by a [Partial State Update Block](link) (PSUB / a Tensor Field for which State, Policy, and Time are dimensions -and PSU functions are values). - -A **System Model Configuration** is comprised of a simulation configuration, initial endogenous states, Partial State -Update Blocks, environmental process, and a user defined policy aggregation function. - -Execution: - -#### Simulation Properties - -###### System Metrics -The following system metrics determine the size of resulting system event datasets: -* `run` - the number of simulations in the resulting dataset -* `timestep` - the number of timestamps in the resulting dataset -* `substep` - the number of PSUs per `timestep` / within PSUBS -* Number of events / records: `run` x `timestep` x `substep` - -###### Simulation Configuration -For the following dictionary, `T` is assigned a `timestep` range, `N` is assigned the number of simulation runs, and -`params` is assigned the [**Parameter Sweep**](link) dictionary. - -```python -from cadCAD.configuration.utils import config_sim - -sim_config = config_sim({ - "N": 2, - "T": range(5), - "M": params, # Optional -}) -``` - -#### Initial Endogenous States -**Enogenous State variables** are read-only variables defined to capture the shape and property of the network and -represent internal input and signal. - -The PSUB tensor field is applied to the following states to produce a resulting system event -dataset. -```python -genesis_states = { - 's1': 0.0, - 's2': 0.0, - 's3': 1.0, - 'timestamp': '2018-10-01 15:16:24' -} -``` - -#### Partial State Update Block: -- ***Partial State Update Block(PSUB)*** ***(Define ?)*** Tensor Field for which State, Policy, Time are dimensions -and Partial State Update functions are values. -- ***Partial State Update (PSU)*** are user defined functions that encodes state updates and are executed in -a specified order PSUBs. PSUs update states given the most recent set of states and PSU policies. -- ***Mechanism*** ***(Define)*** - - -The PSUBs is a list of PSU dictionaries of the structure within the code block below. PSUB elements (PSU dictionaries) -are listed / defined in order of `substeps` and **identity functions** (returning a previous state's value) are assigned -to unreferenced states within PSUs. The number of records produced produced per `timestep` is the number of `substeps`. - -```python -partial_state_update_block = [ - { - "policies": { - "b1": p1_psu1, - "b2": p2_psu1 - }, - "variables": { - "s1": s1_psu1, - "s2": s2_psu1 - } - }, - { - "policies": { - "b1": p1_psu2, - }, - "variables": { - "s2": s2_psu2 - } - }, - {...} -] -``` -*Notes:* -1. An identity function (returning the previous state value) is assigned to `s1` in the second PSU. -2. Currently the only names that need not correspond to the convention below are `'b1'` and `'b2'`. - -#### Policies -- ***Policies*** ***(Define)*** When are policies behavior ? -- ***Behaviors*** 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. - -Policies accept parameter sweep variables [see link] `_g` (`dict`), the most recent -`substep` integer, the state history[see link] (`sH`), the most recent state record `s` (`dict) as inputs and returns a -set of actions (`dict`). - -Policy functions return dictionaries as actions. Policy functions provide access to parameter sweep variables [see link] -via dictionary `_g`. -```python -def p1_psu1(_g, substep, sH, s): - return {'policy1': 1} -def p2_psu1(_g, substep, sH, s): - return {'policy1': 1, 'policy2': 4} -``` -For each PSU, multiple policy dictionaries are aggregated into a single dictionary to be imputted into -all state functions using an initial reduction function (default: `lambda a, b: a + b`) and optional subsequent map -functions. -Example Result: `{'policy1': 2, 'policy2': 4}` - -#### State Updates -State update functions provide access to parameter sweep variables [see link] `_g` (`dict`), the most recent `substep` -integer, the state history[see link] (`sH`), the most recent state record as a dictionary (`s`), the policies of a -PSU (`_input`), and returns a tuple of the state variable's name and the resulting new value of the variable. - -```python -def state_update(_g, substep, sH, s, _input): - ... - return state, update -``` -**Note:** Each state update function updates one state variable at a time. Changes to multiple state variables requires -separate state update functions. A generic example of a PSU is as follows. - -* ##### Endogenous State Updates -They are only updated by PSUs and can be used as inputs to a PSUs. -```python -def s1_update(_g, substep, sH, s, _input): - x = _input['policy1'] + 1 - return 's1', x - -def s2_update(_g, substep, sH, s, _input): - x = _input['policy2'] - return 's2', x -``` - -* ##### Exogenous State Updates -***Exogenous State variables*** ***(Review)*** are read-only variables that represent external input and signal. They -update endogenous states and are only updated by environmental processes. Exgoneous variables can be used -as an input to a PSU that impacts state variables. ***(Expand upon Exogenous state updates)*** - -```python -from datetime import timedelta -from cadCAD.configuration.utils import time_step -def es3_update(_g, substep, sH, s, _input): - x = ... - return 's3' -def es4_update(_g, substep, sH, s, _input): - x = ... - return 's4', x -def update_timestamp(_g, substep, sH, s, _input): - x = time_step(dt_str=s[y], dt_format='%Y-%m-%d %H:%M:%S', _timedelta=timedelta(days=0, minutes=0, seconds=1)) - return 'timestamp', x -``` -Exogenous state update functions (`es3_update`, `es4_update` and `es5_update`) update once per timestamp and should be -included as a part of the first PSU in the PSUB. -```python -partial_state_update_block['psu1']['variables']['s3'] = es3_update -partial_state_update_block['psu1']['variables']['s4'] = es4_update -partial_state_update_block['psu1']['variables']['timestamp'] = update_timestamp -``` - -* #### Environmental Process -- ***Environmental processes*** model external changes that directly impact exogenous states at given specific -conditions such as market shocks at specific timestamps. - -Create a dictionary like `env_processes` below for which the keys are exogenous states and the values are lists of user -defined **Environment Update** functions to be composed (e.g. `[f(params, x), g(params, x)]` becomes -`f(params, g(params, x))`). - -Environment Updates accept the [**Parameter Sweep**](link) dictionary `params` and a state as a result of a PSU. -```python -def env_update(params, state): - . . . - return updated_state - -# OR - -env_update = lambda params, state: state + 5 -``` - -The `env_trigger` function is used to apply composed environment update functions to a list of specific exogenous state -update results. `env_trigger` accepts the total number of `substeps` for the simulation / `end_substep` and returns a -function accepting `trigger_field`, `trigger_vals`, and `funct_list`. - -In the following example functions are used to add `5` to every `s3` update and assign `10` to `s4` at -`timestamp`s `'2018-10-01 15:16:25'`, `'2018-10-01 15:16:27'`, and `'2018-10-01 15:16:29'`. -```python -from cadCAD.configuration.utils import env_trigger -trigger_timestamps = ['2018-10-01 15:16:25', '2018-10-01 15:16:27', '2018-10-01 15:16:29'] -env_processes = { - "s3": [lambda params, x: x + 5], - "s4": env_trigger(end_substep=3)( - trigger_field='timestamp', trigger_vals=trigger_timestamps, funct_list=[lambda params, x: 10] - ) -} -``` - -#### System Model Configuration -`append_configs`, stores a **System Model Configuration** to be (Executed)[url] as -simulations producing system event dataset(s) - -```python -from cadCAD.configuration import append_configs - -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] -) -``` - -#### [System Simulation Execution](link) From 9399c6b72827ede3fd2fdb18eb4ffc3d8b9abdcf Mon Sep 17 00:00:00 2001 From: "Joshua E. Jodesty" Date: Tue, 30 Jul 2019 12:53:25 -0400 Subject: [PATCH 5/9] improved readme --- README.md | 20 ++++++++------------ documentation/Execution.md | 4 ++-- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 129a577..679a8bf 100644 --- a/README.md +++ b/README.md @@ -27,12 +27,13 @@ It allows us to use code to solidify our conceptualized ideas and see if the out iteratively refine our work until we have constructed a model that closely reflects reality at the start of the model, and see how it evolves. We can then use these results to inform business decisions. -#### Simulation Instructional: +#### Documentation: * ##### [System Model Configuration](link) * ##### [System Simulation Execution](link) +* ##### [Tutorials](link) -#### Installation: -**1. Install Dependencies:** + +#### 0. Installation: Install Dependencies **Option A:** Package Repository Access @@ -49,22 +50,17 @@ python3 setup.py sdist bdist_wheel pip3 install dist/*.whl ``` -**2. Configure Simulation:** -Intructions: -`/Simulation.md` +#### 1. [Configure System Model](link) -Examples: -`/simulations/validation/*` - -**3. Import cadCAD & Run Simulations:** +#### 2. [Execute Simulations:](link) ##### Single Process Execution: Example [System Model Configurations](link): * [System Model A](link): `/documentation/examples/sys_model_A.py` * [System Model B](link): `/documentation/examples/sys_model_B.py` -Execution Examples: +Example Simulation Executions: * [System Model A](link): `/documentation/examples/sys_model_A_exec.py` * [System Model B](link): `/documentation/examples/sys_model_B_exec.py` ```python @@ -98,7 +94,7 @@ Documentation: [Simulation Execution](link) Example [System Model Configurations](link): * [System Model A](link): `/documentation/examples/sys_model_A.py` * [System Model B](link): `/documentation/examples/sys_model_B.py` -[Execution Example:](link) `/documentation/examples/sys_model_AB_exec.py` +[Example Simulation Executions::](link) `/documentation/examples/sys_model_AB_exec.py` ```python import pandas as pd from tabulate import tabulate diff --git a/documentation/Execution.md b/documentation/Execution.md index 613c8a6..d8dc83b 100644 --- a/documentation/Execution.md +++ b/documentation/Execution.md @@ -71,7 +71,7 @@ simulation_result = pd.DataFrame(raw_system_events) Example [System Model Configurations](link): * [System Model A](link): `/documentation/examples/sys_model_A.py` * [System Model B](link): `/documentation/examples/sys_model_B.py` -Execution Examples: +Example Simulation Executions: * [System Model A](link): `/documentation/examples/sys_model_A_exec.py` * [System Model B](link): `/documentation/examples/sys_model_B_exec.py` ```python @@ -103,7 +103,7 @@ print() * ##### *Multi Process Execution* Documentation: [Simulation Execution](link) -[Execution Example:](link) `/documentation/examples/sys_model_AB_exec.py` +[Example Simulation Executions::](link) `/documentation/examples/sys_model_AB_exec.py` Example [System Model Configurations](link): * [System Model A](link): `/documentation/examples/sys_model_A.py` * [System Model B](link): `/documentation/examples/sys_model_B.py` From 67c46cfe094c7bbb86becd375ccc71a2b1ca4c22 Mon Sep 17 00:00:00 2001 From: "Joshua E. Jodesty" Date: Wed, 21 Aug 2019 14:16:31 -0400 Subject: [PATCH 6/9] diverged docs --- README.md | 6 +++--- documentation/Simulation_Configuration.md | 16 ++++++++++++---- documentation/System_Model_Parameter_Sweep.md | 4 ++-- .../examples/historical_state_access.py | 1 - documentation/examples/policy_aggregation.py | 2 +- documentation/examples/sys_model_A.py | 3 +++ 6 files changed, 21 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 679a8bf..ee67bb9 100644 --- a/README.md +++ b/README.md @@ -33,11 +33,12 @@ and see how it evolves. We can then use these results to inform business decisio * ##### [Tutorials](link) -#### 0. Installation: Install Dependencies +#### 0. Installation: **Option A:** Package Repository Access -***IMPORTANT NOTE:*** Tokens are issued to and meant to be used by trial users and BlockScience employees **ONLY**. Replace \ with an issued token in the script below. +***IMPORTANT NOTE:*** Tokens are issued to and meant to be used by trial users and BlockScience employees **ONLY**. +Replace \ with an issued token in the script below. ```bash pip3 install pandas pathos fn funcy tabulate pip3 install cadCAD --extra-index-url https://@repo.fury.io/blockscience/ @@ -55,7 +56,6 @@ pip3 install dist/*.whl #### 2. [Execute Simulations:](link) - ##### Single Process Execution: Example [System Model Configurations](link): * [System Model A](link): `/documentation/examples/sys_model_A.py` diff --git a/documentation/Simulation_Configuration.md b/documentation/Simulation_Configuration.md index a49e364..304a01d 100644 --- a/documentation/Simulation_Configuration.md +++ b/documentation/Simulation_Configuration.md @@ -7,7 +7,8 @@ Given a **Simulation Configuration**, cadCAD produces datasets that represent th 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 +`append_configs`, stores a **Simulation Configuration** to be +[Executed](https://github.com/BlockScience/cadCAD-Tutorials/blob/master/Documentation/Simulation_Execution.md) by cadCAD ```python from cadCAD.configuration import append_configs @@ -21,13 +22,20 @@ append_configs( ``` 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) + + See [Policy Aggregation](https://github.com/BlockScience/cadCAD-Tutorials/blob/master/Documentation/Policy_Aggregation.md) + +* **sim_configs** : + + See ## Simulation Properties diff --git a/documentation/System_Model_Parameter_Sweep.md b/documentation/System_Model_Parameter_Sweep.md index aa47e7b..63b7306 100644 --- a/documentation/System_Model_Parameter_Sweep.md +++ b/documentation/System_Model_Parameter_Sweep.md @@ -69,5 +69,5 @@ sim_config = config_sim( ) ``` -#### [Example Configuration](link) -#### [Example Results](link) +#### [Example](link) + diff --git a/documentation/examples/historical_state_access.py b/documentation/examples/historical_state_access.py index fa038e7..327460a 100644 --- a/documentation/examples/historical_state_access.py +++ b/documentation/examples/historical_state_access.py @@ -11,7 +11,6 @@ exclusion_list = ['nonexsistant', 'last_x', '2nd_to_last_x', '3rd_to_last_x', '4 # 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( diff --git a/documentation/examples/policy_aggregation.py b/documentation/examples/policy_aggregation.py index 38865ad..2807a74 100644 --- a/documentation/examples/policy_aggregation.py +++ b/documentation/examples/policy_aggregation.py @@ -80,7 +80,7 @@ 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 + policy_ops=[lambda a, b: a + b] # Default: lambda a, b: a + b , lambda y: y * 2 ) exec_mode = ExecutionMode() diff --git a/documentation/examples/sys_model_A.py b/documentation/examples/sys_model_A.py index 5c54dbe..3614291 100644 --- a/documentation/examples/sys_model_A.py +++ b/documentation/examples/sys_model_A.py @@ -35,6 +35,9 @@ 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'] From 747ec36e5096e959cbdf3f79cbba25f484d8e18d Mon Sep 17 00:00:00 2001 From: "Joshua E. Jodesty" Date: Wed, 21 Aug 2019 14:27:31 -0400 Subject: [PATCH 7/9] os refactor pt 1 --- cadCAD/configuration/__init__.py | 4 +- cadCAD/configuration/utils/__init__.py | 16 +-- .../utils/depreciationHandler.py | 6 -- .../configuration/utils/policyAggregation.py | 3 +- .../configuration/utils/userDefinedObject.py | 17 +-- cadCAD/engine/__init__.py | 1 - cadCAD/engine/simulation.py | 12 --- cadCAD/engine/utils.py | 4 - cadCAD/utils/__init__.py | 31 +----- cadCAD/utils/sys_config.py | 102 ++---------------- 10 files changed, 27 insertions(+), 169 deletions(-) diff --git a/cadCAD/configuration/__init__.py b/cadCAD/configuration/__init__.py index 56aae05..cbfea98 100644 --- a/cadCAD/configuration/__init__.py +++ b/cadCAD/configuration/__init__.py @@ -9,8 +9,6 @@ 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 -# policy_ops=[foldr(dict_elemwise_sum())] -# policy_ops=[reduce, lambda a, b: {**a, **b}] class Configuration(object): def __init__(self, sim_config={}, initial_state={}, seeds={}, env_processes={}, @@ -28,7 +26,7 @@ class Configuration(object): sanitize_config(self) -# ToDo: Remove Seeds + 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: diff --git a/cadCAD/configuration/utils/__init__.py b/cadCAD/configuration/utils/__init__.py index 8c16b8c..ea5f068 100644 --- a/cadCAD/configuration/utils/__init__.py +++ b/cadCAD/configuration/utils/__init__.py @@ -5,12 +5,10 @@ from fn.func import curried from funcy import curry import pandas as pd -# Temporary from cadCAD.configuration.utils.depreciationHandler import sanitize_partial_state_updates from cadCAD.utils import dict_filter, contains_type, flatten_tabulated_dict, tabulate_dict -# ToDo: Fix - Returns empty when partial_state_update is missing in Configuration class TensorFieldReport: def __init__(self, config_proc): self.config_proc = config_proc @@ -56,7 +54,6 @@ def time_step(dt_str, dt_format='%Y-%m-%d %H:%M:%S', _timedelta = tstep_delta): return t.strftime(dt_format) -# ToDo: Inject in first elem of last PSUB from Historical state 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) @@ -65,7 +62,7 @@ def ep_time_step(s_condition, dt_str, fromat_str='%Y-%m-%d %H:%M:%S', _timedelta else: return dt_str -# mech_sweep_filter + 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([ @@ -77,7 +74,7 @@ def partial_state_sweep_filter(state_field, partial_state_updates): def state_sweep_filter(raw_exogenous_states): return dict([(k, v) for k, v in raw_exogenous_states.items() if isinstance(v, list)]) -# sweep_mech_states + @curried def sweep_partial_states(_type, in_config): configs = [] @@ -129,16 +126,19 @@ def exo_update_per_ts(ep): 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) @@ -173,7 +173,6 @@ def env_trigger(end_substep): curry(trigger)(end_substep)(trigger_field)(trigger_vals)(funct_list) -# param sweep enabling middleware def config_sim(d): def process_variables(d): return flatten_tabulated_dict(tabulate_dict(d)) @@ -184,15 +183,18 @@ def config_sim(d): 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): @@ -202,7 +204,7 @@ def genereate_psubs(policy_grid, states_grid, policies, state_updates): return PSUBS -# ToDo: DO NOT filter sH for every state/policy update. Requires a consumable sH (new sH) + def access_block(state_history, target_field, psu_block_offset, exculsion_list=[]): exculsion_list += [target_field] def filter_history(key_list, sH): diff --git a/cadCAD/configuration/utils/depreciationHandler.py b/cadCAD/configuration/utils/depreciationHandler.py index 330823b..ab57082 100644 --- a/cadCAD/configuration/utils/depreciationHandler.py +++ b/cadCAD/configuration/utils/depreciationHandler.py @@ -2,8 +2,6 @@ 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 @@ -18,8 +16,6 @@ def sanitize_config(config): 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') @@ -28,8 +24,6 @@ def sanitize_partial_state_updates(partial_state_updates): 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 isinstance(new_partial_state_updates, list): for v in new_partial_state_updates: rename_keys(v) diff --git a/cadCAD/configuration/utils/policyAggregation.py b/cadCAD/configuration/utils/policyAggregation.py index 96077dc..9309d01 100644 --- a/cadCAD/configuration/utils/policyAggregation.py +++ b/cadCAD/configuration/utils/policyAggregation.py @@ -1,6 +1,7 @@ from fn.op import foldr from fn.func import curried + def get_base_value(x): if isinstance(x, str): return '' @@ -17,7 +18,7 @@ def policy_to_dict(v): add = lambda a, b: a + b -# df_union = lambda a, b: ... + @curried def foldr_dict_vals(f, d): diff --git a/cadCAD/configuration/utils/userDefinedObject.py b/cadCAD/configuration/utils/userDefinedObject.py index 4ced71f..f37b3f2 100644 --- a/cadCAD/configuration/utils/userDefinedObject.py +++ b/cadCAD/configuration/utils/userDefinedObject.py @@ -1,23 +1,22 @@ from collections import namedtuple -from copy import deepcopy 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 - # returns dict to dataframe - def __repr__(self): members = {} variables = { @@ -27,17 +26,7 @@ class udcView(object): members['methods'] = [k for k, v in self.__dict__.items() if str(type(v)) == ""] members.update(variables) - return f"{members}" #[1:-1] - - # def __repr__(self): - # members = {} - # variables = { - # k: val_switch(v) for k, v in self.__dict__.items() - # if str(type(v)) != "" and k not in self.masked_members and k == 'x' # and isinstance(v, DataFrame) is not True - # } - # - # members.update(variables) - # return f"{members}" #[1:-1] + return f"{members}" class udcBroker(object): diff --git a/cadCAD/engine/__init__.py b/cadCAD/engine/__init__.py index a8002b9..8147f5f 100644 --- a/cadCAD/engine/__init__.py +++ b/cadCAD/engine/__init__.py @@ -93,7 +93,6 @@ class Executor: final_result = None 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) final_result = result, tensor_field diff --git a/cadCAD/engine/simulation.py b/cadCAD/engine/simulation.py index dd2d45d..c5818a4 100644 --- a/cadCAD/engine/simulation.py +++ b/cadCAD/engine/simulation.py @@ -63,9 +63,6 @@ class Executor: ) for k, val_list in new_dict.items() } - # [f1] = ops - # return {k: reduce(f1, val_list) for k, val_list in new_dict.items()} - # return foldr(call, col_results)(ops) def apply_env_proc( self, @@ -98,7 +95,6 @@ class Executor: return state_dict - # ToDo: Redifined as a function that applies the tensor field to a set og last conditions # mech_step def partial_state_update( self, @@ -119,8 +115,6 @@ class Executor: ) - # ToDo: add env_proc generator to `last_in_copy` iterator as wrapper function - # ToDo: Can be multithreaded ?? 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)) @@ -134,7 +128,6 @@ class Executor: 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) - # ToDo: make 'substep' & 'timestep' reserve fields last_in_copy['substep'], last_in_copy['timestep'], last_in_copy['run'] = sub_step, time_step, run sL.append(last_in_copy) @@ -155,7 +148,6 @@ class Executor: sub_step = 0 states_list_copy: List[Dict[str, Any]] = deepcopy(simulation_list[-1]) - # ToDo: Causes Substep repeats in sL: genesis_states: Dict[str, Any] = states_list_copy[-1] if len(states_list_copy) == 1: @@ -167,7 +159,6 @@ class Executor: del states_list_copy states_list: List[Dict[str, Any]] = [genesis_states] - # ToDo: Was causing Substep repeats in sL, use for yield sub_step += 1 for [s_conf, p_conf] in configs: # tensor field @@ -196,7 +187,6 @@ class Executor: ) -> List[List[Dict[str, Any]]]: time_seq: List[int] = [x + 1 for x in time_seq] - # ToDo: simulation_list should be a Tensor that is generated throughout the Executor simulation_list: List[List[Dict[str, Any]]] = [states_list] for time_step in time_seq: @@ -209,8 +199,6 @@ class Executor: return simulation_list - # ToDo: Below can be recieved from a tensor field - # configs: List[Tuple[List[Callable], List[Callable]]] def simulation( self, sweep_dict: Dict[str, List[Any]], diff --git a/cadCAD/engine/utils.py b/cadCAD/engine/utils.py index c0f3a76..4fa5b47 100644 --- a/cadCAD/engine/utils.py +++ b/cadCAD/engine/utils.py @@ -24,8 +24,6 @@ def retrieve_state(l, offset): return l[last_index(l) + offset + 1] -# exception_function = f(sub_step, sL, sL[-2], _input) -# try_function = f(sub_step, sL, last_mut_obj, _input) @curried def engine_exception(ErrorType, error_message, exception_function, try_function): try: @@ -38,5 +36,3 @@ def engine_exception(ErrorType, error_message, exception_function, try_function) @curried def fit_param(param, x): return x + param - -# fit_param = lambda param: lambda x: x + param diff --git a/cadCAD/utils/__init__.py b/cadCAD/utils/__init__.py index ccb5f9d..0243464 100644 --- a/cadCAD/utils/__init__.py +++ b/cadCAD/utils/__init__.py @@ -11,15 +11,11 @@ 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 -# def val_switch(v): -# if isinstance(v, DataFrame) is True or isinstance(v, SilentDF) is True: -# return SilentDF(v) -# else: -# return v.x class IndexCounter: def __init__(self): @@ -29,8 +25,6 @@ class IndexCounter: self.i += 1 return self.i -# def compose(*functions): -# return reduce(lambda f, g: lambda x: f(g(x)), functions, lambda x: x) def compose(*functions): return reduce(lambda f, g: lambda x: f(g(x)), functions, lambda x: x) @@ -108,8 +102,7 @@ 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] @@ -147,23 +140,3 @@ def curry_pot(f, *argv): return f(argv[0], argv[1], argv[2]) else: raise TypeError('curry_pot() needs 3 or 4 positional arguments') - -# 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') - -# def rename(newname): -# def decorator(f): -# f.__name__ = newname -# return f -# return decorator diff --git a/cadCAD/utils/sys_config.py b/cadCAD/utils/sys_config.py index 3da1efe..4d1c285 100644 --- a/cadCAD/utils/sys_config.py +++ b/cadCAD/utils/sys_config.py @@ -1,37 +1,46 @@ 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: @@ -40,94 +49,3 @@ def time_model(y, substeps, time_delta, ts_format='%Y-%m-%d %H:%M:%S'): return y, s[y] return lambda _g, step, sL, s, _input: apply_incriment_condition(s) - -# ToDo: Impliment Matrix reduction -# -# [ -# {'conditions': [123], 'opp': lambda a, b: a and b}, -# {'conditions': [123], 'opp': lambda a, b: a and b} -# ] - -# def trigger_condition2(s, conditions, cond_opp): -# # print(conditions) -# condition_bools = [s[field] in precondition_values for field, precondition_values in conditions.items()] -# return reduce(cond_opp, condition_bools) -# -# def trigger_multi_conditions(s, multi_conditions, multi_cond_opp): -# # print([(d['conditions'], d['reduction_opp']) for d in multi_conditions]) -# condition_bools = [ -# trigger_condition2(s, conditions, opp) for conditions, opp in [ -# (d['conditions'], d['reduction_opp']) for d in multi_conditions -# ] -# ] -# return reduce(multi_cond_opp, condition_bools) -# -# def apply_state_condition2(multi_conditions, multi_cond_opp, y, f, _g, step, sL, s, _input): -# if trigger_multi_conditions(s, multi_conditions, multi_cond_opp): -# return f(_g, step, sL, s, _input) -# else: -# return y, s[y] -# -# def proc_trigger2(y, f, multi_conditions, multi_cond_opp): -# return lambda _g, step, sL, s, _input: apply_state_condition2(multi_conditions, multi_cond_opp, y, f, _g, step, sL, s, _input) -# -# def timestep_trigger2(end_substep, y, f): -# multi_conditions = [ -# { -# 'condition': { -# 'substep': [0, end_substep] -# }, -# 'reduction_opp': lambda a, b: a and b -# } -# ] -# multi_cond_opp = lambda a, b: a and b -# return proc_trigger2(y, f, multi_conditions, multi_cond_opp) - -# -# @curried - - - -# print(env_trigger(3).__module__) -# pp.pprint(dir(env_trigger)) - - - -# @curried -# def env_proc_trigger(trigger_time, update_f, time): -# if time == trigger_time: -# return update_f -# else: -# return lambda x: x - - - - -# def p1m1(_g, step, sL, s): -# return {'param1': 1} -# -# def apply_policy_condition(policies, policy_id, f, conditions, _g, step, sL, s): -# if trigger_condition(s, conditions): -# policies[policy_id] = f(_g, step, sL, s) -# return policies -# else: -# return policies -# -# def proc_trigger2(policies, conditions, policy_id, f): -# return lambda _g, step, sL, s: apply_policy_condition(policies, policy_id, f, conditions,_g, step, sL, s) - -# policies_updates = {"p1": udo_policyA, "p2": udo_policyB} - - -# @curried -# def proc_trigger(trigger_time, update_f, time): -# if time == trigger_time: -# return update_f -# else: -# return lambda x: x - - -# def repr(_g, step, sL, s, _input): -# y = 'z' -# x = s['state_udo'].__repr__() -# return (y, x) \ No newline at end of file From 7d0a14efbf707463b5052cf4ceb1d92debc68cdb Mon Sep 17 00:00:00 2001 From: "Joshua E. Jodesty" Date: Thu, 22 Aug 2019 12:52:32 -0400 Subject: [PATCH 8/9] added docs from tutorial --- .gitignore | 9 +- README.md | 28 ++- Simulation.md | 151 -------------- documentation/Policy_Aggregation.md | 21 +- documentation/Simulation_Configuration.md | 159 +++++++++------ .../{Execution.md => Simulation_Execution.md} | 2 +- documentation/System_Model_Parameter_Sweep.md | 6 +- documentation/examples/policy_aggregation.py | 2 +- documentation/examples/sys_model_A.py | 3 - setup.py | 2 +- testing/example.py | 20 -- testing/example2.py | 71 ------- testing/generic_test.py | 16 -- testing/system_models/external_dataset.py | 5 +- .../system_models/historical_state_access.py | 2 - testing/system_models/param_sweep.py | 12 -- testing/system_models/policy_aggregation.py | 5 +- testing/system_models/udo.py | 185 ------------------ .../{external_test.py => external_dataset.py} | 23 +-- testing/tests/historical_state_access.py | 2 - testing/tests/multi_config_test.py | 56 ------ testing/tests/param_sweep.py | 12 -- testing/tests/policy_aggregation.py | 4 + testing/tests/udo.py | 39 ---- 24 files changed, 161 insertions(+), 674 deletions(-) delete mode 100644 Simulation.md rename documentation/{Execution.md => Simulation_Execution.md} (99%) delete mode 100644 testing/example.py delete mode 100644 testing/example2.py delete mode 100644 testing/system_models/udo.py rename testing/tests/{external_test.py => external_dataset.py} (83%) delete mode 100644 testing/tests/multi_config_test.py delete mode 100644 testing/tests/udo.py diff --git a/.gitignore b/.gitignore index 2990b51..8b72b88 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,13 @@ cadCAD.egg-info build cadCAD.egg-info -SimCAD.egg-info + +testing/example.py +testing/example2.py +testing/multi_config_test.py +testing/udo.py +testing/udo_test.py + +Simulation.md monkeytype.sqlite3 \ No newline at end of file diff --git a/README.md b/README.md index ee67bb9..da92cd3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,17 @@ -# cadCAD -**Warning**: -**Do not** publish this package / software to **any** software repository **except** one permitted by BlockScience. +``` + __________ ____ + ________ __ _____/ ____/ | / __ \ + / ___/ __` / __ / / / /| | / / / / +/ /__/ /_/ / /_/ / /___/ ___ |/ /_/ / +\___/\__,_/\__,_/\____/_/ |_/_____/ +by BlockScience +``` + +**Introduction:** + +***cadCAD*** is a Python library that assists in the processes of designing, testing and validating complex systems through +simulation. At its core, cadCAD is a differential games engine that supports parameter sweeping and Monte Carlo analyses +and can be easily integrated with other scientific computing Python modules and data science workflows. **Description:** @@ -35,9 +46,9 @@ and see how it evolves. We can then use these results to inform business decisio #### 0. Installation: -**Option A:** Package Repository Access +**Option A:** Proprietary Build Access -***IMPORTANT NOTE:*** Tokens are issued to and meant to be used by trial users and BlockScience employees **ONLY**. +***IMPORTANT NOTE:*** Tokens are issued to those with access to proprietary builds of cadCAD and BlockScience employees **ONLY**. Replace \ with an issued token in the script below. ```bash pip3 install pandas pathos fn funcy tabulate @@ -147,3 +158,10 @@ for raw_result, tensor_field in run.execute(): print() ``` +### Tests: +```python +python -m unittest testing/tests/param_sweep.py +python -m unittest testing/tests/policy_aggregation.py +python -m unittest testing/tests/historical_state_access.py +python -m unittest testing/tests/external_dataset.py +``` diff --git a/Simulation.md b/Simulation.md deleted file mode 100644 index 8c1d3cf..0000000 --- a/Simulation.md +++ /dev/null @@ -1,151 +0,0 @@ -# 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 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. - -## State Variables and Transitions - -We now define variables and different transition mechanisms that will be inputs to the simulation engine. - -- ***State variables*** are defined to capture the shape and property of the network, such as a vector or a dictionary that captures all user balances. -- ***Exogenous variables*** are variables that represent external input and signal. They are only affected by environmental processes and are not affected by system mechanisms. Nonetheless, exgoneous variables can be used as an input to a mechanism that impacts state variables. They can be considered as read-only variables to the system. -- ***Behaviors per transition*** model agent behaviors in reaction to state variables and exogenous variables. The resulted user action will become an input to state mechanisms. Note that user behaviors should not directly update value of state variables. -- ***State mechanisms per transition*** are system defined mechanisms that take user actions and other states as inputs and produce updates to the value of state variables. -- ***Exogenous state updates*** specify how exogenous variables evolve with time which can indirectly impact state variables through behavior and state mechanisms. -- ***Environmental processes*** model external changes that directly impact state or exogenous variables at specific timestamps or conditions. - -A state evolves to another state via state transition. Each transition is composed of behavior and state mechanisms as functions of state and exogenous variables. A flow of the state transition is as follows. - -Given some state and exogenous variables of the system at the onset of a state transition, agent behavior takes in these variables as input and return a set of agent actions. This models after agent behavior and reaction to a set of variables. Given these agent actions, state mechanism, as defined by the protocol, takes these actions, state, and exogenous variables as inputs and return a new set of state variables. - -## System Configuration File - -Simulation engine takes in system configuration files, e.g. `config.py`, where all the above variables and mechanisms are defined. The following import statements should be added at the beginning of the configuration files. -```python -from decimal import Decimal -import numpy as np -from datetime import timedelta - -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 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), - 's2': Decimal(0.0), - 's3': Decimal(1.0), - 'timestamp': '2018-10-01 15:16:24' -} -``` - -Each potential transition and its state and behavior mechanisms can be defined in the following dictionary object. -```python -transitions = { - "m1": { - "behaviors": { - "b1": b1m1, - "b2": b2m1 - }, - "states": { - "s1": s1m1, - "s2": s2m1 - } - }, - "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 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} - -def b1m2(step, sL, s): - return {'param1': 'a', 'param2': 2} - -def b1m3(step, sL, s): - return {'param1': ['c'], 'param2': np.array([10, 100])} -``` -State mechanism per transition on the other hand takes in the output of behavior mechanisms (`_input`) and returns a tuple of the name of the variable and the new value for the variable. Some examples of a state mechanism per transition are as follows. Note that each state mechanism is supposed to change one state variable at a time. Changes to multiple state variables should be done in separate mechanisms. -```python -def s1m1(step, sL, s, _input): - y = 's1' - x = _input['param1'] + 1 - return (y, x) - -def s1m2(step, sL, s, _input): - y = 's1' - x = _input['param1'] - return (y, x) -``` -Exogenous state update functions, for example `es3p1`, `es4p2` and `es5p2` below, update exogenous variables at every timestamp. Note that every timestamp is consist of all behaviors and state mechanisms in the order defined in `transitions` dictionary. If `exo_update_per_ts` is not used, exogenous state updates will be applied at every mechanism step (`m1`, `m2`, etc). Otherwise, exogenous state updates will only be applied once for every timestamp after all the mechanism steps are executed. -```python -exogenous_states = exo_update_per_ts( - { - "s3": es3p1, - "s4": es4p2, - "timestamp": es5p2 - } -) -``` -To model randomness, we should also define pseudorandom seeds in the configuration as follows. -```python -seed = { - 'z': np.random.RandomState(1), - 'a': np.random.RandomState(2), - 'b': np.random.RandomState(3), - 'c': np.random.RandomState(3) -} -``` -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 - -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, 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 cadCAD. -```python -def env_a(x): - return x -def env_b(x): - return 10 - -env_processes = { - "s3": env_a, - "s4": proc_trigger('2018-10-01 15:16:25', env_b) -} -``` - -Lastly, we set the overall simulation configuration and initialize the `Configuration` class with the following. `T` denotes the time range and `N` refers to the number of simulation runs. Each run will start from the same initial states and run for `T` time range. Every transition is consist of behaviors, state mechanisms, exogenous updates, and potentially environmental processes. All of these happen within one time step in the simulation. -```python -sim_config = { - "N": 2, - "T": range(5) -} - -configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms)) -``` \ No newline at end of file diff --git a/documentation/Policy_Aggregation.md b/documentation/Policy_Aggregation.md index 64b4b1e..b80db6b 100644 --- a/documentation/Policy_Aggregation.md +++ b/documentation/Policy_Aggregation.md @@ -56,5 +56,22 @@ append_configs( ) ``` -#### [Example Configuration](link) -#### [Example Results](link) \ No newline at end of file +#### Example +##### * [System Model Configuration](https://github.com/BlockScience/cadCAD-Tutorials/blob/master/Documentation/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 | ++----+---------------------------------------------+-------+------+-----------+------------+ +``` diff --git a/documentation/Simulation_Configuration.md b/documentation/Simulation_Configuration.md index 304a01d..0656244 100644 --- a/documentation/Simulation_Configuration.md +++ b/documentation/Simulation_Configuration.md @@ -3,12 +3,16 @@ 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). +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) +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](https://github.com/BlockScience/cadCAD-Tutorials/blob/master/Documentation/Simulation_Execution.md) by cadCAD +`append_configs`, stores a **Simulation Configuration** to be [Executed](/JS4Q9oayQASihxHBJzz4Ug) by cadCAD ```python from cadCAD.configuration import append_configs @@ -21,25 +25,15 @@ append_configs( ) ``` 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](https://github.com/BlockScience/cadCAD-Tutorials/blob/master/Documentation/Policy_Aggregation.md) - -* **sim_configs** : - - See +* **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 paramenter, we use the `config_sim` function in `cadCAD.configuration.utils` +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 @@ -59,29 +53,46 @@ append_configs( ### 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)) +>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. +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. +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](https://github.com/BlockScience/cadCAD-Tutorials/blob/master/01%20Tutorials/robot-marbles-part-4/robot-marbles-part-4.ipynb). +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](https://github.com/BlockScience/cadCAD-Tutorials/blob/master/01%20Tutorials/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. +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. +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)) +>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. +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. +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 @@ -99,41 +110,48 @@ append_configs( ) ``` ### 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: +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) +* **_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. +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](https://github.com/BlockScience/cadCAD-Tutorials/blob/master/01%20Tutorials/robot-marbles-part-2/robot-marbles-part-2.ipynb) for details on why and when to use 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](https://github.com/BlockScience/cadCAD-Tutorials/blob/master/01%20Tutorials/robot-marbles-part-2/robot-marbles-part-2.ipynb) +for details on why and when to use policy functions. The general structure of a policy function is: @@ -143,29 +161,38 @@ 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. +* **_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) +* _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. +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). +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 cannnot impact the State Update Functions and Policy Functions in that PSUB - only those in the next PSUB. +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. +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 = [ @@ -191,19 +218,23 @@ 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`. +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: +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) +* `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](link) +#### [System Simulation Execution](https://github.com/BlockScience/cadCAD-Tutorials/blob/master/documentation/Simulation_Execution.md) diff --git a/documentation/Execution.md b/documentation/Simulation_Execution.md similarity index 99% rename from documentation/Execution.md rename to documentation/Simulation_Execution.md index d8dc83b..786cb5f 100644 --- a/documentation/Execution.md +++ b/documentation/Simulation_Execution.md @@ -51,7 +51,7 @@ simulation_result = pd.DataFrame(raw_system_events) ``` ##### Example Result: System Events DataFrame -```python +``` +----+-------+------------+-----------+------+-----------+ | | run | timestep | substep | s1 | s2 | |----+-------+------------+-----------+------+-----------| diff --git a/documentation/System_Model_Parameter_Sweep.md b/documentation/System_Model_Parameter_Sweep.md index 63b7306..57df42a 100644 --- a/documentation/System_Model_Parameter_Sweep.md +++ b/documentation/System_Model_Parameter_Sweep.md @@ -68,6 +68,6 @@ sim_config = config_sim( } ) ``` - -#### [Example](link) - +#### Example +##### * [System Model Configuration](https://github.com/BlockScience/cadCAD-Tutorials/blob/master/Documentation/examples/param_sweep.py) +##### * Simulation Results: diff --git a/documentation/examples/policy_aggregation.py b/documentation/examples/policy_aggregation.py index 2807a74..38865ad 100644 --- a/documentation/examples/policy_aggregation.py +++ b/documentation/examples/policy_aggregation.py @@ -80,7 +80,7 @@ append_configs( sim_configs=sim_config, initial_state=genesis_states, partial_state_update_blocks=psubs, - policy_ops=[lambda a, b: a + b] # Default: lambda a, b: a + b , lambda y: y * 2 + policy_ops=[lambda a, b: a + b, lambda y: y * 2] # Default: lambda a, b: a + b ) exec_mode = ExecutionMode() diff --git a/documentation/examples/sys_model_A.py b/documentation/examples/sys_model_A.py index 3614291..5c54dbe 100644 --- a/documentation/examples/sys_model_A.py +++ b/documentation/examples/sys_model_A.py @@ -35,9 +35,6 @@ 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'] diff --git a/setup.py b/setup.py index 966cce7..1dfe6f9 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ 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.2.4', + version='0.3.0', description="cadCAD: a differential games based simulation software package for research, validation, and \ Computer Aided Design of economic systems", long_description=long_description, diff --git a/testing/example.py b/testing/example.py deleted file mode 100644 index 09ee3aa..0000000 --- a/testing/example.py +++ /dev/null @@ -1,20 +0,0 @@ -import unittest - -class TestStringMethods(unittest.TestCase): - - 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() \ No newline at end of file diff --git a/testing/example2.py b/testing/example2.py deleted file mode 100644 index 081ec32..0000000 --- a/testing/example2.py +++ /dev/null @@ -1,71 +0,0 @@ -from functools import reduce - -import pandas as pd -import unittest -from parameterized import parameterized -from tabulate import tabulate - -from testing.system_models.policy_aggregation import run -from testing.generic_test import make_generic_test -from testing.utils import generate_assertions_df - -raw_result, tensor_field = run.execute() -result = pd.DataFrame(raw_result) - -expected_results = { - (1, 0, 0): {'policies': {}, 's1': 0}, - (1, 1, 1): {'policies': {'policy1': 2, 'policy2': 4}, 's1': 500}, - (1, 1, 2): {'policies': {'policy1': 8, 'policy2': 8}, 's1': 2}, - (1, 1, 3): {'policies': {'policy1': 4, 'policy2': 8, 'policy3': 12}, 's1': 3}, - (1, 2, 1): {'policies': {'policy1': 2, 'policy2': 4}, 's1': 4}, - (1, 2, 2): {'policies': {'policy1': 8, 'policy2': 8}, 's1': 5}, - (1, 2, 3): {'policies': {'policy1': 4, 'policy2': 8, 'policy3': 12}, 's1': 6}, - (1, 3, 1): {'policies': {'policy1': 2, 'policy2': 4}, 's1': 7}, - (1, 3, 2): {'policies': {'policy1': 8, 'policy2': 8}, 's1': 8}, - (1, 3, 3): {'policies': {'policy1': 4, 'policy2': 8, 'policy3': 12}, 's1': 9} -} - -params = [["policy_aggregation", result, expected_results, ['policies', 's1']]] - - -class TestSequence(unittest.TestCase): - @parameterized.expand(params) - def test_validate_results(self, name, result_df, expected_reults, target_cols): - # alt for (*) Exec Debug mode - tested_df = generate_assertions_df(result_df, expected_reults, target_cols) - - erroneous = tested_df[(tested_df['test'] == False)] - for index, row in erroneous.iterrows(): - expected = expected_reults[(row['run'], row['timestep'], row['substep'])] - unexpected = {k: expected[k] for k in expected if k in row and expected[k] != row[k]} - for key in unexpected.keys(): - erroneous[f"invalid_{key}"] = unexpected[key] - # etc. - - # def etc. - - print() - print(tabulate(erroneous, headers='keys', tablefmt='psql')) - - self.assertEqual(reduce(lambda a, b: a and b, tested_df['test']), True) - - s = 'hello world' - # self.assertEqual(s.split(), 1) - # # check that s.split fails when the separator is not a string - # with self.assertRaises(AssertionError): - # tested_df[(tested_df['test'] == False)] - # erroneous = tested_df[(tested_df['test'] == False)] - # for index, row in erroneous.iterrows(): - # expected = expected_reults[(row['run'], row['timestep'], row['substep'])] - # unexpected = {k: expected[k] for k in expected if k in row and expected[k] != row[k]} - # for key in unexpected.keys(): - # erroneous[f"invalid_{key}"] = unexpected[key] - # # etc. - # - # # def etc. - # - # print() - # print(tabulate(erroneous, headers='keys', tablefmt='psql')) - -if __name__ == '__main__': - unittest.main() \ No newline at end of file diff --git a/testing/generic_test.py b/testing/generic_test.py index 796770b..810eb47 100644 --- a/testing/generic_test.py +++ b/testing/generic_test.py @@ -1,21 +1,9 @@ import unittest from parameterized import parameterized from functools import reduce -from tabulate import tabulate - -# ToDo: Exec Debug mode (*) for which state and policy updates are validated during runtime using `expected_results` -# EXAMPLE: ('state_test' T/F, 'policy_test' T/F) -# ToDo: (Sys Model Config) give `expected_results to` `Configuration` for Exec Debug mode (*) -# ToDo: (expected_results) Function to generate sys metrics keys using system model config -# ToDo: (expected_results) Function to generate target_vals given user input (apply fancy validation lib later on) - - -# ToDo: Use self.assertRaises(AssertionError) def generate_assertions_df(df, expected_results, target_cols, evaluations): - # cols = ['run', 'timestep', 'substep'] + target_cols - # print(cols) test_names = [] for eval_f in evaluations: def wrapped_eval(a, b): @@ -54,10 +42,6 @@ def make_generic_test(params): erroneous.at[index, key] = unexpected[key] # etc. - # print() - # print(f"TEST: {test_name}") - # print(tabulate(erroneous, headers='keys', tablefmt='psql')) - # ToDo: Condition that will change false to true self.assertTrue(reduce(lambda a, b: a and b, tested_df[test_name])) diff --git a/testing/system_models/external_dataset.py b/testing/system_models/external_dataset.py index 0265288..3e0087b 100644 --- a/testing/system_models/external_dataset.py +++ b/testing/system_models/external_dataset.py @@ -3,7 +3,7 @@ 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')) +df = SilentDF(pd.read_csv('/DiffyQ-SimCAD/simulations/external_data/output.csv')) def query(s, df): @@ -21,8 +21,7 @@ def p2(_g, substep, sL, s): 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 +# 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()} diff --git a/testing/system_models/historical_state_access.py b/testing/system_models/historical_state_access.py index 8f88e85..1f5db26 100644 --- a/testing/system_models/historical_state_access.py +++ b/testing/system_models/historical_state_access.py @@ -1,7 +1,5 @@ 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 = {}, {} diff --git a/testing/system_models/param_sweep.py b/testing/system_models/param_sweep.py index fabb450..1f7f4ad 100644 --- a/testing/system_models/param_sweep.py +++ b/testing/system_models/param_sweep.py @@ -61,11 +61,6 @@ for m in psu_steps: psu_block[m]["variables"]['sweeped'] = var_timestep_trigger(y='sweeped', f=sweeped) -# 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 = { 'alpha': 0, @@ -75,11 +70,9 @@ genesis_states = { } # Environment Process -# ToDo: Validate - make env proc trigger field agnostic env_process['sweeped'] = 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, @@ -87,11 +80,6 @@ sim_config = config_sim( "M": g, # Optional } ) -# print() -# pp.pprint(g) -# print() -# pp.pprint(sim_config) - # New Convention partial_state_update_blocks = psub_list(psu_block, psu_steps) diff --git a/testing/system_models/policy_aggregation.py b/testing/system_models/policy_aggregation.py index e2be18b..4849ede 100644 --- a/testing/system_models/policy_aggregation.py +++ b/testing/system_models/policy_aggregation.py @@ -73,14 +73,11 @@ sim_config = config_sim( ) -# Aggregation == Reduce Map / Reduce Map Aggregation -# ToDo: subsequent functions should accept the entire datastructure -# 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, - policy_ops=[lambda a, b: a + b, lambda y: y * 2] # Default: lambda a, b: a + b ToDO: reduction function requires high lvl explanation + policy_ops=[lambda a, b: a + b, lambda y: y * 2] # Default: lambda a, b: a + b ) diff --git a/testing/system_models/udo.py b/testing/system_models/udo.py deleted file mode 100644 index 1415908..0000000 --- a/testing/system_models/udo.py +++ /dev/null @@ -1,185 +0,0 @@ -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 - -from cadCAD.engine import ExecutionMode, ExecutionContext, Executor -from cadCAD import configs - - -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 - - -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) - 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() - - -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) diff --git a/testing/tests/external_test.py b/testing/tests/external_dataset.py similarity index 83% rename from testing/tests/external_test.py rename to testing/tests/external_dataset.py index 1d86a3e..563d577 100644 --- a/testing/tests/external_test.py +++ b/testing/tests/external_dataset.py @@ -1,34 +1,22 @@ import unittest -from pprint import pprint 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 from testing.generic_test import make_generic_test -from testing.utils import gen_metric_dict exec_mode = ExecutionMode() print("Simulation Execution: Single Configuration") print() -first_config = configs # only contains config1 +first_config = configs 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(tabulate(result, headers='keys', tablefmt='psql')) - -# cols = ['run', 'substep', 'timestep', 'increment', 'external_data', 'policies'] -# result = result[cols] -# -# metrics = gen_metric_dict(result, ['increment', 'external_data', 'policies']) -# # -# pprint(metrics) def get_expected_results(run): return { @@ -109,6 +97,8 @@ expected_results.update(expected_results_2) def row(a, b): return a == b + + params = [["external_dataset", result, expected_results, ['increment', 'external_data', 'policies'], [row]]] @@ -118,10 +108,3 @@ class GenericTest(make_generic_test(params)): if __name__ == '__main__': unittest.main() - -# print() -# print("Tensor Field: config1") -# print(tabulate(tensor_field, headers='keys', tablefmt='psql')) -# print("Output:") -# print(tabulate(result, headers='keys', tablefmt='psql')) -# print() diff --git a/testing/tests/historical_state_access.py b/testing/tests/historical_state_access.py index ffc2d95..13ae394 100644 --- a/testing/tests/historical_state_access.py +++ b/testing/tests/historical_state_access.py @@ -1,7 +1,6 @@ import unittest import pandas as pd - from cadCAD.engine import ExecutionMode, ExecutionContext, Executor from testing.generic_test import make_generic_test from testing.system_models import historical_state_access @@ -14,7 +13,6 @@ run = Executor(exec_context=single_proc_ctx, configs=configs) raw_result, tensor_field = run.execute() result = pd.DataFrame(raw_result) -# ToDo: Discrepance not reported fot collection values. Needs custom test for collection values expected_results = { (1, 0, 0): {'x': 0, 'nonexsistant': [], 'last_x': [], '2nd_to_last_x': [], '3rd_to_last_x': [], '4th_to_last_x': []}, (1, 1, 1): {'x': 1, diff --git a/testing/tests/multi_config_test.py b/testing/tests/multi_config_test.py deleted file mode 100644 index c668773..0000000 --- a/testing/tests/multi_config_test.py +++ /dev/null @@ -1,56 +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 -from testing.utils import gen_metric_dict - -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) - - -def get_expected_results_1(run): - return { - (run, 0, 0): {'s1': 0, 's2': 0.0, 's3': 5}, - (run, 1, 1): {'s1': 1, 's2': 4, 's3': 5}, - (run, 1, 2): {'s1': 2, 's2': 6, 's3': 5}, - (run, 1, 3): {'s1': 3, 's2': [30, 300], 's3': 5}, - (run, 2, 1): {'s1': 4, 's2': 4, 's3': 5}, - (run, 2, 2): {'s1': 5, 's2': 6, 's3': 5}, - (run, 2, 3): {'s1': 6, 's2': [30, 300], 's3': 5}, - (run, 3, 1): {'s1': 7, 's2': 4, 's3': 5}, - (run, 3, 2): {'s1': 8, 's2': 6, 's3': 5}, - (run, 3, 3): {'s1': 9, 's2': [30, 300], 's3': 5}, - (run, 4, 1): {'s1': 10, 's2': 4, 's3': 5}, - (run, 4, 2): {'s1': 11, 's2': 6, 's3': 5}, - (run, 4, 3): {'s1': 12, 's2': [30, 300], 's3': 5}, - (run, 5, 1): {'s1': 13, 's2': 4, 's3': 5}, - (run, 5, 2): {'s1': 14, 's2': 6, 's3': 5}, - (run, 5, 3): {'s1': 15, 's2': [30, 300], 's3': 5}, - } - -expected_results_1 = {} -expected_results_A = get_expected_results_1(1) -expected_results_B = get_expected_results_1(2) -expected_results_1.update(expected_results_A) -expected_results_1.update(expected_results_B) - -expected_results_2 = {} - -# 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() - print(gen_metric_dict) - i += 1 diff --git a/testing/tests/param_sweep.py b/testing/tests/param_sweep.py index a87dab3..4fca5e1 100644 --- a/testing/tests/param_sweep.py +++ b/testing/tests/param_sweep.py @@ -71,15 +71,3 @@ class GenericTest(make_generic_test(params)): if __name__ == '__main__': unittest.main() - -# 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 \ No newline at end of file diff --git a/testing/tests/policy_aggregation.py b/testing/tests/policy_aggregation.py index 657b6e6..a864f93 100644 --- a/testing/tests/policy_aggregation.py +++ b/testing/tests/policy_aggregation.py @@ -26,14 +26,18 @@ expected_results = { (1, 3, 3): {'policies': {'policy1': 4, 'policy2': 8, 'policy3': 12}, 's1': 9} } + def row(a, b): return a == b + + params = [["policy_aggregation", result, expected_results, ['policies', 's1'], [row]]] class GenericTest(make_generic_test(params)): pass + if __name__ == '__main__': unittest.main() diff --git a/testing/tests/udo.py b/testing/tests/udo.py deleted file mode 100644 index ea4b42a..0000000 --- a/testing/tests/udo.py +++ /dev/null @@ -1,39 +0,0 @@ -import unittest -import ctypes -from copy import deepcopy -from pprint import pprint - -import pandas as pd -from tabulate import tabulate - -from testing.generic_test import make_generic_test -from testing.system_models.udo import run -from testing.utils import generate_assertions_df, gen_metric_dict - -raw_result, tensor_field = run.execute() -result = pd.DataFrame(raw_result) - -cols = ['increment', 'state_udo', 'state_udo_perception_tracker', - 'state_udo_tracker', 'timestamp', 'udo_policies', 'udo_policy_tracker'] - - -# print(list(result.columns) -# ctypes.cast(id(a), ctypes.py_object).value -# pprint(gen_metric_dict(result, cols)) -d = gen_metric_dict(result, cols) -pprint(d) - -# for k1, v1 in d: -# print(v1) -# d_copy = deepcopy(d) -# for k, v in d_copy.items(): -# # print(d[k]['state_udo']) # = -# print(ctypes.cast(id(v['state_udo']['mem_id']), ctypes.py_object).value) - - -# pprint(d_copy) - -# df = generate_assertions_df(result, d, cols) -# -# print(tabulate(df, headers='keys', tablefmt='psql')) -# \ No newline at end of file From 7b428ddb8138937bef8a651b169043965e4722e4 Mon Sep 17 00:00:00 2001 From: Markus Buhatem Koch <34865315+markusbkoch@users.noreply.github.com> Date: Thu, 22 Aug 2019 15:06:17 -0300 Subject: [PATCH 9/9] relative link --- documentation/Policy_Aggregation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/Policy_Aggregation.md b/documentation/Policy_Aggregation.md index b80db6b..f9b24bf 100644 --- a/documentation/Policy_Aggregation.md +++ b/documentation/Policy_Aggregation.md @@ -57,7 +57,7 @@ append_configs( ``` #### Example -##### * [System Model Configuration](https://github.com/BlockScience/cadCAD-Tutorials/blob/master/Documentation/examples/policy_aggregation.py) +##### * [System Model Configuration](examples/policy_aggregation.py) ##### * Simulation Results: ``` +----+---------------------------------------------+-------+------+-----------+------------+