A Go wrapper for the COIN-OR CBC (Coin-or Branch and Cut) Mixed Integer Programming solver.
- Clean Go API for building and solving optimization problems
- Support for continuous and integer variables
- Linear constraints with bounds
- Minimization and maximization
- High-level builder API and low-level CBC interface
You need to have CBC installed on your system:
brew install cbcsudo apt-get install coinor-cbc coinor-libcbc-devsudo yum install coin-or-Cbc coin-or-Cbc-develgo get github.com/dordieux/go-cbcpackage main
import (
"fmt"
"log"
cbc "github.com/dordieux/go-cbc"
)
func main() {
// Create a new problem
problem := cbc.NewProblem()
defer problem.Close()
// Add variables
x := problem.AddIntegerVariable("x", 0, 100)
y := problem.AddIntegerVariable("y", 0, 100)
// Set objective: maximize 3x + 2y
problem.SetObjective(map[*cbc.Variable]float64{
x: 3.0,
y: 2.0,
}, cbc.Maximize)
// Add constraint: x + y <= 50
problem.AddConstraintLe(map[*cbc.Variable]float64{
x: 1.0,
y: 1.0,
}, 50)
// Solve
solution, err := problem.Solve()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Status: %s\n", solution.Status)
fmt.Printf("Objective: %.2f\n", solution.Objective)
fmt.Printf("x = %.2f, y = %.2f\n",
solution.Variables["x"],
solution.Variables["y"])
}See examples/knapsack/main.go for a complete knapsack problem implementation.
NewProblem()- Create a new optimization problemAddVariable(name, lower, upper, isInteger)- Add a decision variableAddIntegerVariable(name, lower, upper)- Add an integer variableAddContinuousVariable(name, lower, upper)- Add a continuous variableSetObjective(coefficients, sense)- Set the objective functionAddConstraint(coefficients, lower, upper)- Add a constraint with boundsAddConstraintLe(coefficients, rhs)- Add a ≤ constraintAddConstraintGe(coefficients, rhs)- Add a ≥ constraintAddConstraintEq(coefficients, rhs)- Add an = constraintSolve()- Solve the problem
The solution contains:
Status- Solution status (Optimal, Infeasible, Unbounded, etc.)Objective- Objective function valueVariables- Map of variable names to their values
cd go-cbc
go test# Simple optimization
go run examples/simple/main.go
# Knapsack problem
go run examples/knapsack/main.goThis library has been tested on:
- macOS (with Homebrew)
- Ubuntu/Debian Linux
- GitHub Actions CI
Note: Windows support requires additional configuration.
CBC is one of the fastest open-source MIP solvers available. It can handle:
- Problems with millions of variables
- Integer, binary, and continuous variables
- Linear constraints and objectives
Benchmark comparisons show CBC is typically 5x faster than lp_solve and competitive with commercial solvers for many problem types.