找回密码
 注册
查看: 11409|回复: 0

R语言:optim()函数中文帮助文档(中英文对照)

[复制链接]
发表于 2012-2-16 17:43:28 | 显示全部楼层 |阅读模式
optim(stats)
optim()所属R语言包:stats

                                        General-purpose Optimization
                                         通用优化

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

General-purpose optimization based on Nelder–Mead, quasi-Newton and conjugate-gradient algorithms. It includes an option for box-constrained optimization and simulated annealing.
通用内尔德米德,准牛顿和共轭梯度算法的优化。它包括一个选项框约束优化和模拟退火。


用法----------Usage----------


optim(par, fn, gr = NULL, ...,
      method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN", "Brent"),
      lower = -Inf, upper = Inf,
      control = list(), hessian = FALSE)



参数----------Arguments----------

参数:par
Initial values for the parameters to be optimized over.
参数的初始值进行优化。


参数:fn
A function to be minimized (or maximized), with first argument the vector of parameters over which minimization is to take place.  It should return a scalar result.
要尽量减少函数(或最大化),与第一个参数的参数向量,其中最小是要发生。它应该返回一个标量结果。


参数:gr
A function to return the gradient for the "BFGS", "CG" and "L-BFGS-B" methods.  If it is NULL, a finite-difference approximation will be used.  For the "SANN" method it specifies a function to generate a new candidate point.  If it is NULL a default Gaussian Markov kernel is used.
一个函数来返回"BFGS","CG"和"L-BFGS-B"方法的梯度。如果是NULL,将使用有限差分近似。 "SANN"方法为它指定一个函数来生成一个新的候选点。如果是的话NULL默认高斯马尔可夫内核被使用。


参数:...
Further arguments to be passed to fn and gr.
进一步的参数被传递给fn和gr。


参数:method
The method to be used. See "Details".
要使用的方法。见“详细资料”。


参数:lower, upper
Bounds on the variables for the "L-BFGS-B" method, or bounds in which to search for method "Brent".
界上"L-BFGS-B"的方法,或边界变量在其中进行搜索的方法"Brent"。


参数:control
A list of control parameters. See "Details".
控制参数的列表。见“详细资料”。


参数:hessian
Logical. Should a numerically differentiated Hessian matrix be returned?
逻辑。应会返回一个数字区别的Hessian矩阵?


Details

详情----------Details----------

Note that arguments after ... must be matched exactly.
请注意,后...参数必须完全匹配。

By default this function performs minimization, but it will maximize if control$fnscale is negative.
此功能默认情况下,执行最小化,但它会最大化control$fnscale如果是负。

The default method is an implementation of that of Nelder and Mead (1965), that uses only function values and is robust but relatively slow.  It will work reasonably well for non-differentiable functions.
默认的方法是实施内尔德和米德(1965),只使用函数值,是强大的,但相对缓慢。非可微函数,它将工作相当不错。

Method "BFGS" is a quasi-Newton method (also known as a variable metric algorithm), specifically that published simultaneously in 1970 by Broyden, Fletcher, Goldfarb and Shanno.  This uses function values and gradients to build up a picture of the surface to be optimized.
方法"BFGS"是一个准牛顿法(又称变尺度算法),具体而言,同时刊登在1970年由Broyden算法,弗莱彻,Goldfarb和Shanno无。这将使用函数值和梯度,以建立一个表面的照片,进行优化。

Method "CG" is a conjugate gradients method based on that by Fletcher and Reeves (1964) (but with the option of Polak–Ribiere or Beale–Sorenson updates).  Conjugate gradient methods will generally be more fragile than the BFGS method, but as they do not store a matrix they may be successful in much larger optimization problems.
方法"CG"是一种共轭梯度法的基础上由弗莱彻和里夫斯(1964)(但波拉克-Ribiere或比尔-索伦森更新的选项)。共轭梯度法通常会比BFGS方法更脆弱,而是因为他们不存储矩阵,他们可能会在更大的优化问题的成功。

Method "L-BFGS-B" is that of Byrd et. al. (1995) which allows box constraints, that is each variable can be given a lower and/or upper bound. The initial value must satisfy the constraints. This uses a limited-memory modification of the BFGS quasi-Newton method. If non-trivial bounds are supplied, this method will be selected, with a warning.
方法"L-BFGS-B"是伯德等。等。 (1995年),这使得框的限制,是每个变量可以给出一个较低的和/或上限。初始值必须满足的约束。使用有限内存BFGS拟牛顿方法的修改。如果提供了不平凡的界限,这种方法将被选中,一个警告。

Nocedal and Wright (1999) is a comprehensive reference for the previous three methods.
nocedal和赖特(1999)是前三种方法综合参考。

