dct_prod_up_prod_stat + no need for dct_list_remove_firm_prod/dct_list_disrupt_firm_prod
This commit is contained in:
210
model.py
210
model.py
@@ -235,11 +235,13 @@ class Model(ap.Model):
|
||||
code in lst_product for code in self.a_lst_total_products.code
|
||||
])
|
||||
self.dct_lst_remove_firm_prod = t_dct
|
||||
self.dct_lst_disrupt_firm_prod = t_dct
|
||||
# self.dct_lst_disrupt_firm_prod = t_dct
|
||||
|
||||
# init output
|
||||
self.lst_dct_lst_remove_firm_prod = []
|
||||
self.lst_dct_lst_disrupt_firm_prod = []
|
||||
# # init output
|
||||
# self.lst_dct_lst_remove_firm_prod = []
|
||||
# self.lst_dct_lst_disrupt_firm_prod = []
|
||||
|
||||
# self.dct_ts_dct_a_lst_remove = {}
|
||||
|
||||
# set the initial firm product that are removed
|
||||
for firm, a_lst_product in self.dct_lst_remove_firm_prod.items():
|
||||
@@ -247,6 +249,8 @@ class Model(ap.Model):
|
||||
assert product in firm.a_lst_product, \
|
||||
f"product {product.code} not in firm {firm.code}"
|
||||
firm.a_lst_product_removed.append(product)
|
||||
firm.dct_prod_up_prod_stat[
|
||||
product]['status'].append(('R', self.t))
|
||||
|
||||
# proactive strategy
|
||||
# get all the firm prod affected
|
||||
@@ -356,20 +360,34 @@ class Model(ap.Model):
|
||||
|
||||
def update(self):
|
||||
self.a_lst_total_firms.clean_before_time_step()
|
||||
# output
|
||||
self.lst_dct_lst_remove_firm_prod.append(
|
||||
(self.t, self.dct_lst_remove_firm_prod))
|
||||
self.lst_dct_lst_disrupt_firm_prod.append(
|
||||
(self.t, self.dct_lst_disrupt_firm_prod))
|
||||
# # output
|
||||
# self.lst_dct_lst_remove_firm_prod.append(
|
||||
# (self.t, self.dct_lst_remove_firm_prod))
|
||||
# self.lst_dct_lst_disrupt_firm_prod.append(
|
||||
# (self.t, self.dct_lst_disrupt_firm_prod))
|
||||
|
||||
# stop simulation if reached terminal number of iteration
|
||||
if self.t == self.int_n_iter or len(
|
||||
self.dct_lst_remove_firm_prod) == 0:
|
||||
self.int_stop_times = self.t
|
||||
self.stop()
|
||||
# # stop simulation if reached terminal number of iteration
|
||||
# if self.t == self.int_n_iter or len(
|
||||
# self.dct_lst_remove_firm_prod) == 0:
|
||||
# self.int_stop_times = self.t
|
||||
# self.stop()
|
||||
|
||||
if self.t > 0:
|
||||
for firm in self.a_lst_total_firms:
|
||||
for prod in firm.dct_prod_up_prod_stat.keys():
|
||||
status, ts = firm.dct_prod_up_prod_stat[prod]['status'][-1]
|
||||
if status == 'R' and ts != 0:
|
||||
print("not stop because", firm.name, prod.code)
|
||||
break
|
||||
else:
|
||||
continue
|
||||
break
|
||||
else:
|
||||
self.int_stop_times = self.t
|
||||
self.stop()
|
||||
|
||||
def step(self):
|
||||
# print('\n', '=' * 20, 'step', self.t, '=' * 20)
|
||||
print('\n', '=' * 20, 'step', self.t, '=' * 20)
|
||||
# print(
|
||||
# 'dct_list_remove_firm_prod', {
|
||||
# key.name: value.code
|
||||
@@ -377,18 +395,33 @@ class Model(ap.Model):
|
||||
# })
|
||||
|
||||
# remove_edge_to_cus_and_cus_up_prod
|
||||
for firm, a_lst_product in self.dct_lst_remove_firm_prod.items():
|
||||
for product in a_lst_product:
|
||||
firm.remove_edge_to_cus_remove_cus_up_prod(product)
|
||||
for firm in self.a_lst_total_firms:
|
||||
for prod in firm.dct_prod_up_prod_stat.keys():
|
||||
status, ts = firm.dct_prod_up_prod_stat[prod]['status'][-1]
|
||||
if status == 'R' and ts == self.t-1:
|
||||
firm.remove_edge_to_cus_remove_cus_up_prod(prod)
|
||||
|
||||
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
|
||||
# shuffle self.a_lst_total_firms
|
||||
self.a_lst_total_firms = self.a_lst_total_firms.shuffle()
|
||||
for firm in self.a_lst_total_firms:
|
||||
if len(firm.a_lst_up_product_removed) > 0:
|
||||
firm.seek_alt_supply()
|
||||
# if len(firm.a_lst_up_product_removed) > 0:
|
||||
# firm.seek_alt_supply()
|
||||
lst_seek_prod = []
|
||||
for prod in firm.dct_prod_up_prod_stat.keys():
|
||||
status = firm.dct_prod_up_prod_stat[prod]['status'][-1][0]
|
||||
if status != 'N':
|
||||
for supply in firm.dct_prod_up_prod_stat[
|
||||
prod]['supply'].keys():
|
||||
if not firm.dct_prod_up_prod_stat[
|
||||
prod]['supply'][supply]:
|
||||
lst_seek_prod.append(supply)
|
||||
# commmon supply only seek once
|
||||
lst_seek_prod = list(set(lst_seek_prod))
|
||||
for supply in lst_seek_prod:
|
||||
firm.seek_alt_supply(supply)
|
||||
|
||||
# handle_request
|
||||
# shuffle self.a_lst_total_firms
|
||||
@@ -402,35 +435,43 @@ class Model(ap.Model):
|
||||
# do not use:
|
||||
# self.a_lst_total_firms.dct_request_prod_from_firm = {} why?
|
||||
|
||||
# based on a_lst_up_product_removed
|
||||
# based on dct_prod_up_prod_stat
|
||||
# update a_lst_product_disrupted / a_lst_product_removed
|
||||
# update dct_lst_disrupt_firm_prod / dct_lst_remove_firm_prod
|
||||
self.dct_lst_remove_firm_prod = {}
|
||||
self.dct_lst_disrupt_firm_prod = {}
|
||||
# self.dct_lst_remove_firm_prod = {}
|
||||
# self.dct_lst_disrupt_firm_prod = {}
|
||||
for firm in self.a_lst_total_firms:
|
||||
if len(firm.a_lst_up_product_removed) > 0:
|
||||
# print(firm.name, 'a_lst_up_product_removed', [
|
||||
# product.code for product in firm.a_lst_up_product_removed
|
||||
# ])
|
||||
for product in firm.a_lst_product:
|
||||
n_up_product_removed = 0
|
||||
for up_product_removed in firm.a_lst_up_product_removed:
|
||||
if product in up_product_removed.a_successors():
|
||||
n_up_product_removed += 1
|
||||
# if len(firm.a_lst_up_product_removed) > 0:
|
||||
# print(firm.name, 'a_lst_up_product_removed', [
|
||||
# product.code for product in firm.a_lst_up_product_removed
|
||||
# ])
|
||||
for product in firm.dct_prod_up_prod_stat.keys():
|
||||
status = firm.dct_prod_up_prod_stat[product]['status'][-1][0]
|
||||
if status == 'D':
|
||||
print(firm.name, 'disrupted product: ', product.code)
|
||||
n_up_product_removed = \
|
||||
sum([not stat for stat in
|
||||
firm.dct_prod_up_prod_stat[
|
||||
product]['supply'].values()])
|
||||
# for product in firm.a_lst_product:
|
||||
# n_up_product_removed = 0
|
||||
# for up_product_removed in firm.a_lst_up_product_removed:
|
||||
# if product in up_product_removed.a_successors():
|
||||
# n_up_product_removed += 1
|
||||
if n_up_product_removed == 0:
|
||||
continue
|
||||
else:
|
||||
# update a_lst_product_disrupted
|
||||
# update dct_lst_disrupt_firm_prod
|
||||
if product not in firm.a_lst_product_disrupted:
|
||||
firm.a_lst_product_disrupted.append(product)
|
||||
if firm in self.dct_lst_disrupt_firm_prod.keys():
|
||||
self.dct_lst_disrupt_firm_prod[firm].append(
|
||||
product)
|
||||
else:
|
||||
self.dct_lst_disrupt_firm_prod[
|
||||
firm] = ap.AgentList(
|
||||
self.model, [product])
|
||||
# # update a_lst_product_disrupted
|
||||
# # update dct_lst_disrupt_firm_prod
|
||||
# if product not in firm.a_lst_product_disrupted:
|
||||
# firm.a_lst_product_disrupted.append(product)
|
||||
# if firm in self.dct_lst_disrupt_firm_prod.keys():
|
||||
# self.dct_lst_disrupt_firm_prod[firm].append(
|
||||
# product)
|
||||
# else:
|
||||
# self.dct_lst_disrupt_firm_prod[
|
||||
# firm] = ap.AgentList(
|
||||
# self.model, [product])
|
||||
# update a_lst_product_removed
|
||||
# update dct_list_remove_firm_prod
|
||||
# mark disrupted firm as removed based conditionally
|
||||
@@ -457,13 +498,20 @@ class Model(ap.Model):
|
||||
p=[prob_remove,
|
||||
1 - prob_remove]):
|
||||
firm.a_lst_product_removed.append(product)
|
||||
if firm in self.dct_lst_remove_firm_prod.keys():
|
||||
self.dct_lst_remove_firm_prod[firm].append(
|
||||
product)
|
||||
else:
|
||||
self.dct_lst_remove_firm_prod[
|
||||
firm] = ap.AgentList(
|
||||
self.model, [product])
|
||||
# if firm in self.dct_lst_remove_firm_prod.keys():
|
||||
# self.dct_lst_remove_firm_prod[firm].append(
|
||||
# product)
|
||||
# else:
|
||||
# self.dct_lst_remove_firm_prod[
|
||||
# firm] = ap.AgentList(
|
||||
# self.model, [product])
|
||||
|
||||
firm.dct_prod_up_prod_stat[
|
||||
product]['status'].append(('R', self.t))
|
||||
print(firm.name, 'removed product: ', product.code)
|
||||
else:
|
||||
firm.dct_prod_up_prod_stat[
|
||||
product]['status'].append(('N', self.t))
|
||||
|
||||
# print(
|
||||
# 'dct_list_remove_firm_prod', {
|
||||
@@ -472,7 +520,7 @@ class Model(ap.Model):
|
||||
# })
|
||||
|
||||
def end(self):
|
||||
# print('/' * 20, 'output', '/' * 20)
|
||||
print('/' * 20, 'output', '/' * 20)
|
||||
# print('dct_list_remove_firm_prod')
|
||||
# for t, dct in self.lst_dct_lst_remove_firm_prod:
|
||||
# for firm, a_lst_product in dct.items():
|
||||
@@ -484,35 +532,35 @@ class Model(ap.Model):
|
||||
# for product in a_lst_product:
|
||||
# print(t, firm.name, product.code)
|
||||
|
||||
qry_result = db_session.query(Result).filter_by(s_id=self.sample.id)
|
||||
if qry_result.count() == 0:
|
||||
lst_result_info = []
|
||||
for t, dct in self.lst_dct_lst_disrupt_firm_prod:
|
||||
for firm, a_lst_product in dct.items():
|
||||
for product in a_lst_product:
|
||||
db_r = Result(s_id=self.sample.id,
|
||||
id_firm=firm.code,
|
||||
id_product=product.code,
|
||||
ts=t,
|
||||
is_disrupted=True)
|
||||
lst_result_info.append(db_r)
|
||||
db_session.bulk_save_objects(lst_result_info)
|
||||
db_session.commit()
|
||||
for t, dct in self.lst_dct_lst_remove_firm_prod:
|
||||
for firm, a_lst_product in dct.items():
|
||||
for product in a_lst_product:
|
||||
# only firm disrupted can be removed theoretically
|
||||
qry_f_p = db_session.query(Result).filter(
|
||||
Result.s_id == self.sample.id,
|
||||
Result.id_firm == firm.code,
|
||||
Result.id_product == product.code)
|
||||
if qry_f_p.count() == 1:
|
||||
qry_f_p.update({"is_removed": True})
|
||||
db_session.commit()
|
||||
self.sample.is_done_flag = 1
|
||||
self.sample.computer_name = platform.node()
|
||||
self.sample.stop_t = self.int_stop_times
|
||||
db_session.commit()
|
||||
# qry_result = db_session.query(Result).filter_by(s_id=self.sample.id)
|
||||
# if qry_result.count() == 0:
|
||||
# lst_result_info = []
|
||||
# for t, dct in self.lst_dct_lst_disrupt_firm_prod:
|
||||
# for firm, a_lst_product in dct.items():
|
||||
# for product in a_lst_product:
|
||||
# db_r = Result(s_id=self.sample.id,
|
||||
# id_firm=firm.code,
|
||||
# id_product=product.code,
|
||||
# ts=t,
|
||||
# is_disrupted=True)
|
||||
# lst_result_info.append(db_r)
|
||||
# db_session.bulk_save_objects(lst_result_info)
|
||||
# db_session.commit()
|
||||
# for t, dct in self.lst_dct_lst_remove_firm_prod:
|
||||
# for firm, a_lst_product in dct.items():
|
||||
# for product in a_lst_product:
|
||||
# # only firm disrupted can be removed theoretically
|
||||
# qry_f_p = db_session.query(Result).filter(
|
||||
# Result.s_id == self.sample.id,
|
||||
# Result.id_firm == firm.code,
|
||||
# Result.id_product == product.code)
|
||||
# if qry_f_p.count() == 1:
|
||||
# qry_f_p.update({"is_removed": True})
|
||||
# db_session.commit()
|
||||
# self.sample.is_done_flag = 1
|
||||
# self.sample.computer_name = platform.node()
|
||||
# self.sample.stop_t = self.int_stop_times
|
||||
# db_session.commit()
|
||||
|
||||
def draw_network(self):
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
Reference in New Issue
Block a user