优化 干扰列表

This commit is contained in:
Cricial 2025-01-27 01:04:21 +08:00
parent 13521ff752
commit 3a46d09b8e
9 changed files with 180 additions and 93 deletions

View File

@ -3,6 +3,13 @@
<component name="CsvFileAttributes">
<option name="attributeMap">
<map>
<entry key="C:\Users\www\Desktop\python项目\数据\抽样第3次数据\firm_amended.csv">
<value>
<Attribute>
<option name="separator" value="," />
</Attribute>
</value>
</entry>
<entry key="\input_data\device_salvage_values.csv">
<value>
<Attribute>

Binary file not shown.

Binary file not shown.

View File

@ -67,12 +67,27 @@ class ControllerDB:
# 行索引 (index):这一行在数据帧中的索引值。
# 行数据 (row):这一行的数据,是一个 pandas.Series 对象,包含该行的所有列和值。
# 读取企业与产品关系数据
firm_industry = pd.read_csv("input_data/firm_industry_relation.csv")
firm_industry['Firm_Code'] = firm_industry['Firm_Code'].astype('string')
# 假设已从 BOM 数据构建了 code_to_indices
bom_nodes = pd.read_csv("input_data/input_product_data/BomNodes.csv")
code_to_indices = bom_nodes.groupby('Code')['Index'].apply(list).to_dict()
# 初始化存储映射结果的列表
list_dct = []
# 遍历 firm_industry 数据
for _, row in firm_industry.iterrows():
code = row['Firm_Code']
row = row['Product_Code']
dct = {code: [row]}
firm_code = row['Firm_Code'] # 企业代码
product_code = row['Product_Code'] # 原始产品代码
# 使用 code_to_indices 映射 Product_Code 到 Product_Indices
mapped_indices = code_to_indices.get(product_code, []) # 如果找不到则返回空列表
# 构建企业到产品索引的映射
dct = {firm_code: mapped_indices}
list_dct.append(dct)
# fill g_bom

10
firm.py
View File

@ -32,13 +32,13 @@ class FirmAgent(Agent):
# 包括 产品时间
self.P1 = {0: P}
# 企业i的供应商
self.upper_i = [agent for u, v in self.firm_network.in_edges(self.unique_id)
for agent in self.model.company_agents if agent.unique_id == u]
self.upper_i = [self.model.agent_map[u] for u, v in self.firm_network.in_edges(self.unique_id)
if u in self.model.agent_map]
# 企业i的客户
self.downer_i = [agent for u, v in self.firm_network.out_edges(self.unique_id)
for agent in self.model.company_agents if agent.unique_id == u]
self.downer_i = [self.model.agent_map[v] for u, v in self.firm_network.out_edges(self.unique_id)
if v in self.model.agent_map]
# 设备c的数量 (总量) 使用这个来判断设备数量
#self.n_equip_c = n_equip_c
# self.n_equip_c = n_equip_c
# 设备c产量 根据设备量进行估算
self.c_yield = production_output
# 消耗材料量 根据设备量进行估算 { }

BIN
firm_network.pkl Normal file

Binary file not shown.

View File

