Ejemplo n.º 1
0
        override public double Run(Converter <OptimizationParameter, double> oneDRealFunction,
                                   /*ref*/ OptimizationParameter param, int gridLineCount, out double bestInput)
        {
            try
            {
                return(Brent.Run(oneDRealFunction, param, gridLineCount, out bestInput));
            }
            catch (Exception exception)
            {
                Console.WriteLine("Error calling Brent");
                Console.WriteLine(exception.Message);
                if (exception.InnerException != null)
                {
                    Console.WriteLine(exception.InnerException.Message);
                }
            }

            return(Grid.Run(oneDRealFunction, param, gridLineCount, out bestInput));
        }
        public double Optimize(Converter <OptimizationParameterList, double> functionToOptimize,
                               //ref List<double> pointList, List<double> lowList, List<double> highList,
                               /*ref*/ OptimizationParameterList paramList, int numberOfIterationsOverParameters, int gridLineCount)
        {
            double eps = 1e-9;

            List <double> pointList = paramList.ExtractParameterValueListForSearch();
            List <double> lowList   = paramList.ExtractParameterLowListForSearch();
            List <double> highList  = paramList.ExtractParameterHighListForSearch();

            double oldScore     = double.NaN;
            double tempOldScore = functionToOptimize(paramList);
            //Console.WriteLine(tempOldScore);
            Dictionary <int, double> paramNumberToInitValue = new Dictionary <int, double>();

            for (int iterationOverParameters = 0; iterationOverParameters < numberOfIterationsOverParameters; ++iterationOverParameters)
            {
                double newScore = double.NaN;
                int    doSearchParameterCount = 0;
                foreach (OptimizationParameter paramToOptimize in paramList)
                {
                    //if (paramList.DoSearch(iParameter))
                    if (paramToOptimize.DoSearch)
                    {
                        ++doSearchParameterCount;

                        //double initValue = SpecialFunctions.GetValueOrDefault(paramNumberToInitValue, doSearchParameterCount, paramToOptimize.Value);
                        Debug.WriteLine("Search Param " + doSearchParameterCount.ToString());

                        double bestInput;
                        newScore = OneDOptimization.Run(
                            delegate(OptimizationParameter param)
                        {
                            if (!double.IsNaN(param.Value) && paramList.SatisfiesConditions())
                            {
                                return(functionToOptimize(paramList));
                            }
                            else
                            {
                                return(double.NaN);
                            }
                        },
                            /*ref*/ paramToOptimize, gridLineCount, out bestInput);

                        Debug.WriteLine("bestInput " + bestInput.ToString());
                        if (newScore > 0)
                        {
                            Debug.Write("stop.");
                        }
                        paramToOptimize.ValueForSearch = bestInput;

                        //Debug.WriteLine("END ITER:" + SpecialFunctions.CreateTabString2(point) + SpecialFunctions.CreateTabString("", newScore));
                    }
                }
                if ((!double.IsNaN(oldScore) && Math.Abs(oldScore - newScore) < eps) ||
                    doSearchParameterCount < 2)    //If only 0 or 1 searchable params, then one pass is enough
                {
                    oldScore = newScore;
                    break;
                }
                oldScore = newScore;
            }


            return(oldScore);
        }