【问题标题】:setup for R optimization problem with weights constraints设置具有权重约束的 R 优化问题
【发布时间】:2019-12-09 00:36:13
【问题描述】:

在 R 中设置以下优化时寻求帮助。试图了解哪个优化包是最好的。我还没有太多可用的代码,正在寻找最好的工具/包的想法,甚至开始解决问题,谢谢。

scores<-c(1.7, 3.0, 5.3, 7.0, 8.43, 6.8, 6.8)
target_scores<-c(2,3,6,7,8.5,7,7)
weights<-rep(0,7)

目标函数:

objective<-function(weights){

                  weights_pct<-weights/sum(weights)

                  return((sum(scores*weights_pct)-1.05)+sum(abs(scores*weights_pct - target_scores)))            
                }

优化目标是返回最优的权重集。 我试图在目标函数中直接包含权重总和为 1 的事实。如果有 是执行此操作的另一种方法,可以在问题设置中进行更改/增强。
欢迎就用于此问题的最佳求解器提供建议(目前已查看 optim 和 solve.qp)。
谢谢。

【问题讨论】:

    标签: r optimization


    【解决方案1】:

    我猜CVXR 会是解决您问题的强大工具

    library(CVXR)
    scores<-c(1.7, 3.0, 5.3, 7.0, 8.43, 6.8, 6.8)
    target_scores<-c(2,3,6,7,8.5,7,7)
    
    # declare variable
    weights<-Variable(length(scores))
    
    # set up minimization problem and solve it
    constraints <- list(weights >=0,sum(weights) == 1) # the constraint for sum up to 1 with positive weights
    f <- Minimize((sum(scores*weights)-1.05)+sum(abs(scores*weights - target_scores)))
    problem <- Problem(f,constraints)
    result <- solve(problem)
    
    # show results of `weights`
    > result$getValue(weights)
               [,1]
    [1,] 0.02049869
    [2,] 0.10787489
    [3,] 0.14437726
    [4,] 0.17562971
    [5,] 0.20452523
    [6,] 0.17354711
    [7,] 0.17354711
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多