This commit is contained in:
HaoYizhi 2023-07-28 18:07:30 +08:00
parent 7255c3b046
commit bf82aa62ff
7 changed files with 45 additions and 24 deletions

Binary file not shown.

Binary file not shown.

View File

@ -72,9 +72,10 @@ class ControllerDB:
# 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']}]
# list_dct = [{'41': ['1.4.5']}] # list_dct = [{'41': ['1.4.5']}]
# list_dct = [{'168': ['1.1.2']}]
# fill g_bom # fill g_bom
BomNodes = pd.read_csv('BomNodes.csv', index_col=0) BomNodes = pd.read_csv('BomNodes.csv', index_col=0)

29
firm.py
View File

@ -35,13 +35,12 @@ class FirmAgent(ap.Agent):
self.dct_prod_up_prod_stat[prod] = { self.dct_prod_up_prod_stat[prod] = {
# (Normal / Disrupted / Removed, time step) # (Normal / Disrupted / Removed, time step)
'p_stat': [('N', 0)], 'p_stat': [('N', 0)],
# have or have no supply
'supply': dict.fromkeys(prod.a_predecessors(), True),
# supply for each component and respective disrupted supplier # supply for each component and respective disrupted supplier
# lst_disrupt_firm is refreshed to [] at each update # set_disrupt_firm is refreshed to empty at each update
's_stat': dict.fromkeys(prod.a_predecessors(), 's_stat': {up_prod: {'stat': True,
{'stat': True, 'set_disrupt_firm': set()}
'lst_disrupt_firm': []}) for up_prod in prod.a_predecessors()}
# Note: do not use fromkeys as it's a shallow copy
} }
# init extra capacity (self para) # init extra capacity (self para)
@ -76,12 +75,9 @@ class FirmAgent(ap.Agent):
if disrupted_prod in \ if disrupted_prod in \
customer.dct_prod_up_prod_stat[ customer.dct_prod_up_prod_stat[
prod]['s_stat'].keys(): prod]['s_stat'].keys():
if self not in customer.dct_prod_up_prod_stat[
prod]['s_stat'][disrupted_prod][
'lst_disrupt_firm']:
customer.dct_prod_up_prod_stat[ customer.dct_prod_up_prod_stat[
prod]['s_stat'][disrupted_prod][ prod]['s_stat'][disrupted_prod][
'lst_disrupt_firm'].append(self) 'set_disrupt_firm'].add(self)
print(f"{self.name} disrupt {customer.name}'s " print(f"{self.name} disrupt {customer.name}'s "
f"{prod.code} due to {disrupted_prod.code}") f"{prod.code} due to {disrupted_prod.code}")
# remove edge to customer # remove edge to customer
@ -93,7 +89,7 @@ class FirmAgent(ap.Agent):
# self's component exists disrupted supplier # self's component exists disrupted supplier
num_lost = \ num_lost = \
len(self.dct_prod_up_prod_stat[prod]['s_stat'] len(self.dct_prod_up_prod_stat[prod]['s_stat']
[disrupted_up_prod]['lst_disrupt_firm']) [disrupted_up_prod]['set_disrupt_firm'])
num_remain = \ num_remain = \
len([u for u, _, _, d in len([u for u, _, _, d in
self.firm_network.graph.in_edges(self.get_firm_network_node(), self.firm_network.graph.in_edges(self.get_firm_network_node(),
@ -117,7 +113,8 @@ class FirmAgent(ap.Agent):
if status != 'D': if status != 'D':
self.dct_prod_up_prod_stat[ self.dct_prod_up_prod_stat[
prod]['p_stat'].append(('D', self.model.t)) prod]['p_stat'].append(('D', self.model.t))
print(f"{self.name}'s {prod.code} turn to D status") print(f"{self.name}'s {prod.code} turn to D status due to "
f"disrupted supplier of {disrupted_up_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
@ -283,9 +280,9 @@ class FirmAgent(ap.Agent):
for prod in down_firm.dct_prod_up_prod_stat.keys(): for prod in down_firm.dct_prod_up_prod_stat.keys():
if product in down_firm.dct_prod_up_prod_stat[ if product in down_firm.dct_prod_up_prod_stat[
prod]['supply'].keys(): prod]['s_stat'].keys():
down_firm.dct_prod_up_prod_stat[ down_firm.dct_prod_up_prod_stat[
prod]['supply'][product] = True prod]['s_stat'][product]['stat'] = True
down_firm.dct_prod_up_prod_stat[ down_firm.dct_prod_up_prod_stat[
prod]['p_stat'].append(('N', self.model.t)) prod]['p_stat'].append(('N', self.model.t))
del down_firm.dct_n_trial_up_prod_disrupted[product] del down_firm.dct_n_trial_up_prod_disrupted[product]
@ -317,10 +314,10 @@ class FirmAgent(ap.Agent):
if ts != self.model.t: if ts != self.model.t:
self.dct_prod_up_prod_stat[prod]['p_stat'].append( self.dct_prod_up_prod_stat[prod]['p_stat'].append(
(status, self.model.t)) (status, self.model.t))
# refresh lst_disrupt_firm # refresh set_disrupt_firm
for up_prod in self.dct_prod_up_prod_stat[prod]['s_stat'].keys(): for up_prod in self.dct_prod_up_prod_stat[prod]['s_stat'].keys():
self.dct_prod_up_prod_stat[prod][ self.dct_prod_up_prod_stat[prod][
's_stat'][up_prod]['lst_disrupt_firm'] = [] 's_stat'][up_prod]['set_disrupt_firm'] = set()
def get_firm_network_node(self): def get_firm_network_node(self):
return self.firm_network.positions[self] return self.firm_network.positions[self]

View File

@ -406,7 +406,7 @@ class Model(ap.Model):
for up_prod in firm.dct_prod_up_prod_stat[prod][ for up_prod in firm.dct_prod_up_prod_stat[prod][
's_stat'].keys(): 's_stat'].keys():
if firm.dct_prod_up_prod_stat[prod][ if firm.dct_prod_up_prod_stat[prod][
's_stat'][up_prod]['lst_disrupt_firm']: 's_stat'][up_prod]['set_disrupt_firm']:
firm.disrupt_cus_prod(prod, up_prod) firm.disrupt_cus_prod(prod, up_prod)
for n_trial in range(self.int_n_max_trial): for n_trial in range(self.int_n_max_trial):
@ -421,9 +421,9 @@ class Model(ap.Model):
status = firm.dct_prod_up_prod_stat[prod]['p_stat'][-1][0] status = firm.dct_prod_up_prod_stat[prod]['p_stat'][-1][0]
if status == 'D': 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]['s_stat'].keys():
if not firm.dct_prod_up_prod_stat[ if not firm.dct_prod_up_prod_stat[
prod]['supply'][supply]: prod]['s_stat'][supply]['stat']:
lst_seek_prod.append(supply) lst_seek_prod.append(supply)
# commmon supply only seek once # commmon supply only seek once
lst_seek_prod = list(set(lst_seek_prod)) lst_seek_prod = list(set(lst_seek_prod))

View File

@ -553,6 +553,29 @@
"G.has_edge(1, 2, 1)" "G.has_edge(1, 2, 1)"
] ]
}, },
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"s = set()\n",
"s.add(1)\n",
"s.add(2)\n",
"s.add(1)\n",
"len(s)\n",
"if s:\n",
" print(1)"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,