model product firm

This commit is contained in:
2023-03-06 22:38:57 +08:00
parent 64ed87da0b
commit b291578889
7 changed files with 297 additions and 34 deletions

43
firm.py
View File

@@ -20,7 +20,7 @@ class FirmAgent(ap.Agent):
self.dct_num_trial_up_product_removed = {}
self.dct_request_prod_from_firm = {}
def remove_edge_to_cus_and_cus_up_prod(self, remove_product):
def remove_edge_to_cus_remove_cus_up_prod(self, remove_product):
list_out_edges = list(
self.firm_network.graph.out_edges(
self.firm_network.positions[self], keys=True, data='Product'))
@@ -39,7 +39,9 @@ class FirmAgent(ap.Agent):
edge for edge in list_in_edges
if edge[-1] == remove_product.code
]
# print(select_edges)
if len(select_edges) == 0:
print('affect', customer.name, remove_product.code)
if remove_product not in \
customer.a_list_up_product_removed:
customer.a_list_up_product_removed.append(
@@ -57,17 +59,21 @@ class FirmAgent(ap.Agent):
# print(customer.a_list_product_disrupted.code)
def seek_alt_supply(self):
print(self.name, 'seek_alt_supply')
for product in self.a_list_up_product_removed:
print(f"{self.name} seek alt supply for {product.code}")
if self.dct_num_trial_up_product_removed[
product] <= self.model.int_n_max_trial:
# select a list of candidate firm that has the product
candidate_alt_supply = self.model.a_list_total_firms.select([
product in firm.a_list_product
and product not in firm.a_list_product_removed
for firm in self.model.a_list_total_firms
])
# print(candidate_alt_supply)
# print(candidate_alt_supply.name)
# print(candidate_alt_supply.a_list_product.code)
if not candidate_alt_supply:
continue
# select based on size
list_prob = [
size / sum(candidate_alt_supply.revenue_log)
@@ -75,7 +81,7 @@ class FirmAgent(ap.Agent):
]
select_alt_supply = self.model.nprandom.choice(
candidate_alt_supply, p=list_prob)
print('select_alt_supply', select_alt_supply.name)
print(f"{self.name} selct alt supply for {product.code} from {select_alt_supply.name}")
assert product in select_alt_supply.a_list_product, \
f"{select_alt_supply} \
does not produce requested product {product}"
@@ -88,21 +94,33 @@ class FirmAgent(ap.Agent):
select_alt_supply.dct_request_prod_from_firm[product] = [
self
]
print({
key.code: [v.name for v in value]
for key, value in
select_alt_supply.dct_request_prod_from_firm.items()
})
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()
})
self.dct_num_trial_up_product_removed[product] += 1
def handle_request(self):
print(self.name, 'handle_request')
for product, list_firm in self.dct_request_prod_from_firm.items():
# if self.dct_prod_capacity[product] > 0:
# if len(list_firm) == 1:
# self.accept_request(list_firm[0], product)
print(product.code, [firm.name for firm in list_firm])
if self.dct_prod_capacity[product] > 0:
if len(list_firm) == 0:
continue
elif len(list_firm) == 1:
self.accept_request(list_firm[0], product)
elif len(list_firm) > 1:
# handling based on connection
# TBC
# handling based on size
list_size = [firm.revenue_log for firm in list_firm]
list_prob = [size / sum(list_size) for size in list_size]
select_customer = self.model.nprandom.choice(list_firm,
p=list_prob)
self.accept_request(select_customer, product)
# print(product.code, [firm.name for firm in list_firm])
def accept_request(self, down_firm, product):
self.firm_network.graph.add_edges_from([
@@ -114,6 +132,7 @@ class FirmAgent(ap.Agent):
self.dct_prod_capacity[product] -= 1
self.dct_request_prod_from_firm[product].remove(down_firm)
down_firm.a_list_up_product_removed.remove(product)
print(f"{self.name} accept {product.code} request from {down_firm.name}")
def clean_before_trial(self):
self.dct_request_prod_from_firm = {}