【问题标题】:ompr : error message when I add some constraint to my modelompr : 当我向我的模型添加一些约束时出现错误消息
【发布时间】:2021-01-20 15:49:01
【问题描述】:

向我的 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.

非常感谢:)

【问题讨论】:

    标签: r ompr


    【解决方案1】:

    我最近遇到了类似的问题。我能够在索引中使用过滤器函数来修复它,而不是使用您在 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"
    

    【讨论】:

    • 谢谢你的回答,它现在运行正常但我没有任何结果,sum(results$value) = 0 你有什么想法吗?
    • 即使我运行你的台词,我也没有$lhs # expression(z[2L] + z[7L]) 我有$lhs expression(0)
    • 您的第二条评论听起来好像该函数正在返回一个全为 FALSE 的向量。您可以修改过滤器函数以在重新调整它之前打印返回向量以查看是否是这种情况?当您从我上面的代码中打印poste 时,它是否像我输入的注释行一样打印?
    【解决方案2】:

    感谢@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], i = 1:n, poste[i] == "G") == as.numeric(input$gardiens)) %>%
                add_constraint( sum_expr(z[i], i = 1:n, poste[i] == "D") == as.numeric(input$def)) %>%
                add_constraint( sum_expr(z[i], i = 1:n, poste[i] == "M") == as.numeric(input$mil)) %>%
                add_constraint( sum_expr(z[i], i = 1:n, poste[i] == "A") == as.numeric(input$att))
                
              contraint3 = as.expression(sum_expr(z[i], i = 1:n, poste[i] == "G"))
              contraint4 = as.expression(sum_expr(z[i], i = 1:n, poste[i] == "D"))
              contraint5 = as.expression(sum_expr(z[i], i = 1:n, poste[i] == "M"))
              contraint6 = as.expression(sum_expr(z[i], i = 1:n, 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的值

    对于您的问题,我检查了打印值时一切正常...我不明白这个错误,如果您有任何其他想法,请不要犹豫。

    【讨论】:

      猜你喜欢
      • 2015-03-31
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多