Skip to content

Commit

Permalink
added viz for states, agents h and v
Browse files Browse the repository at this point in the history
  • Loading branch information
matttyb80 committed Jul 21, 2020
1 parent 6a20e2b commit 3317317
Show file tree
Hide file tree
Showing 8 changed files with 3,800 additions and 2,136 deletions.
1,663 changes: 1,663 additions & 0 deletions .ipynb_checkpoints/mutliscale_ABM_main-checkpoint.ipynb

Large diffs are not rendered by default.

Binary file modified __pycache__/run2.cpython-36.pyc
Binary file not shown.
1,663 changes: 1,663 additions & 0 deletions mutliscale_ABM_main.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions run2.py
Expand Up @@ -9,6 +9,7 @@
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', None)

from src.sim import config

def run(drop_midsteps=True):
print()
Expand Down
4 changes: 2 additions & 2 deletions src/sim/config.py
Expand Up @@ -11,8 +11,8 @@
import pandas as pd
import itertools

time_periods_per_run = 10
monte_carlo_runs = 1
time_periods_per_run = 100
monte_carlo_runs = 3
E = 0.45 # to be reviewed

KAPPA = [2]
Expand Down
2 changes: 1 addition & 1 deletion src/sim/model/parts/private_beliefs.py
Expand Up @@ -25,7 +25,7 @@ def update_private_price(params, substep, state_history, prev_state, policy_inpu
# 'period': ['N/A', 2000, 2000, 2000]
'dP': P0[0]/4,
'period': 2000,
'sigma': [.005], # , 'N/A', 'N/A', 'N/A']
'sigma': .005, # , 'N/A', 'N/A', 'N/A']
}

#params = params[0]
Expand Down
106 changes: 89 additions & 17 deletions src/sim/run.py
@@ -1,29 +1,101 @@

# # The following imports NEED to be in the exact order
# from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
# from cadCAD import configs
# import pandas as pd


# def 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)
# 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()



# The following imports NEED to be in the exact order
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from cadCAD import configs
import pandas as pd
######### ADD FOR PRINTING CONFIG
from cadCAD.configuration.utils import *
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor

from src.sim import config

exec_mode = ExecutionMode()
exec_ctx = ExecutionContext(context=exec_mode.multi_proc)
simulation = Executor(exec_context=exec_ctx, configs=configs)
raw_system_events, tensor_field, session = simulation.execute()
df = pd.DataFrame(raw_system_events)

def get_M(k, v):
if k == 'sim_config':
k, v = 'M', v['M']
return k, v
config_ids = [
dict(
get_M(k, v) for k, v in config.__dict__.items() if k in ['simulation_id', 'run_id', 'sim_config']
) for config in configs
]

def 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)
print('config_ids = ', config_ids)
result_records_list, sim_id_records = [], []
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()))
sim_ids = list(set([_id['simulation_id'] for _id in config_ids]))

# print(sim_ids)
sim_dfs = {_id: [] for _id in sim_ids}
for i, config_id in enumerate(config_ids):
sim_id, run_id = config_id['simulation_id'], config_id['run_id']
params = config_id['M']
result_record = pd.DataFrame.from_records([tuple([i for i in params.values()])], columns=list(params.keys()))

df = pd.DataFrame(raw_result)
mod_record = {'sim_id': sim_id, 'meta': result_record}
if sim_id not in sim_id_records:
sim_id_records.append(sim_id)
result_records_list.append(mod_record)

sim_id = config_id['simulation_id']
# print('sim id first loop = ',sim_id)

sub_df = df[df.simulation == config_id['simulation_id']][df.run == config_id['run_id'] + 1]
sim_dfs[sim_id].append(sub_df)
# print(sub_df[['simulation', 'run', 'substep', 'timestep']].tail(5))
# print(sub_df.tail(5))

for sim_id in sim_ids:
result_record = [d for d in result_records_list if d['sim_id'] == sim_id][0]['meta']
sim_dfs[sim_id] = pd.concat(sim_dfs[sim_id])
sub_df = sim_dfs[sim_id]

# print('sim id second loop = ',sim_id)
# 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)
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'] = [df]
# print(sub_df.head(3))
# print(sub_df.tail(3))
result_record['dataset'] = [sub_df]
results = results.append(result_record)
i += 1
return results.reset_index()
# print(sub_df[['simulation', 'run', 'substep', 'timestep']].tail(5))

return results.reset_index()

0 comments on commit 3317317

Please sign in to comment.