using Hydro.HydraulicHelperNS;
|
|
using Hydro.ConfigModel;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using Hydro.ConfigModel;
|
using System.Threading;
|
using Dapper;
|
using Newtonsoft.Json;
|
using Hydro.ParrelControl;
|
using System.IO;
|
using System.Collections;
|
using System.Xml.Linq;
|
using System.Runtime.Remoting.Messaging;
|
using Hydro.HydraulicModel;
|
using System.Windows.Forms;
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
//using SolutionDBHelper_NS;
|
using Hydro.CommonBase;
|
using System.IO.Compression;
|
using System.Runtime.Serialization.Formatters.Binary;
|
//using Hydro.CommonBase.ExcelHelper;
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
|
using System.Collections.Concurrent;
|
using Hydro.CodeProvider;
|
//using AForge;
|
|
namespace Hydro.HydraulicOptimizer
|
{
|
public class ChildSolutionManager
|
{
|
public List<WDNModelOptimizer> WMOs = new List<WDNModelOptimizer>();
|
public System.Collections.Generic.Queue<WdnmoParam> RequestQueues;
|
public HashSet<Guid> RequestFinished;
|
}
|
|
/// <summary>
|
/// 解决方案计算器
|
/// </summary>
|
public class WDNModelOptimizer
|
{
|
|
public static List<WDNModelOptimizer> InstanceList=null;
|
public int ID { get; set; }
|
Quene Quene;
|
|
private Thread workerThread;
|
string ModelPath; int MaxThread = 8;
|
|
public FinishEvent onFinished;
|
public FinishEvent onError;
|
public FinishEvent onReportProgress;
|
|
public Dictionary<string, ChildSolutionManager> CSM;
|
public List<string> ChildSolutionName;
|
|
public List<Solution> ChildSolutions { get { return this.gParam.GlobalConfig.Ext.ChildSolutionArr.ToList(); } }
|
|
|
|
//bool Busy { get; set; } = false;
|
bool Enabled { get; set; } = true;
|
LogicModelParams param;
|
GeneticParams gParam;
|
WdnmoParam wParam;
|
public LogicModelParams getLogicParam()
|
{
|
return param;
|
}
|
|
bool showResult = false;
|
bool showRunner = false;
|
List<LogicPoint> hosts;
|
EPAParrelController EPAController;
|
|
public WdnmoParam Get设定清单()
|
{
|
return EPAController.helpers[0].Get设定清单();
|
}
|
|
public bool isStarted { get; set; } = false;
|
public bool isInited { get; set; } = false;
|
|
public string Message { get; set; } = null;
|
|
|
|
|
/// <summary>
|
/// 初始化配置,加载运算机器人
|
/// </summary>
|
/// <param name="ConfigPath">配置文件路径</param>
|
/// <param name="ModelPath">模型路径</param>
|
/// <param name="onFinished">回调函数:计算完成,回传参数类型为WdnmoParam,其中ResultPoints包含结果数值</param>
|
/// <param name="onError">回调函数:计算失败,回传参数类型为String失败原因</param>
|
/// <param name="onReportProgress">回调函数:计算进度,回传参数类型为GeneticParams,包括目标函数值、当前迭代次数、运算机器人状态</param>
|
public WDNModelOptimizer(GeneticParams gParam)
|
{
|
if (InstanceList == null) InstanceList = new List<WDNModelOptimizer>();
|
InstanceList.Add(this);
|
this.gParam = gParam;
|
}
|
/// <summary>
|
/// 初始化计算程序
|
/// </summary>
|
/// <param name="ConfigPath"></param>
|
/// <param name="ModelPath"></param>
|
/// <param name="onFinished"></param>
|
/// <param name="onError"></param>
|
/// <param name="onReportProgress"></param>
|
/// <returns></returns>
|
//public string Init(string ConfigPath = null,
|
// string ModelPath = null,
|
// FinishEvent onFinished = null,
|
// FinishEvent onError = null,
|
// FinishEvent onReportProgress = null
|
|
// )
|
//{
|
// string result = null;
|
// if (!string.IsNullOrEmpty(ConfigPath)) Db.Init(ConfigPath);
|
// this.ModelPath = ModelPath;
|
// //获取全局默认设置
|
// var globalConfig = Db.Connection.QueryFirstOrDefault<string>("SELECT FValue FROM MyConfig WHERE FKey='GlobalConfig'");
|
// GlobalConfig.Instance = JsonConvert.DeserializeObject<GlobalConfig>(globalConfig) ?? new GlobalConfig();
|
// if ((result = loadConfigToParam()) != null) return result;
|
// if ((result = loadCalcTogParam(onFinished, onError, onReportProgress)) != null) return result;
|
// if ((result = controllerInit()) != null) return result;
|
// isLoaded = true;
|
// return null;
|
//}
|
public string Init(string ConfigPath = null,
|
string ModelPath = null,
|
FinishEvent onFinished = null,
|
FinishEvent onError = null,
|
FinishEvent onReportProgress = null,
|
WdnmoParam param = null,
|
bool isGUI = false,
|
bool isGUILoading = false
|
)
|
{
|
//Log.Add("初始化开始");
|
string result = null;
|
//if(gParam.Level == 0)MyEval_tempCode.Clear();
|
gParam=new GeneticParams(ConfigPath,gParam);
|
this.ModelPath = ModelPath;
|
if (gParam.Level == 0)
|
{
|
Log.Enable = gParam.GlobalConfig.Ext.显示详细日志;
|
}
|
Quene = gParam.Quene;
|
if ((result = loadLogicParam()) != null) return result;
|
|
if (gParam.Level == 0 && !isGUILoading)
|
{
|
int count = 0;
|
if (this.param.ChildParam != null)
|
foreach (var kp in this.param.ChildParam)
|
{
|
Solution solution = ChildSolutions.Find(s => s.名称 == kp.Key);
|
count += solution.并行数;
|
}
|
|
lock (GlobalProgress.Instance)
|
{
|
GlobalProgress.MaxNum = 1 + count;
|
GlobalProgress.CurrentNum = 0;
|
}
|
|
}
|
|
|
if ((result = loadGeneticParam(onFinished, onError, onReportProgress, SetVars)) != null) { return result; }
|
|
|
if (isGUILoading) return null;
|
|
|
//Log.Add("参数读取完成");
|
if ((result = controllerInit(isGUI, param)) != null) return result;
|
|
if ((result = load试算参数_gParam()) != null) return result;
|
//StartChild
|
lock (GlobalProgress.Instance)
|
{
|
GlobalProgress.CurrentNum++;
|
}
|
|
if (this.param.ChildParam != null)
|
{
|
CSM = new Dictionary<string, ChildSolutionManager>();
|
ChildSolutionName = new List<string>();
|
foreach (var kp in this.param.ChildParam)
|
{
|
ChildSolutionName.Add(kp.Key);
|
var csParam = new ChildSolutionManager();
|
csParam.RequestQueues = new Queue<WdnmoParam>();
|
csParam.RequestFinished = new HashSet<Guid>();
|
csParam.WMOs = new List<WDNModelOptimizer>();
|
|
Solution solution = ChildSolutions.Find(s => s.名称 == kp.Key);
|
for (int i = 0; i < solution.并行数; i++)
|
{
|
var name = kp.Key;
|
GeneticParams gparam = new GeneticParams(gParam.Level + 1);
|
gparam.Db = new Db();
|
WDNModelOptimizer wmo = new WDNModelOptimizer(gparam);
|
csParam.WMOs.Add(wmo);
|
wmo.SetSaveName(kp.Value.childSaveName);
|
result = wmo.Init($@"{GlobalPath.configPath}\config_{name}.wdb", null, null, null, null, kp.Value.wParam, false);
|
|
if (result != null) return result;
|
wmo.Quene.RequestQueues = csParam.RequestQueues;
|
wmo.Quene.RequestFinished = csParam.RequestFinished;
|
}
|
CSM.Add(kp.Key, csParam);
|
string childSaveName = kp.Value.childSaveName;
|
|
#region bak
|
|
//初始化内存存储
|
if (gParam.GlobalConfig.Ext.启用内存存储)
|
{
|
string savePath = GlobalPath.savePath + $@"\{solution.存储位置}";
|
var settings = gParam.GlobalConfig.saveSettings[kp.Key] = solution.settings;
|
//kp.Value.saveSettings = solution.settings;
|
settings.savePath = savePath;
|
if (!isGUILoading)
|
{
|
if (File.Exists(savePath))
|
{
|
settings.saveDatas = MessageCompressHelper.ReadCompressedBase64FromFile<ConcurrentDictionary<ulong, double>>(savePath);
|
}
|
else
|
{
|
settings.saveDatas = new ConcurrentDictionary<ulong, double>();
|
}
|
}
|
|
}
|
//初始化内存存储
|
//if (gParam.GlobalConfig.Ext.启用内存存储)
|
//{
|
// string savePath = GlobalPath.savePath + $@"\{solution.存储位置}";
|
// var settings=gParam.GlobalConfig.saveSettings[kp.Key] = solution.settings;
|
// gParam.GlobalConfig.saveSettings[kp.Key].savePath = savePath;
|
// //kp.Value.saveSettings = solution.settings;
|
// //kp.Value.savePath = savePath;
|
// if (File.Exists(savePath))
|
// {
|
// //string stxt = File.ReadAllText(savePath);
|
// //Base64Helper.ReadCompressedBase64FromFile<List<KeyValuePair<ulong, double>>>(savePath).ToDictionaryEx(t => t.Key, t => t.Value);
|
// settings.saveDatas = Base64Helper.ReadCompressedBase64FromFile<ConcurrentDictionary<ulong, double>>(savePath);
|
// //try
|
// //{
|
// // settings.saveDatas = Base64Helper.ReadCompressedBase64FromFile<ConcurrentDictionary<ulong, double>>(savePath);
|
// //}
|
// //catch
|
// //{
|
// // settings.saveDatas= Base64Helper.ReadCompressedBase64FromFile<List<KeyValuePair<ulong, double>>>(savePath).ToDictionaryEx(t => t.Key, t => t.Value);
|
// //}
|
|
|
// //Base64Helper.ReadCompressedBase64FromFile<List<KeyValuePair<ulong, double>>>(savePath).ToDictionaryEx(t=>t.Key,t=>t.Value);
|
// }
|
// else
|
// {
|
// settings.saveDatas = new ConcurrentDictionary<ulong, double>();
|
// }
|
// //else
|
// //{
|
// // if (!gParam.GlobalConfig.saveSettings.ContainsKey(childSaveName))
|
// // gParam.GlobalConfig.saveSettings.Add(childSaveName, new SaveSettings());
|
// //}
|
|
// //if (File.Exists(savePath))
|
// //{
|
// // if (!gParam.GlobalConfig.saveSettings.ContainsKey(childSaveName))
|
// // gParam.GlobalConfig.saveSettings.Add(childSaveName,Base64Helper.ReadCompressedBase64FromFile<SaveSettings>(savePath));
|
// //}
|
// //else
|
// //{
|
// // if (!gParam.GlobalConfig.saveSettings.ContainsKey(childSaveName))
|
// // gParam.GlobalConfig.saveSettings.Add(childSaveName, new SaveSettings());
|
// //}
|
//}
|
#endregion
|
}
|
}
|
|
|
//初始化存储数据库
|
//if (gParam.GlobalConfig.Ext.启用数据库存储)
|
//{
|
// if (!SolutionDBHelper.isHostInited)
|
// SolutionDBHelper.Init("localhost", "selfAdpDb");
|
// string saveName = gParam.SolutionSaveName ?? gParam.GlobalConfig.ModelObjectID;
|
// gParam.SolutionDBHelper = new SolutionDBHelper(saveName);
|
// if (SolutionDBHelper.InstanceDict.ContainsKey(saveName)) SolutionDBHelper.InstanceDict.Remove(saveName);
|
// SolutionDBHelper.InstanceDict.Add(saveName, gParam.SolutionDBHelper);
|
//}
|
|
|
|
|
|
//Start();
|
this.isInited = true;
|
|
return null;
|
}
|
|
|
|
public string Start()
|
{
|
if (isStarted) return null;
|
isStarted=true;
|
string result = null;
|
if (CSM != null)
|
{
|
ChildSolutionName.ForEach(n =>
|
{
|
var wmos = CSM[n].WMOs;
|
wmos.ForEach(wmo =>
|
{
|
|
wmo.QueneRunnerEnable = true;
|
wmo.StartRunQuene();
|
wmo.Start();
|
});
|
|
});
|
}
|
|
|
EPAController.StartRobot();
|
EPAController.needToStop = false;
|
|
if (gParam.Level==0)Thread.Sleep(500);
|
return result;
|
}
|
|
|
|
|
|
/// <summary>
|
/// 启动计算
|
/// </summary>
|
/// <param name="wParam">计算参数</param>
|
/// <param name="showLog">显示日志</param>
|
/// <param name="CallType">调用类型:0是接口,1是界面</param>
|
/// <returns></returns>
|
public string Run(WdnmoParam wParam,bool showLog=true,int CallType=0)
|
{
|
|
Start();
|
//Log.Add($"子方案计算--①开始任务");
|
//ConsoleLog("输入参数:" + JsonConvert.SerializeObject(wParam.ScadaPoints));
|
string result = null;
|
EPAController.CallBackType = CallType;
|
|
|
|
var controller = this.EPAController;
|
|
|
|
|
|
|
|
|
|
if (wParam!=null)
|
{
|
this.wParam = wParam;
|
|
Quene.TimePoints = wParam.ScadaPoints;
|
|
|
|
|
param.预处理插件.ToList().ForEach(v =>
|
{
|
if (v.IndicatorType == "初始及运行传参" || v.IndicatorType == "运行传参")
|
v.Eval.ChangeParam(param, wParam, gParam,null);
|
});
|
|
|
|
|
|
|
|
controller.setwParam((WdnmoParam)wParam);
|
if ((result = controller.SetVals((WdnmoParam)wParam)) != null)
|
{
|
if (gParam.onError != null) gParam.onError(result);
|
return result;
|
}
|
}
|
|
workerThread = new Thread(controller.SearchSolution);
|
workerThread.Start();
|
|
return result;
|
}
|
|
|
/// <summary>
|
/// 启动计算
|
/// </summary>
|
/// <param name="wParam">计算参数</param>
|
/// <param name="showLog">显示日志</param>
|
/// <param name="CallType">调用类型:0是接口,1是界面</param>
|
/// <returns></returns>
|
public string Train(WdnmoParam wParam,bool showLog=true,int CallType=0)
|
{
|
|
Start();
|
//Log.Add($"子方案计算--①开始任务");
|
//ConsoleLog("输入参数:" + JsonConvert.SerializeObject(wParam.ScadaPoints));
|
string result = null;
|
EPAController.CallBackType = CallType;
|
|
|
|
var controller = this.EPAController;
|
|
if (wParam!=null)
|
{
|
this.wParam = wParam;
|
|
Quene.TimePoints = wParam.ScadaPoints;
|
|
|
|
|
param.预处理插件.ToList().ForEach(v =>
|
{
|
if (v.IndicatorType == "初始及运行传参" || v.IndicatorType == "运行传参")
|
v.Eval.ChangeParam(param, wParam, gParam,null);
|
});
|
|
|
|
|
|
|
|
controller.setwParam((WdnmoParam)wParam);
|
if ((result = controller.SetVals((WdnmoParam)wParam)) != null)
|
{
|
if (gParam.onError != null) gParam.onError(result);
|
return result;
|
}
|
}
|
|
workerThread = new Thread(controller.GenerateSaveSet);
|
workerThread.Start();
|
|
return result;
|
}
|
|
|
|
|
|
public int GetMinutesFromMonday(DateTime time)
|
{
|
DateTime now = time;
|
DayOfWeek dayOfWeek = now.DayOfWeek;
|
|
DateTime monday = now.Date.AddDays(-(int)dayOfWeek + 1);
|
DateTime thisMonday = new DateTime(monday.Year, monday.Month, monday.Day, 0, 0, 0);
|
|
TimeSpan ts = now - thisMonday;
|
|
return (int)ts.TotalMinutes;
|
}
|
|
|
/// <summary>
|
/// 桌面端界面调用
|
/// </summary>
|
/// <returns></returns>
|
public string Run()
|
{
|
Start();
|
EPAController.CallBackType = 1;
|
string result = null;
|
|
var controller = EPAController.Clone();
|
this.EPAController = controller;
|
workerThread = new Thread(controller.SearchSolution);
|
workerThread.Start();
|
return result;
|
}
|
#region 中间过程
|
public void SetVars(string name, WdnmoParam wParam)
|
{
|
wParam.fullSearchMode = true;
|
|
|
|
|
var ID = wParam.ID = Guid.NewGuid();
|
lock (CSM[name].RequestQueues)
|
{
|
CSM[name].RequestQueues.Enqueue(wParam);
|
//Log.Add($"中间列表添加任务,当前{CSM[name].RequestQueues.Count}个");
|
}
|
//while (!CSM[name].RequestFinished.Contains(ID)) ;
|
//if (gParam.Level > 0) Log.Add($"子方案计算--⑥结果返回(中间队列)");
|
|
}
|
|
System.Windows.Forms.Timer timer_requestQueneRunner;
|
public bool QueneRunnerEnable = false;
|
Thread t;
|
//private ManualResetEvent mre = new ManualResetEvent(true);
|
public void StartRunQuene()
|
{
|
//timer_requestQueneRunner = new System.Windows.Forms.Timer();
|
//timer_requestQueneRunner.Tick += OnQueneRunnerTick;
|
//timer_requestQueneRunner.Interval = 100;
|
//timer_requestQueneRunner.Start();
|
if (!QueneRunnerEnable) return;
|
t = new Thread(new ThreadStart(DoCalc));
|
t.Start();
|
//mre.WaitOne(); // 等待 DoCalc 方法完成
|
}
|
|
public void StopRunQuene()
|
{
|
//if (timer_requestQueneRunner != null)
|
//{
|
// timer_requestQueneRunner.Stop();
|
// timer_requestQueneRunner.Dispose();
|
//}
|
if (t!=null&& t.IsAlive) t.Abort();
|
QueneRunnerEnable = false;
|
|
}
|
|
|
private void DoCalc()
|
{
|
while (Enabled)
|
{
|
|
string result = null;
|
|
//if (Busy) continue;
|
|
//wParam = null;
|
if (Quene.RequestQueues == null) continue;
|
WdnmoParam wParam0 = null;
|
lock (Quene.RequestQueues)
|
{
|
if (Quene.RequestQueues.Count <= 0) { /*Busy = false;*/Thread.Sleep(1); continue; }
|
wParam0= Quene.RequestQueues.Dequeue();
|
|
//Busy = true;
|
//Log.Add($"中间列表消费任务,当前{Quene.RequestQueues.Count}个");
|
}
|
gParam.Quene.ResultHash.Clear();
|
gParam.Quene.BestResult = double.MaxValue;
|
//if (wParam == null || wParam!=null && JsonConvert.SerializeObject(wParam.ToSearchDict()) != JsonConvert.SerializeObject(wParam0.ToSearchDict()))
|
//{
|
|
|
// gParam.Quene.ResultHash.Clear();
|
|
|
|
//}
|
|
wParam = wParam0;
|
double resultValue = 0;
|
Guid ID = wParam.ID;
|
this.gParam.onFinished = null;
|
this.gParam.onFinished += (obj) =>
|
{
|
//Busy = false;
|
|
Quene.RequestFinished.Add(ID);
|
StartRunQuene();
|
};
|
this.gParam.onFinished += wParam.onFinish;
|
this.gParam.isNeedOutput = wParam.isNeedOutPut;
|
if ((result = Run(wParam, false)) == null)
|
{
|
break;
|
}
|
else
|
{
|
MessageBox.Show(result);
|
//Busy = false;
|
throw new Exception(result);
|
}
|
|
//mre.Set(); // 通知 StartRunQuene 方法完成
|
}
|
}
|
|
public void SetSaveName(string name)
|
{
|
gParam.SolutionSaveName = name;
|
}
|
#endregion
|
DateTime lastTime = DateTime.Parse("2022-01-01");
|
StringBuilder LogText =new StringBuilder();
|
public void ConsoleLog(string txt)
|
{
|
LogText.AppendLine(("------- "+txt+"\t" + "\t"));
|
var Totolsecond = (DateTime.Now - lastTime).TotalMilliseconds / 1000.0;
|
LogText.AppendLine(($"耗时:{(Totolsecond).ToString("0.00")}秒") );
|
lastTime = DateTime.Now;
|
}
|
|
public void ConsoleLog(string txt,object o)
|
{
|
|
LogText.AppendLine(("------- " + txt+JsonConvert.SerializeObject(o) + "\t" + "\t") );
|
var Totolsecond = (DateTime.Now - lastTime).TotalMilliseconds / 1000.0;
|
LogText.AppendLine(($"耗时:{(Totolsecond).ToString("0.00")}秒") );
|
lastTime = DateTime.Now;
|
|
}
|
public void OutPutLog(bool OpenFile=false)
|
{
|
|
string Pathdir = @"Log\";
|
//string Path = @"Log\Log" + DateTime.Now.ToString("yyyyMMddHHmmss")+".txt";
|
string Path = @"Log\Log.txt";
|
//if (!Directory.Exists(Pathdir)) Directory.CreateDirectory(Pathdir);
|
//if (File.Exists(Path)) File.Delete(Path);
|
//StreamWriter sw = new StreamWriter(Path);
|
//sw.Write(LogText);
|
//sw.Close();
|
//if (OpenFile) System.Diagnostics.Process.Start("explorer",Path);
|
Log.OpenFile();
|
|
}
|
|
/// <summary>
|
/// 单次计算,获取计算结果
|
/// </summary>
|
/// <param name="ConfigPath"></param>
|
/// <param name="ModelPath"></param>
|
/// <param name="wParam"></param>
|
/// <param name="showLog"></param>
|
/// <returns></returns>
|
public WdnmoParam GetResultByScada(string ConfigPath ,
|
string ModelPath, WdnmoParam wParam,bool showLog=false)
|
{
|
lastTime = DateTime.Now;
|
ConsoleLog("GetResultByScada开始");
|
|
string result = this.Init(ConfigPath, ModelPath, _onFinish, _onErrored,null,wParam);
|
//if ((result=this.Init(ConfigPath,ModelPath,_onFinish,_onErrored))!=null)
|
if (result != null)
|
{
|
wParam.ErrorMessage = result;
|
return wParam;
|
}
|
ConsoleLog("initGUI完成");
|
isFinished = false;
|
if ((result = this.Run(wParam, showLog)) != null)
|
{
|
wParam.ErrorMessage = result;
|
this.Close();
|
return wParam;
|
}
|
ConsoleLog("runScada完成");
|
object lockObj = new object();
|
while (true)
|
{
|
lock (lockObj) // 使用 lock 确保 isFinished 能够线程安全地更新
|
{
|
if (isFinished) break; // 如果已经完成了任务,则跳出循环
|
}
|
Thread.Sleep(100); // 暂停一秒钟,等待状态的更新
|
}
|
ConsoleLog("计算完成");
|
if (showLog) OutPutLog();
|
return wParam;
|
}
|
bool isFinished = false;
|
WdnmoParam result_param;
|
#region 调试方法
|
void _onFinish(dynamic modelParams)
|
{
|
result_param = modelParams as WdnmoParam;
|
ConsoleLog("输出参数:" + JsonConvert.SerializeObject(result_param.ResultPoints));
|
if (Robot.Message != null)
|
ConsoleLog("计算反馈错误信息:" + Robot.Message);
|
isFinished = true;
|
}
|
void _onErrored(dynamic result)
|
{
|
result_param = new WdnmoParam() { ErrorMessage = result.ToString() };
|
ConsoleLog("错误信息" + result_param.ErrorMessage);
|
ConsoleLog("输出参数(错误):" + JsonConvert.SerializeObject(result_param.ResultPoints));
|
isFinished = true;
|
}
|
#endregion
|
|
public string ViewResultGUI()
|
{
|
EPAController.ViewResult();
|
return null;
|
}
|
|
public float GetValue(string ID, int code, bool isNode)
|
{
|
return EPAController.ViewResult(ID,code,isNode);
|
}
|
public string SetValue(string ID, int code, double value,bool isNode)
|
{
|
return EPAController.SetResult(ID, code, value, isNode);
|
}
|
//public string SetValue(string ID, EPAModelInput.eType type, float value)
|
//{
|
// return EPAController.SetResult(ID, type, value);
|
//}
|
public EPAParrelController epaParrelController { get { return EPAController; } }
|
|
public string manuCalc()
|
{
|
return EPAController.manuCalc();
|
}
|
public string Stop()
|
{
|
isStarted = false;
|
string result = null;
|
if (EPAController != null)
|
{
|
EPAController.needToStop = true;
|
EPAController.StopRobot();
|
}
|
if (workerThread != null)
|
workerThread.Abort();
|
StopRunQuene();
|
if (Quene.RequestQueues!=null) Quene.RequestQueues.Clear();
|
if (Quene.MissionQuene!=null)Quene.MissionQuene.Clear();
|
if (CSM != null)
|
{
|
foreach (var name in ChildSolutionName)
|
{
|
CSM[name].WMOs.ForEach(w => w.Stop());
|
}
|
}
|
return result;
|
}
|
public string Close()
|
{
|
this.isInited = false;
|
if (gParam!=null)
|
gParam.Dispose();
|
gParam = null;
|
|
isStarted = false;
|
string result = null;
|
|
if (EPAController != null)
|
{
|
EPAController.needToStop = true;
|
EPAController.StopRobot();
|
EPAController.Close();
|
}
|
|
if (workerThread!=null)
|
workerThread.Abort();
|
|
|
if (CSM!=null)
|
{
|
foreach(var name in ChildSolutionName)
|
{
|
if (CSM.ContainsKey(name))
|
CSM[name].WMOs.ForEach(w=>w.Close());
|
}
|
CSM.Clear();
|
CSM = null;
|
ChildSolutionName = null;
|
}
|
StopRunQuene();
|
|
|
return result;
|
}
|
|
|
private string loadLogicParam()
|
{
|
string result = null;
|
//(LogicPoint)((LogicTile)sender).Tag;
|
param = new LogicModelParams();
|
|
|
|
|
var hosts = (List<LogicPoint>)gParam.Db.Connection.Query<LogicPoint>(
|
$"SELECT {LogicPoint.selectColumn} FROM RemoteHost");
|
|
|
double d=0;
|
|
|
foreach (var host in hosts.Where(x => x.LogicType == "初始条件" && x.ParentId != -1 && x.Ext.Enable).OrderBy(x=>x.Sort))
|
{
|
param.初始条件.Add(new variable()
|
{
|
ID = host.Id,
|
modelObjectID = host.ModelObjectID,
|
IndicatorType = host.IndicatorType,
|
Type = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).EpaType,
|
Name = host.Name,
|
expressType = host.ExpressionType,
|
expressString = host.Expression,
|
defineType = host.Ext.accurary,
|
//logicValue = host.ExpressionType == "设定值" ? double.Parse(host.Expression) : null,
|
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,
|
logicType = "初始条件",
|
小数位数 = host.Ext.小数位数,
|
childSolution = host.Ext.childSolution,
|
childTag = host.Ext.childTag,
|
childType = host.Ext.childType,
|
SearchRange =double.TryParse( host.Ext.SaveAcc,out d)?d:0,
|
isNeedToSave = host.Ext.isSave=="是"?true:false,
|
SaveKey=host.Ext.SaveRange,
|
sort=host.Sort,
|
}) ;
|
}
|
foreach (var host in hosts.Where(x => x.LogicType == "下阶段条件" && x.ParentId != -1 && x.Ext.Enable).OrderBy(x => x.Sort))
|
{
|
param.下阶段条件.Add(new variable()
|
{
|
ID = host.Id,
|
modelObjectID = host.ModelObjectID,
|
IndicatorType = host.IndicatorType,
|
Type = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).EpaType,
|
Name = host.Name,
|
expressType = host.ExpressionType,
|
expressString = host.Expression,
|
defineType = host.Ext.accurary,
|
//logicValue = host.ExpressionType == "设定值" ? double.Parse(host.Expression) : null,
|
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,
|
logicType = "下阶段条件",
|
小数位数 = host.Ext.小数位数,
|
childSolution = host.Ext.childSolution,
|
childTag = host.Ext.childTag,
|
childType = host.Ext.childType,
|
SearchRange = double.TryParse(host.Ext.SaveAcc, out d) ? d : 0,
|
isNeedToSave = host.Ext.isSave == "是" ? true : false,
|
SaveKey = host.Ext.SaveRange,
|
sort = host.Sort,
|
});
|
}
|
foreach (var host in hosts.Where(x => x.LogicType == "试算参数" && x.ParentId != -1 && x.Ext.Enable).OrderBy(x => x.Sort))
|
{
|
param.试算变量.Add(new variable()
|
{
|
ID = host.Id,
|
modelObjectID = host.ModelObjectID,
|
IndicatorType = host.IndicatorType,
|
Type = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).EpaType,
|
accuracy = double.Parse(host.Ext.accurary == "" ? "0.1" : host.Ext.accurary),
|
doubleRange = new DRange(double.Parse(host.Expression.Split(',')[0]), double.Parse(host.Expression.Split(',')[1])),
|
Name = host.Name,
|
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,
|
logicType = "试算参数",
|
小数位数 = host.Ext.小数位数,
|
childSolution = host.Ext.childSolution,
|
childTag = host.Ext.childTag,
|
childType = host.Ext.childType,
|
SearchRange = double.TryParse(host.Ext.SaveAcc, out d) ? d : 0,
|
isNeedToSave = host.Ext.isSave == "是" ? true : false,
|
SaveKey = host.Ext.SaveRange,
|
sort = host.Sort,
|
|
});
|
}
|
foreach (var host in hosts.Where(x => x.LogicType == "水量分配" && x.ParentId != -1 && x.Ext.Enable).OrderBy(x => x.Sort))
|
{
|
param.水量分配变量.Add(new variable()
|
{
|
ID = host.Id,
|
modelObjectID = host.ModelObjectID,
|
IndicatorType = host.IndicatorType,
|
Type = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).EpaType,
|
accuracy = double.Parse(host.Ext.accurary == "" ? "0.1" : host.Ext.accurary),
|
expressType = host.ExpressionType,
|
expressString = host.Expression,
|
defineType = host.Ext.accurary,
|
Name = host.Name,
|
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,
|
logicType = "水量分配",
|
小数位数 = host.Ext.小数位数,
|
childSolution = host.Ext.childSolution,
|
childTag = host.Ext.childTag,
|
childType = host.Ext.childType,
|
SearchRange = double.TryParse(host.Ext.SaveAcc, out d) ? d : 0,
|
isNeedToSave = host.Ext.isSave == "是" ? true : false,
|
SaveKey = host.Ext.SaveRange,
|
sort = host.Sort,
|
|
});
|
}
|
foreach (var host in hosts.Where(x => x.LogicType == "变量" && x.ParentId != -1 && x.Ext.Enable).OrderBy(x => x.Sort))
|
{
|
param.变量.Add(new variable()
|
{
|
ID = host.Id,
|
modelObjectID = host.ModelObjectID,
|
IndicatorType = host.IndicatorType,
|
Type = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).EpaType,
|
|
Name = host.Name,
|
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,
|
logicType = "变量",
|
defineType = host.Ext.accurary,
|
expressType = host.ExpressionType,
|
expressString = host.Expression,
|
小数位数 = host.Ext.小数位数,
|
childSolution = host.Ext.childSolution,
|
childTag = host.Ext.childTag,
|
childType = host.Ext.childType,
|
SearchRange = double.TryParse(host.Ext.SaveAcc, out d) ? d : 0,
|
isNeedToSave = host.Ext.isSave == "是" ? true : false,
|
SaveKey = host.Ext.SaveRange,
|
sort = host.Sort,
|
|
});
|
}
|
foreach (var host in hosts.Where(x => x.LogicType == "约束条件" && x.ParentId != -1 && x.Ext.Enable).OrderBy(x => x.Sort))
|
{
|
param.约束变量.Add(new variable()
|
{
|
ID = host.Id,
|
modelObjectID = host.ModelObjectID,
|
IndicatorType = host.IndicatorType,
|
Type = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).EpaType,
|
|
isNode = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).isNode,
|
Name = host.Name,
|
expressString = host.Expression,
|
expressType = host.ExpressionType,
|
ObjType = (int)(GlobalModel.DEFINE_OBJECT.Find(obj => obj.Name == host.Ext.ObjectType) ?? new TypeObject()).EPAObjType,
|
logicType = "约束条件",
|
小数位数 = host.Ext.小数位数,
|
childSolution = host.Ext.childSolution,
|
childTag = host.Ext.childTag,
|
childType = host.Ext.childType,
|
SearchRange = double.TryParse(host.Ext.SaveAcc, out d) ? d : 0,
|
isNeedToSave = host.Ext.isSave == "是" ? true : false,
|
SaveKey = host.Ext.SaveRange,
|
sort = host.Sort,
|
});
|
}
|
|
foreach (var host in hosts.Where(x => (x.LogicType == "集合" || x.LogicType == "选择集" && x.Ext.Enable) && x.ParentId != -1).OrderBy(x=>x.Sort))
|
{
|
param.集合.Add(new variable()
|
{
|
ID = host.Id,
|
modelObjectID = host.ModelObjectID,
|
IndicatorType = host.IndicatorType,
|
Type = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).EpaType,
|
Name = host.Name,
|
expressType = host.ExpressionType,
|
expressString = host.Expression,
|
defineType = host.Ext.accurary,
|
//logicValue = host.ExpressionType == "设定值" ? double.Parse(host.Expression) : null,
|
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,
|
logicType = "集合",
|
小数位数 = host.Ext.小数位数,
|
childSolution = host.Ext.childSolution,
|
childTag = host.Ext.childTag,
|
childType = host.Ext.childType,
|
SearchRange = double.TryParse(host.Ext.SaveAcc, out d) ? d : 0,
|
isNeedToSave = host.Ext.isSave == "是" ? true : false,
|
SaveKey = host.Ext.SaveRange,
|
sort = host.Sort,
|
});
|
}
|
|
foreach (var host in hosts.Where(x => (x.LogicType == "表达式") && x.ParentId != -1 && x.Ext.Enable).OrderBy(x => x.Sort))
|
{
|
param.表达式.Add(new variable()
|
{
|
ID = host.Id,
|
modelObjectID = host.ModelObjectID,
|
IndicatorType = host.IndicatorType,
|
Type = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).EpaType,
|
Name = host.Name,
|
expressType = host.ExpressionType,
|
expressString = host.Expression,
|
defineType = host.Ext.accurary,
|
//logicValue = host.ExpressionType == "设定值" ? double.Parse(host.Expression) : null,
|
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,
|
logicType = "表达式",
|
小数位数 = host.Ext.小数位数,
|
childSolution = host.Ext.childSolution,
|
childTag = host.Ext.childTag,
|
childType = host.Ext.childType,
|
SearchRange = double.TryParse(host.Ext.SaveAcc, out d) ? d : 0,
|
isNeedToSave = host.Ext.isSave == "是" ? true : false,
|
SaveKey = host.Ext.SaveRange,
|
sort = host.Sort,
|
});
|
}
|
foreach (var host in hosts.Where(x => (x.LogicType == "预处理插件") && x.ParentId != -1 && x.Ext.Enable).OrderBy(x => x.Sort))
|
{
|
param.预处理插件.Add(new variable()
|
{
|
ID = host.Id,
|
modelObjectID = host.ModelObjectID,
|
IndicatorType = host.IndicatorType,
|
//Type = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == host.IndicatorType) ?? new TypeIndicator()).EpaType,
|
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,
|
logicType = "预处理插件",
|
小数位数 = host.Ext.小数位数,
|
childSolution = host.Ext.childSolution,
|
childTag = host.Ext.childTag,
|
childType = host.Ext.childType,
|
SearchRange = double.TryParse(host.Ext.SaveAcc, out d) ? d : 0,
|
isNeedToSave = host.Ext.isSave == "是" ? true : false,
|
SaveKey = host.Ext.SaveRange,
|
sort = host.Sort,
|
});
|
}
|
|
{
|
var OBJFunctionList = hosts.FindAll(x => x.LogicType == "目标函数" && x.ParentId != -1 && x.Ext.Enable);
|
if (OBJFunctionList.Count == 0)
|
{
|
//MessageBox.Show("未设定目标函数");
|
return "未设定目标函数";
|
}
|
else if (OBJFunctionList.Count > 1)
|
{
|
//MessageBox.Show("目标函数太多,只能设定一个目标函数");
|
return "目标函数太多,只能设定一个目标函数";
|
}
|
var o = OBJFunctionList[0];
|
DRange to;
|
string[] sp;
|
try
|
{
|
if (!string.IsNullOrEmpty(o.Expression.Trim()) && (sp = o.Expression.Replace(',', ',').Split(',')).Length > 1)
|
{
|
to = new DRange(double.Parse(sp[0]), double.Parse(sp[1]));
|
}
|
else
|
{
|
to = new DRange(0,0);
|
}
|
}
|
catch
|
{
|
return "目标容许范围设置格式错误";
|
}
|
|
|
|
OBJFunction OFunction = new OBJFunction()
|
{
|
Text_origin = o.ModelObjectID,
|
Name = o.Name,
|
ExpressType = o.ExpressionType,
|
tolerance = to,
|
variable = new variable()
|
{
|
ID = o.Id,
|
modelObjectID = o.ModelObjectID,
|
Type = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == o.IndicatorType) ?? new TypeIndicator()).EpaType,
|
isNode = (GlobalModel.DEFINE_INDICATOR.Find(ind => ind.Name == o.IndicatorType) ?? new TypeIndicator()).isNode,
|
Name = o.Name,
|
ObjType = (int)(GlobalModel.DEFINE_OBJECT.Find(obj => obj.Name == o.Ext.ObjectType) ?? new TypeObject()).EPAObjType,
|
小数位数 = o.Ext.小数位数,
|
SearchRange = double.TryParse(o.Ext.SaveAcc, out d) ? d : 0,
|
isNeedToSave = o.Ext.isSave == "是" ? true : false,
|
SaveKey = o.Ext.SaveRange,
|
expressType= "目标函数",
|
sort=o.Sort,
|
}
|
};
|
param.OFunction = OFunction;
|
|
|
|
|
|
}
|
|
var list = param.变量全集.FindAll(v => v.childSolution != "");
|
list.Sort((a,b)=>a.sort>b.sort?1:(a.sort<b.sort?-1:(a.ID>b.ID?1:(a.ID<b.ID?-1:0))));
|
if (list.Count>0)
|
{
|
param.ChildParam = new Dictionary<string, childSolutionParam>();
|
foreach (var childSolution in gParam.GlobalConfig.Ext.ChildSolutionArr)
|
{
|
if (!childSolution.启用) continue;
|
var childSolutionName = childSolution.名称;
|
var childSolutionSaveName = childSolution.存储表名;
|
var l1 = list.FindAll(v => v.childSolution == childSolutionSaveName && v.childType == "传入");
|
|
var l2 = list.FindAll(v => v.childSolution == childSolutionSaveName && v.childType == "返回");
|
if (l1.Count > 0 && l2.Count > 0)
|
{
|
param.ChildParam.Add(childSolutionName, new childSolutionParam());
|
var cp = param.ChildParam[childSolutionName];
|
cp.childSaveName = childSolutionSaveName;
|
cp.childVars_In=l1;
|
cp.childVars_Out=l2;
|
cp.childFitness_Out=( l2.Find(v=>v.childTag.ToLower()=="fitness"));
|
WdnmoParam wparam = new WdnmoParam();
|
wparam.ScadaPoints.AddRange(l1.Select(v =>
|
{
|
|
TimePoint t = new TimePoint() {
|
Name = v.Name,
|
Key = v.childTag,
|
SearchRange=v.SearchRange,
|
|
};
|
|
return t;
|
}));
|
wparam.ResultPoints.AddRange(l2.Select(v =>
|
{
|
TimePoint t = new TimePoint()
|
{
|
Name=v.Name,
|
Key = v.childTag,
|
isNeedtoSave = v.isNeedToSave,
|
SaveKey = v.SaveKey,
|
};
|
|
|
return t;
|
}));
|
cp.wParam= wparam;
|
}
|
|
}
|
}
|
|
|
|
return null;
|
|
}
|
|
|
private string loadGeneticParam(FinishEvent onFinished, FinishEvent onError, FinishEvent onReportProgress, SetEvent SetVarsMethod)
|
{
|
|
string result = null;
|
|
|
//DLengths.ForEach(m => chromosomeLength += m);
|
|
gParam.populationSize = gParam.GlobalConfig.Ext.populationNum;
|
gParam.iterations = gParam.GlobalConfig.Ext.iterationNum;
|
gParam.populationSize_full = 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;
|
gParam.setVars = SetVarsMethod;
|
|
ChildSolutions.ForEach(solution =>
|
{
|
string key = solution.名称;
|
//初始化内存存储
|
if (gParam.GlobalConfig.Ext.启用内存存储)
|
{
|
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<ConcurrentDictionary<ulong, double>>(savePath);
|
// }
|
// else
|
// {
|
// settings.saveDatas = new ConcurrentDictionary<ulong, double>();
|
// }
|
//}
|
|
}
|
});
|
|
return null;
|
|
|
|
|
|
}
|
|
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 / accur;
|
var Dlength = 0;
|
var value = 1;
|
var i = 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))
|
{
|
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;
|
}
|
|
EPAController = new EPAParrelController();
|
|
|
if ((result = EPAController.Init(inp_file, gParam.GlobalConfig.Ext.MaxThread, param, gParam, wParam, isGUI)) != null)
|
{
|
EPAController.Dispose();
|
return result;
|
}
|
|
|
|
return null;
|
}
|
}
|
|
|
}
|