Method "SANN" is by default a variant of simulated annealing given in Belisle (1992). Simulated-annealing belongs to the class of stochastic global optimization methods. It uses only function values but is relatively slow. It will also work for non-differentiable functions. This implementation uses the Metropolis function for the acceptance probability. By default the next candidate point is generated from a Gaussian Markov kernel with scale proportional to the actual temperature. If a function to generate a new candidate point is given, method "SANN" can also be used to solve combinatorial optimization problems. Temperatures are decreased according to the logarithmic cooling schedule as given in Belisle (1992, p. 890); specifically, the temperature is set to temp / log(((t-1) %/% tmax)*tmax + exp(1)), where t is the current iteration step and temp and tmax are specifiable via control, see below.  Note that the "SANN" method depends critically on the settings of the control parameters. It is not a general-purpose method but can be very useful in getting to a good value on a very rough surface.
方法"SANN"默认情况下,在Belisle(1992)模拟退火的一个变种。模拟退火是属于类随机全局优化方法。它仅使用函数值,但相对缓慢。它也将适用于非可微函数。此实现使用验收概率大都市功能。默认情况下,下一个候选点产生高斯马尔可夫内核与规模成正比的实际温度。如果给出一个函数来生成一个新的候选点,方法"SANN"也可以被用来解决组合优化问题。根据对数冷却进度表温度下降Belisle(1992,890)。具体地说,温度设置为temp / log(((t-1) %/% tmax)*tmax + exp(1)),其中t是当前迭代步骤和temp 和tmax是通过control可指定,见下文。注意"SANN"方法的关键取决于控制参数的设置。它不是一个通用的方法,但可以在一个非常粗糙的表面上有一个良好的价值是非常有用的。

Method "Brent" is for one-dimensional problems only, using optimize().  It can be useful in cases where optim() is used inside other functions where only method can be specified, such as in mle from package stats4.
方法"Brent"只有一维的问题,使用optimize()。它可以是有用的情况下optim()只有method可以指定其他功能,如在mle包stats4内使用。

Function fn can return NA or Inf if the function cannot be evaluated at the supplied value, but the initial value must have a computable finite value of fn. (Except for method "L-BFGS-B" where the values should always be finite.)
功能fn可以返回NA或Inf如果该功能不能在所提供的价值进行评估,但初始值必须有一个可计算的有限值fn。 (除方法"L-BFGS-B"值应该始终是有限的。)

optim can be used recursively, and for a single parameter as well as many.  It also accepts a zero-length par, and just evaluates the function with that argument.
optim可以递归使用,并为一个单一的参数,以及许多。它还接受一个零长度的par,只是这样的说法与评估的功能。

The control argument is a list that can supply any of the following components:
control参数是一个列表,可以提供以下组件:




