[Rd] optim "CG" bug w/patch proposal (PR#8786)

westfeld at inf.tu-dresden.de westfeld at inf.tu-dresden.de
Fri Apr 21 20:32:22 CEST 2006


Dear R team,

when using optim with method "CG" I got the wrong $value for the
reported $par.

Example:
f<-function(p) {
        if (!all(p>-.7)) return(2)
        if (!all(p<.7)) return(2)
        sin((p[1])^2)*sin(p[2])
}
optim(c(0.1,-0.1),f,method="CG",control=list(trace=0,type=1))
$par 19280.68 -10622.32
$value -0.2346207 # should be 2!

optim(c(0.1,-0.1),f,method="CG",control=list(trace=0,type=2))
$par 3834.021 -2718.958
$value -0.0009983175 # should be 2!

Fix:
--- optim.c     (Revision 37878)
+++ optim.c     (Arbeitskopie)
@@ -970,7 +970,8 @@
                            if (!accpoint) {
                                steplength *= stepredn;
                                if (trace) Rprintf("*");
-                           }
+                           } else
+                               *Fmin = f;
                        }
                    } while (!(count == n || accpoint));
                    if (count < n) {

After fix:
optim(c(0.1,-0.1),f,method="CG",control=list(trace=0,type=1))
$par 0.6993467 -0.4900145
$value -0.2211150
optim(c(0.1,-0.1),f,method="CG",control=list(trace=0,type=2))
$par 3834.021 -2718.958
$value 2

Wishlist:

1. Please make type=3 the default in optim (it is more robust).

2. The $par reported for type=2 is still not satisfactory. I found out
that this can be improved by limiting G3 to a maximum of about 2000
(maybe even smaller). However, I'm not a "CG" expert and can live with a
suboptimal result.

--- optim.c     (Revision 37878)
+++ optim.c     (Arbeitskopie)
@@ -946,6 +946,8 @@
                        G3 = G1 / G2;
                    else
                        G3 = 1.0;
+                   if (G3 > 2e3)
+                       G3 = 2e3;
                    gradproj = 0.0;
                    for (i = 0; i < n; i++) {
                        t[i] = t[i] * G3 - g[i];

Andreas

-- 
Andreas Westfeld, 0432 01CC F511 9E2B 0B57 5993 0B22 98F8 4AD8 EEEA
<westfeld at inf.tu-dresden.de> http://www.inf.tu-dresden.de/~aw4
TU Dresden Fakultät Informatik, Institut für Systemarchitektur
Datenschutz und Datensicherheit, Tel. +49-351-463-37918



More information about the R-devel mailing list