init extra capacity in firm.py

This commit is contained in:
HaoYizhi 2023-06-18 18:15:37 +08:00
parent dd8a786f6c
commit b0b3cac73e
11 changed files with 41 additions and 31 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -129,7 +129,7 @@ class ControllerDB:
n_max_trial, prf_size, prf_conn,
cap_limit_prob_type, cap_limit_level,
diff_new_conn, crit_supplier, diff_remove,
proactive_ratio, netw_prf_n):
proactive_ratio, drop_t, netw_prf_n):
e = Experiment(
idx_scenario=idx_scenario,
idx_init_removal=idx_init_removal,
@ -146,6 +146,7 @@ class ControllerDB:
crit_supplier=crit_supplier,
diff_remove=diff_remove,
proactive_ratio=proactive_ratio,
drop_t=drop_t,
netw_prf_n=netw_prf_n
)
db_session.add(e)

25
firm.py
View File

@ -12,7 +12,7 @@ class FirmAgent(ap.Agent):
self.type_region = type_region
self.revenue_log = revenue_log
self.a_lst_product = a_lst_product
self.dct_prod_capacity = dict.fromkeys(self.a_lst_product)
self.dct_prod_capacity = {}
# self.a_lst_up_product_removed = ap.AgentList(self.model, [])
# self.a_lst_product_disrupted = ap.AgentList(self.model, [])
@ -35,9 +35,32 @@ class FirmAgent(ap.Agent):
# para
self.is_prf_size = self.model.is_prf_size
self.is_prf_conn = bool(self.p.prf_conn)
self.str_cap_limit_prob_type = str(self.p.cap_limit_prob_type)
self.flt_cap_limit_level = float(self.p.cap_limit_level)
self.flt_diff_new_conn = float(self.p.diff_new_conn)
self.flt_crit_supplier = float(self.p.crit_supplier)
# init extra capacity
for product in self.a_lst_product:
# init extra capacity based on discrete uniform distribution
assert self.str_cap_limit_prob_type in ['uniform', 'normal'], \
"cap_limit_prob_type other than uniform, normal"
if self.str_cap_limit_prob_type == 'uniform':
extra_cap_mean = \
self.revenue_log / self.flt_cap_limit_level
extra_cap = self.model.nprandom.integers(extra_cap_mean-2,
extra_cap_mean+2)
extra_cap = 0 if round(extra_cap) < 0 else round(extra_cap)
# print(firm_agent.name, extra_cap)
self.dct_prod_capacity[product] = extra_cap
elif self.str_cap_limit_prob_type == 'normal':
extra_cap_mean = \
self.revenue_log / self.flt_cap_limit_level
extra_cap = self.model.nprandom.normal(extra_cap_mean, 1)
extra_cap = 0 if round(extra_cap) < 0 else round(extra_cap)
# print(firm_agent.name, extra_cap)
self.dct_prod_capacity[product] = extra_cap
def remove_edge_to_cus_remove_cus_up_prod(self, remove_product):
lst_out_edge = list(
self.firm_network.graph.out_edges(

View File

@ -18,10 +18,9 @@ class Model(ap.Model):
self.int_n_max_trial = int(self.p.n_max_trial)
self.is_prf_size = bool(self.p.prf_size)
self.str_cap_limit_prob_type = str(self.p.cap_limit_prob_type)
self.flt_cap_limit_level = float(self.p.cap_limit_level)
self.flt_diff_remove = float(self.p.diff_remove)
self.proactive_ratio = float(self.p.proactive_ratio)
self.drop_t = int(self.p.drop_t)
self.int_netw_prf_n = int(self.p.netw_prf_n)
# init graph bom
@ -203,25 +202,6 @@ class Model(ap.Model):
code in attr['Product_Code']
for code in self.a_lst_total_products.code
]))
for product in firm_agent.a_lst_product:
# init extra capacity based on discrete uniform distribution
assert self.str_cap_limit_prob_type in ['uniform', 'normal'], \
"cap_limit_prob_type other than uniform, normal"
if self.str_cap_limit_prob_type == 'uniform':
extra_cap_mean = \
firm_agent.revenue_log / self.flt_cap_limit_level
extra_cap = self.nprandom.integers(extra_cap_mean-2,
extra_cap_mean+2)
extra_cap = 0 if round(extra_cap) < 0 else round(extra_cap)
# print(firm_agent.name, extra_cap)
firm_agent.dct_prod_capacity[product] = extra_cap
elif self.str_cap_limit_prob_type == 'normal':
extra_cap_mean = \
firm_agent.revenue_log / self.flt_cap_limit_level
extra_cap = self.nprandom.normal(extra_cap_mean, 1)
extra_cap = 0 if round(extra_cap) < 0 else round(extra_cap)
# print(firm_agent.name, extra_cap)
firm_agent.dct_prod_capacity[product] = extra_cap
self.firm_network.add_agents([firm_agent], [ag_node])
self.a_lst_total_firms = ap.AgentList(self, self.firm_network.agents)
@ -406,6 +386,7 @@ class Model(ap.Model):
# seek_alt_supply
# shuffle self.a_lst_total_firms
self.a_lst_total_firms = self.a_lst_total_firms.shuffle()
is_stop_trial = True
for firm in self.a_lst_total_firms:
# if len(firm.a_lst_up_product_removed) > 0:
# firm.seek_alt_supply()
@ -420,8 +401,12 @@ class Model(ap.Model):
lst_seek_prod.append(supply)
# commmon supply only seek once
lst_seek_prod = list(set(lst_seek_prod))
if len(lst_seek_prod) > 0:
is_stop_trial = False
for supply in lst_seek_prod:
firm.seek_alt_supply(supply)
if is_stop_trial:
break
# handle_request
# shuffle self.a_lst_total_firms

