nloptd 0.3.0 released

jmh530 john.michael.hall at gmail.com
Sat Nov 28 22:18:49 UTC 2026


On Friday, 27 November 2026 at 14:24:18 UTC, jmh530 wrote:
> I’ve released nloptd 0.3.0, the first update to the package in 
> about ten years. It now relies on importC instead of 
> hand-written bindings. GPT-6.2 Astra did all the work on this 
> version.
>
> NLopt is a library for nonlinear optimization. With nloptd, you 
> can write an objective function in D, choose a solver, and 
> supply bounds or nonlinear constraints.
>
> Here’s a small example: find the point closest to 2, subject to 
> `x ≤ 1`. The objective captures `target` and writes the 
> derivative when the solver requests it.
>
> ```d
> import nloptd;
> import std.stdio : writeln;
>
> void main()
> {
>     double target = 2;
>     auto opt = Optimizer(Algorithm.ldSLSQP, 1);
>     opt.setMinObjective((x, gradient) {
>         const delta = x[0] - target;
>         if (gradient.length) gradient[0] = 2 * delta;
>         return delta * delta;
>     });
>     opt.setUpperBounds(1.0);
>     opt.setXTolRel(1e-9);
>     opt.setMaxEvaluations(200);
>
>     double[1] point = [0.25];
>     auto result = opt.optimizeInPlace(point[]);
>     writeln(point[], " ", result);
> }
> ```
>
> The solver finishes at `x = 1`, where the objective value is 
> also 1. The callback receives D slices over NLopt’s coordinate 
> and gradient buffers, so neither array needs to be copied.
>
> To try it, install the native NLopt library and headers, then 
> run `dub add nloptd at 0.3.0` in your DUB project. The wrapper 
> uses ImportC to read the installed `nlopt.h`. If you need the C 
> API directly, import `nloptd.raw`. I tested the example above 
> with the published package on Windows x64 and Ubuntu x86-64 
> using LDC 1.43.0.
>
> [Package on DUB](https://code.dlang.org/packages/nloptd) · 
> [Setup instructions and 
> examples](https://github.com/jmh530/nloptd#readme)
>
> The API has changed since 0.2.1. The [migration 
> guide](https://github.com/jmh530/nloptd/blob/master/MIGRATION.md) walks through the same optimization problem with both versions.

nloptd 0.4.0 is [available on 
DUB](https://code.dlang.org/packages/nloptd).

If you use Mir, you can now pass a contiguous ndslice directly to 
`optimizeInPlace(point)`. It can have any rank as long as its 
backing storage is a writable `double[]` and its element count 
matches the optimizer dimension. The [Mir 
guide](https://github.com/jmh530/nloptd/blob/v0.4.0/docs/mir.md) 
shows how to optimize a matrix row in place and how to handle a 
strided column. `mir-algorithm` remains optional.

The `Optimizer` API now lets you inspect bounds, query initial 
step sizes, set per-variable weights, and configure named 
algorithm parameters. The new [D API 
reference](https://github.com/jmh530/nloptd/blob/v0.4.0/docs/reference.md) covers these features alongside objectives, constraints, and stopping criteria.

[Release 
notes](https://github.com/jmh530/nloptd/releases/tag/v0.4.0)


More information about the Digitalmars-d-announce mailing list