极大优化模型运行速度

This commit is contained in:
Cricial 2025-01-18 11:30:44 +08:00
parent 2ccdc976e7
commit 3d97db42d0
12 changed files with 679 additions and 635 deletions

BIN
G_Firm_add_edges.pkl Normal file

Binary file not shown.

Binary file not shown.

View File

@ -44,227 +44,8 @@ class Computation:
product_network_test = nx.adjacency_graph(json.loads(dct_sample_para['g_bom']))
model = MyModel(dct_sample_para)
for i in range(1):
model.step()
print(i, datetime.datetime.now())
model.end()
model.step() # 运行仿真一步
return False
def initialize_firm_network(self):
# Read the firm data
firm = pd.read_csv("input_data/input_firm_data/Firm_amended.csv")
firm['Code'] = firm['Code'].astype(str)
firm.fillna(0, inplace=True)
firm_attr = firm.loc[:, ["Code", "Type_Region", "Revenue_Log"]]
firm_industry_relation = pd.read_csv("input_data/firm_industry_relation.csv")
firm_industry_relation['Firm_Code'] = firm_industry_relation['Firm_Code'].astype('string')
firm_product = []
grouped = firm_industry_relation.groupby('Firm_Code')['Product_Code'].apply(list)
firm_product.append(grouped)
firm_attr['Product_Code'] = firm_attr['Code'].map(grouped)
firm_attr.set_index('Code', inplace=True)
grouped = firm_industry_relation.groupby('Firm_Code')
self.firm_prod_labels_dict = {code: group['Product_Code'].tolist() for code, group in grouped}
# 遍历'Product_Code' 与 index 交换
for index, row in firm_attr.iterrows():
id_index_list = []
for i in row['Product_Code']:
for key_values in self.id_code.items():
if int(key_values[0]) == i:
for id in key_values[1]:
id_index_list.append(id)
firm_attr.at[index, 'Product_Code'] = id_index_list
self.G_Firm.add_nodes_from(firm["Code"])
# Assign attributes to the firm nodes
firm_labels_dict = {code: firm_attr.loc[code].to_dict() for code in self.G_Firm.nodes}
nx.set_node_attributes(self.G_Firm, firm_labels_dict)
self.Firm = firm
def initialize_firm_product_network(self):
firm_industry_relation = pd.read_csv("input_data/firm_industry_relation.csv")
firm_industry_relation['Firm_Code'] = firm_industry_relation['Firm_Code'].astype('string')
firm_industry_relation['Product_Code'] = firm_industry_relation['Product_Code'].apply(lambda x: [x])
# 将 'firm_prod' 表中的每一行作为图中的节点
self.G_FirmProd.add_nodes_from(firm_industry_relation.index)
# 为每个节点分配属性
# 遍历'Product_Code' 与 index 交换
for index, row in firm_industry_relation.iterrows():
id_index_list = []
for i in row['Product_Code']:
for key_values in self.id_code.items():
if int(key_values[0]) == i:
for id in key_values[1]:
id_index_list.append(id)
firm_industry_relation.at[index, 'Product_Code'] = id_index_list
firm_prod_labels_dict = {code: firm_industry_relation.loc[code].to_dict() for code in
firm_industry_relation.index}
nx.set_node_attributes(self.G_FirmProd, firm_prod_labels_dict)
def add_edges_to_firm_network(self):
""" Add edges between firms based on the product BOM relationships """
# Add edges to G_Firm according to G_bom
for node in nx.nodes(self.G_Firm):
lst_pred_product_code = []
for product_code in self.G_Firm.nodes[node]['Product_Code']:
lst_pred_product_code += list(self.G_bom.predecessors(product_code))
lst_pred_product_code = list(set(lst_pred_product_code))
lst_pred_product_code = list(sorted(lst_pred_product_code)) # Ensure consistency
for pred_product_code in lst_pred_product_code:
# Get a list of firms producing the component (pred_product_code)
lst_pred_firm = [firm_code for firm_code, product in self.firm_prod_labels_dict.items() if
pred_product_code in product]
# Select multiple suppliers (multi-sourcing)
n_pred_firm = self.int_netw_prf_n
if n_pred_firm > len(lst_pred_firm):
n_pred_firm = len(lst_pred_firm)
if self.is_prf_size:
# 获取 firm 的 size 列表
lst_pred_firm_size = [self.G_Firm.nodes[pred_firm]['Revenue_Log'] for pred_firm in lst_pred_firm]
# 检查 lst_pred_firm_size 是否为空或总和为 0
if len(lst_pred_firm_size) == 0 or sum(lst_pred_firm_size) == 0:
# print("警告: lst_pred_firm_size 为空或总和为 0无法生成概率分布")
lst_choose_firm = [] # 返回空结果,或根据需要处理
else:
# 计算总和
sum_pred_firm_size = sum(lst_pred_firm_size)
# 归一化生成 lst_prob
lst_prob = [size / sum_pred_firm_size for size in lst_pred_firm_size]
# 使用 np.isclose() 确保概率总和接近 1
if not pd.np.isclose(sum(lst_prob), 1.0):
# print(f"警告: 概率总和为 {sum(lst_prob)},现在进行修正")
lst_prob = [prob / sum(lst_prob) for prob in lst_prob]
# 确保没有负值或 0
lst_prob = [max(0, prob) for prob in lst_prob]
# 根据修正后的概率选择 firm
lst_choose_firm = self.nprandom.choice(lst_pred_firm, n_pred_firm, replace=False, p=lst_prob)
else:
# 直接进行随机选择
lst_choose_firm = self.nprandom.choice(lst_pred_firm, n_pred_firm, replace=False)
# Add edges from predecessor firms to current node (firm)
lst_add_edge = [(pred_firm, node, {'Product': pred_product_code}) for pred_firm in lst_choose_firm]
self.G_Firm.add_edges_from(lst_add_edge)
# Add edges to firm-product network
self.add_edges_to_firm_product_network(node, pred_product_code, lst_choose_firm)
def add_edges_to_firm_product_network(self, node, pred_product_code, lst_choose_firm):
""" Helper function to add edges to the firm-product network """
set_node_prod_code = set(self.G_Firm.nodes[node]['Product_Code'])
set_pred_succ_code = set(self.G_bom.successors(pred_product_code))
lst_use_pred_prod_code = list(set_node_prod_code & set_pred_succ_code)
if len(lst_use_pred_prod_code) == 0:
print("错误")
pred_node_list = []
for pred_firm in lst_choose_firm:
for n, v in self.G_FirmProd.nodes(data=True):
for v1 in v['Product_Code']:
if v1 == pred_product_code and v['Firm_Code'] == pred_firm:
pred_node_list.append(n)
if len(pred_node_list) != 0:
pred_node = pred_node_list[0]
else:
pred_node = -1
current_node_list = []
for use_pred_prod_code in lst_use_pred_prod_code:
for n, v in self.G_FirmProd.nodes(data=True):
for v1 in v['Product_Code']:
if v1 == use_pred_prod_code and v['Firm_Code'] == node:
current_node_list.append(n)
if len(current_node_list) != 0:
current_node = current_node_list[0]
else:
current_node = -1
if current_node != -1 and pred_node != -1:
self.G_FirmProd.add_edge(pred_node, current_node)
def connect_unconnected_nodes(self):
""" Connect unconnected nodes in the firm network """
for node in nx.nodes(self.G_Firm):
if self.G_Firm.degree(node) == 0:
current_node_list = []
for product_code in self.G_Firm.nodes[node]['Product_Code']:
for n, v in self.G_FirmProd.nodes(data=True):
for v1 in v['Product_Code']:
if v['Firm_Code'] == node and v1 == product_code:
current_node_list.append(n)
if len(current_node_list) != 0:
current_node = current_node_list[0]
else:
current_node = -1
lst_succ_product_code = list(self.G_bom.successors(product_code))
for succ_product_code in lst_succ_product_code:
lst_succ_firm = [firm_code for firm_code, product in self.firm_prod_labels_dict.items() if
succ_product_code in product]
n_succ_firm = self.int_netw_prf_n
if n_succ_firm > len(lst_succ_firm):
n_succ_firm = len(lst_succ_firm)
if self.is_prf_size:
lst_succ_firm_size = [self.G_Firm.nodes[succ_firm]['Revenue_Log'] for succ_firm in
lst_succ_firm]
if len(lst_succ_firm_size) == 0 or sum(lst_succ_firm_size) == 0:
# print("警告: lst_pred_firm_size 为空或总和为 0无法生成概率分布")
lst_choose_firm = [] # 返回空结果,或根据需要处理
else:
# 计算总和
sum_pred_firm_size = sum(lst_succ_firm_size)
# 归一化生成 lst_prob
lst_prob = [size / sum_pred_firm_size for size in lst_succ_firm_size]
# 使用 np.isclose() 确保概率总和接近 1
if not pd.np.isclose(sum(lst_prob), 1.0):
# print(f"警告: 概率总和为 {sum(lst_prob)},现在进行修正")
lst_prob = [prob / sum(lst_prob) for prob in lst_prob]
# 确保没有负值或 0
lst_prob = [max(0, prob) for prob in lst_prob]
lst_choose_firm = self.nprandom.choice(lst_succ_firm, n_succ_firm, replace=False,
p=lst_prob)
else:
lst_choose_firm = self.nprandom.choice(lst_succ_firm, n_succ_firm, replace=False)
lst_add_edge = [(node, succ_firm, {'Product': product_code}) for succ_firm in
lst_choose_firm]
self.G_Firm.add_edges_from(lst_add_edge)
# Add edges to firm-product network
succ_node_list = []
for succ_firm in lst_choose_firm:
for n, v in self.G_FirmProd.nodes(data=True):
for v1 in v['Product_Code']:
if v1 == succ_product_code and v['Firm_Code'] == succ_firm:
succ_node_list.append(n)
if len(succ_node_list) != 0:
succ_node = succ_node_list[0]
else:
succ_node = -1
if current_node != -1 and succ_node != -1:
self.G_FirmProd.add_edge(current_node, succ_node)
self.sample.g_firm = json.dumps(nx.adjacency_data(self.G_Firm))
self.firm_network = self.G_Firm # 直接使用 networkx 图对象
self.firm_prod_network = self.G_FirmProd # 直接使用 networkx 图对象

Binary file not shown.

BIN
firm_network_cache.pkl Normal file

Binary file not shown.

Binary file not shown.

View File

@ -11142,4 +11142,4 @@
11221883697,245.891363896894,142.090621109369,353.107586951086,22.31839,吉林省新胜工程材料有限公司,吉林省,2.5,0
11222997596,245.891363896894,142.090621109369,353.107586951086,22.31839,临泽众城国发新能源有限公司,甘肃省,2.5,0
11223028855,245.891363896894,142.090621109369,353.107586951086,22.31839,兰州海锐普技术有限公司,甘肃省,2.5,0
11223297226,245.891363896894,142.090621109369,353.107586951086,22.31839,吉林省纪明科技有限公司,吉林省,2.5,0
11223297226,245.891363896894,142.090621109369,353.107586951086,22.31839,吉林省纪明科技有限公司,吉林省,2.5,0
Can't render this file because it is too large.

View File

@ -4965,7 +4965,6 @@
2313177432,7,142.0
2313179005,91,3.0
2313208830,91,3.0
2313209417,32,44.0
2313251141,12,719.0
2313307136,7,71.0
2313316346,95,93.0

1 Firm_Code 材料id 材料数量
4965 2313177432 7 142.0
4966 2313179005 91 3.0
4967 2313208830 91 3.0
2313209417 32 44.0
4968 2313251141 12 719.0
4969 2313307136 7 71.0
4970 2313316346 95 93.0

View File

@ -3715,7 +3715,6 @@
716918375,15,74.3946333333333
718074552,12,4011.65997468354
718492116,91,205.648022142857
718610477,32,37.4214719971428
719663677,25,1115.9195
720139706,95,8.927356
720139706,84,2.231839
@ -5758,7 +5757,6 @@
2313177432,7,22.31839
2313179005,91,0.398542678571429
2313208830,91,0.478251214285714
2313209417,32,6.862904925
2313251141,12,113.004506329114
2313262775,85,0.607556172222222
2313262775,84,0.309977638888889

1 Firm_Code 产品id 产品数量
3715 716918375 15 74.3946333333333
3716 718074552 12 4011.65997468354
3717 718492116 91 205.648022142857
718610477 32 37.4214719971428
3718 719663677 25 1115.9195
3719 720139706 95 8.927356
3720 720139706 84 2.231839
5757 2313177432 7 22.31839
5758 2313179005 91 0.398542678571429
5759 2313208830 91 0.478251214285714
2313209417 32 6.862904925
5760 2313251141 12 113.004506329114
5761 2313262775 85 0.607556172222222
5762 2313262775 84 0.309977638888889

View File

@ -44,7 +44,7 @@ if __name__ == '__main__':
# 输入参数
parser = argparse.ArgumentParser(description='setting')
parser.add_argument('--exp', type=str, default='without_exp')
parser.add_argument('--job', type=int, default='3')
parser.add_argument('--job', type=int, default='4')
parser.add_argument('--reset_sample', type=int, default='0')
parser.add_argument('--reset_db', type=bool, default=False)

File diff suppressed because it is too large Load Diff