model firm product
This commit is contained in:
102
model.py
102
model.py
@@ -3,16 +3,19 @@ import pandas as pd
|
||||
import numpy as np
|
||||
import networkx as nx
|
||||
from firm import FirmAgent
|
||||
from product import ProductAgent
|
||||
|
||||
sample = 0
|
||||
seed = 0
|
||||
n_iter = 3
|
||||
dct_list_init_remove_firm_prod = {0: ['1.4.4'], 2: ['1.1.3']}
|
||||
n_max_trial = 2
|
||||
dct_sample_para = {
|
||||
'sample': sample,
|
||||
'seed': seed,
|
||||
'n_iter': n_iter,
|
||||
'dct_list_init_remove_firm_prod': dct_list_init_remove_firm_prod
|
||||
'n_max_trial': n_max_trial,
|
||||
'dct_list_init_remove_firm_prod': dct_list_init_remove_firm_prod,
|
||||
}
|
||||
|
||||
|
||||
@@ -20,8 +23,9 @@ class Model(ap.Model):
|
||||
def setup(self):
|
||||
self.sample = self.p.sample
|
||||
self.nprandom = np.random.default_rng(self.p.seed)
|
||||
self.dct_list_remove_firm_prod = self.p.dct_list_init_remove_firm_prod
|
||||
self.int_n_iter = int(self.p.n_iter)
|
||||
self.int_n_max_trial = int(self.p.n_max_trial)
|
||||
self.dct_list_remove_firm_prod = self.p.dct_list_init_remove_firm_prod
|
||||
|
||||
# init graph bom
|
||||
BomNodes = pd.read_csv('BomNodes.csv', index_col=0)
|
||||
@@ -85,6 +89,7 @@ class Model(ap.Model):
|
||||
# print('-' * 20)
|
||||
|
||||
self.firm_network = ap.Network(self, G_Firm)
|
||||
self.product_network = ap.Network(self, G_bom)
|
||||
# print([node.label for node in self.firm_network.nodes])
|
||||
# print([list(self.firm_network.graph.predecessors(node))
|
||||
# for node in self.firm_network.nodes])
|
||||
@@ -92,6 +97,15 @@ class Model(ap.Model):
|
||||
# for node in self.firm_network.nodes])
|
||||
# print([v for v in self.firm_network.graph.nodes(data=True)])
|
||||
|
||||
# init product
|
||||
for ag_node, attr in self.product_network.graph.nodes(data=True):
|
||||
product_agent = ProductAgent(self,
|
||||
code=ag_node.label,
|
||||
name=attr['Name'])
|
||||
self.product_network.add_agents([product_agent], [ag_node])
|
||||
self.a_list_total_products = ap.AgentList(self,
|
||||
self.product_network.agents)
|
||||
|
||||
# init firm
|
||||
for ag_node, attr in self.firm_network.graph.nodes(data=True):
|
||||
firm_agent = FirmAgent(
|
||||
@@ -100,38 +114,56 @@ class Model(ap.Model):
|
||||
name=attr['Name'],
|
||||
type_region=attr['Type_Region'],
|
||||
revenue_log=attr['Revenue_Log'],
|
||||
list_product=attr['Product_Code'],
|
||||
# init capacity as the degree of out edges
|
||||
capacity=self.firm_network.graph.out_degree(ag_node))
|
||||
a_list_product=self.a_list_total_products.select([
|
||||
code in attr['Product_Code']
|
||||
for code in self.a_list_total_products.code
|
||||
]))
|
||||
# init capacity as the degree of out edges of a specific product
|
||||
list_out_edges = list(
|
||||
self.firm_network.graph.out_edges(ag_node,
|
||||
keys=True,
|
||||
data='Product'))
|
||||
for product in firm_agent.a_list_product:
|
||||
capacity = len([
|
||||
edge for edge in list_out_edges if edge[-1] == product.code
|
||||
])
|
||||
firm_agent.dct_prod_capacity[product] = capacity
|
||||
# print(firm_agent.name, firm_agent.dct_prod_capacity)
|
||||
|
||||
self.firm_network.add_agents([firm_agent], [ag_node])
|
||||
self.a_list_total_firms = ap.AgentList(self, self.firm_network.agents)
|
||||
# print(list(zip(self.a_list_total_firms.code,
|
||||
# self.a_list_total_firms.name,
|
||||
# self.a_list_total_firms.capacity)))
|
||||
|
||||
# set the initial firm product that are removed
|
||||
# init dct_list_remove_firm_prod (from string to agent)
|
||||
t_dct = {}
|
||||
for firm_code, list_product in self.dct_list_remove_firm_prod.items():
|
||||
firm = self.a_list_total_firms.select(
|
||||
self.a_list_total_firms.code == firm_code)[0]
|
||||
for product in list_product:
|
||||
assert product in firm.list_product, \
|
||||
f"product {product} not in firm {firm_code}"
|
||||
firm.dct_product_is_removed[product] = True
|
||||
t_dct[firm] = self.a_list_total_products.select([
|
||||
code in list_product
|
||||
for code in self.a_list_total_products.code
|
||||
])
|
||||
self.dct_list_remove_firm_prod = t_dct
|
||||
|
||||
# set the initial firm product that are removed
|
||||
for firm, a_list_product in self.dct_list_remove_firm_prod.items():
|
||||
for product in a_list_product:
|
||||
assert product in firm.a_list_product, \
|
||||
f"product {product.code} not in firm {firm.code}"
|
||||
firm.a_list_product_removed.append(product)
|
||||
|
||||
def update(self):
|
||||
# Update the firm that is removed
|
||||
# update the firm that is removed
|
||||
self.dct_list_remove_firm_prod = {}
|
||||
for firm in self.a_list_total_firms:
|
||||
for product, flag in firm.dct_product_is_removed.items():
|
||||
if flag is True:
|
||||
if firm.code in self.dct_list_remove_firm_prod.keys():
|
||||
self.dct_list_remove_firm_prod[firm.code].append(
|
||||
product)
|
||||
else:
|
||||
self.dct_list_remove_firm_prod[firm.code] = [product]
|
||||
if len(firm.a_list_product_removed) > 0:
|
||||
self.dct_list_remove_firm_prod[
|
||||
firm] = firm.a_list_product_removed
|
||||
# print(self.dct_list_remove_firm_prod)
|
||||
|
||||
# Stop simulation if reached terminal number of iteration
|
||||
# stop simulation if reached terminal number of iteration
|
||||
if self.t == self.int_n_iter or len(
|
||||
self.dct_list_remove_firm_prod) == 0:
|
||||
self.stop()
|
||||
@@ -141,14 +173,34 @@ class Model(ap.Model):
|
||||
dct_key_list = list(self.dct_list_remove_firm_prod.keys())
|
||||
self.nprandom.shuffle(dct_key_list)
|
||||
self.dct_list_remove_firm_prod = {
|
||||
key: self.dct_list_remove_firm_prod[key]
|
||||
key: self.dct_list_remove_firm_prod[key].shuffle()
|
||||
for key in dct_key_list
|
||||
}
|
||||
for firm_code, list_product in self.dct_list_remove_firm_prod.items():
|
||||
firm = self.a_list_total_firms.select(
|
||||
self.a_list_total_firms.code == firm_code)[0]
|
||||
for product in list_product:
|
||||
firm.remove_edge_to_customer_if_removed(product)
|
||||
# print(self.dct_list_remove_firm_prod)
|
||||
|
||||
# remove_edge_to_cus_and_cus_up_prod
|
||||
for firm, a_list_product in self.dct_list_remove_firm_prod.items():
|
||||
for product in a_list_product:
|
||||
firm.remove_edge_to_cus_and_cus_up_prod(product)
|
||||
|
||||
for n_trial in range(self.int_n_max_trial):
|
||||
print('='*20, n_trial, '='*20)
|
||||
# seek_alt_supply
|
||||
for firm in self.a_list_total_firms:
|
||||
if len(firm.a_list_up_product_removed) > 0:
|
||||
# print(firm.name)
|
||||
# print(firm.a_list_up_product_removed.code)
|
||||
firm.seek_alt_supply()
|
||||
|
||||
# handle_request
|
||||
for firm in self.a_list_total_firms:
|
||||
if len(firm.dct_request_prod_from_firm) > 0:
|
||||
firm.handle_request()
|
||||
|
||||
# reset dct_request_prod_from_firm
|
||||
self.a_list_total_firms.clean_before_trial()
|
||||
# do not use:
|
||||
# self.a_list_total_firms.dct_request_prod_from_firm = {} why?
|
||||
|
||||
def end(self):
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user