private static double GridCompute(GridComputingParameter Para) { return(Training.PerformCrossValidation(Para.problem, Para.parameter, Para.nrfold)); }
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); }