Skip to content

thgngu/gurobi.torch

 
 

Repository files navigation

gurobi.torch • [ ![Build Status] travis-image ] travis [ License license-image ] license

Unofficial Gurobi Torch bindings.


You may also be interested in:

Installation

  1. Set GUROBI_HOME in your shell's current session (with export GUROBI_HOME=..) and initialization file (.bashrc or .zshrc).
  2. luarocks make

Usage

The following solves the linear program

min  c'*x s.t. G*x <= h

Linear Program

local gurobi = require 'gurobi'

local G = torch.Tensor{{-1, 1}, {-1, -1}, {0, -1}, {1, -2}}
local h = torch.Tensor{1.0, -2.0, 0.0, 4.0}
local c = torch.Tensor{2.0, 1.0}

local env = gurobi.loadenv("")
local model = gurobi.newmodel(env, "", c)
gurobi.addconstrs(model, G, 'LE', h)
local status, x = gurobi.solve(model)
print(x) -- Optimal x is [0.5, 1.5]

gurobi.free(env, model)

Linear Program with Incrementally Added Constraints

local gurobi = require 'gurobi'

-- minimize y
-- subject to y >= x
--            y >= -x
--            y >= x + 1
local c = torch.Tensor{0.0, 1.0}
local G = torch.Tensor{{1, -1}, {-1, -1}, {1, -1}}
local h = torch.Tensor{0.0, 0.0, -1.0}

local env = gurobi.loadenv("")
local model = gurobi.newmodel(env, "", c)

local I = {{1,2}}
gurobi.addconstrs(model, G[I], 'LE', h[I])

local status, x = gurobi.solve(model)
print(x) -- Optimal at this point is [0, 0]

gurobi.addconstr(model, G[3], 'LE', h[3])
status, x = gurobi.solve(model)
print(x) -- Optimal at this point is [-0.5, 0.5]

gurobi.free(env, model)

OpenMP Parallel Solves

local env = gurobi.loadenv("")
local model1 = gurobi.newmodel(env, "", c)
gurobi.addconstrs(model1, G, 'LE', h)

local model2 = gurobi.newmodel(env, "", c)
gurobi.addconstrs(model2, G, 'LE', h)

local status, xs = gurobi.solvePar({model1, model2})

Tests

After installing the library with luarocks, our tests in test.lua can be run with th test.lua.

Licensing

  • Gurobi is proprietary software.
  • The original code in this repository (the Gurobi bindings) is Apache-licensed.

About

Unofficial Gurobi Torch bindings.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Lua 66.0%
  • C 27.5%
  • CMake 6.5%