cloudflight
2024-07-27 49eb2a09634e439090d4a2b13ec7d6f911d3deaf
Hydraulic/Hydro.HydraulicOptimizer/WDNModelOptimizer.cs
@@ -16,6 +16,7 @@
using Hydro.CommonBase; 
using Hydro.CodeProvider;
using System.Runtime.CompilerServices;
using System.Collections.Concurrent;
namespace Hydro.HydraulicOptimizer
{
@@ -161,9 +162,11 @@
                }
            }
#if DEBUG
            Eval.本地项目编译 = gParam.GlobalConfig.Ext.本地项目编译;
#endif
            if ((result = loadGeneticParam(onFinished, onError, onReportProgress, SetVars)) != null) { return result; }
            if ((result = gParam.Load_Settings(param,onFinished, onError, onReportProgress, SetVars)) != null) { return result; }
            if (isGUILoading) return null;
@@ -173,7 +176,7 @@
            
            if ((result = controllerInit(isGUI, wParam)) != null) return result;
            if ((result = load试算参数_gParam()) != null) return result;
            if ((result = gParam.Load_GeneticSettings(param)) != null) return result;
            //StartChild
            lock (GlobalProgress.Instance)
            {
@@ -708,7 +711,7 @@
                workerThread.Abort();
            StopRunQuene();
            if (Quene.RequestQueues!=null) Quene.RequestQueues.Clear();
            if (Quene.MissionQuene!=null)Quene.MissionQuene.Clear();
            if (Quene.MissionQuene != null) Quene.MissionQuene = new ConcurrentQueue<calcParam>();//.Clear();
            if (CSM != null)
            {
                foreach (var name in ChildSolutionName)
@@ -735,8 +738,8 @@
                EPAController.Close();
            }
            
            if (workerThread!=null)
            workerThread.Abort();
            //if (workerThread!=null)
            //workerThread.Abort();
            
            
            if (CSM!=null)
@@ -813,6 +816,7 @@
                {
                    var accuracy = double.Parse(host.Ext.accurary == "" ? "0.1" : host.Ext.accurary);
                    var doubleRange = new DRange(double.Parse(host.Expression.Split(',')[0]), double.Parse(host.Expression.Split(',')[1]));
                    host.Ext.doubleRange = doubleRange;
                    var v = GetVarByHost(host, accuracy, doubleRange);
                    param.试算变量.Add(v);
                }
@@ -907,6 +911,7 @@
                        //    sort = o.Sort,
                        //}
                    };
                    param.表达式.Add(v);
                    param.OFunction = OFunction;
                }
                