@ -220,3 +220,22 @@
11164476478,20163.0918395453,11651.4309309683,28954.8221299891,1830.10798,江苏芯诺半导体科技有限公司,江苏省,4.5,0
11212932825,491.782727793788,284.181242218739,706.215173902172,44.63678,河南雷电光源技术研究院,河南省,2.8,0
11220388001,737.674091690682,426.271863328108,1059.32276085326,66.95517,新疆蚂蚁网约出租汽车服务有限公司,新疆维吾尔自治区,3.0,0
397847929,4794.881596,2770.767112,6885.597946,435.208605,厦门科塔电子有限公司,福建省,3.8,0
517717050,40080.29232,23160.77124,57556.53667,3637.89757,深圳市实锐泰科技有限公司,广东省,4.8,0
737770776,5962.865574,3445.697562,8562.858984,541.2209575,福建省南安市物资有限公司,福建省,3.9,0
872394725,25724.57103,15462.17311,55978.44597,7283.825438,北京凯德石英股份有限公司,北京市,4.7,0
2311581270,32088.82299,18542.82605,46080.5401,2912.549895,安徽亚格盛电子新材料股份有限公司,安徽省,4.7,0
2313209417,15122.31888,8738.573198,21716.1166,1372.580985,武汉拓材科技有限公司,湖北省,4.3,0
2347013470,23006.64933,17588.56669,81317.91108,12851.94016,江苏先锋精密科技股份有限公司,江苏省,4.9,0
2350418059,73931.33674,42721.91341,106167.6811,6710.395927,江苏纳沛斯半导体有限公司,江苏省,5.0,0
2352951203,29261.0723,16908.78391,42019.80285,2655.88841,中山市江波龙电子有限公司,广东省,4.6,0
2359644835,232613.2302,134417.7276,334039.7773,21113.19694,广东气派科技有限公司,广东省,5.5,0
2388955897,2458.913639,1420.906211,3531.07587,223.1839,上海鑫匀源科技有限公司,上海市,3.5,0
2977767486,9790.429458,5657.491099,14059.35884,1748.344959,深圳市三联盛科技股份有限公司,广东省,4.1,0
3031009366,43768.66277,25292.13056,62853.15048,3972.67342,天通瑞宏科技有限公司,浙江省,4.8,0
3089095447,245.8913639,142.0906211,353.107587,22.31839,常州金坛沸腾商贸有限公司,江苏省,2.5,0
3100891962,14801.53049,8553.202633,21255.45458,3889.830944,北京中超伟业信息安全技术股份有限公司,北京市,4.3,0
3151203276,341884.6624,201430.569,477385.6924,28574.49401,合肥颀中科技股份有限公司,安徽省,5.7,0
3188352290,13032.24229,7530.802919,18714.70211,1182.87467,湖北深紫科技有限公司,湖北省,4.3,0
3288105727,6639.066825,3836.44677,9533.904848,602.59653,奥趋光电技术(杭州)有限公司,浙江省,4.0,0
3462551351,5901.392734,3410.174907,8474.582087,535.64136,无锡博加电子新材料有限公司,江苏省,3.9,0

1 Code 固定资产原值(万元人民币) 固定资产净值(万元人民币) 资产总和(万元人民币) 存货(万元人民币) 企业名称 Type_Region Revenue_Log 是否自我供给
220 11164476478 20163.0918395453 11651.4309309683 28954.8221299891 1830.10798 江苏芯诺半导体科技有限公司 江苏省 4.5 0
221 11212932825 491.782727793788 284.181242218739 706.215173902172 44.63678 河南雷电光源技术研究院 河南省 2.8 0
222 11220388001 737.674091690682 426.271863328108 1059.32276085326 66.95517 新疆蚂蚁网约出租汽车服务有限公司 新疆维吾尔自治区 3.0 0
223 397847929 4794.881596 2770.767112 6885.597946 435.208605 厦门科塔电子有限公司 福建省 3.8 0
224 517717050 40080.29232 23160.77124 57556.53667 3637.89757 深圳市实锐泰科技有限公司 广东省 4.8 0
225 737770776 5962.865574 3445.697562 8562.858984 541.2209575 福建省南安市物资有限公司 福建省 3.9 0
226 872394725 25724.57103 15462.17311 55978.44597 7283.825438 北京凯德石英股份有限公司 北京市 4.7 0
227 2311581270 32088.82299 18542.82605 46080.5401 2912.549895 安徽亚格盛电子新材料股份有限公司 安徽省 4.7 0
228 2313209417 15122.31888 8738.573198 21716.1166 1372.580985 武汉拓材科技有限公司 湖北省 4.3 0
229 2347013470 23006.64933 17588.56669 81317.91108 12851.94016 江苏先锋精密科技股份有限公司 江苏省 4.9 0
230 2350418059 73931.33674 42721.91341 106167.6811 6710.395927 江苏纳沛斯半导体有限公司 江苏省 5.0 0
231 2352951203 29261.0723 16908.78391 42019.80285 2655.88841 中山市江波龙电子有限公司 广东省 4.6 0
232 2359644835 232613.2302 134417.7276 334039.7773 21113.19694 广东气派科技有限公司 广东省 5.5 0
233 2388955897 2458.913639 1420.906211 3531.07587 223.1839 上海鑫匀源科技有限公司 上海市 3.5 0
234 2977767486 9790.429458 5657.491099 14059.35884 1748.344959 深圳市三联盛科技股份有限公司 广东省 4.1 0
235 3031009366 43768.66277 25292.13056 62853.15048 3972.67342 天通瑞宏科技有限公司 浙江省 4.8 0
236 3089095447 245.8913639 142.0906211 353.107587 22.31839 常州金坛沸腾商贸有限公司 江苏省 2.5 0
237 3100891962 14801.53049 8553.202633 21255.45458 3889.830944 北京中超伟业信息安全技术股份有限公司 北京市 4.3 0
238 3151203276 341884.6624 201430.569 477385.6924 28574.49401 合肥颀中科技股份有限公司 安徽省 5.7 0
239 3188352290 13032.24229 7530.802919 18714.70211 1182.87467 湖北深紫科技有限公司 湖北省 4.3 0
240 3288105727 6639.066825 3836.44677 9533.904848 602.59653 奥趋光电技术(杭州)有限公司 浙江省 4.0 0
241 3462551351 5901.392734 3410.174907 8474.582087 535.64136 无锡博加电子新材料有限公司 江苏省 3.9 0

