From 7e8980d0c6d0f8f6d46de3a44a11f692377c2349 Mon Sep 17 00:00:00 2001 From: HaoYizhi Date: Mon, 15 May 2023 13:37:05 +0800 Subject: [PATCH] remove firm that denied request from candidate list --- .vscode/launch.json | 8 ++- __pycache__/computation.cpython-38.pyc | Bin 1542 -> 1546 bytes __pycache__/controller_db.cpython-38.pyc | Bin 6614 -> 6642 bytes __pycache__/firm.cpython-38.pyc | Bin 5329 -> 5719 bytes __pycache__/model.cpython-38.pyc | Bin 7705 -> 7705 bytes __pycache__/orm.cpython-38.pyc | Bin 3321 -> 3359 bytes __pycache__/product.cpython-38.pyc | Bin 1382 -> 1382 bytes computation.py | 5 +- conf_db_prefix.yaml | 2 +- controller_db.py | 2 +- firm.py | 79 ++++++++++++++--------- main.py | 2 +- model.py | 2 +- orm.py | 23 ++++--- product.py | 12 ++-- 15 files changed, 82 insertions(+), 53 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 9e95ab0..2c06221 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,9 +8,13 @@ "name": "Python: Current File", "type": "python", "request": "launch", - "program": "C:\\Users\\ASUS\\OneDrive\\Project\\ScrAbm\\Dissertation\\IIabm\\analysis.py", + "program": "C:\\Users\\ASUS\\OneDrive\\Project\\ScrAbm\\Dissertation\\IIabm\\main.py", "console": "integratedTerminal", - "justMyCode": true + "justMyCode": true, + "args": [ + "--exp", "test", + "--reset_db", "True" + ] } ] } \ No newline at end of file diff --git a/__pycache__/computation.cpython-38.pyc b/__pycache__/computation.cpython-38.pyc index 7da4cf3741e3e2f1c889a6da05098add2f90615a..a9c363952314fa44abdfd9668fa257cc15457e91 100644 GIT binary patch delta 69 zcmZqU>EhuH<>lpK00Q@gi765rd8?Tjr8jpn%QA|nfkYX37&#bOn0XkP7*+nWFmK+* S;>^e^0~BK7Vd7xq0Ac`{)(Or4 delta 65 zcmeC;Y2)Dy<>lpK00LKMffT-tyw%K%Qky%OWf=vOK%$I1j2w(C%sh-*Oq+MHI5YA} O1G!8*OdO0HKnwswVF(NW diff --git a/__pycache__/controller_db.cpython-38.pyc b/__pycache__/controller_db.cpython-38.pyc index c56b41c570d637e34b46a3c735c2e8ce9ded4079..e923c1bcd2cfad1fe0c7ea45f7e8588a08234409 100644 GIT binary patch delta 804 zcmZ{hOK1~e5XX0Rlg%dCynG~$XhF2v2o}?bN)%Eb#M%(V)>0ZNA<4H*lWvpr+iiU` z@l~TB6bj=_MD*fGq~s!=#FO5{gJ6yxJP0D_O+nn*NN?^3Kj!<-f4&)JKPG-8e3L$( zN3g!fFAD7ShVOv{r#nxjO8^(~k7IS1xtXU76(~bXG=(ZKFLN+;O>8MmrK&KIIhnjB zf?(%-l*4Dv-QdS}&H?z0-7W&kJgptlI4xbCe&27h~Z?6===S;nqdVLVPjsJNuh=Z(U$maiA9TFEqc z7Eh=n{wd0W!K3uN}cziesL#_7jyCm86>V5 zy$zhAcdF>w_A@t0;a+m7e6u&#Nv#t^!e(E-NLOmdGlXfvvW1wGQ*^`38+mIdRZ?%_ z)b_8*|5j2FzfA~Gxx*guUnQw4xDuX$75o$)*|$m#nH!{=1iLM#$R00CQ5#c{=hiR! z7Ksk0NZ3Va5pG%Ns)kuGE2Ra^oU5~5=R1_7b@ID-tnb%YZ|#jjjTH=jfQp>8rG>`T lu2^f>-MdOnGcVWq{1u*{vf~6V;U2j~R^->|b_1Ky++T$(!uRi!j&#ece>+mP!?SqV~la3aifWGzNvBNEPgrXJLkLS+&ka8x4WZfqS1QA z|K5IT6kD&O4>ZbEo@ z`%pV=^RLj+_GpVzsw-KWy5E%)u~PJjBH~ZAGLdqhmK%jKs-zNp&giK;Gd@!5F>FBf z)JQQ+rv#Z~QIns?Ox`r#B>MfT8~_G^PF@Hfpgz7AKHM3UI-w2$eLkvW%N8U7S`qbD zK7<8Pnuj7YG{7$-^M{6^t7s1`uh?=`8i=)QK3|(uhg5Zh?BcYQqzsQ+>DXC_R9r8SEMs9A&$=OneBKT|K#EuIH zgF(iullN>X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vDRiG1(i*2@W?#I1=iNMR}j78l7T~ zIW!3uP*6i&L(8CDdah`^4dr13q$%Hx5J4CKSfN}Fo6Q|m14~G1!icz&38o?kAxyfujU zvluC_L-xA)8jPQ!Yjd|+b3NYyt9J%~FtNR3=iW3;(-=)qlbP^@?p7qin@LkNu|<2i zTl}8v7Rw#IbV=Om7^fcznwktJt^pHwWC$yCciOIB;1SSnDc*bH&(vsLQVi!E$0-Xn zls|fROH<9I=J&gOD%#Six>cZ>z(&jT#L-`gqTox8-L&Xc?4uwAF{8Y`Nq zT+z+W+ON7(W$uPw^H0Rroo^1`%Q?N-IbU*Z&vz@IiI=<1)q8O1#}S@FcmV--!p|a{ zLy*g^gWN0w0~X1{Dj3vA8H2{KF=TWZ3P+ozH6z(+ch;|53S0UiP&T!rsFDt?tg`sw Jxy0dg?|=QR)&l?l diff --git a/__pycache__/model.cpython-38.pyc b/__pycache__/model.cpython-38.pyc index 7f4280626424a5ba71302443ed18d3428c4bff08..2d9380b37044e20b4edcb3070b8faeddddffb85e 100644 GIT binary patch delta 478 zcmY+=O-lkn7zglX*EY&F%S{E{g#s&!>KKCIWQS$&glB{Ele zmfk&t!JhY%yWaXmSY=wi5=a6*eCqHrPt$P@BQ71r$qJG}hL9N2&tV?NwQQZxbVY5y z5l9kV{3E^u?ld{ON5x{PKx6RZPw?MNDp8VxoDw68@T3f?GuUL6qkCx@>rJE=$yB}- zOCWjJ3C{8_AA@~T{RBy<26YXO$E4z3$t_Tt$GnLY5Cb|x( zb+jEXVTg9gWuk34^b`)&0CC_#y$@_*)ZN81S&mNdrhmP?z2G?3wwEhkP VMly)VF+_`&sRyTx$UfAxrC;$gchdj> delta 478 zcmY+=O-lkn7zglX*EY&F%~#M}C@@0|f)FZHl+sX(3{8YRXy!`^>Tcr}1l@xuIy?|~ zs;Fa^@GEqR2)ynmD1r`sg68oMG>0G0%>4J+ndwD(5wReO6C-TMAGX@-vv?zrYX4Dq zF%bot=1*pR@{1__n0U#N6ueoa&?ZaMFvB1^4dG-9$skKe3<)v}Q&(5D0HN8wWWDA{ z8lG&+mK5%cbq|jljaHq;;K!C>zlqv?Bm?{QIN5*)`=S)bCfkgFnP#!hBYq^;|F)|f zDMQJ%#=g9F%?ra5q~YBaknwni&}g>wI;CaI?;#bW2o86QWMS7m>nyMZg|Aihf1NMf zZQiqk*(Bol!--V~roA3QVZoauCqTV(zqtsbU!*!NJJI$j8LP z%)wdj7$tb_@9k=GY{KQM#g~2m)PH%+JH1@!88Char|dt1StY)Wa42IU=#z= zNSc}Wn2Ho8^RdZpX6CeJWK5cD&$U}J1jSY)LpF1BuVG|#nY@EXSBi&`je+ey8w(F3 T2NN3;7qftvfP{b$7f>|-ozfw# delta 177 zcmbO)^;42Bl$V!_0SJO#2&Met-N?6^g)w>ZE*1|))5*-NZ{$^gQpF-bf`gHZk&lUo znTHXK83ma5n2IDP|KpI|Y|OTdk 0: if len(lst_firm) == 0: @@ -114,8 +122,12 @@ class FirmAgent(ap.Agent): in_edges = self.model.firm_network.graph.in_edges( self.model.firm_network.positions[firm], keys=True) lst_adj_firm = [] - lst_adj_firm += [ap.AgentIter(self.model, edge[1]).to_list()[0].code for edge in out_edges] - lst_adj_firm += [ap.AgentIter(self.model, edge[0]).to_list()[0].code for edge in in_edges] + lst_adj_firm += \ + [ap.AgentIter(self.model, edge[1]).to_list()[ + 0].code for edge in out_edges] + lst_adj_firm += \ + [ap.AgentIter(self.model, edge[0]).to_list()[ + 0].code for edge in in_edges] if self.code in lst_adj_firm: lst_firm_connect.append(firm) if len(lst_firm_connect) == 0: @@ -124,19 +136,21 @@ class FirmAgent(ap.Agent): lst_prob = [ size / sum(lst_firm_size) for size in lst_firm_size ] - select_customer = self.model.nprandom.choice(lst_firm, - p=lst_prob) + select_customer = \ + self.model.nprandom.choice(lst_firm, p=lst_prob) self.accept_request(select_customer, product) elif len(lst_firm_connect) == 1: self.accept_request(lst_firm_connect[0], product) elif len(lst_firm_connect) > 1: # handling based on size of firm that has connection - lst_firm_size = [firm.revenue_log for firm in lst_firm_connect] + lst_firm_size = [ + firm.revenue_log for firm in lst_firm_connect] lst_prob = [ size / sum(lst_firm_size) for size in lst_firm_size ] - select_customer = self.model.nprandom.choice(lst_firm_connect, - p=lst_prob) + select_customer = \ + self.model.nprandom.choice(lst_firm_connect, + p=lst_prob) self.accept_request(select_customer, product) def accept_request(self, down_firm, product): @@ -151,18 +165,21 @@ class FirmAgent(ap.Agent): (self.firm_network.positions[self], 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_lst_up_product_removed.remove(product) - # print( - # f"{self.name} accept {product.code} request from {down_firm.name}" - # ) + 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) def clean_before_trial(self): self.dct_request_prod_from_firm = {} def clean_before_time_step(self): - self.dct_n_trial_up_product_removed = {} + self.dct_n_trial_up_prod_removed = {} self.a_lst_up_product_removed = ap.AgentList(self.model, []) diff --git a/main.py b/main.py index 04618b7..485fd3c 100644 --- a/main.py +++ b/main.py @@ -38,7 +38,7 @@ if __name__ == '__main__': from controller_db import ControllerDB controller_db = ControllerDB(args.exp, reset_flag=args.reset_sample) # controller_db.reset_db() - + # force drop controller_db.reset_db(force_drop=args.reset_db) diff --git a/model.py b/model.py index 8015860..e76e44f 100644 --- a/model.py +++ b/model.py @@ -221,7 +221,7 @@ class Model(ap.Model): 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]): + [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( diff --git a/orm.py b/orm.py index 4bc80a8..5e12c73 100644 --- a/orm.py +++ b/orm.py @@ -23,9 +23,11 @@ with open('conf_db_prefix.yaml') as file: str_login = 'mysql://{}:{}@{}:{}/{}'.format(dct_conf_db['user_name'], dct_conf_db['password'], dct_conf_db['address'], dct_conf_db['port'], dct_conf_db['db_name']) -print('DB is {}:{}/{}'.format(dct_conf_db['address'], dct_conf_db['port'], dct_conf_db['db_name'])) +print('DB is {}:{}/{}'.format(dct_conf_db['address'], + dct_conf_db['port'], dct_conf_db['db_name'])) -engine = create_engine(str_login, poolclass=NullPool) # must be null pool to avoid connection lost error +# must be null pool to avoid connection lost error +engine = create_engine(str_login, poolclass=NullPool) ins = inspect(engine) Base = declarative_base() @@ -48,7 +50,8 @@ class Experiment(Base): dct_lst_init_remove_firm_prod = Column(PickleType, nullable=False) g_bom = Column(Text(4294000000), nullable=False) - sample = relationship('Sample', back_populates='experiment', lazy='dynamic') + sample = relationship( + 'Sample', back_populates='experiment', lazy='dynamic') def __repr__(self): return f'' @@ -57,18 +60,21 @@ class Experiment(Base): class Sample(Base): __tablename__ = f"{db_name_prefix}_sample" id = Column(Integer, primary_key=True, autoincrement=True) - e_id = Column(Integer, ForeignKey('{}.id'.format(f"{db_name_prefix}_experiment")), nullable=False) + e_id = Column(Integer, ForeignKey('{}.id'.format( + f"{db_name_prefix}_experiment")), nullable=False) idx_sample = Column(Integer, nullable=False) seed = Column(BigInteger, nullable=False) - is_done_flag = Column(Integer, nullable=False) # -1, waiting; 0, running; 1, done + # -1, waiting; 0, running; 1, done + is_done_flag = Column(Integer, nullable=False) computer_name = Column(String(64), nullable=True) ts_done = Column(DateTime(timezone=True), onupdate=func.now()) stop_t = Column(Integer, nullable=True) g_firm = Column(Text(4294000000), nullable=True) - - experiment = relationship('Experiment', back_populates='sample', uselist=False) + + experiment = relationship( + 'Experiment', back_populates='sample', uselist=False) result = relationship('Result', back_populates='sample', lazy='dynamic') def __repr__(self): @@ -78,7 +84,8 @@ class Sample(Base): class Result(Base): __tablename__ = f"{db_name_prefix}_result" id = Column(Integer, primary_key=True, autoincrement=True) - s_id = Column(Integer, ForeignKey('{}.id'.format(f"{db_name_prefix}_sample")), nullable=False) + s_id = Column(Integer, ForeignKey('{}.id'.format( + f"{db_name_prefix}_sample")), nullable=False) id_firm = Column(String(10), nullable=False) id_product = Column(String(10), nullable=False) diff --git a/product.py b/product.py index b0cb415..09238f9 100644 --- a/product.py +++ b/product.py @@ -14,10 +14,10 @@ class ProductAgent(ap.Agent): return ap.AgentList( self.model, [ap.AgentIter(self.model, node).to_list()[0] for node in nodes]) - + def a_predecessors(self): - nodes = self.product_network.graph.predecessors( - self.product_network.positions[self]) - return ap.AgentList( - self.model, - [ap.AgentIter(self.model, node).to_list()[0] for node in nodes]) \ No newline at end of file + nodes = self.product_network.graph.predecessors( + self.product_network.positions[self]) + return ap.AgentList( + self.model, + [ap.AgentIter(self.model, node).to_list()[0] for node in nodes])