Rsolnp:在cbind(temp,funv)中:结果的行数不是向量长度的倍数(arg 1) [英] Rsolnp: In cbind(temp, funv) : number of rows of result is not a multiple of vector length (arg 1)

查看:590
本文介绍了Rsolnp:在cbind(temp,funv)中:结果的行数不是向量长度的倍数(arg 1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是stackoverflow的新手,并进行了很多搜索,但找不到我的问题的答案.我正在尝试使用优化软件包Rsolnp来最大程度地减少问题.尽管求解器为我提供了解决方案,但是每次运行代码时,我都会收到以下警告消息:

I'm new to stackoverflow and searched a lot, but couldn't find an answer to my question. I'm trying to minimise the problem bellow with the optimisation package Rsolnp. Although the solver gives me a solution, every time I run the code I get the following warning message:

警告消息:1:在cbind(temp,funv)中: 结果的行数不是向量长度(arg 1)的倍数

Warning messages: 1: In cbind(temp, funv) : number of rows of result is not a multiple of vector length (arg 1)

此外,该解决方案与我通过ipop和resolve.QP获得的解决方案完全不同.他们的解决方案几乎是相同的(0.2480,0.0000,0.0121,0.7400). 我尝试了许多不同的问题提法,但无法弄清楚我做错了什么.感谢所有帮助和信息!

Furthermore, the solution is completely different from the solutions I get with ipop and solve.QP. Their solutions are almost the same (0.2480, 0.0000, 0.0121, 0.7400). I tried many different formulations of the problem, but could not figure out what I did wrong. I am thankful for all help and information!

library(Rsolnp)
# Starting Values
x0 <- c(0.25,0,0.01,0.75)


fn <- function(x){
  d <- c(0.0308, 0.0269, 0.0145, 0.0130)
  d <- -d
  D <- cbind(c(0.1486, 0.0778, -0.0240, -0.0154), 
         c(0.0778, 0.1170, 0.0066, 0.0029), 
         c(-0.0240, 0.0066, 0.0444, 0.0193), 
         c(-0.0154, 0.0029, 0.0193, 0.0148))
 out <- t(d) %*% x + 0.5 * (t(x) %*% D %*% x)
 out
}


# Inequality Constraint: 0 =< x 0 =< 1
lx <- rep(0,4)
ux <- rep(1,4)



sol <- solnp(pars = x0, 
         fun = fn, 
         eqfun = sum, 
         eqB = 1, 
         ineqLB = lx, 
         ineqUB = ux)
sol$pars

推荐答案

欢迎使用SO.

您在这里遇到了两个问题:a)没有结果的问题,以及b)无法理解的警告消息.

You got two issues here a) problem w.r.t the result and b) a warning message you can't make any sense of.

ad a) 这仅仅是因为LaGrange优化通常是通过最小化负值而不是最大化正值来完成的.那就是区别.将d设置为-d时(我在编辑中是这样做的),该解决方案与其他求解器产生的解决方案相同.

ad a) This is simply because LaGrange optimization is often done by minimizing a negative value instead of maximizing a positive value. That's been the difference. When setting d to -d (I did so in the edit) the solution is the same as the solution produced by other solvers.

ad b) 警告消息问题有些棘手.该消息本身意味着cbind试图绑定两个不同长度的列. R的作用是,它会根据需要多次重复较短列中的值,以使两个相等长度的列绑定.如果长度不是较短的整数倍,则会收到此警告,但是R继续重复较短的值(如果这意味着存在一些未使用的余数).

ad b) the warning message issue is somewhat tricky. The message itself means that cbind tries to bind two columns of different length. What R does is it repeats the values in the shorter column as often as necessary to get two columns of equal length to bind. If the longer is not a multiple of the shorter you get this warning, but R continues to repeat values of the shorter if it means there's some unused remainder.

但是,看起来软件包中有错误. 当您运行

However, it looks like there's a bug in the package. When you run

options(warn = 2) # turns warnings to errors and breaks code
debug(solnp)
sol <- solnp(pars = x0, 
     fun = fn, 
     eqfun = sum, 
     eqB = 1, 
     ineqLB = lx, 
     ineqUB = ux)

您意识到正在创建一个称为tempdf的对象,从而导致该问题.但是,此对象不再在solnp代码中使用.我会问作者,也许他们可以确认这是一个错误或以其他方式显示.

you realize that there is an object called tempdf being created that causes the problem. However, this object is never used again in the code of solnp. I'll ask the authors, maybe they can confirm this is a bug or show otherwise.

这篇关于Rsolnp:在cbind(temp,funv)中:结果的行数不是向量长度的倍数(arg 1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