CodePlexProject Hosting for Open Source Software

The class NLoptPtr serves as wrapper for the internal opaque pointer of the algorithm of the NLopt library. Therefore you will find for almost all function of the API of the NLopt library a counterpart in this class.

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; }); var argMin = new double[2] { 1.0, 4.8 }; double minimum; // expected: 1.123 var errorCode = ptr.FindMinimum(argMin, out minimum);

In contrast to NLoptPtr the class NLoptMultiDimOptimizer implements the more common class (Ordinary)MultiDimOptimizer of Dodoni.BasicMathLibrary which serves as general infastructure for multi-dimensional optimization. Therefore one first create a specific (NLopt}MultiDimOptimizer object. Constraints and the representation of the objective function are specific for the NLopt library. Therefore one has to apply the specific factory for it. The following code snippet shows a simple example.

var cobyla = new NLoptMultiDimOptimizer(NLoptAlgorithm.LN_COBYLA); var nloptBoxConstraint = cobyla.Constraint.Create( MultiDimRegion.Interval.Create(dimension: 2, lowerBounds: new[] { 1.0, 5.0 }, upperBounds: new[] { 12.4, 34.2 })); var alg = cobyla.Create(nloptBoxConstraint); alg.Function = cobyla.Function.Create(2,x => x[0]*x[0] + x[1]*x[1] + 1.123); // alternative: alg.SetFunction(x => x[0]*x[0] + x[1]*x[1] + 1.123); var argMin = new [] { 1.4, 5.8 }; double minimum; var errorCode = alg.FindMinimum(argMin, out minimum);

Moreover a logging can be used via an optional argument of NLoptMultiDimOptimizer. You need more freedom in the adjustment of the specific algorithm? No problem, the constructors of NLoptMultiDimOptimizer provides an optional argument that is a delegate (function pointer) which will be applied to the internal NLoptPtr object. Therefore one can apply lambda calculus, for example to establish a local optimizer, set initial step size etc.

var cobyla = new NLoptMultiDimOptimizer(NLoptAlgorithm.LN_COBYLA, nloptPtr => {nloptPtr.SetInitialStepSize(new []{1.0, 2.0}} );

If you need additional information of a specific NLopt algorithm, i.e. whether it is a local/global approach, required gradient etc. you can use the Configuration property of NLoptMultiDimOptimizer. Moreover you can create such a configuration separately, for example via

```
var config = NLoptConfiguration.Create(NLoptAlgorithm.LN_COBYLA);
```

Last edited Oct 8, 2014 at 11:33 AM by dodoni, version 14