ADMB Documentation  11.1.1632
 All Classes Files Functions Variables Typedefs Friends Defines
paral3re.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: paral3re.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 #if defined(USE_LAPLACE)
00008 #include <df1b2fun.h>
00009 #include <adrndeff.h>
00010 #include <admodel.h>
00011 
00012 void function_minimizer::quasi_newton_block_pvm_master_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     dvariable xf=initial_params::reset(dvar_vector(x));
00108     reset_gradient_stack();
00109 
00110     gradcalc(0,g);
00111     while (fmc.ireturn>=0)
00112     {
00113       fmc.fmin(f,x,g);
00114       if (fmc.ireturn>0)
00115       {
00116         dvariable vf=0.0;
00117         vf=initial_params::reset(dvar_vector(x));
00118         *objective_function_value::pobjfun=0.0;
00119         userfunction();
00120         vf+=*objective_function_value::pobjfun;
00121         f=value(vf);
00122         gradcalc(nvar,g);
00123       }
00124     }
00125   }
00126   else
00127   {
00128 
00129     // calculate the number of random effects unvar
00130     // this turns on random effects variables and turns off
00131     // everything else
00132     //cout << nvar << endl;
00133     initial_params::set_active_only_random_effects();
00134     //cout << nvar << endl;
00135     int unvar=initial_params::nvarcalc(); // get the number of active
00136     //df1b2_gradlist::set_no_derivatives();
00137 
00138     if (lapprox)
00139     {
00140       delete lapprox;
00141       lapprox=0;
00142       df1b2variable::pool->deallocate();
00143 
00144       for (int i=0;i<df1b2variable::adpool_counter;i++)
00145       {
00146         delete df1b2variable::adpool_vector[i];
00147         df1b2variable::adpool_vector[i]=0;
00148         df1b2variable::nvar_vector[i]=0;
00149         df1b2variable::adpool_counter=0;
00150       }
00151     }
00152     lapprox=new laplace_approximation_calculator(nvar,unvar,1,nvar+unvar,
00153       this);
00154     initial_df1b2params::current_phase=initial_params::current_phase;
00155 
00156     initial_df1b2params::save_varsptr();
00157     allocate();
00158     initial_df1b2params::restore_varsptr();
00159 
00160     df1b2_gradlist::set_no_derivatives();
00161     int nvar=initial_params::nvarcalc_all();
00162     dvector y(1,nvar);
00163     initial_params::xinit_all(y);
00164     initial_df1b2params::reset_all(y);
00165 
00166     //vmon_begin();
00167     while (fmc.ireturn>=0)
00168     {
00169       fmc.fmin(f,x,g);
00170       send_int_to_slaves(fmc.ireturn);
00171       if (fmc.ireturn>0)
00172       {
00173         //int arrive=check_pvm_message(1,1);
00174         //if (arrive)
00175         //  cout << arrive << endl;
00176         g=(*lapprox)(x,f,this);
00177         //g=lapprox->default_calculations_parallel_master(x,f,this);
00178         //arrive=check_pvm_message(1,1);
00179         //if (arrive)
00180         //  cout << arrive << endl;
00181       }
00182       //cout << setprecision(16) << f << " " << x << " " << g << endl;
00183     }
00184     //vmon_done();
00185     // turn off the random effects
00186     initial_params::set_inactive_only_random_effects();
00187     //df1b2variable::pool.deallocate();
00188 
00189   }
00190 
00191 
00192 
00193 
00194   gradient_structure::set_NO_DERIVATIVES();
00195   ffbest=fmc.fbest;
00196   iexit=fmc.iexit;
00197   ifn=fmc.ifn;
00198   ihflag=fmc.ihflag;
00199   ihang=fmc.ihang;
00200   maxfn_flag=fmc.maxfn_flag;
00201   quit_flag=fmc.quit_flag;
00202   objective_function_value::gmax=fabs(fmc.gmax);
00203 } // end block for quasi newton minimization
00204 
00205 /*
00206 void function_minimizer::pvm_master_function_evaluation_random_effects
00207   (double& f,independent_variables& x,const dvector & _g,int nvar)
00208 {
00209   dvector& g=(dvector&) _g;
00210   g=lapprox->function_default_calculations_parallel(x,f,this);
00211 }
00212 */
00213   // void function_minimizer::function_evaluation_block_pvm_slave(void)
00214   // {
00215   //   gradient_structure::set_YES_DERIVATIVES();
00216   //   reset_gradient_stack();
00217   //   //gradcalc(nvar,g);
00218   //   do
00219   //   {
00220   //     int ireturn=get_int_from_master();
00221   //     if (ireturn<0) break;
00222   //     pvm_slave_function_evaluation();
00223   //   }
00224   //   while(1);
00225   //   gradient_structure::set_NO_DERIVATIVES();
00226   // }
00227   //
00228   // void function_minimizer::pvm_slave_function_evaluation(void)
00229   // {
00230   //   dvariable vf=0.0;
00231   //   dvar_vector vx=get_x_from_master();
00232   //   vf=initial_params::reset(vx);
00233   //   *objective_function_value::pobjfun=0.0;
00234   //   userfunction();
00235   //   vf+=*objective_function_value::pobjfun;
00236   //   send_int_to_master(67);
00237   //   send_f_to_master(vf);
00238   //   slave_gradcalc();
00239   // }
00240   //
00241   // void function_minimizer::pvm_slave_function_evaluation_noder(void)
00242   // {
00243   //   dvariable vf=0.0;
00244   //   dvar_vector vx=get_x_from_master();
00245   //   vf=initial_params::reset(vx);
00246   //   *objective_function_value::pobjfun=0.0;
00247   //   userfunction();
00248   //   vf+=*objective_function_value::pobjfun;
00249   //   send_int_to_master(67);
00250   //   send_f_to_master(vf);
00251   // }
00252   //
00253 #endif // #if defined(USE_LAPLACE)