/nlopt-ruby

Nonlinear optimization for Ruby

Primary LanguageRubyMIT LicenseMIT

NLopt Ruby

NLopt - nonlinear optimization - for Ruby

Build Status

Installation

First, install NLopt. For Homebrew, use:

brew install nlopt

And for Ubuntu 22.04+, use:

sudo apt-get install libnlopt0

Then add this line to your application’s Gemfile:

gem "nlopt"

Getting Started

Create an optimization

opt = NLopt::Opt.new("LN_COBYLA", 2)

Set the objective function

f = lambda do |x, grad|
  x[0] + x[1]
end
opt.set_min_objective(f)

Set constraints

opt.set_lower_bounds([1, 2])
opt.set_upper_bounds([3, 4])

Perform the optimization

xopt = opt.optimize([2, 3])

API

This library follows the NLopt Python API. You can follow Python tutorials and convert the code to Ruby in many cases. Feel free to open an issue if you run into problems.

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/ankane/nlopt-ruby.git
cd nlopt-ruby
bundle install
bundle exec rake test