Exemple #1
0
		public static void Train(params string[] args)
		{
			Parameter parameters = default(Parameter);
			Problem problem = default(Problem);
			bool flag = default(bool);
			int nrfold = default(int);
			string filename = default(string);
			Training.parseCommandLine(args, out parameters, out problem, out flag, out nrfold, out filename);
			if (flag)
			{
				Training.PerformCrossValidation(problem, parameters, nrfold);
			}
			else
			{
				Model.Write(filename, Training.Train(problem, parameters));
			}
		}
        public static RegressionParameter[] GridRegression(Problem problem, Parameter parameters, List <double> CValues, List <double> GammaValues, List <double> PValues, string outputFile, int nrfold, int MaxBestCount)
        {
            if (!string.IsNullOrEmpty(outputFile))
            {
                File.WriteAllText(outputFile, string.Empty);
            }
            List <RegressionParameter> list = new List <RegressionParameter>(CValues.Count * GammaValues.Count * PValues.Count);

            for (int i = 0; i < CValues.Count; i++)
            {
                for (int j = 0; j < GammaValues.Count; j++)
                {
                    for (int k = 0; k < PValues.Count; k++)
                    {
                        parameters.C     = CValues[i];
                        parameters.Gamma = GammaValues[j];
                        parameters.P     = PValues[k];
                        double num = Math.Abs(Training.PerformCrossValidation(problem, parameters, nrfold));
                        Console.Write("{0} {1} {2} {3}", parameters.C, parameters.Gamma, parameters.P, num);
                        if (!string.IsNullOrEmpty(outputFile))
                        {
                            File.AppendAllText(outputFile, string.Format("{0} {1} {2} {3}\n", parameters.C, parameters.Gamma, parameters.P, num));
                        }
                        list.Add(new RegressionParameter(parameters.C, parameters.Gamma, parameters.P, num));
                    }
                }
            }
            list.Sort();
            if (MaxBestCount > 0)
            {
                int l;
                for (l = Math.Min(MaxBestCount, list.Count); l < list.Count && l < MaxBestCount * 3 && list[l - 1].SortValue == list[l].SortValue; l++)
                {
                }
                if (list.Count > l)
                {
                    list.RemoveRange(l, list.Count - l);
                }
                list.TrimExcess();
            }
            return(list.ToArray());
        }
        public static void GridRegression(Problem problem, Parameter parameters, List <double> CValues, List <double> GammaValues, List <double> PValues, string outputFile, int nrfold, out double C, out double Gamma, out double P)
        {
            C     = 0.0;
            Gamma = 0.0;
            P     = 0.0;
            double num = 1.7976931348623157E+308;

            if (!string.IsNullOrEmpty(outputFile))
            {
                File.WriteAllText(outputFile, string.Empty);
            }
            for (int i = 0; i < CValues.Count; i++)
            {
                for (int j = 0; j < GammaValues.Count; j++)
                {
                    for (int k = 0; k < PValues.Count; k++)
                    {
                        parameters.C     = CValues[i];
                        parameters.Gamma = GammaValues[j];
                        parameters.P     = PValues[k];
                        double num2 = Math.Abs(Training.PerformCrossValidation(problem, parameters, nrfold));
                        Console.Write("{0} {1} {2} {3}", parameters.C, parameters.Gamma, parameters.P, num2);
                        if (!string.IsNullOrEmpty(outputFile))
                        {
                            File.AppendAllText(outputFile, string.Format("{0} {1} {2} {3}\n", parameters.C, parameters.Gamma, parameters.P, num2));
                        }
                        if (num2 < num)
                        {
                            C     = parameters.C;
                            Gamma = parameters.Gamma;
                            P     = parameters.P;
                            num   = num2;
                            Console.WriteLine(" New Min!");
                        }
                        else
                        {
                            Console.WriteLine();
                        }
                    }
                }
            }
        }
        public static void Grid(Problem problem, Parameter parameters, List <double> CValues, List <double> GammaValues, string outputFile, int nrfold, out double C, out double Gamma)
        {
            C     = 0.0;
            Gamma = 0.0;
            double num = -1.7976931348623157E+308;

            if (!string.IsNullOrEmpty(outputFile))
            {
                File.WriteAllText(outputFile, string.Empty);
            }
            for (int i = 0; i < CValues.Count; i++)
            {
                for (int j = 0; j < GammaValues.Count; j++)
                {
                    parameters.C     = CValues[i];
                    parameters.Gamma = GammaValues[j];
                    double num2 = Training.PerformCrossValidation(problem, parameters, nrfold);
                    Console.Write("{0} {1} {2}", parameters.C, parameters.Gamma, num2);
                    if (!string.IsNullOrEmpty(outputFile))
                    {
                        File.AppendAllText(outputFile, string.Format("{0} {1} {2}\n", parameters.C, parameters.Gamma, num2));
                    }
                    if (num2 > num)
                    {
                        C     = parameters.C;
                        Gamma = parameters.Gamma;
                        num   = num2;
                        Console.WriteLine(" New Maximum!");
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                }
            }
        }
 private static double GridCompute(GridComputingParameter Para)
 {
     return(Training.PerformCrossValidation(Para.problem, Para.parameter, Para.nrfold));
 }
        public static int SmartGridRegression(Problem problem, Parameter parameter, double C_Start, double C_Multi, double Gamma_Start, double Gamma_Multi, double P_Start, double P_Multi, string outputFile, int nrfold, out double C, out double Gamma, out double P)
        {
            C     = C_Start;
            Gamma = Gamma_Start;
            P     = P_Start;
            double num = -1.7976931348623157E+308;

            if (!string.IsNullOrEmpty(outputFile))
            {
                File.WriteAllText(outputFile, string.Empty);
            }
            int num2 = 0;

            double[] array = new double[21]
            {
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                C_Start,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0
            };
            double[] array2 = new double[21]
            {
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                Gamma_Start,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0
            };
            double[] array3 = new double[21]
            {
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                P_Start,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0
            };
            for (int i = 0; i < 10; i++)
            {
                array[9 - i]   = array[10 - i] / C_Multi;
                array[11 + i]  = array[10 + i] * C_Multi;
                array2[9 - i]  = array2[10 - i] / Gamma_Multi;
                array2[11 + i] = array2[10 + i] * Gamma_Multi;
                array3[9 - i]  = array3[10 - i] / P_Multi;
                array3[11 + i] = array3[10 + i] / P_Multi;
            }
            double[,,] array4 = new double[21, 21, 21];
            Point3D point3D = new Point3D(10, 10, 10);

            Point3D[] array5 = ParameterSelection.FindNeedCompute(array4, point3D);
            while (array5.Length > 0)
            {
                for (int j = 0; j < array5.Length; j++)
                {
                    parameter.C     = array[array5[j].C];
                    parameter.Gamma = array2[array5[j].G];
                    parameter.P     = array3[array5[j].P];
                    double num3 = Training.PerformCrossValidation(problem, parameter, nrfold);
                    array4[array5[j].C, array5[j].G, array5[j].P] = num3;
                    Console.Write(string.Format("{0} {1} {2} {3}", array[array5[j].C], array2[array5[j].G], array3[array5[j].P], num3));
                    if (!string.IsNullOrEmpty(outputFile))
                    {
                        File.AppendAllText(outputFile, string.Format("{0} {1} {2} {3}\n", array[array5[j].C], array2[array5[j].G], array3[array5[j].P], num3));
                    }
                    if (num3 < num)
                    {
                        num       = num3;
                        point3D.C = array5[j].C;
                        point3D.G = array5[j].G;
                        point3D.P = array5[j].P;
                        C         = array[array5[j].C];
                        Gamma     = array2[array5[j].G];
                        P         = array3[array5[j].P];
                        Console.WriteLine(" New Min!");
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                    num2++;
                }
                array5 = ParameterSelection.FindNeedCompute(array4, point3D);
            }
            return(num2);
        }
        public static int SmartGrid(Problem problem, Parameter parameters, double C_Start, double C_Multi, double Gamma_Start, double Gamma_Multi, string outputFile, int nrfold, out double C, out double Gamma, bool Parallel = true)
        {
            C     = C_Start;
            Gamma = Gamma_Start;
            double num = -1.7976931348623157E+308;

            if (!string.IsNullOrEmpty(outputFile))
            {
                File.WriteAllText(outputFile, string.Empty);
            }
            int num2 = 0;

            double[,] array = new double[21, 21];
            double[] array2 = new double[21]
            {
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                C_Start,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0
            };
            double[] array3 = new double[21]
            {
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                Gamma_Start,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0
            };
            for (int i = 0; i < 10; i++)
            {
                array2[9 - i]  = array2[10 - i] / C_Multi;
                array2[11 + i] = array2[10 + i] * C_Multi;
                array3[9 - i]  = array3[10 - i] / Gamma_Multi;
                array3[11 + i] = array3[10 + i] * Gamma_Multi;
            }
            Point p = new Point(10, 10);

            Point[] array4 = ParameterSelection.FindNeedCompute(array, p, TrendLineDirection.LeftBelow_to_RightHigh);
            while (array4.Length > 0)
            {
                Task <double>[] array5 = new Task <double> [array4.Length];
                if (Parallel)
                {
                    for (int j = 0; j < array4.Length; j++)
                    {
                        Parameter parameter = (Parameter)parameters.Clone();
                        parameter.C     = array2[array4[j].X];
                        parameter.Gamma = array3[array4[j].Y];
                        GridComputingParameter state = new GridComputingParameter(problem, parameter, nrfold);
                        array5[j] = new Task <double>((object obj) => ParameterSelection.GridCompute((GridComputingParameter)obj), state);
                        array5[j].Start();
                    }
                }
                for (int k = 0; k < array4.Length; k++)
                {
                    double num3;
                    if (Parallel)
                    {
                        num3 = array5[k].Result;
                    }
                    else
                    {
                        parameters.C     = array2[array4[k].X];
                        parameters.Gamma = array3[array4[k].Y];
                        num3             = Training.PerformCrossValidation(problem, parameters, nrfold);
                    }
                    array[array4[k].X, array4[k].Y] = num3;
                    Console.Write(string.Format("{0} {1} {2}", array2[array4[k].X], array3[array4[k].Y], num3));
                    if (!string.IsNullOrEmpty(outputFile))
                    {
                        File.AppendAllText(outputFile, string.Format("{0} {1} {2}\n", array2[array4[k].X], array3[array4[k].Y], num3));
                    }
                    if (num3 > num)
                    {
                        num   = num3;
                        p.X   = array4[k].X;
                        p.Y   = array4[k].Y;
                        C     = array2[array4[k].X];
                        Gamma = array3[array4[k].Y];
                        Console.WriteLine(" New Maximum!");
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                    num2++;
                }
                array4 = ParameterSelection.FindNeedCompute(array, p, TrendLineDirection.LeftBelow_to_RightHigh);
            }
            return(num2);
        }