ADMB Documentation  11.1.2276
 All Classes Files Functions Variables Typedefs Friends Defines
f1b2trst.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: f1b2trst.cpp 1935 2014-04-26 02:02:58Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #  include <admodel.h>
00012 #  include <df1b2fun.h>
00013 #  include <adrndeff.h>
00014 //#include <vmon.h>
00015 
00020 void function_minimizer::trust_region_update(int nvar,int _crit,
00021   independent_variables& x,const dvector& _g,const double& _f)
00022 {
00023   double & f= (double&)_f;
00024   dvector & g= (dvector&)_g;
00025   fmm fmc(nvar);
00026   if (random_effects_flag)
00027   {
00028     initial_params::set_active_only_random_effects();
00029     //int unvar=initial_params::nvarcalc(); // get the number of active
00030     initial_params::restore_start_phase();
00031     initial_params::set_inactive_random_effects();
00032     int nvar1=initial_params::nvarcalc(); // get the number of active
00033     if (nvar1 != nvar)
00034     {
00035       cerr << "failed sanity check in "
00036        "void function_minimizer::quasi_newton_block" << endl;
00037       ad_exit(1);
00038     }
00039   }
00040 
00041   uistream uis("admodel.hes");
00042   if (!uis)
00043   {
00044     cerr << "Error trying to open file admodel.hes" << endl;
00045     ad_exit(1);
00046   }
00047   int hnvar = 0;
00048   uis >> hnvar;
00049   dmatrix Hess(1,hnvar,1,hnvar);
00050   uis >> Hess;
00051   if (!uis)
00052   {
00053     cerr << "Error trying to read Hessian from admodel.hes" << endl;
00054     ad_exit(1);
00055   }
00056 
00057   dmatrix tester(1,hnvar,1,hnvar);
00058 
00059   tester=Hess;
00060 
00061   dvector e=sort(eigenvalues(Hess));
00062 
00063   double lambda=-e(1)+100.;
00064 
00065   for (int i=1;i<=hnvar;i++)
00066   {
00067     tester(i,i)+=lambda;
00068   }
00069   dvector step =  x-solve(tester,g);
00070 
00071   {
00072     // calculate the number of random effects unvar
00073     // this turns on random effects variables and turns off
00074     // everything else
00075     //cout << nvar << endl;
00076     initial_params::set_active_only_random_effects();
00077     //cout << nvar << endl;
00078     int unvar=initial_params::nvarcalc(); // get the number of active
00079     //df1b2_gradlist::set_no_derivatives();
00080 
00081     if (lapprox)
00082     {
00083       delete lapprox;
00084       lapprox=0;
00085       df1b2variable::pool->deallocate();
00086 
00087       for (int i=0;i<df1b2variable::adpool_counter;i++)
00088       {
00089         delete df1b2variable::adpool_vector[i];
00090         df1b2variable::adpool_vector[i]=0;
00091         df1b2variable::nvar_vector[i]=0;
00092         df1b2variable::adpool_counter=0;
00093       }
00094     }
00095     lapprox=new laplace_approximation_calculator(nvar,unvar,1,nvar+unvar,
00096       this);
00097     initial_df1b2params::current_phase=initial_params::current_phase;
00098 
00099     initial_df1b2params::save_varsptr();
00100     allocate();
00101     initial_df1b2params::restore_varsptr();
00102 
00103     df1b2_gradlist::set_no_derivatives();
00104     int nvar=initial_params::nvarcalc_all();
00105     dvector y(1,nvar);
00106     initial_params::xinit_all(y);
00107     initial_df1b2params::reset_all(y);
00108 
00109     g=(*lapprox)(step,f,this);
00110   }
00111 } // end block for quasi newton minimization