Beispiel #1
0
        public void SolveProblem_HessianApproximation_ReturnsSucceededStatus()
        {
            var instance = new HS040(true, true, false);
            var x        = new[] { 0.8, 0.8, 0.8, 0.8 };

            const IpoptReturnCode expected = IpoptReturnCode.Solve_Succeeded;
            double obj;
            var    actual = instance.SolveProblem(x, out obj, null, null, null, null);

            Assert.AreEqual(expected, actual);
        }
Beispiel #2
0
        public void SolveProblem_HessianApproximation_VariablesEqualOptimalValues()
        {
            var instance = new HS040(true, true, false);
            var actual   = new[] { 0.8, 0.8, 0.8, 0.8 };

            var    expected = new[] { 0.793701, 0.707107, 0.529732, 0.840896 };
            double obj;

            instance.SolveProblem(actual, out obj, null, null, null, null);

            CollectionAssert.AreEqual(expected, actual, new DoubleComparer(1.0e-5));
        }
Beispiel #3
0
        public void SolveProblem_HessianApproximation_ReturnsTrueOptimalValue()
        {
            var instance = new HS040(true, true, false);
            var x        = new[] { 0.8, 0.8, 0.8, 0.8 };

            const double expected = -0.25;
            double       actual;

            instance.SolveProblem(x, out actual, null, null, null, null);

            Assert.AreEqual(expected, actual, 1e-7);
        }
Beispiel #4
0
        public void SolveProblem_HessianApproximation_ConstraintsSatisfied()
        {
            var instance = new HS040(true, true, false);
            var x        = new[] { 0.8, 0.8, 0.8, 0.8 };

            var    expected = new[] { 1.0, 0.0, 0.0 };
            double obj;
            var    actual = new double[3];

            instance.SolveProblem(x, out obj, actual, null, null, null);

            CollectionAssert.AreEqual(expected, actual, new DoubleComparer(1.0e-5));
        }
Beispiel #5
0
        public void SolveProblem_HessianApproximation_NumberOfIterationsSufficientlyLow()
        {
            var instance = new HS040(true, true, true);
            var x        = new[] { 0.8, 0.8, 0.8, 0.8 };

            const int expected = 20;
            double    obj;

            instance.SolveProblem(x, out obj, null, null, null, null);
            var actual = instance.NumberIterations;

            Assert.Greater(actual, 0);
            Assert.Less(actual, expected);
        }
Beispiel #6
0
        public void SolveProblem_ExactHessian_NumberOfIterationsLessThanOrEqualToHessianApprox()
        {
            // Get number of iterations for Hessian approximation
            var    approx = new HS040(true, true, true);
            var    x      = new[] { 0.8, 0.8, 0.8, 0.8 };
            double obj;

            approx.SolveProblem(x, out obj, null, null, null, null);
            var expected = approx.NumberIterations;

            // Get number of iterations for exact Hessian
            var exact = new HS040(true, false, true);

            x = new[] { 0.8, 0.8, 0.8, 0.8 };
            exact.SolveProblem(x, out obj, null, null, null, null);
            var actual = exact.NumberIterations;

            Assert.LessOrEqual(actual, expected);
        }
Beispiel #7
0
        public void OpenOutputFile_HighestPrintLevel_ProducesLargeLogFile()
        {
            const string logFileName = "hs040_dedicated.txt";

            if (File.Exists(logFileName))
            {
                File.Delete(logFileName);
            }

            using (var instance = new HS040(false, false, false))
            {
                Assert.IsTrue(instance.OpenOutputFile(logFileName, 12));
                double obj;
                var    x = new[] { 1.0, 1.0, 1.0, 1.0 };
                instance.SolveProblem(x, out obj);
            }
            const int expected = 10000;
            var       actual   = File.ReadAllText(logFileName).Length;

            Assert.Greater(actual, expected);
        }
Beispiel #8
0
        public void SetScaling_NonUnityGradientScaling_YieldsDifferentObjectiveValue()
        {
            // Get objective value for unity scaling
            double expected;

            using (var unity = new HS040(false, false, false))
            {
                var x = new[] { 0.8, 0.8, 0.8, 0.8 };
                unity.SolveProblem(x, out expected);
            }

            // Get objective value for non-unity scaling
            double actual;

            using (var scaled = new HS040(false, false, false))
            {
                var x = new[] { 0.8, 0.8, 0.8, 0.8 };
                Assert.IsTrue(scaled.SetScaling(1.0, new[] { 1.0, 1.0, 1.0, 1.0 }, new[] { 1.0e-6, 1.0, 1.0e6 }));
                scaled.SolveProblem(x, out actual);
            }

            Assert.AreNotEqual(expected, actual);
        }