From 49eb2a09634e439090d4a2b13ec7d6f911d3deaf Mon Sep 17 00:00:00 2001
From: cloudflight <cloudflight@126.com>
Date: 星期六, 27 七月 2024 11:44:21 +0800
Subject: [PATCH] fix:修复沿程损失计算异常的问题

---
 Hydraulic/Hydro.CommonBase/ParamModel.cs |  261 ++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 184 insertions(+), 77 deletions(-)

diff --git a/Hydraulic/Hydro.CommonBase/ParamModel.cs b/Hydraulic/Hydro.CommonBase/ParamModel.cs
index f92314d..16ce6e2 100644
--- a/Hydraulic/Hydro.CommonBase/ParamModel.cs
+++ b/Hydraulic/Hydro.CommonBase/ParamModel.cs
@@ -14,6 +14,7 @@
 using System.Collections.Concurrent;
 using Dapper;
 using NPOI.SS.Formula.Functions;
+using NPOI.SS.Formula.Eval;
 //using NPOI.Util.Collections;
 
 namespace Hydro.CommonBase
@@ -36,12 +37,78 @@
             if (Quene!=null) Quene.Dispose();
             Quene = new Quene();
             this.Quene= Quene;
-            //获取全局默认设置
-            var globalConfig = Db.Connection.QueryFirstOrDefault<string>("SELECT FValue FROM MyConfig WHERE FKey='GlobalConfig'");
-            GlobalConfig = JsonConvert.DeserializeObject<GlobalConfig>(globalConfig) ?? new GlobalConfig();
-            this.GlobalConfig = GlobalConfig;
+            if (ConfigPath!=null)
+            {
+                //获取全局默认设置
+                var globalConfig = Db.Connection.QueryFirstOrDefault<string>("SELECT FValue FROM MyConfig WHERE FKey='GlobalConfig'");
+                GlobalConfig = JsonConvert.DeserializeObject<GlobalConfig>(globalConfig) ?? new GlobalConfig();
+                this.GlobalConfig = GlobalConfig;
+            }
+            
             isInited = true;
             //return gParam;
+        }
+        public string Load_GeneticSettings(LogicModelParams param)
+        {
+
+            var ranges = new List<DRange>();
+            var accuracys = new List<Double?>();
+            var DLengths = new List<int>();
+            //chromosomeLength = 0;
+            param.试算变量.ForEach(v =>
+            {
+                ranges.Add(v.doubleRange);
+                DLengths.Add(getDLength(v.doubleRange, v.accuracy));
+                accuracys.Add(v.accuracy);
+            });
+            this.ranges = ranges;
+            this.DLengths = DLengths;
+            this.accuracys = accuracys;
+            return null;
+        }
+        public string Load_Settings(LogicModelParams param, FinishEvent onFinished, FinishEvent onError, FinishEvent onReportProgress, SetEvent SetVarsMethod)
+        {
+            //string result = null;
+
+
+            //DLengths.ForEach(m => chromosomeLength += m);
+
+            this.populationSize = this.GlobalConfig.Ext.populationNum;
+            this.iterations = this.GlobalConfig.Ext.iterationNum;
+            this.populationSize_min = this.GlobalConfig.Ext.populationNum_full;
+            this.iterations_full = this.GlobalConfig.Ext.iterationNum_full;
+
+            this.selectionMethod = 0;
+            this.optimizationMode = param.OFunction.ExpressType == "最大" ? 1 : 0;
+            this.强制重计算 = this.GlobalConfig.Ext.是否优选方案;
+            this.iterations_min = this.GlobalConfig.Ext.iterationNum_Min;
+
+            this.onReportProgress = onReportProgress;
+            this.onFinished = onFinished;
+            this.onError = onError;
+            this.setVars = SetVarsMethod;
+
+            
+
+            return null;
+          
+        }
+        int getDLength(DRange dr, double? accur)
+        {
+            if (accur == null) accur = 0.1;
+            var num_Total = dr.Length / (double)accur;
+            var Dlength = 0;
+            var value = 1;
+            var i = 0;
+            //判断需要多少位二进制数
+            //Dlength=Math.Log(num_Total, 2) > 0 ? (int)Math.Ceiling( Math.Log(num_Total, 2)) : 0;
+            while (value <= num_Total && i < 1000)
+            {
+                value *= 2;
+                Dlength += 1;
+                i++;
+            }
+            return Dlength;
         }
         public void Dispose()
         {
@@ -51,19 +118,23 @@
 
         }
         
