Exemplo n.º 1
0
        public void DefaultBatchTest()
        {
            foreach (var problem in problems)
            {
                LBFGSComparer cmp = new LBFGSComparer();

                var expected = cmp.Expected(problem);
                var actual   = cmp.Actual(problem);

                Assert.AreEqual(expected.Length, actual.Length);
                for (int i = 0; i < expected.Length; i++)
                {
                    var a = actual[i];
                    var e = expected[i];

                    Assert.AreEqual(e.fx, a.Value);

                    for (int j = 0; j < e.g.Length; j++)
                    {
                        Assert.AreEqual(e.g[j], a.Gradient[j]);
                    }
                    Assert.AreEqual(e.gnorm, a.GradientNorm);
                    Assert.AreEqual(e.step, a.Step);
                }
            }
        }
Exemplo n.º 2
0
        private static void inner(List <Specification> problems, int m,
                                  double epsilon, int past, double delta, int max_iterations, int linesearch)
        {
            bool executed = false;

            for (var ftol = 1e-8; ftol < 1e-5; ftol *= 1000)
            {
                for (var wolfe = 0.0; wolfe <= 1.0; wolfe += 0.4)
                {
                    for (var gtol = 0.0; gtol <= 1.0; gtol += 0.5)
                    {
                        for (var xtol = 1.0e-16; xtol < 1e-6; xtol *= 10000)
                        {
                            for (var orthantwise_c = 0.0; orthantwise_c < 2; orthantwise_c += 0.4)
                            {
                                for (var orthantwise_start = 0; orthantwise_start < 2; orthantwise_start++)
                                {
                                    for (var orthantwise_end = -1; orthantwise_end < 2; orthantwise_end++)
                                    {
                                        executed = true;

                                        LBFGSComparer cmp = new LBFGSComparer()
                                        {
                                            m              = m,
                                            epsilon        = epsilon,
                                            past           = past,
                                            delta          = delta,
                                            max_iterations = max_iterations,
                                            linesearch     = (LineSearch)linesearch,
                                            //max_linesearch = max_linesearch,
                                            // min_step = min_step,
                                            // max_step = max_step,
                                            ftol              = ftol,
                                            wolfe             = wolfe,
                                            gtol              = gtol,
                                            xtol              = xtol,
                                            orthantwise_c     = orthantwise_c,
                                            orthantwise_start = orthantwise_start,
                                            orthantwise_end   = orthantwise_end
                                        };

                                        compute(problems, cmp);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            Assert.IsTrue(executed);
        }
Exemplo n.º 3
0
        private static void compute(List <Specification> problems, LBFGSComparer cmp)
        {
            foreach (var problem in problems)
            {
                string actualStr   = String.Empty;
                string expectedStr = String.Empty;

                OptimizationProgressEventArgs[] actual = null;

                try { actual = cmp.Actual(problem); }
                catch (Exception ex)
                {
                    actualStr = ex.Data["Code"] as string;
                    if (actualStr == null)
                    {
                        throw;
                    }
                }

                var expected = cmp.Expected(problem);
                expectedStr = cmp.NativeCode;

                if (actualStr == String.Empty)
                {
                    actualStr = cmp.ActualMessage;
                }

                Assert.AreEqual(expectedStr, actualStr);

                if (expectedStr != "LBFGS_SUCCESS")
                {
                    continue;
                }

                Assert.AreEqual(expected.Length, actual.Length);
                for (int i = 0; i < expected.Length; i++)
                {
                    var a = actual[i];
                    var e = expected[i];

                    Assert.AreEqual(e.fx, a.Value);

                    for (int j = 0; j < e.g.Length; j++)
                    {
                        Assert.AreEqual(e.g[j], a.Gradient[j]);
                    }
                    Assert.AreEqual(e.gnorm, a.GradientNorm);
                    Assert.AreEqual(e.step, a.Step);
                }
            }
        }
Exemplo n.º 4
0
        public void ParameterTest1()
        {
            LBFGSComparer cmp = new LBFGSComparer()
            {
                m                 = 2,
                epsilon           = 0.000000099999999999999995,
                past              = 0,
                delta             = 0.00000001,
                max_iterations    = 0,
                linesearch        = (LineSearch)0,
                ftol              = 0.00000001,
                wolfe             = 0,
                gtol              = 0,
                xtol              = 0.000000000000000099999999999999998,
                orthantwise_c     = 0,
                orthantwise_start = 0,
                orthantwise_end   = -1
            };

            compute(problems, cmp);
        }
Exemplo n.º 5
0
        public void InvalidLineSearchTest()
        {
            double t = 0;
            double s = 0;
            int    n = 10;

            Func <double[], double> function = (parameters) =>
            {
                t = parameters[0];
                s = parameters[1];

                return(-(n * Math.Log(s) - n * Math.Log(Math.PI)));
            };

            Func <double[], double[]> gradient = (parameters) =>
            {
                t = parameters[0];
                s = parameters[1];

                double dt = -2.0;
                double ds = +2.0 - n / s;

                return(new[] { dt, ds });
            };

            double[] start = { 0, 0 };


            Specification problem = new Specification(2, function, gradient, start);
            LBFGSComparer cmp     = new LBFGSComparer();

            compute(new List <Specification>()
            {
                problem
            }, cmp);
        }
Exemplo n.º 6
0
        public void DefaultBatchTest()
        {
            foreach (var problem in problems)
            {
                LBFGSComparer cmp = new LBFGSComparer();

                var expected = cmp.Expected(problem);
                var actual = cmp.Actual(problem);

                Assert.AreEqual(expected.Length, actual.Length);
                for (int i = 0; i < expected.Length; i++)
                {
                    var a = actual[i];
                    var e = expected[i];

                    Assert.AreEqual(e.fx, a.Value);

                    for (int j = 0; j < e.g.Length; j++)
                        Assert.AreEqual(e.g[j], a.Gradient[j]);
                    Assert.AreEqual(e.gnorm, a.GradientNorm);
                    Assert.AreEqual(e.step, a.Step);
                }
            }
        }
Exemplo n.º 7
0
        public void InvalidLineSearchTest()
        {
            double t = 0;
            double s = 0;
            int n = 10;

            Func<double[], double> function = (parameters) =>
            {
                t = parameters[0];
                s = parameters[1];

                return -(n * Math.Log(s) - n * Math.Log(Math.PI));
            };

            Func<double[], double[]> gradient = (parameters) =>
            {
                t = parameters[0];
                s = parameters[1];

                double dt = -2.0;
                double ds = +2.0 - n / s;

                return new[] { dt, ds };
            };

            double[] start = { 0, 0 };


            Specification problem = new Specification(2, function, gradient, start);
            LBFGSComparer cmp = new LBFGSComparer();

            compute(new List<Specification>() { problem }, cmp);
        }
Exemplo n.º 8
0
        private static void compute(List<Specification> problems, LBFGSComparer cmp)
        {
            foreach (var problem in problems)
            {
                string actualStr = String.Empty;
                string expectedStr = String.Empty;

                OptimizationProgressEventArgs[] actual = null;

                try { actual = cmp.Actual(problem); }
                catch (Exception ex)
                {
                    actualStr = ex.Data["Code"] as string;
                    if (actualStr == null)
                        throw;
                }

                var expected = cmp.Expected(problem);
                expectedStr = cmp.NativeCode;

                if (actualStr == String.Empty)
                    actualStr = cmp.ActualMessage;

                Assert.AreEqual(expectedStr, actualStr);

                if (expectedStr != "LBFGS_SUCCESS")
                    continue;

                Assert.AreEqual(expected.Length, actual.Length);
                for (int i = 0; i < expected.Length; i++)
                {
                    var a = actual[i];
                    var e = expected[i];

                    Assert.AreEqual(e.fx, a.Value);

                    for (int j = 0; j < e.g.Length; j++)
                        Assert.AreEqual(e.g[j], a.Gradient[j]);
                    Assert.AreEqual(e.gnorm, a.GradientNorm);
                    Assert.AreEqual(e.step, a.Step);
                }
            }
        }
Exemplo n.º 9
0
        private static void inner(List<Specification> problems, int m,
            double epsilon, int past, double delta, int max_iterations, int linesearch)
        {
            bool executed = false;

            for (var ftol = 1e-8; ftol < 1e-5; ftol *= 1000)
                for (var wolfe = 0.0; wolfe <= 1.0; wolfe += 0.4)
                    for (var gtol = 0.0; gtol <= 1.0; gtol += 0.5)
                        for (var xtol = 1.0e-16; xtol < 1e-6; xtol *= 10000)
                            for (var orthantwise_c = 0.0; orthantwise_c < 2; orthantwise_c += 0.4)
                                for (var orthantwise_start = 0; orthantwise_start < 2; orthantwise_start++)
                                    for (var orthantwise_end = -1; orthantwise_end < 2; orthantwise_end++)
                                    {
                                        executed = true;

                                        LBFGSComparer cmp = new LBFGSComparer()
                                        {
                                            m = m,
                                            epsilon = epsilon,
                                            past = past,
                                            delta = delta,
                                            max_iterations = max_iterations,
                                            linesearch = (LineSearch)linesearch,
                                            //max_linesearch = max_linesearch,
                                            // min_step = min_step,
                                            // max_step = max_step,
                                            ftol = ftol,
                                            wolfe = wolfe,
                                            gtol = gtol,
                                            xtol = xtol,
                                            orthantwise_c = orthantwise_c,
                                            orthantwise_start = orthantwise_start,
                                            orthantwise_end = orthantwise_end
                                        };

                                        compute(problems, cmp);
                                    }

            Assert.IsTrue(executed);
        }
Exemplo n.º 10
0
        public void ParameterTest1()
        {
            LBFGSComparer cmp = new LBFGSComparer()
            {
                m = 2,
                epsilon = 0.000000099999999999999995,
                past = 0,
                delta = 0.00000001,
                max_iterations = 0,
                linesearch = (LineSearch)0,
                ftol = 0.00000001,
                wolfe = 0,
                gtol = 0,
                xtol = 0.000000000000000099999999999999998,
                orthantwise_c = 0,
                orthantwise_start = 0,
                orthantwise_end = -1
            };

            compute(problems, cmp);
        }