def run(drop_midsteps: bool = True) -> pd.DataFrame: """ Run all experiments and return their output on the dataset column. Each line represents an iteration of the parameter-sweep combinations. """ exec_mode = ExecutionMode() multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc) run = Executor(exec_context=multi_proc_ctx, configs=configs) results = pd.DataFrame() i = 0 for raw_result, _ in run.execute(): params = configs[i].sim_config['M'] result_record = pd.DataFrame.from_records( [tuple([i for i in params.values()])], columns=list(params.keys())) df = pd.DataFrame(raw_result) # keep only last substep of each timestep if drop_midsteps: max_substep = max(df.substep) is_droppable = (df.substep != max_substep) & (df.substep != 0) df.drop(df[is_droppable].index, inplace=True) result_record['dataset'] = [df] results = results.append(result_record) i += 1 return results.reset_index()
def run_simulation(c: CommonsSimulationConfiguration): initial_conditions, simulation_parameters = bootstrap_simulation(c) exp = Experiment() exp.append_configs( initial_state=initial_conditions, partial_state_update_blocks=partial_state_update_blocks, sim_configs=simulation_parameters ) # Do not use multi_proc, breaks ipdb.set_trace() exec_mode = ExecutionMode() single_proc_context = ExecutionContext(exec_mode.local_mode) executor = Executor(single_proc_context, configs) raw_system_events, tensor_field, sessions = executor.execute() df = pd.DataFrame(raw_system_events) df_final = df[df.substep.eq(2)] result = { "timestep": list(df_final["timestep"]), "funding_pool": list(df_final["funding_pool"]), "token_supply": list(df_final["token_supply"]), "collateral": list(df_final["collateral_pool"]), "sentiment": list(df_final["sentiment"]) } return result, df_final
def run(drop_midsteps=True): ''' Definition: Run simulation ''' exec_mode = ExecutionMode() local_mode_ctx = ExecutionContext(context=exec_mode.local_mode) simulation = Executor(exec_context=local_mode_ctx, configs=configs) raw_system_events, tensor_field, sessions = simulation.execute() # Result System Events DataFrame df = pd.DataFrame(raw_system_events) config_ids = [ dict( get_M(k, v) for k, v in config.__dict__.items() if k in ['simulation_id', 'run_id', 'sim_config', 'subset_id']) for config in configs ] results = pd.DataFrame() for i, config_id in enumerate(config_ids): params = config_id['M'] result_record = pd.DataFrame.from_records( [tuple([i for i in params.values()])], columns=list(params.keys())) sub_df = df[df.subset == config_id['subset_id']] max_substep = max(sub_df.substep) is_droppable = (sub_df.substep != max_substep) & (sub_df.substep != 0) sub_df.drop(sub_df[is_droppable].index, inplace=True) result_record['dataset'] = [sub_df] results = results.append(result_record) return results.reset_index()
def run(drop_midsteps: bool = True) -> pd.DataFrame: """ Run all experiments and return their output on the dataset column. Each line represents an iteration of the parameter-sweep combinations. """ exec_mode = ExecutionMode() exec_context = ExecutionContext(exec_mode.local_mode) run = Executor(exec_context=exec_context, configs=configs) results = pd.DataFrame() (system_events, tensor_field, sessions) = run.execute() df = pd.DataFrame(system_events) results = [] for i, (_, subset_df) in enumerate(df.groupby(["simulation", "subset"])): params = configs[i].sim_config['M'] result_record = pd.DataFrame.from_records( [tuple([i for i in params.values()])], columns=list(params.keys())) # keep only last substep of each timestep if drop_midsteps: max_substep = max(subset_df.substep) is_droppable = (subset_df.substep != max_substep) is_droppable &= (subset_df.substep != 0) subset_df = subset_df.loc[~is_droppable] result_record['dataset'] = [subset_df] results.append(result_record) return pd.concat(results).reset_index()
def run(drop_midsteps: bool=True) -> pd.DataFrame: """ Run all experiments and return their output on the dataset column. Each line represents an iteration of the parameter-sweep combinations. """ exec_mode = ExecutionMode() local_proc_ctx = ExecutionContext(context=exec_mode.multi_mode) simulation = Executor(exec_context=local_proc_ctx, configs=configs) raw_system_events, tensor_field, sessions = simulation.execute() simulation_result = pd.DataFrame(raw_system_events) return simulation_result
def run(): exec_mode = ExecutionMode() multi_mode_ctx = ExecutionContext(context=exec_mode.multi_proc) simulation = Executor(exec_context=multi_mode_ctx, configs=configs) raw_system_events, tensor_field, sessions = simulation.execute() # Result System Events DataFrame df = pd.DataFrame(raw_system_events) return df
def run(drop_midsteps: bool = True) -> pd.DataFrame: """ Run all experiments and return their output on the dataset column. Each line represents an iteration of the parameter-sweep combinations. """ exec_mode = ExecutionMode() multi_mode_ctx = ExecutionContext(context=exec_mode.multi_mode) run = Executor(exec_context=multi_mode_ctx, configs=configs) raw_result, tensor_field, sessions = run.execute() results = pd.DataFrame(raw_result) return results
def run(): ''' Definition: Run simulation ''' exec_mode = ExecutionMode() local_mode_ctx = ExecutionContext(context=exec_mode.local_mode) simulation = Executor(exec_context=local_mode_ctx, configs=configs) raw_system_events, tensor_field, sessions = simulation.execute() # Result System Events DataFrame df = pd.DataFrame(raw_system_events) return df
def temp_run(drop_midsteps=True): exec_mode = ExecutionMode() multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc) run = Executor(exec_context=multi_proc_ctx, configs=configs) i = 0 results = {} for raw_result, tensor_field in run.execute(): result = pd.DataFrame(raw_result) results[i] = {} results[i]['result'] = result i += 1 return results
def main(): c = Central(300) params = Parameters(c) config = Configuration( initial_state=params.initial_state(), partial_state_update_blocks=params.partial_state_update_blocks, sim_config=simulation_parameters) exec_mode = ExecutionMode() exec_context = ExecutionContext(exec_mode.single_proc) executor = Executor(exec_context, [config]) raw_result, tensor = executor.execute() raw_result = [d for d in raw_result] plot_results(raw_result)
def run(initial_state, partial_state_update_block, sim_configs): exp = Experiment() exp.append_configs(initial_state=initial_state, partial_state_update_blocks=partial_state_update_block, sim_configs=sim_configs) # Do not use multi_proc, breaks ipdb.set_trace() exec_mode = ExecutionMode() single_proc_context = ExecutionContext(exec_mode.single_proc) executor = Executor(single_proc_context, configs) raw_system_events, tensor_field, sessions = executor.execute() df = pd.DataFrame(raw_system_events) return df
def run(input_config=configs): ''' Definition: Run simulation Parameters: input_config: Optional way to pass in system configuration ''' exec_mode = ExecutionMode() local_mode_ctx = ExecutionContext(context=exec_mode.local_mode) simulation = Executor(exec_context=local_mode_ctx, configs=input_config) raw_system_events, tensor_field, sessions = simulation.execute() # Result System Events DataFrame df = pd.DataFrame(raw_system_events) return df
def run_simulation(c: CommonsSimulationConfiguration): initial_conditions, simulation_parameters = bootstrap_simulation(c) exp = Experiment() exp.append_configs(initial_state=initial_conditions, partial_state_update_blocks=partial_state_update_blocks, sim_configs=simulation_parameters) # Do not use multi_proc, breaks ipdb.set_trace() exec_mode = ExecutionMode() single_proc_context = ExecutionContext(exec_mode.local_mode) executor = Executor(single_proc_context, configs) raw_system_events, tensor_field, sessions = executor.execute() df = pd.DataFrame(raw_system_events) return df
def run(): ''' Definition: Run simulation ''' # Single exec_mode = ExecutionMode() local_mode_ctx = ExecutionContext(context=exec_mode.local_mode) simulation = Executor(exec_context=local_mode_ctx, configs=exp.configs) raw_system_events, tensor_field, sessions = simulation.execute() # Result System Events DataFrame df = pd.DataFrame(raw_system_events) # subset to last substep df = df[df['substep'] == df.substep.max()] return df
def run(): ''' Definition: Run simulation Parameters: input_config: Optional way to pass in system configuration ''' exec_mode = ExecutionMode() local_mode_ctx = ExecutionContext(context=exec_mode.local_mode) simulation = Executor(exec_context=local_mode_ctx, configs=configs) raw_system_events, tensor_field, sessions = simulation.execute() df = (pd.DataFrame(raw_system_events)) # Clean substeps first_ind = (df.substep == 0) & (df.timestep == 0) last_ind = df.substep == max(df.substep) inds_to_drop = (first_ind | last_ind) df = df.loc[inds_to_drop].drop(columns=['substep']) # Set indexes df = df.set_index(['simulation', 'subset', 'run', 'timestep']) return df
def run_3(drop_midsteps=True): exec_mode = ExecutionMode() multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc) run = Executor(exec_context=multi_proc_ctx, configs=configs) results = pd.DataFrame() i = 0 for raw_result, _ in run.execute(): params = configs[i].sim_config['M'] result_record = pd.DataFrame.from_records( [tuple([i for i in params.values()])], columns=list(params.keys())) df = pd.DataFrame(raw_result) # keep only last substep of each timestep if drop_midsteps: max_substep = max(df.substep) is_droppable = (df.substep != max_substep) & (df.substep != 0) df.drop(df[is_droppable].index, inplace=True) result_record['dataset'] = [df] results = results.append(result_record) i += 1 return results.reset_index()
from pprint import pprint import pandas as pd from tabulate import tabulate from cadCAD.engine import ExecutionMode, ExecutionContext, Executor from simulations.regression_tests.models import sweep_config exec_mode = ExecutionMode() local_proc_ctx = ExecutionContext(context=exec_mode.local_mode) run = Executor(exec_context=local_proc_ctx, configs=sweep_config.exp.configs) raw_result, tensor_fields, _ = run.execute() result = pd.DataFrame(raw_result) print(tabulate(tensor_fields[0], headers='keys', tablefmt='psql')) print(tabulate(result, headers='keys', tablefmt='psql'))
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: local_proc_ctx = ExecutionContext(context=exec_mode.local_mode) sys_model_AB_simulation = Executor(exec_context=local_proc_ctx, configs=configs) i = 0 config_names = ['sys_model_A', 'sys_model_B'] sys_model_AB_raw_result, sys_model_AB_tensor_field, sessions = sys_model_AB_simulation.execute() sys_model_AB_result = pd.DataFrame(sys_model_AB_raw_result) print() print(f"Tensor Field:") 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()
from cadCAD.configuration import append_configs # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # The configurations above are then packaged into a `Configuration` object append_configs( initial_state= initial_conditions, #dict containing variable names and initial values partial_state_update_blocks= partial_state_update_blocks, #dict containing state update functions sim_configs=simulation_parameters #dict containing simulation parameters ) from cadCAD.engine import ExecutionMode, ExecutionContext, Executor from cadCAD import configs import pandas as pd exec_mode = ExecutionMode() multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc) run = Executor(exec_context=multi_proc_ctx, configs=configs) i = 0 for raw_result, tensor_field in run.execute(): result = pd.DataFrame(raw_result) print() print(f"Tensor Field: {type(tensor_field)}") print(tabulate(tensor_field, headers='keys', tablefmt='psql')) print(f"Output: {type(result)}") print(tabulate(result, headers='keys', tablefmt='psql')) print() i += 1
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
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()
def run_simulation(): def update_collateral_pool(params, step, sL, s, _input): commons = s["commons"] s["collateral_pool"] = commons._collateral_pool return "collateral_pool", commons._collateral_pool def update_token_supply(params, step, sL, s, _input): commons = s["commons"] s["token_supply"] = commons._token_supply return "token_supply", commons._token_supply def update_funding_pool(params, step, sL, s, _input): commons = s["commons"] s["funding_pool"] = commons._funding_pool return "funding_pool", commons._funding_pool # In[3]: # contributions = [5e5, 5e5, 2.5e5] contributions = [np.random.rand() * 10e5 for i in range(60)] token_batches, initial_token_supply = create_token_batches( contributions, 0.1, 60) commons = Commons(sum(contributions), initial_token_supply, exit_tribute=0.35) network = bootstrap_network(token_batches, 3, commons._funding_pool, commons._token_supply) initial_conditions = { "network": network, "commons": commons, "funding_pool": commons._funding_pool, "collateral_pool": commons._collateral_pool, "token_supply": commons._token_supply, "sentiment": 0.5, } partial_state_update_blocks = [ { "policies": { "generate_new_participants": GenerateNewParticipant.p_randomly, }, 'variables': { 'network': GenerateNewParticipant.su_add_to_network, 'commons': GenerateNewParticipant.su_add_investment_to_commons, } }, { "policies": {}, "variables": { "funding_pool": update_funding_pool, "collateral_pool": update_collateral_pool, "token_supply": update_token_supply, } }, { "policies": { "generate_new_proposals": GenerateNewProposal.p_randomly, }, "variables": { "network": GenerateNewProposal.su_add_to_network, } }, { "policies": { "generate_new_funding": GenerateNewFunding. p_exit_tribute_of_average_speculator_position_size, }, "variables": { "network": GenerateNewFunding.su_add_funding, } }, ] # In[4]: # TODO: make it explicit that 1 timestep is 1 day simulation_parameters = { 'T': range(150), 'N': 1, 'M': { "sentiment_decay": 0.01, # termed mu in the state update function "trigger_threshold": trigger_threshold, "min_proposal_age_days": 7, # minimum periods passed before a proposal can pass, "sentiment_sensitivity": 0.75, "alpha": 0.5, # conviction voting parameter 'min_supp': 50, # number of tokens that must be stake for a proposal to be a candidate } } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # The configurations above are then packaged into a `Configuration` object config = Configuration( initial_state= initial_conditions, # dict containing variable names and initial values # dict containing state update functions partial_state_update_blocks=partial_state_update_blocks, sim_config=simulation_parameters # dict containing simulation parameters ) exec_mode = ExecutionMode() # Do not use multi_proc, breaks ipdb.set_trace() exec_context = ExecutionContext(exec_mode.single_proc) # Pass the configuration object inside an array executor = Executor(exec_context, [config]) # The `execute()` method returns a tuple; its first elements contains the raw results raw_result, tensor = executor.execute() # In[5]: df = pd.DataFrame(raw_result) df_final = df[df.substep.eq(2)] # In[6]: df_final.plot("timestep", "collateral_pool", grid=True) df_final.plot("timestep", "token_supply", grid=True) df_final.plot("timestep", "funding_pool", grid=True)
import unittest, pandas as pd from tabulate import tabulate from testing.models import policy_aggregation as policy_agg from testing.results_comparison import dataframe_difference, compare_results from cadCAD.engine import ExecutionMode, ExecutionContext, Executor exec_mode = ExecutionMode() exec_ctx = ExecutionContext(context=exec_mode.local_mode) run = Executor(exec_context=exec_ctx, configs=policy_agg.exp.configs) raw_result, _, _ = run.execute() result_df = pd.DataFrame(raw_result) expected_df = pd.read_pickle("expected_results/policy_agg_4.pkl") result_diff = dataframe_difference(result_df, expected_df) print(tabulate(result_diff, headers='keys', tablefmt='psql')) class PolicyAggTest(compare_results(result_diff)): pass if __name__ == '__main__': unittest.main()
}, 'variables': { PATS: add_pat, } }, ] config = Configuration(initial_state=initial_conditions, #dict containing variable names and initial values partial_state_update_blocks=partial_state_update_blocks, #dict containing state update functions sim_config=simulation_parameters #dict containing simulation parameters ) exec_mode = ExecutionMode() exec_context = ExecutionContext(exec_mode.single_proc) executor = Executor(exec_context, [config]) # Pass the configuration object inside an array raw_result, tensor = executor.execute() df = pd.DataFrame(raw_result) print(df) # # Initial design V0 # In[34]: initial_conditions = { USERS: {0:{}}, # users are numerical ids and a dictionary of their current wallet PATS: [0], # pats are currently simply numerical ids CLAIMS: {}, }
'N': monte_carlo_runs, 'M': params }) from cadCAD.configuration import append_configs # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # The configurations above are then packaged into a `Configuration` object append_configs( initial_state= initial_conditions, #dict containing variable names and initial values partial_state_update_blocks= partial_state_update_blocks, #dict containing state update functions sim_configs=simulation_parameters #dict containing simulation parameters ) from cadCAD.engine import ExecutionMode, ExecutionContext, Executor 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) raw_result, tensor = run.execute() #import pandas as pd #df = pd.DataFrame(raw_result) # exec_mode = ExecutionMode() # exec_context = ExecutionContext(context=exec_mode.multi_proc) # # run = Executor(exec_context=exec_context, configs=configs) # executor = Executor(exec_context, configs) # Pass the configuration object inside an array # raw_result, tensor = executor.execute() # The `main()` method returns a tuple; its first elements contains the raw results
# `N` is the number of times the simulation will be run (Monte Carlo runs) time_periods_per_run = 250 monte_carlo_runs = 1 simulation_parameters = { 'T': range(time_periods_per_run), 'N': monte_carlo_runs, 'M': params } from cadCAD.engine import ExecutionMode, ExecutionContext, Executor from cadCAD.configuration import append_configs from cadCAD import configs # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # The configurations above are then packaged into a `Configuration` object config = append_configs( initial_state= initial_conditions, #dict containing variable names and initial values partial_state_update_blocks= partial_state_update_blocks, #dict containing state update functions sim_configs=simulation_parameters #dict containing simulation parameters ) exec_mode = ExecutionMode() exec_context = ExecutionContext(context=exec_mode.multi_proc) run = Executor(exec_context=exec_context, configs=configs) executor = Executor(exec_context, configs) # Pass the configuration object inside an array raw_result, tensor = executor.execute( ) # The `main()` method returns a tuple; its first elements contains the raw results
def cadcad(): try: n = getInteger('participants') #initial participants m = getInteger('proposals') #initial proposals alpha = getFloat('alpha') beta = getFloat('beta') exit_tribute = getFloat('exit_tribute') theta = getFloat('theta') initial_sentiment = 0.1 # getFloat('initial_sentiment') hatch_price = 0.1 # getFloat('hatch_price') kappa = 6 # getFloat('kappa') rho = 0.05 # getFloat('rho') except Exception as err: return str(err), 422 #initializer network, initial_supply, total_requested = initialize_network(n, m) initial_funds = total_funds_given_total_supply(initial_supply, theta, hatch_price) initial_reserve, invariant, starting_price = initialize_bonding_curve( initial_supply, initial_price=hatch_price, kappa=kappa, theta=theta) initial_conditions = { 'supply': initial_supply, 'funds': initial_funds, 'reserve': initial_reserve, 'spot_price': starting_price, 'sentiment': initial_sentiment, 'network': network } def trigger_threshold(requested, funds, supply, beta=beta, rho=rho): share = requested / funds if share < beta: return rho * supply / (beta - share)**2 else: return np.inf params = { 'sensitivity': [.75], 'tmin': [7], #unit days; minimum periods passed before a proposal can pass 'min_supp': [ 50 ], #number of tokens that must be stake for a proposal to be a candidate 'sentiment_decay': [.01], #termed mu in the state update function 'alpha': [alpha], 'base_completion_rate': [100], 'base_failure_rate': [200], 'trigger_func': [trigger_threshold], 'kappa': [kappa], #bonding curve curvature 'invariant': [invariant], #set by bonding curve choices 'tax_rate': [exit_tribute] } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Settings of general simulation parameters, unrelated to the system itself # `T` is a range with the number of discrete units of time the simulation will run for; # `N` is the number of times the simulation will be run (Monte Carlo runs) time_periods_per_run = 100 monte_carlo_runs = 1 from cadCAD.configuration.utils import config_sim simulation_parameters = config_sim({ 'T': range(time_periods_per_run), 'N': monte_carlo_runs, 'M': params }) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # The Partial State Update Blocks partial_state_update_blocks = [ { 'policies': { #new proposals or new participants 'random': driving_process }, 'variables': { 'network': update_network, 'funds': increment_funds, 'supply': increment_supply, 'reserve': increment_reserve } }, { 'policies': { 'completion': check_progress #see if any of the funded proposals completes }, 'variables': { # The following state variables will be updated simultaneously 'sentiment': update_sentiment_on_completion, #note completing decays sentiment, completing bumps it 'network': complete_proposal #book-keeping } }, { 'policies': { 'release': trigger_function #check each proposal to see if it passes }, 'variables': { # The following state variables will be updated simultaneously 'funds': decrement_funds, #funds expended 'sentiment': update_sentiment_on_release, #releasing funds can bump sentiment 'network': update_proposals #reset convictions, and participants sentiments #update based on affinities } }, { 'policies': { #currently naive decisions; future: strategic 'participants_act': participants_decisions, #high sentiment, high affinity =>buy #low sentiment, low affinities => burn #assign tokens to top affinities }, 'variables': { 'supply': update_supply, #book-keeping from participants decisions 'reserve': update_reserve, #funds under the bonding curve 'spot_price': update_price, #new bonding curve spot price 'funds': update_funds, #capture taxes 'network': update_tokens #update everyones holdings #and their conviction for each proposal } } ] from cadCAD import configs configs.clear() from cadCAD.configuration import append_configs # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # The configurations above are then packaged into a `Configuration` object append_configs( initial_state= initial_conditions, #dict containing variable names and initial values partial_state_update_blocks= partial_state_update_blocks, #dict containing state update functions sim_configs=simulation_parameters #dict containing simulation parameters ) from tabulate import tabulate from cadCAD.engine import ExecutionMode, ExecutionContext, Executor from cadCAD import configs import pandas as pd exec_mode = ExecutionMode() multi_proc_ctx = ExecutionContext(context=exec_mode.multi_proc) run = Executor(exec_context=multi_proc_ctx, configs=configs) i = 0 verbose = False results = {} for raw_result, tensor_field in run.execute(): result = pd.DataFrame(raw_result) if verbose: print() print(f"Tensor Field: {type(tensor_field)}") print(tabulate(tensor_field, headers='keys', tablefmt='psql')) print(f"Output: {type(result)}") print(tabulate(result, headers='keys', tablefmt='psql')) print() results[i] = {} results[i]['result'] = result results[i]['simulation_parameters'] = simulation_parameters[i] i += 1 for ind in range(len(results)): r = results[ind]['result'] #print(results[ind]['simulation_parameters']) r.plot(x='timestep', y='funds') plt.savefig('static/plot8-' + str(ind) + '.png') plt.clf() fig, ax1 = plt.subplots() ax2 = ax1.twinx( ) # instantiate a second axes that shares the same x-axis df = r rdf = df[df.substep == 4].copy() rdf.plot(x='timestep', y=['funds', 'reserve', 'supply'], ax=ax1) rdf.plot(x='timestep', y='spot_price', style='--', color='red', ax=ax2, legend=False) ax2.set_ylabel('Price in xDAI per Token', color='red') ax1.set_ylabel('Quantity of Assets') ax2.tick_params(axis='y', labelcolor='red') plt.title('Summary of Local Economy') plt.savefig('static/plot9-' + str(ind) + '.png') plt.clf() return jsonify({'results': ['plot8-0.png', 'plot9-0.png']})
import pandas as pd from cadCAD.configuration.utils import config_sim from cadCAD.configuration import Experiment as cadCADExperiment from cadCAD.engine import ExecutionMode, ExecutionContext from cadCAD.engine import Executor from cadCAD import configs from tests.test_cases import benchmark_model states = benchmark_model.states state_update_blocks = benchmark_model.state_update_blocks params = benchmark_model.params TIMESTEPS = 100_000 RUNS = 5 c = config_sim({"N": RUNS, "T": range(TIMESTEPS), "M": params}) exp = cadCADExperiment() exp.append_configs(initial_state=states, partial_state_update_blocks=state_update_blocks, sim_configs=c) exec_mode = ExecutionMode() local_mode_ctx = ExecutionContext(context=exec_mode.local_mode) simulation = Executor(exec_context=local_mode_ctx, configs=configs) if __name__ == "__main__": results, _, _ = simulation.execute() assert len(results) > 0
import unittest import pandas as pd from cadCAD import configs from cadCAD.engine import ExecutionMode, ExecutionContext, Executor from testing.generic_test import make_generic_test exec_mode = ExecutionMode() first_config = configs single_proc_ctx = ExecutionContext(context=exec_mode.local_mode) run = Executor(exec_context=single_proc_ctx, configs=first_config) raw_result, tensor_field, sessions = run.execute() result = pd.DataFrame(raw_result) def get_expected_results(run): return { (0, run, 0, 0): { 'external_data': {'ds1': None, 'ds2': None, 'ds3': None}, 'increment': 0, 'policies': {'ds1': None, 'ds2': None, 'ds3': None} }, (0, run, 1, 1): { 'external_data': {'ds1': 0, 'ds2': 0, 'ds3': 1}, 'increment': 1, 'policies': {'ds1': 0, 'ds2': 0, 'ds3': 1} }, (0, run, 1, 2): { 'external_data': {'ds1': 1, 'ds2': 40, 'ds3': 5},
"variables": { # The following state variables will be updated simultaneously "box_A": update_A, "box_B": update_B, }, } ] simulation_parameters = {"T": range(10), "N": 1, "M": {}} from cadCAD.configuration import Configuration config = Configuration( initial_state= initial_conditions, # dict containing variable names and initial values partial_state_update_blocks= partial_state_update_blocks, # dict containing state update functions sim_config=simulation_parameters, # dict containing simulation parameters ) from cadCAD.engine import ExecutionMode, ExecutionContext, Executor exec_mode = ExecutionMode() exec_context = ExecutionContext(exec_mode.single_proc) executor = Executor(exec_context, [config]) ( raw_result, tensor, ) = executor.execute() # The `execute()` method returns a tuple; its first print(raw_result)