diff --git a/__pycache__/controller_db.cpython-38.pyc b/__pycache__/controller_db.cpython-38.pyc index eff0ada..0fed664 100644 Binary files a/__pycache__/controller_db.cpython-38.pyc and b/__pycache__/controller_db.cpython-38.pyc differ diff --git a/__pycache__/model.cpython-38.pyc b/__pycache__/model.cpython-38.pyc index 26ebd3f..f02fad2 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 c3e0a71..5fed76e 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 e96ccfd..d8c4bc9 100644 --- a/controller_db.py +++ b/controller_db.py @@ -118,6 +118,7 @@ class ControllerDB: 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, cap_limit_prob_type, cap_limit_level, diff_new_conn, diff_remove, proactive_ratio): e = Experiment( @@ -135,6 +136,8 @@ class ControllerDB: 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, cap_limit_prob_type=cap_limit_prob_type, cap_limit_level=cap_limit_level, diff_new_conn=diff_new_conn, diff --git a/model.py b/model.py index 296a2d2..88431b7 100644 --- a/model.py +++ b/model.py @@ -21,6 +21,8 @@ class Model(ap.Model): 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.flt_netw_sply_prf_size = float(self.p.netw_sply_prf_size) + self.int_netw_cust_prf_n = int(self.p.netw_cust_prf_n) + self.flt_netw_cust_prf_size = float(self.p.netw_cust_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) @@ -65,54 +67,54 @@ class Model(ap.Model): firm_prod_labels_dict[code] = firm_prod.loc[code].to_dict() nx.set_node_attributes(G_FirmProd, firm_prod_labels_dict) - # # add edge to G_firm according to G_bom - # for node in nx.nodes(G_Firm): - # lst_pred_product_code = [] - # for product_code in G_Firm.nodes[node]['Product_Code']: - # lst_pred_product_code += list(G_bom.predecessors(product_code)) - # lst_pred_product_code = list(set(lst_pred_product_code)) - # # to generate consistant graph - # lst_pred_product_code = list(sorted(lst_pred_product_code)) - # for pred_product_code in lst_pred_product_code: - # # for each product predecessor (component) the firm need - # # get a list of firm producing this component - # lst_pred_firm = \ - # Firm['Code'][Firm[pred_product_code] == 1].to_list() - # lst_pred_firm_size_damp = \ - # [G_Firm.nodes[pred_firm]['Revenue_Log'] ** - # self.flt_netw_sply_prf_size - # for pred_firm in lst_pred_firm] - # lst_prob = \ - # [size_damp / sum(lst_pred_firm_size_damp) - # for size_damp in lst_pred_firm_size_damp] - # # select multiple supplier (multi-sourcing) - # n_pred_firm = self.int_netw_sply_prf_n - # if n_pred_firm > len(lst_pred_firm): - # n_pred_firm = len(lst_pred_firm) - # lst_choose_firm = self.nprandom.choice(lst_pred_firm, - # n_pred_firm, - # replace=False, - # p=lst_prob) - # lst_add_edge = [(pred_firm, node, - # {'Product': pred_product_code}) - # for pred_firm in lst_choose_firm] - # G_Firm.add_edges_from(lst_add_edge) + # add edge to G_firm according to G_bom + for node in nx.nodes(G_Firm): + lst_pred_product_code = [] + for product_code in G_Firm.nodes[node]['Product_Code']: + lst_pred_product_code += list(G_bom.predecessors(product_code)) + lst_pred_product_code = list(set(lst_pred_product_code)) + # to generate consistant graph + lst_pred_product_code = list(sorted(lst_pred_product_code)) + for pred_product_code in lst_pred_product_code: + # for each product predecessor (component) the firm need + # get a list of firm producing this component + lst_pred_firm = \ + Firm['Code'][Firm[pred_product_code] == 1].to_list() + lst_pred_firm_size_damp = \ + [G_Firm.nodes[pred_firm]['Revenue_Log'] ** + self.flt_netw_sply_prf_size + for pred_firm in lst_pred_firm] + lst_prob = \ + [size_damp / sum(lst_pred_firm_size_damp) + for size_damp in lst_pred_firm_size_damp] + # select multiple supplier (multi-sourcing) + n_pred_firm = self.int_netw_sply_prf_n + if n_pred_firm > len(lst_pred_firm): + n_pred_firm = len(lst_pred_firm) + lst_choose_firm = self.nprandom.choice(lst_pred_firm, + n_pred_firm, + replace=False, + p=lst_prob) + lst_add_edge = [(pred_firm, node, + {'Product': pred_product_code}) + for pred_firm in lst_choose_firm] + G_Firm.add_edges_from(lst_add_edge) - # # graph firm prod - # set_node_prod_code = set(G_Firm.nodes[node]['Product_Code']) - # set_pred_succ_code = set(G_bom.successors(pred_product_code)) - # lst_use_pred_prod_code = list( - # set_node_prod_code & set_pred_succ_code) - # for pred_firm in lst_choose_firm: - # pred_node = [n for n, v in G_FirmProd.nodes(data=True) - # if v['Firm_Code'] == pred_firm and - # v['Product_Code'] == pred_product_code][0] - # for use_pred_prod_code in lst_use_pred_prod_code: - # current_node = \ - # [n for n, v in G_FirmProd.nodes(data=True) - # if v['Firm_Code'] == node and - # v['Product_Code'] == use_pred_prod_code][0] - # G_FirmProd.add_edge(pred_node, current_node) + # graph firm prod + set_node_prod_code = set(G_Firm.nodes[node]['Product_Code']) + set_pred_succ_code = set(G_bom.successors(pred_product_code)) + lst_use_pred_prod_code = list( + set_node_prod_code & set_pred_succ_code) + for pred_firm in lst_choose_firm: + pred_node = [n for n, v in G_FirmProd.nodes(data=True) + if v['Firm_Code'] == pred_firm and + v['Product_Code'] == pred_product_code][0] + for use_pred_prod_code in lst_use_pred_prod_code: + current_node = \ + [n for n, v in G_FirmProd.nodes(data=True) + if v['Firm_Code'] == node and + v['Product_Code'] == use_pred_prod_code][0] + G_FirmProd.add_edge(pred_node, current_node) # nx.to_pandas_adjacency(G_Firm).to_csv('adj_g_firm.csv') # nx.to_pandas_adjacency(G_FirmProd).to_csv('adj_g_firm_prod.csv') @@ -121,6 +123,12 @@ class Model(ap.Model): if G_Firm.degree(node) == 0: for product_code in G_Firm.nodes[node]['Product_Code']: # unconnect node does not have possible suppliers + # current node in graph firm prod + current_node = \ + [n for n, v in G_FirmProd.nodes(data=True) + if v['Firm_Code'] == node and + v['Product_Code'] == product_code][0] + lst_succ_product_code = list( G_bom.successors(product_code)) # different from for different types of product, @@ -131,8 +139,8 @@ class Model(ap.Model): # for each product successor (finished product) # the firm sells to, # get a list of firm producing this finished product - lst_succ_firm = \ - Firm['Code'][Firm[succ_product_code] == 1].to_list() + lst_succ_firm = Firm['Code'][ + Firm[succ_product_code] == 1].to_list() lst_succ_firm_size_damp = \ [G_Firm.nodes[succ_firm]['Revenue_Log'] ** self.flt_netw_cust_prf_size @@ -149,27 +157,17 @@ class Model(ap.Model): replace=False, p=lst_prob) lst_add_edge = [(node, succ_firm, - {'Product': pred_product_code}) + {'Product': product_code}) for succ_firm in lst_choose_firm] G_Firm.add_edges_from(lst_add_edge) # graph firm prod - set_node_prod_code = set( - G_Firm.nodes[node]['Product_Code']) - set_pred_succ_code = set( - G_bom.successors(pred_product_code)) - lst_use_pred_prod_code = list( - set_node_prod_code & set_pred_succ_code) - for pred_firm in lst_choose_firm: - pred_node = [n for n, v in G_FirmProd.nodes(data=True) - if v['Firm_Code'] == pred_firm and - v['Product_Code'] == pred_product_code][0] - for use_pred_prod_code in lst_use_pred_prod_code: - current_node = \ - [n for n, v in G_FirmProd.nodes(data=True) - if v['Firm_Code'] == node and - v['Product_Code'] == use_pred_prod_code][0] - G_FirmProd.add_edge(pred_node, current_node) + for succ_firm in lst_choose_firm: + succ_node = \ + [n for n, v in G_FirmProd.nodes(data=True) + if v['Firm_Code'] == succ_firm and + v['Product_Code'] == succ_product_code][0] + G_FirmProd.add_edge(current_node, succ_node) self.sample.g_firm = json.dumps(nx.adjacency_data(G_Firm)) self.firm_network = ap.Network(self, G_Firm) @@ -336,10 +334,10 @@ class Model(ap.Model): 'Product': di_supp_prod.code }) ]) - # print(f"proactive add {select_cand.code} to " - # f"{pro_firm.code} " - # f"for {di_supp_node['Firm_Code']} " - # f"{di_supp_node['Product_Code']}") + print(f"proactive add {select_cand.code} to " + f"{pro_firm.code} " + f"for {di_supp_node['Firm_Code']} " + f"{di_supp_node['Product_Code']}") # change capacity select_cand.dct_prod_capacity[di_supp_prod] -= 1 break diff --git a/network.png b/network.png index 051c121..1ff41b0 100644 Binary files a/network.png and b/network.png differ diff --git a/oa_without_exp.csv b/oa_without_exp.csv index d810132..72b9261 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 -0,0,0,0,0,0,0,0,0,0,0,0,0 +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 diff --git a/orm.py b/orm.py index 52e6679..1e6121e 100644 --- a/orm.py +++ b/orm.py @@ -62,6 +62,8 @@ class Experiment(Base): firm_acc_prf_conn = Column(Boolean, nullable=False) netw_sply_prf_n = Column(Integer, nullable=False) netw_sply_prf_size = Column(DECIMAL(8, 4), nullable=False) + netw_cust_prf_n = Column(Integer, nullable=False) + netw_cust_prf_size = Column(DECIMAL(8, 4), nullable=False) cap_limit_prob_type = Column(String(16), nullable=False) cap_limit_level = Column(DECIMAL(8, 4), nullable=False) diff_new_conn = Column(DECIMAL(8, 4), nullable=False) diff --git a/xv_with_exp.csv b/xv_with_exp.csv index f996f82..534ea83 100644 --- a/xv_with_exp.csv +++ b/xv_with_exp.csv @@ -1,4 +1,4 @@ -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,cap_limit_prob_type,cap_limit_level,diff_new_conn,diff_remove,proactive_ratio -15,2,2,True,2,True,3,2,uniform,5,0.5,0.5,0.4 -10,1,1,False,1,False,2,1,normal,10,1,1,0.6 -5,0.5,0.5,,0.5,,1,0.5,,15,2,2,0.8 +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,cap_limit_prob_type,cap_limit_level,diff_new_conn,diff_remove,proactive_ratio +15,2,2,TRUE,2,TRUE,3,2,3,2,uniform,5,0.5,0.5,0.4 +10,1,1,FALSE,1,FALSE,2,1,2,1,normal,10,1,1,0.6 +5,0.5,0.5,,0.5,,1,0.5,1,0.5,,15,2,2,0.8 diff --git a/xv_without_exp.csv b/xv_without_exp.csv index f501b47..f0c1f5d 100644 --- a/xv_without_exp.csv +++ b/xv_without_exp.csv @@ -1,4 +1,2 @@ -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,cap_limit_prob_type,cap_limit_level,diff_new_conn,diff_remove,proactive_ratio -10,1,1,True,1,True,1,1,uniform,10,1,1,0.9 -,,,,,,,,,,,, -,,,,,,,,,,,, +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,cap_limit_prob_type,cap_limit_level,diff_new_conn,diff_remove,proactive_ratio +10,1,1,TRUE,1,TRUE,2,1,2,1,uniform,10,1,1,0.9