public void NLoptTutorialExample_TestCase_AnalyticSolution(NLoptAlgorithm nloptAlgorithm) { var multiDimOptimizer = new NLoptMultiDimOptimizer(nloptAlgorithm, NLoptAbortCondition.Create(relativeXTolerance: 1E-4)); var nloptBoxConstraint = multiDimOptimizer.Constraint.Create(MultiDimRegion.Interval.Create(dimension: 2, lowerBounds: new[] { Double.NegativeInfinity, 0.0 }, upperBounds: new[] { Double.PositiveInfinity, Double.PositiveInfinity })); double a1 = 2.0; double b1 = 0.0; /* This code uses generic constraints, i.e. polynomial constraints: */ /* The constraints in the Tutorial of the NLopt documentation can be re-written as polynomial in the following form: * * x_2 - a^3 * x_1^3 - 3*a^2*b*x_1^2 - 3*a*b^2 * x_1 >= b^3 */ var polynomialConstraint1 = MultiDimRegion.Polynomial.Create(2, b1 * b1 * b1, Double.PositiveInfinity, new[] { 1.0, -a1 * a1 * a1, -3.0 * a1 * a1 * b1, -3.0 * a1 * b1 * b1 }, MultiDimRegion.Polynomial.Monomial.Create(1, 1), MultiDimRegion.Polynomial.Monomial.Create(0, 3), MultiDimRegion.Polynomial.Monomial.Create(0, 2), MultiDimRegion.Polynomial.Monomial.Create(0, 1)); double a2 = -1; double b2 = 1.0; var polynomialConstraint2 = MultiDimRegion.Polynomial.Create(2, b2 * b2 * b2, Double.PositiveInfinity, new[] { 1.0, -a2 * a2 * a2, -3.0 * a2 * a2 * b2, -3.0 * a2 * b2 * b2 }, MultiDimRegion.Polynomial.Monomial.Create(1, 1), MultiDimRegion.Polynomial.Monomial.Create(0, 3), MultiDimRegion.Polynomial.Monomial.Create(0, 2), MultiDimRegion.Polynomial.Monomial.Create(0, 1)); var optimizer = multiDimOptimizer.Create(nloptBoxConstraint, multiDimOptimizer.Constraint.Create(polynomialConstraint1), multiDimOptimizer.Constraint.Create(polynomialConstraint2)); optimizer.Function = multiDimOptimizer.Function.Create(2, (x, grad) => { if (grad != null) { grad[0] = 0.0; grad[1] = 0.5 / Math.Sqrt(x[1]); } return(Math.Sqrt(x[1])); }); var actualArgMin = new[] { 1.234, 5.678 }; double actualMinimum; var state = optimizer.FindMinimum(actualArgMin, out actualMinimum); double expectedMinimum = Math.Sqrt(8.0 / 27.0); double expectedArgMin0 = 1.0 / 3.0; double expectedArgMin1 = 8.0 / 27.0; Assert.That(actualMinimum, Is.EqualTo(expectedMinimum).Within(1E-3), String.Format("<Minimum> State: {0}; Actual minimum: {1}; Expected minimum: {2}; Actual argMin: ({3}; {4}); Expected argMin: ({5}; {6})", state, actualMinimum, expectedMinimum, actualArgMin[0], actualArgMin[1], expectedArgMin0, expectedArgMin1)); Assert.That(actualArgMin[0], Is.EqualTo(expectedArgMin0).Within(1E-3), String.Format("<argMin[0]> State: {0}; Actual minimum: {1}; Expected minimum: {2}; Actual argMin: ({3}; {4}); Expected argMin: ({5}; {6})", state, actualMinimum, expectedMinimum, actualArgMin[0], actualArgMin[1], expectedArgMin0, expectedArgMin1)); Assert.That(actualArgMin[1], Is.EqualTo(expectedArgMin1).Within(1E-3), String.Format("<argMin[1]> State: {0}; Actual minimum: {1}; Expected minimum: {2}; Actual argMin: ({3}; {4}); Expected argMin: ({5}; {6})", state, actualMinimum, expectedMinimum, actualArgMin[0], actualArgMin[1], expectedArgMin0, expectedArgMin1)); }
public void NLoptTutorialExample_TestCaseNLoptConstraints_AnalyticSolution(NLoptAlgorithm nloptAlgorithm) { var multiDimOptimizer = new NLoptMultiDimOptimizer(NLoptAlgorithm.LN_COBYLA, NLoptAbortCondition.Create(relativeXTolerance: 1E-4)); var nloptBoxConstraint = multiDimOptimizer.Constraint.Create(MultiDimRegion.Interval.Create(dimension: 2, lowerBounds: new[] { Double.NegativeInfinity, 0.0 }, upperBounds: new[] { Double.PositiveInfinity, Double.PositiveInfinity })); double a1 = 2.0; double b1 = 0.0; double a2 = -1; double b2 = 1.0; /* this code uses NLopt specific constraints: */ var optimizer = multiDimOptimizer.Create(nloptBoxConstraint, multiDimOptimizer.Constraint.Create(2, (x, grad) => { if (grad != null) { grad[0] = 3.0 * a1 * (a1 * x[0] + b1) * (a1 * x[0] + b1); grad[1] = -1.0; } return((a1 * x[0] + b1) * (a1 * x[0] + b1) * (a1 * x[0] + b1) - x[1]); }), multiDimOptimizer.Constraint.Create(2, (x, grad) => { if (grad != null) { grad[0] = 3.0 * a2 * (a2 * x[0] + b2) * (a2 * x[0] + b2); grad[1] = -1.0; } return((a2 * x[0] + b2) * (a2 * x[0] + b2) * (a2 * x[0] + b2) - x[1]); } )); optimizer.Function = multiDimOptimizer.Function.Create(2, (x, grad) => { if (grad != null) { grad[0] = 0.0; grad[1] = 0.5 / Math.Sqrt(x[1]); } return(Math.Sqrt(x[1])); }); var actualArgMin = new[] { 1.234, 5.678 }; double actualMinimum; optimizer.FindMinimum(actualArgMin, out actualMinimum); double expectedMinimum = Math.Sqrt(8.0 / 27.0); double expectedArgMin0 = 1.0 / 3.0; double expectedArgMin1 = 8.0 / 27.0; Assert.That(actualMinimum, Is.EqualTo(expectedMinimum).Within(1E-3)); Assert.That(actualArgMin[0], Is.EqualTo(expectedArgMin0).Within(1E-3)); Assert.That(actualArgMin[1], Is.EqualTo(expectedArgMin1).Within(1E-3)); }