Rvmmin(Rvmmin)
Rvmmin()所属R语言包:Rvmmin
Variable metric nonlinear function minimization with bounds constraints
有界约束变尺度非线性函数的最小化
译者:生物统计家园网 机器人LoveR
描述----------Description----------
An R implementation of a nonlinear variable metric method for minimization of nonlinear functions possibly subject to bounds (box) constraints and masks (fixed parameters). The algorithm is based on Nash (1979) Algorithm 21 for main structure, which is itself drawn from Fletcher's (1970) variable metric code. This is also the basis of optim() method 'BFGS' which, however, does not deal with bounds or masks. In the present method, an approximation to the inverse Hessian (B) is used to generate a search direction t = - B &*& g, a simple backtracking line search is used until an acceptable point is found, and the matrix B is updated using a BFGS formula. If no acceptable point can be found, we reset B to the identity i.e., the search direction becomes the negative gradient. If the search along the negative gradient is unsuccessful, the method terminates.
R的实现可能受边界(盒)的限制和口罩(固定参数)的最小化非线性函数的非线性变度量法。该算法是基于纳什(1979年)的主要结构,这本身就是来自弗莱彻(1970)的变度量代码的算法21。这也是OPTIM()方法的BFGS的,但是,不处理边界或面罩的基础。在本方法中,一个近似到逆Hessian矩阵(B)是用来生成一个搜索方向t = - B&*&克,使用一个简单的回溯线搜索,直到一个可接受的点被找到,和矩阵B使用BFGS公式更新。如果没有可接受的点,可以发现,我们的身份,即复位b,搜索方向变为负梯度。如果沿负梯度搜索是不成功的,则该方法终止。
This code is entirely in R to allow users to explore and understand the method. It also allows bounds (or box) constraints and masks (equality constraints) to be imposed on parameters.
此代码完全是在R允许用户探索和理解的方法。它也允许界限(或箱)的限制和口罩(等式约束的参数)的罚款。
用法----------Usage----------
Rvmmin(par, fn, gr, lower, upper, bdmsk, control = list(), ...)
参数----------Arguments----------
参数:par
A numeric vector of starting estimates.
开始估计一个数值向量。
参数:fn
A function that returns the value of the objective at the supplied set of parameters par using auxiliary data in .... The first argument of fn must be par.
在提供的一组参数的函数返回值的客观par使用辅助数据...... fn的第一个参数必须par。
参数:gr
A function that returns the gradient of the objective at the supplied set of parameters par using auxiliary data in .... The first argument of fn must be par. This function returns the gradient as a numeric vector.
一个函数,返回的目的在提供的一组参数的梯度par使用辅助数据...... fn的第一个参数必须par。这个函数返回一个数值向量的梯度。
If gr is NOT supplied, a numerical approximation will be used. This is discouraged, as the method seems to work better with accurate gradients. Furthermore, the computation of numerical gradients is many times slower – of the order of 1000s – than using an analytic function. Note the option of using numerical gradients from package numDeriv.
如果不提供克,将使用一个数值逼近。这是气馁,因为这个方法似乎更好地与准确的梯度。此外,数值梯度计算是慢很多倍 - 1000个的顺序 - 比使用一个解析函数。请注意该选项的数值梯度包numDeriv。
参数:lower
A vector of lower bounds on the parameters.
一个向量的参数的下限。
参数:upper
A vector of upper bounds on the parameters.
一个向量参数的上界。
参数:bdmsk
An indicator vector, having 1 for each parameter that is "free" or unconstrained, and 0 for any parameter that is fixed or MASKED for the duration of the optimization.
一个指标向量,具有1的每个参数,该参数是“自由”或无约束,和0的任何参数,该参数是固定的持续时间的优化或屏蔽。
参数:control
An optional list of control settings.
可选列表控制设置。
参数:...
Further arguments to be passed to fn.
进一步的参数被传递给fn。
Details
详细信息----------Details----------
Functions fn must return a numeric value. The control argument is a list. Successful completion. The source code Rvmmin for R is still a work in progress, so users should watch the console output.
功能fn必须返回一个数值。 control参数是一个列表。成功完成。源代码RvmminR是仍然是一个进展中的工作,因此用户应该注意的控制台输出。
The control argument is a list.
control参数是一个列表。
maxitA limit on the number of iterations (default 500). Note that this is used to compute a quantity maxfeval<-round(sqrt(n+1)*maxit) where n is the number of parameters to be minimized.
maxitA限制的迭代次数(默认值500)。请注意,这是用来计算出的数量maxfeval<全面(的sqrt第(n +1)*麦克斯特),其中,n是数以最小化的参数。
maximizeSet TRUE to maximize the function (default FALSE).
maximizeSet TRUE发挥最大的功用(默认为FALSE)。
traceSet 0 (default) for no output, >0 for trace output (larger values imply more output).
traceSet 0(默认)无输出,跟踪输出为0(值越大意味着更多的输出)。
epsTolerance used to calculate numerical gradients. Default is 1.0E-7. See source code for Rvmmin for details of application.
epsTolerance用于计算数值梯度。默认值为1.0E-7。 Rvmmin的应用程序的详细信息,请参阅源代码。
usenumDerivDefault is FALSE. Set TRUE to use numDeriv for numerical gradient approximations.
usenumDerivDefault是FALSE。设置为TRUE数值梯度近似使用numDeriv。
dowarn= TRUE if we want warnings generated by optimx. Default is TRUE.
dowarn= TRUE,如果我们想警告所产生的optimx。默认值是TRUE。
值----------Value----------
A list with components: <table summary="R valueblock"> <tr valign="top"><td>par</td> <td> The best set of parameters found.</td></tr> <tr valign="top"><td>value</td> <td> The value of the objective at the best set of parameters found.</td></tr> <tr valign="top"><td>counts</td> <td> A vector of two integers giving the number of function and gradient evaluations.</td></tr> <tr valign="top"><td>convergence</td> <td> An integer indicating the situation on termination of the function. 0 indicates that the method believes it has succeeded. Other values:
的组件列表:<table summary="R valueblock"> <tr valign="top"> <TD> par </ TD> <TD>最好的一组参数。</ TD> </ TR> <tr valign="top"> <TD> value </ TD> <TD>最好的一套参数,发现的价值目标。</ TD> </ TR> <TR VALIGN = “顶”> <TD> counts </ TD> <td>一个向量的两个整数数的功能和梯度评价。</ TD> </ TR> <tr valign="top"> < convergence TD> </ TD> <TD>一个整数,指示的情况下终止的功能。 0表明,该方法认为它已经成功。其它值:
1indicates that the iteration limit maxit had been reached.
1表示迭代限制maxit已达到。
20indicates that the initial set of parameters is inadmissible, that is, that the function cannot be computed or returns an infinite, NULL, or NA value.
20表明,最初的一组参数是不可接受的,那就是,该函数不能计算或返回一个无限的,NULL,NA值。
21indicates that an intermediate set of parameters is inadmissible. </td></tr> <tr valign="top"><td>message</td> <td> A description of the situation on termination of the function.</td></tr> <tr valign="top"><td>bdmsk</td> <td> Returned index describing the status of bounds and masks at the proposed solution. Parameters for which bdmsk are 1 are unconstrained or "free", those with bdmsk 0 are masked i.e., fixed. For historical reasons, we indicate a parameter is at a lower bound using -3 or upper bound using -1.</td></tr> </table>
21表明,中间的一组参数是不可接受的。 </ TD> </ TR> <tr valign="top"> <TD> message</ TD> <TD>说明的情况下终止的功能。</ TD> </ TR> <tr valign="top"> <TD> bdmsk </ TD> <TD>返回的索引描述状态的边界和口罩所建议的解决方案。参数的bdmsk 1不受约束或“自由”,那些与bdmsk 0被屏蔽,即,固定的。由于历史的原因,我们一个参数是一个下界使用-3的上限的使用-1。</ TD> </ TR> </ TABLE>
参考文献----------References----------
Fletcher, R (1970) ...
Nash, J C (1979, 1990) ...
To be added.
参见----------See Also----------
optim
optim
实例----------Examples----------
#####################[####################]
## Rosenbrock Banana function[#的Rosenbrock香蕉功能]
fr <- function(x) {
x1 <- x[1]
x2 <- x[2]
100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}
ansrosenbrock <- Rvmmin(fn=fr,par=c(1,2))
print(ansrosenbrock) # use print to allow copy to separate file that [使用打印到单独的文件中,允许拷贝,]
# can be called using source()[可以使用源代码(被称为)]
#####################[####################]
# Simple bounds and masks test[简单的边界和口罩测试]
bt.f<-function(x){
sum(x*x)
}
bt.g<-function(x){
gg<-2.0*x
}
n<-10
xx<-rep(0,n)
lower<-rep(0,n)
upper<-lower # to get arrays set[阵列设置]
bdmsk<-rep(1,n)
bdmsk[(trunc(n/2)+1)]<-0
for (i in 1:n) {
lower[i]<-1.0*(i-1)*(n-1)/n
upper[i]<-1.0*i*(n+1)/n
}
xx<-0.5*(lower+upper)
ansbt<-Rvmmin(xx, bt.f, bt.g, lower, upper, bdmsk, control=list(trace=1))
print(ansbt)
#####################[####################]
genrose.f<- function(x, gs=NULL){ # objective function[目标函数]
## One generalization of the Rosenbrock banana valley function (n parameters)[第一个泛化的Rosenbrock香蕉山谷功能(参数)]
n <- length(x)
if(is.null(gs)) { gs=100.0 }
fval<-1.0 + sum (gs*(x[1n-1)]^2 - x[2:n])^2 + (x[2:n] - 1)^2)
return(fval)
}
genrose.g <- function(x, gs=NULL){
# vectorized gradient for genrose.f[矢量化梯度genrose.f]
# Ravi Varadhan 2009-04-03[拉维Varadhan 2009-04-03]
n <- length(x)
if(is.null(gs)) { gs=100.0 }
gg <- as.vector(rep(0, n))
tn <- 2:n
tn1 <- tn - 1
z1 <- x[tn] - x[tn1]^2
z2 <- 1 - x[tn]
gg[tn] <- 2 * (gs * z1 - z2)
gg[tn1] <- gg[tn1] - 4 * gs * x[tn1] * z1
gg
}
# analytic gradient test[解析梯度测试]
xx<-rep(pi,10)
lower<-NULL
upper<-NULL
bdmsk<-NULL
genrosea<-Rvmmin(xx,genrose.f, genrose.g)
genrosenl<-Rvmmin(xx,genrose.f) # use local numerical gradient[使用本地的数值梯度]
genrosenx<-Rvmmin(xx,genrose.f,control=list(usenumDeriv=TRUE)) # use numDeriv for gradients[使用numDeriv的渐变]
cat("genrosea uses analytic gradient\n")
print(genrosea)
cat("genrosenl uses standard numerical gradient\n")
print(genrosenl)
cat("genrosenx uses numDeriv gradient\n")
print(genrosenx)
maxfn<-function(x) {
n<-length(x)
ss<-seq(1,n)
f<-10-(crossprod(x-ss))^2
f<-as.numeric(f)
return(f)
}
negmaxfn<-function(x) {
f<-(-1)*maxfn(x)
return(f)
}
cat("test that maximize=TRUE works correctly\n")
n<-6
xx<-rep(1,n)
ansmax<-Rvmmin(xx,maxfn, control=list(maximize=TRUE,trace=1))
print(ansmax)
cat("using the negmax function should give same parameters\n")
ansnegmax<-Rvmmin(xx,negmaxfn, control=list(trace=1))
print(ansnegmax)
#####################[####################]
cat("test bounds and masks\n")
nn<-4
startx<-rep(pi,nn)
lo<-rep(2,nn)
up<-rep(10,nn)
grbds1<-Rvmmin(startx,genrose.f,lower=lo,upper=up)
print(grbds1)
cat("test lower bound only\n")
nn<-4
startx<-rep(pi,nn)
lo<-rep(2,nn)
grbds2<-Rvmmin(startx,genrose.f,lower=lo)
print(grbds2)
cat("test lower bound single value only\n")
nn<-4
startx<-rep(pi,nn)
lo<-2
up<-rep(10,nn)
grbds3<-Rvmmin(startx,genrose.f,lower=lo)
print(grbds3)
cat("test upper bound only\n")
nn<-4
startx<-rep(pi,nn)
lo<-rep(2,nn)
up<-rep(10,nn)
grbds4<-Rvmmin(startx,genrose.f,upper=up)
print(grbds4)
cat("test upper bound single value only\n")
nn<-4
startx<-rep(pi,nn)
grbds5<-Rvmmin(startx,genrose.f,upper=10)
print(grbds5)
cat("test masks only\n")
nn<-6
bd<-c(1,1,0,0,1,1)
startx<-rep(pi,nn)
grbds6<-Rvmmin(startx,genrose.f,bdmsk=bd)
print(grbds6)
cat("test upper bound on first two elements only\n")
nn<-4
startx<-rep(pi,nn)
upper<-c(10,8, Inf, Inf)
grbds7<-Rvmmin(startx,genrose.f,upper=upper)
print(grbds7)
cat("test lower bound on first two elements only\n")
nn<-4
startx<-rep(0,nn)
lower<-c(0,1.1, -Inf, -Inf)
grbds8<-Rvmmin(startx,genrose.f,genrose.g,lower=lower, control=list(maxit=2000))
print(grbds8)
cat("test n=1 problem using simple squares of parameter\n")
sqtst<-function(xx) {
res<-sum((xx-2)*(xx-2))
}
nn<-1
startx<-rep(0,nn)
onepar<-Rvmmin(startx,sqtst, control=list(trace=1))
print(onepar)
cat("Suppress warnings\n")
oneparnw<-Rvmmin(startx,sqtst, control=list(dowarn=FALSE,trace=1))
print(oneparnw)
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|