From d63b83c2c745c1f8a140221c28c0e6dbe61d6110 Mon Sep 17 00:00:00 2001
From: ningshuxia <ningshuxia0927@outlook.com>
Date: 星期二, 30 四月 2024 17:47:17 +0800
Subject: [PATCH] 调度修改

---
 Schedule/IStation.Algorithm/Schedule/SchedulingHelper.cs |  347 ++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 208 insertions(+), 139 deletions(-)

diff --git a/Schedule/IStation.Algorithm/Schedule/SchedulingHelper.cs b/Schedule/IStation.Algorithm/Schedule/SchedulingHelper.cs
index 9f08205..0e9e97c 100644
--- a/Schedule/IStation.Algorithm/Schedule/SchedulingHelper.cs
+++ b/Schedule/IStation.Algorithm/Schedule/SchedulingHelper.cs
@@ -1,21 +1,34 @@
-锘縰sing System;
-
-namespace IStation.Algorithm
+锘縩amespace IStation.Algorithm
 {
 
     #region ViewModel
 
     /// <summary>
+    /// 
+    /// </summary>
+    public class FreCombine
+    {
+        public FreCombine() { }
+        public List<int> Flags { get; set; }
+        public int RunCount { get; set; }
+        public double Frequency { get; set; }
+        public double Flow { get; set; }
+        public double Power { get; set; }
+    }
+
+
+
+    /// <summary>
     /// 鍒嗘瀽娉甸」
     /// </summary>
-    public class AnaItem
+    public class OptimalCombine
     {
-        public AnaItem() { }
-        public string RunFlag { get; set; }
-        public int RunCount { get; set; }
-        public List<long> PumpIds { get; set; }
+        public OptimalCombine() { }
+        public List<int> Flags { get; set; }
         public double Flow { get; set; }
         public double Head { get; set; }
+        public double Power { get; set; }
+        public List<FreCombine> Combines { get; set; }
     }
 
     #endregion
@@ -26,9 +39,9 @@
     public class SchedulingHelper
     {
         double _frequency_def = 50;
-        double _frequency_min = 25;
-        double _frequency_max = 50;
-        double _frequency_space = 0.1;//棰戠巼闂撮殧  
+        decimal _frequency_min = 25;
+        decimal _frequency_max = 50;
+        decimal _frequency_space = 0.1m;//棰戠巼闂撮殧  
 
         double _head_space = 0.1;//鎵▼闂撮殧
 
@@ -37,69 +50,45 @@
         string _falgFixPumpTag = "G";
         string _falgSpaceMark = "_";
 
-        string GetRunFlag(List<AnaPumpItem> pumpItems)
-        {
-            var tableName = string.Empty;
-            var count = pumpItems.Count;
-            pumpItems = pumpItems.OrderBy(p => p.ID).ToList();
-            for (int i = 0; i < count; i++)
-            {
-                var pumpItem = pumpItems[i];
-                tableName += GetRunFlag(pumpItem);
-                if ((i + 1) != count)
-                {
-                    tableName += _falgSpaceMark;
-                }
-            }
-            return tableName;
-        }
 
-        string GetRunFlag(AnaPumpItem pumpItem)
+        string GetRunFlag(int[] flags)
         {
-            return (pumpItem.IsBp ? _falgFrePumpTag : _falgFixPumpTag) + pumpItem.ID;
-        }
-
-
-        string GetRunFlag(Dictionary<int, bool> dic)
-        {
-            var tableName = string.Empty;
+            var runFlag = string.Empty;
             var index = 0;
-            var count = dic.Count;
-            foreach (var item in dic)
-            {
-                tableName += GetRunFlag(item.Key, item.Value);
-                index++;
-                if (index != count)
-                {
-                    tableName += _falgSpaceMark;
-                }
-            }
-            return tableName;
-        }
-
-        string GetRunFlag(long id, bool isBp)
-        {
-            return (isBp ? _falgFrePumpTag : _falgFixPumpTag) + id;
-        }
-
-        string GetRunFlag(List<string> flags)
-        {
-            var tableName = string.Empty;
-            var count = flags.Count;
-            var index = 0;
+            var count = flags.Length;
             foreach (var flag in flags)
             {
-                tableName += flag;
+                runFlag += GetGFlag(flag);
                 index++;
                 if (index != count)
                 {
-                    tableName += _falgSpaceMark;
+                    runFlag += _falgSpaceMark;
                 }
             }
-            return tableName;
+            return runFlag;
         }
 
+        string GetRunFlag(List<int> flags)
+        {
+            var runFlag = string.Empty;
+            var index = 0;
+            var count = flags.Count;
+            foreach (var flag in flags)
+            {
+                runFlag += GetGFlag(flag);
+                index++;
+                if (index != count)
+                {
+                    runFlag += _falgSpaceMark;
+                }
+            }
+            return runFlag;
+        }
 
+        string GetGFlag(int flag)
+        {
+            return _falgFrePumpTag + flag;
+        }
 
         #endregion
 
@@ -108,102 +97,182 @@
         DAL.ScheduleAnaLog _dalAnaLog = new DAL.ScheduleAnaLog();
 
 
-        public class CurrentViewModel : Entity.ScheduleConclusion
+        private List<int> _combineFlags1 = new List<int>() { 11, 12, 13, 14, 16, 17, 18 };
+        private List<int> _combineFlags2 = new List<int>() { 15 };
+
+        public void Ana(List<Pump> pumps, double taget_flow, double tagetHead)
         {
-            public CurrentViewModel(Entity.ScheduleConclusion rhs) : base(rhs)
+            tagetHead = Math.Round(tagetHead);
+            var pump_flag_list = pumps.Select(x => x.ID).ToList();
+            for (int pumpCount = 1; pumpCount <= pumps.Count; pumpCount++)
             {
-                this.Flags = new List<int>();
-                var flags = rhs.RunFlag.Split('_');
-                foreach (var item in flags)
+                var combine_list = PermutationAndCombination<int>.GetCombination(pump_flag_list.ToArray(), pumpCount);
+                foreach (var combine in combine_list)
                 {
-                    if (int.TryParse(item.Substring(1), out int flag))
-                    {
-                        this.Flags.Add(flag);
-                    }
+                    List<int> combine_flag_list_part1 = new List<int>();
+                    List<int> combine_flag_list_part2 = new List<int>();
 
-                }
-            }
-            public List<int> Flags { get; set; }
-        }
-
-
-        public void Ana(List<Pump> pumps, double tagetFlow, double tagetHead, List<int> openPumpCombine)
-        {
-            var pumpIds = pumps.Select(x => x.ID);
-            var dic_pump = pumps.ToDictionary(x => x.ID, x => x.IsBp);
-            if (openPumpCombine != null && openPumpCombine.Any())
-            {
-                var allCombineList = PermutationAndCombination<int>.GetCombination(pumpIds.ToArray(), 2);
-                var combineList = new List<List<int>>();
-                foreach (var combine in allCombineList)
-                {
-                    var pump1 = combine[0];
-                    var pump2 = combine[1];
-                    if (!openPumpCombine.Contains(pump1))
-                        continue;
-                    if (!openPumpCombine.Contains(pump2))
-                        continue;
-                    combineList.Add(combine.ToList());
-                }
-                if (combineList.Count < 1)
-                    return;
-
-                var avgFlow = tagetFlow / 2;
-
-                var optimalConclusionList = new List<CurrentViewModel>();
-                foreach (var combine in combineList)
-                {
-                    var dic = new Dictionary<int, bool>();
                     foreach (var pump in combine)
                     {
-                        if (dic_pump.ContainsKey(pump))
+                        if (_combineFlags1.Contains(pump))
                         {
-                            dic.Add(pump, dic_pump[pump]);
+                            combine_flag_list_part1.Add(pump);
+                        }
+                        else
+                        {
+                            combine_flag_list_part2.Add(pump);
                         }
                     }
-                    var runFalg = GetRunFlag(dic);
-                    var list = _dalScheduleConclusion.GetList(runFalg, avgFlow, tagetHead, 1);
-                    if (list != null && list.Any())
+
+
+                    List<FreCombine> fre_combine_list_part1 = new List<FreCombine>();
+                    List<FreCombine> fre_combine_list_part2 = new List<FreCombine>();
+
+                    if (combine_flag_list_part1.Count > 0)
                     {
-                        var vmList = list.Select(x => new CurrentViewModel(x)).ToList();
-                        optimalConclusionList.AddRange(vmList);
+                        var conclusion_list_dic = new Dictionary<int, List<Entity.ScheduleConclusion>>();
+                        foreach (var flag in combine_flag_list_part1)
+                        {
+                            var runFlag = GetGFlag(flag);
+                            if (conclusion_list_dic.ContainsKey(flag))
+                                continue;
+                            var conclusionList = _dalScheduleConclusion.GetList(runFlag, tagetHead);
+                            conclusion_list_dic[flag] = conclusionList;
+                        }
+
+                        if (conclusion_list_dic.Count < 1)
+                        {
+                            continue;
+                        }
+
+                        for (decimal fre = _frequency_max; fre >= _frequency_min; fre -= _frequency_space)
+                        {
+                            var freCombine = new FreCombine();
+                            freCombine.Frequency = (double)fre;
+                            freCombine.Flags = new List<int>();
+                            foreach (var item in conclusion_list_dic)
+                            {
+                                var conclusion = item.Value.Find(x => x.Pump1 == (double)fre);
+                                if (conclusion != null)
+                                {
+                                    freCombine.Flags.Add(item.Key);
+                                    freCombine.Flow += conclusion.Flow;
+                                    freCombine.Power += conclusion.Power;
+                                    freCombine.RunCount++;
+                                }
+                            }
+                            if (freCombine.Flags.Count < 1)
+                                continue;
+                            fre_combine_list_part1.Add(freCombine);
+                        }
+
+
+
+
                     }
-                }
-                if (optimalConclusionList == null || !optimalConclusionList.Any())
-                {
-                    return;
+
+                    if (combine_flag_list_part2.Count > 0)
+                    {
+                        var conclusion_list_dic = new Dictionary<int, List<Entity.ScheduleConclusion>>();
+                        foreach (var flag in combine_flag_list_part2)
+                        {
+                            var runFlag = GetGFlag(flag);
+                            if (conclusion_list_dic.ContainsKey(flag))
+                                continue;
+                            var conclusionList = _dalScheduleConclusion.GetList(runFlag, tagetHead);
+                            conclusion_list_dic[flag] = conclusionList;
+                        }
+
+                        if (conclusion_list_dic.Count < 1)
+                        {
+                            continue;
+                        }
+
+                        for (decimal fre = _frequency_max; fre >= _frequency_min; fre -= _frequency_space)
+                        {
+                            var freCombine = new FreCombine();
+                            freCombine.Frequency = (double)fre;
+                            freCombine.Flags = new List<int>();
+                            foreach (var item in conclusion_list_dic)
+                            {
+                                var conclusion = item.Value.Find(x => x.Pump1 == (double)fre);
+                                if (conclusion != null)
+                                {
+                                    freCombine.Flags.Add(item.Key);
+                                    freCombine.Flow += conclusion.Flow;
+                                    freCombine.Power += conclusion.Power;
+                                    freCombine.RunCount++;
+                                }
+                            }
+                            if (freCombine.Flags.Count < 1)
+                                continue;
+                            fre_combine_list_part2.Add(freCombine);
+                        }
+                    }
+
+
+                    var optimalCombine = AnaOptimalCombine(fre_combine_list_part1, fre_combine_list_part2, taget_flow);
                 }
 
-                var optimalConclusionCombineList = PermutationAndCombination<CurrentViewModel>.GetCombination(optimalConclusionList.ToArray(), 2);
-                var combines = new List<Combine>();
-                foreach (var arr in optimalConclusionCombineList)
-                {
-                    var falgs = arr.SelectMany(x => x.Flags).ToList();
-                    if (falgs.GroupBy(x => x).Where(x => x.Count() > 1).Count() > 0)
-                    {
-                        continue;
-                    }
-                    var combine = new Combine();
-                    combine.Power = arr.Sum(x => x.Power);
-                    combine.Head = arr.Average(x => x.Head);
-                    combine.Flow = arr.Sum(x => x.Flow);
-                    combine.RunFlagList = falgs;
-                    combines.Add(combine);
-                }
-
-                var a = combines;
             }
+
         }
 
-        private class Combine
+
+        private OptimalCombine AnaOptimalCombine(List<FreCombine> fre_combine_list_part1, List<FreCombine> fre_combine_list_part2, double target_flow)
         {
-            public double Flow { get; set; }
-            public double Head { get; set; }
-            public double Power { get; set; }
-            public List<int> RunFlagList { get; set; }
+            double flow_deviation = target_flow;
+            double total_power = double.MaxValue;
+            double total_flow = double.MaxValue;
+
+            FreCombine optimal_combine_part1 = null;
+            FreCombine optimal_combine_part2 = null;
+            for (int Index_part1 = 0; Index_part1 < fre_combine_list_part1.Count; Index_part1++)
+            {
+                for (int Index_part2 = 0; Index_part2 < fre_combine_list_part2.Count; Index_part2++)
+                {
+                    var fre_combine1 = fre_combine_list_part1[Index_part1];
+                    var fre_combine2 = fre_combine_list_part2[Index_part2];
+
+                    var current_flow = fre_combine1.Flow + fre_combine2.Flow;
+                    var current_power = fre_combine1.Power + fre_combine2.Power;
+
+                    var diff_flow = Math.Abs(current_flow - target_flow);
+                    if (diff_flow < flow_deviation)
+                    {
+                        optimal_combine_part1 = fre_combine1;
+                        optimal_combine_part2 = fre_combine2;
+                        total_power = fre_combine1.Power + fre_combine2.Power;
+                        total_flow = current_flow;
+                        flow_deviation = diff_flow;
+                    }
+
+                    if (diff_flow < target_flow * 0.01 && current_power < total_power)
+                    {
+                        optimal_combine_part1 = fre_combine1;
+                        optimal_combine_part2 = fre_combine2;
+                        total_power = fre_combine1.Power + fre_combine2.Power;
+                        total_flow = current_flow;
+                    }
+
+                }
+            }
+            if (optimal_combine_part1 == null && optimal_combine_part2 == null)
+                return default;
+            var optimal_combine = new OptimalCombine();
+            optimal_combine.Combines = new List<FreCombine>();
+            if (optimal_combine_part1 != null)
+            {
+                optimal_combine.Combines.Add(optimal_combine_part1);
+            }
+            if (optimal_combine_part2 != null)
+            {
+                optimal_combine.Combines.Add(optimal_combine_part2);
+            }
+            optimal_combine.Flow = total_flow;
+            optimal_combine.Power = total_power;
+            return optimal_combine;
         }
-
-
 
         /// <summary>
         /// 鎻掑叆鍒嗘瀽鏃ュ織

--
Gitblit v1.9.3