Beispiel #1
0
 /// <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);
 }
Beispiel #3
0
        /// <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.Cartesian(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, parallelOptions, 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));
        }
Beispiel #4
0
        /// <summary>
        ///   Searches for the best combination of parameters that results in the most accurate model.
        /// </summary>
        ///
        /// <param name="bestParameters">The best combination of parameters found by the grid search.</param>
        /// <param name="error">The minimum error of the best model found by the grid search.</param>
        /// <returns>The best model found during the grid search.</returns>
        ///
        public TModel Compute(out GridSearchParameterCollection bestParameters, out double error)
        {
            // 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];
            int best;

            // 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 to fit a model using the parameters
                models[i] = Fitting(parameters[i], out errors[i]);
            });


            // Select the minimum error
            error          = errors.Min(out best);
            bestParameters = parameters[best];

            // Return the best model found.
            return(models[best]);
        }