From 4b2f29442a2cd3fa9f03c4808300dd3e1066e107 Mon Sep 17 00:00:00 2001 From: CJK_mkp <113243675+CJKmkp@users.noreply.github.com> Date: Fri, 7 Nov 2025 10:36:24 +0800 Subject: [PATCH] =?UTF-8?q?improve:=E7=82=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/Windows/NewStyleRollCallWindow.cs | 89 ++++++++++++-------- 1 file changed, 56 insertions(+), 33 deletions(-) diff --git a/Ink Canvas/Windows/NewStyleRollCallWindow.cs b/Ink Canvas/Windows/NewStyleRollCallWindow.cs index bfe85d88..84dec3a3 100644 --- a/Ink Canvas/Windows/NewStyleRollCallWindow.cs +++ b/Ink Canvas/Windows/NewStyleRollCallWindow.cs @@ -126,9 +126,10 @@ namespace Ink_Canvas private DateTime lastActivityTime = DateTime.Now; // 机器学习相关 - private RollCallHistoryData historyData = new RollCallHistoryData(); - private int maxRecentHistory = 20; - private double avoidanceWeight = 0.8; // 避免重复的权重 + private static RollCallHistoryData historyData = null; + private static readonly object historyLock = new object(); + private static int maxRecentHistory = 20; + private static double avoidanceWeight = 0.8; // 避免重复的权重 private const double FREQUENCY_WEIGHT = 0.2; // 频率平衡的权重 // 单次抽相关 @@ -394,13 +395,13 @@ namespace Ink_Canvas switch (selectedRollCallMode) { case "Random": - return SelectNamesWithML(availableNames, count); + return SelectNamesWithML(availableNames, count, random); case "Sequential": return SelectNamesSequentially(availableNames, count); case "Group": return SelectNamesInGroups(availableNames, count); default: - return SelectNamesWithML(availableNames, count); + return SelectNamesWithML(availableNames, count, random); } } @@ -471,18 +472,25 @@ namespace Ink_Canvas /// /// 可用名单 /// 需要选择的人数 + /// 随机数生成器 /// 选择的人员名单 - private List SelectNamesWithML(List availableNames, int count) + public static List SelectNamesWithML(List availableNames, int count, Random random) { if (availableNames == null || availableNames.Count == 0) return new List(); + // 确保历史数据已初始化 + if (historyData == null) + { + LoadRollCallHistory(); + } + // 检查是否启用机器学习避免重复 bool enableML = MainWindow.Settings?.RandSettings?.EnableMLAvoidance ?? true; if (!enableML) { // 如果禁用机器学习,使用简单随机选择 - return SelectNamesRandomly(availableNames, count); + return SelectNamesRandomly(availableNames, count, random); } var selectedNames = new List(); @@ -490,7 +498,7 @@ namespace Ink_Canvas for (int i = 0; i < count && remainingNames.Count > 0; i++) { - string selectedName = SelectSingleNameWithML(remainingNames, selectedNames); + string selectedName = SelectSingleNameWithML(remainingNames, selectedNames, random); if (!string.IsNullOrEmpty(selectedName)) { selectedNames.Add(selectedName); @@ -504,7 +512,7 @@ namespace Ink_Canvas /// /// 简单随机选择点名人员 /// - private List SelectNamesRandomly(List availableNames, int count) + private static List SelectNamesRandomly(List availableNames, int count, Random random) { if (availableNames == null || availableNames.Count == 0) return new List(); @@ -525,7 +533,7 @@ namespace Ink_Canvas /// /// 使用机器学习算法选择单个人员 /// - private string SelectSingleNameWithML(List availableNames, List alreadySelected) + private static string SelectSingleNameWithML(List availableNames, List alreadySelected, Random random) { if (availableNames.Count == 0) return null; if (availableNames.Count == 1) return availableNames[0]; @@ -554,15 +562,15 @@ namespace Ink_Canvas } // 使用加权随机选择 - return WeightedRandomSelection(nameWeights); + return WeightedRandomSelection(nameWeights, random); } /// /// 计算避免最近重复的权重 /// - private double CalculateRecentAvoidanceWeight(string name) + private static double CalculateRecentAvoidanceWeight(string name) { - if (historyData.History == null || historyData.History.Count == 0) + if (historyData == null || historyData.History == null || historyData.History.Count == 0) return 0.0; // 获取最近记录 @@ -576,9 +584,9 @@ namespace Ink_Canvas /// /// 计算频率平衡权重 /// - private double CalculateFrequencyWeight(string name) + private static double CalculateFrequencyWeight(string name) { - if (historyData.NameFrequency == null || !historyData.NameFrequency.ContainsKey(name)) + if (historyData == null || historyData.NameFrequency == null || !historyData.NameFrequency.ContainsKey(name)) return 0.5; // 如果从未被选中,给予中等权重 int totalSelections = historyData.NameFrequency.Values.Sum(); @@ -594,7 +602,7 @@ namespace Ink_Canvas /// /// 加权随机选择 /// - private string WeightedRandomSelection(Dictionary nameWeights) + private static string WeightedRandomSelection(Dictionary nameWeights, Random random) { if (nameWeights.Count == 0) return null; @@ -619,15 +627,23 @@ namespace Ink_Canvas /// /// 更新点名历史记录 /// - private void UpdateRollCallHistory(List selectedNames) + public static void UpdateRollCallHistory(List selectedNames) { if (selectedNames == null || selectedNames.Count == 0) return; - // 更新历史记录 - if (historyData.History == null) - historyData.History = new List(); + // 确保历史数据已初始化 + if (historyData == null) + { + LoadRollCallHistory(); + } - historyData.History.AddRange(selectedNames); + lock (historyLock) + { + // 更新历史记录 + if (historyData.History == null) + historyData.History = new List(); + + historyData.History.AddRange(selectedNames); // 保持历史记录不超过100条 if (historyData.History.Count > 100) @@ -647,18 +663,20 @@ namespace Ink_Canvas historyData.NameFrequency[name] = 1; } - historyData.LastUpdate = DateTime.Now; + historyData.LastUpdate = DateTime.Now; - // 保存到文件 - SaveRollCallHistory(); + // 保存到文件 + SaveRollCallHistory(); + } } + #endregion #region 数据持久化 /// /// 加载点名历史记录 /// - private void LoadRollCallHistory() + private static void LoadRollCallHistory() { try { @@ -695,7 +713,7 @@ namespace Ink_Canvas /// /// 保存点名历史记录 /// - private void SaveRollCallHistory() + private static void SaveRollCallHistory() { try { @@ -1292,8 +1310,9 @@ namespace Ink_Canvas // 动画结束,显示最终结果 Application.Current.Dispatcher.Invoke(() => { - // 使用60个数字进行抽选 - var selectedNumbers = SelectMultipleNumbers(currentCount); + // 使用降重抽选方法选择数字 + var numberList = Enumerable.Range(1, 60).Select(n => n.ToString()).ToList(); + var selectedNumbers = SelectNamesWithML(numberList, currentCount, random); // 更新历史记录 UpdateRollCallHistory(selectedNumbers); @@ -1384,8 +1403,8 @@ namespace Ink_Canvas // 动画结束,显示最终结果 Application.Current.Dispatcher.Invoke(() => { - // 根据选择的模式进行不同的点名逻辑 - var selectedNames = SelectNamesByMode(nameList, currentCount); + // 使用降重抽选方法 + var selectedNames = SelectNamesWithML(nameList, currentCount, random); // 更新历史记录 UpdateRollCallHistory(selectedNames); @@ -1442,8 +1461,12 @@ namespace Ink_Canvas // 动画结束,显示最终结果 Application.Current.Dispatcher.Invoke(() => { - // 根据选择的数量进行抽选 - var selectedNumbers = SelectMultipleNumbers(currentCount); + // 使用降重抽选方法选择数字 + var numberList = Enumerable.Range(1, 60).Select(n => n.ToString()).ToList(); + var selectedNumbers = SelectNamesWithML(numberList, currentCount, random); + + // 更新历史记录 + UpdateRollCallHistory(selectedNumbers); if (selectedNumbers.Count == 1) { @@ -1482,7 +1505,7 @@ namespace Ink_Canvas } /// - /// 选择多个数字(不重复) + /// 选择多个数字 /// private List SelectMultipleNumbers(int count) {