+        //世代最优解列表
+        public List<double> list_FitnessByItera;
 
-        public List<double> list_BestEnerge;
-        public List<double> list_AvgHz;
+        #region 解析参数
         public List<DRange> ranges;
         public List<int> DLengths;
         public List<double?> accuracys;
-        public List<int> Dependencies;
-        public double[] list_AtemtpArr;
+        #endregion 
+
+    
+        
+        //public double[] list_AtemtpArr;
 
         public string SolutionSaveName;
         public int populationSize;
         public int iterations;
-        public int populationSize_full;
+        public int populationSize_min;
         public int iterations_full;
         public List<ulong> Upopulations = null;
 
@@ -82,15 +153,20 @@
         public int num;
         public double BestOptValue;
         public DRange tolerance;
+        [JsonIgnore]
         public FinishEvent onFinished;
+        [JsonIgnore]
         public FinishEvent onError;
+        [JsonIgnore]
         public FinishEvent onReportProgress;
+        [JsonIgnore]
         public SetEvent setVars;
 
         public Db Db;
         public dynamic SolutionDBHelper = null;
         public GlobalConfig GlobalConfig;
         public bool isInited=false;
+        [JsonIgnore]
         public Quene Quene;
         public int Level;
         public int ID;
@@ -98,47 +174,46 @@
         public bool isNeedOutput = true;
 
         public double CurrentTotalDemand = -1;
