修改了评价指标,新的输入案例

This commit is contained in:
Hgq 2025-12-09 16:24:20 +08:00
parent dcea3818f1
commit 3e6355be3d
2 changed files with 71 additions and 60 deletions

View File

@ -2,76 +2,88 @@
class OrderData: class OrderData:
"""订单数据类:存储物料需求、交货期、成本等信息""" """订单数据类:存储物料需求、交货期、成本等信息"""
def __init__(self): def __init__(self):
self.I = 5 # 物料种类数 self.I = 8 # 物料种类数
self.Q = [6000, 12000, 20000, 7500, 13500] # 各物料的需求数量(整数) self.Q = [6000, 12000, 22000, 7500, 13500, 16000, 8000, 14000] # 各物料的需求数量
self.Dd = 30 # 需求交货期(单位:时间,整数 self.Dd = 40 # 需求交货期(单位:时间
self.P0 = [45, 30, 30, 50, 40] # 风险企业的单位采购价(整数) self.P0 = [45, 30, 30, 50, 40, 45, 30, 30] # 风险企业的单位采购价
self.T0 = [5, 8, 6, 7, 9] # 风险企业的单位运输成本(整数) self.T0 = [5, 8, 6, 7, 9, 4, 6, 7] # 风险企业的单位运输成本
self.transport_speed = 10 # 运输速度(单位:距离/时间,整数 self.transport_speed = 10 # 运输速度(单位:距离/时间
class RiskEnterpriseData: class RiskEnterpriseData:
"""风险企业数据类:存储风险企业的产能、距离等信息""" """风险企业数据类:存储风险企业的产能、距离等信息"""
def __init__(self): def __init__(self):
self.I = 5 # 物料种类数(与订单一致) self.I = 8 # 物料种类数(与订单一致)
self.C0_i_min = [50, 100, 150, 80, 100] # 单物料的单位时间最小产能(整数) self.C0_i_min = [50, 100, 150, 80, 100, 150, 80, 100] # 单物料的单位时间最小产能
self.C0_total_max = 900 # 总产能上限(单位时间,整数 self.C0_total_max = 18000 # 总产能上限(单位时间
self.distance = 20 # 与需求点的距离(整数) self.distance = 30 # 与需求点的距离
class SupplierData: class SupplierData:
"""供应商数据类:存储各供应商的产能、价格、距离等信息""" """供应商数据类:存储各供应商的产能、价格、距离等信息"""
def __init__(self, I=5): def __init__(self, I=8):
self.I = I # 物料种类数 self.I = I # 物料种类数
self.supplier_count = 4 # 供应商数量 self.supplier_count = 6 # 供应商数量
self.names = ["S0", "S1", "S2", "S3"] # 供应商名称 self.names = ["S0", "S1", "S2", "S3", "S4", "S5"] # 供应商名称
# 能否生产某物料的矩阵supplier_count × I1=能生产0=不能 # 能否生产某物料的矩阵supplier_count × I1=能生产0=不能
self.can_produce = [ self.can_produce = [
[1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 0, 1, 1],
[1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 1, 0, 1],
[0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 0, 1, 0],
[0, 0, 1, 1, 1] [0, 1, 0, 1, 1, 1, 1, 1],
[1, 1, 0, 1, 0, 0, 1, 0],
[0, 0, 1, 0, 1, 0, 1, 1]
] ]
# 单物料单位时间最小产能supplier_count × I0表示不能生产该物料整数 # 单物料单位时间最小产能supplier_count × I0表示不能生产该物料
self.Cj_i_min = [ self.Cj_i_min = [
[30, 80, 100, 60, 80], [30, 80, 0, 60, 80, 0, 80, 90],
[60, 0, 180, 0, 120], [35, 0, 120, 0, 90, 110, 0, 110],
[0, 150, 0, 120, 0], [0, 70, 0, 70, 0, 0, 85, 0],
[0, 0, 170, 105, 115] [0, 75, 0, 75, 85, 95, 90, 100],
[25, 60, 0, 80, 0, 0, 90, 0],
[0, 0, 150, 0, 100, 0, 100, 95]
] ]
# 供应商单位时间的最大总产能supplier_count整数 # 供应商单位时间的最大总产能supplier_count
self.Cj_total_max = [700, 800, 600, 850] self.Cj_total_max = [1100, 950, 850, 1350, 750, 1000]
# 最小起订量supplier_count × I,整数 # 最小起订量supplier_count × I
self.MinOrder = [ self.MinOrder = [
[800, 1500, 3000, 800, 1500], [300, 800, 0, 600, 800, 0, 800, 900],
[1000, 0, 3500, 0, 1800], [350, 0, 1200, 0, 900, 1100, 0, 1100],
[0, 1700, 0, 1000, 0], [0, 700, 0, 700, 0, 0, 850, 0],
[0, 0, 2500, 500, 1000] [0, 750, 0, 750, 850, 950, 900, 1000],
[250, 600, 0, 800, 0, 0, 900, 0],
[0, 0, 1500, 0, 1000, 0, 1000, 950]
] ]
# 最大供应量supplier_count × I整数 # 最大供应量supplier_count × I
self.MaxOrder = [ self.MaxOrder = [
[5000, 10000, 18000, 6500, 11000], [3000, 8000, 0, 6000, 8000, 0, 8000, 9000],
[8000, 0, 25000, 0, 15000], [3500, 0, 12000, 0, 9000, 11000, 0, 11000],
[0, 8000, 0, 6000, 0], [0, 7000, 0, 7000, 0, 0, 8500, 0],
[0, 0, 20000, 7500, 13500] [0, 7500, 0, 7500, 8500, 9500, 9000, 7000],
[3000, 6000, 0, 8000, 0, 0, 9000, 0],
[0, 0, 15000, 0, 8000, 0, 6500, 9500]
] ]
# 单位采购价格supplier_count × I整数 # 单位采购价格supplier_count × I
self.P_ij = [ self.P_ij = [
[50, 35, 28, 47, 38], [50, 32, 0, 60, 42, 0, 32, 33],
[43, 0, 28, 0, 36], [55, 0, 33, 0, 44, 48, 0, 36],
[0, 31, 0, 52, 0], [0, 33, 0, 62, 0, 0, 32, 0],
[0, 0, 32, 52, 43] [0, 36, 0, 59, 46, 40, 34, 40],
[53, 37, 0, 58, 0, 0, 39, 0],
[0, 0, 32, 0, 43, 0, 35, 38]
] ]
# 单位运输成本supplier_count × I整数 # 单位运输成本supplier_count × I
self.T_ij = [ self.T_ij = [
[6, 9, 8, 9, 12], [8, 8, 0, 5, 15, 0, 8, 11],
[4, 0, 5, 0, 15], [13, 0, 8, 0, 13, 8, 0, 13],
[0, 10, 0, 7, 0], [0, 10, 0, 9, 0, 0, 11, 0],
[0, 0, 8, 9, 11] [0, 6, 0, 8, 11, 7, 9, 10],
[4, 12, 0, 12, 0, 0, 12, 0],
[0, 0, 10, 0, 12, 0, 8, 16]
] ]
# 供应商与需求点的距离supplier_count整数 # 供应商与需求点的距离supplier_count
self.distance = [60, 50, 70, 40] self.distance = [50, 40, 60, 30, 60, 80, 50, 60]
class Config: class Config:
"""算法参数配置类存储NSGA-II的各类参数""" """算法参数配置类存储NSGA-II的各类参数"""
def __init__(self): def __init__(self):
# 种群参数 # 种群参数
self.pop_size = 100 # 种群大小 self.pop_size = 200 # 种群大小
self.N1_ratio = 0.2 # 优先成本的种群比例 self.N1_ratio = 0.2 # 优先成本的种群比例
self.N2_ratio = 0.2 # 优先延期的种群比例 self.N2_ratio = 0.2 # 优先延期的种群比例
self.N3_ratio = 0.3 # 强制风险企业的种群比例 self.N3_ratio = 0.3 # 强制风险企业的种群比例
@ -87,26 +99,23 @@ class Config:
self.early_stop_threshold = 0.15 # 目标值变化阈值 self.early_stop_threshold = 0.15 # 目标值变化阈值
# 目标函数数量 # 目标函数数量
self.objective_num = 2 # 双目标(成本+延期) self.objective_num = 2 # 双目标(成本+延期)
self.duplicate_threshold = 0.05 # 重复解保留数量 self.duplicate_threshold = 0.01 # 重复解保留数量
self.print_top_n = 10 # 打印前N个最优解 self.print_top_n = 10 # 打印前N个最优解
class DataStructures: class DataStructures:
"""数据结构工具类:提供评价指标计算等功能""" """数据结构工具类:提供评价指标计算等功能"""
@staticmethod @staticmethod
def calculate_evaluation_index(objectives, optimal_cost, optimal_tardiness): def calculate_evaluation_index(objectives, optimal_cost, optimal_tardiness, max_cost, max_tardiness):
""" """
计算评价指标 计算评价指标
:param objectives: 解的目标值 (成本, 延期) : objectives: 解的目标值 (成本, 延期)
:param optimal_cost: 最优成本值 : optimal_cost: 最优成本值
:param optimal_tardiness: 最优延期值 : optimal_tardiness: 最优延期值
: max_cost: 最大成本值
: max_tardiness: 最大延期值
:return: 评价指标值 :return: 评价指标值
""" """
cost, tardiness = objectives cost, tardiness = objectives
# 避免除以零成本最优值为0时的保护 cost_ratio = cost/(max_cost - optimal_cost)
if optimal_cost == 0: tardiness_ratio = tardiness/( max_tardiness - optimal_tardiness)
cost_ratio = cost
else:
cost_ratio = 2*(cost / optimal_cost)
# 延期处理(+1避免除以零
tardiness_ratio = 800*((tardiness + 1) / (optimal_tardiness + 1))
return cost_ratio + tardiness_ratio return cost_ratio + tardiness_ratio

View File

@ -186,12 +186,14 @@ def main():
# 找出各目标的最优值 # 找出各目标的最优值
optimal_cost = min(obj[0] for obj in unique_front_objs) optimal_cost = min(obj[0] for obj in unique_front_objs)
optimal_tardiness = min(obj[1] for obj in unique_front_objs) optimal_tardiness = min(obj[1] for obj in unique_front_objs)
max_cost = max(obj[0] for obj in unique_front_objs)
max_tardiness = max(obj[1] for obj in unique_front_objs)
# 计算每个解的评价指标 # 计算每个解的评价指标
evaluated_solutions = [] evaluated_solutions = []
for sol, obj in zip(unique_front, unique_front_objs): for sol, obj in zip(unique_front, unique_front_objs):
index = DataStructures.calculate_evaluation_index( index = DataStructures.calculate_evaluation_index(
obj, optimal_cost, optimal_tardiness obj, optimal_cost, optimal_tardiness, max_cost, max_tardiness
) )
evaluated_solutions.append((sol, obj, index)) evaluated_solutions.append((sol, obj, index))