generate network pred + succ

This commit is contained in:
HaoYizhi 2023-06-11 13:17:34 +08:00
parent 5387268d48
commit 6d43865f79
10 changed files with 81 additions and 80 deletions

Binary file not shown.

Binary file not shown.

View File

@ -118,6 +118,7 @@ class ControllerDB:
firm_req_prf_size, firm_req_prf_conn, firm_req_prf_size, firm_req_prf_conn,
firm_acc_prf_size, firm_acc_prf_conn, firm_acc_prf_size, firm_acc_prf_conn,
netw_sply_prf_n, netw_sply_prf_size, netw_sply_prf_n, netw_sply_prf_size,
netw_cust_prf_n, netw_cust_prf_size,
cap_limit_prob_type, cap_limit_level, cap_limit_prob_type, cap_limit_level,
diff_new_conn, diff_remove, proactive_ratio): diff_new_conn, diff_remove, proactive_ratio):
e = Experiment( e = Experiment(
@ -135,6 +136,8 @@ class ControllerDB:
firm_acc_prf_conn=firm_acc_prf_conn, firm_acc_prf_conn=firm_acc_prf_conn,
netw_sply_prf_n=netw_sply_prf_n, netw_sply_prf_n=netw_sply_prf_n,
netw_sply_prf_size=netw_sply_prf_size, 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_prob_type=cap_limit_prob_type,
cap_limit_level=cap_limit_level, cap_limit_level=cap_limit_level,
diff_new_conn=diff_new_conn, diff_new_conn=diff_new_conn,

136
model.py
View File

@ -21,6 +21,8 @@ class Model(ap.Model):
self.int_n_max_trial = int(self.p.n_max_trial) 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.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.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.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_cap_limit_level = float(self.p.cap_limit_level)
self.flt_diff_remove = float(self.p.diff_remove) 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() firm_prod_labels_dict[code] = firm_prod.loc[code].to_dict()
nx.set_node_attributes(G_FirmProd, firm_prod_labels_dict) nx.set_node_attributes(G_FirmProd, firm_prod_labels_dict)
# # add edge to G_firm according to G_bom # add edge to G_firm according to G_bom
# for node in nx.nodes(G_Firm): for node in nx.nodes(G_Firm):
# lst_pred_product_code = [] lst_pred_product_code = []
# for product_code in G_Firm.nodes[node]['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(G_bom.predecessors(product_code))
# lst_pred_product_code = list(set(lst_pred_product_code)) lst_pred_product_code = list(set(lst_pred_product_code))
# # to generate consistant graph # to generate consistant graph
# lst_pred_product_code = list(sorted(lst_pred_product_code)) lst_pred_product_code = list(sorted(lst_pred_product_code))
# for pred_product_code in lst_pred_product_code: for pred_product_code in lst_pred_product_code:
# # for each product predecessor (component) the firm need # for each product predecessor (component) the firm need
# # get a list of firm producing this component # get a list of firm producing this component
# lst_pred_firm = \ lst_pred_firm = \
# Firm['Code'][Firm[pred_product_code] == 1].to_list() Firm['Code'][Firm[pred_product_code] == 1].to_list()
# lst_pred_firm_size_damp = \ lst_pred_firm_size_damp = \
# [G_Firm.nodes[pred_firm]['Revenue_Log'] ** [G_Firm.nodes[pred_firm]['Revenue_Log'] **
# self.flt_netw_sply_prf_size self.flt_netw_sply_prf_size
# for pred_firm in lst_pred_firm] for pred_firm in lst_pred_firm]
# lst_prob = \ lst_prob = \
# [size_damp / sum(lst_pred_firm_size_damp) [size_damp / sum(lst_pred_firm_size_damp)
# for size_damp in lst_pred_firm_size_damp] for size_damp in lst_pred_firm_size_damp]
# # select multiple supplier (multi-sourcing) # select multiple supplier (multi-sourcing)
# n_pred_firm = self.int_netw_sply_prf_n n_pred_firm = self.int_netw_sply_prf_n
# if n_pred_firm > len(lst_pred_firm): if n_pred_firm > len(lst_pred_firm):
# n_pred_firm = len(lst_pred_firm) n_pred_firm = len(lst_pred_firm)
# lst_choose_firm = self.nprandom.choice(lst_pred_firm, lst_choose_firm = self.nprandom.choice(lst_pred_firm,
# n_pred_firm, n_pred_firm,
# replace=False, replace=False,
# p=lst_prob) p=lst_prob)
# lst_add_edge = [(pred_firm, node, lst_add_edge = [(pred_firm, node,
# {'Product': pred_product_code}) {'Product': pred_product_code})
# for pred_firm in lst_choose_firm] for pred_firm in lst_choose_firm]
# G_Firm.add_edges_from(lst_add_edge) G_Firm.add_edges_from(lst_add_edge)
# # graph firm prod # graph firm prod
# set_node_prod_code = set(G_Firm.nodes[node]['Product_Code']) set_node_prod_code = set(G_Firm.nodes[node]['Product_Code'])
# set_pred_succ_code = set(G_bom.successors(pred_product_code)) set_pred_succ_code = set(G_bom.successors(pred_product_code))
# lst_use_pred_prod_code = list( lst_use_pred_prod_code = list(
# set_node_prod_code & set_pred_succ_code) set_node_prod_code & set_pred_succ_code)
# for pred_firm in lst_choose_firm: for pred_firm in lst_choose_firm:
# pred_node = [n for n, v in G_FirmProd.nodes(data=True) pred_node = [n for n, v in G_FirmProd.nodes(data=True)
# if v['Firm_Code'] == pred_firm and if v['Firm_Code'] == pred_firm and
# v['Product_Code'] == pred_product_code][0] v['Product_Code'] == pred_product_code][0]
# for use_pred_prod_code in lst_use_pred_prod_code: for use_pred_prod_code in lst_use_pred_prod_code:
# current_node = \ current_node = \
# [n for n, v in G_FirmProd.nodes(data=True) [n for n, v in G_FirmProd.nodes(data=True)
# if v['Firm_Code'] == node and if v['Firm_Code'] == node and
# v['Product_Code'] == use_pred_prod_code][0] v['Product_Code'] == use_pred_prod_code][0]
# G_FirmProd.add_edge(pred_node, current_node) 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_Firm).to_csv('adj_g_firm.csv')
# nx.to_pandas_adjacency(G_FirmProd).to_csv('adj_g_firm_prod.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: if G_Firm.degree(node) == 0:
for product_code in G_Firm.nodes[node]['Product_Code']: for product_code in G_Firm.nodes[node]['Product_Code']:
# unconnect node does not have possible suppliers # 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( lst_succ_product_code = list(
G_bom.successors(product_code)) G_bom.successors(product_code))
# different from for different types of product, # different from for different types of product,
@ -131,8 +139,8 @@ class Model(ap.Model):
# for each product successor (finished product) # for each product successor (finished product)
# the firm sells to, # the firm sells to,
# get a list of firm producing this finished product # get a list of firm producing this finished product
lst_succ_firm = \ lst_succ_firm = Firm['Code'][
Firm['Code'][Firm[succ_product_code] == 1].to_list() Firm[succ_product_code] == 1].to_list()
lst_succ_firm_size_damp = \ lst_succ_firm_size_damp = \
[G_Firm.nodes[succ_firm]['Revenue_Log'] ** [G_Firm.nodes[succ_firm]['Revenue_Log'] **
self.flt_netw_cust_prf_size self.flt_netw_cust_prf_size
@ -149,27 +157,17 @@ class Model(ap.Model):
replace=False, replace=False,
p=lst_prob) p=lst_prob)
lst_add_edge = [(node, succ_firm, lst_add_edge = [(node, succ_firm,
{'Product': pred_product_code}) {'Product': product_code})
for succ_firm in lst_choose_firm] for succ_firm in lst_choose_firm]
G_Firm.add_edges_from(lst_add_edge) G_Firm.add_edges_from(lst_add_edge)
# graph firm prod # graph firm prod
set_node_prod_code = set( for succ_firm in lst_choose_firm:
G_Firm.nodes[node]['Product_Code']) succ_node = \
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) [n for n, v in G_FirmProd.nodes(data=True)
if v['Firm_Code'] == node and if v['Firm_Code'] == succ_firm and
v['Product_Code'] == use_pred_prod_code][0] v['Product_Code'] == succ_product_code][0]
G_FirmProd.add_edge(pred_node, current_node) G_FirmProd.add_edge(current_node, succ_node)
self.sample.g_firm = json.dumps(nx.adjacency_data(G_Firm)) self.sample.g_firm = json.dumps(nx.adjacency_data(G_Firm))
self.firm_network = ap.Network(self, G_Firm) self.firm_network = ap.Network(self, G_Firm)
@ -336,10 +334,10 @@ class Model(ap.Model):
'Product': di_supp_prod.code 'Product': di_supp_prod.code
}) })
]) ])
# print(f"proactive add {select_cand.code} to " print(f"proactive add {select_cand.code} to "
# f"{pro_firm.code} " f"{pro_firm.code} "
# f"for {di_supp_node['Firm_Code']} " f"for {di_supp_node['Firm_Code']} "
# f"{di_supp_node['Product_Code']}") f"{di_supp_node['Product_Code']}")
# change capacity # change capacity
select_cand.dct_prod_capacity[di_supp_prod] -= 1 select_cand.dct_prod_capacity[di_supp_prod] -= 1
break break

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 MiB