-        public GeneticParams DeepCopy()
-        {
-            var copy = new GeneticParams(this.Level);
-            copy.list_BestEnerge = this.list_BestEnerge?.ToList();
-            copy.list_AvgHz = this.list_AvgHz?.ToList();
-            copy.ranges = this.ranges?.Select(range => new DRange(range.Min, range.Max))?.ToList();
-            copy.DLengths = this.DLengths?.ToList();
-            copy.accuracys = this.accuracys?.ToList();
-            copy.Dependencies = this.Dependencies?.ToList();
-            copy.list_AtemtpArr = this.list_AtemtpArr?.ToArray();
-            copy.SolutionSaveName = this.SolutionSaveName;
-            copy.populationSize = this.populationSize;
-            copy.iterations = this.iterations;
-            copy.populationSize_full = this.populationSize_full;
-            copy.iterations_full = this.iterations_full;
-            copy.Upopulations = this.Upopulations?.ToList();
-            copy.selectionMethod = this.selectionMethod;
-            copy.optimizationMode = this.optimizationMode;
-            copy.强制重计算 = this.强制重计算;
-            copy.iterations_min = this.iterations_min;
-            copy.accuracyNum = this.accuracyNum;
-            copy.MaxRobot = this.MaxRobot;
-            copy.RuningRobot = this.RuningRobot;
-            copy.num = this.num;
-            copy.BestOptValue = this.BestOptValue;
-            copy.tolerance = tolerance==null?null: new DRange(this.tolerance.Min, this.tolerance.Max);
-            copy.onFinished = this.onFinished;
-            copy.onError = this.onError;
-            copy.onReportProgress = this.onReportProgress;
-            copy.setVars = this.setVars;
-            copy.Db = this.Db; // Assuming Db is a reference type
-            copy.SolutionDBHelper = this.SolutionDBHelper; // Assuming SolutionDBHelper is a reference type
-            copy.GlobalConfig = this.GlobalConfig.DeepCopyByBin<GlobalConfig>(); // Assuming GlobalConfig has a copy constructor
-            copy.isInited = this.isInited;
-            copy.Quene = this.Quene; // Assuming Quene has a copy constructor
-            copy.ID = this.ID;
-            copy.isNeedOutput = this.isNeedOutput;
-            copy.CurrentTotalDemand = this.CurrentTotalDemand;
+        //public GeneticParams DeepCopy()
+        //{
+        //    var copy = new GeneticParams(this.Level);
+        //    copy.list_FitnessByItera = this.list_FitnessByItera?.ToList();
+        //    copy.ranges = this.ranges?.Select(range => new DRange(range.Min, range.Max))?.ToList();
+        //    copy.DLengths = this.DLengths?.ToList();
+        //    copy.accuracys = this.accuracys?.ToList();
+            
+        //    //copy.list_AtemtpArr = this.list_AtemtpArr?.ToArray();
+        //    copy.SolutionSaveName = this.SolutionSaveName;
+        //    copy.populationSize = this.populationSize;
+        //    copy.iterations = this.iterations;
+        //    copy.populationSize_min = this.populationSize_min;
+        //    copy.iterations_full = this.iterations_full;
+        //    copy.Upopulations = this.Upopulations?.ToList();
+        //    copy.selectionMethod = this.selectionMethod;
+        //    copy.optimizationMode = this.optimizationMode;
+        //    copy.强制重计算 = this.强制重计算;
+        //    copy.iterations_min = this.iterations_min;
+        //    copy.accuracyNum = this.accuracyNum;
+        //    copy.MaxRobot = this.MaxRobot;
+        //    copy.RuningRobot = this.RuningRobot;
+        //    copy.num = this.num;
+        //    copy.BestOptValue = this.BestOptValue;
+        //    copy.tolerance = tolerance==null?null: new DRange(this.tolerance.Min, this.tolerance.Max);
+        //    copy.onFinished = this.onFinished;
+        //    copy.onError = this.onError;
+        //    copy.onReportProgress = this.onReportProgress;
+        //    copy.setVars = this.setVars;
+        //    copy.Db = this.Db; // Assuming Db is a reference type
+        //    copy.SolutionDBHelper = this.SolutionDBHelper; // Assuming SolutionDBHelper is a reference type
+        //    copy.GlobalConfig = this.GlobalConfig;//.DeepCopyByBin<GlobalConfig>(); // Assuming GlobalConfig has a copy constructor
+        //    copy.isInited = this.isInited;
+        //    copy.Quene = this.Quene; // Assuming Quene has a copy constructor
+        //    copy.ID = this.ID;
+        //    copy.isNeedOutput = this.isNeedOutput;
+        //    copy.CurrentTotalDemand = this.CurrentTotalDemand;
 
-            return copy;
-        }
+        //    return copy;
+        //}
     }
     public class Result
     {
@@ -163,12 +238,13 @@
 
         public System.Collections.Generic.Queue<WdnmoParam> RequestQueues;
         public HashSet<Guid> RequestFinished;
-        public Dictionary<ulong, double> ResultHash;
+        public ConcurrentDictionary<ulong, double> ResultHash;
         public int Level;
-        public Dictionary<Guid, double?> ResultDictionary;
-        public Dictionary<Guid, ChromosomeBase> ResultChrome;
+        public ConcurrentDictionary<Guid, double?> ResultDictionary;
+        public ConcurrentDictionary<Guid, ChromosomeBase> ResultChrome;
         public int MissionQueneCount;
-        public List<calcParam> MissionQuene;
+        //public  List<calcParam> MissionQuene;
+        public ConcurrentQueue<calcParam> MissionQuene;
         public DateTime ScadaTime;
         //public List<TimePoint> TimePoints;
         public double BestResult=double.MaxValue;
@@ -202,7 +278,7 @@
             }
             if (MissionQuene != null)
             {
-                MissionQuene.Clear();
+                //MissionQuene.Clear();
                 MissionQuene = null;
             }
 
@@ -451,6 +527,8 @@
 
         public List<SetVar> setVars = new List<SetVar>();
 
+        public int CurrentIndex = -1;
+
     }
     public class SetVar
     {
@@ -543,6 +621,9 @@
         public List<variable> 变量 = new List<variable>();
         public List<variable> 表达式 = new List<variable>();
         public List<variable> 预处理插件 = new List<variable>();
+        public variable ChromeFilter = null;
+        public variable GeneFilter = null;
+        [JsonIgnore]
         public List<variable> 输出变量
         {
             get
@@ -557,6 +638,7 @@
                 return all;
             }
         }
+        [JsonIgnore]
         public List<variable> 接口变量
         {
             get
@@ -570,6 +652,7 @@
                 return all;
             }
         }
