Calling variable in rmpk optimization

59 Views Asked by At

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)
0

There are 0 best solutions below