This is a C# wrapper around the NLopt C library. It includes native binaries for NLopt 2.10.1 across all major platforms:
- Windows: x86, x64
- Linux: x64, arm64, musl-x64 (Alpine)
- macOS: x64 (Intel), arm64 (Apple Silicon)
This package inherits NLopt's LGPL license.
Originally created by Brannon King. Thanks to ASI for sponsoring some time on the original project. Modernization and macOS support by Jeff Clement.
Example:
[TestMethod]
public void FindParabolaMinimum()
{
using (var solver = new NLoptSolver(NLoptAlgorithm.LN_COBYLA, 1, 0.001, 100))
{
solver.SetLowerBounds(new[] { -10.0 });
solver.SetUpperBounds(new[] { 100.0 });
solver.SetMinObjective(variables =>
{
return Math.Pow(variables[0] - 3.0, 2.0) + 4.0;
});
double? finalScore;
var initialValue = new[] { 2.0 };
var result = solver.Optimize(initialValue, out finalScore);
Assert.AreEqual(NloptResult.XTOL_REACHED, result);
Assert.AreEqual(3.0, initialValue[0], 0.1);
Assert.AreEqual(4.0, finalScore.Value, 0.1);
}
}You can use the derivative-based optimizers like this:
solver.SetMinObjective((variables, gradient) =>
{
if (gradient != null)
gradient[0] = (variables[0] - 3.0) * 2.0;
return Math.Pow(variables[0] - 3.0, 2.0) + 4.0;
});And add constraints like this:
solver.AddEqualZeroConstraint((variables, gradient) =>
{
if (gradient != null)
gradient[0] = 1.0;
return variables[0] - 100.0;
});To use the Augmented Lagrangian:
using(var solver = new NLoptSolver(NLoptAlgorithm.LN_AUGLAG, 2, 0.001, 1000, NLoptAlgorithm.LN_SBPLX))
{
solver.SetLowerBounds(new[] { 1.0, 1.0 });
solver.SetUpperBounds(new[] { 200.0, 200.0 });
solver.AddLessOrEqualZeroConstraint(Constrain);
solver.SetMinObjective(Score);
var result = solver.Optimize(best, out finalScore);
}Install via NuGet Package Manager:
Install-Package NLoptNet
Or via .NET CLI:
dotnet add package NLoptNetOr add to your .csproj file:
<PackageReference Include="NLoptNet" Version="2.10.1" />This package supports:
- .NET Standard 2.0+
- .NET 6.0+
- .NET 8.0+
- .NET Framework 4.7+
Native binaries are automatically selected for your platform at runtime.
- Clone the repository
- Run the GitHub Actions workflow to build NLopt binaries for all platforms
- Run
dotnet buildto build the managed wrapper - Run
dotnet packto create the NuGet package
This wrapper is licensed under the same LGPL 2.1 or later license as NLopt itself. See the NLopt documentation for more details.
Contributions are welcome! Please read through the NLopt documentation before posting questions/issues here.