From 0340a1bb6012a97de0e029b26b5c882472749050 Mon Sep 17 00:00:00 2001
From: cloudflight <cloudflight@126.com>
Date: 星期六, 06 七月 2024 10:28:01 +0800
Subject: [PATCH] [增强]添加优化计算库

---
 Hydraulic/Hydro.CommonBase/ParamModel.cs |  141 ++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 114 insertions(+), 27 deletions(-)

diff --git a/Hydraulic/Hydro.CommonBase/ParamModel.cs b/Hydraulic/Hydro.CommonBase/ParamModel.cs
index f92314d..a8f2964 100644
--- a/Hydraulic/Hydro.CommonBase/ParamModel.cs
+++ b/Hydraulic/Hydro.CommonBase/ParamModel.cs
@@ -36,12 +36,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 +117,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;
 
@@ -101,17 +171,16 @@
         public GeneticParams DeepCopy()
         {
             var copy = new GeneticParams(this.Level);
-            copy.list_BestEnerge = this.list_BestEnerge?.ToList();
-            copy.list_AvgHz = this.list_AvgHz?.ToList();
+            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.Dependencies = this.Dependencies?.ToList();
-            copy.list_AtemtpArr = this.list_AtemtpArr?.ToArray();
+            
+            //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.populationSize_min = this.populationSize_min;
             copy.iterations_full = this.iterations_full;
             copy.Upopulations = this.Upopulations?.ToList();
             copy.selectionMethod = this.selectionMethod;
@@ -130,7 +199,7 @@
             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.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;
@@ -451,6 +520,8 @@
 
         public List<SetVar> setVars = new List<SetVar>();
 
+        public int CurrentIndex = -1;
+
     }
     public class SetVar
     {
@@ -543,6 +614,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 +631,7 @@
                 return all;
             }
         }
+        [JsonIgnore]
         public List<variable> 接口变量
         {
             get
@@ -570,6 +645,7 @@
                 return all;
             }
         }
+        [JsonIgnore]
         public List<variable> 设定变量
         {
             get
@@ -580,7 +656,7 @@
                 return all;
             }
         }
-        
+        [JsonIgnore]
         public List<variable> 变量全集
         {
             get
@@ -607,6 +683,7 @@
         //[NonSerialized]
         [JsonIgnore]
         public Dictionary<int, variable> dict_ID { get; set; }
+        [JsonIgnore]
         public Dictionary<string, variable> dict_Tag { get; private set; }
         
         //[NonSerialized]
@@ -645,6 +722,14 @@
                     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 (vv.IndicatorType=="方案筛选器")
+                    ChromeFilter = vv;
+                if (vv.IndicatorType == "基因筛选器")
+                    GeneFilter = vv;
             });
 
         }
@@ -731,21 +816,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>() { "基准值(界面)", "定义值", "基准值(接口)", "模式系数(界面)", "模式系数(接口)" };
@@ -826,12 +912,10 @@
         public int arrgFuncType;
         public string defaultExpress;
         private int _indicatorType;
-      
         public string IndicatorType 
         { 
             get
             {
-                
                 if (_indicatorType<0)
                 {
                     return list_indicatorTypeName[0];
@@ -867,7 +951,7 @@
             {
                 return _Values;
             }
-            private set
+            set
             {
                 _Values = value;
             }
@@ -913,7 +997,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 +1108,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;

--
Gitblit v1.9.3