opt

Convex optimization for Ruby

APACHE-2.0 License

Downloads
11.1K
Stars
26
Committers
1

Opt

πŸ”₯ Convex optimization for Ruby

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

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