static public OptimizationParameter GetTanInstance(string name, double value, bool doSearch, double low, double high)
        {
            OptimizationParameter parameter = new OptimizationParameter();

            parameter.Name     = name;
            parameter.Value    = value;
            parameter.DoSearch = doSearch;
            //parameter.LowForSearch = parameter.TransformForSearch(parameter.Low);
            //parameter.HighForSearch = parameter.TransformForSearch(parameter.High);
            parameter.ConvertToTanInstance(low, high);

            return(parameter);
        }
        public OptimizationParameter Clone()
        {
            OptimizationParameter parameter = new OptimizationParameter();

            parameter.TransformForSearch  = TransformForSearch;
            parameter.TransformFromSearch = TransformFromSearch;
            parameter.Name     = Name;
            parameter.Value    = Value;
            parameter.DoSearch = DoSearch;
            parameter.Low      = Low;
            parameter.High     = High;

            return(parameter);
        }
        static public OptimizationParameter GetPositiveFactorInstance(string name, double value, bool doSearch)
        {
            OptimizationParameter parameter = new OptimizationParameter();

            //parameter.TransformForSearch = delegate(double r) { return Math.Log(r); };
            //parameter.TransformFromSearch = delegate(double r) { return Math.Exp(r); };
            //parameter.Low = 1.0 / 1000.0;
            //parameter.High = 1000;
            //parameter.LowForSearch = parameter.TransformForSearch(parameter.Low);
            //parameter.HighForSearch = parameter.TransformForSearch(parameter.High);

            parameter.Name     = name;
            parameter.Value    = value;
            parameter.DoSearch = doSearch;
            parameter.ConvertToPositiveFactorInstance();

            return(parameter);
        }
示例#4
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));
        }
        static public OptimizationParameter GetProbabilityInstance(string name, double value, bool doSearch)
        {
            OptimizationParameter parameter = new OptimizationParameter();

            //parameter.TransformForSearch = SpecialFunctions.LogOdds;
            //parameter.TransformFromSearch = SpecialFunctions.Probability;
            //parameter.Low = .001;
            //parameter.High = .999;
            //parameter.LowForSearch = parameter.TransformForSearch(parameter.Low);
            //parameter.HighForSearch = parameter.TransformForSearch(parameter.High);

            parameter.Name     = name;
            parameter.Value    = value;
            parameter.DoSearch = doSearch;

            parameter.ConvertToProbabilityInstance();

            return(parameter);
        }
示例#6
0
        override public double Run(Converter <OptimizationParameter, double> oneDRealFunction,
                                   OptimizationParameter param, int gridLineCount, out double bestInput)
        {
            double low  = param.LowForSearch;
            double high = param.HighForSearch;
            Converter <double, double> oneDRealFunctionInDoubleSpace = delegate(double d)
            {
                param.ValueForSearch = d;
                return(oneDRealFunction(param));
            };

            double initParamValue = param.ValueForSearch;
            BestSoFar <double, double> bestFound = OneDOptimizationX(oneDRealFunctionInDoubleSpace, low, high, gridLineCount);

            bestFound.Compare(oneDRealFunctionInDoubleSpace(initParamValue), initParamValue); // make sure we didn't get any worse

            bestInput            = bestFound.Champ;
            param.ValueForSearch = bestFound.Champ;
            return(bestFound.ChampsScore);
        }
        override public double Run(Converter <OptimizationParameter, double> oneDRealFunction,
                                   /*ref*/ OptimizationParameter param, int gridLineCount, out double bestInput)
        {
            ++DebugCount;

            double outputTol = .00000001;

            Converter <double, double> oneDRealFunctionInDoubleSpace = delegate(double d)
            {
                Debug.Assert(!double.IsNaN(d));
                param.ValueForSearch = d;
                try
                {
                    double odrf = oneDRealFunction(param);
                    if (double.IsNaN(odrf))
                    {
                        return(double.PositiveInfinity);
                    }
                    //Debug.Assert(!double.IsNegativeInfinity(odrf)); //!!!for debugging
                    return(-odrf);
                }
                catch (Exception exception)
                {
                    Console.WriteLine("Exception turned to NaN in OneDOptimizationBrent");
                    Console.WriteLine(exception.Message);
                    if (exception.InnerException != null)
                    {
                        Console.WriteLine(exception.InnerException.Message);
                    }
                    return(double.NaN);
                }
            };

            double a = param.LowForSearch;
            double b = param.ValueForSearch;

            Debug.WriteLine(SpecialFunctions.CreateTabString(DebugCount, "Get a and b from param\n", "", "a", a, "\n", "", "b", b, "\n"));

            double c;

            if (a > b || Math.Abs(a - b) < InputTol || double.IsInfinity(b))
            {
                c = param.HighForSearch;
                b = a + GoldenRatio * (c - a);
                Debug.WriteLine(SpecialFunctions.CreateTabString(DebugCount, "if (a > b || Math.Abs(a - b) < inputTol || double.IsInfinity(b)){c = param.HighForSearch;b = a + goldenRatio * (c - a);}\n", "", "a", a, "\n", "", "b", b, "\n", "", "c", c, "\n"));
            }
            else
            {
                Debug.Assert(a <b && Math.Abs(a - b)> InputTol);   // real assert
                c = b + InvGold * (b - a);
                Debug.WriteLine(SpecialFunctions.CreateTabString(DebugCount, "NOT if (a > b || Math.Abs(a - b) < inputTol || double.IsInfinity(b)){c = b + invGold * (b - a);}", "a", a, "", "\n", "", "b", b, "", "\n", "", "c", c, "", "\n"));
            }


            double fOfA;
            double fOfB;
            double fOfC;

            if (FindBracketWithInfinities(ref a, ref b, ref c, oneDRealFunctionInDoubleSpace, InputTol, outputTol, out bestInput, out fOfA, out fOfB, out fOfC, DebugCount))
            {
                return(-fOfB);
            }


            Debug.WriteLine(SpecialFunctions.CreateTabString(DebugCount, "About to enter Brent\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));
            SpecialFunctions.CheckCondition(fOfB < fOfA && fOfB < fOfC, string.Format("About to enter Brent, but prediction, f(b)<f(a) && f(b) < f(c), has failed"));
            FuncMin.FuncMinBrent(a, ref b, c, fOfA, ref fOfB, fOfC, InputTol, oneDRealFunctionInDoubleSpace);
            Debug.WriteLine(SpecialFunctions.CreateTabString(DebugCount, "After Brent\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));
            bestInput = b;
            return(-fOfB);
        }
示例#8
0
 abstract public double Run(Converter <OptimizationParameter, double> oneDRealFunction, OptimizationParameter paramToOptimize, int gridLineCount, out double bestInput);