From 9490957edfc694a0378ee52af47e921b8ac44e7a Mon Sep 17 00:00:00 2001
From: qin <a@163.com>
Date: 星期二, 04 六月 2024 09:12:11 +0800
Subject: [PATCH] 冲突

---
 Hydraulic/Hydro.HydraulicOptimizer/WDNModelOptimizer.cs |  375 +++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 307 insertions(+), 68 deletions(-)

diff --git a/Hydraulic/Hydro.HydraulicOptimizer/WDNModelOptimizer.cs b/Hydraulic/Hydro.HydraulicOptimizer/WDNModelOptimizer.cs
index 0567b7b..835c278 100644
--- a/Hydraulic/Hydro.HydraulicOptimizer/WDNModelOptimizer.cs
+++ b/Hydraulic/Hydro.HydraulicOptimizer/WDNModelOptimizer.cs
@@ -14,7 +14,8 @@
 using System.Xml.Linq; 
 using Hydro.HydraulicModel; 
 using Hydro.CommonBase; 
-using Hydro.CodeProvider; 
+using Hydro.CodeProvider;
+using System.Runtime.CompilerServices;
 
 namespace Hydro.HydraulicOptimizer
 {
@@ -680,7 +681,7 @@
         {
             return EPAController.SetResult(ID, code, value, isNode);
         }
-        public string SetValue(string ID, EPAModelInput.eType type, float value)
+        public string SetValue(string ID, HydraulicInputType type, float value)
         {
             return EPAController.SetResult(ID, type, value);
         }
@@ -758,8 +759,34 @@
 
         private string loadLogicParam()
         {
+
+            GlobalModel.dict_StringToType = new Dictionary<string, dynamic>();
+            GlobalModel.dict_StringToObjType = new Dictionary<string, int>();
+            GlobalModel.dict_StringToisNode = new Dictionary<string, bool>();
+            GlobalModel.dict_StringToFuncType = new Dictionary<string, int>();
+            GlobalModel.DEFINE_INDICATOR.ForEach(ind =>
+            {
+                GlobalModel.dict_StringToType[ind.Name] = ind.Type;
+                GlobalModel.dict_StringToisNode[ind.Name] = ind.isNode;
+            });
+            GlobalModel.DEFINE_OBJECT.ForEach(obj =>
+            {
+                GlobalModel.dict_StringToObjType[obj.Name] = (int)obj.Type;
+            });
+            for(int i=0;i< GlobalModel.DEFIND_AGGFUNC.Count;i++)
+            {
+                var func = GlobalModel.DEFIND_AGGFUNC[i];
+                GlobalModel.dict_StringToFuncType[func.Name] = i;
+            }
+
+            //if (!GlobalModel.dict_StringToType.ContainsKey(host.IndicatorType)) GlobalModel.dict_StringToType[host.IndicatorType] = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).Type;
+            //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;
+            //if (!GlobalModel.dict_StringToisNode.ContainsKey(host.IndicatorType)) GlobalModel.dict_StringToisNode[host.IndicatorType] = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).isNode;
+            //if (!GlobalModel.dict_StringToFuncType.ContainsKey(host.Ext.accurary)) GlobalModel.dict_StringToFuncType[host.Ext.accurary] = GlobalModel.DEFIND_AGGFUNC.FindIndex(func => func.Name == host.Ext.accurary);
+
             //string result = null;
             //(LogicPoint)((LogicTile)sender).Tag;
+            variable.list_indicatorTypeName=GlobalModel.DEFINE_INDICATOR.Select(ind => ind.Name).ToList();
             param = new LogicModelParams();
 
 
@@ -782,10 +809,15 @@
             }
             foreach (var host in hosts.Where(x => x.LogicType == "璇曠畻鍙傛暟" && x.ParentId != -1 && x.Ext.Enable).OrderBy(x => x.Sort))
             {
-                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]));
-                var v = GetVarByHost(host, accuracy, doubleRange);
-                param.璇曠畻鍙橀噺.Add(v);
+                try
+                {
+                    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]));
+                    var v = GetVarByHost(host, accuracy, doubleRange);
+                    param.璇曠畻鍙橀噺.Add(v);
+                }
+                catch { }
+                
             }
             foreach (var host in hosts.Where(x => x.LogicType == "姘撮噺鍒嗛厤"  && x.ParentId != -1 && x.Ext.Enable).OrderBy(x => x.Sort))
             {
@@ -802,7 +834,7 @@
                 param.绾︽潫鍙橀噺.Add(GetVarByHost(host));
             }
 
-            foreach (var host in hosts.Where(x => (x.LogicType == "闆嗗悎" || x.LogicType == "閫夋嫨闆�" && x.Ext.Enable) && x.ParentId != -1).OrderBy(x=>x.Sort))
+            foreach (var host in hosts.Where(x => ((x.LogicType == "闆嗗悎" || x.LogicType == "閫夋嫨闆�") && x.Ext.Enable) && x.ParentId != -1).OrderBy(x=>x.Sort))
             {
                 param.闆嗗悎.Add(GetVarByHost(host));
             }
@@ -937,73 +969,192 @@
             }
             return null;
         }
