ADMB Documentation  11.1.1632
 All Classes Files Functions Variables Typedefs Friends Defines
paral5re.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: paral5re.cpp 1112 2013-07-12 21:41:41Z 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 <df1b2fun.h>
00009 #include <adrndeff.h>
00010 #include <admodel.h>
00011 
00012 void function_minimizer::function_evaluation_block_pvm_slave_random_effects
00013   (int nvar,int _crit,
00014   independent_variables& x,const dvector& _g,const double& _f)
00015 {
00016   double & f= (double&)_f;
00017   //dvector& g=(dvector&) _g;
00018   // *********************************************************
00019   // block for quasi-newton minimization
00020   tracing_message(traceflag,"C2");
00021   //int itnold=0;
00022   fmm fmc(nvar);
00023   int on1;
00024   if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-nox"))>-1)
00025   {
00026     fmc.noprintx=1;
00027   }
00028   fmc.maxfn= maxfn;
00029   int nopt=0;
00030   if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-dd",nopt))>-1)
00031   {
00032     if (!nopt)
00033     {
00034       cerr << "Usage -iprint option needs integer  -- ignored" << endl;
00035       fmc.iprint=iprint;
00036     }
00037     else
00038     {
00039       int jj=atoi(ad_comm::argv[on1+1]);
00040       fmc.dcheck_flag=jj;
00041     }
00042   }
00043   nopt=0;
00044   if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-iprint",nopt))>-1)
00045   {
00046     if (!nopt)
00047     {
00048       cerr << "Usage -iprint option needs integer  -- ignored" << endl;
00049       fmc.iprint=iprint;
00050     }
00051     else
00052     {
00053       int jj=atoi(ad_comm::argv[on1+1]);
00054       fmc.iprint=jj;
00055     }
00056   }
00057   else
00058   {
00059     fmc.iprint= iprint;
00060   }
00061   fmc.crit = crit;
00062   fmc.imax = imax;
00063   fmc.dfn= dfn;
00064   fmc.scroll_flag= scroll_flag;
00065   fmc.min_improve=min_improve;
00066   tracing_message(traceflag,"D2");
00067   gradient_structure::set_YES_DERIVATIVES();
00068   // set convergence criterion for this phase
00069   if (_crit)
00070   {
00071     fmc.crit = _crit;
00072   }
00073   if (!(!convergence_criteria))
00074   {
00075     int ind=min(convergence_criteria.indexmax(),
00076       initial_params::current_phase);
00077     fmc.crit=convergence_criteria(ind);
00078   }
00079   if (!(!maximum_function_evaluations))
00080   {
00081     int ind=min(maximum_function_evaluations.indexmax(),
00082       initial_params::current_phase);
00083     fmc.maxfn= (int) maximum_function_evaluations(ind);
00084   }
00085 
00086 
00087 
00088   int unvar=1;
00089   if (random_effects_flag)
00090   {
00091     initial_params::set_active_only_random_effects();
00092     //cout << nvar << endl;
00093     unvar=initial_params::nvarcalc(); // get the number of active
00094     initial_params::restore_start_phase();
00095     initial_params::set_inactive_random_effects();
00096     int nvar1=initial_params::nvarcalc(); // get the number of active
00097     if (nvar1 != nvar)
00098     {
00099       cerr << "failed sanity check in "
00100        "void function_minimizer::quasi_newton_block" << endl;
00101       ad_exit(1);
00102     }
00103   }
00104 
00105   if (!random_effects_flag || !unvar)
00106   {
00107    /*
00108     dvariable xf=initial_params::reset(dvar_vector(x));
00109     reset_gradient_stack();
00110 
00111     gradcalc(0,g);
00112     while (fmc.ireturn>=0)
00113     {
00114       fmc.fmin(f,x,g);
00115       if (fmc.ireturn>0)
00116       {
00117         dvariable vf=0.0;
00118         vf=initial_params::reset(dvar_vector(x));
00119         *objective_function_value::pobjfun=0.0;
00120         userfunction();
00121         vf+=*objective_function_value::pobjfun;
00122         f=value(vf);
00123         gradcalc(nvar,g);
00124       }
00125     }
00126     */
00127   }
00128   else
00129   {
00130 
00131     // calculate the number of random effects unvar
00132     // this turns on random effects variables and turns off
00133     // everything else
00134     //cout << nvar << endl;
00135     initial_params::set_active_only_random_effects();
00136     //cout << nvar << endl;
00137     int unvar=initial_params::nvarcalc(); // get the number of active
00138     //df1b2_gradlist::set_no_derivatives();
00139 
00140     if (lapprox)
00141     {
00142       delete lapprox;
00143       lapprox=0;
00144       df1b2variable::pool->deallocate();
00145 
00146       for (int i=0;i<df1b2variable::adpool_counter;i++)
00147       {
00148         delete df1b2variable::adpool_vector[i];
00149         df1b2variable::adpool_vector[i]=0;
00150         df1b2variable::nvar_vector[i]=0;
00151         df1b2variable::adpool_counter=0;
00152       }
00153     }
00154     lapprox=new laplace_approximation_calculator(nvar,unvar,1,nvar+unvar,
00155       this);
00156     initial_df1b2params::current_phase=initial_params::current_phase;
00157 
00158     initial_df1b2params::save_varsptr();
00159     allocate();
00160     initial_df1b2params::restore_varsptr();
00161 
00162     df1b2_gradlist::set_no_derivatives();
00163     int nvar=initial_params::nvarcalc_all();
00164     dvector y(1,nvar);
00165     initial_params::xinit_all(y);
00166     initial_df1b2params::reset_all(y);
00167 
00168     //vmon_begin();
00169     int ireturn=0;
00170     while (ireturn>=0)
00171     {
00172       //fmc.fmin(f,x,g);
00173       ireturn=get_int_from_master();
00174       if (ireturn>0)
00175       {
00176         //lapprox->default_calculations_parallel_slave(x,f,this);
00177         (*lapprox)(x,f,this);
00178       }
00179     }
00180     //vmon_done();
00181     // turn off the random effects
00182     initial_params::set_inactive_only_random_effects();
00183     //df1b2variable::pool.deallocate();
00184 
00185   }
00186 
00187 
00188 
00189 
00190   gradient_structure::set_NO_DERIVATIVES();
00191   ffbest=fmc.fbest;
00192   iexit=fmc.iexit;
00193   ifn=fmc.ifn;
00194   ihflag=fmc.ihflag;
00195   ihang=fmc.ihang;
00196   maxfn_flag=fmc.maxfn_flag;
00197   quit_flag=fmc.quit_flag;
00198   objective_function_value::gmax=fabs(fmc.gmax);
00199 } // end block for quasi newton minimization
00200 
00201 /*
00202 void function_minimizer::pvm_master_function_evaluation_random_effects
00203   (double& f,independent_variables& x,const dvector & _g,int nvar)
00204 {
00205   dvector& g=(dvector&) _g;
00206   g=lapprox->function_default_calculations_parallel(x,f,this);
00207 }
00208 */
00209   // void function_minimizer::function_evaluation_block_pvm_slave(void)
00210   // {
00211   //   gradient_structure::set_YES_DERIVATIVES();
00212   //   reset_gradient_stack();
00213   //   //gradcalc(nvar,g);
00214   //   do
00215   //   {
00216   //     int ireturn=get_int_from_master();
00217   //     if (ireturn<0) break;
00218   //     pvm_slave_function_evaluation();
00219   //   }
00220   //   while(1);
00221   //   gradient_structure::set_NO_DERIVATIVES();
00222   // }
00223   //
00224   // void function_minimizer::pvm_slave_function_evaluation(void)
00225   // {
00226   //   dvariable vf=0.0;
00227   //   dvar_vector vx=get_x_from_master();
00228   //   vf=initial_params::reset(vx);
00229   //   *objective_function_value::pobjfun=0.0;
00230   //   userfunction();
00231   //   vf+=*objective_function_value::pobjfun;
00232   //   send_int_to_master(67);
00233   //   send_f_to_master(vf);
00234   //   slave_gradcalc();
00235   // }
00236   //
00237   // void function_minimizer::pvm_slave_function_evaluation_noder(void)
00238   // {
00239   //   dvariable vf=0.0;
00240   //   dvar_vector vx=get_x_from_master();
00241   //   vf=initial_params::reset(vx);
00242   //   *objective_function_value::pobjfun=0.0;
00243   //   userfunction();
00244   //   vf+=*objective_function_value::pobjfun;
00245   //   send_int_to_master(67);
00246   //   send_f_to_master(vf);
00247   // }
00248   //
00249 #endif //#if defined(USE_LAPLACE)