/opt

Convex optimization for Ruby

Primary LanguageRubyApache License 2.0Apache-2.0

Opt

🔥 Convex optimization for Ruby

Supports Cbc, Clp, GLOP, GLPK, HiGHS, OSQP, and SCS

Build Status

Installation

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

Getting Started

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")

Reference

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)

Credits

This project was inspired by CVXPY and OR-Tools.

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/opt.git
cd opt
bundle install
bundle exec rake test