/// <summary> /// Gets the array of GridSearchParameters to try. /// </summary> /// public GridSearchParameter[] GetParameters() { GridSearchParameter[] parameters = new GridSearchParameter[Values.Length]; for (int i = 0; i < Values.Length; i++) { parameters[i] = new GridSearchParameter(Name, Values[i]); } return(parameters); }
/// <summary> /// Determines whether the specified object is equal /// to the current GridSearchParameter object. /// </summary> /// public override bool Equals(object obj) { if (obj is GridSearchParameter) { GridSearchParameter g = (GridSearchParameter)obj; if (g.name != name || g.value != value) { return(false); } return(true); } return(false); }
/// <summary> /// Searches for the best combination of parameters that results in the most accurate model. /// </summary> /// /// <returns>The results found during the grid search.</returns> /// public GridSearchResult <TModel> Compute() { // Get the total number of different parameters var values = new GridSearchParameter[ranges.Count][]; for (int i = 0; i < values.Length; i++) { values[i] = ranges[i].GetParameters(); } // Generate the Cartesian product between all parameters GridSearchParameter[][] grid = Matrix.CartesianProduct(values); // Initialize the search var parameters = new GridSearchParameterCollection[grid.Length]; var models = new TModel[grid.Length]; var errors = new double[grid.Length]; var msgs = new string[grid.Length]; // Search the grid for the optimal parameters Parallel.For(0, grid.Length, i => { // Get the current parameters for the current point parameters[i] = new GridSearchParameterCollection(grid[i]); try { // Try to fit a model using the parameters models[i] = Fitting(parameters[i], out errors[i]); } catch (ConvergenceException ex) { errors[i] = Double.PositiveInfinity; msgs[i] = ex.Message; } }); // Select the minimum error int best; errors.Min(out best); // Return the best model found. return(new GridSearchResult <TModel>(grid.Length, parameters, models, errors, best)); }