NLopt - nonlinear optimization - for Ruby
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"
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])
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.
View the changelog
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development:
git clone https://github.com/ankane/nlopt-ruby.git
cd nlopt-ruby
bundle install
bundle exec rake test