[Lme4-commits] r1489 - pkg/lme4Eigen/src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Dec 28 19:57:03 CET 2011
Author: dmbates
Date: 2011-12-28 19:57:03 +0100 (Wed, 28 Dec 2011)
New Revision: 1489
Modified:
pkg/lme4Eigen/src/external.cpp
pkg/lme4Eigen/src/optimizer.cpp
pkg/lme4Eigen/src/optimizer.h
Log:
Removed debugging code, added methods to NelderMead to set convergence characteristics and exported same.
Modified: pkg/lme4Eigen/src/external.cpp
===================================================================
--- pkg/lme4Eigen/src/external.cpp 2011-12-22 21:56:23 UTC (rev 1488)
+++ pkg/lme4Eigen/src/external.cpp 2011-12-28 18:57:03 UTC (rev 1489)
@@ -1,6 +1,6 @@
// external.cpp: externally .Call'able functions in lme4Eigen
//
-// Copyright (C) 2011 Douglas Bates, Martin Maechler and Ben Bolker
+// Copyright (C) 2011-2012 Douglas Bates, Martin Maechler and Ben Bolker
//
// This file is part of lme4.
@@ -35,6 +35,7 @@
using optimizer::Golden;
using optimizer::Nelder_Mead;
+ using optimizer::nm_status;
using std::runtime_error;
@@ -596,11 +597,6 @@
SEXP NelderMead_Create(SEXP lb_, SEXP ub_, SEXP xstep0_, SEXP x_, SEXP xtol_) {
BEGIN_RCPP;
MVec lb(as<MVec>(lb_)), ub(as<MVec>(ub_)), xstep0(as<MVec>(xstep0_)), x(as<MVec>(x_)), xtol(as<MVec>(xtol_));
- // Rcpp::Rcout << "lb: " << lb.adjoint() << std::endl;
- // Rcpp::Rcout << "ub: " << ub.adjoint() << std::endl;
- // Rcpp::Rcout << "xstep0: " << xstep0.adjoint() << std::endl;
- // Rcpp::Rcout << "x: " << x.adjoint() << std::endl;
- // Rcpp::Rcout << "xtol: " << xtol.adjoint() << std::endl;
Nelder_Mead *ans =
new Nelder_Mead(lb, ub, xstep0, x, optimizer::nl_stop(as<MVec>(xtol_)));
return wrap(XPtr<Nelder_Mead>(ans, true));
@@ -609,10 +605,49 @@
SEXP NelderMead_newf(SEXP ptr_, SEXP f_) {
BEGIN_RCPP;
- XPtr<Nelder_Mead>(ptr_)->newf(::Rf_asReal(f_));
+ switch (XPtr<Nelder_Mead>(ptr_)->newf(::Rf_asReal(f_))) {
+ case optimizer::nm_evals: return ::Rf_ScalarInteger(-4);
+ case optimizer::nm_forced: return ::Rf_ScalarInteger(-3);
+ case optimizer::nm_nofeasible: return ::Rf_ScalarInteger(-2);
+ case optimizer::nm_x0notfeasible: return ::Rf_ScalarInteger(-1);
+ case optimizer::nm_active: return ::Rf_ScalarInteger(0);
+ case optimizer::nm_minf_max: return ::Rf_ScalarInteger(1);
+ case optimizer::nm_fcvg: return ::Rf_ScalarInteger(2);
+ case optimizer::nm_xcvg: return ::Rf_ScalarInteger(3);
+ }
END_RCPP;
}
+
+ SEXP NelderMead_setForce_stop(SEXP ptr_, SEXP stp_) {
+ BEGIN_RCPP;
+ XPtr<Nelder_Mead>(ptr_)->setForce_stop(::Rf_asLogical(stp_));
+ END_RCPP;
+ }
+
+ SEXP NelderMead_setFtol_abs(SEXP ptr_, SEXP fta_) {
+ BEGIN_RCPP;
+ XPtr<Nelder_Mead>(ptr_)->setFtol_rel(::Rf_asReal(fta_));
+ END_RCPP;
+ }
+
+ SEXP NelderMead_setFtol_rel(SEXP ptr_, SEXP ftr_) {
+ BEGIN_RCPP;
+ XPtr<Nelder_Mead>(ptr_)->setFtol_rel(::Rf_asReal(ftr_));
+ END_RCPP;
+ }
+
+ SEXP NelderMead_setMaxeval(SEXP ptr_, SEXP mm_) {
+ BEGIN_RCPP;
+ XPtr<Nelder_Mead>(ptr_)->set_Maxeval(::Rf_asInteger(mm_));
+ END_RCPP;
+ }
+ SEXP NelderMead_setMinf_max(SEXP ptr_, SEXP mm_) {
+ BEGIN_RCPP;
+ XPtr<Nelder_Mead>(ptr_)->setMinf_max(::Rf_asReal(mm_));
+ END_RCPP;
+ }
+
SEXP NelderMead_xeval(SEXP ptr_) {
BEGIN_RCPP;
return wrap(XPtr<Nelder_Mead>(ptr_)->xeval());
@@ -631,6 +666,13 @@
END_RCPP;
}
+ // return the number of function evaluations performed
+ SEXP NelderMead_evals(SEXP ptr_) {
+ BEGIN_RCPP;
+ return wrap(XPtr<Nelder_Mead>(ptr_)->evals());
+ END_RCPP;
+ }
+
// nonlinear model response (also the base class for other response classes)
SEXP nls_Create(SEXP y, SEXP weights, SEXP offset, SEXP mu, SEXP sqrtXwt,
@@ -754,6 +796,11 @@
CALLDEF(NelderMead_Create, 5),
CALLDEF(NelderMead_newf, 2),
+ CALLDEF(NelderMead_setForce_stop, 2),
+ CALLDEF(NelderMead_setFtol_abs, 2),
+ CALLDEF(NelderMead_setFtol_rel, 2),
+ CALLDEF(NelderMead_setMaxeval, 2),
+ CALLDEF(NelderMead_setMinf_max, 2),
CALLDEF(NelderMead_value, 1),
CALLDEF(NelderMead_xeval, 1),
CALLDEF(NelderMead_xpos, 1),
@@ -763,8 +810,6 @@
CALLDEF(nls_Laplace, 4), // methods
CALLDEF(nls_updateMu, 2),
-// CALLDEF(test_NMopt, 0),
-
{NULL, NULL, 0}
};
Modified: pkg/lme4Eigen/src/optimizer.cpp
===================================================================
--- pkg/lme4Eigen/src/optimizer.cpp 2011-12-22 21:56:23 UTC (rev 1488)
+++ pkg/lme4Eigen/src/optimizer.cpp 2011-12-28 18:57:03 UTC (rev 1489)
@@ -143,23 +143,15 @@
nm_status Nelder_Mead::restart(const Scalar& f) {
d_fl = d_vals.minCoeff(&d_il);
d_fh = d_vals.maxCoeff(&d_ih);
- // Rcpp::Rcout << "restart: neval = " << d_stop.ev()
- // << ", d_fl = " << d_fl
- // << ", d_fh = " << d_fh
- // << ", d_il = " << d_il
- // << ", d_ih = " << d_ih << std::endl;
d_c = (d_pts.rowwise().sum() - d_pts.col(d_ih)) / d_n; // compute centroid
- // Rcpp::Rcout << "centroid: " << d_c.adjoint() << std::endl;
-
- // Check if the simplex has gotten to be too small. First
- // calculate the coefficient-wise maximum abs deviation for
- // the centroid.
- d_xcur = (d_pts.colwise() - d_c).array().abs().rowwise().maxCoeff();
- // Rcpp::Rcout << "Radius: " << d_xcur.adjoint() << std::endl;
- if (d_stop.x(VectorXd::Constant(d_n, 0.), d_xcur)) return nm_xcvg;
+ // check for x convergence by calculating the maximum absolute
+ // deviation from the centroid for each coordinate in the simplex
+ if (d_stop.x(VectorXd::Constant(d_n, 0.),
+ (d_pts.colwise() - d_c).array().abs().rowwise().maxCoeff()))
+ return nm_xcvg;
+
if (!reflectpt(d_xcur, d_c, alpha, d_pts.col(d_ih))) return nm_xcvg;
- // Rcpp::Rcout << "d_xcur (after reflectpt): " << d_xcur.adjoint() << std::endl;
d_xeval = d_xcur;
d_stage = nm_postreflect;
return nm_active;
Modified: pkg/lme4Eigen/src/optimizer.h
===================================================================
--- pkg/lme4Eigen/src/optimizer.h 2011-12-22 21:56:23 UTC (rev 1488)
+++ pkg/lme4Eigen/src/optimizer.h 2011-12-28 18:57:03 UTC (rev 1489)
@@ -34,12 +34,13 @@
public:
nl_stop(const VectorXd&); // constructor
+ void incrEvals() {nevals++;}
void resetEvals() {nevals = 0;} // setters
- void incrEvals() {nevals++;}
- void setMinf_max(const Scalar& mm) {minf_max = mm;}
void setFtol_rel(const Scalar& ftr) {ftol_rel = ftr;}
void setFtol_abs(const Scalar& fta) {ftol_abs = fta;}
void setForce_stop(const bool& stp) {force_stop = stp;}
+ void setMinf_max(const Scalar& mm) {minf_max = mm;}
+ void set_Maxeval(const unsigned int& mm) {maxeval = mm;}
bool f(const Scalar& f, const Scalar& oldf) const { // convergence checking
return (f <= minf_max || ftol(f, oldf));
@@ -121,6 +122,12 @@
Scalar value() const {return d_minf;}
nm_status newf(const Scalar&);
+
+ void setForce_stop(const bool& stp) {d_stop.setForce_stop(stp);}
+ void setFtol_abs(const Scalar& fta) {d_stop.setFtol_abs(fta);}
+ void setFtol_rel(const Scalar& ftr) {d_stop.setFtol_rel(ftr);}
+ void set_Maxeval(const unsigned int& mm) {d_stop.set_Maxeval(mm);}
+ void setMinf_max(const Scalar& mm) {d_stop.setMinf_max(mm);}
};
class Golden {
More information about the Lme4-commits
mailing list