Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-cbc

A Go wrapper for the COIN-OR CBC (Coin-or Branch and Cut) Mixed Integer Programming solver.

Features

  • 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

Prerequisites

You need to have CBC installed on your system:

macOS

brew install cbc

Ubuntu/Debian

sudo apt-get install coinor-cbc coinor-libcbc-dev

Fedora/RHEL

sudo yum install coin-or-Cbc coin-or-Cbc-devel

Installation

go get github.com/dordieux/go-cbc

Usage

Simple Example

package 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"])
}

Knapsack Problem Example

See examples/knapsack/main.go for a complete knapsack problem implementation.

API Reference

Problem Builder

  • NewProblem() - Create a new optimization problem
  • AddVariable(name, lower, upper, isInteger) - Add a decision variable
  • AddIntegerVariable(name, lower, upper) - Add an integer variable
  • AddContinuousVariable(name, lower, upper) - Add a continuous variable
  • SetObjective(coefficients, sense) - Set the objective function
  • AddConstraint(coefficients, lower, upper) - Add a constraint with bounds
  • AddConstraintLe(coefficients, rhs) - Add a ≤ constraint
  • AddConstraintGe(coefficients, rhs) - Add a ≥ constraint
  • AddConstraintEq(coefficients, rhs) - Add an = constraint
  • Solve() - Solve the problem

Solution

The solution contains:

  • Status - Solution status (Optimal, Infeasible, Unbounded, etc.)
  • Objective - Objective function value
  • Variables - Map of variable names to their values

Running Tests

cd go-cbc
go test

Running Examples

# Simple optimization
go run examples/simple/main.go

# Knapsack problem
go run examples/knapsack/main.go

Platform Support

This library has been tested on:

  • macOS (with Homebrew)
  • Ubuntu/Debian Linux
  • GitHub Actions CI

Note: Windows support requires additional configuration.

Performance

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.

About

Go wrapper for COIN-OR CBC Mixed Integer Programming solver

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages