ADMB Documentation  11.1.1897
 All Classes Files Functions Variables Typedefs Friends Defines
xmodelm4.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: xmodelm4.cpp 1136 2013-08-05 19:40:39Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00007 #  include <admodel.h>
00008 int function_minimizer::random_effects_flag=0;
00009 int function_minimizer::test_trust_flag=0;
00010 int function_minimizer::negative_eigenvalue_flag=0;
00011 #if defined(USE_LAPLACE)
00012 #  include <df1b2fun.h>
00013 #include <adrndeff.h>
00014 //dvariable AD_uf_inner(const dvector& x,const dvar_vector& u);
00015 void get_second_ders(int xs,int us,const init_df1b2vector y,dmatrix& Hess,
00016   dmatrix& Dux, df1b2_gradlist * f1b2gradlist,function_minimizer * pfmin);
00017 double calculate_laplace_approximation(const dvector& x,const dvector& u0,
00018   const dmatrix& Hess,const dvector& _xadjoint,const dvector& _uadjoint,
00019   const dmatrix& _Hessadjoint,function_minimizer * pmin);
00020 
00021 //int function_minimizer::hesstype=0;
00022 
00023 /*
00024 int function_minimizer::set_hessian_type(int n)
00025 {
00026   if (n<0 || n>4)
00027   {
00028     cerr << "Illegal value of " << n
00029       << " in function_minimizer::set_hessian_type(int) " << endl;
00030     ad_exit(1);
00031   }
00032   hesstype=n;
00033 }
00034 */
00035 
00036 void   random_effects_userfunction(double f,const dvector& x,
00037   const dvector& g);
00038 
00039 void function_minimizer::AD_uf_outer(void){;}
00040 void function_minimizer::AD_uf_inner(void){;}
00041 //void function_minimizer::user_function(const init_df1b2vector& x,
00042  // df1b2variable& f){;}
00043 void function_minimizer::user_function(void){;}
00044 
00045 #else      // if !defined(USE_LAPLACE)
00046 
00047 
00048 void function_minimizer::quasi_newton_block(int nvar,int _crit,
00049   independent_variables& x,const dvector& _g,const double& _f)
00050 {
00051   double & f= (double&)_f;
00052   dvector & g= (dvector&)_g;
00053   // *********************************************************
00054   // block for quasi-newton minimization
00055   int itnold=0;
00056   fmm fmc(nvar);
00057   int on1;
00058   if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-nox"))>-1)
00059   {
00060     fmc.noprintx=1;
00061   }
00062   fmc.maxfn= maxfn;
00063   int nopt=0;
00064   if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-dd",nopt))>-1)
00065   {
00066     if (!nopt)
00067     {
00068       cerr << "Usage -iprint option needs integer  -- ignored" << endl;
00069       fmc.iprint=iprint;
00070     }
00071     else
00072     {
00073       int jj=atoi(ad_comm::argv[on1+1]);
00074       fmc.dcheck_flag=jj;
00075     }
00076   }
00077   nopt=0;
00078   if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-iprint",nopt))>-1)
00079   {
00080     if (!nopt)
00081     {
00082       cerr << "Usage -iprint option needs integer  -- ignored" << endl;
00083       fmc.iprint=iprint;
00084     }
00085     else
00086     {
00087       int jj=atoi(ad_comm::argv[on1+1]);
00088       fmc.iprint=jj;
00089     }
00090   }
00091   else
00092   {
00093     fmc.iprint= iprint;
00094   }
00095   fmc.crit = crit;
00096   fmc.imax = imax;
00097   fmc.dfn= dfn;
00098   fmc.scroll_flag= scroll_flag;
00099   fmc.min_improve=min_improve;
00100   gradient_structure::set_YES_DERIVATIVES();
00101   // set convergence criterion for this phase
00102   if (_crit)
00103   {
00104     fmc.crit = _crit;
00105   }
00106   if (!(!convergence_criteria))
00107   {
00108     int ind=min(convergence_criteria.indexmax(),
00109       initial_params::current_phase);
00110     fmc.crit=convergence_criteria(ind);
00111   }
00112   if (!(!maximum_function_evaluations))
00113   {
00114     int ind=min(maximum_function_evaluations.indexmax(),
00115       initial_params::current_phase);
00116     fmc.maxfn= (int) maximum_function_evaluations(ind);
00117   }
00118 
00119   //if (!random_effects_flag)
00120   {
00121     dvariable xf=initial_params::reset(dvar_vector(x));
00122     reset_gradient_stack();
00123     gradcalc(0,g);
00124     while (fmc.ireturn>=0)
00125     {
00126       fmc.fmin(f,x,g);
00127       if (fmc.ireturn>0)
00128       {
00129         dvariable vf=0.0;
00130         vf=initial_params::reset(dvar_vector(x));
00131         *objective_function_value::pobjfun=0.0;
00132         userfunction();
00133         vf+=*objective_function_value::pobjfun;
00134         f=value(vf);
00135         gradcalc(nvar,g);
00136       }
00137     }
00138   }
00139 
00140   gradient_structure::set_NO_DERIVATIVES();
00141   ffbest=fmc.fbest;
00142   iexit=fmc.iexit;
00143   ifn=fmc.ifn;
00144   ihflag=fmc.ihflag;
00145   ihang=fmc.ihang;
00146   maxfn_flag=fmc.maxfn_flag;
00147   quit_flag=fmc.quit_flag;
00148   objective_function_value::gmax=fabs(fmc.gmax);
00149 } // end block for quasi newton minimization
00150 #endif    // if !defined(USE_LAPLACE)