示例#1
0
 public StepValueDeriv(DiffFunc1D func, Float initStep, Float initVal, Float initDeriv)
 {
     _func  = func;
     _step  = initStep;
     _value = initVal;
     _deriv = initDeriv;
 }
示例#2
0
        /// <summary>
        /// Finds a local minimum of the function
        /// </summary>
        /// <param name="f">Function to minimize</param>
        /// <param name="initVal">Value of function at 0</param>
        /// <param name="initDeriv">Derivative of function at 0</param>
        /// <returns>Minimizing value</returns>
        public Float Minimize(DiffFunc1D f, Float initVal, Float initDeriv)
        {
            Contracts.Check(initDeriv < 0, "Cannot search in direction of ascent!");

            Float dummy;

            for (_step *= 2; ; _step /= 2)
            {
                Float newVal = f(_step, out dummy);
                if (newVal <= initVal + _c1 * _step * initDeriv)
                {
                    return(_step);
                }
            }
        }
示例#3
0
        private static void RunTest(DiffFunc1D f)
        {
            CubicInterpLineSearch cils = new CubicInterpLineSearch((Float)1e-8);
            Float val;
            Float deriv;

            val = f(0, out deriv);
            Float min = cils.Minimize(f, val, deriv);

            val = f(min, out deriv);
            Console.WriteLine(deriv);
            GoldenSectionSearch gss = new GoldenSectionSearch((Float)1e-8);

            min = gss.Minimize(f);
            val = f(min, out deriv);
            Console.WriteLine(deriv);
        }
示例#4
0
        /// <summary>
        /// Finds a local minimum of the function
        /// </summary>
        /// <param name="func">Function to minimize</param>
        /// <returns>Minimizing value</returns>
        public Float Minimize(DiffFunc1D func)
        {
            Float d;

            return(Minimize(x => func(x, out d)));
        }
示例#5
0
 /// <summary>
 /// Finds a local minimum of the function
 /// </summary>
 /// <param name="f">Function to minimize</param>
 /// <param name="initVal">Value of function at 0</param>
 /// <param name="initDeriv">Derivative of function at 0</param>
 /// <returns>Minimizing value</returns>
 public Float Minimize(DiffFunc1D f, Float initVal, Float initDeriv)
 {
     return(Minimize(f));
 }
示例#6
0
        private Float FindMinimum(DiffFunc1D func, Float initValue, Float initDeriv)
        {
            Contracts.CheckParam(initDeriv < 0, nameof(initDeriv), "Cannot search in direction of ascent!");

            StepValueDeriv lo = new StepValueDeriv(func, 0, initValue, initDeriv);
            StepValueDeriv hi = new StepValueDeriv(func, _step);

            // bracket minimum
            while (hi.Deriv < 0)
            {
                Swap(ref lo, ref hi);
                if (lo.Step >= MaxStep)
                {
                    return(MaxStep);
                }
                hi.Step = lo.Step * 2;
            }

            Float window = 1;

            StepValueDeriv mid = new StepValueDeriv(func);

            for (int numSteps = 1; ; ++numSteps)
            {
                Float interp = CubicInterp(lo, hi);
                if (window <= MinWindow || numSteps == MaxNumSteps)
                {
                    return(interp);
                }

                // insure minimal progress to narrow interval
                Float minProgressStep = _minProgress * (hi.Step - lo.Step);
                Float maxMid          = hi.Step - minProgressStep;
                if (interp > maxMid)
                {
                    interp = maxMid;
                }
                Float minMid = lo.Step + minProgressStep;
                if (interp < minMid)
                {
                    interp = minMid;
                }

                mid.Step = interp;

                if (mid.Deriv == 0 || mid.Step == lo.Step || mid.Step == hi.Step)
                {
                    return(mid.Step);
                }

                if (mid.Deriv < 0)
                {
                    Swap(ref lo, ref mid);
                }
                else
                {
                    Swap(ref hi, ref mid);
                }

                if (lo.Step >= MaxStep)
                {
                    return(MaxStep);
                }

                window = (hi.Step - lo.Step) / hi.Step;
            }
        }
示例#7
0
 /// <summary>
 /// Finds a local minimum of the function
 /// </summary>
 /// <param name="func">Function to minimize</param>
 /// <param name="initValue">Value of function at 0</param>
 /// <param name="initDeriv">Derivative of function at 0</param>
 /// <returns>Minimizing value</returns>
 public Float Minimize(DiffFunc1D func, Float initValue, Float initDeriv)
 {
     _step = FindMinimum(func, initValue, initDeriv);
     return(Math.Min(_step, MaxStep));
 }
示例#8
0
 public StepValueDeriv(DiffFunc1D func, Float initStep)
 {
     _func = func;
     Step  = initStep;
 }
示例#9
0
 public StepValueDeriv(DiffFunc1D func)
 {
     _func = func;
 }