Exemplo n.º 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);
 }
Exemplo n.º 2
0
 /// <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);
 }
Exemplo n.º 3
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
            GABIZ.Base.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]);
        }