-        public static void UpdateVarByHost(variable s,LogicPoint host,LogicModelParams param)
+        public static string UpdateExpress(LogicModelParams param, HashSet<int> list_express)
         {
-            StringBuilder result = new StringBuilder();
-            var v = GetVarByHost(host);
-            //鎸夌収v鏇存柊s
-            s.ID = v.ID;
-            s.modelObjectID = v.modelObjectID;
-            s.IndicatorType = v.IndicatorType;
-            s.Type = v.Type;
-            s.isNode = v.isNode;
-            s.Name = v.Name;
-            s.ObjType = v.ObjType;
-            s.灏忔暟浣嶆暟 = v.灏忔暟浣嶆暟;
-            s.SearchRange = v.SearchRange;
-            s.isNeedToSave = v.isNeedToSave;
-            s.SaveKey = v.SaveKey;
-            s.expressType = v.expressType;
-            s.sort = v.sort;
-            s.logicType = v.logicType;
-            s.accuracy = v.accuracy;
-            s.doubleRange = v.doubleRange;
+            //if (host.ExpressionType == "琛ㄨ揪寮忚绠�")
+            //{
+            //    string text = HydraulicHelper.GetExVarText(s, param, result);
+            //    try
+            //    {
+            //        Eval val = new Hydro.CodeProvider.Eval(text, s.Vars.Count);
+            //        if (!val.isInit) { result.AppendLine($"琛ㄨ揪寮廩{s.Name}]鍏紡閿欒锛�" + val.errString); }
+            //        s.Eval = val;
+            //        s.Value_Set(param.Period, double.NaN);
+            //    }
+            //    catch (Exception ee)
+            //    {
+            //        { result.AppendLine(ee.Message); }
+            //    }
 
-            if (host.ExpressionType == "琛ㄨ揪寮忚绠�")
+            //}
+            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.CalcValue_Set = null;
+                    s.Value_Set(param.Period, double.NaN);
                 }
-                catch (Exception ee)
+                catch (Exception e)
                 {
-                    { result.AppendLine(ee.Message); }
+                    resultBuilder.AppendLine(e.Message);
+                    flag = false;
+                    return;
+                }
+            });
+    
+           
+
+            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)
+        {
+            HashSet<int> list_express = new HashSet<int>();
+            StringBuilder result = new StringBuilder();
+            //
+            if (isDelete)
+            {
+                var v = GetVarByHost(host);
+                switch (v.logicType)
+                {
+                    case "鍙橀噺":
+                        param.鍙橀噺.Remove(s);
+                        break;
+                    case "璇曠畻鍙傛暟":
+                        param.璇曠畻鍙橀噺.Remove(s);
+                        break;
+                    case "姘撮噺鍒嗛厤":
+                        param.姘撮噺鍒嗛厤鍙橀噺.Remove(s);
+                        break;
+                    case "绾︽潫鏉′欢":
+                        param.绾︽潫鍙橀噺.Remove(s);
+                        break;
+                    case "闆嗗悎":
+                        param.闆嗗悎.Remove(s);
+                        break;
+                    case "琛ㄨ揪寮�":
+                        param.琛ㄨ揪寮�.Remove(s);
+                        break;
+                    case "棰勫鐞嗘彃浠�":
+                        param.棰勫鐞嗘彃浠�.Remove(s);
+                        break;
                 }
                 
             }
-            else if (host.ExpressionType=="鍩哄噯鍊�(鐣岄潰)")
+            if (s==null)
+            {
+                s = GetVarByHost(host);
+                //鏍规嵁s鐨刲ogicType鍒ゆ柇鏄摢绉嶅彉閲�
+                switch(s.logicType)
+                {
+                    case "鍙橀噺":
+                        param.鍙橀噺.Add(s);
+                        break;
+                    case "璇曠畻鍙傛暟":
+                        param.璇曠畻鍙橀噺.Add(s);
+                        break;
+                    case "姘撮噺鍒嗛厤":
+                        param.姘撮噺鍒嗛厤鍙橀噺.Add(s);
+                        break;
+                    case "绾︽潫鏉′欢":
+                        param.绾︽潫鍙橀噺.Add(s);
+                        break;
+                    case "闆嗗悎":
+                        param.闆嗗悎.Add(s);
+                        break;
+                    case "琛ㄨ揪寮�":
+                        param.琛ㄨ揪寮�.Add(s);
+                        break;
+                    case "棰勫鐞嗘彃浠�":
+                        param.棰勫鐞嗘彃浠�.Add(s);
+                        break;
+                }
+            }
+            else
+            {
+                UpdateVarByHost(s, host);
+            }
+            
+
+
+            if (host.ExpressionType == "琛ㄨ揪寮忚绠�")
+            {
+                list_express.Add(s.ID);
+            }
+            else
+            if (host.ExpressionType=="鍩哄噯鍊�(鐣岄潰)")
             {
                 if ( double.TryParse( host.Expression,out double d)) s.LogicValue =d;
             }
             else if (host.ExpressionType == "妯″紡绯绘暟(鐣岄潰)")
             {
-                s.LogicPattern= host.Expression.Split(',').ToList().Select(str => { if (double.TryParse(str, out double dd)) return dd; else return 0; }).ToArray();
-                
+                s.LogicValues= host.Expression.Split(',').ToList().Select(str => { if (double.TryParse(str, out double dd)) return dd; else return 0; }).ToArray();
+                s.Values_Set = s.LogicValues;
             }
-            else
-            {
-                s.CalcValue_Set = null;
-            }
+            return list_express;
         }
         public static variable GetVarByHost( LogicPoint host,double? accuracy=null,DRange doubleRange=null)
         {
             double d;
-            return new variable()
+            /*[Cloudflight淇敼]2024-5-21 
+             * 杩佺Щ鏂规硶--闆嗗悎
+             */
+
+
+
+            
+
+
+            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;
+
+
+            bool isNode = true;
+            if (GlobalModel.dict_StringToisNode.TryGetValue(host.IndicatorType, out bool isnode))
+                isNode = isnode;
+
+
+            int arrgFuncType = -1;
+            if (GlobalModel.dict_StringToFuncType.TryGetValue(host.Ext.accurary, out int FuncInt))
+                arrgFuncType=FuncInt;
+
+
+
+            
+
+            var v = new variable()
             {
                 ID = host.Id,
                 modelObjectID = host.ModelObjectID,
                 IndicatorType = host.IndicatorType,
-                Type = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).EpaType,
+                Type = VarType,
+                arrgFuncType = arrgFuncType,
+                ObjType = (int)ObjType,
                 Name = host.Name,
                 expressType = host.ExpressionType,
                 expressString = host.Expression,
-                defineType = host.Ext.accurary,
-                isNode = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).isNode,
-                ObjType = (int)(GlobalModel.DEFINE_OBJECT.Find(obj => obj.Name == host.Ext.ObjectType) ?? new TypeObject()).EPAObjType,
+                Tag = host.Ext.Tag,
+                
+                defaultExpress = host.Ext.accurary,
+                isNode = isNode,
+                
                 灏忔暟浣嶆暟 = host.Ext.灏忔暟浣嶆暟,
                 childSolution = host.Ext.childSolution,
                 childTag = host.Ext.childTag,
@@ -1012,11 +1163,87 @@
                 isNeedToSave = host.Ext.isSave == "鏄�" ? true : false,
                 SaveKey = host.Ext.SaveRange,
                 sort = host.Sort,
-
-                logicType =host.LogicType,
+                logicType = host.LogicType,
                 accuracy = accuracy,
-                doubleRange=doubleRange,
+                doubleRange = doubleRange,
             };
+
+          
+            
+            return v;
+        }
+        public static void UpdateVarByHost(variable v,LogicPoint host, double? accuracy = null, DRange doubleRange = null)
+        {
+            double d;
+            /*[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 (!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;
+
+
+            bool isNode = true;
+            if (GlobalModel.dict_StringToisNode.TryGetValue(host.IndicatorType, out bool isnode))
+                isNode = isnode;
+
+
+            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;
         }
 
         private string loadGeneticParam(FinishEvent onFinished, FinishEvent onError, FinishEvent onReportProgress, SetEvent  SetVarsMethod)
@@ -1093,7 +1320,12 @@
             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); });
+            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;
@@ -1103,36 +1335,43 @@
         int getDLength(DRange dr, double? accur)
         {
             if (accur == null) accur = 0.1;
-            var num_Total = dr.Length / accur;
+            var num_Total = dr.Length / (double)accur;
             var Dlength = 0;
             var value = 1;
             var i = 0;
-            while (value <= num_Total && i < 1000)
-            {
-                value *= 2;
-                Dlength += 1;
-                i++;
-            }
+            //鍒ゆ柇闇�瑕佸灏戜綅浜岃繘鍒舵暟
+            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)
         {
             string result = null;
-            string inp_file;
-            if (string.IsNullOrEmpty(ModelPath))
+            string inp_file=null;
+            if (gParam.GlobalConfig.Ext.isWaterModelCalc)
             {
-                inp_file = (gParam.GlobalConfig.ModelFilePath[0] == '\\' ? GlobalPath.Path : "") + gParam.GlobalConfig.ModelFilePath; //System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), @"\model\pump.inp");
+                if (string.IsNullOrEmpty(ModelPath))
+                {
+                    inp_file = (gParam.GlobalConfig.ModelFilePath[0] == '\\' ? GlobalPath.Path : "") + gParam.GlobalConfig.ModelFilePath; //System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), @"\model\pump.inp");
 
-            }
-            else
-            {
-                inp_file = ModelPath;
-            }
-            if (!System.IO.File.Exists(inp_file))
-            {
-                //MessageBox.Show();
-                return "鏂囦欢鏈壘鍒癨r\n" + inp_file;
+                }
+                else
+                {
+                    inp_file = ModelPath;
+                }
+                if (!System.IO.File.Exists(inp_file))
+                {
+
+                    //MessageBox.Show();
+                    return "鏂囦欢鏈壘鍒癨r\n" + inp_file;
+                }
+
             }
 
             EPAController = new EPAParrelController();

--
Gitblit v1.9.3