From bf82aa62ffa54b90c1aa98736dcb9861143f13a7 Mon Sep 17 00:00:00 2001 From: HaoYizhi Date: Fri, 28 Jul 2023 18:07:30 +0800 Subject: [PATCH] debug --- __pycache__/controller_db.cpython-38.pyc | Bin 7536 -> 7535 bytes __pycache__/firm.cpython-38.pyc | Bin 8866 -> 8989 bytes __pycache__/model.cpython-38.pyc | Bin 13401 -> 13409 bytes controller_db.py | 3 +- firm.py | 37 +++++++++++------------ model.py | 6 ++-- test.ipynb | 23 ++++++++++++++ 7 files changed, 45 insertions(+), 24 deletions(-) diff --git a/__pycache__/controller_db.cpython-38.pyc b/__pycache__/controller_db.cpython-38.pyc index 7e581bfbd3e9bd78051ccdc97d95c3189df5d3a4..007c91ae5e92ceae80f35eede72736c95fb11c22 100644 GIT binary patch delta 116 zcmV-)0E_?dI`29SRSgXa00000L6gH|h_Mar2nz!-HaL0(F)lGKGP4E=*#ZLy37Hvs)NR0Rir_r5POo W0s51}8pHtzvxplD0RcpllpNgYlO%`$ delta 117 zcmV-*0E++bI`BFTRSgXa00000`f9^uU9k=A2n+%@IeG^&E-@}ME;O?R3E2Vz1q=nV z;R^}^0d%wM4U+)@kh6dfNdWy6%p_SSNHq6hZ5Nu??r&_)h{dn$ml>b#2$~ySEca zYojyKM2*5{5CbHjhGZ|i5Z_ExOf>i)K54u$f)76UV&uh`K$z!zceG=Uh)wS4`ObI# zeZOyzT@f~1NQM762**M+=v zsrjYO955iXIF;(LVl0xzkg zNx%nqe~VC%;a`{vTx!kBs;f~z+8|sO3oav=cR>_VObTGyt&(ZU5*1u|7Z}NTCj|Z$ zIG^fHiWIp=Id22Eo~2H?&sQGhQx*0Lp^8yT0E3WSG|4FYvf6(PVcCzcGy_S%0mi%q zk|(NQ2y<|x3nCL|>|iM#2#Ka$H)4iaqiaS~P3Sb2HKOAQnhp`u1~O0(!y8OzE*s6# zj5;q+7b`dhN8AxL5jWzQbhft(BVl1AQM@k-BC#)=>KFtUJ0`SgBMeS**>EJ4iN{iU zZ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7c68527f5f941ef90ce7c8c6bdb0 100644 GIT binary patch delta 315 zcmcba@i2ool$V!_0SNYW98USTk@uw<&8p z1{a3dq{)BOwdFY^8K8WPT27E`4U;583Q)9$6G-z-cG57^Y)av;Wv}4?>Jb2`S-@Ar zxsZ{OVFF`eTj9GJ_6dwd%fP%Rljmrt^6mht5Ln0n7TGcRoQ4Baaq46NO?B3y9-!+s z+iS8iDn+psmlhP{R2E49g&>3^h-D2VIv5!kit{EXYXxuKq{Yj`IDPX0okc7%i-A(_ zK~ii$1{)*Ke>Mh9rk6Y*UCSod8M-pAnta6YulNd}KonPMN_uL1PGVAO4#e<9lW!QU ZW19z3ym7L;iTLEj#)XV?CySc6004JDT0H;& delta 266 zcmaEuaWjKAl$V!_0SKJ452w7`$oo=_@yX=xYNr?_CLdB)7hYD&n9fkkQNx(UyntmP zg9}4!+~hy%+R}WI3{bvGEhk8}hDnkk1t?m>IoVCaP_r(Dzm~m*1E^jAB)@>KhI1h! zBf|v7!luG^HS7}@ixz=-4<;|rQ03hMR3WgC0W7j*@+A!irkvu*BAV(Px7dnH3kq^7 zH#=yuGO`v;0lIUtua+cZ*5q`p;LZEAIGGq{Y(Am0h=p;<r2S)4I=7X4nN3r$ diff --git a/controller_db.py b/controller_db.py index 1e1b50d..7ddd066 100644 --- a/controller_db.py +++ b/controller_db.py @@ -72,9 +72,10 @@ class ControllerDB: # list_dct = [{'133': ['1.4.4.1']}] # list_dct = [{'2': ['1.1.3']}] # list_dct = [{'135': ['1.3.2.1']}] - list_dct = [{'79': ['2.1.3.4']}] + # list_dct = [{'79': ['2.1.3.4']}] # list_dct = [{'99': ['1.3.3']}] # list_dct = [{'41': ['1.4.5']}] + # list_dct = [{'168': ['1.1.2']}] # fill g_bom BomNodes = pd.read_csv('BomNodes.csv', index_col=0) diff --git a/firm.py b/firm.py index 395b553..a1f6233 100644 --- a/firm.py +++ b/firm.py @@ -35,13 +35,12 @@ class FirmAgent(ap.Agent): self.dct_prod_up_prod_stat[prod] = { # (Normal / Disrupted / Removed, time step) 'p_stat': [('N', 0)], - # have or have no supply - 'supply': dict.fromkeys(prod.a_predecessors(), True), # supply for each component and respective disrupted supplier - # lst_disrupt_firm is refreshed to [] at each update - 's_stat': dict.fromkeys(prod.a_predecessors(), - {'stat': True, - 'lst_disrupt_firm': []}) + # set_disrupt_firm is refreshed to empty at each update + 's_stat': {up_prod: {'stat': True, + 'set_disrupt_firm': set()} + for up_prod in prod.a_predecessors()} + # Note: do not use fromkeys as it's a shallow copy } # init extra capacity (self para) @@ -76,14 +75,11 @@ class FirmAgent(ap.Agent): if disrupted_prod in \ customer.dct_prod_up_prod_stat[ prod]['s_stat'].keys(): - if self not in customer.dct_prod_up_prod_stat[ - prod]['s_stat'][disrupted_prod][ - 'lst_disrupt_firm']: - customer.dct_prod_up_prod_stat[ - prod]['s_stat'][disrupted_prod][ - 'lst_disrupt_firm'].append(self) - print(f"{self.name} disrupt {customer.name}'s " - f"{prod.code} due to {disrupted_prod.code}") + customer.dct_prod_up_prod_stat[ + prod]['s_stat'][disrupted_prod][ + 'set_disrupt_firm'].add(self) + print(f"{self.name} disrupt {customer.name}'s " + f"{prod.code} due to {disrupted_prod.code}") # remove edge to customer self.firm_network.graph.remove_edge(n1, n2, key) @@ -93,7 +89,7 @@ class FirmAgent(ap.Agent): # self's component exists disrupted supplier num_lost = \ len(self.dct_prod_up_prod_stat[prod]['s_stat'] - [disrupted_up_prod]['lst_disrupt_firm']) + [disrupted_up_prod]['set_disrupt_firm']) num_remain = \ len([u for u, _, _, d in self.firm_network.graph.in_edges(self.get_firm_network_node(), @@ -117,7 +113,8 @@ class FirmAgent(ap.Agent): if status != 'D': self.dct_prod_up_prod_stat[ prod]['p_stat'].append(('D', self.model.t)) - print(f"{self.name}'s {prod.code} turn to D status") + print(f"{self.name}'s {prod.code} turn to D status due to " + f"disrupted supplier of {disrupted_up_prod.code}") def seek_alt_supply(self, product): # para product is the product that self is seeking @@ -283,9 +280,9 @@ class FirmAgent(ap.Agent): for prod in down_firm.dct_prod_up_prod_stat.keys(): if product in down_firm.dct_prod_up_prod_stat[ - prod]['supply'].keys(): + prod]['s_stat'].keys(): down_firm.dct_prod_up_prod_stat[ - prod]['supply'][product] = True + prod]['s_stat'][product]['stat'] = True down_firm.dct_prod_up_prod_stat[ prod]['p_stat'].append(('N', self.model.t)) del down_firm.dct_n_trial_up_prod_disrupted[product] @@ -317,10 +314,10 @@ class FirmAgent(ap.Agent): if ts != self.model.t: self.dct_prod_up_prod_stat[prod]['p_stat'].append( (status, self.model.t)) - # refresh lst_disrupt_firm + # refresh set_disrupt_firm for up_prod in self.dct_prod_up_prod_stat[prod]['s_stat'].keys(): self.dct_prod_up_prod_stat[prod][ - 's_stat'][up_prod]['lst_disrupt_firm'] = [] + 's_stat'][up_prod]['set_disrupt_firm'] = set() def get_firm_network_node(self): return self.firm_network.positions[self] diff --git a/model.py b/model.py index 0a716b5..aef168a 100644 --- a/model.py +++ b/model.py @@ -406,7 +406,7 @@ class Model(ap.Model): for up_prod in firm.dct_prod_up_prod_stat[prod][ 's_stat'].keys(): if firm.dct_prod_up_prod_stat[prod][ - 's_stat'][up_prod]['lst_disrupt_firm']: + 's_stat'][up_prod]['set_disrupt_firm']: firm.disrupt_cus_prod(prod, up_prod) for n_trial in range(self.int_n_max_trial): @@ -421,9 +421,9 @@ class Model(ap.Model): status = firm.dct_prod_up_prod_stat[prod]['p_stat'][-1][0] if status == 'D': for supply in firm.dct_prod_up_prod_stat[ - prod]['supply'].keys(): + prod]['s_stat'].keys(): if not firm.dct_prod_up_prod_stat[ - prod]['supply'][supply]: + prod]['s_stat'][supply]['stat']: lst_seek_prod.append(supply) # commmon supply only seek once lst_seek_prod = list(set(lst_seek_prod)) diff --git a/test.ipynb b/test.ipynb index c37ecb1..5015e92 100644 --- a/test.ipynb +++ b/test.ipynb @@ -553,6 +553,29 @@ "G.has_edge(1, 2, 1)" ] }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "s = set()\n", + "s.add(1)\n", + "s.add(2)\n", + "s.add(1)\n", + "len(s)\n", + "if s:\n", + " print(1)" + ] + }, { "cell_type": "code", "execution_count": null,