Exemplo n.º 1
0
        public static Results Calcu(List <PValue>[] inputs, CalcuInfo calcuinfo)
        {
            //公用变量
            bool   _errorFlag   = false;
            string _errorInfo   = "";
            bool   _warningFlag = false;
            string _warningInfo = "";
            bool   _fatalFlag   = false;
            string _fatalInfo   = "";

            //0输出初始化:该算法如果没有有效输入值(inputs为null)或者输入值得有效值为null,给出的计算结果。值为0,计算标志位为StatusConst.InputIsNull
            List <PValue>[] results = new List <PValue> [16];   //平均值;最小值;最小值所在的点号;最大值;最大值所在的点号;最均差;最大差(最大值与最小值得差);和;绝对值和
            for (int i = 0; i < results.Length; i++)
            {
                results[i] = new List <PValue>();
                results[i].Add(new PValue(0, calcuinfo.fstarttime, calcuinfo.fendtime, (long)StatusConst.InputIsNull));
            }
            try
            {
                //0.1、输入处理:输入长度。当输入为空时,则输出项也为空.
                if (inputs == null || inputs.Length == 0 || inputs[0] == null)
                {
                    return(new Results(results, _errorFlag, _errorInfo, _warningFlag, _warningInfo, _fatalFlag, _fatalInfo));
                }


                //1 参数处理

                string[] paras = calcuinfo.fparas.Split(';');



                string[] vibstr = paras[0].Split(',');
                double[] vib    = new double[vibstr.Length];
                for (int i = 0; i < vibstr.Length - 1; i++)
                {
                    vib[i] = Convert.ToDouble(vibstr[i]);
                }

                string[] visstr = paras[1].Split(',');
                double[] vis    = new double[visstr.Length];
                for (int i = 0; i < visstr.Length - 1; i++)
                {
                    vis[i] = Convert.ToDouble(visstr[i]);
                }

                List <XFClass> XF    = new List <XFClass>();
                string[]       XFStr = paras[2].Split(',');
                foreach (string item in XFStr)
                {
                    XFClass xfClass = new XFClass();
                    xfClass.A = Convert.ToInt32(item.Split(':')[0]);
                    List <double> bList = new List <double>();
                    foreach (string bItem in item.Split(':')[1].Split('*'))
                    {
                        bList.Add(Convert.ToDouble(bItem));
                    }
                    xfClass.B = bList;
                }

                double[]   Y = new double[inputs[0].Count];
                double[][] X = new double[inputs[1].Count][];
                for (int i = 0; i < inputs[0].Count - 1; i++)
                {
                    Y[i] = inputs[0][i].Value;
                }
                for (int i = 1; i < inputs.Length - 1; i++)
                {
                    double[] xList = new double[inputs[i].Count];
                    for (int l = 0; l < inputs[i].Count - 1; l++)
                    {
                        xList[l] = inputs[i][l].Value;
                    }
                    X[i] = xList;
                }
                int      k          = 0;
                double   p          = 0;
                double[] mlist      = new double[] { };
                double[] resultlist = new double[] { };
                string   errmsg     = string.Empty;
                MultipleRegressionAlgorithmCaculate mm = new MultipleRegressionAlgorithmCaculate();
                int s = mm.Regression(0, vib, vis, X, Y, XF, ref k, ref p, ref mlist, ref resultlist, ref errmsg);
                if (s != 0)
                {
                    _fatalFlag = true;
                    _errorInfo = errmsg;
                    return(new Results(results, _errorFlag, _errorInfo, _warningFlag, _warningInfo, _fatalFlag, _fatalInfo));
                }
                PValue kValue = new PValue(k, calcuinfo.fstarttime, calcuinfo.fendtime, 0);
                PValue pValue = new PValue(p, calcuinfo.fstarttime, calcuinfo.fendtime, 0);
                results[0] = new List <PValue>();
                results[0].Add(kValue);
                results[1] = new List <PValue>();
                results[1].Add(pValue);
                for (int i = 0; i < mlist.Length - 1; i++)
                {
                    PValue mValue = new PValue(mlist[i], calcuinfo.fstarttime, calcuinfo.fendtime, 0);
                    results[i + 2] = new List <PValue>();
                    results[i + 2].Add(mValue);
                }
                //分别对应R2; sey; F;df;SSR;SSE;
                for (int i = 0; i < resultlist.Length - 1; i++)
                {
                    PValue rValue = new PValue(resultlist[i], calcuinfo.fstarttime, calcuinfo.fendtime, 0);
                    results[i + 2 + mlist.Length] = new List <PValue>();
                    results[i + 2 + mlist.Length].Add(rValue);
                }
                //不够16位的0补齐
                if (results.Length < 16)
                {
                    int l = results.Length;
                    for (int i = 0; i < 16 - results.Length; i++)
                    {
                        PValue rValue = new PValue(0, calcuinfo.fstarttime, calcuinfo.fendtime, 0);
                        results[i + l] = new List <PValue>();
                        results[i + l].Add(rValue);
                    }
                }

                //计算结果初始化
                return(new Results(results, _errorFlag, _errorInfo, _warningFlag, _warningInfo, _fatalFlag, _fatalInfo));
            }
            catch (Exception ex)
            {
                _fatalFlag = true;
                _fatalInfo = ex.ToString();
                return(new Results(null, _errorFlag, _errorInfo, _warningFlag, _warningInfo, _fatalFlag, _fatalInfo));
            }
        }
        public static Results Calcu(List <PValue>[] inputs, CalcuInfo calcuinfo)
        {
            //公用变量
            bool   _errorFlag   = false;
            string _errorInfo   = "";
            bool   _warningFlag = false;
            string _warningInfo = "";
            bool   _fatalFlag   = false;
            string _fatalInfo   = "";

            //0输出初始化:该算法如果没有有效输入值(inputs为null)或者输入值得有效值为null,给出的计算结果。值为0,计算标志位为StatusConst.InputIsNull
            List <PValue>[] results = new List <PValue> [18];   //平均值;最小值;最小值所在的点号;最大值;最大值所在的点号;最均差;最大差(最大值与最小值得差);和;绝对值和
            for (int i = 0; i < results.Length; i++)
            {
                results[i] = new List <PValue>();
                results[i].Add(new PValue(0, calcuinfo.fstarttime, calcuinfo.fendtime, (long)StatusConst.InputIsNull));
            }
            try
            {
                //0.1、输入处理:输入长度。当输入为空时,则输出项也为空.
                if (inputs == null || inputs.Length == 0 || inputs[0] == null)
                {
                    return(new Results(results, _errorFlag, _errorInfo, _warningFlag, _warningInfo, _fatalFlag, _fatalInfo));
                }


                //1 参数处理

                string[] paras = calcuinfo.fparas.Split(';');



                string[] vibstr = paras[0].Split(',');
                double[] vib    = new double[vibstr.Length];
                for (int i = 0; i < vibstr.Length - 1; i++)
                {
                    vib[i] = Convert.ToDouble(vibstr[i]);
                }

                string[] visstr = paras[1].Split(',');
                double[] vis    = new double[visstr.Length];
                for (int i = 0; i < visstr.Length - 1; i++)
                {
                    vis[i] = Convert.ToDouble(visstr[i]);
                }

                List <XFClass> XF    = new List <XFClass>();
                string[]       XFStr = paras[2].Split(',');
                foreach (string item in XFStr)
                {
                    XFClass xfClass = new XFClass();
                    xfClass.A = Convert.ToInt32(item.Split(':')[0]);
                    List <double> bList = new List <double>();
                    foreach (string bItem in item.Split(':')[1].Split('*'))
                    {
                        bList.Add(Convert.ToDouble(bItem));
                    }
                    xfClass.B = bList;
                }

                double[]   Y = new double[inputs[0].Count];
                double[][] X = new double[inputs[1].Count][];
                for (int i = 0; i < inputs[0].Count - 1; i++)
                {
                    Y[i] = inputs[0][i].Value;
                }
                for (int i = 1; i < inputs.Length - 1; i++)
                {
                    double[] xList = new double[inputs[i].Count];
                    for (int l = 0; l < inputs[i].Count - 1; l++)
                    {
                        xList[l] = inputs[i][l].Value;
                    }
                    X[i] = xList;
                }
                int      k          = 0;
                double   p          = 0;
                double[] mlist      = new double[] { };
                double[] resultlist = new double[] { };
                string   errmsg     = string.Empty;
                MultipleRegressionAlgorithmCaculate mm = new MultipleRegressionAlgorithmCaculate();
                int s = mm.Regression(0, vib, vis, X, Y, XF, ref k, ref p, ref mlist, ref resultlist, ref errmsg);
                if (s != 0)
                {
                    _fatalFlag = true;
                    _errorInfo = errmsg;
                    return(new Results(results, _errorFlag, _errorInfo, _warningFlag, _warningInfo, _fatalFlag, _fatalInfo));
                }
                PValue kValue = new PValue(k, calcuinfo.fstarttime, calcuinfo.fendtime, 0);
                PValue pValue = new PValue(p, calcuinfo.fstarttime, calcuinfo.fendtime, 0);
                results[0] = new List <PValue>();
                results[0].Add(kValue);
                results[1] = new List <PValue>();
                results[1].Add(pValue);

                //分别对应R2; sey; F;df;SSR;SSE;
                for (int i = 0; i < resultlist.Length - 1; i++)
                {
                    PValue rValue = new PValue(resultlist[i], calcuinfo.fstarttime, calcuinfo.fendtime, 0);
                    results[i + 2 + mlist.Length] = new List <PValue>();
                    results[i + 2 + mlist.Length].Add(rValue);
                }
                for (int i = 0; i < mlist.Length - 1; i++)
                {
                    PValue mValue = new PValue(mlist[i], calcuinfo.fstarttime, calcuinfo.fendtime, 0);
                    results[i + 2] = new List <PValue>();
                    results[i + 2].Add(mValue);
                }
                //不够16位的0补齐
                if (results.Length < 18)
                {
                    int l = results.Length;
                    for (int i = 0; i < 18 - results.Length; i++)
                    {
                        PValue rValue = new PValue(0, calcuinfo.fstarttime, calcuinfo.fendtime, 0);
                        results[i + l] = new List <PValue>();
                        results[i + l].Add(rValue);
                    }
                }
                string   year  = string.Empty;
                string   month = string.Empty;
                string   day   = string.Empty;
                string   hour  = string.Empty;
                DateTime dt    = Convert.ToDateTime(inputs[0][0].Timestamp);
                year  = dt.Year.ToString();
                month = dt.Month.ToString();
                day   = dt.Day.ToString();
                hour  = dt.Hour.ToString();
                string type = calcuInfo.fsourtagids[0].ToString();
                MultipleRegressionAlgorithmOutClass messageIn = new MultipleRegressionAlgorithmOutClass();
                messageIn.k       = results[0][0].Value.ToString();
                messageIn.b       = results[1][0].Value.ToString();
                messageIn.r2      = results[2][0].Value.ToString();
                messageIn.sey     = results[3][0].Value.ToString();
                messageIn.F       = results[4][0].Value.ToString();
                messageIn.df      = results[5][0].Value.ToString();
                messageIn.ssreg   = results[6][0].Value.ToString();
                messageIn.ssresid = results[7][0].Value.ToString();
                messageIn.m1      = results[8][0].Value.ToString();
                messageIn.m2      = results[9][0].Value.ToString();
                messageIn.m3      = results[10][0].Value.ToString();
                messageIn.m4      = results[11][0].Value.ToString();
                messageIn.m5      = results[12][0].Value.ToString();
                messageIn.m6      = results[13][0].Value.ToString();
                messageIn.m7      = results[14][0].Value.ToString();
                messageIn.m8      = results[15][0].Value.ToString();
                messageIn.m9      = results[16][0].Value.ToString();
                messageIn.m10     = results[17][0].Value.ToString();
                //计算结果存入数据库
                bool isok = BLL.AlgorithmBLL.InsertMultipleRegressionAlgorithm(messageIn, type, year, month, day, hour);
                //计算结果初始化
                return(new Results(results, _errorFlag, _errorInfo, _warningFlag, _warningInfo, _fatalFlag, _fatalInfo));
            }
            catch (Exception ex)
            {
                _fatalFlag = true;
                _fatalInfo = ex.ToString();
                return(new Results(null, _errorFlag, _errorInfo, _warningFlag, _warningInfo, _fatalFlag, _fatalInfo));
            }
        }