After

Width:  |  Height:  |  Size: 3.5 MiB

View File

@ -1,2 +1,2 @@
X1,X2,X3,X4,X5,X6,X7,X8,X9,X10,X11,X12,X13 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,0,0,0,0,0,0,0,0,0,0,0,0,0

1 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

2
orm.py
View File

@ -62,6 +62,8 @@ class Experiment(Base):
firm_acc_prf_conn = Column(Boolean, nullable=False) firm_acc_prf_conn = Column(Boolean, nullable=False)
netw_sply_prf_n = Column(Integer, nullable=False) netw_sply_prf_n = Column(Integer, nullable=False)
netw_sply_prf_size = Column(DECIMAL(8, 4), 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_prob_type = Column(String(16), nullable=False)
cap_limit_level = Column(DECIMAL(8, 4), nullable=False) cap_limit_level = Column(DECIMAL(8, 4), nullable=False)
diff_new_conn = Column(DECIMAL(8, 4), nullable=False) diff_new_conn = Column(DECIMAL(8, 4), nullable=False)

View File

@ -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 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,uniform,5,0.5,0.5,0.4 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,normal,10,1,1,0.6 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,,15,2,2,0.8 5,0.5,0.5,,0.5,,1,0.5,1,0.5,,15,2,2,0.8

1 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
2 15 2 2 True TRUE 2 True TRUE 3 2 3 2 uniform 5 0.5 0.5 0.4
3 10 1 1 False FALSE 1 False FALSE 2 1 2 1 normal 10 1 1 0.6
4 5 0.5 0.5 0.5 1 0.5 1 0.5 15 2 2 0.8

View File

@ -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 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,1,1,uniform,10,1,1,0.9 10,1,1,TRUE,1,TRUE,2,1,2,1,uniform,10,1,1,0.9
,,,,,,,,,,,,
,,,,,,,,,,,,

1 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
2 10 1 1 True TRUE 1 True TRUE 1 2 1 2 1 uniform 10 1 1 0.9