This commit is contained in:
HaoYizhi 2023-07-02 23:34:44 +08:00
parent 6b77bec1c3
commit 9dce034cf2
3 changed files with 41 additions and 41 deletions

View File

@ -1 +1 @@
db_name_prefix: without db_name_prefix: without_exp

52
firm.py
View File

@ -99,12 +99,12 @@ class FirmAgent(ap.Agent):
if status != 'D': if status != 'D':
customer.dct_prod_up_prod_stat[ customer.dct_prod_up_prod_stat[
prod]['status'].append(('D', self.model.t)) prod]['status'].append(('D', self.model.t))
print(self.name, disrupted_prod.code, 'disrupt', # print(self.name, disrupted_prod.code, 'disrupt',
customer.name, prod.code) # customer.name, prod.code)
def seek_alt_supply(self, product): def seek_alt_supply(self, product):
# para product is the product that self is seeking # para product is the product that self is seeking
print(f"{self.name} seek alt supply for {product.code}") # print(f"{self.name} seek alt supply for {product.code}")
if self.dct_n_trial_up_prod_disrupted[ if self.dct_n_trial_up_prod_disrupted[
product] <= self.model.int_n_max_trial: product] <= self.model.int_n_max_trial:
if self.dct_n_trial_up_prod_disrupted[product] == 0: if self.dct_n_trial_up_prod_disrupted[product] == 0:
@ -163,10 +163,10 @@ class FirmAgent(ap.Agent):
else: else:
select_alt_supply = \ select_alt_supply = \
self.model.nprandom.choice(lst_firm_connect) self.model.nprandom.choice(lst_firm_connect)
print( # print(
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 select_alt_supply.is_prod_in_current_normal(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}"
@ -179,17 +179,17 @@ class FirmAgent(ap.Agent):
select_alt_supply.dct_request_prod_from_firm[product] = [ select_alt_supply.dct_request_prod_from_firm[product] = [
self self
] ]
print( # print(
select_alt_supply.name, 'dct_request_prod_from_firm', { # select_alt_supply.name, 'dct_request_prod_from_firm', {
key.code: [v.name for v in value] # key.code: [v.name for v in value]
for key, value in # for key, value in
select_alt_supply.dct_request_prod_from_firm.items() # select_alt_supply.dct_request_prod_from_firm.items()
}) # })
self.dct_n_trial_up_prod_disrupted[product] += 1 self.dct_n_trial_up_prod_disrupted[product] += 1
def handle_request(self): def handle_request(self):
print(self.name, 'handle_request') # print(self.name, 'handle_request')
for product, lst_firm in self.dct_request_prod_from_firm.items(): for product, lst_firm in self.dct_request_prod_from_firm.items():
if self.dct_prod_capacity[product] > 0: if self.dct_prod_capacity[product] > 0:
if len(lst_firm) == 0: if len(lst_firm) == 0:
@ -254,10 +254,10 @@ class FirmAgent(ap.Agent):
down_firm.dct_cand_alt_supp_up_prod_disrupted[ down_firm.dct_cand_alt_supp_up_prod_disrupted[
product].remove(self) product].remove(self)
print( # print(
f"{self.name} denied {product.code} request " # f"{self.name} denied {product.code} request "
f"from {down_firm.name} for lack of capacity" # f"from {down_firm.name} for lack of capacity"
) # )
def accept_request(self, down_firm, product): def accept_request(self, down_firm, product):
# para product is the product that self is selling # para product is the product that self is selling
@ -283,17 +283,17 @@ class FirmAgent(ap.Agent):
del down_firm.dct_n_trial_up_prod_disrupted[product] del down_firm.dct_n_trial_up_prod_disrupted[product]
del down_firm.dct_cand_alt_supp_up_prod_disrupted[product] del down_firm.dct_cand_alt_supp_up_prod_disrupted[product]
print( # print(
f"{self.name} accept {product.code} request " # f"{self.name} accept {product.code} request "
f"from {down_firm.name}" # f"from {down_firm.name}"
) # )
else: else:
down_firm.dct_cand_alt_supp_up_prod_disrupted[product].remove(self) down_firm.dct_cand_alt_supp_up_prod_disrupted[product].remove(self)
print( # print(
f"{self.name} denied {product.code} request " # f"{self.name} denied {product.code} request "
f"from {down_firm.name}" # f"from {down_firm.name}"
) # )
def clean_before_trial(self): def clean_before_trial(self):
self.dct_request_prod_from_firm = {} self.dct_request_prod_from_firm = {}

View File

@ -222,14 +222,14 @@ class Model(ap.Model):
self.dct_lst_init_disrupt_firm_prod = t_dct self.dct_lst_init_disrupt_firm_prod = t_dct
# set the initial firm product that are disrupted # set the initial firm product that are disrupted
print('\n', '=' * 20, 'step', self.t, '=' * 20) # print('\n', '=' * 20, 'step', self.t, '=' * 20)
for firm, a_lst_product in self.dct_lst_init_disrupt_firm_prod.items(): for firm, a_lst_product in self.dct_lst_init_disrupt_firm_prod.items():
for product in a_lst_product: for product in a_lst_product:
assert product in firm.dct_prod_up_prod_stat.keys(), \ 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.dct_prod_up_prod_stat[ firm.dct_prod_up_prod_stat[
product]['status'].append(('D', self.t)) product]['status'].append(('D', self.t))
print(f"initial disruption {firm.name} {product.code}") # print(f"initial disruption {firm.name} {product.code}")
# proactive strategy # proactive strategy
# get all the firm prod affected # get all the firm prod affected
@ -331,10 +331,10 @@ class Model(ap.Model):
'Product': di_supp_prod.code 'Product': di_supp_prod.code
}) })
]) ])
print(f"proactive add {select_cand.name} to " # print(f"proactive add {select_cand.name} to "
f"{pro_firm.name} " # f"{pro_firm.name} "
f"for {di_supp_node['Firm_Code']} " # f"for {di_supp_node['Firm_Code']} "
f"{di_supp_node['Product_Code']}") # f"{di_supp_node['Product_Code']}")
# change capacity # change capacity
select_cand.dct_prod_capacity[di_supp_prod] -= 1 select_cand.dct_prod_capacity[di_supp_prod] -= 1
break break
@ -356,8 +356,8 @@ class Model(ap.Model):
/ len(firm.dct_prod_up_prod_stat.keys()) \ / len(firm.dct_prod_up_prod_stat.keys()) \
/ self.remove_t / self.remove_t
firm.size_stat.append((size, self.t)) firm.size_stat.append((size, self.t))
print(f'in ts {self.t}, reduce {firm.name} size ' # print(f'in ts {self.t}, reduce {firm.name} size '
f'to {firm.size_stat[-1][0]} due to {prod.code}') # f'to {firm.size_stat[-1][0]} due to {prod.code}')
lst_is_disrupt = \ lst_is_disrupt = \
[stat == 'D' for stat, _ in [stat == 'D' for stat, _ in
firm.dct_prod_up_prod_stat[prod]['status'] firm.dct_prod_up_prod_stat[prod]['status']
@ -377,7 +377,7 @@ class Model(ap.Model):
firm in self.dct_lst_init_disrupt_firm_prod.keys() \ firm in self.dct_lst_init_disrupt_firm_prod.keys() \
and prod in self.dct_lst_init_disrupt_firm_prod[firm] and prod in self.dct_lst_init_disrupt_firm_prod[firm]
if status == 'D' and not is_init: if status == 'D' and not is_init:
print("not stop because", firm.name, prod.code) # print("not stop because", firm.name, prod.code)
break break
else: else:
continue continue
@ -390,7 +390,7 @@ class Model(ap.Model):
self.stop() self.stop()
def step(self): def step(self):
print('\n', '=' * 20, 'step', self.t, '=' * 20) # print('\n', '=' * 20, 'step', self.t, '=' * 20)
# remove edge to customer and disrupt customer up product # remove edge to customer and disrupt customer up product
for firm in self.a_lst_total_firms: for firm in self.a_lst_total_firms:
@ -402,7 +402,7 @@ class Model(ap.Model):
firm.remove_edge_to_cus_disrupt_cus_up_prod(prod) firm.remove_edge_to_cus_disrupt_cus_up_prod(prod)
for n_trial in range(self.int_n_max_trial): 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 # seek_alt_supply
# shuffle self.a_lst_total_firms # shuffle self.a_lst_total_firms
self.a_lst_total_firms = self.a_lst_total_firms.shuffle() self.a_lst_total_firms = self.a_lst_total_firms.shuffle()
@ -439,7 +439,7 @@ class Model(ap.Model):
# self.a_lst_total_firms.dct_request_prod_from_firm = {} why? # self.a_lst_total_firms.dct_request_prod_from_firm = {} why?
def end(self): def end(self):
print('/' * 20, 'output', '/' * 20) # print('/' * 20, 'output', '/' * 20)
qry_result = db_session.query(Result).filter_by(s_id=self.sample.id) qry_result = db_session.query(Result).filter_by(s_id=self.sample.id)
if qry_result.count() == 0: if qry_result.count() == 0:
@ -450,8 +450,8 @@ class Model(ap.Model):
lst_is_normal = [stat == 'N' for stat, _ lst_is_normal = [stat == 'N' for stat, _
in dct_status_supply['status']] in dct_status_supply['status']]
if not all(lst_is_normal): if not all(lst_is_normal):
print(f"{firm.name} {prod.code}:") # print(f"{firm.name} {prod.code}:")
print(dct_status_supply['status']) # print(dct_status_supply['status'])
for status, ts in dct_status_supply['status']: for status, ts in dct_status_supply['status']:
db_r = Result(s_id=self.sample.id, db_r = Result(s_id=self.sample.id,
id_firm=firm.code, id_firm=firm.code,