Asoka.Wang 21f044712c 1
2025-08-27 18:39:19 +08:00

194 lines
7.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using NPP.SmartSchedue.Api.Contracts.Domain.Work;
namespace NPP.SmartSchedue.Api.Contracts.Services.Integration.Input
{
/// <summary>
/// 全局优化分配输入
/// </summary>
public class GlobalAllocationInput
{
/// <summary>
/// 待分配的任务ID列表当Tasks为空时使用
/// </summary>
public List<long> TaskIds { get; set; } = new();
/// <summary>
/// 待分配的任务对象列表(性能优化:避免重复数据库查询)
/// </summary>
public List<WorkOrderEntity> Tasks { get; set; } = new();
/// <summary>
/// 排除的人员ID列表
/// </summary>
public List<long> ExcludedPersonnelIds { get; set; } = new();
/// <summary>
/// 全局优化配置
/// </summary>
public GlobalOptimizationConfig OptimizationConfig { get; set; } = new();
}
/// <summary>
/// 全局优化配置
/// </summary>
public class GlobalOptimizationConfig
{
/// <summary>
/// 遗传算法种群大小
/// 生产环境推荐:小规模(20任务)使用50-100中规模(50-100任务)使用80-150
/// </summary>
[Range(50, 500, ErrorMessage = "种群大小应在50-500之间")]
public int PopulationSize { get; set; } = 100; // 从200降低到100适合100任务规模
/// <summary>
/// 最大迭代代数
/// 生产环境推荐:由于预筛选机制,可以适当减少迭代次数
/// </summary>
[Range(20, 200, ErrorMessage = "迭代代数应在20-200之间")]
public int MaxGenerations { get; set; } = 60; // 从100降低到60在质量和性能间平衡
/// <summary>
/// 最大执行时间(秒)
/// 生产环境为100任务增加更宽裕的时间限制
/// </summary>
[Range(5, 300, ErrorMessage = "执行时间应在5-300秒之间")]
public int MaxExecutionTimeSeconds { get; set; } = 120; // 从30秒增加到120秒适应100任务的复杂度
/// <summary>
/// 基尼系数公平性权重0-1
/// </summary>
[Range(0.0, 1.0, ErrorMessage = "公平性权重应在0-1之间")]
public double FairnessWeight { get; set; } = 0.3;
/// <summary>
/// 约束满足权重0-1
/// </summary>
[Range(0.0, 1.0, ErrorMessage = "约束权重应在0-1之间")]
public double ConstraintWeight { get; set; } = 0.6;
/// <summary>
/// 资质匹配权重0-1
/// </summary>
[Range(0.0, 1.0, ErrorMessage = "资质权重应在0-1之间")]
public double QualificationWeight { get; set; } = 0.1;
/// <summary>
/// 是否启用智能协商
/// </summary>
public bool EnableIntelligentNegotiation { get; set; } = true;
/// <summary>
/// 收敛阈值
/// </summary>
[Range(0.001, 0.1, ErrorMessage = "收敛阈值应在0.001-0.1之间")]
public double ConvergenceThreshold { get; set; } = 0.001;
/// <summary>
/// 创建适合生产环境的优化配置
/// 适用7天100任务30人员的大规模调度场景
/// </summary>
/// <param name="taskCount">任务数量</param>
/// <param name="personnelCount">人员数量</param>
/// <returns>优化后的配置</returns>
public static GlobalOptimizationConfig CreateProductionConfig(int taskCount, int personnelCount)
{
var config = new GlobalOptimizationConfig();
// 根据任务规模动态调整参数
if (taskCount <= 20)
{
// 小规模超快速模式针对6个任务优化
if (taskCount <= 6)
{
config.PopulationSize = 15; // 从50降到15减少计算量
config.MaxGenerations = 10; // 从30降到10快速收敛
config.MaxExecutionTimeSeconds = 15; // 从60降到15秒
}
else
{
config.PopulationSize = 30; // 7-20任务使用中等参数
config.MaxGenerations = 20;
config.MaxExecutionTimeSeconds = 30;
}
}
else if (taskCount <= 50)
{
// 中规模:平衡模式
config.PopulationSize = 80;
config.MaxGenerations = 50;
config.MaxExecutionTimeSeconds = 90;
}
else if (taskCount <= 100)
{
// 大规模:高质量模式(适合生产环境)
config.PopulationSize = 100;
config.MaxGenerations = 60;
config.MaxExecutionTimeSeconds = 120;
}
else
{
// 超大规模:保守模式
config.PopulationSize = Math.Min(150, taskCount + 50);
config.MaxGenerations = 80;
config.MaxExecutionTimeSeconds = 180;
}
// 根据人员数量调整权重
if (personnelCount < 20)
{
// 人员较少,提高公平性权重
config.FairnessWeight = 0.4;
config.ConstraintWeight = 0.5;
config.QualificationWeight = 0.1;
}
else
{
// 人员充足,保持默认权重
config.FairnessWeight = 0.3;
config.ConstraintWeight = 0.6;
config.QualificationWeight = 0.1;
}
return config;
}
/// <summary>
/// 创建快速测试配置(用于开发和测试环境)
/// </summary>
/// <returns>快速测试配置</returns>
public static GlobalOptimizationConfig CreateFastTestConfig()
{
return new GlobalOptimizationConfig
{
PopulationSize = 30,
MaxGenerations = 20,
MaxExecutionTimeSeconds = 30,
FairnessWeight = 0.2,
ConstraintWeight = 0.7,
QualificationWeight = 0.1,
ConvergenceThreshold = 0.01 // 更宽松的收敛条件以加速测试
};
}
/// <summary>
/// 创建高质量配置(用于关键业务场景)
/// </summary>
/// <returns>高质量配置</returns>
public static GlobalOptimizationConfig CreateHighQualityConfig()
{
return new GlobalOptimizationConfig
{
PopulationSize = 200,
MaxGenerations = 100,
MaxExecutionTimeSeconds = 300,
FairnessWeight = 0.35,
ConstraintWeight = 0.55,
QualificationWeight = 0.1,
ConvergenceThreshold = 0.0005
};
}
}
}