From dd8a786f6cf76ac7097400c8e6d05eb1a83af8cf Mon Sep 17 00:00:00 2001 From: HaoYizhi Date: Sun, 18 Jun 2023 17:29:11 +0800 Subject: [PATCH] dct_prod_up_prod_stat + no need for dct_list_remove_firm_prod/dct_list_disrupt_firm_prod --- __pycache__/controller_db.cpython-38.pyc | Bin 7907 -> 7909 bytes __pycache__/firm.cpython-38.pyc | Bin 6685 -> 7734 bytes __pycache__/model.cpython-38.pyc | Bin 13212 -> 12963 bytes controller_db.py | 4 +- firm.py | 128 +++++++++----- model.py | 210 ++++++++++++++--------- test.ipynb | 32 ++++ xv_without_exp.csv | 2 +- 8 files changed, 247 insertions(+), 129 deletions(-) diff --git a/__pycache__/controller_db.cpython-38.pyc b/__pycache__/controller_db.cpython-38.pyc index 9e762976c8ba1276aff1aab363fc428185e28bc4..7682b7fa793237cb6f58a03ee0ca1ef2a3941873 100644 GIT binary patch delta 46 zcmaEC`_z^@l$V!_0SFR0dpB}NaB-VkRXeTu7pHvwB87Q2><3EnX(HzmxN`l|AB)_9M($U}1 z9NiV3zNwcCM_kZ~#>b%5ma3Ev|0D{h@c1tyae17Qbc7?{(Mr0bI|kl{V>%Yzrjv4P zye%i~WbjToStp0L?Tk5jywlFOGl6%;nFPnFD1EN3)=sau&7f*w^!_h}43*E|@&AS- z)I7}-p;pF&oJ3BBRMtbv8=;QW3=O1KXd+Do_I4VysU4!q1ljEz^<2?*h<<^Nws1 z-}zvHOGmkMU$1mOmmY95+VrKOjKq=>SmZ@yEkCFP8$R9}?RMjO@xjPkYdLNs%GIz_ z<)$0F)l#d`cw4ocjcSlo7*$Y3MzgZ!M%mzc+bt`1rQT{rS>;}Jn;UMq(OQYdD&>YB zlzW{c!>Lz;$gZi@+Nyiqk0u?ANR8!crCq7kgX@v3w4*d1(93l{h>na5P)V<-hQfbNoq1vTHQ!bK^63k&U08ml>7G^ftM2lvs^z)WVEIB-oxZ%be75d`2%Ct* zT|R%lf+||%@%Ht|^xa^iO?u-$g+vn>k&%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Jf0&OfFm3yys zb!4GqgTlY zm*@$Gyf>ZAkWgE{7~|P}iAmZ%JRIqWX&mHd`l#7PJE(WPE5S-n;sJ;rQr{ys#EW;eKp>novMKFS*ILdTHM=K|iGx7xKcaT;`SA#SjPaoC2E}o74 zNlK+Fc<)%H1N$yk3(E04dUN+Y<$4}o>Nfy~o+^6Y`bNE-w8;BNg)>CHOJtb{9VY1! z`2mqS5xuozQlUChg}k7?s3CM*aPD(nalY=%Ifi5E)~cm)b^13*|9Wyy{R+A(OTXFc U4hQj7&Z$J%XXym$T>x|Ae~jPYegFUf diff --git a/__pycache__/model.cpython-38.pyc b/__pycache__/model.cpython-38.pyc index e4dd652a9d3c077e78eca7ee463dbaa064e9c861..233a5ebaa340fb9f7231ddc34348983592fe32b5 100644 GIT binary patch delta 3073 zcmZuzeQZ zoKoi9g-!M**z2NwYt%AYF-!;J3^v&wefW3xo` zoI*1Tp6qoTmt?EA8+zp-Z%4!PI6G!P zFkA7VXU{C?*%51~>?y&#=Z5DT@IAcYx{jVkDhI6Z%71%@pbx!=<**TgetE(O`(m{? zc7zW|%jkze`IfP9bnlAY;C1|(fuEyYR2~S{$-eps{8C2iOYkT8as8L@x%_KGx38+@tSKv>tHQVC z`+?S_VB>q>KZtepwjvtwB@W5IH@*CjEJ-*v1%(q@lApjR6Mv;8BjE{H> zxw&8}ZFS|Wbf)WUe0nOMi$@G`Np@~+AO9X{&k?zXr0ULy3n@zslE6vPvm{l?G~yO7 zk@%1!p(Hp_s*079*{$v25Xuokzkx6Ypc#Vr(-Z|X`OenPq8}$cI9xb#|3$Pd#(^u` zh194OGUKsu&<$|AzIsPI*!0SA?AER#cexnE4wWlvNW8-hBL;B zr@tt;F{IADwwvoEwHB_TFJii4h+{&*&^HS(!wT91$(sEC@Z7+D(RrW*+?Z7tKn$Q&9i<;_1{{kZePvU%EiJ(5JA`L(wfs{p-k8l|l4O_STWmaigM2$99 zD0*obABxd$;)M3Q@oDaAG&kul3qbcGu$5;RS;^9J5H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|2JJ0PzS2GOaAjVk3H_MdW%@=T0NhA)Emw^a(c<9kRILjl! zs>F5K-A$~MIkcSN@KiFJ=*SuOSYeyEHT*>768dB55F@O@8LZuFFeQEod@Sq@(;%Q? znCi;fuP3MhFoHA;5(N7+)pKvZ^ZJpzX=O)nVKcC>JAa5xI1s|^QP4PmOb$Fc(h_^pUXxy?WeU)s!gbGN z$EtG#5R0TWXz(WizdX?HO3eq)q6)l)K;VUbQ$e}8tT||?q`j^;?RPa89_+r$-m=eS zHQn_|s5@MIq&NbJ1Fn4S00*!oAua?_oG+vxQ`2{4I1U-s7F-KEtdeeNrpqvg0a8Fx z$+cqY46A#v2jxz@a8izhCk4+WiaVESi|-U`#bp!GRx#S04(vL~cLGJMQH9jQiN!6MB{Cvvcc-b}7W<0r0{(W@Aqsm`JU%9$`4jv8J*e$QlF*Y- z%xQxsH1wT?YDpxz14j*yl{5c}EwvZ?NvIRrNP!W7H-!&4Xb#Fg9aO#8HTcW`1t6B} zr_ZpT!tn3I`O2$Xe?{rD;)#(Rv|YdpRDT8ncEh#Izb5?KzXi|5~byYZVKNa* 0: if len(lst_firm) == 0: @@ -210,6 +233,7 @@ class FirmAgent(ap.Agent): self.accept_request(select_customer, product) def accept_request(self, down_firm, product): + # para product is the product that self is selling prod_accept = self.flt_diff_new_conn if self.model.nprandom.choice([True, False], p=[prod_accept, 1 - prod_accept]): @@ -221,11 +245,23 @@ class FirmAgent(ap.Agent): ]) self.dct_prod_capacity[product] -= 1 self.dct_request_prod_from_firm[product].remove(down_firm) - down_firm.a_lst_up_product_removed.remove(product) - # print( - # f"{self.name} accept {product.code} request " - # f"from {down_firm.name}" - # ) + # down_firm.a_lst_up_product_removed.remove(product) + + for prod in down_firm.dct_prod_up_prod_stat.keys(): + if product in down_firm.dct_prod_up_prod_stat[ + prod]['supply'].keys(): + down_firm.dct_prod_up_prod_stat[ + prod]['supply'][product] = True + down_firm.dct_prod_up_prod_stat[ + prod]['status'].append(('N', self.model.t)) + # print(down_firm.dct_prod_up_prod_stat) + del down_firm.dct_n_trial_up_prod_removed[product] + del down_firm.dct_cand_alt_supply_up_prod_removed[product] + + print( + f"{self.name} accept {product.code} request " + f"from {down_firm.name}" + ) else: down_firm.dct_cand_alt_supply_up_prod_removed[product].remove(self) @@ -233,8 +269,10 @@ class FirmAgent(ap.Agent): self.dct_request_prod_from_firm = {} def clean_before_time_step(self): - self.dct_n_trial_up_prod_removed = {} - self.a_lst_up_product_removed = ap.AgentList(self.model, []) + self.dct_n_trial_up_prod_removed = \ + dict.fromkeys(self.dct_n_trial_up_prod_removed.keys(), 0) + self.dct_cand_alt_supply_up_prod_removed = {} + # self.a_lst_up_product_removed = ap.AgentList(self.model, []) def get_firm_network_node(self): return self.firm_network.positions[self] diff --git a/model.py b/model.py index 551bd3f..a0b48fe 100644 --- a/model.py +++ b/model.py @@ -235,11 +235,13 @@ class Model(ap.Model): code in lst_product for code in self.a_lst_total_products.code ]) self.dct_lst_remove_firm_prod = t_dct - self.dct_lst_disrupt_firm_prod = t_dct + # self.dct_lst_disrupt_firm_prod = t_dct - # init output - self.lst_dct_lst_remove_firm_prod = [] - self.lst_dct_lst_disrupt_firm_prod = [] + # # init output + # self.lst_dct_lst_remove_firm_prod = [] + # self.lst_dct_lst_disrupt_firm_prod = [] + + # self.dct_ts_dct_a_lst_remove = {} # set the initial firm product that are removed for firm, a_lst_product in self.dct_lst_remove_firm_prod.items(): @@ -247,6 +249,8 @@ class Model(ap.Model): assert product in firm.a_lst_product, \ f"product {product.code} not in firm {firm.code}" firm.a_lst_product_removed.append(product) + firm.dct_prod_up_prod_stat[ + product]['status'].append(('R', self.t)) # proactive strategy # get all the firm prod affected @@ -356,20 +360,34 @@ class Model(ap.Model): def update(self): self.a_lst_total_firms.clean_before_time_step() - # output - 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)) + # # output + # 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_lst_remove_firm_prod) == 0: - self.int_stop_times = self.t - self.stop() + # # stop simulation if reached terminal number of iteration + # if self.t == self.int_n_iter or len( + # self.dct_lst_remove_firm_prod) == 0: + # self.int_stop_times = self.t + # self.stop() + + if self.t > 0: + 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' and ts != 0: + print("not stop because", firm.name, prod.code) + break + else: + continue + break + else: + self.int_stop_times = self.t + self.stop() def step(self): - # print('\n', '=' * 20, 'step', self.t, '=' * 20) + print('\n', '=' * 20, 'step', self.t, '=' * 20) # print( # 'dct_list_remove_firm_prod', { # key.name: value.code @@ -377,18 +395,33 @@ class Model(ap.Model): # }) # remove_edge_to_cus_and_cus_up_prod - 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 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' and ts == self.t-1: + firm.remove_edge_to_cus_remove_cus_up_prod(prod) for n_trial in range(self.int_n_max_trial): - # print('=' * 10, 'trial', n_trial, '=' * 10) + print('=' * 10, 'trial', n_trial, '=' * 10) # seek_alt_supply # 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() + # if len(firm.a_lst_up_product_removed) > 0: + # firm.seek_alt_supply() + lst_seek_prod = [] + for prod in firm.dct_prod_up_prod_stat.keys(): + status = firm.dct_prod_up_prod_stat[prod]['status'][-1][0] + if status != 'N': + for supply in firm.dct_prod_up_prod_stat[ + prod]['supply'].keys(): + if not firm.dct_prod_up_prod_stat[ + prod]['supply'][supply]: + lst_seek_prod.append(supply) + # commmon supply only seek once + lst_seek_prod = list(set(lst_seek_prod)) + for supply in lst_seek_prod: + firm.seek_alt_supply(supply) # handle_request # shuffle self.a_lst_total_firms @@ -402,35 +435,43 @@ class Model(ap.Model): # do not use: # self.a_lst_total_firms.dct_request_prod_from_firm = {} why? - # based on a_lst_up_product_removed + # based on dct_prod_up_prod_stat # 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 = {} + # 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_lst_product: - n_up_product_removed = 0 - 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 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.dct_prod_up_prod_stat.keys(): + status = firm.dct_prod_up_prod_stat[product]['status'][-1][0] + if status == 'D': + print(firm.name, 'disrupted product: ', product.code) + n_up_product_removed = \ + sum([not stat for stat in + firm.dct_prod_up_prod_stat[ + product]['supply'].values()]) + # for product in firm.a_lst_product: + # n_up_product_removed = 0 + # 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_lst_product_disrupted - # update 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_lst_disrupt_firm_prod[ - firm] = ap.AgentList( - self.model, [product]) + # # update a_lst_product_disrupted + # # update 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_lst_disrupt_firm_prod[ + # firm] = ap.AgentList( + # self.model, [product]) # update a_lst_product_removed # update dct_list_remove_firm_prod # mark disrupted firm as removed based conditionally @@ -457,13 +498,20 @@ class Model(ap.Model): p=[prob_remove, 1 - prob_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_lst_remove_firm_prod[ - firm] = ap.AgentList( - self.model, [product]) + # if firm in self.dct_lst_remove_firm_prod.keys(): + # self.dct_lst_remove_firm_prod[firm].append( + # product) + # else: + # self.dct_lst_remove_firm_prod[ + # firm] = ap.AgentList( + # self.model, [product]) + + firm.dct_prod_up_prod_stat[ + product]['status'].append(('R', self.t)) + print(firm.name, 'removed product: ', product.code) + else: + firm.dct_prod_up_prod_stat[ + product]['status'].append(('N', self.t)) # print( # 'dct_list_remove_firm_prod', { @@ -472,7 +520,7 @@ class Model(ap.Model): # }) def end(self): - # print('/' * 20, 'output', '/' * 20) + print('/' * 20, 'output', '/' * 20) # print('dct_list_remove_firm_prod') # for t, dct in self.lst_dct_lst_remove_firm_prod: # for firm, a_lst_product in dct.items(): @@ -484,35 +532,35 @@ class Model(ap.Model): # 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.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, - ts=t, - is_disrupted=True) - lst_result_info.append(db_r) - db_session.bulk_save_objects(lst_result_info) - db_session.commit() - 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, - Result.id_product == product.code) - if qry_f_p.count() == 1: - qry_f_p.update({"is_removed": True}) - db_session.commit() - self.sample.is_done_flag = 1 - self.sample.computer_name = platform.node() - self.sample.stop_t = self.int_stop_times - db_session.commit() + # 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.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, + # ts=t, + # is_disrupted=True) + # lst_result_info.append(db_r) + # db_session.bulk_save_objects(lst_result_info) + # db_session.commit() + # 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, + # Result.id_product == product.code) + # if qry_f_p.count() == 1: + # qry_f_p.update({"is_removed": True}) + # db_session.commit() + # self.sample.is_done_flag = 1 + # self.sample.computer_name = platform.node() + # self.sample.stop_t = self.int_stop_times + # db_session.commit() def draw_network(self): import matplotlib.pyplot as plt diff --git a/test.ipynb b/test.ipynb index 04fe93a..759c09e 100644 --- a/test.ipynb +++ b/test.ipynb @@ -266,6 +266,38 @@ " )\n", "lst_choose_firm" ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 0\n", + "0 1\n", + "0 2\n", + "1 0\n", + "1 1\n", + "break\n" + ] + } + ], + "source": [ + "\n", + "for j in range(3):\n", + " for k in range(3):\n", + " print(j, k)\n", + " if j == k == 1:\n", + " print('break')\n", + " break\n", + " else:\n", + " continue\n", + " break\n", + "\n" + ] } ], "metadata": { diff --git a/xv_without_exp.csv b/xv_without_exp.csv index 2d1cd7c..008c91b 100644 --- a/xv_without_exp.csv +++ b/xv_without_exp.csv @@ -1,2 +1,2 @@ n_max_trial,prf_size,prf_conn,cap_limit_prob_type,cap_limit_level,diff_new_conn,crit_supplier,diff_remove,proactive_ratio,netw_prf_n -10,TRUE,TRUE,uniform,10,0.5,1,1,0,2 +10,TRUE,TRUE,uniform,10,0.1,1,0.01,0,2