README.md

rmpk.glpk

Lifecycle:
experimental Travis build
status

Bindings for the GLPK optimizer. WIP

Example

library(rmpk)
library(rmpk.glpk)
set.seed(42)
v <- rnorm(10)
w <- rnorm(10)
solver <- GLPK()
model <- MIPModel(solver)
model$add_variable(x[i], type = "binary", i = 1:10)
model$set_objective(sum_expr(v[i] * x[i], i = 1:10), sense = "max")
model$add_constraint(sum_expr(w[i] * x[i], i = 1:10) <= 10)

# we register a callback that adds a constraint if more than 3 items are selected
solver$set_irowgen_callback(function() {
  values <- vapply(1:10, function(i) {
    # in GLPK you can only access the values of the relaxation
    solver$glpk_get_col_prim(x[i])
  }, numeric(1L))
  all_integral <- all(values %% 1 == 0)
  if (all_integral && sum(values) > 3) {
    model$add_constraint(sum_expr(x[i], i = 1:10) <= 3)
  }
})

model$optimize()
model$get_variable_value(x[i])
#>    name  i value
#> 1     x  9     1
#> 2     x  3     0
#> 3     x  2     0
#> 4     x  8     0
#> 5     x 10     0
#> 6     x  6     0
#> 7     x  5     0
#> 8     x  1     1
#> 9     x  4     0
#> 10    x  7     1
model$objective_value()
#> [1] 4.900904


dirkschumacher/rmpk.glpk documentation built on Nov. 4, 2019, 10:54 a.m.