🔥 Convex optimization for Ruby
Supports Cbc, Clp, GLOP, GLPK, HiGHS, OSQP, and SCS
Add this line to your application’s Gemfile:
gem "opt-rb"
And install one or more solvers based on your problem types:
Solver | LP | QP | MIP | License |
---|---|---|---|---|
Cbc | ✓ | ✓ | EPL-2.0 | |
Clp | ✓ | EPL-2.0 | ||
GLOP | ✓ | Apache-2.0 | ||
GLPK | ✓ | ✓ | GPL-3.0-or-later | |
HiGHS | ✓ | ✓ | ✓ | MIT |
OSQP | ✓ | ✓ | Apache-2.0 | |
SCS | ✓ | * | MIT |
* supports, but not implemented yet
Create and solve a problem
x1 = Opt::Variable.new(0.., "x1")
x2 = Opt::Variable.new(0.., "x2")
prob = Opt::Problem.new
prob.add(2 * x1 + 2 * x2 >= 7)
prob.add(3 * x1 + 4 * x2 >= 12)
prob.add(2 * x1 + x2 >= 6)
prob.minimize(8 * x1 + 10 * x2)
prob.solve
Get the value of a variable
x1.value
QP
prob.minimize(x1 * x1)
MIP
x1 = Opt::Integer.new(0.., "x1")
x2 = Opt::Binary.new("x2")
MIP with semi-continuous variables - HiGHS only at the moment
x1 = Opt::SemiContinuous.new(2.., "x1")
x2 = Opt::SemiInteger.new(2.., "x2")
Specify the solver
prob.solve(solver: :cbc)
Enable verbose logging
prob.solve(verbose: true)
Set the time limit in seconds
prob.solve(time_limit: 30)
This project was inspired by CVXPY and OR-Tools.
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/opt.git
cd opt
bundle install
bundle exec rake test