@@ -969,54 +974,54 @@
            }
            return null;
        }
        public static string UpdateExpress(LogicModelParams param, HashSet<int> list_express)
        {
            //if (host.ExpressionType == "表达式计算")
            return HydraulicHelper.CreateExpressAssembly(param);
            //var resultBuilder=new StringBuilder();
            //var flag=true;
            //param.表达式.ForEach(s =>
            //{
            //    string text = HydraulicHelper.GetExVarText(s, param, result);
            //    if (!list_express.Contains(s.ID)) return;
            //    string text = HydraulicHelper.GetExVarText(s, param, resultBuilder);
            //    if (resultBuilder.Length != 0)
            //    {
            //        flag = false;
            //        return;
            //    }
            //    try
            //    {
            //        Eval val = new Hydro.CodeProvider.Eval(text, s.Vars.Count);
            //        if (!val.isInit) { result.AppendLine($"表达式[{s.Name}]公式错误," + val.errString); }
            //        if (!val.isInit) { resultBuilder.AppendLine($"表达式[{s.Name}]公式错误," + val.errString); }
            //        s.Eval = val;
            //        s.Value_Set(param.Period, double.NaN);
            //    }
            //    catch (Exception ee)
            //    catch (Exception e)
            //    {
            //        { result.AppendLine(ee.Message); }
            //        resultBuilder.AppendLine(e.Message);
            //        flag = false;
            //        return;
            //    }
            //}
            var resultBuilder=new StringBuilder();
            var flag=true;
            param.表达式.ForEach(s =>
            {
                if (!list_express.Contains(s.ID)) return;
                string text = HydraulicHelper.GetExVarText(s, param, resultBuilder);
                if (resultBuilder.Length != 0)
                {
                    flag = false;
                    return;
                }
                try
                {
                    Eval val = new Hydro.CodeProvider.Eval(text, s.Vars.Count);
                    if (!val.isInit) { resultBuilder.AppendLine($"表达式[{s.Name}]公式错误," + val.errString); }
                    s.Eval = val;
                    s.Value_Set(param.Period, double.NaN);
                }
                catch (Exception e)
                {
                    resultBuilder.AppendLine(e.Message);
                    flag = false;
                    return;
                }
            });
            //});
    
           
            //param.预处理插件.ForEach(v =>
            //{
            param.表达式.Sort((a, b) => a.Level > b.Level ? 1 : (a.Level == b.Level ? 0 : -1));
            return resultBuilder.ToString();
            //    //BookMark    :处理预处理插件
            //    if (!flag) return;
            //    Eval val;
            //    val = new Eval(v.modelObjectID, EvalTemplate.TempCode_PreTreat);
            //    if (val.errString != null) { resultBuilder.AppendLine( $"预处理插件[{v.Name}]函数编写错误," + val.errString); flag = false; return; }
            //    v.Eval = val;
            //});
            //param.表达式.Sort((a, b) => a.Level > b.Level ? 1 : (a.Level == b.Level ? 0 : -1));
            //return resultBuilder.ToString();
        }
        public static HashSet<int> UpdateVarByHost(variable s,LogicPoint host,LogicModelParams param,bool isDelete=false)
        {
@@ -1088,7 +1093,7 @@
            
            if (host.ExpressionType == "表达式计算")
            if (host.LogicType == "表达式" || host.LogicType=="预处理插件")
            {
                list_express.Add(s.ID);
            }
@@ -1166,6 +1171,12 @@
                logicType = host.LogicType,
                accuracy = accuracy,
                doubleRange = doubleRange,
                isInput = variable.list_输入值.Contains(host.ExpressionType),
                AnFrom  = host.Ext.AnFrom,
                AnValue =host.Ext.AnValue,
                MuRange =host.Ext.MuRange,
                MuRate =host.Ext.MuRate,
            };
          
@@ -1175,75 +1186,100 @@
        public static void UpdateVarByHost(variable v,LogicPoint host, double? accuracy = null, DRange doubleRange = null)
        {
            double d;
            var s= GetVarByHost(host, accuracy, doubleRange);
            v.ID = s.ID;
            v.modelObjectID = s.modelObjectID;
            v.IndicatorType = s.IndicatorType;
            v.Type = s.Type;
            v.Name = s.Name;
            v.expressType = s.expressType;
            v.expressString = s.expressString;
            v.Tag = s.Tag;
            v.arrgFuncType = s.arrgFuncType;
            v.defaultExpress = s.defaultExpress;
            v.isNode = s.isNode;
            v.ObjType = s.ObjType;
            v.小数位数 = s.小数位数;
            v.childSolution = s.childSolution;
            v.childTag = s.childTag;
            v.childType = s.childType;
            v.SearchRange = s.SearchRange;
            v.isNeedToSave = s.isNeedToSave;
            v.SaveKey = s.SaveKey;
            v.sort = s.sort;
            v.logicType = s.logicType;
            v.accuracy = s.accuracy;
            v.doubleRange = s.doubleRange;
            v.isInput = s.isInput;
            v.AnFrom = s.AnFrom;
            v.AnValue = s.AnValue;
            v.MuRange = s.MuRange;
            v.MuRate = s.MuRate;
            /*[Cloudflight修改]2024-5-21 
             * 迁移方法--集合
             */
            if (host.LogicType == "集合")
            {
                List<string> list = new List<string>() { "最大值", "最小值", "平均值", "累计值", "计数" };
                if (list.IndexOf(host.ExpressionType) >= 0)
                {
                    host.accurary = host.ExpressionType;
                    host.ExpressionType = "计算值";
                }
            }
            //if (host.LogicType == "集合")
            //{
            //    List<string> list = new List<string>() { "最大值", "最小值", "平均值", "累计值", "计数" };
            //    if (list.IndexOf(host.ExpressionType) >= 0)
            //    {
            //        host.accurary = host.ExpressionType;
            //        host.ExpressionType = "计算值";
            //    }
            //}
            //if (!GlobalModel.dict_StringToType.ContainsKey(host.IndicatorType)) GlobalModel.dict_StringToType[host.IndicatorType] = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).Type;
            //var VarType = GlobalModel.dict_StringToType[host.IndicatorType];
            //if (!GlobalModel.dict_StringToObjType.ContainsKey(host.Ext.ObjectType)) GlobalModel.dict_StringToObjType[host.Ext.ObjectType] = (int)(GlobalModel.DEFINE_OBJECT.Find(obj => obj.Name == host.Ext.ObjectType) ?? new TypeObject()).Type;
            //int ObjType =GlobalModel.dict_StringToObjType[host.Ext.ObjectType];
            //if (!GlobalModel.dict_StringToisNode.ContainsKey(host.IndicatorType)) GlobalModel.dict_StringToisNode[host.IndicatorType] = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).isNode;
            //bool isNode = GlobalModel.dict_StringToisNode[host.IndicatorType];
            //if (!GlobalModel.dict_StringToFuncType.ContainsKey(host.Ext.accurary)) GlobalModel.dict_StringToFuncType[host.Ext.accurary] = GlobalModel.DEFIND_AGGFUNC.FindIndex(func => func.Name == host.Ext.accurary);
            //int arrgFuncType = GlobalModel.dict_StringToFuncType[host.Ext.accurary];
            dynamic VarType = HydraulicInputType.None;
            if (GlobalModel.dict_StringToType.TryGetValue(host.IndicatorType, out dynamic vartype))
                VarType = vartype;
            int ObjType = 0;
            if (GlobalModel.dict_StringToObjType.TryGetValue(host.Ext.ObjectType, out int objType))
                ObjType = objType;
            //dynamic VarType = HydraulicInputType.None;
            //if (GlobalModel.dict_StringToType.TryGetValue(host.IndicatorType, out dynamic vartype))
            //    VarType = vartype;
            bool isNode = true;
            if (GlobalModel.dict_StringToisNode.TryGetValue(host.IndicatorType, out bool isnode))
                isNode = isnode;
            //int ObjType = 0;
            //if (GlobalModel.dict_StringToObjType.TryGetValue(host.Ext.ObjectType, out int objType))
            //    ObjType = objType;
            int arrgFuncType = -1;
            if (GlobalModel.dict_StringToFuncType.TryGetValue(host.Ext.accurary, out int FuncInt))
                arrgFuncType = FuncInt;
            //bool isNode = true;
            //if (GlobalModel.dict_StringToisNode.TryGetValue(host.IndicatorType, out bool isnode))
            //    isNode = isnode;
            v.ID = host.Id;
            v.modelObjectID = host.ModelObjectID;
            v.IndicatorType = host.IndicatorType;
            v.Type = VarType;
            v.Name = host.Name;
            v.expressType = host.ExpressionType;
            v.expressString = host.Expression;
            v.Tag = host.Ext.Tag;
            v.arrgFuncType = arrgFuncType;
            v.defaultExpress = host.Ext.accurary;
            v.isNode = isNode;
            v.ObjType = (int)ObjType;
            v.小数位数 = host.Ext.小数位数;
            v.childSolution = host.Ext.childSolution;
            v.childTag = host.Ext.childTag;
            v.childType = host.Ext.childType;
            v.SearchRange = double.TryParse(host.Ext.SaveAcc, out d) ? d : 0;
            v.isNeedToSave = host.Ext.isSave == "是" ? true : false;
            v.SaveKey = host.Ext.SaveRange;
            v.sort = host.Sort;
            v.logicType = host.LogicType;
            v.accuracy = accuracy;
            v.doubleRange = doubleRange;
            //int arrgFuncType = -1;
            //if (GlobalModel.dict_StringToFuncType.TryGetValue(host.Ext.accurary, out int FuncInt))
            //    arrgFuncType = FuncInt;
            //v.ID = host.Id;
            //v.modelObjectID = host.ModelObjectID;
            //v.IndicatorType = host.IndicatorType;
            //v.Type = VarType;
            //v.Name = host.Name;
            //v.expressType = host.ExpressionType;
            //v.expressString = host.Expression;
            //v.Tag = host.Ext.Tag;
            //v.arrgFuncType = arrgFuncType;
            //v.defaultExpress = host.Ext.accurary;
            //v.isNode = isNode;
            //v.ObjType = (int)ObjType;
            //v.小数位数 = host.Ext.小数位数;
            //v.childSolution = host.Ext.childSolution;
            //v.childTag = host.Ext.childTag;
            //v.childType = host.Ext.childType;
            //v.SearchRange = double.TryParse(host.Ext.SaveAcc, out d) ? d : 0;
            //v.isNeedToSave = host.Ext.isSave == "是" ? true : false;
            //v.SaveKey = host.Ext.SaveRange;
            //v.sort = host.Sort;
            //v.logicType = host.LogicType;
            //v.accuracy = accuracy;
            //v.doubleRange = doubleRange;
            //v.isInput = variable.list_输入值.Contains(host.ExpressionType);
            //v.AnFrom = host.Ext.AnFrom;
            //v.AnValue=host.Ext.AnValue;
            //v.MuRange=host.Ext.MuRange;
            //v.MuRate=host.Ext.MuRate;
        }
        private string loadGeneticParam(FinishEvent onFinished, FinishEvent onError, FinishEvent onReportProgress, SetEvent  SetVarsMethod)
