Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R约束优化误差_R_Mathematical Optimization - Fatal编程技术网

R约束优化误差

R约束优化误差,r,mathematical-optimization,R,Mathematical Optimization,请注意,我不是很熟悉R,我正在尝试解决以下优化(最小化)。如有任何意见,将不胜感激。 问题似乎在于初始值;我不确定如何选取有效的初始值。尽管它似乎满足文件中给出的标准 thetaImp = 5*(10^-5); eps1 = -0.23; eps2 = 0.31; minFunc <- function(x) { x1 <- x[1]; x2 <- x[2]; -1*(max(thetaImp*x1+eps1,0)*(x1) + max(

请注意,我不是很熟悉R,我正在尝试解决以下优化(最小化)。如有任何意见,将不胜感激。 问题似乎在于初始值;我不确定如何选取有效的初始值。尽管它似乎满足文件中给出的标准

  thetaImp = 5*(10^-5);
  eps1 = -0.23;
  eps2 = 0.31;
  minFunc <- function(x) {
    x1 <- x[1];
    x2 <- x[2];
    -1*(max(thetaImp*x1+eps1,0)*(x1) + max(thetaImp*x2+eps2,0)*(x1+x2))
  }
  ui = rbind(c(1,1), c(1,0), c(0,1));
  ci = c(10000,0,0);
  initValues = c(5000,5000);
  constrOptim(initValues, minFunc, NULL, ui, ci);
  ui %*% initValues - ci
taimp=5*(10^-5);
eps1=-0.23;
eps2=0.31;

minFunc对于等式约束的优化,有
Rsolnp

library(Rsolnp)

thetaImp = 5*(10^-5);
eps1 = -0.23;
eps2 = 0.31;
W = 10000

f <- function(x) { # function to minimize
  x1 <- x[1];
  x2 <- x[2];
  max(thetaImp*x1+eps1,0)*x1 + max(thetaImp*x2+eps2,0)*W
}

eqfun <- function(x){ # function defining the equality
  x[1] + x[2]
}

solnp(c(5000, 5000), # starting values 
      f, 
      eqfun = eqfun, 
      eqB = W, # the equality constraint
      LB=c(0,0) # lower bounds
) 

在这种情况下,
K=2
,我们可以通过一维优化等效地解决问题,以检查:

g <- function(t) { # function to minimize
  x1 <- t
  x2 <- W-t;
  max(thetaImp*x1+eps1,0)*x1 + max(thetaImp*x2+eps2,0)*W
}
optim(5000, g, method="L-BFGS-B", lower=0, upper=W) 


> optim(5000, g, lower=0, upper=W)
$par
[1] 7300
......

g感谢您的详细回答(以及您在统计网站上的其他建议)。我会试试看。另外,如果以后我需要将约束更改为不等式,我将如何使用constrOptim使其在上述情况下工作?另外,我想应该排除封闭形式的解决方案?@user249613您的意思是您需要对$\sum S_I$进行不等式约束?谢谢Stephane,感谢您抽出时间提供这些建议。限制因素将是总额(目前在您的eqfun@user249613中,
solnp
函数中有一个
uneqfun
参数,正是用于处理此类约束的。@stehaneLaurent,感谢您迄今为止的建议。solnp似乎在大部分情况下工作正常。有几个问题。问题一:有时它似乎进入无限期循环,永不返回。在这种情况下,我们可以做任何事情。
g <- function(t) { # function to minimize
  x1 <- t
  x2 <- W-t;
  max(thetaImp*x1+eps1,0)*x1 + max(thetaImp*x2+eps2,0)*W
}
optim(5000, g, method="L-BFGS-B", lower=0, upper=W) 


> optim(5000, g, lower=0, upper=W)
$par
[1] 7300
......