Bugzilla – Bug 14245

constrOptim() with "SANN" method

Last modified: 2010-04-11 16:46:11 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

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.

not R bug - contributed package!

The function constrOptim() is in the "stats" package, which is (as far as I know) a "base" package (and not a "contributed" package).

Fix is incorrect, does not even work on the supplied example (grad is missing). Changd for 2.12.0.