COIN-OR
COIN-OR
Raku interface to the COIN-OR numerical optimization solver.
The initial API wraps CBC's portable C interface through Raku NativeCall.
It supports constructing and solving linear mixed-integer models; see examples listed below.
For large, sparse constraint matrices, use Model.load-problem rather than
adding rows one at a time. It accepts CBC's compressed sparse column (CSC)
format: starts has one offset per column plus a final offset, while indexes
and values hold the row index and coefficient of each nonzero.
my $model = COIN-OR::Model.new(:name<csc-example>);
$model.load-problem(
:columns(2), :rows(1),
:starts([0, 1, 2]), :indexes([0, 0]), :values([1e0, 1e0]),
:column-upper([2e0, 3e0]), :objective([3e0, 2e0]),
:row-upper([4e0]), :integer-columns([0, 1]),
);
$model.maximize;
$model.solve;add-row is already sparse: its column-index and coefficient lists contain
only the nonzero entries of that row. CBC's C interface has no
Cbc_addSparseRow; Cbc_loadProblem is its bulk sparse-matrix API.
Installation
macOS
#!/bin/bash
# Ensure Homebrew is up to date
brew update
# Install all 4 core COIN-OR packages
brew install coinutils clp cbc ipopt
# Verify installations
echo -e "\n--- Verification ---"
for cmd in cbc clp ipopt; do
if command -v $cmd &> /dev/null; then
echo "ā
$cmd installed: $($cmd --version 2>&1 | head -n 1)"
else
echo "ā $cmd failed to install"
fi
doneHomebrew installs formulae below an architecture-dependent prefix (normally
/opt/homebrew on Apple Silicon and /usr/local on Intel). Configure the
binding with either of these optional environment variables before running
Raku:
# Recommended: ask Homebrew for the exact CBC C-interface library.
export COIN_OR_CBC_LIBRARY="$(brew --prefix cbc)/lib/libCbcSolver.dylib"
# Alternative: provide the containing directory; the package chooses
# libCbcSolver.dylib on macOS (and libCbcSolver.so on Linux).
export COIN_OR_LIBRARY_DIR="$(brew --prefix cbc)/lib"COIN_OR_CBC_LIBRARY takes precedence over COIN_OR_LIBRARY_DIR. Neither is
needed on a system where the dynamic linker can already locate libCbcSolver.
For a persistent macOS configuration, add the preferred export line to
~/.zprofile (zsh's login-shell configuration) and start a new terminal.
Ubuntu Linux
sudo apt update
# Install CBC (Mixed-Integer Linear Programming Solver)
sudo apt install coinor-cbc coinor-libcbc-dev
# Install CLP (Linear Programming Solver)
sudo apt install coinor-clp coinor-libclp-dev
# Install Ipopt (Nonlinear Programming Solver)
sudo apt install coinor-ipopt coinor-libipopt-devRaku package
From Zef ecosystem:
zef install COIN-ORFrom GitHub:
zef install https://github.com/antononcube/Raku-COIN-ORInstallation scripts
With Raku and zef already installed, the local distribution can be installed
with its CBC dependencies using:
sh resources/install.shThe script supports macOS (Homebrew) and Linux (APT or Pacman). On Windows,
install MSYS2 and Raku with zef, then run from PowerShell:
.\resources\install-windows.ps1Examples
Here are Raku scripts with worked examples: