rename 3 status into (Normal / Disrupted / Removed)

This commit is contained in:
HaoYizhi 2023-07-02 15:03:51 +08:00
parent baf60ffd76
commit c348f3eb9c
6 changed files with 18 additions and 18 deletions

Binary file not shown.

Binary file not shown.

View File

@ -79,9 +79,9 @@ class ControllerDB:
for product_code in row.index[row == 1].to_list(): for product_code in row.index[row == 1].to_list():
dct = {code: [product_code]} dct = {code: [product_code]}
list_dct.append(dct) list_dct.append(dct)
list_dct = [{'140': ['1.4.5.1']}] # list_dct = [{'140': ['1.4.5.1']}]
# list_dct = [{'133': ['1.4.4.1']}] # list_dct = [{'133': ['1.4.4.1']}]
# list_dct = [{'2': ['1.1.3']}] list_dct = [{'2': ['1.1.3']}]
# list_dct = [{'135': ['1.3.2.1']}] # list_dct = [{'135': ['1.3.2.1']}]
# list_dct = [{'79': ['2.1.3.4']}] # list_dct = [{'79': ['2.1.3.4']}]
# list_dct = [{'99': ['1.3.3']}] # list_dct = [{'99': ['1.3.3']}]

14
firm.py
View File

@ -35,7 +35,7 @@ class FirmAgent(ap.Agent):
# init dct_prod_up_prod_stat (self para) # init dct_prod_up_prod_stat (self para)
for prod in a_lst_product: for prod in a_lst_product:
self.dct_prod_up_prod_stat[prod] = { self.dct_prod_up_prod_stat[prod] = {
# (Normal / Affected / Removed, time step) # (Normal / Disrupted / Removed, time step)
'status': [('N', -1)], # ts -1 denotes initialization 'status': [('N', -1)], # ts -1 denotes initialization
# have or have no supply # have or have no supply
'supply': dict.fromkeys(prod.a_predecessors(), True) 'supply': dict.fromkeys(prod.a_predecessors(), True)
@ -62,8 +62,8 @@ class FirmAgent(ap.Agent):
# print(firm_agent.name, extra_cap) # print(firm_agent.name, extra_cap)
self.dct_prod_capacity[product] = extra_cap self.dct_prod_capacity[product] = extra_cap
def remove_edge_to_cus_affect_cus_up_prod(self, disrupted_prod): def remove_edge_to_cus_disrupt_cus_up_prod(self, disrupted_prod):
# para remove_product is the product that self got disrupted # para disrupted_prod is the product that self got disrupted
lst_out_edge = list( lst_out_edge = list(
self.firm_network.graph.out_edges( self.firm_network.graph.out_edges(
self.firm_network.positions[self], keys=True, data='Product')) self.firm_network.positions[self], keys=True, data='Product'))
@ -95,8 +95,8 @@ class FirmAgent(ap.Agent):
customer.dct_prod_up_prod_stat[ customer.dct_prod_up_prod_stat[
prod]['supply'][disrupted_prod] = False prod]['supply'][disrupted_prod] = False
customer.dct_prod_up_prod_stat[ customer.dct_prod_up_prod_stat[
prod]['status'].append(('A', self.model.t)) prod]['status'].append(('D', self.model.t))
print(self.name, disrupted_prod.code, 'affect', 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):
@ -134,9 +134,9 @@ class FirmAgent(ap.Agent):
# select based on size or not # select based on size or not
if self.is_prf_size: if self.is_prf_size:
lst_size = \ lst_size = \
[size for size in [firm.size_stat[-1][0] for firm in
self.dct_cand_alt_supp_up_prod_disrupted[ self.dct_cand_alt_supp_up_prod_disrupted[
product].size_stat[-1][0]] product]]
lst_prob = [size / sum(lst_size) lst_prob = [size / sum(lst_size)
for size in lst_size] for size in lst_size]
select_alt_supply = self.model.nprandom.choice( select_alt_supply = self.model.nprandom.choice(

View File

@ -226,8 +226,8 @@ class Model(ap.Model):
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(('A', self.t)) product]['status'].append(('D', self.t))
print(f"initial removal {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
@ -341,7 +341,7 @@ class Model(ap.Model):
for firm in self.a_lst_total_firms: for firm in self.a_lst_total_firms:
for prod in firm.dct_prod_up_prod_stat.keys(): for prod in firm.dct_prod_up_prod_stat.keys():
status, ts = firm.dct_prod_up_prod_stat[prod]['status'][-1] status, ts = firm.dct_prod_up_prod_stat[prod]['status'][-1]
if status == 'A': if status == 'D':
size = firm.size_stat[-1][0] - \ size = firm.size_stat[-1][0] - \
firm.size_stat[0][0] \ firm.size_stat[0][0] \
/ len(firm.dct_prod_up_prod_stat.keys()) \ / len(firm.dct_prod_up_prod_stat.keys()) \
@ -354,12 +354,12 @@ class Model(ap.Model):
firm.dct_prod_up_prod_stat[ firm.dct_prod_up_prod_stat[
prod]['status'].append(('R', self.t)) prod]['status'].append(('R', self.t))
# stop simulation if any firm still in affected except inital removal # stop simulation if any firm still in disrupted except inital removal
if self.t > 0: if self.t > 0:
for firm in self.a_lst_total_firms: for firm in self.a_lst_total_firms:
for prod in firm.dct_prod_up_prod_stat.keys(): for prod in firm.dct_prod_up_prod_stat.keys():
status, ts = firm.dct_prod_up_prod_stat[prod]['status'][-1] status, ts = firm.dct_prod_up_prod_stat[prod]['status'][-1]
if status == 'A' and ts != 0: if status == 'D' and ts != 0:
print("not stop because", firm.name, prod.code) print("not stop because", firm.name, prod.code)
break break
else: else:
@ -372,12 +372,12 @@ class Model(ap.Model):
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 affect 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:
for prod in firm.dct_prod_up_prod_stat.keys(): for prod in firm.dct_prod_up_prod_stat.keys():
status, ts = firm.dct_prod_up_prod_stat[prod]['status'][-1] status, ts = firm.dct_prod_up_prod_stat[prod]['status'][-1]
if status == 'A' and ts == self.t-1: if status == 'D' and ts == self.t-1:
firm.remove_edge_to_cus_affect_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)
@ -389,7 +389,7 @@ class Model(ap.Model):
lst_seek_prod = [] lst_seek_prod = []
for prod in firm.dct_prod_up_prod_stat.keys(): for prod in firm.dct_prod_up_prod_stat.keys():
status = firm.dct_prod_up_prod_stat[prod]['status'][-1][0] status = firm.dct_prod_up_prod_stat[prod]['status'][-1][0]
if status == 'A': if status == 'D':
for supply in firm.dct_prod_up_prod_stat[ for supply in firm.dct_prod_up_prod_stat[
prod]['supply'].keys(): prod]['supply'].keys():
if not firm.dct_prod_up_prod_stat[ if not firm.dct_prod_up_prod_stat[