I have an error message when I add a constraint to my OMPR model (it works properly like this)
n = dim(note_mpg)[1]
nb_joueurs = 18
perf = scale(note_mpg$performance_beta)
cote = note_mpg$cote_alpha
poste = note_mpg$Poste
note_mpg$Buts[is.na(note_mpg$Buts)] <- 0
buts = scale(note_mpg$Buts)
results = MIPModel() %>%
add_variable(z[i], i = 1:n, type = "binary") %>%
set_objective(sum_expr((perf[i] + buts[i]) * z[i], i = 1:n), "max") %>%
add_constraint(sum_expr(z[i], i = 1:n) == nb_joueurs) %>%
# add_constraint(sum_expr( (poste[i] == "G") * z[i], i = 1:n) == 2) %>%
# add_constraint(sum_expr( (poste[i] == "D") * z[i], i = 1:n) == 6) %>%
# add_constraint(sum_expr( (poste[i] == "M") * z[i], i = 1:n) == 6) %>%
# add_constraint(sum_expr( (poste[i] == "A") * z[i], i = 1:n) == 4) %>%
add_constraint(sum_expr(cote[i] * z[i], i = 1:n) <= 500) %>%
solve_model(with_ROI(solver = "glpk")) %>%
get_solution(z[i]) %>%
filter(value > 0)
If I add a/some constraint(s) (I remove my # on a comment) on the poste
I get the message
Error in check_for_unknown_vars_impl(model, the_ast) :
The expression contains a variable that is not part of the model.
Many thanks :)
I've run into a similar problem recently. I was able to fix it using a filter function in the indexing instead of using the comparisons you are in the
sum_expr
.