+        [JsonIgnore]
         public List<variable> 设定变量
         {
             get
@@ -580,7 +663,7 @@
                 return all;
             }
         }
-        
+        [JsonIgnore]
         public List<variable> 变量全集
         {
             get
@@ -607,6 +690,7 @@
         //[NonSerialized]
         [JsonIgnore]
         public Dictionary<int, variable> dict_ID { get; set; }
+        [JsonIgnore]
         public Dictionary<string, variable> dict_Tag { get; private set; }
         
         //[NonSerialized]
@@ -633,18 +717,26 @@
             dict_Name = new Dictionary<string, variable>();
             dict_ID=new Dictionary<int, variable>();
             dict_Tag=new Dictionary<string, variable>();
-            dict_OutPut = new Dictionary<string, Ivariable[]>();
+            //dict_OutPut = new Dictionary<string, Ivariable[]>();
 
             变量全集.ForEach(vv => { if (!string.IsNullOrEmpty(vv.Name) && !dict_Name.ContainsKey(vv.Name)) dict_Name.Add(vv.Name, vv); });
             变量全集.ForEach(vv => {  if (!dict_ID.ContainsKey(vv.ID)) dict_ID.Add(vv.ID, vv); });
             变量全集.ForEach(vv => { if (!string.IsNullOrEmpty(vv.Tag) && !dict_Tag.ContainsKey(vv.Tag)) dict_Tag.Add(vv.Tag, vv); });
             
-            输出变量.ForEach(vv => 
+            //输出变量.ForEach(vv => 
+            //{
+            //    if (!string.IsNullOrEmpty(vv.Tag) &&  !dict_OutPut.ContainsKey(vv.Tag))
+            //        dict_OutPut.Add(vv.Tag, new Ivariable[] { vv });
+            //    if (!string.IsNullOrEmpty(vv.expressString) &&!dict_OutPut.ContainsKey(vv.expressString)) 
+            //        dict_OutPut.Add(vv.expressString,new Ivariable[] { vv }); 
+            //});
+
+            预处理插件.ForEach(vv => 
             {
-                if (!string.IsNullOrEmpty(vv.Tag) &&  !dict_OutPut.ContainsKey(vv.Tag))
-                    dict_OutPut.Add(vv.Tag, new Ivariable[] { vv });
-                if (!string.IsNullOrEmpty(vv.expressString) &&!dict_OutPut.ContainsKey(vv.expressString)) 
-                    dict_OutPut.Add(vv.expressString,new Ivariable[] { vv }); 
+                if (vv.IndicatorType=="方案筛选器")
+                    ChromeFilter = vv;
+                if (vv.IndicatorType == "基因筛选器")
+                    GeneFilter = vv;
             });
 
         }
@@ -731,21 +823,22 @@
     [Serializable]
     public class variable: Ivariable
     {
-        public static List<string> list_indicatorTypeName
+        public static List<string> list_indicatorTypeName 
         {
             get
             {
-                if (_list_indicatorTypeName == null)
+                if (_list_indicatorTypeName==null)
                 {
-                    _list_indicatorTypeName = new List<string>() { "已知压力", "节点用水量", "节点用水模式", "水池水位", "水泵转速比", "管线阻力系数", "阀门开度", "水泵开关", "阀门开关", "管线开关", "管线直径", "管线长度", "总水量", "节点压力", "节点自由压力", "节点需水量", "节点标高", "节点水龄", "节点水质", "管线流量", "管线流速", "水头损失", "当前状态", "水泵能耗", "水泵开关", "水泵转速比", "初始及运行传参", "初始传参", "运行传参", "种群生成器", "方案筛选器", "计算前处理", "结果处理", "当量分配", "随机分配" };
+                    _list_indicatorTypeName =new List<string>() { "已知压力", "节点用水量", "节点用水模式", "水池水位", "水泵转速比", "管线阻力系数", "阀门开度", "水泵开关", "阀门开关", "管线开关", "管线直径", "管线长度", "总水量", "节点压力", "节点自由压力", "节点需水量", "节点标高", "节点水龄", "节点水质", "管线流量", "管线流速", "水头损失", "当前状态", "水泵能耗", "水泵开关", "水泵转速比", "初始化","初始及运行传参", "初始传参", "运行传参", "种群生成器", "方案筛选器", "基因筛选器", "计算前处理", "输出计算处理", "结果处理", "当量分配", "随机分配" };
                 }
                 return _list_indicatorTypeName;
             }
-
-
+         
+            
         }
 
         private static List<string> _list_indicatorTypeName = null;
+
         public static List<string> listUsedInitValue = new List<string>() { "初始计算值", "变化值", "绝对变化值" };
         //public static List<string> list_输入值 = new List<string>() { "基准值(界面)", "定义值", "基准值(接口)",  "模式系数(界面)", "模式系数(接口)" };
         public static HashSet<string> list_输入值 = new HashSet<string>() { "基准值(界面)", "定义值", "基准值(接口)", "模式系数(界面)", "模式系数(接口)" };
@@ -812,7 +905,7 @@
         public bool isInput
         {
             get;
-            private set;
+            set;
         } = false;
         public bool isNode { get; set; }
         //public bool? isOutput;
@@ -826,12 +919,10 @@
         public int arrgFuncType;
         public string defaultExpress;
         private int _indicatorType;
-      
         public string IndicatorType 
         { 
             get
             {
-                
                 if (_indicatorType<0)
                 {
                     return list_indicatorTypeName[0];
@@ -867,7 +958,7 @@
             {
                 return _Values;
             }
-            private set
+            set
             {
                 _Values = value;
             }
@@ -892,7 +983,7 @@
             }
             set
             {
-                isInput = true;
+                
                 if (value==null) 
                     LogicValues[0]=double.NaN;
                 else
@@ -913,7 +1004,8 @@
         /// </summary>
         public string Value_Set(int period,double value)
         {
-            _Values[period] = value;
+            if (_Values!=null && period < _Values.Length)
+                _Values[period] = value;
             return null;
             string result = null;
             double formatValue = value;
@@ -1023,9 +1115,11 @@
                 if (_level!=0) return _level;
                 int tempLevel = 1;
                 if (Vars!=null)
-                foreach (var v in Vars) 
+                for (int i=0;i<Vars.Count;i++) 
                 {
-                    if (v!=this && v is variable vv && tempLevel < vv.Level + 1) tempLevel = vv.Level + 1;
+                    var v = Vars[i];
+                   
+                    if (!VarIndexs.ContainsKey(i) && v !=this && v is variable vv && tempLevel < vv.Level + 1) tempLevel = vv.Level + 1;
                 }
                 _level= tempLevel;
                 return _level;
@@ -1034,6 +1128,10 @@
         }
 
         public string Tag { get; set; }
+        public string AnFrom { get; set; }
+        public string AnValue { get; set; }
+        public double MuRange { get; set; }
+        public double MuRate { get; set; }
 
         public override string ToString()
         {
@@ -1156,16 +1254,21 @@
             v.ObjType = this.ObjType;
             v.logicType = this.logicType;
             v.LogicValue = this.LogicValue;
-            v.LogicValues = this.LogicValues;
+            v.LogicValues = this.LogicValues.ToArray();
+            v.Values_Set = this.Values.ToArray();
             v.小数位数 = this.小数位数;
+            v.defaultExpress=this.defaultExpress;
+            v.sort = this.sort;
+            v.isInput = this.isInput;
             v.virturl = this.virturl;
-            v.doubleRange =new DRange( this.doubleRange);
+            if (this.doubleRange == null) v.doubleRange = null;
+            else v.doubleRange =new DRange( this.doubleRange);
             v.accuracy = this.accuracy;
             //v.Vars = this.Vars;
             v.Name = this.Name;
             v.modelObjectID = this.modelObjectID;
             v.modelIndex = this.modelIndex;
-            v.PeriodCount = this.PeriodCount;
+            //v.PeriodCount = this.PeriodCount;
             v.childSolution = this.childSolution;
             v.childTag = this.childTag;
             v.childType = this.childType;
@@ -1174,6 +1277,10 @@
             v.objListString = this.objListString;
             v.obj_Best=this.obj_Best;
             v._level=this._level;
+            v.AnFrom=this.AnFrom;
+            v.AnValue=this.AnValue;
+            v.MuRange=this.MuRange;
+            v.MuRate=this.MuRate;
             return v;
         }
     }

--
Gitblit v1.9.3