From 1d8f319adb8629f280b5dd1b553163a3988cfcec Mon Sep 17 00:00:00 2001 From: HaoYizhi Date: Sun, 18 Jun 2023 20:27:23 +0800 Subject: [PATCH] revenue reduce --- __pycache__/firm.cpython-38.pyc | Bin 8186 -> 8204 bytes __pycache__/model.cpython-38.pyc | Bin 12577 -> 12818 bytes firm.py | 15 ++++++++------- model.py | 20 ++++++++++++++++---- test.ipynb | 19 +++++++++++++++++++ 5 files changed, 43 insertions(+), 11 deletions(-) diff --git a/__pycache__/firm.cpython-38.pyc b/__pycache__/firm.cpython-38.pyc index ebcd1571155a5e5ff869bd110d8c28e471ad2261..650066999aa2d639875b9aec5c79e404b63bd6d9 100644 GIT binary patch delta 1995 zcma)6TTC2f6rO)}XPDXTa9LovEbMYEowf)_y%oYmXt0uC5vX+|9am=9>>d9ZP@6?e ziHRoE*!(tqkpyCl;ZaF^RG)nD!6#!fNfV-tF+LbyjIqY(IsdL(K&WoA-<wnHS zb8mQKM4Qtzp8&rS|u;HO$% z8W-lgQk*|9e%Q^=2Zw% z^JQ?WyC8*3H6>D%4kzt`bUvPJf3CNV%Vi2Pg6^NvMVCxb!fuH+ImJ}ZaAj1<(S%_q zu34@Zad*!1E74p(wN|X`vSp-marTV#FzII3rRfv1xJu$9K2avSccgJK*bN8tApqAm zm`>QvLN2p3bcsEt%#wb#p?pmG*pS*qo?wfrReJ`VdBo$Tuue9ubdu3s*>}rx4sE(f ztI%2tup`n(;o*806Evd}dKL|(lJq>Nud=VTQK(W?KHu{snvrgF3_(M91Yn0y9p!_O zZ?nzvtKrN4IML_+?Xm$bt?WjX%El^cPxEkuVq3Jb6mc-~d#t5?>^%1*uE$=^(C5KZ zC$5sV9X{+st=tB)?FEpD_!k|3%hW*Cmd-+oLY-%iDfR&t>){PV(FIHG5Ld%eIJTqe zeaOD5{EpuThFT5*Fmyg>vQp^m;C()4-&RfZpF=+n_oGuFyV6Q5y=)x{03Mi$g#r&g zW7B~PWRl$u%*+iz1RX{gK^RB4h%kZBj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k-`AMsr)SUmU?8D;Ci@yP+Gpo0iQ2*U_ZBb-2JMnDPDE`*Z^Tq2!lMG%l_M@rg8 z#)_&%K6}_$sLqE{Ep{GeG!)y%kL9KshR+~yNpS&i0asgwk=ABRAR<3EXI{!~d>Q&l zbdN(SDk(-9g_L_moQDXV!217`$TzI3{z}J-56t_%B&D_vPSsf~xF4lIBc%toQ%a|? zg2y&ha~neUs^)(T_zed<```h;ZygW~(NWw3>ZM4@ELuT?6YOeZ$I?z5`*E2Q%{ktr zH|i~M27aRTI zuZWCyiN;ucWYEV|FhUc;D7zJD>Ot{L`Q~U&*Jk2z zW6q}KuUnZ76S72hH}b*5<*&%*suPTx#!OZ_Z_MVXq1iM!V|K95n_nx==EEw>*qN3l z(#2kBN%ZmiQn?QEoZ5s@y5d>GzL=wFEt}JgURK>YE!E!|YaT`;ifF9O+oX$l4S$wp5i5qx5HTdfqe!3D)B`3+i3}02{oj!~g&Q diff --git a/__pycache__/model.cpython-38.pyc b/__pycache__/model.cpython-38.pyc index d65f2ac685b747796d411e52260f90d54daad1f8..432b0f5bd8709331e1e90725d825276af354ca07 100644 GIT binary patch delta 1138 zcmah|PiRzU5dY?TZ#SFGX1l~>lNjAd5_H#S8~=ctm}p{BtcqHR7PDAhck^E8i|fAZ z>`RdF)_@ze2rZ!>2;LHUQhKO7@DLAzBF(jk6!swW*jq2XcrcxL!HB1R@cq8u%r`UN z%*V`H;b${`rMWpOXubINC-d3DllZ|F$oa!9w`B8i(ms;dN(T*NMY!1BS}%ZY4YT3j81*2rU#EjJ-tJidV63_p;C0Y z{m^Q&8Dhs7xdtH0sd9oPGkjfN0qi5r$zA;<)zSY+b;HxiU&Eyhx6y3)kWozos(l5@ zvGPWFJz#z1^&n?q#67Y48UKwbCPgN40SD>Z{@$d8L;RsJf;wD4`bV*tC^_z)itEhn zDOl6_`HGcEV3{s*$?2k%FBt`E#zD())k;re-eq@>A;l&U2W_KbFIs4`E@-7GV)f7* z+9em~S$l*b=&kRVT_&jO{9q=8Rn~A1)OGG!98`by^bTf8As`x$LL8D1fiSdghGi03 zK?k`hY3mT#jA${~vPeJzB2q%c-_h%8hqzk2&9GhVVsvDC33_A(lsyF+FU|A*76Y!0?l75(Ahq`vdE)nE!;#KuN z-M)(lD--`%C*I`%L5BC#q0DmVKFL*oJ+l`m*kAiovO)UL{}SMmdUfC^45@bqYSD{i xG$zUMYjyA7FEHWvf4&TGSpD|JkKrFU;C(edkcG7J2C_}TNRcrOj(R>&_zyV#45|PC delta 855 zcmY+C&rcIU6vyYyZmYDmX%Gs<3V{;nVn9(0l^+$P7!}i~NDM^N0K3E%XzA-hh}kMy z&~VU$bMi)GqCq`qHsK#2(X(=L{{!Pe}TG*dGGsPX6CtGp7Brkd>$r; z_vC{5OOf6wfU!FBnXe{Pd3>_2pcyJlKm6S{SDSLGb5z^5Vq)Z$moF1w!0D+VqY;a7&wdF04yfEW@^- z%z!Me!@P=X&58p_@w(lD9nl(H?KrsEUJ9X`-3E-4O}ZE&G)R0A zAESLx5}+mEmbj?B2vCBfET*#()2lcserR=JdL)|vpi|r+fl3dLMN7xJYg4ZC_S=p& zXf9DK?8jD${j2CGMppj~_OBDW05`-=XAg9V@0}&jHJNgjOU3JAwd*Cs?C{BTfHqM+ e_0;`@Ds76M?ihr{Vt342c{egxz7Fxed*&}WdC7AC diff --git a/firm.py b/firm.py index ba91572..d6c38d0 100644 --- a/firm.py +++ b/firm.py @@ -10,7 +10,8 @@ class FirmAgent(ap.Agent): self.code = code self.name = name self.type_region = type_region - self.revenue_log = revenue_log + self.ori_size = revenue_log + self.size = revenue_log self.a_lst_product = a_lst_product self.dct_prod_capacity = {} @@ -47,7 +48,7 @@ class FirmAgent(ap.Agent): "cap_limit_prob_type other than uniform, normal" if self.str_cap_limit_prob_type == 'uniform': extra_cap_mean = \ - self.revenue_log / self.flt_cap_limit_level + self.size / self.flt_cap_limit_level extra_cap = self.model.nprandom.integers(extra_cap_mean-2, extra_cap_mean+2) extra_cap = 0 if round(extra_cap) < 0 else round(extra_cap) @@ -55,7 +56,7 @@ class FirmAgent(ap.Agent): self.dct_prod_capacity[product] = extra_cap elif self.str_cap_limit_prob_type == 'normal': extra_cap_mean = \ - self.revenue_log / self.flt_cap_limit_level + self.size / self.flt_cap_limit_level extra_cap = self.model.nprandom.normal(extra_cap_mean, 1) extra_cap = 0 if round(extra_cap) < 0 else round(extra_cap) # print(firm_agent.name, extra_cap) @@ -146,7 +147,7 @@ class FirmAgent(ap.Agent): lst_size = \ [size for size in self.dct_cand_alt_supply_up_prod_removed[ - product].revenue_log] + product].size] lst_prob = [size / sum(lst_size) for size in lst_size] select_alt_supply = self.model.nprandom.choice( @@ -159,7 +160,7 @@ class FirmAgent(ap.Agent): # select based on size of connected firm or not if self.is_prf_size: lst_firm_size = \ - [firm.revenue_log for firm in lst_firm_connect] + [firm.size for firm in lst_firm_connect] lst_prob = \ [size / sum(lst_firm_size) for size in lst_firm_size] @@ -228,7 +229,7 @@ class FirmAgent(ap.Agent): # handling based on size or not if self.is_prf_size: lst_firm_size = \ - [firm.revenue_log for firm in lst_firm] + [firm.size for firm in lst_firm] lst_prob = \ [size / sum(lst_firm_size) for size in lst_firm_size] @@ -243,7 +244,7 @@ class FirmAgent(ap.Agent): # handling based on size of connected firm or not if self.is_prf_size: lst_firm_size = \ - [firm.revenue_log for firm in lst_firm_connect] + [firm.size for firm in lst_firm_connect] lst_prob = \ [size / sum(lst_firm_size) for size in lst_firm_size] diff --git a/model.py b/model.py index 6c42b76..d14ac1f 100644 --- a/model.py +++ b/model.py @@ -300,7 +300,7 @@ class Model(ap.Model): for path in lst_shortest_path]) \ / len(lst_shortest_path) drs = n2n_betweenness / \ - (len(lst_cand) * di_supp_firm.revenue_log) + (len(lst_cand) * di_supp_firm.size) dct_drs[di_supp_code] = drs dct_drs = dict(sorted( dct_drs.items(), key=lambda kv: kv[1], reverse=True)) @@ -352,6 +352,18 @@ class Model(ap.Model): # self.int_stop_times = self.t # self.stop() + # reduce the size of removed firm + for firm in self.a_lst_total_firms: + for prod in firm.dct_prod_up_prod_stat.keys(): + status, ts = firm.dct_prod_up_prod_stat[prod]['status'][-1] + if status == 'R': + firm.size -= \ + firm.ori_size / len(firm.a_lst_product) / self.drop_t + print(self.t, firm.name, prod.code, firm.size) + if self.t - ts + 1 == self.drop_t: + firm.dct_prod_up_prod_stat[ + prod]['status'].append(('N', self.t)) + if self.t > 0: for firm in self.a_lst_total_firms: for prod in firm.dct_prod_up_prod_stat.keys(): @@ -462,14 +474,14 @@ class Model(ap.Model): # mark disrupted firm as removed based conditionally lost_percent = n_up_product_removed / len( product.a_predecessors()) - lst_size = self.a_lst_total_firms.revenue_log - lst_size = [firm.revenue_log for firm + lst_size = self.a_lst_total_firms.size + lst_size = [firm.size for firm in self.a_lst_total_firms if product in firm.a_lst_product and product not in firm.a_lst_product_removed ] - std_size = (firm.revenue_log - min(lst_size) + + std_size = (firm.size - min(lst_size) + 1) / (max(lst_size) - min(lst_size) + 1) prob_remove = 1 - std_size * (1 - lost_percent) # damp prod diff --git a/test.ipynb b/test.ipynb index 759c09e..83d5656 100644 --- a/test.ipynb +++ b/test.ipynb @@ -298,6 +298,25 @@ " break\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.25\n", + "2.25\n" + ] + } + ], + "source": [ + "print(27 / (4 * 3))\n", + "print(27 / 4 / 3)" + ] } ], "metadata": {