namespace IStation.Algorithm
|
{
|
|
#region ViewModel
|
|
/// <summary>
|
/// 分析泵项
|
/// </summary>
|
public class AnaItem
|
{
|
public AnaItem() { }
|
public string RunFlag { get; set; }
|
public int RunCount { get; set; }
|
public List<long> PumpIds { get; set; }
|
public double Flow { get; set; }
|
public double Head { get; set; }
|
}
|
|
#endregion
|
|
/// <summary>
|
/// 调度分析辅助类
|
/// </summary>
|
public class SchedulingHelper
|
{
|
double _frequency_def = 50;
|
double _frequency_min = 25;
|
double _frequency_max = 50;
|
double _frequency_space = 0.1;//频率间隔
|
|
double _head_space = 0.1;//扬程间隔
|
|
#region RunFlag
|
string _falgFrePumpTag = "B";
|
string _falgFixPumpTag = "G";
|
string _falgSpaceMark = "_";
|
|
string GetRunFlag(List<AnaPumpItem> pumpItems)
|
{
|
var tableName = string.Empty;
|
var count = pumpItems.Count;
|
pumpItems = pumpItems.OrderBy(p => p.ID).ToList();
|
for (int i = 0; i < count; i++)
|
{
|
var pumpItem = pumpItems[i];
|
tableName += GetRunFlag(pumpItem);
|
if ((i + 1) != count)
|
{
|
tableName += _falgSpaceMark;
|
}
|
}
|
return tableName;
|
}
|
|
string GetRunFlag(AnaPumpItem pumpItem)
|
{
|
return (pumpItem.IsBp ? _falgFrePumpTag : _falgFixPumpTag) + pumpItem.ID;
|
}
|
|
|
string GetRunFlag(Dictionary<int, bool> dic)
|
{
|
var tableName = string.Empty;
|
var index = 0;
|
var count = dic.Count;
|
foreach (var item in dic)
|
{
|
tableName += GetRunFlag(item.Key, item.Value);
|
index++;
|
if (index != count)
|
{
|
tableName += _falgSpaceMark;
|
}
|
}
|
return tableName;
|
}
|
|
string GetRunFlag(long id, bool isBp)
|
{
|
return (isBp ? _falgFrePumpTag : _falgFixPumpTag) + id;
|
}
|
|
string GetRunFlag(List<string> flags)
|
{
|
var tableName = string.Empty;
|
var count = flags.Count;
|
var index = 0;
|
foreach (var flag in flags)
|
{
|
tableName += flag;
|
index++;
|
if (index != count)
|
{
|
tableName += _falgSpaceMark;
|
}
|
}
|
return tableName;
|
}
|
|
string GetRunFlag(List<int> flags)
|
{
|
var tableName = string.Empty;
|
var count = flags.Count;
|
var index = 0;
|
foreach (var flag in flags)
|
{
|
tableName += flag;
|
index++;
|
if (index != count)
|
{
|
tableName += _falgSpaceMark;
|
}
|
}
|
return tableName;
|
}
|
|
|
#endregion
|
|
DAL.ScheduleCombine _dal = new DAL.ScheduleCombine();
|
DAL.ScheduleConclusion _dalScheduleConclusion = new DAL.ScheduleConclusion();
|
DAL.ScheduleAnaLog _dalAnaLog = new DAL.ScheduleAnaLog();
|
|
|
public class CurrentViewModel : Entity.ScheduleConclusion
|
{
|
public CurrentViewModel(Entity.ScheduleConclusion rhs) : base(rhs)
|
{
|
this.Flags = new List<int>();
|
var flags = rhs.RunFlag.Split('_');
|
foreach (var item in flags)
|
{
|
if (int.TryParse(item.Substring(1), out int flag))
|
{
|
this.Flags.Add(flag);
|
}
|
|
}
|
}
|
public List<int> Flags { get; set; }
|
}
|
|
|
public void Ana(List<Pump> pumps, double tagetFlow, double tagetHead, List<int> openPumpCombine)
|
{
|
var pumpIds = pumps.Select(x => x.ID);
|
var dic_pump = pumps.ToDictionary(x => x.ID, x => x.IsBp);
|
|
|
if (openPumpCombine != null && openPumpCombine.Any())
|
{
|
{
|
var optimalConclusionList = new List<CurrentViewModel>();
|
foreach (var pump in openPumpCombine)
|
{
|
var runFalg = GetRunFlag(pump, dic_pump[pump]);
|
var list = _dalScheduleConclusion.GetList(runFalg, tagetHead, 1);
|
if (list != null && list.Any())
|
{
|
var vmList = list.Select(x => new CurrentViewModel(x)).ToList();
|
optimalConclusionList.AddRange(vmList);
|
}
|
}
|
if (optimalConclusionList == null || !optimalConclusionList.Any())
|
{
|
return;
|
}
|
|
var optimalConclusionCombineList = PermutationAndCombination<CurrentViewModel>.GetCombination(optimalConclusionList.ToArray(), 4);
|
var combines = new List<Combine>();
|
foreach (var arr in optimalConclusionCombineList)
|
{
|
var falgs = arr.SelectMany(x => x.Flags).ToList();
|
if (falgs.GroupBy(x => x).Where(x => x.Count() > 1).Count() > 0)
|
{
|
continue;
|
}
|
var combine = new Combine();
|
combine.RunFlag = GetRunFlag(falgs);
|
combine.Power = arr.Sum(x => x.Power);
|
combine.Head = arr.Average(x => x.Head);
|
combine.Flow = arr.Sum(x => x.Flow);
|
combine.RunFlagList = falgs;
|
combines.Add(combine);
|
}
|
|
var a = combines.OrderBy(x => x.Flow).ThenBy(x => x.Power).ToList();
|
}
|
|
|
{
|
var allCombineList = PermutationAndCombination<int>.GetCombination(pumpIds.ToArray(), 2);
|
var combineList = new List<List<int>>();
|
foreach (var combine in allCombineList)
|
{
|
var pump1 = combine[0];
|
var pump2 = combine[1];
|
if (!openPumpCombine.Contains(pump1))
|
continue;
|
if (!openPumpCombine.Contains(pump2))
|
continue;
|
combineList.Add(combine.ToList());
|
}
|
if (combineList.Count < 1)
|
return;
|
|
var avgFlow = tagetFlow / 2;
|
|
var optimalConclusionList = new List<CurrentViewModel>();
|
foreach (var combine in combineList)
|
{
|
var dic = new Dictionary<int, bool>();
|
foreach (var pump in combine)
|
{
|
if (dic_pump.ContainsKey(pump))
|
{
|
dic.Add(pump, dic_pump[pump]);
|
}
|
}
|
var runFalg = GetRunFlag(dic);
|
var list = _dalScheduleConclusion.GetList(runFalg, avgFlow, tagetHead, 5);
|
if (list != null && list.Any())
|
{
|
var vmList = list.Select(x => new CurrentViewModel(x)).ToList();
|
optimalConclusionList.AddRange(vmList);
|
}
|
}
|
if (optimalConclusionList == null || !optimalConclusionList.Any())
|
{
|
return;
|
}
|
|
var optimalConclusionCombineList = PermutationAndCombination<CurrentViewModel>.GetCombination(optimalConclusionList.ToArray(), 2);
|
var combines = new List<Combine>();
|
foreach (var arr in optimalConclusionCombineList)
|
{
|
var falgs = arr.SelectMany(x => x.Flags).ToList();
|
if (falgs.GroupBy(x => x).Where(x => x.Count() > 1).Count() > 0)
|
{
|
continue;
|
}
|
var combine = new Combine();
|
combine.RunFlag = GetRunFlag(falgs);
|
combine.Power = arr.Sum(x => x.Power);
|
combine.Head = arr.Average(x => x.Head);
|
combine.Flow = arr.Sum(x => x.Flow);
|
combine.RunFlagList = falgs;
|
combines.Add(combine);
|
}
|
|
var a = combines.OrderBy(x => x.Flow).ThenBy(x => x.Power).ToList();
|
}
|
|
|
}
|
}
|
|
private class Combine
|
{
|
public double Flow { get; set; }
|
public double Head { get; set; }
|
public double Power { get; set; }
|
public string RunFlag { get; set; }
|
public List<int> RunFlagList { get; set; }
|
}
|
|
|
|
/// <summary>
|
/// 插入分析日志
|
/// </summary>
|
private void InsertAnaLog(string info)
|
{
|
var entity = new Entity.ScheduleAnaLog(info);
|
_dalAnaLog.Insert(entity);
|
}
|
|
|
/// <summary>
|
/// 判断表是否存在
|
/// </summary>
|
public bool ExistTable(string runFlag)
|
{
|
return _dal.ExistTable(runFlag);
|
}
|
|
}
|
}
|