Search code examples
roptimizationlinear-programmingglpk

Is it possible to use a product expression as a constraint in an Ompr optimisation?


I'm aware of the sum_expr function in the ompr package as a way to create a constraint with a dynamic sum. However, I'm wondering if there's a way to create a constraint that uses the product instead of the sum. Or is this is not possible in linear optimisation?

For example:

library(dplyr)
library(ROI)
library(ROI.plugin.glpk)
library(ompr)
library(ompr.roi)

n <- 20
score <- round(runif(n, 0, 25))
penalties <- round(runif(n, 0, 25))

model <- MIPModel() %>%
  add_variable(x[i], i = 1:n, type = "binary") %>%
  set_objective(sum_expr(score[i] * x[i], i = 1:n), "max") %>%
  add_constraint(sum_expr(penalties[i] * x[i], i = 1:n) <= 100)

result <- solve_model(model, with_ROI(solver = "glpk", verbose = TRUE))
result$solution

Instead of add_constraint(sum_expr()), is there a way of doing add_constraint(product_expr())?

If it's not possible with linear optimisation, where should I be looking instead?


Solution

  • I managed to find an answer to my original question. In case of use to anyone the constraint I needed was:

    add_constraint(sum_expr(x[i] * log(penalties[i]), i = 1:n) >= log(100))
    

    So in words, I'm summing the log transformed penalty values (for which x[i] = 1) against the log transformed penalty total, to mimic a product constraint.

    My original question erroneously implied something that likely misled readers. I was looking for the product of all penalties for which x[i] = 1. Not the product of the values (penalties[i] * x[i]) which as soon as any x[i] = 0 becomes 0.