简体   繁体   English

目标和约束中的变量数量错误

[英]Error with number of variables in objective and constraints

I'm trying to set up a linear optimization using the ROI package in R, following instructions in this link: https://roi.r-forge.r-project.org/use_case_portfolio_optimization.html#introduction . I'm trying to set up a linear optimization using the ROI package in R, following instructions in this link: https://roi.r-forge.r-project.org/use_case_portfolio_optimization.html#introduction . However i'm getting an error when trying to implement a 'Group constraint' ( https://roi.r-forge.r-project.org/use_case_portfolio_optimization.html#group_constraints ).但是,在尝试实施“组约束”( https://roi.r-forge.r-project.org/use_case_portfolio_optimization.html#group_constraints )时出现错误。 Here is my sample code这是我的示例代码

df <- data.frame(Group=rep(c('A', 'B', 'C', 'D'), each=4),
                 SubGroup=c('A.1', 'A.2', 'A.3', 'A.1', 'B.1', 'B.1', 'B.2', 'B.2', 'C.1', 'C.2', 'C.2', 'C.2', 'D.1', 'D.2', 'D.3', 'D.4'),
                 score=round(runif(16, 0, 1),2),
                 wgt=rep(1/16,16),
                 id=1:16)

data <- matrix(round(runif(256, -5, 5),3), ncol=16, byrow=TRUE)
Cov1 <- as.matrix(cov(data))

Taken from the linked article:摘自链接文章:

group_constraint <- function(r_mat, index, coef.index = 1, dir = "==", rhs) {
  ## index = (i, j)
  ## coef.index = c(a,b)
  ## rhs = c
  #x.names <- colnames(r_mat)
  N <- NCOL(r_mat)
  L <- rep(0, N)
  L[index] <- coef.index
  L_constraint(L = L, dir = dir, rhs = rhs)
}

group_1 <- group_constraint(df$score, index = c(3, 12, 13), dir = "<=", rhs = 0.5)

My optimization problem我的优化问题

full_invest <- L_constraint(rep(1, 16), "==", 1)

LP <- OP(objective = df$score,
          group_1,
          bounds = V_bound(ui = seq_len(16), ub = rep(0.40, 16)),
          max = TRUE)
sol1 <- ROI_solve(LP, "glpk")
sol1
x <- solution(sol1)
x

When i run this i get the following error: "Error in.check_constraints.L_constraint(constr, x): dimension missmatch. OP has 16 variables the constraints have 13".当我运行它时,我收到以下错误:“错误 in.check_constraints.L_constraint(constr, x):尺寸不匹配。OP 有 16 个变量,约束有 13 个”。 If i change group_1 to group_1 <- group_constraint(df$score, index = c(3, 12, 16), dir = "<=", rhs = 0.6) This now works, as ncol(group_1) is 16.如果我将 group_1 更改为group_1 <- group_constraint(df$score, index = c(3, 12, 16), dir = "<=", rhs = 0.6)现在可以使用,因为 ncol(group_1) 是 16。

Based on example 1 in the link ( https://roi.r-forge.r-project.org/use_case_portfolio_optimization.html#example_1:_maximize_expected_return_subject_to_budget_normalization_and_group_constraints ) I cant see where i'm going wrong with my example.基于链接中的示例 1( https://roi.r-forge.r-project.org/use_case_portfolio_optimization.html#example_1:_maximize_expected_return_subject_to_budget_normalization_and_group_constraints )我看不出我的示例哪里出错了。

Any help would be appreciated.任何帮助,将不胜感激。

What ROI tries to tell you is that your objective has a different shape than your constraint matrix, since your objective has length 16 and your constraint matrix has only 13 columns. ROI试图告诉您的是,您的目标与约束矩阵的形状不同,因为您的目标长度为16 ,而约束矩阵只有13列。

This happens because the group_constraint function expects a matrix and when passed a vector is thinks the passed matrix has only 1 column.发生这种情况是因为group_constraint function 需要一个矩阵,并且在传递一个向量时认为传递的矩阵只有 1 列。

You can simply fix this by exchanging the call to the group_sparsity constraint with group_1 <- group_constraint(matrix(df$score, nrow=1), index = c(3, 12, 13), dir = "<=", rhs = 0.5)您可以通过使用group_1 <- group_constraint(matrix(df$score, nrow=1), index = c(3, 12, 13), dir = "<=", rhs = 0.5)交换对group_sparsity约束的调用来简单地解决此问题group_1 <- group_constraint(matrix(df$score, nrow=1), index = c(3, 12, 13), dir = "<=", rhs = 0.5)

Sys.setenv(ROI_LOAD_PLUGINS = FALSE)
library("ROI.plugin.glpk")
library("ROI")
#> ROI: R Optimization Infrastructure
#> Registered solver plugins: nlminb, glpk.
#> Default solver: auto.
df <- data.frame(Group=rep(c('A', 'B', 'C', 'D'), each=4),
                 SubGroup=c('A.1', 'A.2', 'A.3', 'A.1', 'B.1', 'B.1', 'B.2', 'B.2', 'C.1', 'C.2', 'C.2', 'C.2', 'D.1', 'D.2', 'D.3', 'D.4'),
                 score=round(runif(16, 0, 1),2),
                 wgt=rep(1/16,16),
                 id=1:16)

data <- matrix(round(runif(256, -5, 5),3), ncol=16, byrow=TRUE)
Cov1 <- as.matrix(cov(data))

group_constraint <- function(r_mat, index, coef.index = 1, dir = "==", rhs) {
  ## index = (i, j)
  ## coef.index = c(a,b)
  ## rhs = c
  #x.names <- colnames(r_mat)
  N <- NCOL(r_mat)
  L <- rep(0, N)
  L[index] <- coef.index
  L_constraint(L = L, dir = dir, rhs = rhs)
}

group_1 <- group_constraint(matrix(df$score, nrow=1), index = c(3, 12, 13), dir = "<=", rhs = 0.5)

full_invest <- L_constraint(rep(1, 16), "==", 1)

LP <- OP(objective = df$score,
          group_1,
          bounds = V_bound(ui = seq_len(16), ub = rep(0.40, 16)),
          max = TRUE)
sol1 <- ROI_solve(LP, "glpk")
sol1
#> Optimal solution found.
#> The objective value is: 2.600000e+00
x <- solution(sol1)
x
#>  [1] 0.4 0.4 0.0 0.4 0.4 0.4 0.4 0.4 0.4 0.4 0.4 0.1 0.4 0.4 0.4 0.4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM