无报错

This commit is contained in:
Cricial 2024-10-22 10:51:02 +08:00
parent b0247e2bee
commit f7354a4a44
8 changed files with 11489 additions and 28365 deletions

6
.idea/encodings.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/input_data/input_firm_data/Firm_amended.csv" charset="UTF-8" />
</component>
</project>

Binary file not shown.

View File

@ -39,7 +39,8 @@ class ControllerDB:
self.lst_saved_s_id = []
self.experiment_data = []
self.batch_size = 999 # 根据需求设置每批次的大小
self.batch_size = 2000
# 根据需求设置每批次的大小
def init_tables(self):
self.fill_experiment_table()

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

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='4')
parser.add_argument('--job', type=int, default='1')
parser.add_argument('--reset_sample', type=int, default='0')
parser.add_argument('--reset_db', type=bool, default=False)

View File

@ -91,7 +91,8 @@ class MyModel(Model):
firm = pd.read_csv("input_data/input_firm_data/Firm_amended.csv")
firm['Code'] = firm['Code'].astype('string')
firm['Code'] = firm['Code'].astype(str)
firm.fillna(0, inplace=True)
firm_attr = firm.loc[:, ["Code", "Type_Region", "Revenue_Log", "原材料", "设备数量", "库存商品"]]
@ -136,6 +137,15 @@ class MyModel(Model):
# Add edges to G_Firm according to G_bom
for node in nx.nodes(self.G_Firm):
lst_pred_product_code = []
product_code = self.G_Firm.nodes[node].get('Product_Code')
# 打印值和类型
#print(f"节点 {node} 的 'Product_Code': {product_code}, 类型: {type(product_code)}")
# 如果 'Product_Code' 是 float 类型或单个值,将其转换为列表
if isinstance(product_code, float):
#print(f"警告: 节点 {node} 的 'Product_Code' 为浮点数,已转换为列表")
product_code = [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))
@ -152,13 +162,30 @@ class MyModel(Model):
n_pred_firm = len(lst_pred_firm)
if self.is_prf_size:
lst_pred_firm_size = [self.G_Firm.nodes[pred_firm]['Revenue_Log'] for pred_firm in
lst_pred_firm]
lst_prob = [size / sum(lst_pred_firm_size) for size in lst_pred_firm_size]
lst_choose_firm = self.nprandom.choice(lst_pred_firm, n_pred_firm, replace=False, p=lst_prob)
# 获取 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 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)
@ -400,8 +427,8 @@ class MyModel(Model):
purchase_machinery_firms = {}
material_list = []
machinery_list = []
list_seek_material_firm = [] # 每一个收到请求的企业
list_seek_machinery_firm = [] # 每一个收到请求的企业
list_seek_material_firm = [] # 每一个收到请求的企业
list_seek_machinery_firm = [] # 每一个收到请求的企业
for firm in self.company_agents:
# 资源