コード例 #1
0
        public double[] Solve(
            Func <double[], double> f,
            Func <double[], double>[] df,
            double[] startValue,
            int nIter)
        {
            OptVector xOld          = new OptVector(startValue);
            OptVector xNew          = new OptVector();
            OptVector derivativeOld = OptimizationHelper.Derivative(df, xOld);
            OptVector direction     = -1.0 * derivativeOld;

            OptVector derivativeNew = new OptVector();

            for (int i = 0; i < nIter; i++)
            {
                StepSize = strongWolfeLineSearch.GetStepLength(f, df, direction, xOld, 20);

                xNew = xOld + StepSize * direction;

                if (CheckEarlyExit(xNew, xOld))
                {
                    break;
                }

                derivativeNew = OptimizationHelper.Derivative(df, xNew);

                double beta = PolakRibiere(derivativeNew, derivativeOld);

                direction = beta * direction - derivativeNew;

                xOld          = xNew;
                derivativeOld = derivativeNew;
            }
            return(xNew.MinArray);
        }
コード例 #2
0
        public double GetStepLength(
            Func <double[], double> f,
            Func <double[], double>[] df,
            OptVector d,
            OptVector x0,
            double alpham)
        {
            int    maxIter = 15;
            double alphap  = 0;

            double c1 = 1E-4;
            double c2 = 0.5;

            Random rnd = new Random();

            double alphax = 1;

            double fx0 = f(x0.MinArray);
            double gx0 = OptimizationHelper.Derivative(df, x0) * d;

            double fxp = fx0;
            double gxp = gx0;

            for (int i = 1; i < maxIter; i++)
            {
                OptVector xx  = x0 + alphax * d;
                double    fxx = f(xx.MinArray);
                double    gxx = OptimizationHelper.Derivative(df, xx) * d;

                if (fxx > fx0 + c1 * alphax * gx0 ||
                    (i > 1 && fxx >= fxp))
                {
                    return(Zoom(f, df, x0, d, alphap, alphax));
                }

                if (Math.Abs(gxx) <= -c2 * gx0)
                {
                    return(alphax);
                }

                if (gxx >= 0)
                {
                    return(Zoom(f, df, x0, d, alphax, alphap));
                }

                alphap = alphax;
                fxp    = fxx;
                gxp    = gxx;

                alphax = alphax + (alpham - alphax) * 0.3;
            }

            return(alphax);
        }
コード例 #3
0
        public IEnumerable <Tuple <Expert, double> > GetOptimalWeights()
        {
            var wdm = new Func <double, double> (alpha => {
                const int cdm = 1;                 // manager.GetDMCalibrationScore ();
                var idm       = GetDMInformationScore(alpha);
                var sum       = GetWeights(alpha).Sum(x => x.Item2);
                return(sum > 0 ? ((cdm * idm) / sum) : (cdm * idm));
            });

            var upperbound = GetWeights().Max(x => x.Item2);

            var optimalAlpha = OptimizationHelper.LocalMin(0, upperbound, x => - wdm(x), 1.2e-16, Math.Sqrt(Double.Epsilon));

            Console.WriteLine(optimalAlpha);
            return(GetWeights(optimalAlpha));
        }
コード例 #4
0
        private static double Zoom(
            Func <double[], double> f,
            Func <double[], double>[] df,
            OptVector x0,
            OptVector d,
            double alphal,
            double alphah)
        {
            int    maxIter = 10;
            double c1      = 1E-4;
            double c2      = 0.5;

            double fx0 = f(x0.MinArray);
            double gx0 = OptimizationHelper.Derivative(df, x0) * d;

            double alphax = 0.0;

            for (int i = 0; i < maxIter; i++)
            {
                alphax = 0.5 * (alphal + alphah);
                OptVector xx  = x0 + alphax * d;
                double    fxx = f(xx.MinArray);
                double    gxx = OptimizationHelper.Derivative(df, xx) * d;
                OptVector xl  = x0 + alphal * d;
                double    fxl = f(xl.MinArray);

                if (fxx > fx0 + c1 * alphax * gx0 ||
                    fxx >= fxl)
                {
                    alphah = alphax;
                }
                else
                {
                    if (Math.Abs(gxx) <= -c2 * gx0)
                    {
                        return(alphax);
                    }
                    if (gxx * (alphah - alphal) >= 0.0)
                    {
                        alphah = alphal;
                    }
                    alphal = alphax;
                }
            }
            return(alphax);
        }