From 28d6996c586e8e4a4a99c276e4f1d48d19ebd59a Mon Sep 17 00:00:00 2001 From: Tian jianyong <11429339@qq.com> Date: Fri, 1 Nov 2024 16:58:08 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=99=E6=95=88=E8=83=BD=E8=AF=84=E4=BC=B0?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E5=90=84=E6=96=87=E4=BB=B6=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Program.cs | 208 +-------------------- src/Evaluation/AHPEvaluationModel.cs | 36 +++- src/Evaluation/BayesianNetworkModel.cs | 45 +++-- src/Evaluation/DirectWeightModel.cs | 26 ++- src/Evaluation/EvaluationExample.cs | 84 +++++---- src/Evaluation/EvaluationModelFactory.cs | 33 +++- src/Evaluation/FuzzyEvaluationModel.cs | 43 ++++- src/Evaluation/GreyRelationModel.cs | 36 +++- src/Evaluation/IEvaluationModel.cs | 5 +- src/Simulation/SimulationExample.cs | 222 +++++++++++++++++++++++ src/Simulation/SimulationManager.cs | 4 +- 11 files changed, 462 insertions(+), 280 deletions(-) create mode 100644 src/Simulation/SimulationExample.cs diff --git a/Program.cs b/Program.cs index 0542cfc..577c0f6 100644 --- a/Program.cs +++ b/Program.cs @@ -1,9 +1,4 @@ -using System; -using System.Threading; using ActiveProtect.Simulation; -using ActiveProtect.Models; -using ActiveProtect.Utility; -using System.Collections.Generic; using ActiveProtect.Evaluation; namespace ActiveProtect @@ -15,207 +10,10 @@ namespace ActiveProtect { static void Main(string[] args) { - // 初始化坦克信息 - TankInfo tankinfo = new() - { - xp = 100, - yp = 0, - zp = 100 - }; - - // 创建仿真配置 - var config = new SimulationConfig - { - // 配置坦克 - TankConfigs = new List - { - new(tankinfo) - { - Id = "Tank_1", - InitialOrientation = new Orientation(Math.PI/4, 0, 0), - InitialSpeed = 20, - MaxSpeed = 25, - MaxArmor = 100, - InfraredRadiationIntensity = 150, - RadarCrossSection = 10, - MillimeterWaveRadiationTemperature = 100, - HasLaserWarner = false, - HasLaserJammer = true - } - }, - // 配置激光指示器 - LaserDesignatorConfig = new LaserDesignatorConfig - { - Id = "LD_1", - InitialPosition = new Vector3D(2000, 150, 100), - LaserPower = 1e6, - LaserDivergenceAngle = 0.01 - }, - // 配置激光告警器 - LaserWarnerConfig = new LaserWarnerConfig - { - Id = "LW_1", - AlarmDuration = 5.0 - }, - // 配置激光干扰器 - LaserJammerConfig = new LaserJammerConfig - { - Id = "LJ_1", - MaxJammingCooldown = 5.0, - MaxJammingPower = 10000.0, - InitialJammingPower = 4000.0, - PowerIncreaseRate = 2000.0 - }, - // 配置激光驾束仪 - LaserBeamRiderConfig = new LaserBeamRiderConfig - { - Id = "LBR_1", - InitialPosition = new Vector3D(2000, 10, 100), - LaserPower = 1000, - ControlFieldDiameter = 6 - }, - // 配置导弹 - MissileConfigs = new List - { - // 激光半主动制导导弹配置 - new() { - Id = "LSGM_1", - InitialPosition = new Vector3D(2000, 100, 100), - InitialOrientation = new Orientation(Math.PI, -0.1, 0), - InitialSpeed = 700, - MaxSpeed = 800, - MaxFlightTime = 10, - MaxFlightDistance = 5000, - LaunchAcceleration = 50, - MaxEngineBurnTime = 10, - MaxAcceleration = 400, - ProportionalNavigationCoefficient = 4, - ExplosionRadius = 5, - HitProbability = 0.9, - Mass = 50, - Type = MissileType.LaserSemiActiveGuidance - }, - // 激光驾束制导导弹配置 - new() { - Id = "LBRM_1", - InitialPosition = new Vector3D(2000, 10, 100), - InitialOrientation = new Orientation(Math.PI, 0.0, 0.0), - InitialSpeed = 300, - MaxSpeed = 400, - MaxFlightTime = 10, - MaxFlightDistance = 3000, - LaunchAcceleration = 50, - MaxEngineBurnTime = 10, - MaxAcceleration = 400, - ProportionalNavigationCoefficient = 3, - ExplosionRadius = 5, - HitProbability = 0.9, - Mass = 50, - Type = MissileType.LaserBeamRiderGuidance - }, - // 末敏弹配置 - new() { - Id = "TSM_1", - InitialPosition = new Vector3D(3000, 0, 100), // 发射位置 - InitialOrientation = new Orientation(Math.PI, 0, 0), // 使用计算得到的发射角度 - InitialSpeed = 800, - MaxSpeed = 1000, - MaxFlightTime = 30, - MaxFlightDistance = 5000, - LaunchAcceleration = 0, - MaxEngineBurnTime = 0, - MaxAcceleration = 10, - ProportionalNavigationCoefficient = 3, - ExplosionRadius = 5, - HitProbability = 0.9, - Mass = 50, - Type = MissileType.TerminalSensitiveMissile - }, - // 红外指令制导导弹配置 - new() { - Id = "ICGM_1", - InitialPosition = new Vector3D(2000, 10, 100), - InitialOrientation = new Orientation(Math.PI, 0.0, 0), - InitialSpeed = 300, - MaxSpeed = 400, - MaxFlightTime = 60, - MaxFlightDistance = 5000, - LaunchAcceleration = 50, - MaxEngineBurnTime = 10, - MaxAcceleration = 100, - ExplosionRadius = 5, - HitProbability = 0.9, - ProportionalNavigationCoefficient = 3, - Mass = 50, - Type = MissileType.InfraredCommandGuidance - }, - // 红外成像末制导导弹配置 - new() { - Id = "ITGM_1", - InitialPosition = new Vector3D(2000, 20, 100), - InitialOrientation = new Orientation(Math.PI, 0.0, 0), - InitialSpeed = 300, - MaxSpeed = 400, - MaxFlightTime = 60, - MaxFlightDistance = 5000, - LaunchAcceleration = 50, - MaxEngineBurnTime = 10, - MaxAcceleration = 50, - ExplosionRadius = 5, - HitProbability = 0.9, - ProportionalNavigationCoefficient = 3, - Mass = 50, - Type = MissileType.InfraredImagingTerminalGuidance - }, - // 毫米波导引头制导导弹配置 - new() { - Id = "MMWG_1", - InitialPosition = new Vector3D(2000, 200, 100), - InitialOrientation = new Orientation(Math.PI, 0.0, 0), - InitialSpeed = 300, - MaxSpeed = 400, - MaxFlightTime = 60, - MaxFlightDistance = 5000, - LaunchAcceleration = 50, - MaxEngineBurnTime = 10, - MaxAcceleration = 50, - ExplosionRadius = 5, - HitProbability = 0.9, - ProportionalNavigationCoefficient = 3, - Mass = 50, - Type = MissileType.MillimeterWaveTerminalGuidance - } - }, - // 配置红外测角仪 - InfraredTrackerConfig = new InfraredTrackerConfig - { - Id = "IT_1", - InitialPosition = new Vector3D(2000, 0, 100), - InitialOrientation = new Orientation(Math.PI, 0, 0), - MaxTrackingRange = 10000, // 10公里 - FieldOfView = Math.PI / 4, // 45度 - AngleMeasurementAccuracy = 0.0005, // 0.0005弧度 (约0.029度) - UpdateFrequency = 100 // 100赫兹 - }, - SimulationTimeStep = 0.01 // 仿真时间步长, 激光驾束制导导弹需要更小的步长,< 0.025s - }; - - // 创建仿真管理器 - var simulationManager = new SimulationManager(config); - - // 运行仿真 - int maxIterations = 10000; - int iteration = 0; - while (!simulationManager.IsSimulationEnded && iteration < maxIterations) - { - simulationManager.Update(); - simulationManager.PrintStatus(); - Thread.Sleep(20); // 暂停100毫秒,使输出更易读 - iteration++; - } - - Console.WriteLine("仿真结束"); + // 运行仿真示例 + //SimulationExample.RunAllExamples(); + // 运行效能评估示例 EvaluationExample.RunAllExamples(); } } diff --git a/src/Evaluation/AHPEvaluationModel.cs b/src/Evaluation/AHPEvaluationModel.cs index 07423bd..4269695 100644 --- a/src/Evaluation/AHPEvaluationModel.cs +++ b/src/Evaluation/AHPEvaluationModel.cs @@ -5,22 +5,45 @@ using System.Linq; namespace ActiveProtect.Evaluation { /// - /// 层次分析法评估模型 - /// 适用于层次分明的指标体系 + /// 层次分析法(AHP)评估模型 + /// 适用于层次分明的指标体系,通过建立判断矩阵进行评估 /// + /// + /// 特点: + /// 1. 层次结构清晰 + /// 2. 考虑指标间的相对重要性 + /// 3. 结果具有较好的可解释性 + /// public class AHPEvaluationModel : IEvaluationModel { - private readonly float[,] judgmentMatrix; // 判断矩阵 - private readonly float[] weights; // 计算得到的权重 + /// + /// 判断矩阵,用于表示指标间的相对重要性 + /// 矩阵元素a[i,j]表示指标i相对于指标j的重要程度 + /// + private readonly float[,] judgmentMatrix; + + /// + /// 权重向量,由判断矩阵计算得到 + /// + private readonly float[] weights; public string ModelName => "层次分析法模型"; + /// + /// 构造函数 + /// + /// 判断矩阵,必须是正互反矩阵 public AHPEvaluationModel(float[,] judgmentMatrix) { this.judgmentMatrix = judgmentMatrix; this.weights = CalculateWeights(); } + /// + /// 执行AHP评估 + /// + /// 评估数据字典 + /// 评估结果,范围在[0,1]之间 public float Evaluate(Dictionary evaluationData) { float result = 0; @@ -35,6 +58,11 @@ namespace ActiveProtect.Evaluation return result; } + /// + /// 计算权重向量 + /// 使用几何平均法计算特征向量作为权重 + /// + /// 归一化后的权重向量 private float[] CalculateWeights() { int n = judgmentMatrix.GetLength(0); diff --git a/src/Evaluation/BayesianNetworkModel.cs b/src/Evaluation/BayesianNetworkModel.cs index 6eb79b1..50ac1db 100644 --- a/src/Evaluation/BayesianNetworkModel.cs +++ b/src/Evaluation/BayesianNetworkModel.cs @@ -8,13 +8,28 @@ namespace ActiveProtect.Evaluation /// 贝叶斯网络评估模型 /// 适用于具有不确定性和条件依赖关系的指标评估 /// + /// + /// 特点: + /// 1. 考虑指标间的概率依赖关系 + /// 2. 支持先验知识的引入 + /// 3. 可进行概率推理和预测 + /// public class BayesianNetworkModel : IEvaluationModel { - // 节点之间的条件概率表 + /// + /// 条件概率表,表示节点间的条件依赖关系 + /// key为子节点,value为父节点及对应的条件概率 + /// private readonly Dictionary> conditionalProbabilities; - // 先验概率 + + /// + /// 先验概率,表示节点的初始概率分布 + /// private readonly Dictionary priorProbabilities; - // 节点之间的依赖关系 + + /// + /// 节点依赖关系,key为子节点,value为父节点列表 + /// private readonly Dictionary> dependencies; public string ModelName => "贝叶斯网络模型"; @@ -22,7 +37,7 @@ namespace ActiveProtect.Evaluation /// /// 构造函数 /// - /// 先验概率 + /// 先验概率字典 /// 条件概率表 /// 节点依赖关系 public BayesianNetworkModel( @@ -35,24 +50,29 @@ namespace ActiveProtect.Evaluation this.dependencies = dependencies; } + /// + /// 执行贝叶斯网络评估 + /// + /// 评估数据字典 + /// 评估结果,范围在[0,1]之间 public float Evaluate(Dictionary evaluationData) { float result = 0; - - // 对每个评估指标进行概率计算 foreach (var node in evaluationData.Keys) { + // 计算每个节点的后验概率 float nodeProb = CalculateNodeProbability(node, evaluationData); result += nodeProb * evaluationData[node]; } - - // 归一化处理 return result / evaluationData.Count; } /// /// 计算节点的后验概率 /// + /// 目标节点 + /// 观测数据 + /// 节点的后验概率 private float CalculateNodeProbability(string node, Dictionary evidence) { // 如果是根节点,直接返回先验概率 @@ -62,8 +82,6 @@ namespace ActiveProtect.Evaluation } float probability = 1.0f; - - // 获取父节点 var parents = dependencies[node]; // 计算条件概率 @@ -83,8 +101,11 @@ namespace ActiveProtect.Evaluation } /// - /// 更新条件概率表 + /// 更新条件概率 /// + /// 子节点 + /// 父节点 + /// 新的条件概率值 public void UpdateConditionalProbability(string node, string parent, float probability) { if (!conditionalProbabilities.ContainsKey(node)) @@ -97,6 +118,8 @@ namespace ActiveProtect.Evaluation /// /// 添加新的依赖关系 /// + /// 子节点 + /// 父节点 public void AddDependency(string node, string parent) { if (!dependencies.ContainsKey(node)) diff --git a/src/Evaluation/DirectWeightModel.cs b/src/Evaluation/DirectWeightModel.cs index 08cc1b9..fe2dabf 100644 --- a/src/Evaluation/DirectWeightModel.cs +++ b/src/Evaluation/DirectWeightModel.cs @@ -6,14 +6,29 @@ namespace ActiveProtect.Evaluation { /// /// 直接赋权评估模型 - /// 最简单的评估方法,直接通过权重进行加权计算 + /// 通过直接指定权重进行加权计算的最基础评估方法 /// + /// + /// 适用场景: + /// 1. 评价指标较少(通常≤5个) + /// 2. 各指标重要性差异明显 + /// 3. 有充分的专家经验支持 + /// 4. 需要快速得出评估结果 + /// public class DirectWeightModel : IEvaluationModel { - private readonly float[] weights; // 权重向量 + /// + /// 权重向量,表示各个指标的相对重要性 + /// + private readonly float[] weights; public string ModelName => "直接赋权模型"; + /// + /// 构造函数 + /// + /// 权重数组,必须满足非负性和归一性 + /// 当权重无效时抛出异常 public DirectWeightModel(float[] weights) { this.weights = weights; @@ -24,6 +39,12 @@ namespace ActiveProtect.Evaluation } } + /// + /// 执行评估计算 + /// + /// 评估数据 + /// 加权平均后的评估结果 + /// 当数据数量与权重数量不匹配时抛出异常 public float Evaluate(Dictionary evaluationData) { if (evaluationData.Count != weights.Length) @@ -46,6 +67,7 @@ namespace ActiveProtect.Evaluation /// /// 验证权重的有效性 /// + /// 权重是否有效 private bool ValidateWeights() { // 检查权重是否都为非负数 diff --git a/src/Evaluation/EvaluationExample.cs b/src/Evaluation/EvaluationExample.cs index 8dd27a6..9c18e17 100644 --- a/src/Evaluation/EvaluationExample.cs +++ b/src/Evaluation/EvaluationExample.cs @@ -5,7 +5,16 @@ namespace ActiveProtect.Evaluation { /// /// 评估模型示例类 + /// 展示了各种评估模型的使用方法和效果 /// + /// + /// 包含的示例: + /// 1. 直接赋权模型 + /// 2. 模糊评价模型 + /// 3. AHP层次分析模型 + /// 4. 灰色关联分析模型 + /// 5. 贝叶斯网络模型 + /// public class EvaluationExample { /// @@ -16,34 +25,46 @@ namespace ActiveProtect.Evaluation // 准备评估数据 Dictionary evaluationData = new Dictionary { - { "告警响应时间", 0.85f }, - { "探测覆盖范围", 0.92f }, - { "系统稳定性", 0.78f } + { "告警响应时间", 0.85f }, // 值越大表示响应越快 + { "探测覆盖范围", 0.92f }, // 值越大表示覆盖范围越大 + { "系统稳定性", 0.78f } // 值越大表示系统越稳定 }; - // 1. 模糊评价模型示例 - DemoFuzzyEvaluation(evaluationData); - - // 2. AHP层次分析模型示例 - DemoAHPEvaluation(evaluationData); - - // 3. 灰色关联分析模型示例 - DemoGreyRelationEvaluation(evaluationData); - - // 4. 贝叶斯网络模型示例 - DemoBayesianEvaluation(evaluationData); - - // 5. 直接赋权模型示例 + // 依次运行各个评估模型示例 DemoDirectWeightEvaluation(evaluationData); + DemoFuzzyEvaluation(evaluationData); + DemoAHPEvaluation(evaluationData); + DemoGreyRelationEvaluation(evaluationData); + DemoBayesianEvaluation(evaluationData); + } + + /// + /// 直接赋权模型示例 + /// + /// 评估数据 + private static void DemoDirectWeightEvaluation(Dictionary evaluationData) + { + Console.WriteLine("\n=== 直接赋权模型示例 ==="); + + // 设置权重,权重之和必须为1 + float[] weights = new float[] { 0.4f, 0.3f, 0.3f }; + + var directModel = EvaluationModelFactory.CreateModel("direct", + new object[] { weights }); + + float result = directModel.Evaluate(evaluationData); + Console.WriteLine($"直接赋权评价结果: {result:F4}"); } /// /// 模糊评价模型示例 /// + /// 评估数据 private static void DemoFuzzyEvaluation(Dictionary evaluationData) { Console.WriteLine("\n=== 模糊评价模型示例 ==="); + // 设置隶属度矩阵,每行和为1 float[,] membershipMatrix = new float[3,3] { {0.5f, 0.3f, 0.2f}, // 告警响应时间的隶属度分布 {0.4f, 0.4f, 0.2f}, // 探测覆盖范围的隶属度分布 @@ -61,14 +82,16 @@ namespace ActiveProtect.Evaluation /// /// AHP层次分析模型示例 /// + /// 评估数据 private static void DemoAHPEvaluation(Dictionary evaluationData) { Console.WriteLine("\n=== AHP层次分析模型示例 ==="); + // 设置判断矩阵,表示指标间的相对重要性 float[,] judgmentMatrix = new float[3,3] { - {1.0f, 2.0f, 3.0f}, - {0.5f, 1.0f, 2.0f}, - {0.33f, 0.5f, 1.0f} + {1.0f, 2.0f, 3.0f}, // 告警响应时间相对于其他指标的重要性 + {0.5f, 1.0f, 2.0f}, // 探测覆盖范围相对于其他指标的重要性 + {0.33f, 0.5f, 1.0f} // 系统稳定性相对于其他指标的重要性 }; var ahpModel = EvaluationModelFactory.CreateModel("ahp", @@ -81,10 +104,12 @@ namespace ActiveProtect.Evaluation /// /// 灰色关联分析模型示例 /// + /// 评估数据 private static void DemoGreyRelationEvaluation(Dictionary evaluationData) { Console.WriteLine("\n=== 灰色关联分析模型示例 ==="); + // 设置参考序列,表示理想的指标值 float[] referenceSequence = new float[] { 0.9f, 0.95f, 0.85f }; var greyModel = EvaluationModelFactory.CreateModel("grey", @@ -97,10 +122,12 @@ namespace ActiveProtect.Evaluation /// /// 贝叶斯网络模型示例 /// + /// 评估数据 private static void DemoBayesianEvaluation(Dictionary evaluationData) { Console.WriteLine("\n=== 贝叶斯网络模型示例 ==="); + // 设置先验概率 var priorProbabilities = new Dictionary { { "告警响应时间", 0.8f }, @@ -108,6 +135,7 @@ namespace ActiveProtect.Evaluation { "系统稳定性", 0.9f } }; + // 设置条件概率表 var conditionalProbabilities = new Dictionary> { { "系统稳定性", new Dictionary @@ -118,6 +146,7 @@ namespace ActiveProtect.Evaluation } }; + // 设置节点依赖关系 var dependencies = new Dictionary> { { "系统稳定性", new List { "告警响应时间", "探测覆盖范围" } } @@ -129,22 +158,5 @@ namespace ActiveProtect.Evaluation float result = bayesianModel.Evaluate(evaluationData); Console.WriteLine($"贝叶斯网络评价结果: {result:F4}"); } - - /// - /// 直接赋权模型示例 - /// - private static void DemoDirectWeightEvaluation(Dictionary evaluationData) - { - Console.WriteLine("\n=== 直接赋权模型示例 ==="); - - // 设置权重 - float[] weights = new float[] { 0.4f, 0.3f, 0.3f }; - - var directModel = EvaluationModelFactory.CreateModel("direct", - new object[] { weights }); - - float result = directModel.Evaluate(evaluationData); - Console.WriteLine($"直接赋权评价结果: {result:F4}"); - } } } \ No newline at end of file diff --git a/src/Evaluation/EvaluationModelFactory.cs b/src/Evaluation/EvaluationModelFactory.cs index edeb41a..8d65a37 100644 --- a/src/Evaluation/EvaluationModelFactory.cs +++ b/src/Evaluation/EvaluationModelFactory.cs @@ -5,40 +5,55 @@ namespace ActiveProtect.Evaluation { /// /// 评估模型工厂类 - /// 用于创建和管理不同的评估模型 + /// 用于创建和管理不同类型的评估模型 /// + /// + /// 支持的模型类型: + /// - direct: 直接赋权模型 + /// - fuzzy: 模糊评价模型 + /// - ahp: 层次分析模型 + /// - grey: 灰色关联分析模型 + /// - bayesian: 贝叶斯网络模型 + /// public class EvaluationModelFactory { + /// + /// 创建评估模型实例 + /// + /// 模型类型名称 + /// 模型参数数组 + /// 评估模型实例 + /// 当模型类型不支持时抛出异常 public static IEvaluationModel CreateModel(string modelType, object[] parameters) { switch (modelType.ToLower()) { case "fuzzy": return new FuzzyEvaluationModel( - (float[,])parameters[0], - (float[])parameters[1] + (float[,])parameters[0], // 隶属度矩阵 + (float[])parameters[1] // 权重向量 ); case "ahp": return new AHPEvaluationModel( - (float[,])parameters[0] + (float[,])parameters[0] // 判断矩阵 ); case "grey": return new GreyRelationModel( - (float[])parameters[0] + (float[])parameters[0] // 参考序列 ); case "bayesian": return new BayesianNetworkModel( - (Dictionary)parameters[0], - (Dictionary>)parameters[1], - (Dictionary>)parameters[2] + (Dictionary)parameters[0], // 先验概率 + (Dictionary>)parameters[1], // 条件概率表 + (Dictionary>)parameters[2] // 依赖关系 ); case "direct": return new DirectWeightModel( - (float[])parameters[0] + (float[])parameters[0] // 权重向量 ); default: diff --git a/src/Evaluation/FuzzyEvaluationModel.cs b/src/Evaluation/FuzzyEvaluationModel.cs index dabaa6a..0b5fc40 100644 --- a/src/Evaluation/FuzzyEvaluationModel.cs +++ b/src/Evaluation/FuzzyEvaluationModel.cs @@ -6,15 +6,36 @@ namespace ActiveProtect.Evaluation { /// /// 模糊评价模型 - /// 适用于不确定性强的指标评估 + /// 适用于不确定性强的指标评估,通过隶属度矩阵进行综合评价 /// + /// + /// 特点: + /// 1. 能够处理模糊性和不确定性 + /// 2. 支持多指标综合评价 + /// 3. 通过隶属度函数量化定性指标 + /// public class FuzzyEvaluationModel : IEvaluationModel { - private readonly float[,] membershipMatrix; // 隶属度矩阵,每一行表示一个指标对不同评价等级的隶属度分布 - private readonly float[] weights; // 权重向量,表示各个指标的权重 + /// + /// 隶属度矩阵,每一行表示一个指标对不同评价等级的隶属度分布 + /// 矩阵元素必须在[0,1]区间内,且每行元素之和为1 + /// + private readonly float[,] membershipMatrix; + + /// + /// 权重向量,表示各个指标的相对重要性 + /// 权重必须为非负数且和为1 + /// + private readonly float[] weights; public string ModelName => "模糊评价模型"; + /// + /// 构造函数 + /// + /// 隶属度矩阵 + /// 权重向量 + /// 当隶属度矩阵无效时抛出异常 public FuzzyEvaluationModel(float[,] membershipMatrix, float[] weights) { this.membershipMatrix = membershipMatrix; @@ -22,10 +43,15 @@ namespace ActiveProtect.Evaluation if (!ValidateMembershipMatrix()) { - throw new ArgumentException("隶属度矩阵无效"); + throw new ArgumentException("隶属度矩阵无效:矩阵元素必须在[0,1]区间内,且每行元素之和为1"); } } + /// + /// 执行模糊综合评价 + /// + /// 评估数据字典 + /// 评估结果,范围在[0,1]之间 public float Evaluate(Dictionary evaluationData) { int factorCount = evaluationData.Count; // 评价因素个数 @@ -38,6 +64,7 @@ namespace ActiveProtect.Evaluation { for (int j = 0; j < gradeCount; j++) { + // 计算每个指标对每个评价等级的隶属度 fuzzyMatrix[i, j] = membershipMatrix[i, j] * data.Value; } i++; @@ -63,13 +90,19 @@ namespace ActiveProtect.Evaluation finalResult += result[i] * (i * gradeStep); } - // 归一化处理 + // 归一化处理,确保结果在[0,1]区间内 return finalResult / result.Sum(); } /// /// 验证隶属度矩阵的有效性 /// + /// 矩阵是否有效 + /// + /// 验证规则: + /// 1. 所有元素必须在[0,1]区间内 + /// 2. 每行元素之和必须为1(允许小误差) + /// private bool ValidateMembershipMatrix() { int rows = membershipMatrix.GetLength(0); diff --git a/src/Evaluation/GreyRelationModel.cs b/src/Evaluation/GreyRelationModel.cs index bf9f5c1..a9ba97e 100644 --- a/src/Evaluation/GreyRelationModel.cs +++ b/src/Evaluation/GreyRelationModel.cs @@ -6,20 +6,42 @@ namespace ActiveProtect.Evaluation { /// /// 灰色关联分析模型 - /// 适用于数据不完整的情况 + /// 适用于数据不完整的情况,通过计算评估指标与参考序列的关联度进行评估 /// + /// + /// 特点: + /// 1. 适应不完整信息 + /// 2. 计算简单直观 + /// 3. 能反映系统发展趋势 + /// public class GreyRelationModel : IEvaluationModel { - private readonly float[] referenceSequence; // 参考数列 - private readonly float resolution = 0.5f; // 分辨系数 + /// + /// 参考数列,表示理想的评估指标值 + /// + private readonly float[] referenceSequence; + + /// + /// 分辨系数,通常取0.5 + /// + private readonly float resolution = 0.5f; public string ModelName => "灰色关联分析模型"; + /// + /// 构造函数 + /// + /// 参考数列,表示最优或理想的指标值序列 public GreyRelationModel(float[] referenceSequence) { this.referenceSequence = referenceSequence; } + /// + /// 执行灰色关联分析评估 + /// + /// 评估数据字典 + /// 关联度,范围在[0,1]之间,值越大表示与参考序列越接近 public float Evaluate(Dictionary evaluationData) { float[] compareSequence = evaluationData.Values.ToArray(); @@ -34,10 +56,16 @@ namespace ActiveProtect.Evaluation ); } - // 计算关联度 + // 计算平均关联度 return correlationCoefficients.Average(); } + /// + /// 计算两个值之间的关联系数 + /// + /// 参考值 + /// 比较值 + /// 关联系数 private float CalculateCorrelationCoefficient(float refValue, float compValue) { float delta = Math.Abs(refValue - compValue); diff --git a/src/Evaluation/IEvaluationModel.cs b/src/Evaluation/IEvaluationModel.cs index f73cbdd..da84f9a 100644 --- a/src/Evaluation/IEvaluationModel.cs +++ b/src/Evaluation/IEvaluationModel.cs @@ -5,14 +5,15 @@ namespace ActiveProtect.Evaluation { /// /// 评估模型接口 + /// 定义了所有评估模型必须实现的基本功能 /// public interface IEvaluationModel { /// /// 执行评估 /// - /// 评估数据 - /// 评估结果 + /// 评估数据字典,键为指标名称,值为指标值 + /// 评估结果,范围在[0,1]之间,值越大表示评估结果越好 float Evaluate(Dictionary evaluationData); /// diff --git a/src/Simulation/SimulationExample.cs b/src/Simulation/SimulationExample.cs new file mode 100644 index 0000000..e5c3f8c --- /dev/null +++ b/src/Simulation/SimulationExample.cs @@ -0,0 +1,222 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using ActiveProtect.Models; +using ActiveProtect.Utility; + +namespace ActiveProtect.Simulation +{ + /// + /// 仿真示例类 + /// + public class SimulationExample + { + /// + /// 运行所有仿真示例 + /// + public static void RunAllExamples() + { + // 初始化坦克信息 + TankInfo tankinfo = new() + { + xp = 100, + yp = 0, + zp = 100 + }; + + // 创建仿真配置 + var config = new SimulationConfig + { + // 配置坦克 + TankConfigs = new List + { + new(tankinfo) + { + Id = "Tank_1", + InitialOrientation = new Orientation(Math.PI/4, 0, 0), + InitialSpeed = 20, + MaxSpeed = 25, + MaxArmor = 100, + InfraredRadiationIntensity = 150, + RadarCrossSection = 10, + MillimeterWaveRadiationTemperature = 100, + HasLaserWarner = false, + HasLaserJammer = true + } + }, + // 配置激光指示器 + LaserDesignatorConfig = new LaserDesignatorConfig + { + Id = "LD_1", + InitialPosition = new Vector3D(2000, 150, 100), + LaserPower = 1e6, + LaserDivergenceAngle = 0.01 + }, + // 配置激光告警器 + LaserWarnerConfig = new LaserWarnerConfig + { + Id = "LW_1", + AlarmDuration = 5.0 + }, + // 配置激光干扰器 + LaserJammerConfig = new LaserJammerConfig + { + Id = "LJ_1", + MaxJammingCooldown = 5.0, + MaxJammingPower = 10000.0, + InitialJammingPower = 4000.0, + PowerIncreaseRate = 2000.0 + }, + // 配置激光驾束仪 + LaserBeamRiderConfig = new LaserBeamRiderConfig + { + Id = "LBR_1", + InitialPosition = new Vector3D(2000, 10, 100), + LaserPower = 1000, + ControlFieldDiameter = 6 + }, + // 配置导弹 + MissileConfigs = new List + { + // 激光半主动制导导弹配置 + new() { + Id = "LSGM_1", + InitialPosition = new Vector3D(2000, 100, 100), + InitialOrientation = new Orientation(Math.PI, -0.1, 0), + InitialSpeed = 700, + MaxSpeed = 800, + MaxFlightTime = 10, + MaxFlightDistance = 5000, + LaunchAcceleration = 50, + MaxEngineBurnTime = 10, + MaxAcceleration = 400, + ProportionalNavigationCoefficient = 4, + ExplosionRadius = 5, + HitProbability = 0.9, + Mass = 50, + Type = MissileType.LaserSemiActiveGuidance + }, + // 激光驾束制导导弹配置 + new() { + Id = "LBRM_1", + InitialPosition = new Vector3D(2000, 10, 100), + InitialOrientation = new Orientation(Math.PI, 0.0, 0.0), + InitialSpeed = 300, + MaxSpeed = 400, + MaxFlightTime = 10, + MaxFlightDistance = 3000, + LaunchAcceleration = 50, + MaxEngineBurnTime = 10, + MaxAcceleration = 400, + ProportionalNavigationCoefficient = 3, + ExplosionRadius = 5, + HitProbability = 0.9, + Mass = 50, + Type = MissileType.LaserBeamRiderGuidance + }, + // 末敏弹配置 + new() { + Id = "TSM_1", + InitialPosition = new Vector3D(3000, 0, 100), // 发射位置 + InitialOrientation = new Orientation(Math.PI, 0, 0), // 使用计算得到的发射角度 + InitialSpeed = 800, + MaxSpeed = 1000, + MaxFlightTime = 30, + MaxFlightDistance = 5000, + LaunchAcceleration = 0, + MaxEngineBurnTime = 0, + MaxAcceleration = 10, + ProportionalNavigationCoefficient = 3, + ExplosionRadius = 5, + HitProbability = 0.9, + Mass = 50, + Type = MissileType.TerminalSensitiveMissile + }, + // 红外指令制导导弹配置 + new() { + Id = "ICGM_1", + InitialPosition = new Vector3D(2000, 10, 100), + InitialOrientation = new Orientation(Math.PI, 0.0, 0), + InitialSpeed = 300, + MaxSpeed = 400, + MaxFlightTime = 60, + MaxFlightDistance = 5000, + LaunchAcceleration = 50, + MaxEngineBurnTime = 10, + MaxAcceleration = 100, + ExplosionRadius = 5, + HitProbability = 0.9, + ProportionalNavigationCoefficient = 3, + Mass = 50, + Type = MissileType.InfraredCommandGuidance + }, + // 红外成像末制导导弹配置 + new() { + Id = "ITGM_1", + InitialPosition = new Vector3D(2000, 20, 100), + InitialOrientation = new Orientation(Math.PI, 0.0, 0), + InitialSpeed = 300, + MaxSpeed = 400, + MaxFlightTime = 60, + MaxFlightDistance = 5000, + LaunchAcceleration = 50, + MaxEngineBurnTime = 10, + MaxAcceleration = 50, + ExplosionRadius = 5, + HitProbability = 0.9, + ProportionalNavigationCoefficient = 3, + Mass = 50, + Type = MissileType.InfraredImagingTerminalGuidance + }, + // 毫米波导引头制导导弹配置 + new() { + Id = "MMWG_1", + InitialPosition = new Vector3D(2000, 200, 100), + InitialOrientation = new Orientation(Math.PI, 0.0, 0), + InitialSpeed = 300, + MaxSpeed = 400, + MaxFlightTime = 60, + MaxFlightDistance = 5000, + LaunchAcceleration = 50, + MaxEngineBurnTime = 10, + MaxAcceleration = 50, + ExplosionRadius = 5, + HitProbability = 0.9, + ProportionalNavigationCoefficient = 3, + Mass = 50, + Type = MissileType.MillimeterWaveTerminalGuidance + } + }, + // 配置红外测角仪 + InfraredTrackerConfig = new InfraredTrackerConfig + { + Id = "IT_1", + InitialPosition = new Vector3D(2000, 0, 100), + InitialOrientation = new Orientation(Math.PI, 0, 0), + MaxTrackingRange = 10000, // 10公里 + FieldOfView = Math.PI / 4, // 45度 + AngleMeasurementAccuracy = 0.0005, // 0.0005弧度 (约0.029度) + UpdateFrequency = 100 // 100赫兹 + }, + SimulationTimeStep = 0.01 // 仿真时间步长, 激光驾束制导导弹需要更小的步长,< 0.025s + }; + + // 创建仿真管理器 + var simulationManager = new SimulationManager(config); + + // 运行仿真 + int maxIterations = 10000; + int iteration = 0; + while (!simulationManager.IsSimulationEnded && iteration < maxIterations) + { + simulationManager.Update(); + simulationManager.PrintStatus(); + Thread.Sleep(20); // 暂停100毫秒,使输出更易读 + iteration++; + } + + Console.WriteLine("仿真结束"); + + } + } +} diff --git a/src/Simulation/SimulationManager.cs b/src/Simulation/SimulationManager.cs index fa02617..f386f92 100644 --- a/src/Simulation/SimulationManager.cs +++ b/src/Simulation/SimulationManager.cs @@ -239,7 +239,7 @@ namespace ActiveProtect.Simulation private void ActivateSomeElements() { // 激活坦克 - //Elements.FindAll(e => e.Id == "Tank_1").ForEach(e => e.Activate()); + Elements.FindAll(e => e.Id == "Tank_1").ForEach(e => e.Activate()); // 激活激光告警器 //Elements.FindAll(e => e.Id == "LW_1").ForEach(e => e.Activate()); @@ -269,7 +269,7 @@ namespace ActiveProtect.Simulation //Elements.FindAll(e => e.Id == "MMWG_1").ForEach(e => e.Activate()); // 激活末敏弹 - //Elements.FindAll(e => e.Id == "TSM_1").ForEach(e => e.Activate()); + Elements.FindAll(e => e.Id == "TSM_1").ForEach(e => e.Activate()); // 激活毫米波干扰器 //Elements.FindAll(e => e.Id == "MMWJ_1").ForEach(e => e.Activate());