ADMB Documentation  11.1.1022
 All Classes Files Functions Variables Typedefs Friends Defines
newmodm5.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: newmodm5.cpp 542 2012-07-10 21:04:06Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California 
00006  */
00007 #if defined(USE_LAPLACE)
00008 #  include <admodel.h>
00009 #  include <df1b2fun.h>
00010 #  include <adrndeff.h>
00011  
00012 void function_minimizer::prof_minimize_re(int iprof, double sigma,
00013   double new_value, const double& _fprof,const int underflow_flag,
00014   double global_min, const double& _penalties, const double& _final_weight)
00015    {
00016      double& penalties=(double&) _penalties;
00017      double& fprof=(double&) _fprof;
00018      double& final_weight=(double&) _final_weight;
00019     if (!underflow_flag)
00020     {
00021      int max_profile_phases=3;
00022      int profile_phase=1;
00023      initial_params::current_phase = initial_params::max_number_phases;
00024      while (profile_phase <= max_profile_phases)
00025      {
00026       {
00027 // ****************************************************************
00028 // ****************************************************************
00029 // ****************************************************************
00030 // ****************************************************************
00031        int unvar=1;
00032        initial_params::set_active_only_random_effects(); 
00033        //cout << nvar << endl;
00034        unvar=initial_params::nvarcalc(); // get the number of active
00035        initial_params::restore_start_phase(); 
00036        initial_params::set_inactive_random_effects(); 
00037        int nvar=initial_params::nvarcalc(); // get the number of active
00038 
00039 // ****************************************************************
00040 // ****************************************************************
00041 // ****************************************************************
00042 // ****************************************************************
00043 
00044 
00045        dvector g(1,nvar);
00046        independent_variables x(1,nvar);
00047        initial_params::xinit(x);    // get the initial values into the
00048      // x vector
00049        fmm fmc(nvar);
00050        fmc.maxfn= maxfn;
00051        fmc.iprint= iprint;
00052        fmc.crit = crit;
00053        fmc.imax = imax;
00054        fmc.dfn= dfn;
00055        fmc.scroll_flag= scroll_flag;
00056        fmc.min_improve=min_improve;
00057        double f=0.0;
00058        gradient_structure::set_YES_DERIVATIVES();
00059        // set convergence criterion for this phase
00060        if (!(!convergence_criteria))
00061        {
00062          int ind=min(convergence_criteria.indexmax(),
00063            initial_params::current_phase);
00064          fmc.crit=convergence_criteria(ind);
00065        }
00066        if (!(!maximum_function_evaluations))
00067        {
00068   int ind=min(maximum_function_evaluations.indexmax(),
00069            initial_params::current_phase);
00070          fmc.maxfn=int(maximum_function_evaluations(ind));
00071        }
00072        int itnsave=0;
00073        //double weight=pow(50.0,profile_phase)/(sigma*sigma);
00074        double weight;
00075        if (sigma)
00076        {
00077          weight=pow(120.0,profile_phase)/(sigma*sigma);
00078        }
00079        else
00080        {
00081          weight=pow(120.0,profile_phase);
00082        }
00083        final_weight=weight;
00084 
00085 // ****************************************************************
00086 // ****************************************************************
00087 // ****************************************************************
00088 // ****************************************************************
00089   {
00090     // calculate the number of random effects unvar
00091     // this turns on random effects variables and turns off
00092     // everything else
00093     //cout << nvar << endl;
00094     initial_params::set_active_only_random_effects(); 
00095     //cout << nvar << endl;
00096     int unvar=initial_params::nvarcalc(); // get the number of active
00097     //df1b2_gradlist::set_no_derivatives();
00098 
00099     if (funnel_init_var::py)
00100     {
00101       delete funnel_init_var::py;
00102       funnel_init_var::py=0;
00103     }
00104     if (lapprox)
00105     {
00106       delete lapprox;
00107       lapprox=0;
00108       df1b2variable::pool->deallocate();
00109 
00110       for (int i=1;i<df1b2variable::adpool_counter;i++)
00111       {
00112         delete df1b2variable::adpool_vector[i];
00113         df1b2variable::adpool_vector[i]=0;
00114         df1b2variable::nvar_vector[i]=0;
00115       }
00116       df1b2variable::adpool_counter=0;
00117     }
00118     lapprox=new laplace_approximation_calculator(nvar,unvar,1,nvar+unvar,
00119       this); 
00120     if (lapprox==0)
00121     {
00122       cerr << "Error allocating memory for lapprox" << endl;
00123       ad_exit(1);
00124     }
00125     initial_df1b2params::current_phase=initial_params::current_phase;
00126     
00127     initial_df1b2params::save_varsptr();
00128     allocate();
00129     initial_df1b2params::restore_varsptr();
00130 
00131     df1b2_gradlist::set_no_derivatives();
00132     int nvar=initial_params::nvarcalc_all(); 
00133     dvector y(1,nvar);
00134     initial_params::xinit_all(y); 
00135     initial_df1b2params::reset_all(y);
00136 
00137     gradient_structure::set_NO_DERIVATIVES();
00138     //vmon_begin();
00139     // see what kind of hessian we are dealing with
00140     int on=0;
00141     int nopt=0;
00142     //  DF Nov 27 11
00143     initial_params::set_inactive_only_random_effects(); 
00144     nvar=initial_params::nvarcalc(); // get the number of active
00145 
00146     if (lapprox->have_users_hesstype==0)
00147     {
00148       if (initial_df1b2params::separable_flag)
00149       {
00150         if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-newht",nopt))>-1)
00151         {
00152           lapprox->check_hessian_type2(this);
00153         }
00154         else
00155         { 
00156           lapprox->check_hessian_type(this);
00157         }
00158       }
00159       cout << "Hesstype = " << lapprox->hesstype << endl;
00160     }
00161 
00162    /*
00163     cout << "NEED to remove !!!! " << endl;
00164     initial_df1b2params::separable_flag=0;
00165     lapprox->hesstype=1;
00166    */
00167 
00168     // linear mixed effects optimization
00169     if (laplace_approximation_calculator::variance_components_vector) 
00170     {
00171       if (!lapprox)
00172       {
00173         cerr << "this can't happen" << endl;
00174         ad_exit(1);
00175       }
00176       lapprox->get_hessian_components_banded_lme(this);
00177     }
00178 
00179     if (negdirections==0)
00180     {
00181       dvector g1(1,nvar);
00182       while (fmc.ireturn>=0)
00183       {
00184         fmc.fmin(f,x,g);
00185         double diff =
00186         new_value-value(likeprof_params::likeprofptr[iprof]->variable());
00187         if (fmc.itn>itnsave && diff < pow(.1,iprof)*sigma)
00188         {
00189           fmc.ifn=fmc.imax;
00190         }
00191         if (fmc.ireturn>0)
00192         {
00193           g=(*lapprox)(x,f,this);
00194         }
00195         dvariable vf=0.0;
00196         vf=initial_params::reset(dvar_vector(x));
00197         *objective_function_value::pobjfun=0.0;
00198       //**********************************************************
00199       //**********************************************************
00200       //**********************************************************
00201       #if defined(USE_LAPLACE)
00202         if (lapprox)
00203         {
00204           if (lapprox->hesstype==2) 
00205           {
00206             //lapprox->num_separable_calls=0;
00207             lapprox->separable_calls_counter=0;
00208           }
00209         }
00210       #endif
00211       //**********************************************************
00212       //**********************************************************
00213       //**********************************************************
00214       //**********************************************************
00215         userfunction();
00216         dvariable tv=likeprof_params::likeprofptr[iprof]->variable();
00217         vf+=weight*square(new_value-tv);
00218         f+=value(vf);
00219         gradcalc(nvar,g1);
00220         g+=g1;
00221       }
00222     }
00223     initial_params::set_inactive_only_random_effects(); 
00224   }
00225 
00226 
00227 
00228 
00229 // ****************************************************************
00230 // ****************************************************************
00231 // ****************************************************************
00232 // ****************************************************************
00233 
00234 
00235 
00236        gradient_structure::set_NO_DERIVATIVES();
00237        iexit=fmc.iexit;
00238        ihflag=fmc.ihflag;
00239        ihang=fmc.ihang;
00240        maxfn_flag=fmc.maxfn_flag;
00241        quit_flag=fmc.quit_flag;
00242        fprof=value(initial_params::reset(dvar_vector(x)));
00243        *objective_function_value::pobjfun=0.0;
00244       //**********************************************************
00245       //**********************************************************
00246       //**********************************************************
00247       #if defined(USE_LAPLACE)
00248         if (lapprox)
00249         {
00250           if (lapprox->hesstype==2) 
00251           {
00252             //lapprox->num_separable_calls=0;
00253             lapprox->separable_calls_counter=0;
00254           }
00255         }
00256       #endif
00257       //**********************************************************
00258       //**********************************************************
00259       //**********************************************************
00260       //**********************************************************
00261        userfunction();
00262        double tv=value(likeprof_params::likeprofptr[iprof]->variable());
00263        fprof+=value(*objective_function_value::pobjfun);
00264        penalties=weight*(new_value-tv)*(new_value-tv);
00265        fprof+=penalties;
00266        if (quit_flag=='Q') break;
00267        if (!quit_flag || quit_flag == 'N')
00268        {
00269          profile_phase++;
00270        }
00271       }
00272      }
00273     }
00274     else
00275     {
00276       fprof=global_min+20.0;
00277     }
00278    }
00279 
00280 #endif //#if defined(USE_LAPLACE)