Unofficial Gurobi Torch bindings.
APACHE-2.0 License
Unofficial Gurobi Torch bindings.
You may also be interested in:
GUROBI_HOME
in your shell's current session (with export GUROBI_HOME=..
).bashrc
or .zshrc
).luarocks make
The following solves the linear program
min c'*x s.t. G*x <= h
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)
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)
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})
After installing the library with luarocks
, our tests in
test.lua
can be run with th test.lua
.