trace Non-negative integer. If positive, tracing information on the progress of the optimization is produced. Higher values may produce more tracing information: for method "L-BFGS-B" there are six levels of tracing.  (To understand exactly what
trace非负整数。如果是正数,优化进度的跟踪信息产生。更高的值可能会产生更多的跟踪信息:方法"L-BFGS-B"有6个级别的跟踪。 (要了解什么




fnscale An overall scaling to be applied to the value of fn and gr during optimization. If negative, turns the problem into a maximization problem. Optimization is
fnscale一个整体的应用扩展到优化过程中的价值fn和gr。如果是负数,变成一个最大化问题的问题。优化是




parscale A vector of scaling values for the parameters. Optimization is performed on par/parscale and these should be comparable in the sense that a unit change in any element produces about a unit change in the scaled value.  Not used (nor needed)
parscale一个矢量缩放参数值。优化执行par/parscale的,这些应该是在单位中的任何元素的变化,有关单位的换算值变化产生的意义相媲美。不使用(也不需要)




ndeps A vector of step sizes for the finite-difference approximation to the gradient, on par/parscale
ndeps的矢量有限差分近似的梯度步长par/parscale




maxit The maximum number of iterations. Defaults to 100 for the derivative-based methods, and 500 for "Nelder-Mead".
maxit最大迭代次数。默认100衍生为基础的方法,500"Nelder-Mead"。

For "SANN" maxit gives the total number of function evaluations: there is no other stopping criterion. Defaults to 10000.
"SANN"maxit给功能评价总数:有没有其他的停止准则。 10000默认。




abstol The absolute convergence tolerance. Only
abstol绝对收敛性。只




reltol Relative convergence tolerance.  The algorithm stops if it is unable to reduce the value by a factor of reltol * (abs(val) + reltol) at a step.  Defaults to
reltol相对收敛公差。该算法停止,如果它不能由一个一步reltol * (abs(val) + reltol)的因素,以减少价值。默认为




alpha, beta, gamma Scaling parameters for the "Nelder-Mead" method. alpha is the reflection factor (default 1.0), beta the contraction factor (0.5) and
alpha,beta,gamma"Nelder-Mead"方法缩放参数。 alpha是反射系数(默认为1.0),beta收缩因子(0.5)




REPORT The frequency of reports for the "BFGS", "L-BFGS-B" and "SANN" methods if control$trace is positive. Defaults to every 10 iterations for "BFGS" and
REPORT如果"BFGS","L-BFGS-B"和"SANN"方法的报告频率control$trace是积极的。默认每10次迭代"BFGS"“




type for the conjugate-gradients method. Takes value 1 for the Fletcher–Reeves update, 2 for
type共轭梯度法。注意到弗莱彻·里夫斯更新1值,2




lmm is an integer giving the number of BFGS updates
lmm是一个整数的BFGS更新




factr controls the convergence of the "L-BFGS-B" method. Convergence occurs when the reduction in the objective is within this factor of the machine tolerance. Default is 1e7,
factr控制"L-BFGS-B"方法的收敛性。收敛发生时减少在客观上是在本机容忍的因素。默认值是1e7




pgtol helps control the convergence of the "L-BFGS-B" method. It is a tolerance on the projected gradient in the current search direction. This defaults to zero, when the check is
pgtol有助于控制收敛"L-BFGS-B"方法。这是一个在当前的搜索方向上的投影梯度的容忍。这默认为零,当检查




temp controls the "SANN" method. It is the starting temperature for the cooling schedule. Defaults to
temp控制"SANN"方法。它是为冷却进度表的起始温度。默认为




tmax is the number of function evaluations at each
tmax是在每个功能评价

Any names given to par will be copied to the vectors passed to fn and gr.  Note that no other attributes of par are copied over.
par任何名称将复制到传递fn向量和gr。注意par没有其他属性被复制。


值----------Value----------

A list with components:
与组件列表:


参数:par
The best set of parameters found.
找到最佳的参数集。


参数:value
The value of fn corresponding to par.
fn相应par的价值。


参数:counts
A two-element integer vector giving the number of calls to fn and gr respectively. This excludes those calls needed to compute the Hessian, if requested, and any calls to fn to compute a finite-difference approximation to the gradient.
两个元素的整数向量的电话号码fn和gr分别给予。这不包括那些需要计算黑森州,如果要求调用,并调用任何fn计算有限差分近似的梯度。


参数:convergence
An integer code. 0 indicates successful completion (which is always the case for "SANN" and "Brent").  Possible error codes are     
一个整数的代码。 0表示成功完成(这始终是"SANN"和"Brent"的情况下)。可能出现的错误代码

1indicates that the iteration limit maxit had been reached.  
1表示迭代限制maxit已经达到。

10indicates degeneracy of the Nelder–Mead simplex.  
10表示内尔德Mead单纯退化。

51indicates a warning from the "L-BFGS-B" method; see component message for further details.  
51表示从"L-BFGS-B"方法的警告,详情见组件message。

52indicates an error from the "L-BFGS-B" method; see component message for further details.     
52表示从"L-BFGS-B"方法的错误;详见组件message。


参数:message
A character string giving any additional information returned by the optimizer, or NULL.
给予任何其他信息的一个字符串返回的优化,或NULL。


参数:hessian
Only if argument hessian is true. A symmetric matrix giving an estimate of the Hessian at the solution found.  Note that this is the Hessian of the unconstrained problem even if the box constraints are active.
只有当参数hessian是真实的。发现对称矩阵估计的Hessian提供解决方案。请注意,这是无约束问题的Hessian框约束,即使是积极的。


注意----------Note----------

optim will work with one-dimensional pars, but the default method does not work well (and will warn).  Method "Brent" uses optimize provided bounds are available; "BFGS" often works well enough if not.
optim将一维par的工作,但默认的方法不能很好地工作(并会发出警告)。方法"Brent"使用optimize提供的范围;"BFGS"不够好,如果不经常。


源----------Source----------

The code for methods "Nelder-Mead", "BFGS" and "CG" was based originally on Pascal code in Nash (1990) that was translated by p2c and then hand-optimized.  Dr Nash has agreed that the code can be made freely available.
方法的代码"Nelder-Mead","BFGS"和"CG"Pascal代码最初是基于纳什(1990)被翻译p2c然后用手优化。纳什博士已同意该代码可以免费提供。

The code for method "L-BFGS-B" is based on Fortran code by Zhu, Byrd, Lu-Chen and Nocedal obtained from Netlib (file "opt/lbfgs_bcm.shar": another version is in "toms/778").
代码的方法"L-BFGS-B"朱鲁,伯德,陈和Nocedal基于Fortran代码从NETLIB(文件获得“opt/lbfgs_bcm.shar:另一个版本是在toms/778 )。

The code for method "SANN" was contributed by A. Trapletti.
代码的方法"SANN"贡献由A. Trapletti。


参考文献----------References----------

annealing algorithms on <code>Rd</code>. J. Applied Probability, 29, 885&ndash;895.
memory algorithm for bound constrained optimization. SIAM J. Scientific Computing, 16, 1190&ndash;1208.
conjugate gradients. Computer Journal 7, 148&ndash;154.
Computers. Linear Algebra and Function Minimisation. Adam Hilger.
minimization. Computer Journal 7, 308&ndash;313.
Springer.

参见----------See Also----------

nlm, nlminb.
nlm,nlminb。

optimize for one-dimensional minimization and constrOptim for constrained optimization.
optimize一维的最小化和constrOptim约束优化。


举例----------Examples----------


require(graphics)

fr &lt;- function(x) {   ## Rosenbrock Banana function[]
    x1 <- x[1]
    x2 <- x[2]
    100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}
grr &lt;- function(x) { ## Gradient of 'fr'[#梯度“FR”]
    x1 <- x[1]
    x2 <- x[2]
    c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1),
       200 *      (x2 - x1 * x1))
}
optim(c(-1.2,1), fr)
optim(c(-1.2,1), fr, grr, method = "BFGS")
optim(c(-1.2,1), fr, NULL, method = "BFGS", hessian = TRUE)
## These do not converge in the default number of steps[#这些不衔接的步骤默认数量]
optim(c(-1.2,1), fr, grr, method = "CG")
optim(c(-1.2,1), fr, grr, method = "CG", control=list(type=2))
optim(c(-1.2,1), fr, grr, method = "L-BFGS-B")

