diff --git a/.vscode/launch.json b/.vscode/launch.json index 87ed9da..e582557 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -12,9 +12,9 @@ "console": "integratedTerminal", "justMyCode": true, "args": [ - "--exp", "with_exp", + "--exp", "test", "--reset_db", "True", - "--job", "24" + "--job", "1" ] } ] diff --git a/__pycache__/controller_db.cpython-38.pyc b/__pycache__/controller_db.cpython-38.pyc index 5bba38a..6b6b583 100644 Binary files a/__pycache__/controller_db.cpython-38.pyc and b/__pycache__/controller_db.cpython-38.pyc differ diff --git a/__pycache__/firm.cpython-38.pyc b/__pycache__/firm.cpython-38.pyc index efa32fd..bcbf5a1 100644 Binary files a/__pycache__/firm.cpython-38.pyc and b/__pycache__/firm.cpython-38.pyc differ diff --git a/__pycache__/model.cpython-38.pyc b/__pycache__/model.cpython-38.pyc index 0559496..1bd50d8 100644 Binary files a/__pycache__/model.cpython-38.pyc and b/__pycache__/model.cpython-38.pyc differ diff --git a/__pycache__/orm.cpython-38.pyc b/__pycache__/orm.cpython-38.pyc index ed5e1b5..eb1ad50 100644 Binary files a/__pycache__/orm.cpython-38.pyc and b/__pycache__/orm.cpython-38.pyc differ diff --git a/controller_db.py b/controller_db.py index 6acff43..b153cc4 100644 --- a/controller_db.py +++ b/controller_db.py @@ -23,7 +23,13 @@ class ControllerDB: def __init__(self, prefix, reset_flag=0): with open('conf_experiment.yaml') as yaml_file: dct_conf_experiment = yaml.full_load(yaml_file) + + assert prefix in ['test', 'without_exp', 'with_exp'], \ + "db name not in test, without_exp, with_exp" + self.is_test = prefix == 'test' + self.is_with_exp = \ + False if prefix == 'test' or prefix == 'without_exp' else True self.db_name_prefix = prefix dct_para_in_test = dct_conf_experiment[ 'test'] if self.is_test else dct_conf_experiment['not_test'] @@ -46,18 +52,8 @@ class ControllerDB: Firm.fillna(0, inplace=True) # fill dct_lst_init_remove_firm_prod - assert self.db_name_prefix in ['test', 'without_exp', 'with_exp'], \ - "db name not in test, without_exp, with_exp" - list_dct = [] - if self.db_name_prefix in ['test', 'without_exp']: - for _, row in Firm.iterrows(): - code = row['Code'] - row = row['1':] - for product_code in row.index[row == 1].to_list(): - dct = {code: [product_code]} - list_dct.append(dct) - elif self.db_name_prefix in ['with_exp']: + if self.is_with_exp: str_sql = "select e_id, count, max_max_ts, " \ "dct_lst_init_remove_firm_prod from " \ "iiabmdb.without_exp_experiment as a " \ @@ -76,13 +72,20 @@ class ControllerDB: lambda x: pickle.loads(x)) list_dct = result.loc[result['count'] >= 12, 'dct_lst_init_remove_firm_prod'].to_list() + else: + for _, row in Firm.iterrows(): + code = row['Code'] + row = row['1':] + for product_code in row.index[row == 1].to_list(): + dct = {code: [product_code]} + list_dct.append(dct) # list_dct = [{'140': ['1.4.5.1']}] # list_dct = [{'133': ['1.4.4.1']}] # list_dct = [{'2': ['1.1.3']}] # list_dct = [{'135': ['1.3.2.1']}] # list_dct = [{'79': ['2.1.3.4']}] # list_dct = [{'99': ['1.3.3']}] - # list_dct = [{'41': ['1.4.5']}] + list_dct = [{'41': ['1.4.5']}] # fill g_bom BomNodes = pd.read_csv('BomNodes.csv', index_col=0) @@ -98,9 +101,13 @@ class ControllerDB: g_product_js = json.dumps(nx.adjacency_data(g_bom)) # insert exp - df_xv = pd.read_csv("xv_with_exp.csv", index_col=None) + df_xv = pd.read_csv( + f"xv_{'with_exp' if self.is_with_exp else 'without_exp'}.csv", + index_col=None) # read the OA table - df_oa = pd.read_csv("oa_with_exp.csv", index_col=None) + df_oa = pd.read_csv( + f"oa_{'with_exp' if self.is_with_exp else 'without_exp'}.csv", + index_col=None) df_oa = df_oa.iloc[:, 0:df_xv.shape[1]] for idx_scenario, row in df_oa.iterrows(): dct_exp_para = {} @@ -119,13 +126,10 @@ class ControllerDB: def add_experiment_1(self, idx_scenario, idx_init_removal, dct_lst_init_remove_firm_prod, g_bom, - n_max_trial, crit_supplier, - firm_req_prf_size, firm_req_prf_conn, - firm_acc_prf_size, firm_acc_prf_conn, - netw_sply_prf_n, netw_sply_prf_size, - netw_cust_prf_n, netw_cust_prf_size, + n_max_trial, prf_size, prf_conn, cap_limit_prob_type, cap_limit_level, - diff_new_conn, diff_remove, proactive_ratio): + diff_new_conn, crit_supplier, diff_remove, + proactive_ratio, netw_prf_n): e = Experiment( idx_scenario=idx_scenario, idx_init_removal=idx_init_removal, @@ -134,20 +138,15 @@ class ControllerDB: dct_lst_init_remove_firm_prod=dct_lst_init_remove_firm_prod, g_bom=g_bom, n_max_trial=n_max_trial, - crit_supplier=crit_supplier, - firm_req_prf_size=firm_req_prf_size, - firm_req_prf_conn=firm_req_prf_conn, - firm_acc_prf_size=firm_acc_prf_size, - firm_acc_prf_conn=firm_acc_prf_conn, - netw_sply_prf_n=netw_sply_prf_n, - netw_sply_prf_size=netw_sply_prf_size, - netw_cust_prf_n=netw_cust_prf_n, - netw_cust_prf_size=netw_cust_prf_size, + prf_size=prf_size, + prf_conn=prf_conn, cap_limit_prob_type=cap_limit_prob_type, cap_limit_level=cap_limit_level, diff_new_conn=diff_new_conn, + crit_supplier=crit_supplier, diff_remove=diff_remove, - proactive_ratio=proactive_ratio + proactive_ratio=proactive_ratio, + netw_prf_n=netw_prf_n ) db_session.add(e) db_session.commit() diff --git a/firm.py b/firm.py index 211caac..0621625 100644 --- a/firm.py +++ b/firm.py @@ -23,12 +23,10 @@ class FirmAgent(ap.Agent): self.dct_request_prod_from_firm = {} # para - self.flt_crit_supplier = float(self.p.crit_supplier) - self.is_firm_req_prf_size = bool(self.p.firm_req_prf_size) - self.is_firm_req_prf_conn = bool(self.p.firm_req_prf_conn) - self.is_firm_acc_prf_size = bool(self.p.firm_acc_prf_size) - self.is_firm_acc_prf_conn = bool(self.p.firm_acc_prf_conn) + self.is_prf_size = self.model.is_prf_size + self.is_prf_conn = bool(self.p.prf_conn) self.flt_diff_new_conn = float(self.p.diff_new_conn) + self.flt_crit_supplier = float(self.p.crit_supplier) def remove_edge_to_cus_remove_cus_up_prod(self, remove_product): lst_out_edge = list( @@ -80,7 +78,7 @@ class FirmAgent(ap.Agent): continue # select based on connection lst_firm_connect = [] - if self.is_firm_req_prf_conn: + if self.is_prf_conn: for firm in \ self.dct_cand_alt_supply_up_prod_removed[product]: out_edges = self.model.firm_network.graph.out_edges( @@ -98,7 +96,7 @@ class FirmAgent(ap.Agent): lst_firm_connect.append(firm) if len(lst_firm_connect) == 0: # select based on size or not - if self.is_firm_req_prf_size: + if self.is_prf_size: lst_size = \ [size for size in self.dct_cand_alt_supply_up_prod_removed[ @@ -113,7 +111,7 @@ class FirmAgent(ap.Agent): self.dct_cand_alt_supply_up_prod_removed[product]) elif len(lst_firm_connect) > 0: # select based on size of connected firm or not - if self.is_firm_req_prf_size: + if self.is_prf_size: lst_firm_size = \ [firm.revenue_log for firm in lst_firm_connect] lst_prob = \ @@ -161,7 +159,7 @@ class FirmAgent(ap.Agent): elif len(lst_firm) > 1: # handling based on connection lst_firm_connect = [] - if self.is_firm_acc_prf_conn: + if self.is_prf_conn: for firm in lst_firm: out_edges = \ self.model.firm_network.graph.out_edges( @@ -182,7 +180,7 @@ class FirmAgent(ap.Agent): lst_firm_connect.append(firm) if len(lst_firm_connect) == 0: # handling based on size or not - if self.is_firm_acc_prf_size: + if self.is_prf_size: lst_firm_size = \ [firm.revenue_log for firm in lst_firm] lst_prob = \ @@ -197,7 +195,7 @@ class FirmAgent(ap.Agent): self.accept_request(select_customer, product) elif len(lst_firm_connect) > 0: # handling based on size of connected firm or not - if self.is_firm_acc_prf_size: + if self.is_prf_size: lst_firm_size = \ [firm.revenue_log for firm in lst_firm_connect] lst_prob = \ diff --git a/model.py b/model.py index 5efa281..e1fd664 100644 --- a/model.py +++ b/model.py @@ -19,14 +19,12 @@ class Model(ap.Model): self.dct_lst_remove_firm_prod = self.p.dct_lst_init_remove_firm_prod self.int_n_max_trial = int(self.p.n_max_trial) - self.int_netw_sply_prf_n = int(self.p.netw_sply_prf_n) - self.is_netw_sply_prf_size = bool(self.p.netw_sply_prf_size) - self.int_netw_cust_prf_n = int(self.p.netw_cust_prf_n) - self.is_netw_cust_prf_size = bool(self.p.netw_cust_prf_size) + self.is_prf_size = bool(self.p.prf_size) self.str_cap_limit_prob_type = str(self.p.cap_limit_prob_type) self.flt_cap_limit_level = float(self.p.cap_limit_level) self.flt_diff_remove = float(self.p.diff_remove) self.proactive_ratio = float(self.p.proactive_ratio) + self.int_netw_prf_n = int(self.p.netw_prf_n) # init graph bom G_bom = nx.adjacency_graph(json.loads(self.p.g_bom)) @@ -81,11 +79,11 @@ class Model(ap.Model): lst_pred_firm = \ Firm['Code'][Firm[pred_product_code] == 1].to_list() # select multiple supplier (multi-sourcing) - n_pred_firm = self.int_netw_sply_prf_n + n_pred_firm = self.int_netw_prf_n if n_pred_firm > len(lst_pred_firm): n_pred_firm = len(lst_pred_firm) # based on size or not - if self.is_netw_sply_prf_size: + if self.is_prf_size: lst_pred_firm_size = \ [G_Firm.nodes[pred_firm]['Revenue_Log'] for pred_firm in lst_pred_firm] @@ -147,11 +145,11 @@ class Model(ap.Model): lst_succ_firm = Firm['Code'][ Firm[succ_product_code] == 1].to_list() # select multiple customer (multi-selling) - n_succ_firm = self.int_netw_cust_prf_n + n_succ_firm = self.int_netw_prf_n if n_succ_firm > len(lst_succ_firm): n_succ_firm = len(lst_succ_firm) # based on size or not - if self.is_netw_cust_prf_size: + if self.is_prf_size: lst_succ_firm_size = \ [G_Firm.nodes[succ_firm]['Revenue_Log'] for succ_firm in lst_succ_firm] diff --git a/oa_with_exp.csv b/oa_with_exp.csv index 6034405..69f55af 100644 --- a/oa_with_exp.csv +++ b/oa_with_exp.csv @@ -1,37 +1,37 @@ X1,X2,X3,X4,X5,X6,X7,X8,X9,X10,X11,X12,X13,X14,X15,X16,X17,X18,X19,X20,X21,X22,X23 -1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 -2,1,1,1,2,1,1,2,2,2,2,2,1,2,1,1,1,1,1,2,2,2,2 -3,1,1,1,3,1,1,3,3,3,3,3,1,3,1,1,1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,1,0,1,1,0,1,1,0,1,0,1,0,0,0,0,1,1,2,0,2,2 +0,1,1,0,2,0,1,0,1,2,1,2,0,0,1,0,1,1,0,1,1,2,0 +1,1,1,0,0,0,1,1,2,0,2,0,0,1,1,0,1,1,0,2,2,0,1 +2,1,1,0,1,0,1,2,0,1,0,1,0,2,1,0,1,1,0,0,0,1,2 diff --git a/oa_without_exp.csv b/oa_without_exp.csv index 72b9261..66c1886 100644 --- a/oa_without_exp.csv +++ b/oa_without_exp.csv @@ -1,2 +1,2 @@ -X1,X2,X3,X4,X5,X6,X7,X8,X9,X10,X11,X12,X13,X14,X15 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +X1,X2,X3,X4,X5,X6,X7,X8,X9,X10 +0,0,0,0,0,0,0,0,0,0 diff --git a/orm.py b/orm.py index ff53deb..c3a25ac 100644 --- a/orm.py +++ b/orm.py @@ -55,20 +55,15 @@ class Experiment(Base): g_bom = Column(Text(4294000000), nullable=False) n_max_trial = Column(Integer, nullable=False) - firm_req_prf_size = Column(Boolean, nullable=False) - firm_req_prf_conn = Column(Boolean, nullable=False) + prf_size = Column(Boolean, nullable=False) + prf_conn = Column(Boolean, nullable=False) cap_limit_prob_type = Column(String(16), nullable=False) cap_limit_level = Column(DECIMAL(8, 4), nullable=False) - firm_acc_prf_size = Column(Boolean, nullable=False) - firm_acc_prf_conn = Column(Boolean, nullable=False) diff_new_conn = Column(DECIMAL(8, 4), nullable=False) crit_supplier = Column(DECIMAL(8, 4), nullable=False) diff_remove = Column(DECIMAL(8, 4), nullable=False) proactive_ratio = Column(DECIMAL(8, 4), nullable=False) - netw_sply_prf_n = Column(Integer, nullable=False) - netw_sply_prf_size = Column(Boolean, nullable=False) - netw_cust_prf_n = Column(Integer, nullable=False) - netw_cust_prf_size = Column(Boolean, nullable=False) + netw_prf_n = Column(Integer, nullable=False) sample = relationship( 'Sample', back_populates='experiment', lazy='dynamic') diff --git a/xv_with_exp.csv b/xv_with_exp.csv index dc41e90..5451536 100644 --- a/xv_with_exp.csv +++ b/xv_with_exp.csv @@ -1,4 +1,4 @@ -n_max_trial,firm_req_prf_size,firm_req_prf_conn,cap_limit_prob_type,cap_limit_level,firm_acc_prf_size,firm_acc_prf_conn,diff_new_conn,crit_supplier,diff_remove,proactive_ratio,netw_sply_prf_n,netw_sply_prf_size,netw_cust_prf_n,netw_cust_prf_size -15,TRUE,TRUE,uniform,5,TRUE,TRUE,0.3,2,0.5,0.3,3,TRUE,3,TRUE -10,FALSE,FALSE,normal,10,FALSE,FALSE,0.5,1,1,0.5,2,FALSE,2,FALSE -5,,,,15,,,0.7,0.5,2,0.7,1,,1, +n_max_trial,prf_size,prf_conn,cap_limit_prob_type,cap_limit_level,diff_new_conn,crit_supplier,diff_remove,proactive_ratio,netw_prf_n +15,TRUE,TRUE,uniform,5,0.3,2,0.5,0.3,3 +10,FALSE,FALSE,normal,10,0.5,1,1,0.5,2 +5,,,,15,0.7,0.5,2,0.7,1 diff --git a/xv_without_exp.csv b/xv_without_exp.csv index 065f822..2d1cd7c 100644 --- a/xv_without_exp.csv +++ b/xv_without_exp.csv @@ -1,2 +1,2 @@ -n_max_trial,firm_req_prf_size,firm_req_prf_conn,cap_limit_prob_type,cap_limit_level,firm_acc_prf_size,firm_acc_prf_conn,diff_new_conn,crit_supplier,diff_remove,proactive_ratio,netw_sply_prf_n,netw_sply_prf_size,netw_cust_prf_n,netw_cust_prf_size -10,TRUE,TRUE,uniform,10,TRUE,TRUE,0.5,1,1,0,2,TRUE,2,TRUE +n_max_trial,prf_size,prf_conn,cap_limit_prob_type,cap_limit_level,diff_new_conn,crit_supplier,diff_remove,proactive_ratio,netw_prf_n +10,TRUE,TRUE,uniform,10,0.5,1,1,0,2