【问题标题】:Constrained optimisation with function in the constraint and binary variable在约束和二元变量中使用函数进行约束优化
【发布时间】:2019-09-09 19:16:06
【问题描述】:

我正在寻找一种方法来解决 - 在 R 中 - 形式的约束优化问题

min sum(x)

s.t. f(x) < k

其中 x 是长度为 n 的二进制变量(0 或 1),f(x) 是依赖于整个 x 变量的函数,k 是整数常量。因此,f(x) 不是针对 x 的每个值的一组 n 个约束(例如 sqrt(x)),而是基于二进制变量 x 的整个值集满足的约束。

我已尝试使用以下语法的 ompr R 包

v < 1:10
result <- MILPModel() %>%
add_variable(x[i], i = 1:v, type = "binary") %>%
set_objective(sum_expr(x[i], i = 1:v), sense = "min") %>%
add_constraint(f(x) <= 60) %>%
solve_model(with_ROI(solver = "glpk"))

但它不起作用,因为我认为该包不接受全局 f(x) 约束。

【问题讨论】:

  • f(x) 使模型非线性。 OMPR 仅支持线性模型。
  • 有解决问题的具体代码和包建议吗?
  • f 的功能是什么?您能否编辑您的帖子以提供一个完全可重现的示例?
  • 函数比较复杂,我就不贴了。对于这里提出的玩具示例,我们假设它是 sd(x)。

标签: r optimization


【解决方案1】:

这是rgenoud 包的解决方案。

library(rgenoud)

g <- function(x){
  c(
    ifelse(sd(x) > 0.2, 0, 1), # set the constraint (here sd(x)>0.2) in this way
    sum(x) # the objective function (to minimize/maximize)
  )
}

solution <- genoud(
  g, lexical = 2,
  nvars = 30, 
  starting.values = rep(0, 30), 
  Domains = cbind(rep(0,30), rep(1,30)),
  data.type.int = TRUE)

solution$par # the values of x
## [1] 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
sd(solution$par) # is the constraint satisfied ?
## [1] 0.2537081
solution$value
## [1] 0 2 ; 0 is the value of ifelse(sd(x)>0.2,0,1) and 2 is the value of sum(x)

请参阅?genoud 中的备注部分以了解lexical 参数。

【讨论】:

    猜你喜欢
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 2022-01-22
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多