flb <- function(x)
    { p <- length(x); sum(c(1, rep(4, p-1)) * (x - c(1, x[-p])^2)^2) }
## 25-dimensional box constrained[#25维框约束]
optim(rep(3, 25), flb, NULL, method = "L-BFGS-B",
      lower=rep(2, 25), upper=rep(4, 25)) # par[24] is *not* at boundary[面值[24] *不*边界]

## "wild" function , global minimum at about -15.81515[#“野生”的功能,全球至少约-15.81515]
fw <- function (x)
    10*sin(0.3*x)*sin(1.3*x^2) + 0.00001*x^4 + 0.2*x+80
plot(fw, -50, 50, n=1000, main = "optim() minimising 'wild function'")

res <- optim(50, fw, method="SANN",
             control=list(maxit=20000, temp=20, parscale=20))
res
## Now improve locally {typically only by a small bit}:[#现在提高本地{通常只由一个小位}:]
(r2 <- optim(res$par, fw, method="BFGS"))
points(r2$par, r2$value, pch = 8, col = "red", cex = 2)

## Combinatorial optimization: Traveling salesman problem[#组合优化旅行商问题]
library(stats) # normally loaded[通常加载]

eurodistmat <- as.matrix(eurodist)

distance &lt;- function(sq) {  # Target function[目标函数]
    sq2 <- embed(sq, 2)
    sum(eurodistmat[cbind(sq2[,2],sq2[,1])])
}

genseq &lt;- function(sq) {  # Generate new candidate sequence[生成新的候选序列]
    idx <- seq(2, NROW(eurodistmat)-1)
    changepoints <- sample(idx, size=2, replace=FALSE)
    tmp <- sq[changepoints[1]]
    sq[changepoints[1]] <- sq[changepoints[2]]
    sq[changepoints[2]] <- tmp
    sq
}

sq &lt;- c(1:nrow(eurodistmat), 1)  # Initial sequence: alphabetic[初始序列:字母]
distance(sq)
# rotate for conventional orientation[传统的方向旋转]
loc <- -cmdscale(eurodist, add=TRUE)$points
x <- loc[,1]; y <- loc[,2]
s <- seq_len(nrow(eurodistmat))
tspinit <- loc[sq,]

plot(x, y, type="n", asp=1, xlab="", ylab="",
     main="initial solution of traveling salesman problem", axes = FALSE)
arrows(tspinit[s,1], tspinit[s,2], tspinit[s+1,1], tspinit[s+1,2],
       angle=10, col="green")
text(x, y, labels(eurodist), cex=0.8)

set.seed(123) # chosen to get a good soln relatively quickly[soln的相对迅速获得了良好的选择]
res <- optim(sq, distance, genseq, method = "SANN",
             control = list(maxit = 30000, temp = 2000, trace = TRUE,
                            REPORT = 500))
res  # Near optimum distance around 12842[近约12842最佳距离]

tspres <- loc[res$par,]
plot(x, y, type="n", asp=1, xlab="", ylab="",
     main="optim() 'solving' traveling salesman problem", axes = FALSE)
arrows(tspres[s,1], tspres[s,2], tspres[s+1,1], tspres[s+1,2],
       angle=10, col="red")
text(x, y, labels(eurodist), cex=0.8)

转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。


注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2025-1-23 06:03 , Processed in 0.032678 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表