From 5387268d4878bbeaa75e9e2a6ff372a4faaaf931 Mon Sep 17 00:00:00 2001 From: HaoYizhi Date: Sun, 11 Jun 2023 11:49:15 +0800 Subject: [PATCH] larger sample than population when replace is False --- __pycache__/model.cpython-38.pyc | Bin 12359 -> 12269 bytes model.py | 149 +++++++++++++++++++++---------- test.ipynb | 27 ++++++ 3 files changed, 128 insertions(+), 48 deletions(-) diff --git a/__pycache__/model.cpython-38.pyc b/__pycache__/model.cpython-38.pyc index da5cc09ee8ea20c9456870cb7d8e39e72556bf51..26ebd3f09eb616b0db25af09eb1c9f14d5b9feff 100644 GIT binary patch delta 1277 zcmZ8gZA=?w9KZiv?|MC;?OF=-okA%T3Z>hKVF4qIEEzCeA$zgPHs+POGLc@7xFuJ| zDA=pOtp1&unVL2saf!xcb|3JAM89Z!NyY~=BU$EajKsJo-&}O~92yh7%kz8w|KI=R z`904)_wlJ;P8yF?RS6V%HhUMu>!+_9yFjQUE<;B8R{0Pt(QpDcm_tyBExId$+^tkA zwuh)J;77Xg3)c!vYPLY9z98c{*sQ$+QdOTZ%KeHl3qZ;JWF;u>Q;a3Zk{76gF4ZFUXDMPasV&)C+%LN^ z#vXJ!mzlgtH7EVWA=Q)s#h!o?=}q#NaE*=jxkT$cPvQ?JqXR@YQRH6cG7iO&93gA2 z?$p+bo=L92rU&Fv(VK-iAo;aoJyFg|SMV@rbNcEi#VPt~sS=%W$ww7eR@)gx;`2Dm zx%CY@xglKPnqi$VHI4$C3XwJkibIOyKeE2TKe!=hvQ@E5S#H5zo&vC9k{$P$K{N8SMSD6z7B#&8ycY%1BM8+ z;cvr7Tmx$%q#FI?%y>7xADDQOYl7&uq?pO z__<{fe#Z~2Zv5J2z*(CYU$-{EpM-f4wsE9711zprpCETUY2w>3jS`cX9wskZQ4w}BN zV|ZGd>1|r)VvS&iYOnwgH6YOC?>r}gt*}6i4hQR38^*{_w*j&=LS|`3gR}-w4bE#Y zgnnN$gv-Oe-3-j(1^+=9!B6~MFpAs$a)nkeD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Z3n860P@7jJpI&{E#; zbPAA0Z_`UKf{CVXcnv>mDp#?7S!FPWk>*y*>nwbgK^uc9{IvO9%UKrIQCQO?mB?&Q zdB-*b4|#mfHwpuI+2@8MxZ(>~@~m!=!4iWrxZ!Juz1Zw;hYTL@?}Sgv7yQ40D8EdX z@znO+@IBtxo`pL&7&r$f@lL=A>-b0DJvfN@*8M>6r`9F$1TDUab8R{6sfsFta}3^R zZ~~WuHuMLr(16beZT>Mzu1SygUp~wdF*bK>_P9Jq<568KqP!KCXWo_%;>n;_y2K<7 J;pf5rzX3>#i*En` diff --git a/model.py b/model.py index dd600ea..296a2d2 100644 --- a/model.py +++ b/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.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.str_cap_limit_prob_type = str(self.p.cap_limit_prob_type) 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() 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_size - 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') # unconnected node - # for node in nx.nodes(G_Firm): - # if node. + for node in nx.nodes(G_Firm): + 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.firm_network = ap.Network(self, G_Firm) diff --git a/test.ipynb b/test.ipynb index 6a6de56..04fe93a 100644 --- a/test.ipynb +++ b/test.ipynb @@ -239,6 +239,33 @@ " )\n", "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": {