我正在尝试使用 R 中的 ompr 包编写优化问题。我尝试了一些示例,现在对尝试实现我的问题的简单版本感到满意。
如果我尝试更改我拥有的代码,我会收到多个错误。这是我使用的代码。我得到的第一个错误:
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)
这里的代码:
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)
如果我将目标更改为:
set_objective(sum(skater_proj*skaters_lineup) + sum(goalie_proj*goalies_lineup))
我遇到了这个问题:
Error in set_objective_(model, expression = lazyeval::as.lazy(substitute(expression), :
object 'skaters_lineup' not found
我不知道这是怎么发生的,我之前定义了变量。
有人有想法吗?