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 = [{'2': ['1.1.3']}]
# 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 = [{'41': ['1.4.5']}]
# list_dct = [{'168': ['1.1.2']}]
# fill g_bom
BomNodes = pd.read_csv('BomNodes.csv', index_col=0)

37
firm.py
View File

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

View File

@ -553,6 +553,29 @@
"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",
"execution_count": null,