using Microsoft.SolverFoundation.Services; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Xml; namespace OTSExtremum.Business { public class Tools { /// /// 判断字符串是否是int/double /// public static bool IsIntOrDouble(string strNumber) { Regex objNotNumberPattern = new Regex("[^0-9.-]"); Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*"); Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*"); const string strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$"; const string strValidIntegerPattern = "^([-]|[0-9])[0-9]*$"; Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")"); return !objNotNumberPattern.IsMatch(strNumber) && !objTwoDotPattern.IsMatch(strNumber) && !objTwoMinusPattern.IsMatch(strNumber) && objNumberPattern.IsMatch(strNumber); } /// /// 数据离群分析,排除外围数据。返回值:1最大值越界;2最小值越界;0正常范围 /// public static int Grubbls(double[] arrData) { //格布拉斯(Grubbls)检验法的临界值--k值 double[] grubbls = { 0,0,1.156,1.492,1.749,1.944,2.097,2.221,2.323,2.410,2.485,2.550, 2.607,2.650,2.705,2.747,2.785,2.821,2.854,2.884,2.912,2.939,2.963,2.987, 3.009,3.029,3.049,3.085,3.103,3.119,3.135,3.150,3.164,3.178,3.178,3.191, 3.204,3.216,3.228,3.240,3.251,3.261,3.271,3.282,3.292,3.302,3.310,3.319, 3.329,3.336,3.345,3.353,3.361,3.368,3.376,3.383,3.391,3.397,3.405,3.411, 3.418,3.424,3.430,3.437,3.442,3.449,3.454,3.46,3.466,3.471 }; double xAvg = Mean(arrData); int arrNum = arrData.Length; double sdev = StDev(arrData); double max = arrData.Max(); double min= arrData.Min(); double Tmax = (max - xAvg) / sdev; double Tmin= (xAvg - min) / sdev; double T = grubbls[arrData.Count() - 1]; if (Tmax > T) { return 1; } else if (Tmin > T) { return 2; } else { return 0; } } //计算标准偏差 public static double StDev(double[] arrData) { double xAvg = Mean(arrData); double sSum = 0; double tmpStDev = 0; int arrNum = arrData.Length; for (int j = 0; j < arrNum; j++) { sSum += ((arrData[j] - xAvg) * (arrData[j] - xAvg)); } tmpStDev = Convert.ToSingle(Math.Sqrt((sSum / (arrNum - 1))).ToString()); return tmpStDev; } //平均值 public static double Mean(double[] arrData) { double xSum = 0; double xAvg = 0; int arrNum = arrData.Length; for (int i = 0; i < arrNum; i++) { xSum += arrData[i]; } xAvg = xSum / arrNum; return xAvg; } //δmom public static double Delta(double[] arrData) { double sdev = StDev(arrData); double xAvg=sdev*Math.Sqrt(6) / 3.1416;//δ return xAvg; } //λmom public static double Lambda(double[] arrData) { double delta = Delta(arrData); double mean = Mean(arrData); double xAvg = mean - 0.5772 * delta; return xAvg; } /// /// 规划求值 /// /// 传入原始数据 /// double[δ,λ,max] public static double[] OPTMax(double[] gg) { SolverContext context = SolverContext.GetContext(); //创建模型 Model model = context.CreateModel(); //优化决策因子,变量为实数,Domain.Integer|Domain.IntegerNonnegative为整数优化 Decision x = new Decision(Domain.Real, "x"); Decision y = new Decision(Domain.Real, "y"); //添加 model.AddDecisions(x, y); //优化 Solution solution = context.Solve(); Goal gmax = model.AddGoal("zmax", GoalKind.Maximize, GetFun(gg,x, y)); //优化 solution = context.Solve(); double[] ret = new double[] { x.ToDouble(), y.ToDouble(), solution.Goals.First().ToDouble() }; context.ClearModel(); return ret; } //返回公式 private static Term GetFun(double[] gg,Decision delta, Decision lambda) { // double[] gg = { 140, 240, 532, 205, 150, 110, 200, 180, 220, 300, 70, 390, 100, 90, 320, 190, 250, 150, 280, 250, 490, 120, 500, 200 }; // Array.Sort(gg); Term xx = Model.Log(1 / delta) - (gg[0] - lambda) / delta - Model.Exp((lambda - gg[0]) / delta); for (int i = 1; i < gg.Count(); i++) { xx = xx + Model.Log(1 / delta) - (gg[i] - lambda) / delta - Model.Exp((lambda - gg[i]) / delta); } return xx; } /// /// 获取XML节点参数 /// /// 节点参数名称 /// 节点参数 public static string GetXMLInformations(string xmlFilePath, string Name) { try { string value = string.Empty; XmlDocument doc = new XmlDocument(); doc.Load(xmlFilePath); //加载Xml文件 XmlElement root = doc.DocumentElement; //获取根节点 XmlNodeList mainNodes = root.GetElementsByTagName("parameter"); //获取子节点集合 foreach (XmlNode node in mainNodes) { //获取Name属性值 string name = ((XmlElement)node).GetAttribute("Name"); if (name.Equals(Name)) { value = ((XmlElement)node).GetAttribute("Value"); } } return value; } catch (Exception) { return ""; } } #region Base64加密解密 /// /// Base64加密 /// /// 需要加密的字符串 /// public static string Base64Encrypt(string input) { return Base64Encrypt(input, new UTF8Encoding()); } /// /// Base64加密 /// /// 需要加密的字符串 /// 字符编码 /// public static string Base64Encrypt(string input, Encoding encode) { return Convert.ToBase64String(encode.GetBytes(input)); } /// /// Base64解密 /// /// 需要解密的字符串 /// public static string Base64Decrypt(string input) { return Base64Decrypt(input, new UTF8Encoding()); } /// /// Base64解密 /// /// 需要解密的字符串 /// 字符的编码 /// public static string Base64Decrypt(string input, Encoding encode) { return encode.GetString(Convert.FromBase64String(input)); } #endregion } //分组数据信息 public class groupInfo { //数据 public double[] values { set; get; } //名称 public string groupTitle { set; get; } } }