0

I'm trying to set a model objective, and I have a matrix A with the same number of rows as the number of variables, which are binary. And basically I want to take a columnwise average using only the rows that end up being chosen (where x = 1). In this example 3 rows will be chosen. My attempt (shown below) at doing this involves using sum_expr on rows of A; the mth row would only contribute to the sum if x[m] = 1. But this doesn't work. Is there a way to do this?

k <- 3
x <- model$add_variable("x", type="binary", i=1:n_models)
model$set_objective(sum_expr(mean(k/sum_expr(A[m,]*x[m],m=1:n_models)*A[i,])*x[i],i=1:n_models))
model$add_constraint(sum_expr(x[i],i=1:n_models)==k)

Alternatively, is there a way to "get" the value of the variable inside sum_expr, i.e., treat it like a normal variable? For example:

sum_expr(mean(apply(sweep(A,1,x,'*'),2,sum)*A[i,])*x[i],i=1:n_models)
Alex
  • 77
  • 1
  • 6
  • Can you use `dput(model)` to share your R object `model` in a format that can be loaded into an IDE for testing? – Josh Gray MD Dec 06 '21 at 21:02
  • I assume the model has to stay linear. So things like apply, mean, and other functions are not allowed. In other words, keep in mind that OMPR has to generate `Ax=b`. – Erwin Kalvelagen Dec 07 '21 at 17:16
  • I have been able to use the 'mean' function inside sum_expr. @JoshGray Hmm, how do I do that? – Alex Dec 07 '21 at 18:26

0 Answers0