@@ -1253,29 +1289,19 @@
            //DLengths.ForEach(m => chromosomeLength += m);
#if DEBUG
            Eval.本地项目编译=gParam.GlobalConfig.Ext.本地项目编译;
#endif
            gParam.populationSize = gParam.GlobalConfig.Ext.populationNum;
            gParam.iterations = gParam.GlobalConfig.Ext.iterationNum;
            gParam.populationSize_full = gParam.GlobalConfig.Ext.populationNum_full;
            gParam.populationSize_min = gParam.GlobalConfig.Ext.populationNum_full;
            gParam.iterations_full = gParam.GlobalConfig.Ext.iterationNum_full;
            gParam.selectionMethod = 0;
            gParam.optimizationMode = param.OFunction.ExpressType=="最大"?1:0;
            gParam.强制重计算 = gParam.GlobalConfig.Ext.是否优选方案;
            gParam.iterations_min = gParam.GlobalConfig.Ext.iterationNum_Min;
            //gParam.MultiTimes = gParam.GlobalConfig.Ext.MultiTimes;
            //gParam.accuracyNum = accuracyNum;
            //gParam.onReportProgress += OnReportProgress;
            //gParam.onFinished += ControlunLocking;
            //gParam.onError += ControlunLocking;
            //EPAController = new EPAParrelController();
            //EPAController.gParam = gParam;
            //Ehelper.SearchSolution();
            gParam.onReportProgress = onReportProgress;
            gParam.onFinished = onFinished;
            gParam.onError = onError;
@@ -1289,19 +1315,8 @@
                {
                    string savePath = GlobalPath.savePath + $@"\{solution.存储位置}";
                    var settings = gParam.GlobalConfig.saveSettings[key] = solution.settings;
                    //kp.Value.saveSettings = solution.settings;
                    settings.savePath = savePath;
                    //if (!isGUILoading)
                    //{
                    //    if (File.Exists(savePath))
                    //    {
                    //        settings.saveDatas = Base64Helper.ReadCompressedBase64FromFile<SaveDict>(savePath);
                    //    }
                    //    else
                    //    {
                    //        settings.saveDatas = new SaveDict();
                    //    }
                    //}
                    
                }
            });
@@ -1314,41 +1329,9 @@
            
        }
        private string load试算参数_gParam()
        {
            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);
            });
            gParam.ranges = ranges;
            gParam.DLengths = DLengths;
            gParam.accuracys = accuracys;
            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;
        }
       
        private string controllerInit(bool isGUI=false,WdnmoParam wParam=null)
        {
@@ -1387,6 +1370,8 @@
            return null;
        }
    }