|
36 | 36 | using System.Collections.Generic; |
37 | 37 | using System.Collections; |
38 | 38 | using NUnit.Framework.Interfaces; |
| 39 | +using MathNet.Numerics.LinearAlgebra; |
| 40 | +using MathNet.Numerics.Optimization.ObjectiveFunctions; |
39 | 41 |
|
40 | 42 | namespace MathNet.Numerics.UnitTests.OptimizationTests |
41 | 43 | { |
@@ -155,5 +157,44 @@ public void Mgh_Tests(TestFunctions.TestCase test_case) |
155 | 157 | var success = (abs_min <= 1 && abs_err < 1e-3) || (abs_min > 1 && rel_err < 1e-3); |
156 | 158 | Assert.That(success, "Minimal function value is not as expected."); |
157 | 159 | } |
| 160 | + |
| 161 | + |
| 162 | + [Test] |
| 163 | + public void BfgsMinimizer_MinimizesProperlyWhenConstrainedInOneVariable() |
| 164 | + { |
| 165 | + // Test comes from github issue 510 |
| 166 | + // https://github.com/mathnet/mathnet-numerics/issues/510 |
| 167 | + Func<Vector<double>, double> function = (Vector<double> vectorArg) => |
| 168 | + { |
| 169 | + double x = vectorArg[0]; |
| 170 | + double y = -1.0 * Math.Exp(-x * x); |
| 171 | + return y; |
| 172 | + }; |
| 173 | + |
| 174 | + double gradientTolerance = 1e-10; |
| 175 | + double parameterTolerance = 1e-10; |
| 176 | + double functionProgressTolerance = 1e-10; |
| 177 | + int maxIterations = 1000; |
| 178 | + |
| 179 | + var initialGuess = new DenseVector(new[] { -1.0 }); |
| 180 | + |
| 181 | + // Bad Bound |
| 182 | + var lowerBound = new DenseVector(new[] { -2.0 }); |
| 183 | + var upperBound = new DenseVector(new[] { 2.0 }); |
| 184 | + |
| 185 | + |
| 186 | + var objective = ObjectiveFunction.Value(function); |
| 187 | + var objectiveWithGradient = new ForwardDifferenceGradientObjectiveFunction(objective, lowerBound, upperBound); |
| 188 | + var algorithm = new BfgsBMinimizer(gradientTolerance, parameterTolerance, functionProgressTolerance, maxIterations); |
| 189 | + var result = algorithm.FindMinimum(objectiveWithGradient, lowerBound, upperBound, initialGuess); |
| 190 | + |
| 191 | + var resultVector = result.MinimizingPoint; |
| 192 | + double xResult = resultVector[0]; |
| 193 | + |
| 194 | + // (actual minimum at zero) |
| 195 | + var abs_err = Math.Abs(xResult); |
| 196 | + var success = abs_err < 1e-6; |
| 197 | + Assert.That(success, "Minimal function value is not as expected."); |
| 198 | + } |
158 | 199 | } |
159 | 200 | } |
0 commit comments