IIabm/firm.py

147 lines
6.5 KiB
Python
Raw Normal View History

2023-02-24 15:16:28 +08:00
import agentpy as ap
import math
2023-02-24 15:16:28 +08:00
2023-02-24 17:53:55 +08:00
class FirmAgent(ap.Agent):
2023-03-14 18:53:00 +08:00
def setup(self, code, name, type_region, revenue_log, a_lst_product):
2023-02-24 15:16:28 +08:00
self.firm_network = self.model.firm_network
2023-02-27 22:02:46 +08:00
self.product_network = self.model.product_network
2023-02-24 17:53:55 +08:00
self.code = code
self.name = name
self.type_region = type_region
self.revenue_log = revenue_log
2023-03-14 18:53:00 +08:00
self.a_lst_product = a_lst_product
self.dct_prod_capacity = dict.fromkeys(self.a_lst_product)
2023-02-24 17:53:55 +08:00
2023-03-14 18:53:00 +08:00
self.a_lst_up_product_removed = ap.AgentList(self.model, [])
self.a_lst_product_disrupted = ap.AgentList(self.model, [])
self.a_lst_product_removed = ap.AgentList(self.model, [])
2023-02-26 21:58:05 +08:00
2023-03-14 18:53:00 +08:00
self.dct_n_trial_up_product_removed = {}
2023-02-27 22:02:46 +08:00
self.dct_request_prod_from_firm = {}
2023-03-06 22:38:57 +08:00
def remove_edge_to_cus_remove_cus_up_prod(self, remove_product):
2023-03-14 18:53:00 +08:00
lst_out_edge = list(
2023-02-27 22:02:46 +08:00
self.firm_network.graph.out_edges(
self.firm_network.positions[self], keys=True, data='Product'))
2023-03-14 18:53:00 +08:00
for n1, n2, key, product_code in lst_out_edge:
2023-02-27 22:02:46 +08:00
if product_code == remove_product.code:
# remove edge
self.firm_network.graph.remove_edge(n1, n2, key)
2023-03-14 17:51:17 +08:00
# remove customer up product conditionally
2023-02-27 22:02:46 +08:00
customer = ap.AgentIter(self.model, n2).to_list()[0]
2023-03-14 18:53:00 +08:00
lst_in_edge = list(
2023-02-28 16:56:12 +08:00
self.firm_network.graph.in_edges(n2,
keys=True,
data='Product'))
2023-03-14 18:53:00 +08:00
lst_select_in_edge = [
edge for edge in lst_in_edge
2023-02-28 16:56:12 +08:00
if edge[-1] == remove_product.code
]
2023-03-14 18:53:00 +08:00
prod_remove = math.exp(-1 * len(lst_select_in_edge))
if self.model.nprandom.choice([True, False],
p=[prod_remove,
1 - prod_remove]):
print(self.name, remove_product.code, 'affect',
customer.name)
2023-02-28 16:56:12 +08:00
if remove_product not in \
2023-03-14 18:53:00 +08:00
customer.a_lst_up_product_removed:
customer.a_lst_up_product_removed.append(
2023-02-28 16:56:12 +08:00
remove_product)
2023-03-14 18:53:00 +08:00
customer.dct_n_trial_up_product_removed[
2023-02-28 16:56:12 +08:00
remove_product] = 0
2023-02-27 22:02:46 +08:00
def seek_alt_supply(self):
2023-03-14 18:53:00 +08:00
for product in self.a_lst_up_product_removed:
2023-03-06 22:38:57 +08:00
print(f"{self.name} seek alt supply for {product.code}")
2023-03-14 18:53:00 +08:00
if self.dct_n_trial_up_product_removed[
2023-02-27 22:02:46 +08:00
product] <= self.model.int_n_max_trial:
2023-03-06 22:38:57 +08:00
# select a list of candidate firm that has the product
2023-03-14 18:53:00 +08:00
candidate_alt_supply = self.model.a_lst_total_firms.select([
product in firm.a_lst_product
and product not in firm.a_lst_product_removed
for firm in self.model.a_lst_total_firms
2023-02-27 22:02:46 +08:00
])
2023-03-06 22:38:57 +08:00
if not candidate_alt_supply:
continue
2023-02-27 22:02:46 +08:00
# select based on size
2023-03-14 18:53:00 +08:00
lst_prob = [
2023-02-27 22:02:46 +08:00
size / sum(candidate_alt_supply.revenue_log)
for size in candidate_alt_supply.revenue_log
]
select_alt_supply = self.model.nprandom.choice(
2023-03-14 18:53:00 +08:00
candidate_alt_supply, p=lst_prob)
print(
f"{self.name} selct alt supply for {product.code} from {select_alt_supply.name}"
)
assert product in select_alt_supply.a_lst_product, \
2023-02-27 22:02:46 +08:00
f"{select_alt_supply} \
does not produce requested product {product}"
if product in select_alt_supply.dct_request_prod_from_firm.\
keys():
select_alt_supply.dct_request_prod_from_firm[
product].append(self)
else:
select_alt_supply.dct_request_prod_from_firm[product] = [
self
]
2023-03-06 22:38:57 +08:00
print(
select_alt_supply.name, 'dct_request_prod_from_firm', {
key.code: [v.name for v in value]
for key, value in
select_alt_supply.dct_request_prod_from_firm.items()
})
2023-02-27 22:02:46 +08:00
2023-03-14 18:53:00 +08:00
self.dct_n_trial_up_product_removed[product] += 1
2023-02-27 22:02:46 +08:00
def handle_request(self):
print(self.name, 'handle_request')
2023-03-14 18:53:00 +08:00
for product, lst_firm in self.dct_request_prod_from_firm.items():
2023-03-06 22:38:57 +08:00
if self.dct_prod_capacity[product] > 0:
2023-03-14 18:53:00 +08:00
if len(lst_firm) == 0:
2023-03-06 22:38:57 +08:00
continue
2023-03-14 18:53:00 +08:00
elif len(lst_firm) == 1:
self.accept_request(lst_firm[0], product)
elif len(lst_firm) > 1:
2023-03-06 22:38:57 +08:00
# handling based on connection
# TBC
# handling based on size
2023-03-14 18:53:00 +08:00
lst_firm_size = [firm.revenue_log for firm in lst_firm]
lst_prob = [
size / sum(lst_firm_size) for size in lst_firm_size
]
select_customer = self.model.nprandom.choice(lst_firm,
p=lst_prob)
2023-03-06 22:38:57 +08:00
self.accept_request(select_customer, product)
2023-02-27 22:02:46 +08:00
def accept_request(self, down_firm, product):
2023-03-14 18:53:00 +08:00
lst_firm_size = [
firm.revenue_log for firm in self.model.a_lst_total_firms
if product in firm.a_lst_product
]
prod_accept = self.revenue_log / sum(lst_firm_size)
if self.model.nprandom.choice([True, False],
p=[prod_accept, 1 - prod_accept]):
self.firm_network.graph.add_edges_from([
(self.firm_network.positions[self],
2023-03-14 18:53:00 +08:00
self.firm_network.positions[down_firm], {
'Product': product.code
})
])
self.dct_prod_capacity[product] -= 1
self.dct_request_prod_from_firm[product].remove(down_firm)
2023-03-14 18:53:00 +08:00
down_firm.a_lst_up_product_removed.remove(product)
print(
f"{self.name} accept {product.code} request from {down_firm.name}"
)
2023-02-27 22:02:46 +08:00
def clean_before_trial(self):
self.dct_request_prod_from_firm = {}
2023-03-07 12:29:27 +08:00
def clean_before_time_step(self):
2023-03-14 18:53:00 +08:00
self.dct_n_trial_up_product_removed = {}
self.a_lst_up_product_removed = ap.AgentList(self.model, [])