微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ompr:向模型添加一些约束时出现错误消息

如何解决ompr:向模型添加一些约束时出现错误消息

当我向OMPR模型添加约束时,我会收到一条错误消息(它可以像这样正常工作)

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)

如果我在poste添加一个/某些约束(删除注释中的#),则会收到消息

Error in check_for_unkNown_vars_impl(model,the_ast) : 
  The expression contains a variable that is not part of the model.

非常感谢:)

解决方法

我最近也遇到了类似的问题。我能够使用索引中的过滤器功能而不是使用sum_expr中的比较来修复它。

# Example to replicate your poste variable
poste = rep(LETTERS[1:5],2)
print(poste)

#  [1] "A" "B" "C" "D" "E" "A" "B" "C" "D" "E"


# function that accepts the indices and the letter you want to filter poste to
# returns a vector of T/F (one for each index in i_indices)
filter_function <- function(i_indices,letter){
  # A list of indices that align to each of the letters in poste
  # Change this for your actual data
  index_list = lapply(unique(poste),function(letter) which(poste==letter))
  names(index_list) = unique(poste)
  
  # Get the T/F value for each index in i_indices
  # T if poste[index] == the provided letter
  # F otherwise
  return(sapply(i_indices,function(index) index %in% index_list[[letter]]))
}


# Build the model
m = MIPModel() %>%
  add_variable(z[i],i=1:10,type='binary') %>%
  # Call the filter function after your indices
  # Passing the index and the letter you want to limit the indices to
  add_constraint(sum_expr(z[i],i = 1:10,filter_function(i,'B')) == 2)

m$constraints

# Only sums the indices of z where poste == 'B'
# (i = 2 and i = 7)
# [[1]]
# $lhs
# expression(z[2L] + z[7L])
# 
# $sense
# [1] "=="
# 
# $rhs
# expression(2)
# 
# attr(,"class")
# [1] "model_constraint"
,

感谢@cookesd的回答,并感谢您的延迟。

我终于找到了办法,但这不是很干净...

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(cote[i] * z[i],i = 1:n) <= 500)  %>%
            add_constraint( sum_expr(z[i],poste[i] == "G") == as.numeric(input$gardiens)) %>%
            add_constraint( sum_expr(z[i],poste[i] == "D") == as.numeric(input$def)) %>%
            add_constraint( sum_expr(z[i],poste[i] == "M") == as.numeric(input$mil)) %>%
            add_constraint( sum_expr(z[i],poste[i] == "A") == as.numeric(input$att))
            
          contraint3 = as.expression(sum_expr(z[i],poste[i] == "G"))
          contraint4 = as.expression(sum_expr(z[i],poste[i] == "D"))
          contraint5 = as.expression(sum_expr(z[i],poste[i] == "M"))
          contraint6 = as.expression(sum_expr(z[i],poste[i] == "A"))
          
          results$constraints[[3]]$lhs =contraint3
          results$constraints[[4]]$lhs =contraint4
          results$constraints[[5]]$lhs =contraint5
          results$constraints[[6]]$lhs =contraint6

我手动添加results$constraints[[k]]$lhs的值

对于您的问题,我检查了一下,在打印值时一切都很好...我不明白该错误,如果您有其他想法,请不要犹豫。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。