From b935f0ce799d3b01aeb18774d9c875349819a84d Mon Sep 17 00:00:00 2001 From: HaoYizhi Date: Mon, 13 Mar 2023 21:53:50 +0800 Subject: [PATCH] database --- __pycache__/computation.cpython-38.pyc | Bin 1542 -> 1542 bytes __pycache__/controller_db.cpython-38.pyc | Bin 6236 -> 6056 bytes __pycache__/model.cpython-38.pyc | Bin 7713 -> 8548 bytes __pycache__/orm.cpython-38.pyc | Bin 3212 -> 3230 bytes controller_db.py | 15 ++++--- model.py | 53 ++++++++++++++++++++--- orm.py | 4 +- 7 files changed, 58 insertions(+), 14 deletions(-) diff --git a/__pycache__/computation.cpython-38.pyc b/__pycache__/computation.cpython-38.pyc index 2049456eb34d4785ea28edd48f489ac7338b9984..2a37f85cb1876e18c7b3c50a45c014349897fdb0 100644 GIT binary patch delta 20 ZcmZqUY2)Dz<>lpK0D?9F{*BzfSpglpK0D=mC-i_S9Spg?)1UUcz diff --git a/__pycache__/controller_db.cpython-38.pyc b/__pycache__/controller_db.cpython-38.pyc index 03cb1bae59595331773d26d43c4ab772ba891834..82956738859274cebfb41db51c20ec11e8a49186 100644 GIT binary patch delta 1007 zcmZ{i&ubG=5XblJZZ^M~-==9}YjY?mvDzjzN?UA6e^4#0KN}0yrZLTKn~nL^w_B|x z5v1ThFbG1C)`Osk2dOvp5AYy(5D%;1$%EoS@G9t=P3b{!;eD9-?##S-%zTbN>33am zxf~q(zD7NA<%R3F=(aFVVrPOKO^1*rF)mwW+bU7XDrxY#Sm(FMB$qk1$Tx(BSm$;v zb)n7`CE4E2)x}2w;dHC)s0-Vi?0hI}5yGvF!+Xn+OGqT+o7O~MvhQSH0=!Z`u|Z1e zg1b`Btc8ZyeTF!t((;0>q-uIr&g+I%l{d1=M$HhIN84FQ6JfME7<$>!4l!`VLHxu` zM0*Q_tvlZ1OrrgWIHDWgTF*yKu^asagF%#kOJcdvy%Af62nNZ*j=hu2z#IFR4=a5m zh(GL$i3WHa3uFL_j_^_Q7AqFyRoReowVG0q4d3#5=|)z|-BhyG<)V_;HJXHHj#Nhq zBZ=0$dYC!Xd`%~_ruVGQmpmDS*RCU$(@a=`FRnA>BJ{YI$OznXA0eag*c~UA;G=u4 zZ3dHZPKJ~(RMosfMN}@pm}fFDg{Ik+w4#=Dh0<|&;F)rtHRENaq%bWBKRlN$=TL$P zZ#P+lf_IzT0nyixWNwa&*(Q@TlNzowaKf%Xr zkoNa>OtWIK{c#FZ(aa$j00wm65V)xncZ=eb=_;bcV6BTRpnX9zI^+1>txl8Jy>>>a z91W?Ogm}z-oF=k2Cd9+E@iLGQ{{hd22Y4KhJR_BJ9AFX>mhR8z{Lbg~IsWRzFSCVf zg@Pr)^TW?J{orxoPsKJt)8L$k6Fn1S>fGIg^puCvinOuzk=)63ltiKOgw#>ttyAe9 z%D~l8H?~0QX_@zUM@!U1yG?x3(G$6+C;CG|q;0zWMrz;#3l+EA_mEmQP6FWZ6fb0^6ty$1M#KS9eNWy z`6+}2grj0vzjQ1kkD`7WASLX-m1coneDbZnL0;h}L4HJ_<<{*Ny`n~tSqpYqpHDAwAIEhlpCFej+hnHNWVNtCU# zhgQ)_qJF7!pz!SOhe29>@|pEiE-!;vHKUk2v0G~}j$h+NXM6?J8{)&lJLH<^6>gHU z*e^_x3u4NiCD+A!_LcGXG2mT*RP#0i-(y@s=BjvRuZ&zoG3!;kAHD!v~qlXdai;C<2+ouSTTtiq~Eb#0H}B0YemBNCOaNENGFyk54h zZZghFE}Y%*Qsg$n1}ohPcnRiRnk%_2S~@B26zB81#y@n$6kivQAHmPYvGt1D->V#F zQGc}aXj&J46lb(9h*3#Q4kE$ZpSXAbS%*M)>#U{9{z{ z!h8{74S@kvanYOCE0KTYLU*za!uL)&PP XX^l5fYi52NDh2!?Bpnm&vH8CM22Lv$ diff --git a/__pycache__/model.cpython-38.pyc b/__pycache__/model.cpython-38.pyc index 19ab1d46df32f99a2e22b1beba971456607f52b9..d18096c5a55e8ee4151cc6857aaebb5fa9014abd 100644 GIT binary patch delta 3776 zcmZt}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&%Rk$PY%Y zsvYWw_|yiKw8mLD)BHKi_0;2p{aor@!b@+R3+J&MO|sDh)D6$i`DAkT|(@8l}NH1CRr_r zc&MTWi^7AR#Dh0Mgx-Z7{0qEE4&FQn{sVe)COK#iMNG&i^CpwW%=dj?G~YCxTaIHD z@Y+3G<4<>-=c>Zwwz`RI6+a_?6yGXc`LeVNi9@3=$ZpiV1|gR5~M>QSBV3 z%oO1y;ZI{(Pc!nFaqQwOeK|##Bhbijny`0JFfo_v>jMbAgq4Krb0)tT*L$fRCxggM zph`b=vMR_M+EE1iF&qzigEmY+BCStG@vx48e~8 delta 855 zcmb7?&ui3B5XU#!&5z9{@zD$BMOt*qNxsQ8Pzl_KKF*6Jab5cj=8B-ss(Ca&R>&<^aPtIf&w+9gu_+&C~-g`6OnYR-!C!D*EW0mmv zvhA|rC+E4UNU^0pL3-s~oh;r*ER!h{C_Xy^l-Fhi?G#5MxH zkxH?mt$1znxAx;4{d(fF)=)h0+B_+~Yde$8VsDUzNgDGtIDx-Ev`3HI5rgAs-++er zsvo@|&3bhA5@sek2D4ukE1M?`YO)`aSl@yaS9=>R_26nxQ)}~IYT&0 z_>)+XQ(HVUrmoD=${gW5fl7vX!u~?RM4!~x`;dgWtneUWQtTPG7koX-`)QDaOPeN( zC>SO5S2ddAfjO--Ne$(V_-HPRMcp3iOlQWX>f2o7zikI?m#KySz?glIA7Un};tp%G z>>p+|WC8Y6Y#GNpG3f!dNu9e|sJMMPIAiEEWvv0ZiksoGw( zIjqC~=mtx8g{)*KnvVk$tVfXe78)t^*`S+CHP67t7~hVvfJGS$(ws94$*_o||B$nh zywra{S&xl3vuJD4(T;d$)m>Zp^{TE49kFBeaH!{N_mD4Y*Y%>qOJcXSuw2YkAw-7` jrjn$Y@I1tc3#G{b8ym*4w7S4?2FV^9qS3wLmc8~94L!%N diff --git a/controller_db.py b/controller_db.py index c726afa..6f648b2 100644 --- a/controller_db.py +++ b/controller_db.py @@ -45,12 +45,15 @@ class ControllerDB: Firm['Code'] = Firm['Code'].astype('string') Firm.fillna(0, inplace=True) list_dct = [] - for _, row in Firm.iterrows(): - code = row['Code'] - row = row['1':] - for product_code in row.index[row == 1].to_list(): - dct = {code: [product_code]} - list_dct.append(dct) + # for _, row in Firm.iterrows(): + # code = row['Code'] + # row = row['1':] + # for product_code in row.index[row == 1].to_list(): + # dct = {code: [product_code]} + # list_dct.append(dct) + # # break + # # break + list_dct = [{'140': ['1.4.5.1']}] for idx_exp, dct in enumerate(list_dct): self.add_experiment_1(idx_exp, self.dct_parameter['n_max_trial'], dct) diff --git a/model.py b/model.py index 0e946b7..93221d1 100644 --- a/model.py +++ b/model.py @@ -5,6 +5,8 @@ import random import networkx as nx from firm import FirmAgent from product import ProductAgent +from orm import db_session, Sample, Result +import platform sample = 0 seed = 0 @@ -34,6 +36,7 @@ dct_sample_para = { class Model(ap.Model): def setup(self): self.sample = self.p.sample + self.int_stop_times, self.int_stop_t = 0, None self.random = random.Random(self.p.seed) self.nprandom = np.random.default_rng(self.p.seed) self.int_n_iter = int(self.p.n_iter) @@ -80,7 +83,7 @@ class Model(ap.Model): 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() + 1].to_list() list_revenue_log = [ G_Firm.nodes[succ_firm]['Revenue_Log'] for succ_firm in list_succ_firms @@ -191,12 +194,16 @@ class Model(ap.Model): def update(self): self.a_list_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.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)) # 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.int_stop_times = self.t + print(self.int_stop_times, self.t) self.stop() def step(self): @@ -245,13 +252,15 @@ class Model(ap.Model): # self.a_list_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 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]) + print(firm.name, 'a_list_up_product_removed', [ + product.code for product in firm.a_list_up_product_removed + ]) for product in firm.a_list_product: n_up_product_removed = 0 for up_product_removed in firm.a_list_up_product_removed: @@ -307,7 +316,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.list_dct_list_remove_firm_prod: for firm, a_list_product in dct.items(): @@ -319,6 +328,38 @@ class Model(ap.Model): for product in a_list_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) + 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.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 + 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, self.sample.computer_name = 1, platform.node( + ) + self.sample.stop_t = self.int_stop_times + db_session.commit() + def draw_network(self): import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = 'SimHei' diff --git a/orm.py b/orm.py index 94283ff..2bb0594 100644 --- a/orm.py +++ b/orm.py @@ -77,8 +77,8 @@ class Result(Base): id = Column(Integer, primary_key=True, autoincrement=True) s_id = Column(Integer, ForeignKey('{}.id'.format(f"{db_name_prefix}_sample")), nullable=False) - id_firm = Column(Integer, nullable=False) - id_product = Column(Integer, nullable=False) + id_firm = Column(String(10), nullable=False) + id_product = Column(String(10), nullable=False) ts = Column(Integer, nullable=False) is_disrupted = Column(Boolean, nullable=True) is_removed = Column(Boolean, nullable=True)