View File

@ -42,6 +42,7 @@ class MyModel(Model):
- seed (int): 随机种子的值用于确保实验的可重复性
"""
# 仿真参数
self.agent_map = None
self.firm_prod_labels_dict = None
self.firm_relationship_cache = None
self.firm_product_cache = None
@ -85,12 +86,22 @@ class MyModel(Model):
self.company_agents = [] # 初始化公司代理列表
# 初始化模型的网络和代理
# 检查缓存是否存在
cache_file = "firm_network.pkl"
if os.path.exists(cache_file):
# 从缓存加载 firm_network
with open(cache_file, 'rb') as f:
self.firm_network = pickle.load(f)
print("Loaded firm network from cache.")
else:
# 执行完整的初始化流程
self.initialize_product_network(params)
self.initialize_firm_network()
self.build_firm_prod_labels_dict()
self.initialize_firm_product_network()
self.add_edges_to_firm_network()
self.connect_unconnected_nodes()
self.initialize_product_network(params) # 初始化产品网络。
self.initialize_firm_network() # 初始化企业网络。
self.build_firm_prod_labels_dict() # 构建企业与产品的映射关系字典
self.initialize_firm_product_network() # 初始化企业与产品的网络。
self.add_edges_to_firm_network() # 添加企业之间的边。
self.connect_unconnected_nodes() # 连接未连接的节点。
self.resource_integration()
self.j_comp_consumed_produced()
self.initialize_agents() # 初始化代理。
@ -402,8 +413,12 @@ class MyModel(Model):
if current_node != -1 and succ_node != -1:
self.firm_prod_network.add_edge(current_node, succ_node)
# 保存网络数据到样本
self.firm_prod_network = self.firm_prod_network # 使用 networkx 图对象表示的企业与产品关系网络
# 保存构建完成的 firm_network 到缓存
cache_file = "firm_network.pkl"
os.makedirs("cache", exist_ok=True)
with open(cache_file, 'wb') as f:
pickle.dump(self.firm_network, f)
# print("Firm network has been saved to cache.")
def initialize_agents(self):
"""
@ -442,7 +457,8 @@ class MyModel(Model):
C = self.firm_resource_C.loc[int(ag_node)]
except KeyError:
R, P, C = [], {}, [] # 如果任何资源不存在,返回空列表
# 在模型初始化时,构建 unique_id -> agent 的快速映射字典
self.agent_map = {agent.unique_id: agent for agent in self.company_agents}
# 创建企业代理
firm_agent = FirmAgent(
unique_id=ag_node,
@ -466,14 +482,25 @@ class MyModel(Model):
- 更新公司与产品的生产状态为干扰状态
"""
# 构建公司与受干扰产品的映射字典
disruption_mapping = {
firm: [
product for product in self.product_agents
if product.unique_id in lst_product
disruption_mapping = {}
for firm_code, lst_product_indices in self.dct_lst_init_disrupt_firm_prod.items():
# 查找企业对象
firm = next((f for f in self.company_agents if f.unique_id == firm_code), None)
if not firm:
print(f"Warning: Firm {firm_code} not found. Skipping.")
continue
# 查找有效的产品代理
valid_products = [
product for product in self.product_agents if product.unique_id in lst_product_indices
]
for firm_code, lst_product in self.dct_lst_init_disrupt_firm_prod.items()
if (firm := next((f for f in self.company_agents if f.unique_id == firm_code), None))
}
if not valid_products:
print(f"Warning: No valid products found for Firm {firm_code}. Skipping.")
continue
# 更新映射
disruption_mapping[firm] = valid_products
# 更新干扰字典
self.dct_lst_init_disrupt_firm_prod = disruption_mapping
@ -481,10 +508,13 @@ class MyModel(Model):
# 设置初始干扰状态
for firm, disrupted_products in disruption_mapping.items():
for product in disrupted_products:
# 确保产品在公司的生产状态中
# 检查产品是否在企业的生产状态中
if product not in firm.dct_prod_up_prod_stat:
raise ValueError(
f"Product {product.unique_id} not found in firm {firm.unique_id}'s production status.")
print(
f"Warning: Product {product.unique_id} not found in firm "
f"{firm.unique_id}'s production status. Skipping."
)
continue
# 更新产品状态为干扰状态,并记录干扰时间
firm.dct_prod_up_prod_stat[product]['p_stat'].append(('D', self.t))
@ -663,71 +693,71 @@ class MyModel(Model):
"""
判断并处理资源的采购
"""
# 存储需要采购资源的企业及其需求
purchase_material_firms = {}
material_list = []
list_seek_material_firm = [] # 每一个收到请求的企业
# 遍历所有企业,检查资源需求
for firm in self.company_agents:
# 如果 firm.R 为 None跳过处理
if not firm.R:
# print(f"Firm {firm.unique_id} has no resource data.")
if not firm.R: # 跳过没有资源的企业
continue
# 处理资源需求
for sub_list in firm.R:
if sub_list[1] <= firm.s_r:
required_material_quantity = firm.S_r - sub_list[1]
material_list.append([sub_list[0], required_material_quantity])
purchase_material_firms[firm] = material_list
# 寻源并发送资源请求
for material_firm_key, sub_list_values in purchase_material_firms.items():
for mater_list in sub_list_values:
result = material_firm_key.seek_material_supply(mater_list[0])
if result != -1:
list_seek_material_firm.append(result)
# 遍历资源列表,检查哪些资源需要补货
for resource_id, resource_quantity in firm.R:
if resource_quantity <= firm.s_r: # 如果资源低于阈值,记录需求
required_quantity = firm.S_r - resource_quantity
if firm not in purchase_material_firms:
purchase_material_firms[firm] = []
purchase_material_firms[firm].append((resource_id, required_quantity))
if list_seek_material_firm:
for seek_material_firm in list_seek_material_firm:
seek_material_firm.handle_material_request(mater_list)
for R_list in firm.R:
R_list[1] = firm.S_r
# 寻找供应商并处理补货
for firm, material_requests in purchase_material_firms.items():
for resource_id, required_quantity in material_requests:
# 寻找供应商
supplier = firm.seek_material_supply(resource_id)
if supplier != -1: # 如果找到供应商
# 供应商处理资源请求
supplier.handle_material_request([resource_id, required_quantity])
# 更新当前企业的资源数量
for resource in firm.R:
if resource[0] == resource_id:
resource[1] = firm.S_r
def _handle_machinery_purchase(self):
"""
判断并处理设备的采购
"""
# 存储需要采购设备的企业及其需求
purchase_machinery_firms = {}
machinery_list = []
list_seek_machinery_firm = [] # 每一个收到请求的企业
# 遍历所有企业,检查设备需求
for firm in self.company_agents:
# 处理设备需求
# 如果 firm.C 为 None跳过处理
if not firm.C:
# print(f"Firm {firm.unique_id} has no equipment to maintain or purchase.")
if not firm.C: # 跳过没有设备的企业
continue
for sub_list in firm.C:
sub_list[2] -= firm.x # 减少设备残值
if sub_list[2] <= 0: # 残值小于等于 0
sub_list[1] -= 1
required_machinery_quantity = 1
machinery_list.append([sub_list[0], required_machinery_quantity])
purchase_machinery_firms[firm] = machinery_list
# 检查设备残值,记录需要补充的设备
for equipment in firm.C:
equipment_id, equipment_quantity, equipment_salvage = equipment
equipment_salvage -= firm.x # 减少设备残值
if equipment_salvage <= 0: # 如果残值小于等于 0
equipment_quantity -= 1
required_quantity = 1 # 需要补充的设备数量
if firm not in purchase_machinery_firms:
purchase_machinery_firms[firm] = []
purchase_machinery_firms[firm].append((equipment_id, required_quantity))
# 寻源并发送设备请求
for machinery_firm, sub_list in purchase_machinery_firms.items():
for machi_list in sub_list:
result = machinery_firm.seek_machinery_supply(machi_list[0])
if result != -1:
list_seek_machinery_firm.append(result)
if list_seek_machinery_firm:
for seek_machinery_firm in list_seek_machinery_firm:
seek_machinery_firm.handle_machinery_request(machi_list)
for C_list, C0_list in zip(firm.C, firm.C0):
C_list[1] = C0_list[1] # 恢复初始值
C_list[2] = C0_list[2]
# 寻找供应商并处理设备补充
for firm, machinery_requests in purchase_machinery_firms.items():
for equipment_id, required_quantity in machinery_requests:
# 寻找供应商
supplier = firm.seek_machinery_supply(equipment_id)
if supplier != -1: # 如果找到供应商
# 供应商处理设备请求
supplier.handle_machinery_request([equipment_id, required_quantity])
# 恢复企业的设备数量和残值
for equipment, initial_equipment in zip(firm.C, firm.C0):
if equipment[0] == equipment_id:
equipment[1] = initial_equipment[1] # 恢复数量
equipment[2] = initial_equipment[2] # 恢复残值
def _consume_resources_and_produce(self):
"""
@ -736,36 +766,52 @@ class MyModel(Model):
k = 0.6 # 资源消耗比例
production_increase_ratio = 1.6 # 产品生产比例
# 遍历每个企业
for firm in self.company_agents:
# 计算行业的资源消耗
consumed_resources = {}
for industry in firm.indus_i:
# 计算当前行业的资源消耗量
consumed_quantity = sum(
product[1] * k # product[1] 是产品的数量
for product in firm.P
if product[0] == industry.unique_id # product[0] 是产品的 ID
)
# 将计算的消耗量记录到字典中
consumed_resources[industry] = consumed_quantity
# 计算资源消耗
consumed_resources = self._calculate_consumed_resources(firm, k)
# 消耗资源
for resource in firm.R:
resource_id, resource_quantity = resource[0], resource[1]
for industry, consumed_quantity in consumed_resources.items():
if resource_id == industry.unique_id: # 判断资源是否属于该行业
resource[1] = max(0, resource_quantity - consumed_quantity)
self._consume_resources(firm, consumed_resources)
# 生产产品
for product in firm.P:
product_id, product_quantity = product[0], product[1]
product[1] = product_quantity * production_increase_ratio
self._produce_products(firm, production_increase_ratio)
# 刷新资源和设备状态
firm.refresh_R()
firm.refresh_C()
firm.refresh_P()
def _calculate_consumed_resources(self, firm, k):
"""
计算企业的资源消耗量
"""
consumed_resources = {}
for industry in firm.indus_i:
consumed_quantity = sum(
product[1] * k
for product in firm.P
if product[0] == industry.unique_id
)
consumed_resources[industry.unique_id] = consumed_quantity
return consumed_resources
def _consume_resources(self, firm, consumed_resources):
"""
消耗企业的资源
"""
for resource in firm.R:
resource_id, resource_quantity = resource[0], resource[1]
if resource_id in consumed_resources:
resource[1] = max(0, resource_quantity - consumed_resources[resource_id])
def _produce_products(self, firm, production_increase_ratio):
"""
生产企业的产品
"""
for product in firm.P:
product[1] *= production_increase_ratio
def end(self):
"""
结束模型运行并保存结果