drop firm attr a_lst_product/_disrupted

This commit is contained in:
HaoYizhi 2023-06-18 22:12:47 +08:00
parent 3e87cffa63
commit 11abfa5ce8
4 changed files with 19 additions and 18 deletions

Binary file not shown.

Binary file not shown.

18
firm.py
View File

@ -13,8 +13,6 @@ class FirmAgent(ap.Agent):
self.type_region = type_region self.type_region = type_region
self.ori_size = revenue_log self.ori_size = revenue_log
self.size = revenue_log self.size = revenue_log
self.a_lst_product = a_lst_product
self.a_lst_product_disrupted = ap.AgentList(self.model, [])
self.dct_prod_up_prod_stat = {} self.dct_prod_up_prod_stat = {}
self.dct_prod_capacity = {} self.dct_prod_capacity = {}
@ -41,7 +39,7 @@ class FirmAgent(ap.Agent):
} }
# init extra capacity (self para) # init extra capacity (self para)
for product in self.a_lst_product: for product in a_lst_product:
# init extra capacity based on discrete uniform distribution # init extra capacity based on discrete uniform distribution
assert self.str_cap_limit_prob_type in ['uniform', 'normal'], \ assert self.str_cap_limit_prob_type in ['uniform', 'normal'], \
"cap_limit_prob_type other than uniform, normal" "cap_limit_prob_type other than uniform, normal"
@ -107,8 +105,7 @@ class FirmAgent(ap.Agent):
# select a list of candidate firm that has the product # select a list of candidate firm that has the product
self.dct_cand_alt_supp_up_prod_disrupted[product] = \ self.dct_cand_alt_supp_up_prod_disrupted[product] = \
self.model.a_lst_total_firms.select([ self.model.a_lst_total_firms.select([
product in firm.a_lst_product firm.is_prod_in_current_normal(product)
and product not in firm.a_lst_product_disrupted
for firm in self.model.a_lst_total_firms for firm in self.model.a_lst_total_firms
]) ])
if self.dct_cand_alt_supp_up_prod_disrupted[product]: if self.dct_cand_alt_supp_up_prod_disrupted[product]:
@ -163,7 +160,7 @@ class FirmAgent(ap.Agent):
f"{self.name} selct alt supply for {product.code} " f"{self.name} selct alt supply for {product.code} "
f"from {select_alt_supply.name}" f"from {select_alt_supply.name}"
) )
assert product in select_alt_supply.a_lst_product, \ assert select_alt_supply.is_prod_in_current_normal(product), \
f"{select_alt_supply} \ f"{select_alt_supply} \
does not produce requested product {product}" does not produce requested product {product}"
@ -286,3 +283,12 @@ class FirmAgent(ap.Agent):
def get_firm_network_node(self): def get_firm_network_node(self):
return self.firm_network.positions[self] return self.firm_network.positions[self]
def is_prod_in_current_normal(self, prod):
if prod in self.dct_prod_up_prod_stat.keys():
if self.dct_prod_up_prod_stat[prod]['status'][-1][0] == 'N':
return True
else:
return False
else:
return False

View File

@ -224,9 +224,8 @@ class Model(ap.Model):
print('\n', '=' * 20, 'step', self.t, '=' * 20) print('\n', '=' * 20, 'step', self.t, '=' * 20)
for firm, a_lst_product in self.dct_lst_disrupt_firm_prod.items(): for firm, a_lst_product in self.dct_lst_disrupt_firm_prod.items():
for product in a_lst_product: for product in a_lst_product:
assert product in firm.a_lst_product, \ assert product in firm.dct_prod_up_prod_stat.keys(), \
f"product {product.code} not in firm {firm.code}" f"product {product.code} not in firm {firm.code}"
firm.a_lst_product_disrupted.append(product)
firm.dct_prod_up_prod_stat[ firm.dct_prod_up_prod_stat[
product]['status'].append(('D', self.t)) product]['status'].append(('D', self.t))
@ -289,8 +288,7 @@ class Model(ap.Model):
[firm.code == di_supp_node['Firm_Code'] [firm.code == di_supp_node['Firm_Code']
for firm in self.a_lst_total_firms])[0] for firm in self.a_lst_total_firms])[0]
lst_cand = self.model.a_lst_total_firms.select([ lst_cand = self.model.a_lst_total_firms.select([
di_supp_prod in firm.a_lst_product and firm.is_prod_in_current_normal(di_supp_prod)
di_supp_prod not in firm.a_lst_product_disrupted
for firm in self.model.a_lst_total_firms for firm in self.model.a_lst_total_firms
]) ])
n2n_betweenness = \ n2n_betweenness = \
@ -311,8 +309,7 @@ class Model(ap.Model):
for product in self.a_lst_total_products])[0] for product in self.a_lst_total_products])[0]
# find a dfferent firm can produce the same product # find a dfferent firm can produce the same product
lst_cand = self.model.a_lst_total_firms.select([ lst_cand = self.model.a_lst_total_firms.select([
di_supp_prod in firm.a_lst_product and firm.is_prod_in_current_normal(di_supp_prod)
di_supp_prod not in firm.a_lst_product_disrupted
and firm.code != di_supp_node['Firm_Code'] and firm.code != di_supp_node['Firm_Code']
for firm in self.model.a_lst_total_firms for firm in self.model.a_lst_total_firms
]) ])
@ -345,7 +342,9 @@ class Model(ap.Model):
status, ts = firm.dct_prod_up_prod_stat[prod]['status'][-1] status, ts = firm.dct_prod_up_prod_stat[prod]['status'][-1]
if status == 'D': if status == 'D':
firm.size -= \ firm.size -= \
firm.ori_size / len(firm.a_lst_product) / self.remove_t firm.ori_size \
/ len(firm.dct_prod_up_prod_stat.keys()) \
/ self.remove_t
print(self.t, firm.name, prod.code, firm.size) print(self.t, firm.name, prod.code, firm.size)
if self.t - ts + 1 == self.remove_t: if self.t - ts + 1 == self.remove_t:
# turn disrupted firm into removed firm # turn disrupted firm into removed firm
@ -432,10 +431,7 @@ class Model(ap.Model):
lst_size = self.a_lst_total_firms.size lst_size = self.a_lst_total_firms.size
lst_size = [firm.size for firm lst_size = [firm.size for firm
in self.a_lst_total_firms in self.a_lst_total_firms
if product in firm.a_lst_product if firm.is_prod_in_current_normal(product)]
and product
not in firm.a_lst_product_disrupted
]
std_size = (firm.size - min(lst_size) + std_size = (firm.size - min(lst_size) +
1) / (max(lst_size) - min(lst_size) + 1) 1) / (max(lst_size) - min(lst_size) + 1)
prob_disrupt = 1 - std_size * (1 - lost_percent) prob_disrupt = 1 - std_size * (1 - lost_percent)
@ -449,7 +445,6 @@ class Model(ap.Model):
if self.nprandom.choice([True, False], if self.nprandom.choice([True, False],
p=[prob_disrupt, p=[prob_disrupt,
1 - prob_disrupt]): 1 - prob_disrupt]):
firm.a_lst_product_disrupted.append(product)
firm.dct_prod_up_prod_stat[ firm.dct_prod_up_prod_stat[
product]['status'].append(('D', self.t)) product]['status'].append(('D', self.t))
print(firm.name, 'disrupted product:', print(firm.name, 'disrupted product:',