Bug 14245 - constrOptim() with "SANN" method
Summary: constrOptim() with "SANN" method
Alias: None
Product: R
Classification: Unclassified
Component: Misc (show other bugs)
Version: R 2.10.1
Hardware: All All
: P5 normal
Assignee: R-core
Depends on:
Reported: 2010-03-31 15:06 UTC by Arne Henningsen
Modified: 2015-12-14 13:46 UTC (History)
3 users (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Arne Henningsen 2010-03-31 15:06:08 UTC
The documentation of constrOptim() says that it can be used with "Any ‘optim’ method that permits infinite values for the objective function [...] (currently all but "L-BFGS-B")." Hence, it seems that constrOptim() can be used with the Simulated Annealing (SANN) method. However, this is not (reasonably) possible, because constrOptim() always sets argument "gr" of optim() equal to the gradient, although this argument must be "a function to generate a new candidate point" (and *not* the gradient) if optim() is called with the "SANN" method. A simple solution is to replace the lines
gradient <- function(theta, ...) {
   dR(theta, theta.old, ...)
by the lines
if( method == "SANN" ) {
   if( is.null( grad ) ) {
      gradient <- NULL
   } else {
      gradient <- grad
} else {
   gradient <- function(theta, ...) {
      dR(theta, theta.old, ...)
Hence, if the user chooses the "SANN" method and does *not* use the "grad" argument, optim() is called without argument "gr" equal to NULL:

constrOptim( c(.5,0), fr, ui=rbind(c(-1,0),c(1,-1)), ci=c(-0.9,0.1), method="SANN")

And if the user chooses the "SANN" method and uses the "grad" argument to specify "a function to generate a new candidate point", optim() is called with argument "gr" equal to "grad".

constrOptim( c(.5,0), fr, function(x)c(rnorm(1,x[1]),rnorm(1,x[2])), ui=rbind(c(-1,0),c(1,-1)), ci=c(-0.9,0.1), method="SANN")

It might be argued that it does not make sense to use constrOptim() with "SANN" (see [1] and the following messages in this thread). However, even if all developeRs agree on this, constrOptim() should be changed anyway, e.g. the documentation should state that SANN does not make sense and constrOptim() should not call optim() with the SANN method and using the gradient function for generating new candidate points.

[1] http://tolstoy.newcastle.edu.au/R/e4/help/08/01/1328.html
Comment 1 Arne Henningsen 2010-04-07 08:44:40 UTC
An improved version of constrOptim() that fixes this bug is available as constrOptim2() in the developer version of the maxLik package on R-Forge now. It will be available on CRAN in some days. Comments and suggestions are welcome.
Comment 2 Simon Urbanek 2010-04-07 14:06:52 UTC
not R bug - contributed package!
Comment 3 Arne Henningsen 2010-04-07 14:15:21 UTC
The function constrOptim() is in the "stats" package, which is (as far as I know) a "base" package (and not a "contributed" package).
Comment 4 Brian Ripley 2010-04-11 16:46:11 UTC
Fix is incorrect, does not even work on the supplied example (grad is missing).

Changd for 2.12.0.