Go library for modeling and solving linear programming problems
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Go Linear Programming Abstraction

Build Status GoDoc

GoLPA is a library for modeling and solving linear programming problems. It uses lp_solve for the actual number crunching, but offers a slightly higher-level interface for problem modeling relative to the underlying library. Since the intention is providing a simpler interface, the underlying API is not completely exposed. If there are features of the low-level library you'd like to see exposed by GoLPA, please open an issue.

Warning: the API is currently not stable.


You need the liblpsolve55-dev (Debian variants) or lpsolve-devel (Red Hat variants) package installed in order to be able to compile GoLPA.


If you have a properly set up GOPATH, just run:

$ go get github.com/costela/golpa/golpa

Example usage

The model of the following problem:

  z = x1 + 2 x2 - 3 x3
  0 <= x1 <= 40
  5 <= x3 <= 11
Subject to:
  0 <= - x1 + x2 + 5.3 x3 <= 10
  -inf <= 2 x1 - 5 x2 + 3 x3 <= 20
  x2 - 8 x3 = 0

can be expressed with GoLPA like this:

package main

import (

func main() {
  model := golpa.NewModel("some model", golpa.Maximize)
  x1, _ := model.AddVariable("x1")
  x1.SetBounds(0, 40)
  x2, _ := model.AddVariable("x2")
  // alternatively, all information pertaining can be given at once:
  x3, _ := model.AddDefinedVariable("x3", golpa.ContinuousVariable, 3, 5, 11)

  model.AddConstraint(0, 10, []*golpa.Variable{x1, x2, x3}, []float64{-1, 1, 5.3})
  model.AddConstraint(math.Inf(-1), 20, []*golpa.Variable{x1, x2, x3}, []float64{2, -5, 3})
  model.AddConstraint(0, 0, []*golpa.Variable{x1, x3}, []float64{1, -8})

The model can than be solved and the resulting values can than be retrieved as follows:

result, _ := model.Solve() // you should check for errors

  fmt.Printf("solution optimal? %t", result.GetStatus() == golpa.SolutionOptimal)
  fmt.Printf("z = %f\n", result.GetObjectiveValue())
  fmt.Printf("x1 = %f\n", result.GetValue(x1))