This package provides bindings to the COIN-CBC solver.
It is currently work in progress.
The package requires COIN-CBC solver headers and libs. On Debian/Ubuntu:
sudo apt-get install coinor-libcbc-dev coinor-libclp-dev
On Fedora:
sudo yum install coin-or-Cbc-devel coin-or-Clp-devel
And on MacOS:
brew install coin-or-tools/coinor/cbc
Now install the package in R:
devtools::install_github("dirkschumacher/rcbc")
library(rcbc)
# max 1 * x + 2 * y
# s.t.
# x + y <= 1
# x, y binary
A <- matrix(c(1, 1), ncol = 2, nrow = 1)
result <- cbc_solve(
obj = c(1, 2),
mat = A, # <- can also be a sparse matrix
is_integer = c(TRUE, TRUE),
row_lb = -Inf, row_ub = 1, max = TRUE,
col_lb = c(0, 0), col_ub = c(1, 1),
cbc_args = list("SEC" = "1"))
solution_status(result)
#> [1] "optimal"
objective_value(result)
#> [1] 2
column_solution(result)
#> [1] 0 1
Here we solve a larger Knapsack problem
set.seed(1)
max_capacity <- 1000
n <- 100
weights <- round(runif(n, max = max_capacity))
cost <- round(runif(n) * 100)
A <- matrix(weights, ncol = n, nrow = 1)
result <- cbc_solve(
obj = cost,
mat = A,
is_integer = rep.int(TRUE, n),
row_lb = 0, row_ub = max_capacity, max = TRUE,
col_lb = rep.int(0, n), col_ub = rep.int(1, n))
solution_status(result)
#> [1] "optimal"
objective_value(result)
#> [1] 607
column_solution(result)
#> [1] 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
#> [38] 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
#> [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0
CBC has a number of
parameters.
You can pass them to the solver using the cbc_args
argument.
For example the code below sets the timelimit of the solver to 5 seconds:
cbc_solve(..., cbc_args = list("sec" = 5))
There is now a work in progress ROI plugin.
Feel free to open issues and send PRs.