简体   繁体   中英

Error in optimization problem with ompr package in R

I am trying to code an optimization problem with the ompr package in R. I tried out a few examples and was now comfortable with trying to implement a simple version of my problem.

I get multiple errors if I try to change the code I have. This is the code I used. The first error i get:

Error in set_objective(sum(skater_proj[i] * skaters_lineup[i]) + sum(goalie_proj[f] *  : 
  unused arguments (i = 1:num_skaters, f = 1:num_goalies)

Here the code:

model <- MIPModel() %>% 
  add_variable(skaters_lineup[i],i = 1:num_skaters,type = "binary") %>%
  add_variable(goalies_lineup[i],i = 1:num_goalies,type = "binary") %>%
  add_constraint(sum(goalies_lineup[i]) == 1, i=1:num_goalies)%>%
  add_constraint(sum(skaters_lineup[i]) == 8, i=1:num_skaters)%>%
  add_constraint(sum(centers[i]*skaters_lineup[i]) <= 3, i=1:num_skaters)%>%  
  add_constraint(sum(centers[i]*skaters_lineup[i]) >= 2, i=1:num_skaters)%>%  
  add_constraint(sum(wingers[i]*skaters_lineup[i]) <= 4, i=1:num_skaters)%>%  
  add_constraint(sum(wingers[i]*skaters_lineup[i]) >= 3, i=1:num_skaters)%>%   
  add_constraint(sum(defenders[i]*skaters_lineup[i]) <= 3, i=1:num_skaters)%>%  
  add_constraint(sum(defenders[i]*skaters_lineup[i]) >= 2, i=1:num_skaters)%>% 
  add_constraint(sum(salary_skater[i]*skaters_lineup[i]) + sum(salary_goalie[d]*goalies_lineup[d]) <= 50000 , i=1:num_skaters,d=1:num_goalies)

set_objective(sum(skater_proj[i]*skaters_lineup[i]) + sum(goalie_proj[f]*goalies_lineup[f]),i=1:num_skaters , f = 1:num_goalies)

If I change the objective to:

set_objective(sum(skater_proj*skaters_lineup) + sum(goalie_proj*goalies_lineup))

i get this problem:

Error in set_objective_(model, expression = lazyeval::as.lazy(substitute(expression),  : 
  object 'skaters_lineup' not found

I don't know how this can happen, I defined the variable before.

Has anybody an idea?

You should try the sum_expr function from ompr instead of the base sum function because it allows you to set the summation index to sum across. The second objective you listed, won't work even with the sum_expr call because you have to provide the index values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM