From 5b0e17ce523abd5cdac019ebf312abf797c9158e Mon Sep 17 00:00:00 2001 From: HaoYizhi Date: Tue, 14 Mar 2023 18:53:00 +0800 Subject: [PATCH] formatting --- __pycache__/computation.cpython-38.pyc | Bin 1542 -> 1542 bytes __pycache__/controller_db.cpython-38.pyc | Bin 6191 -> 6175 bytes __pycache__/firm.cpython-38.pyc | Bin 5089 -> 5120 bytes __pycache__/model.cpython-38.pyc | Bin 8987 -> 8734 bytes __pycache__/orm.cpython-38.pyc | Bin 3230 -> 3229 bytes __pycache__/product.cpython-38.pyc | Bin 1382 -> 1382 bytes controller_db.py | 7 +- firm.py | 125 +++++---- model.py | 315 +++++++++-------------- orm.py | 2 +- 10 files changed, 183 insertions(+), 266 deletions(-) diff --git a/__pycache__/computation.cpython-38.pyc b/__pycache__/computation.cpython-38.pyc index 2a37f85cb1876e18c7b3c50a45c014349897fdb0..7da4cf3741e3e2f1c889a6da05098add2f90615a 100644 GIT binary patch delta 20 ZcmZqUY2)Dz<>lpK00LKMfsNe1Spg-_1N{I1 delta 20 ZcmZqUY2)Dz<>lpK0D?9F{*BzfSpg6MvXhsur)4R=pQgF>c)*5pA3 zB971A7^47>X&Q33unQxotw6pRn=rbf+E#~Dlh{} zD%}rtb~mS$#mLhDD4uk`s69~7cobL$P_EuZMRqr7Jv3+pvVDCwBiY^ka%CwVz7tN# z9>SvM;nUqaWaw^|pgMjJb`*}U?T@#rJr-B+6Z)PrA<@RtfUw=h_4RY=3@s#nhx%GO*7~ z(6{)vl39nowVnfq##19b9xK8EnYS7Iwq=fdd;SAlqgSchat5;2P8g!IR(fxi1zikP z1|gxYyI#?&l&{#{r5ZN+&vTNmS6ru;R=2f9hEX|yx?Gy7snM7g)GYd9y*rX*RpYVd sid8JSf(N%As?J}TTd1qmxrWDPmfhftiX~Cw)10ppvUg36%Gb7uKkEqZiU0rr delta 1055 zcmZ`%%WD&15Z~EslFc@|N%Lyc+G>5YR7KMEpluXbF}(Pz2k9 z2zqc35#I{lRHPU6;K74>Q1Idk%|-+TH}L!B`^|4Mznz`ixyLzkw`m%R zeBKVk@z4`h_^XAEa! z5>rwc$24Xn)9NI!4cn)cDX^0`2ea5AvneOxq_Oh`;4JK#)?_Y&Fsk(TG@@(jHBZ#5 zRY$D7toHTV@v7?;+~Nrb3vQu;g&{lf4>M30oLQ;{A>Oe5&d{pBeQ2l8f!EMS54Dx> zoW5&QbSBu@6Ui#4!*>Iqc=DA_>(>=*xfz%OP>!ERMH-3HvtXXo(8W+n&PAl@N@(Bg zMKW}|c~D?L0xZxV4mw03Bkz_*R`^!uQkE}c7-m=?q0x7flc*h_D$(e@o&yot*~<)igV*%2BuyJ;x45YluomQ8M9!gmomT&a4F5F6=2Y`wM3 zuR^ElNNxpvitU8anSppp?M-t;8twJrXvv6ZACsW#qq=TqFbo`|Q_zi@z(G9ixw_ z129G->6N`MD?*~oJTmxgOC0(3{0}fmchcwN4$R9eggkA{WCwFB=wcXWI4+?Pb-kij zDUUne(KqwFy#J41 diff --git a/__pycache__/firm.cpython-38.pyc b/__pycache__/firm.cpython-38.pyc index a8701b3d665d90aeb57296dd84684d1967aff253..f70af26137667b7e34e6d6d354855ff90295d992 100644 GIT binary patch delta 606 zcmY*VO=}cE5S{92+i|`!J2BZ&L;`_C7d?1V5k)uU>rhbA}gEh+A}LAAS8J3 zqCxB*5c~liWNw0>2Y-QhS(M<-i$B4PuQujjLl;%As$NySpVOPC!ucR@ExgY&QMPsb zefW-Ku#|SpC%Sw)fm4)eznAuOH|sB#sm@osPx5Tg+|L}qS-a5vUWvrm|A~Y8lNp-V zs#)<;&~&vcPf*}9b+=!pdG;_ju9}+i7-_E!frXfZBJbpA{D%!Zo@Z{U>sN2`$|T4k z*AkYfWi9Q9ToK#jKwW&X2Ta1Nm5eC_qH6fIPrya>MFoPcn6J(Ux>0Q(iHH`>j=N6x z%`vZ(4OZZdcTYGS^V_?2{sN{IcZM^48Yc->=96}8t3S>m#KIMFPf3S(8zlrK$W}}K zGoqf^4W6GK1bom+Td$@?dn4CPu<3-&B+O&{Lc%CAFTy#V`y)IznK3*8W06mM1-2i0 zz6|+F+*6$$S*DS7E>tjz&#%o+^v%;Vh!^%X+iWy1&LVuubuAbR?F!O_Aa$`Ygk(ZX zLL|_tm?8|~Gd2?QHcHI*#shj~F2!3kZ$8E?dToBik38noER4Qu7Dro$C!90RQD@9- HkIwxC>(rQC delta 533 zcmZ8dziSjh6rMLXH#gba-P`P$^@Q^x7|ju|NGk-B6jl<5F(GWTgxiT+xF5^zo|s@D z(bhuqngsF}2q|O>!N$(o#=;;%dTlLpzTNAb;eCAb=6m1cy`%QkyuE2z2BY(J(Fs2_ zKiU^iws(SF7r($#`Bu(Oxc9J;lOzqIco>d4X%Kdkcod~Ny!8J2iFJSYg7SAlk_|@v z>-DBz4tc?+_Ec?htNzpXd-d@7>plkS0zZ&F>{$l)%*LGB_5 zB*RmCqj3LYFV6P!e3O8I2`<=#qF|=5>7A|AI#7F^izI<$oEZ!==lhyp=~1WMAn{cc z>~uOhN+&HJk#5p=o!AjY(js8oBli|4+6(Q2i7}@ur3()A!>j3+@D6?V7@pxzcL5M9 a^}TBD5O3zNg|F%hm9~1I>dL{3`o=$YgN5M$ diff --git a/__pycache__/model.cpython-38.pyc b/__pycache__/model.cpython-38.pyc index ed4194c550092e28fb1d0ae29842a7b6c6680155..fca4da6d4ed6e05e6d335519498656d302ab3a38 100644 GIT binary patch delta 3007 zcmZuzYit}>6`ngYJ08zIyk6VuT|cs$rb)9nA!$kyCpb= zJF~eno7mO{L((556{6(|5UQvgfq)7LeuSTu5YImp0TM`P{vcEdsS-#$B|s(SoI7jB zwlb^v_TF>OJ@?%6y7SiQwtyIb zhx`+T6M8q^6ld1WuP2eIgB_r+A7Y=_lctvrP~91 zQXB3rv?#R6Mh8lb?JmHjD`MJ@z zB&Q5i`2V_km)s#RKDh!M{!8uOq?7-=V}Ts!$8~4C7mivXkrFiP6`6IzEK-Bo5(Qe$ z4MHnuR;$)bjnyrrG8AWh2zh{${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mzU(q;AdWo(+c88G{%FV7j;oMekaY&ZY?L9JbU{4GCnW_sb5mC!jiD{F6RcGU zsJW?vwGrj%+yU~ZC#Q2iB?-TOJ-Z>vNFvkT!T*(?E{J$ytneK2>?#$hb_GrxpMQH` zm7V+`Jd>@A$@TU!%8ca33!t?^~cqKPM&U2|sLTKI)k9EH9GG0L1fcEk1WG}%p` zKQ6tqaveKycNtQh-9z9Ar7wesO&WBs!0TaAHHfhy?J<%@-Pp&lr<}**6AsJPl1S4) zZ#fO>cx)Ov0}W1M=$L{wI}BhMxQ-QA$Ray|O2G*kTcVIDXrmz6ki1MoKY{-a0!2Q4iZsQ>@~ diff --git a/__pycache__/orm.cpython-38.pyc b/__pycache__/orm.cpython-38.pyc index 778c97301a357e148c728685fe3d62d240dd4c5b..bd5c1d99857b10e7144391c6a5b297e6ff74d328 100644 GIT binary patch delta 34 ocmbOyIaiW5l$V!_0SLU@1yWiz@@BBI$)+Ti#OG|TVO3%W0E5N|E&u=k delta 35 pcmbO$IZu)|l$V!_0SGeH`BPdq@@BBI%cUfj#OGvgu4YwY2LO#I2$ui= diff --git a/__pycache__/product.cpython-38.pyc b/__pycache__/product.cpython-38.pyc index d5d56c71a51a7b38a55e036072117c50c0a05aa2..af14d2b17d50ec800e2ce423d4132825a2b4bef3 100644 GIT binary patch delta 20 acmaFH^^A)Qv`tk delta 20 acmaFH^^A) 0: - if len(list_firm) == 0: + if len(lst_firm) == 0: continue - elif len(list_firm) == 1: - self.accept_request(list_firm[0], product) - elif len(list_firm) > 1: + elif len(lst_firm) == 1: + self.accept_request(lst_firm[0], product) + elif len(lst_firm) > 1: # handling based on connection # TBC # handling based on size - list_size = [firm.revenue_log for firm in list_firm] - list_prob = [size / sum(list_size) for size in list_size] - select_customer = self.model.nprandom.choice(list_firm, - p=list_prob) + lst_firm_size = [firm.revenue_log for firm in lst_firm] + lst_prob = [ + size / sum(lst_firm_size) for size in lst_firm_size + ] + select_customer = self.model.nprandom.choice(lst_firm, + p=lst_prob) self.accept_request(select_customer, product) - # print(product.code, [firm.name for firm in list_firm]) def accept_request(self, down_firm, product): - # if self.model.nprandom.choice([True, False], p=[0.1, 0.9]): - lst_f_s = [firm.revenue_log for firm in self.model.a_list_total_firms if product in firm.a_list_product] - p_accept = self.revenue_log / sum(lst_f_s) - if self.model.nprandom.choice([True, False], p=[p_accept, 1-p_accept]): + lst_firm_size = [ + firm.revenue_log for firm in self.model.a_lst_total_firms + if product in firm.a_lst_product + ] + prod_accept = self.revenue_log / sum(lst_firm_size) + if self.model.nprandom.choice([True, False], + p=[prod_accept, 1 - prod_accept]): self.firm_network.graph.add_edges_from([ (self.firm_network.positions[self], - self.firm_network.positions[down_firm], { - 'Product': product.code - }) + self.firm_network.positions[down_firm], { + 'Product': product.code + }) ]) self.dct_prod_capacity[product] -= 1 self.dct_request_prod_from_firm[product].remove(down_firm) - down_firm.a_list_up_product_removed.remove(product) - print(f"{self.name} accept {product.code} request from {down_firm.name}") + down_firm.a_lst_up_product_removed.remove(product) + print( + f"{self.name} accept {product.code} request from {down_firm.name}" + ) def clean_before_trial(self): self.dct_request_prod_from_firm = {} def clean_before_time_step(self): - self.dct_num_trial_up_product_removed = {} - self.a_list_up_product_removed = ap.AgentList(self.model, []) + self.dct_n_trial_up_product_removed = {} + self.a_lst_up_product_removed = ap.AgentList(self.model, []) diff --git a/model.py b/model.py index e169b59..27b7d4f 100644 --- a/model.py +++ b/model.py @@ -5,33 +5,9 @@ import random import networkx as nx from firm import FirmAgent from product import ProductAgent -from orm import db_session, Sample, Result +from orm import db_session, Result import platform -sample = 0 -seed = 0 -n_iter = 10 -# dct_list_init_remove_firm_prod = {133: ['1.4.4.1'], 2: ['1.1.3']} -# dct_list_init_remove_firm_prod = { -# 135: ['1.3.2.1'], -# 133: ['1.4.4.1'], -# 2: ['1.1.3'] -# } -dct_list_init_remove_firm_prod = { - '140': ['1.4.5.1'], - '135': ['1.3.2.1'], - '133': ['1.4.4.1'], - '2': ['1.1.3'] -} -n_max_trial = 5 -dct_sample_para = { - 'sample': sample, - 'seed': seed, - 'n_iter': n_iter, - 'n_max_trial': n_max_trial, - 'dct_list_init_remove_firm_prod': dct_list_init_remove_firm_prod, -} - class Model(ap.Model): def setup(self): @@ -41,7 +17,7 @@ class Model(ap.Model): self.nprandom = np.random.default_rng(self.p.seed) self.int_n_iter = int(self.p.n_iter) self.int_n_max_trial = int(self.p.n_max_trial) - self.dct_list_remove_firm_prod = self.p.dct_list_init_remove_firm_prod + self.dct_lst_remove_firm_prod = self.p.dct_lst_init_remove_firm_prod # init graph bom BomNodes = pd.read_csv('BomNodes.csv', index_col=0) @@ -77,69 +53,52 @@ class Model(ap.Model): # add edge to G_firm according to G_bom for node in nx.nodes(G_Firm): - # print(node, '-' * 20) for product_code in G_Firm.nodes[node]['Product_Code']: - # print(product_code) for succ_product_code in list(G_bom.successors(product_code)): - # print(succ_product_code) - list_succ_firms = Firm['Code'][Firm[succ_product_code] == - 1].to_list() - list_revenue_log = [ + # for each product of a certain firm + # get each successor (finished product) of this product + # get a list of firm producing this successor + lst_succ_firm = Firm['Code'][Firm[succ_product_code] == + 1].to_list() + lst_succ_firm_size = [ G_Firm.nodes[succ_firm]['Revenue_Log'] - for succ_firm in list_succ_firms + for succ_firm in lst_succ_firm ] - # list_prob = [ - # (v - min(list_revenue_log) + 1) / - # (max(list_revenue_log) - min(list_revenue_log) + 1) - # for v in list_revenue_log - # ] - # list_flag = [ - # self.nprandom.choice([1, 0], p=[prob, 1 - prob]) - # for prob in list_prob - # ] - # # print(list(zip(list_succ_firms,list_flag,list_prob))) - # list_added_edges = [(node, succ_firm, { - # 'Product': product_code - # }) for succ_firm, flag in zip(list_succ_firms, list_flag) - # if flag == 1] - list_prob = [ - size / sum(list_revenue_log) - for size in list_revenue_log + lst_prob = [ + size / sum(lst_succ_firm_size) + for size in lst_succ_firm_size ] - list_f_same_p = Firm['Code'][Firm[product_code] == - 1].to_list() - list_f_size_same_p = [ + # select multiple successors based on relative size of this firm + lst_same_prod_firm = Firm['Code'][Firm[product_code] == + 1].to_list() + lst_same_prod_firm_size = [ G_Firm.nodes[f]['Revenue_Log'] - for f in list_f_same_p + for f in lst_same_prod_firm ] - share = G_Firm.nodes[node]['Revenue_Log'] / sum(list_f_size_same_p) - num_succ_f = round(share * len(list_succ_firms)) if round(share * len(list_succ_firms)) > 0 else 1 - list_choose_firm = self.nprandom.choice(list_succ_firms, num_succ_f, - p=list_prob) - list_choose_firm = list(set(list_choose_firm)) - list_added_edges = [(node, succ_firm, { + share = G_Firm.nodes[node]['Revenue_Log'] / sum( + lst_same_prod_firm_size) + n_succ_firm = round(share * len(lst_succ_firm)) if round( + share * len(lst_succ_firm)) > 0 else 1 # at least one + lst_choose_firm = self.nprandom.choice(lst_succ_firm, + n_succ_firm, + p=lst_prob) + lst_choose_firm = list( + set(lst_choose_firm + )) # nprandom.choice may have duplicates + lst_add_edge = [(node, succ_firm, { 'Product': product_code - }) for succ_firm in list_choose_firm] - G_Firm.add_edges_from(list_added_edges) - # print('-' * 20) + }) for succ_firm in lst_choose_firm] + G_Firm.add_edges_from(lst_add_edge) self.firm_network = ap.Network(self, G_Firm) self.product_network = ap.Network(self, G_bom) - # print([node.label for node in self.firm_network.nodes]) - # print([list(self.firm_network.graph.predecessors(node)) - # for node in self.firm_network.nodes]) - # print([self.firm_network.graph.nodes[node.label]['Name'] - # for node in self.firm_network.nodes]) - # print([v for v in self.firm_network.graph.nodes(data=True)]) # init product for ag_node, attr in self.product_network.graph.nodes(data=True): - product_agent = ProductAgent(self, - code=ag_node.label, - name=attr['Name']) - self.product_network.add_agents([product_agent], [ag_node]) - self.a_list_total_products = ap.AgentList(self, - self.product_network.agents) + product = ProductAgent(self, code=ag_node.label, name=attr['Name']) + self.product_network.add_agents([product], [ag_node]) + self.a_lst_total_products = ap.AgentList(self, + self.product_network.agents) # init firm for ag_node, attr in self.firm_network.graph.nodes(data=True): @@ -149,201 +108,164 @@ class Model(ap.Model): name=attr['Name'], type_region=attr['Type_Region'], revenue_log=attr['Revenue_Log'], - a_list_product=self.a_list_total_products.select([ + a_lst_product=self.a_lst_total_products.select([ code in attr['Product_Code'] - for code in self.a_list_total_products.code + for code in self.a_lst_total_products.code ])) - # init capacity based on discrete uniform distribution - # list_out_edges = list( - # self.firm_network.graph.out_edges(ag_node, - # keys=True, - # data='Product')) - # for product in firm_agent.a_list_product: - # capacity = len([ - # edge for edge in list_out_edges if edge[-1] == - # product.code]) - # firm_agent.dct_prod_capacity[product] = capacity - for product in firm_agent.a_list_product: + # init extra capacity based on discrete uniform distribution + for product in firm_agent.a_lst_product: firm_agent.dct_prod_capacity[product] = self.nprandom.integers( firm_agent.revenue_log / 5, firm_agent.revenue_log / 5 + 2) # print(firm_agent.name, firm_agent.dct_prod_capacity) self.firm_network.add_agents([firm_agent], [ag_node]) - self.a_list_total_firms = ap.AgentList(self, self.firm_network.agents) - # print(list(zip(self.a_list_total_firms.code, - # self.a_list_total_firms.name, - # self.a_list_total_firms.capacity))) + self.a_lst_total_firms = ap.AgentList(self, self.firm_network.agents) # init dct_list_remove_firm_prod (from string to agent) t_dct = {} - for firm_code, list_product in self.dct_list_remove_firm_prod.items(): - firm = self.a_list_total_firms.select( - self.a_list_total_firms.code == firm_code)[0] - t_dct[firm] = self.a_list_total_products.select([ - code in list_product - for code in self.a_list_total_products.code + for firm_code, lst_product in self.dct_lst_remove_firm_prod.items(): + firm = self.a_lst_total_firms.select( + self.a_lst_total_firms.code == firm_code)[0] + t_dct[firm] = self.a_lst_total_products.select([ + code in lst_product for code in self.a_lst_total_products.code ]) - self.dct_list_remove_firm_prod = t_dct - self.dct_list_disrupt_firm_prod = t_dct + self.dct_lst_remove_firm_prod = t_dct + self.dct_lst_disrupt_firm_prod = t_dct # init output - self.list_dct_list_remove_firm_prod = [] - self.list_dct_list_disrupt_firm_prod = [] + self.lst_dct_lst_remove_firm_prod = [] + self.lst_dct_lst_disrupt_firm_prod = [] # set the initial firm product that are removed - for firm, a_list_product in self.dct_list_remove_firm_prod.items(): - for product in a_list_product: - assert product in firm.a_list_product, \ + for firm, a_lst_product in self.dct_lst_remove_firm_prod.items(): + for product in a_lst_product: + assert product in firm.a_lst_product, \ f"product {product.code} not in firm {firm.code}" - firm.a_list_product_removed.append(product) + firm.a_lst_product_removed.append(product) # draw network # self.draw_network() def update(self): - self.a_list_total_firms.clean_before_time_step() + self.a_lst_total_firms.clean_before_time_step() # output - self.list_dct_list_remove_firm_prod.append( - (self.t, self.dct_list_remove_firm_prod)) - self.list_dct_list_disrupt_firm_prod.append( - (self.t, self.dct_list_disrupt_firm_prod)) + self.lst_dct_lst_remove_firm_prod.append( + (self.t, self.dct_lst_remove_firm_prod)) + self.lst_dct_lst_disrupt_firm_prod.append( + (self.t, self.dct_lst_disrupt_firm_prod)) # stop simulation if reached terminal number of iteration if self.t == self.int_n_iter or len( - self.dct_list_remove_firm_prod) == 0: + self.dct_lst_remove_firm_prod) == 0: self.int_stop_times = self.t - print(self.int_stop_times, self.t) self.stop() def step(self): - # shuffle self.dct_list_remove_firm_prod - # dct_key_list = list(self.dct_list_remove_firm_prod.keys()) - # self.nprandom.shuffle(dct_key_list) - # self.dct_list_remove_firm_prod = { - # key: self.dct_list_remove_firm_prod[key].shuffle() - # for key in dct_key_list - # } - # print(self.dct_list_remove_firm_prod) - print('\n', '=' * 20, 'step', self.t, '=' * 20) print( 'dct_list_remove_firm_prod', { key.name: value.code - for key, value in self.dct_list_remove_firm_prod.items() + for key, value in self.dct_lst_remove_firm_prod.items() }) # remove_edge_to_cus_and_cus_up_prod - for firm, a_list_product in self.dct_list_remove_firm_prod.items(): - for product in a_list_product: + for firm, a_lst_product in self.dct_lst_remove_firm_prod.items(): + for product in a_lst_product: firm.remove_edge_to_cus_remove_cus_up_prod(product) for n_trial in range(self.int_n_max_trial): print('=' * 10, 'trial', n_trial, '=' * 10) # seek_alt_supply - # shuffle self.a_list_total_firms - self.a_list_total_firms = self.a_list_total_firms.shuffle() - for firm in self.a_list_total_firms: - if len(firm.a_list_up_product_removed) > 0: - # print(firm.name) - # print(firm.a_list_up_product_removed.code) + # shuffle self.a_lst_total_firms + self.a_lst_total_firms = self.a_lst_total_firms.shuffle() + for firm in self.a_lst_total_firms: + if len(firm.a_lst_up_product_removed) > 0: firm.seek_alt_supply() # handle_request - # shuffle self.a_list_total_firms - self.a_list_total_firms = self.a_list_total_firms.shuffle() - for firm in self.a_list_total_firms: + # shuffle self.a_lst_total_firms + self.a_lst_total_firms = self.a_lst_total_firms.shuffle() + for firm in self.a_lst_total_firms: if len(firm.dct_request_prod_from_firm) > 0: firm.handle_request() # reset dct_request_prod_from_firm - self.a_list_total_firms.clean_before_trial() + self.a_lst_total_firms.clean_before_trial() # do not use: - # self.a_list_total_firms.dct_request_prod_from_firm = {} why? + # self.a_lst_total_firms.dct_request_prod_from_firm = {} why? - # based on a_list_up_product_removed, - # update a_list_product_disrupted / a_list_product_removed - # update dct_list_disrupt_firm_prod / dct_list_remove_firm_prod - self.dct_list_remove_firm_prod = {} - self.dct_list_disrupt_firm_prod = {} - for firm in self.a_list_total_firms: - if len(firm.a_list_up_product_removed) > 0: - print(firm.name, 'a_list_up_product_removed', [ - product.code for product in firm.a_list_up_product_removed + # based on a_lst_up_product_removed + # update a_lst_product_disrupted / a_lst_product_removed + # update dct_lst_disrupt_firm_prod / dct_lst_remove_firm_prod + self.dct_lst_remove_firm_prod = {} + self.dct_lst_disrupt_firm_prod = {} + for firm in self.a_lst_total_firms: + if len(firm.a_lst_up_product_removed) > 0: + print(firm.name, 'a_lst_up_product_removed', [ + product.code for product in firm.a_lst_up_product_removed ]) - for product in firm.a_list_product: + for product in firm.a_lst_product: n_up_product_removed = 0 - for up_product_removed in firm.a_list_up_product_removed: + for up_product_removed in firm.a_lst_up_product_removed: if product in up_product_removed.a_successors(): n_up_product_removed += 1 if n_up_product_removed == 0: continue else: - # update a_list_product_disrupted / dct_list_disrupt_firm_prod - if product not in firm.a_list_product_disrupted: - firm.a_list_product_disrupted.append(product) - if firm in self.dct_list_disrupt_firm_prod.keys(): - self.dct_list_disrupt_firm_prod[firm].append( + # update a_lst_product_disrupted / dct_lst_disrupt_firm_prod + if product not in firm.a_lst_product_disrupted: + firm.a_lst_product_disrupted.append(product) + if firm in self.dct_lst_disrupt_firm_prod.keys(): + self.dct_lst_disrupt_firm_prod[firm].append( product) else: - self.dct_list_disrupt_firm_prod[ + self.dct_lst_disrupt_firm_prod[ firm] = ap.AgentList( self.model, [product]) - # update a_list_product_removed / dct_list_remove_firm_prod + # update a_lst_product_removed / dct_list_remove_firm_prod + # mark disrupted firm as removed based conditionally lost_percent = n_up_product_removed / len( product.a_predecessors()) - list_revenue_log = self.a_list_total_firms.revenue_log - std_size = (firm.revenue_log - min(list_revenue_log) + - 1) / (max(list_revenue_log) - - min(list_revenue_log) + 1) - p_remove = 1 - std_size * (1 - lost_percent) - flag = self.nprandom.choice([1, 0], - p=[p_remove, 1 - p_remove]) - # flag = 1 - if flag == 1: - firm.a_list_product_removed.append(product) - # if firm in - # self.dct_list_remove_firm_prod[firm] = firm.a_list_product_removed - if firm in self.dct_list_remove_firm_prod.keys(): - self.dct_list_remove_firm_prod[firm].append( + lst_size = self.a_lst_total_firms.revenue_log + std_size = (firm.revenue_log - min(lst_size) + + 1) / (max(lst_size) - min(lst_size) + 1) + prod_remove = 1 - std_size * (1 - lost_percent) + if self.nprandom.choice( + [True, False], p=[prod_remove, 1 - prod_remove]): + firm.a_lst_product_removed.append(product) + if firm in self.dct_lst_remove_firm_prod.keys(): + self.dct_lst_remove_firm_prod[firm].append( product) else: - self.dct_list_remove_firm_prod[ + self.dct_lst_remove_firm_prod[ firm] = ap.AgentList( self.model, [product]) - # # update the firm that is removed - # self.dct_list_remove_firm_prod = {} - # for firm in self.a_list_total_firms: - # if len(firm.a_list_product_removed) > 0: - # self.dct_list_remove_firm_prod[ - # firm] = firm.a_list_product_removed - # print(self.dct_list_remove_firm_prod) print( 'dct_list_remove_firm_prod', { key.name: value.code - for key, value in self.dct_list_remove_firm_prod.items() + for key, value in self.dct_lst_remove_firm_prod.items() }) def end(self): print('/' * 20, 'output', '/' * 20) print('dct_list_remove_firm_prod') - for t, dct in self.list_dct_list_remove_firm_prod: - for firm, a_list_product in dct.items(): - for product in a_list_product: + for t, dct in self.lst_dct_lst_remove_firm_prod: + for firm, a_lst_product in dct.items(): + for product in a_lst_product: print(t, firm.name, product.code) - print('dct_list_disrupt_firm_prod') - for t, dct in self.list_dct_list_disrupt_firm_prod: - for firm, a_list_product in dct.items(): - for product in a_list_product: + print('dct_lst_disrupt_firm_prod') + for t, dct in self.lst_dct_lst_disrupt_firm_prod: + for firm, a_lst_product in dct.items(): + for product in a_lst_product: print(t, firm.name, product.code) qry_result = db_session.query(Result).filter_by(s_id=self.sample.id) if qry_result.count() == 0: lst_result_info = [] - for t, dct in self.list_dct_list_disrupt_firm_prod: - for firm, a_list_product in dct.items(): - for product in a_list_product: - # print(t, firm.name, product.code) + for t, dct in self.lst_dct_lst_disrupt_firm_prod: + for firm, a_lst_product in dct.items(): + for product in a_lst_product: db_r = Result(s_id=self.sample.id, id_firm=firm.code, id_product=product.code, @@ -352,11 +274,10 @@ class Model(ap.Model): lst_result_info.append(db_r) db_session.bulk_save_objects(lst_result_info) db_session.commit() - for t, dct in self.list_dct_list_remove_firm_prod: - for firm, a_list_product in dct.items(): - for product in a_list_product: - # print(t, firm.name, product.code) - # only firm disrupted can be removed + for t, dct in self.lst_dct_lst_remove_firm_prod: + for firm, a_lst_product in dct.items(): + for product in a_lst_product: + # only firm disrupted can be removed theoretically qry_f_p = db_session.query(Result).filter( Result.s_id == self.sample.id, Result.id_firm == firm.code, @@ -364,8 +285,8 @@ class Model(ap.Model): if qry_f_p.count() == 1: qry_f_p.update({"is_removed": True}) db_session.commit() - self.sample.is_done_flag, self.sample.computer_name = 1, platform.node( - ) + self.sample.is_done_flag = 1 + self.sample.computer_name = platform.node() self.sample.stop_t = self.int_stop_times db_session.commit() @@ -401,7 +322,3 @@ class Model(ap.Model): edge_label, font_size=4) plt.savefig("network.png") - - -# model = Model(dct_sample_para) -# model.run() diff --git a/orm.py b/orm.py index 2bb0594..f6d008e 100644 --- a/orm.py +++ b/orm.py @@ -45,7 +45,7 @@ class Experiment(Base): # variables n_max_trial = Column(Integer, nullable=False) - dct_list_init_remove_firm_prod = Column(PickleType, nullable=False) + dct_lst_init_remove_firm_prod = Column(PickleType, nullable=False) sample = relationship('Sample', back_populates='experiment', lazy='dynamic')