public void NLoptTutorialExample_TestCase_AnalyticSolution(NLoptAlgorithm nloptAlgorithm) { NLoptPtr ptr = new NLoptPtr(nloptAlgorithm, 2); /* add and create boundary and nonlinear constraints: */ ptr.SetLowerBounds(new[] { Double.NegativeInfinity, 0.0 }); double a1 = 2.0; double b1 = 0.0; ptr.AddInequalityConstraint((n, x, grad, data) => { 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]); }, 1E-8); double a2 = -1; double b2 = 1.0; var code = ptr.AddInequalityConstraint((n, x, grad, data) => { 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]); }, 1E-8); ptr.SetFunction((n, x, grad, data) => { if (grad != null) { grad[0] = 0.0; grad[1] = 0.5 / Math.Sqrt(x[1]); } return(Math.Sqrt(x[1])); }); ptr.TrySetRelativeXTolerance(1E-4); var actualArgMin = new[] { 1.234, 5.678 }; double actualMinimum; ptr.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)); }
/// <summary>Sets a specific subsidiary (local) optimizer. /// </summary> /// <param name="nloptLocalOptimizer">The local optimizer.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="nloptLocalOptimizer"/> is <c>null</c>.</exception> public NLoptResultCode SetLocalOptimizer(NLoptPtr nloptLocalOptimizer) { if (nloptLocalOptimizer == null) { throw new ArgumentNullException(nameof(nloptLocalOptimizer)); } if (m_NLopPtr == IntPtr.Zero) { throw new ObjectDisposedException("NLoptPtr"); } return(nlopt_set_local_optimizer((IntPtr)m_NLopPtr, (IntPtr)nloptLocalOptimizer.m_NLopPtr)); }
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { m_NLoptPtr.Dispose(); m_NLoptPtr = null; if (m_LoggerStream != null) { m_LoggerStreamGCHandle.Free(); // m_LoggerStream.Dispose(); m_LoggerStream = null; } }
/// <summary>Initializes a new instance of the <see cref="NLoptMultiDimOptimizer" /> class. /// </summary> /// <param name="algorithm">A value indicating the specific NLopt algorithm.</param> /// <param name="abortCondition">The abort (stopping) condition of the NLopt algorithm.</param> /// <param name="nloptPtrAdjustment">An optional delegate which will be called in the <c>Create</c> methods for <see cref="IMultiDimOptimizerAlgorithm"/> objects that allows individual adjustments of the internal <see cref="NLoptPtr"/> representation.</param> /// <param name="loggerStreamFactory">A factory for <see cref="ILoggerStream"/> objects, i.e. for a logging. Each <see cref="IMultiDimOptimizerAlgorithm"/> object will track the function values in the specified logger.</param> /// <remarks>One can use <paramref name="nloptPtrAdjustment"/> to change the Initial step size, initial "population" of random points, set Local/subsidiary optimization algorithm etc. See /// the documentation of the NLopt library http://ab-initio.mit.edu/wiki/index.php/NLopt for further details.</remarks> public NLoptMultiDimOptimizer(NLoptAlgorithm algorithm, NLoptAbortCondition abortCondition, Action <NLoptPtr> nloptPtrAdjustment = null, Func <IMultiDimOptimizerAlgorithm, ILogger> loggerStreamFactory = null) { Algorithm = algorithm; Configuration = NLoptConfiguration.Create(algorithm); if (abortCondition == null) { throw new ArgumentNullException("abortCondition"); } AbortCondition = abortCondition; m_LongName = new IdentifierString(NLoptPtr.GetName(algorithm)); m_Name = new IdentifierString(algorithm.ToFormatString(EnumStringRepresentationUsage.StringAttribute)); Constraint = new NLoptConstraintFactory(this); Function = new NLoptFunctionFactory(this); m_nloptPtrAdjustment = nloptPtrAdjustment; m_LoggerStreamFactory = loggerStreamFactory; }
public void NLoptPRAXIS_TestCase_AnalyticMinimum() { NLoptPtr ptr = new NLoptPtr(NLoptAlgorithm.LN_PRAXIS, 2); ptr.TrySetAbsoluteFValueTolerance(1E-6); ptr.TrySetRelativeFValueTolerance(1E-6); ptr.TrySetAbsoluteXTolerance(1E-6); ptr.TrySetRelativeXTolerance(1E-6); ptr.SetFunction((n, x, gradient, data) => { return(x[0] * x[0] + x[1] * x[1] + 1.123); }); double[] argMin = new double[2] { 1.0, 4.8 }; double actual; var code = ptr.FindMinimum(argMin, out actual); double expected = 1.123; Assert.That(actual, Is.EqualTo(expected).Within(1E-6)); }
/// <summary>Initializes a new instance of the <see cref="Wrapper" /> class. /// </summary> /// <param name="nloptMultiDimOptimizer">The <see cref="NLoptMultiDimOptimizer"/> object that has been used to create the current instance.</param> /// <param name="dimension">The dimension.</param> public Wrapper(NLoptMultiDimOptimizer nloptMultiDimOptimizer, int dimension) { m_Factory = nloptMultiDimOptimizer; m_NLoptPtr = new NLoptPtr(nloptMultiDimOptimizer.Algorithm, dimension); // check whether the algorithm allows an unconstraint feasible region: if ((nloptMultiDimOptimizer.Configuration.BoxConstraintRequirement == NLoptBoundConstraintRequirement.Required) || (nloptMultiDimOptimizer.Configuration.NonlinearConstraintRequirement.HasFlag(NLoptNonlinearConstraintRequirement.RequiredInequalityConstraints)) || (nloptMultiDimOptimizer.Configuration.NonlinearConstraintRequirement.HasFlag(NLoptNonlinearConstraintRequirement.RequiredEqualityConstraints))) { throw new InvalidOperationException("The optimizer does not support unconstraint feasible region."); } nloptMultiDimOptimizer.AbortCondition.ApplyTo(m_NLoptPtr); if (nloptMultiDimOptimizer.m_nloptPtrAdjustment != null) // apply some optional individual adjustments { nloptMultiDimOptimizer.m_nloptPtrAdjustment(m_NLoptPtr); } if (nloptMultiDimOptimizer.m_LoggerStreamFactory != null) { m_LoggerStream = nloptMultiDimOptimizer.m_LoggerStreamFactory(this); m_LoggerStreamGCHandle = GCHandle.Alloc(m_LoggerStream); } }
/// <summary>Initializes a new instance of the <see cref="Wrapper" /> class. /// </summary> /// <param name="nloptMultiDimOptimizer">The <see cref="NLoptMultiDimOptimizer"/> object that has been used to create the current instance.</param> /// <param name="dimension">The dimension.</param> /// <param name="nloptConstraints">The constraints of the optimization algorithm in its <see cref="NLoptConstraint"/> representation.</param> internal Wrapper(NLoptMultiDimOptimizer nloptMultiDimOptimizer, int dimension, IEnumerable <NLoptConstraint> nloptConstraints) { m_Factory = nloptMultiDimOptimizer; m_NLoptPtr = new NLoptPtr(nloptMultiDimOptimizer.Algorithm, dimension); nloptMultiDimOptimizer.AbortCondition.ApplyTo(m_NLoptPtr); foreach (var constraint in nloptConstraints) { if (constraint.Dimension != dimension) { throw new ArgumentException(String.Format(ExceptionMessages.ArgumentHasWrongDimension, constraint), nameof(nloptConstraints)); } constraint.ApplyTo(m_NLoptPtr); } if (nloptMultiDimOptimizer.m_nloptPtrAdjustment != null) // apply some optional individual adjustments { nloptMultiDimOptimizer.m_nloptPtrAdjustment(m_NLoptPtr); } if (nloptMultiDimOptimizer.m_LoggerStreamFactory != null) { m_LoggerStream = nloptMultiDimOptimizer.m_LoggerStreamFactory(this); m_LoggerStreamGCHandle = GCHandle.Alloc(m_LoggerStream); } }
/// <summary>Applies the constraints represented by the current instance to a specific NLopt algorithm in its <see cref="NLoptPtr"/> representation. /// </summary> /// <param name="nloptPtr">The NLopt algorithm to apply the abort (stopping) condition in its <see cref="NLoptPtr"/> representation.</param> internal void ApplyTo(NLoptPtr nloptPtr) { m_ApplyToNLoptPtr(nloptPtr); }
/// <summary>Applies the abort (stopping) condition represented by the current instance to a specific NLopt algorithm in its <see cref="NLoptPtr"/> representation. /// </summary> /// <param name="nloptPtr">The NLopt algorithm in its <see cref="NLoptPtr"/> representation to apply the abort (stopping) condition.</param> public void ApplyTo(NLoptPtr nloptPtr) { m_ApplyToNLoptPtr(nloptPtr); }