| | |
| | | using System.Xml.Linq; |
| | | using Hydro.HydraulicModel; |
| | | using Hydro.CommonBase; |
| | | using Hydro.CodeProvider; |
| | | using Hydro.CodeProvider; |
| | | using System.Runtime.CompilerServices; |
| | | |
| | | namespace Hydro.HydraulicOptimizer |
| | | { |
| | |
| | | { |
| | | 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); |
| | | } |
| | |
| | | |
| | | 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(); |
| | | |
| | | |
| | |
| | | } |
| | | 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)) |
| | | { |
| | |
| | | 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)); |
| | | } |
| | |
| | | } |
| | | 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的logicType判断是哪种变量 |
| | | 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, |
| | |
| | | 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) |
| | |
| | | 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; |
| | |
| | | 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(); |