View File

@ -1,2 +1,2 @@
X1,X2,X3,X4,X5,X6,X7,X8,X9,X10
0,0,0,0,0,0,0,0,0,0
X1,X2,X3,X4,X5,X6,X7,X8,X9,X10,X11
0,0,0,0,0,0,0,0,0,0,0

1 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11
2 0 0 0 0 0 0 0 0 0 0 0

1
orm.py
View File

@ -63,6 +63,7 @@ class Experiment(Base):
crit_supplier = Column(DECIMAL(8, 4), nullable=False)
diff_remove = Column(DECIMAL(8, 4), nullable=False)
proactive_ratio = Column(DECIMAL(8, 4), nullable=False)
drop_t = Column(Integer, nullable=False)
netw_prf_n = Column(Integer, nullable=False)
sample = relationship(

View File

@ -1,4 +1,4 @@
n_max_trial,prf_size,prf_conn,cap_limit_prob_type,cap_limit_level,diff_new_conn,crit_supplier,diff_remove,proactive_ratio,netw_prf_n
15,TRUE,TRUE,uniform,5,0.3,2,0.5,0.3,3
10,FALSE,FALSE,normal,10,0.5,1,1,0.5,2
5,,,,15,0.7,0.5,2,0.7,1
n_max_trial,prf_size,prf_conn,cap_limit_prob_type,cap_limit_level,diff_new_conn,crit_supplier,diff_remove,proactive_ratio,drop_t,netw_prf_n
15,TRUE,TRUE,uniform,5,0.3,2,0.5,0.3,3,3
10,FALSE,FALSE,normal,10,0.5,1,1,0.5,5,2
5,,,,15,0.7,0.5,2,0.7,7,1

1 n_max_trial prf_size prf_conn cap_limit_prob_type cap_limit_level diff_new_conn crit_supplier diff_remove proactive_ratio drop_t netw_prf_n
2 15 TRUE TRUE uniform 5 0.3 2 0.5 0.3 3 3
3 10 FALSE FALSE normal 10 0.5 1 1 0.5 5 2
4 5 15 0.7 0.5 2 0.7 7 1

View File

@ -1,2 +1,2 @@
n_max_trial,prf_size,prf_conn,cap_limit_prob_type,cap_limit_level,diff_new_conn,crit_supplier,diff_remove,proactive_ratio,netw_prf_n
10,TRUE,TRUE,uniform,10,0.1,1,0.01,0,2
n_max_trial,prf_size,prf_conn,cap_limit_prob_type,cap_limit_level,diff_new_conn,crit_supplier,diff_remove,proactive_ratio,drop_t,netw_prf_n
10,TRUE,TRUE,uniform,10,0.1,1,0.01,0,5,2

1 n_max_trial prf_size prf_conn cap_limit_prob_type cap_limit_level diff_new_conn crit_supplier diff_remove proactive_ratio drop_t netw_prf_n
2 10 TRUE TRUE uniform 10 0.1 1 0.01 0 5 2