experiment design

This commit is contained in:
HaoYizhi 2023-05-15 16:19:05 +08:00
parent 5d42d74639
commit a5f278c4cb
12 changed files with 37 additions and 13 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -6,9 +6,7 @@ meta_seed: 0
test: # only for test scenarios test: # only for test scenarios
n_sample: 1 n_sample: 1
n_iter: 20 n_iter: 20
n_max_trial: 10
not_test: # normal scenarios not_test: # normal scenarios
n_sample: 50 n_sample: 50
n_iter: 20 n_iter: 20
n_max_trial: 10

View File

@ -72,20 +72,34 @@ class ControllerDB:
g_product_js = json.dumps(nx.adjacency_data(g_bom)) g_product_js = json.dumps(nx.adjacency_data(g_bom))
# insert exp # insert exp
for idx_exp, dct in enumerate(list_dct): df_xv = pd.read_csv("xv.csv", index_col=None)
self.add_experiment_1(idx_exp, self.dct_parameter['n_max_trial'], # read the OA table
dct, g_product_js) # same g_bom for all exp df_oa = pd.read_fwf("oa.csv", index_col=None)
print(f'Inserted experiment for exp {idx_exp}!') for idx_scenario, row in df_oa.iterrows():
dct_exp_para = {}
for idx_col, para_level in enumerate(row):
dct_exp_para[df_xv.columns[idx_col]] = \
df_xv.iloc[para_level, idx_col]
# different initial removal
for idx_init_removal, dct_init_removal in enumerate(list_dct):
self.add_experiment_1(idx_scenario,
idx_init_removal,
dct_init_removal,
g_product_js,
**dct_exp_para)
print(f"Inserted experiment for scenario {idx_scenario}, "
f"init_removal {idx_init_removal}!")
def add_experiment_1(self, idx_exp, n_max_trial, def add_experiment_1(self, idx_scenario, idx_init_removal,
dct_lst_init_remove_firm_prod, g_bom): dct_lst_init_remove_firm_prod, g_bom, n_max_trial):
e = Experiment( e = Experiment(
idx_exp=idx_exp, idx_scenario=idx_scenario,
idx_init_removal=idx_init_removal,
n_sample=int(self.dct_parameter['n_sample']), n_sample=int(self.dct_parameter['n_sample']),
n_iter=int(self.dct_parameter['n_iter']), n_iter=int(self.dct_parameter['n_iter']),
n_max_trial=n_max_trial,
dct_lst_init_remove_firm_prod=dct_lst_init_remove_firm_prod, dct_lst_init_remove_firm_prod=dct_lst_init_remove_firm_prod,
g_bom=g_bom) g_bom=g_bom,
n_max_trial=n_max_trial)
db_session.add(e) db_session.add(e)
db_session.commit() db_session.commit()

View File

@ -157,6 +157,7 @@ class FirmAgent(ap.Agent):
lst_firm_size = [ lst_firm_size = [
firm.revenue_log for firm in self.model.a_lst_total_firms firm.revenue_log for firm in self.model.a_lst_total_firms
if product in firm.a_lst_product if product in firm.a_lst_product
and product not in firm.a_lst_product_removed
] ]
prod_accept = self.revenue_log / sum(lst_firm_size) prod_accept = self.revenue_log / sum(lst_firm_size)
if self.model.nprandom.choice([True, False], if self.model.nprandom.choice([True, False],

View File

@ -220,6 +220,10 @@ class Model(ap.Model):
lost_percent = n_up_product_removed / len( lost_percent = n_up_product_removed / len(
product.a_predecessors()) product.a_predecessors())
lst_size = self.a_lst_total_firms.revenue_log lst_size = self.a_lst_total_firms.revenue_log
lst_size = [firm.revenue_log for firm in self.a_lst_total_firms
if product in firm.a_lst_product
and product not in firm.a_lst_product_removed
]
std_size = (firm.revenue_log - min(lst_size) + std_size = (firm.revenue_log - min(lst_size) +
1) / (max(lst_size) - min(lst_size) + 1) 1) / (max(lst_size) - min(lst_size) + 1)
prod_remove = 1 - std_size * (1 - lost_percent) prod_remove = 1 - std_size * (1 - lost_percent)

3
oa.csv Normal file
View File

@ -0,0 +1,3 @@
X1
0
1
1 X1
2 0
3 1

5
orm.py
View File

@ -43,16 +43,17 @@ class Experiment(Base):
__tablename__ = f"{db_name_prefix}_experiment" __tablename__ = f"{db_name_prefix}_experiment"
id = Column(Integer, primary_key=True, autoincrement=True) id = Column(Integer, primary_key=True, autoincrement=True)
idx_exp = Column(Integer, nullable=False) idx_scenario = Column(Integer, nullable=False)
idx_init_removal = Column(Integer, nullable=False)
# fixed parameters # fixed parameters
n_sample = Column(Integer, nullable=False) n_sample = Column(Integer, nullable=False)
n_iter = Column(Integer, nullable=False) n_iter = Column(Integer, nullable=False)
# variables # variables
n_max_trial = Column(Integer, nullable=False)
dct_lst_init_remove_firm_prod = Column(PickleType, nullable=False) dct_lst_init_remove_firm_prod = Column(PickleType, nullable=False)
g_bom = Column(Text(4294000000), nullable=False) g_bom = Column(Text(4294000000), nullable=False)
n_max_trial = Column(Integer, nullable=False)
sample = relationship( sample = relationship(
'Sample', back_populates='experiment', lazy='dynamic') 'Sample', back_populates='experiment', lazy='dynamic')

3
xv.csv Normal file
View File

@ -0,0 +1,3 @@
n_max_trial
5
10
1 n_max_trial
2 5
3 10