矩阵中值的优化 [英] Optimization of values in a matrix

查看:111
本文介绍了矩阵中值的优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用Rsolnp进行优化,但是我很难弄清楚如何要求R查找值以填充矩阵(而不是向量).使用Rsolnp或任何其他优化程序可以做到这一点吗?

I typically use Rsolnp for optimization but I am having trouble figuring out how to ask R to find values to fill a matrix (instead of a vector). Is that possible with Rsolnp or any other optimizer?

这是一个不起作用的简化示例:

Here is a simplified example that is not working:

library(Rsolnp)

a<-matrix(rnorm(9), ncol=3)
b<-matrix(rnorm(9), ncol=3)

f1<-function(H) {
    return(sum(H*a))
}

f2<-function(H) {
    return(sum(H*b))
}

lH<-matrix(rep(0, 9), ncol=3)
uH<-matrix(rep(1, 9), ncol=3)
pars<-uH
target<-1.2

sol <- gosolnp(pars, fixed=NULL, fun=f1, eqfun=f2, eqB=target, LB=lH, UB=uH, distr=uH, n.restarts=10, n.sim=20000, cluster= NULL)

从输出中可以看到,Rsolnp似乎被请求弄糊涂了:

As you can see from the output, Rsolnp seems to be confused by the request:

> sol
$values
[1] 1e+10

$convergence
[1] 0

$pars
[1] NA NA NA NA NA NA NA NA NA

$start.pars
[1] 0.90042133 0.33262541 0.94586530 0.02083822 0.99953060 0.10720068 0.14302770 0.67162637 0.25463806

$rseed
[1] 1487866229

推荐答案

gosolnp()似乎不适用于矩阵.我在调试模式下通过了该函数,并且调用solnp()失败,并显示以下消息:

It seems that gosolnp() does not work with matrices. I went through the function in debugging mode and there is a call of solnp() that fails with the message:

pb/cbind(vscale [(neq + 2):( neq + mm + 1)],vscale [(neq + 2):( neq +: 不整合数组

Error in pb/cbind(vscale[(neq + 2):(neq + mm + 1)], vscale[(neq + 2):(neq + : non-conformable arrays

但是,由于矩阵只是具有维度属性集的向量,因此您始终可以根据向量来重新表述问题.在您的情况下,这很容易,因为您永远不会做实际上需要矩阵的事情(例如,矩阵乘积).只是在任何地方省略matrix()都可以.

But since a matrix is just a vector with the dimension attribute set, you can always reformulate your problem in terms of vectors. In your case, this is very easy, because you never do something that actually requires a matrix (like, for instance, a matrix product). Just omitting matrix() everywhere works fine.

但是我认为这只是简化问题的一个属性,而实际问题确实需要用矩阵来表示.您可以通过仅在函数f1()f2()中的 中将向量转换为矩阵来解决该问题,如下所示:

But I assume that this is just a property of your simplified problem and your actual problem indeed needs to be expressed in terms of matrices. You could get around the problem by converting your vectors into matrices only inside the functions f1() and f2() as follows:

f1 <- function(H) {
    return(sum(matrix(H, ncol = 3) * a))
}

f2 <- function(H) {
    return(sum(matrix(H, ncol = 3) * b))
}

然后可以像以前一样将ab定义为矩阵,但是lHuH必须是向量:

You can then define a and b as matrices as before, but lH and uH must be vectors:

a <- matrix(rnorm(9), ncol=3)
b <- matrix(rnorm(9), ncol=3)

lH <- rep(0, 9)
uH <- rep(1, 9)
pars <- uH
target <- 1.2

现在您可以拨打gosolnp():

sol <- gosolnp(pars, fixed = NULL, fun = f1, eqfun = f2,
               eqB = target, LB = lH, UB = uH, distr = uH,
               n.restarts = 10, n.sim = 20000, cluster = NULL)
sol$pars
## [1] 3.917819e-08 9.999997e-01 4.748336e-07 1.000000e+00 5.255060e-09 5.114680e-10
## [7] 4.899963e-01 1.000000e+00 9.260947e-08

这篇关于矩阵中值的优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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