larger sample than population when replace is False
This commit is contained in:
parent
49d3c6791e
commit
5387268d48
Binary file not shown.
149
model.py
149
model.py
|
@ -19,7 +19,7 @@ class Model(ap.Model):
|
||||||
self.dct_lst_remove_firm_prod = self.p.dct_lst_init_remove_firm_prod
|
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_n_max_trial = int(self.p.n_max_trial)
|
||||||
self.int_netw_sply_prf_size = 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.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)
|
||||||
|
@ -65,58 +65,111 @@ 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_size
|
# n_pred_firm = self.int_netw_sply_prf_n
|
||||||
lst_choose_firm = self.nprandom.choice(lst_pred_firm,
|
# if n_pred_firm > len(lst_pred_firm):
|
||||||
n_pred_firm,
|
# n_pred_firm = len(lst_pred_firm)
|
||||||
replace=False,
|
# lst_choose_firm = self.nprandom.choice(lst_pred_firm,
|
||||||
p=lst_prob)
|
# n_pred_firm,
|
||||||
lst_add_edge = [(pred_firm, node,
|
# replace=False,
|
||||||
{'Product': pred_product_code})
|
# p=lst_prob)
|
||||||
for pred_firm in lst_choose_firm]
|
# lst_add_edge = [(pred_firm, node,
|
||||||
G_Firm.add_edges_from(lst_add_edge)
|
# {'Product': pred_product_code})
|
||||||
|
# for pred_firm in lst_choose_firm]
|
||||||
|
# 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')
|
||||||
# unconnected node
|
# unconnected node
|
||||||
# for node in nx.nodes(G_Firm):
|
for node in nx.nodes(G_Firm):
|
||||||
# if node.
|
if G_Firm.degree(node) == 0:
|
||||||
|
for product_code in G_Firm.nodes[node]['Product_Code']:
|
||||||
|
# unconnect node does not have possible suppliers
|
||||||
|
lst_succ_product_code = list(
|
||||||
|
G_bom.successors(product_code))
|
||||||
|
# different from for different types of product,
|
||||||
|
# finding a common supplier (the logic above),
|
||||||
|
# for different types of product,
|
||||||
|
# finding a custormer for each product
|
||||||
|
for succ_product_code in lst_succ_product_code:
|
||||||
|
# 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_size_damp = \
|
||||||
|
[G_Firm.nodes[succ_firm]['Revenue_Log'] **
|
||||||
|
self.flt_netw_cust_prf_size
|
||||||
|
for succ_firm in lst_succ_firm]
|
||||||
|
lst_prob = \
|
||||||
|
[size_damp / sum(lst_succ_firm_size_damp)
|
||||||
|
for size_damp in lst_succ_firm_size_damp]
|
||||||
|
# select multiple customer (multi-selling)
|
||||||
|
n_succ_firm = self.int_netw_cust_prf_n
|
||||||
|
if n_succ_firm > len(lst_succ_firm):
|
||||||
|
n_succ_firm = len(lst_succ_firm)
|
||||||
|
lst_choose_firm = self.nprandom.choice(lst_succ_firm,
|
||||||
|
n_succ_firm,
|
||||||
|
replace=False,
|
||||||
|
p=lst_prob)
|
||||||
|
lst_add_edge = [(node, succ_firm,
|
||||||
|
{'Product': pred_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)
|
||||||
|
|
||||||
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)
|
||||||
|
|
27
test.ipynb
27
test.ipynb
|
@ -239,6 +239,33 @@
|
||||||
" )\n",
|
" )\n",
|
||||||
"print(lst_choose_firm)"
|
"print(lst_choose_firm)"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"ename": "ValueError",
|
||||||
|
"evalue": "Cannot take a larger sample than population when replace is False",
|
||||||
|
"output_type": "error",
|
||||||
|
"traceback": [
|
||||||
|
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
||||||
|
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
|
||||||
|
"Cell \u001b[1;32mIn[9], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m nprandom \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39mrandom\u001b[39m.\u001b[39mdefault_rng(\u001b[39m0\u001b[39m)\n\u001b[1;32m----> 2\u001b[0m lst_choose_firm \u001b[39m=\u001b[39m nprandom\u001b[39m.\u001b[39;49mchoice([\u001b[39m1\u001b[39;49m,\u001b[39m2\u001b[39;49m],\n\u001b[0;32m 3\u001b[0m \u001b[39m3\u001b[39;49m,\n\u001b[0;32m 4\u001b[0m replace\u001b[39m=\u001b[39;49m\u001b[39mFalse\u001b[39;49;00m\n\u001b[0;32m 5\u001b[0m )\n\u001b[0;32m 6\u001b[0m lst_choose_firm\n",
|
||||||
|
"File \u001b[1;32m_generator.pyx:753\u001b[0m, in \u001b[0;36mnumpy.random._generator.Generator.choice\u001b[1;34m()\u001b[0m\n",
|
||||||
|
"\u001b[1;31mValueError\u001b[0m: Cannot take a larger sample than population when replace is False"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"nprandom = np.random.default_rng(0)\n",
|
||||||
|
"lst_choose_firm = nprandom.choice([1,2],\n",
|
||||||
|
" 3,\n",
|
||||||
|
" replace=False\n",
|
||||||
|
" )\n",
|
||||||
|
"lst_choose_firm"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|
Loading…
Reference in New Issue