log status of every firm product in every time step + mysql
This commit is contained in:
parent
0965a5daa4
commit
84828272e2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
11
firm.py
11
firm.py
|
@ -29,14 +29,14 @@ class FirmAgent(ap.Agent):
|
||||||
self.flt_crit_supplier = float(self.p.crit_supplier)
|
self.flt_crit_supplier = float(self.p.crit_supplier)
|
||||||
|
|
||||||
# init size_stat (self para)
|
# init size_stat (self para)
|
||||||
# (size, time step), ts -1 denotes initialization
|
# (size, time step)
|
||||||
self.size_stat.append((revenue_log, -1))
|
self.size_stat.append((revenue_log, 0))
|
||||||
|
|
||||||
# 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 / Disrupted / Removed, time step)
|
# (Normal / Disrupted / Removed, time step)
|
||||||
'status': [('N', -1)], # ts -1 denotes initialization
|
'status': [('N', 0)],
|
||||||
# have or have no supply
|
# have or have no supply
|
||||||
'supply': dict.fromkeys(prod.a_predecessors(), True)
|
'supply': dict.fromkeys(prod.a_predecessors(), True)
|
||||||
}
|
}
|
||||||
|
@ -294,6 +294,11 @@ class FirmAgent(ap.Agent):
|
||||||
|
|
||||||
def clean_before_trial(self):
|
def clean_before_trial(self):
|
||||||
self.dct_request_prod_from_firm = {}
|
self.dct_request_prod_from_firm = {}
|
||||||
|
for prod in self.dct_prod_up_prod_stat.keys():
|
||||||
|
status, ts = self.dct_prod_up_prod_stat[prod]['status'][-1]
|
||||||
|
if ts != self.model.t:
|
||||||
|
self.dct_prod_up_prod_stat[prod]['status'].append(
|
||||||
|
(status, self.model.t))
|
||||||
|
|
||||||
def clean_before_time_step(self):
|
def clean_before_time_step(self):
|
||||||
self.dct_n_trial_up_prod_disrupted = \
|
self.dct_n_trial_up_prod_disrupted = \
|
||||||
|
|
91
model.py
91
model.py
|
@ -17,7 +17,8 @@ class Model(ap.Model):
|
||||||
self.product_network = None # agentpy network
|
self.product_network = None # agentpy network
|
||||||
self.firm_network = None # agentpy network
|
self.firm_network = None # agentpy network
|
||||||
self.firm_prod_network = None # networkx
|
self.firm_prod_network = None # networkx
|
||||||
self.dct_lst_disrupt_firm_prod = self.p.dct_lst_init_disrupt_firm_prod
|
self.dct_lst_init_disrupt_firm_prod = \
|
||||||
|
self.p.dct_lst_init_disrupt_firm_prod
|
||||||
|
|
||||||
# external variable
|
# external variable
|
||||||
self.int_n_max_trial = int(self.p.n_max_trial)
|
self.int_n_max_trial = int(self.p.n_max_trial)
|
||||||
|
@ -209,19 +210,20 @@ class Model(ap.Model):
|
||||||
self.firm_network.add_agents([firm_agent], [ag_node])
|
self.firm_network.add_agents([firm_agent], [ag_node])
|
||||||
self.a_lst_total_firms = ap.AgentList(self, self.firm_network.agents)
|
self.a_lst_total_firms = ap.AgentList(self, self.firm_network.agents)
|
||||||
|
|
||||||
# init dct_lst_disrupt_firm_prod (from string to agent)
|
# init dct_lst_init_disrupt_firm_prod (from string to agent)
|
||||||
t_dct = {}
|
t_dct = {}
|
||||||
for firm_code, lst_product in self.dct_lst_disrupt_firm_prod.items():
|
for firm_code, lst_product in \
|
||||||
|
self.dct_lst_init_disrupt_firm_prod.items():
|
||||||
firm = self.a_lst_total_firms.select(
|
firm = self.a_lst_total_firms.select(
|
||||||
self.a_lst_total_firms.code == firm_code)[0]
|
self.a_lst_total_firms.code == firm_code)[0]
|
||||||
t_dct[firm] = self.a_lst_total_products.select([
|
t_dct[firm] = self.a_lst_total_products.select([
|
||||||
code in lst_product for code in self.a_lst_total_products.code
|
code in lst_product for code in self.a_lst_total_products.code
|
||||||
])
|
])
|
||||||
self.dct_lst_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_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}"
|
||||||
|
@ -231,7 +233,7 @@ class Model(ap.Model):
|
||||||
|
|
||||||
# proactive strategy
|
# proactive strategy
|
||||||
# get all the firm prod affected
|
# get all the firm prod affected
|
||||||
for firm, a_lst_product in self.dct_lst_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:
|
||||||
init_node = \
|
init_node = \
|
||||||
[n for n, v in
|
[n for n, v in
|
||||||
|
@ -349,8 +351,13 @@ class Model(ap.Model):
|
||||||
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}')
|
||||||
if self.t - ts + 1 == self.remove_t:
|
lst_is_disrupt = \
|
||||||
|
[stat == 'D' for stat, _ in
|
||||||
|
firm.dct_prod_up_prod_stat[prod]['status']
|
||||||
|
[-1 * self.remove_t:]]
|
||||||
|
if all(lst_is_disrupt):
|
||||||
# turn disrupted firm into removed firm
|
# turn disrupted firm into removed firm
|
||||||
|
# when last self.remove_t times status is all disrupted
|
||||||
firm.dct_prod_up_prod_stat[
|
firm.dct_prod_up_prod_stat[
|
||||||
prod]['status'].append(('R', self.t))
|
prod]['status'].append(('R', self.t))
|
||||||
|
|
||||||
|
@ -358,8 +365,11 @@ class Model(ap.Model):
|
||||||
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, _ = firm.dct_prod_up_prod_stat[prod]['status'][-1]
|
||||||
if status == 'D' and ts != 0:
|
is_init = \
|
||||||
|
firm in self.dct_lst_init_disrupt_firm_prod.keys() \
|
||||||
|
and prod in self.dct_lst_init_disrupt_firm_prod[firm]
|
||||||
|
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:
|
||||||
|
@ -422,45 +432,30 @@ class Model(ap.Model):
|
||||||
def end(self):
|
def end(self):
|
||||||
print('/' * 20, 'output', '/' * 20)
|
print('/' * 20, 'output', '/' * 20)
|
||||||
|
|
||||||
for firm in self.a_lst_total_firms:
|
qry_result = db_session.query(Result).filter_by(s_id=self.sample.id)
|
||||||
is_size = False
|
if qry_result.count() == 0:
|
||||||
for prod, dct_status_supply in firm.dct_prod_up_prod_stat.items():
|
lst_result_info = []
|
||||||
if len(dct_status_supply['status']) > 1:
|
for firm in self.a_lst_total_firms:
|
||||||
is_size = True
|
for prod, dct_status_supply in \
|
||||||
print(f"{firm.name} {prod.code}:")
|
firm.dct_prod_up_prod_stat.items():
|
||||||
print(dct_status_supply['status'])
|
lst_is_normal = [stat == 'N' for stat, _
|
||||||
if is_size:
|
in dct_status_supply['status']]
|
||||||
print(firm.size_stat)
|
if not all(lst_is_normal):
|
||||||
|
print(f"{firm.name} {prod.code}:")
|
||||||
# qry_result = db_session.query(Result).filter_by(s_id=self.sample.id)
|
print(dct_status_supply['status'])
|
||||||
# if qry_result.count() == 0:
|
for status, ts in dct_status_supply['status']:
|
||||||
# lst_result_info = []
|
db_r = Result(s_id=self.sample.id,
|
||||||
# for t, dct in self.lst_dct_lst_disrupt_firm_prod:
|
id_firm=firm.code,
|
||||||
# for firm, a_lst_product in dct.items():
|
id_product=prod.code,
|
||||||
# for product in a_lst_product:
|
ts=ts,
|
||||||
# db_r = Result(s_id=self.sample.id,
|
status=status)
|
||||||
# id_firm=firm.code,
|
lst_result_info.append(db_r)
|
||||||
# id_product=product.code,
|
db_session.bulk_save_objects(lst_result_info)
|
||||||
# ts=t,
|
db_session.commit()
|
||||||
# is_disrupted=True)
|
self.sample.is_done_flag = 1
|
||||||
# lst_result_info.append(db_r)
|
self.sample.computer_name = platform.node()
|
||||||
# db_session.bulk_save_objects(lst_result_info)
|
self.sample.stop_t = self.int_stop_ts
|
||||||
# db_session.commit()
|
db_session.commit()
|
||||||
# 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:
|
|
||||||
# # 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_ts
|
|
||||||
# db_session.commit()
|
|
||||||
|
|
||||||
def draw_network(self):
|
def draw_network(self):
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
3
orm.py
3
orm.py
|
@ -105,8 +105,7 @@ class Result(Base):
|
||||||
id_firm = Column(String(10), nullable=False)
|
id_firm = Column(String(10), nullable=False)
|
||||||
id_product = Column(String(10), nullable=False)
|
id_product = Column(String(10), nullable=False)
|
||||||
ts = Column(Integer, nullable=False)
|
ts = Column(Integer, nullable=False)
|
||||||
is_disrupted = Column(Boolean, nullable=True)
|
status = Column(String(5), nullable=False)
|
||||||
is_removed = Column(Boolean, nullable=True)
|
|
||||||
|
|
||||||
sample = relationship('Sample', back_populates='result', uselist=False)
|
sample = relationship('Sample', back_populates='result', uselist=False)
|
||||||
|
|
||||||
|
|
40
test.ipynb
40
test.ipynb
|
@ -317,6 +317,46 @@
|
||||||
"print(27 / (4 * 3))\n",
|
"print(27 / (4 * 3))\n",
|
||||||
"print(27 / 4 / 3)"
|
"print(27 / 4 / 3)"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"for i in range(1,1):\n",
|
||||||
|
" print(i)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"[6, 7, 8, 9, 10]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"lst = list(range(1,11))\n",
|
||||||
|
"print(lst)\n",
|
||||||
|
"\n",
|
||||||
|
"lst[-5:]"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|
Loading…
Reference in New Issue