General Linear Optimization in Go. goop
provides general interface for solving
mixed integer linear optimization problems using a variety of back-end solvers
including LPSolve and Gurobi.
We are going to start with a simple example showing how goop
can be used to
solve integer linear programs. The example below seeks to maximize the following
MIP:
maximize x + y + 2 z
subject to x + 2 y + 3 z <= 4
x + y >= 1
x, y, z binary
This is is the same example implemented here. Below
we have implemented the model using goop
and have optimized the model using
the supported Gurobi solver.
package main
import (
"fmt"
"github.com/mit-drl/goop"
"github.com/mit-drl/goop/solvers"
)
func main() {
// Instantiate a new model
m := goop.NewModel()
// Add your variables to the model
x := m.AddBinaryVar()
y := m.AddBinaryVar()
z := m.AddBinaryVar()
// Add your constraints
m.AddConstr(goop.Sum(x, y.Mult(2), z.Mult(3)).LessEq(goop.K(4)))
m.AddConstr(goop.Sum(x, y).GreaterEq(goop.One))
// Set a linear objective using your variables
obj := goop.Sum(x, y, z.Mult(2))
m.SetObjective(obj, goop.SenseMaximize)
// Optimize the variables according to the model
sol, err := m.Optimize(solvers.NewGurobiSolver())
// Check if there is an error from the solver. No error should be returned
// for this model
if err != nil {
panic("Should not have an error")
}
// Print out the solution
fmt.Println("x =", sol.Value(x))
fmt.Println("y =", sol.Value(y))
fmt.Println("z =", sol.Value(z))
// Output:
// x = 1
// y = 0
// z = 1
}
mkdir -p $GOPATH/github.com/mit-drl && cd $GOPATH/github.com/mit-drl
git clone https://github.com/mit-drl/goop && cd goop
./install.sh
Follow the instructions for your solver of choice. Note, currently only Gurobi is supported
go build
Note that due to a quirk with Gurobi, if you are using Ubuntu < 16.04, you must build with
go build -tags pre_xenial
govendor test -v +local
We currently have bindings for Gurobi and LPSolve. Please follow the instructions below for using these specific solvers.
GUROBI_HOME
environment variable must be set to the home directory
of your Gurobi installationLPSolve is installed using the normal install procedure and should work out of the box.