ADMB Documentation  11.1.1635
 All Classes Files Functions Variables Typedefs Friends Defines
profmin.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: profmin.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 
00009 void fooledit2(void){;}
00010 
00011 void fooledit(void)
00012 {
00013   fooledit2();
00014 }
00015 
00016 #if defined(USE_ADPVM)
00017 int check_pvm_message(int i,int j)
00018 {
00019   int arrived=0;
00020  /*
00021   int mestag=-1;
00022   int arrived=pvm_probe(ad_comm::pvm_manager->id(i,j),mestag);
00023   if (arrived) fooledit();
00024  */
00025   return arrived;
00026 }
00027 
00028 void function_minimizer::pvm_master_function_evaluation_profile(double& f,
00029   independent_variables& x,const dvector & _g,int nvar,int iprof,double weight,
00030   double new_value)
00031 {
00032   dvector & g=(dvector &) _g;
00033 
00034   grad_chk();
00035   dvariable vf=0.0;
00036   dvar_vector vx=dvar_vector(x);
00037   vf=initial_params::reset(vx);
00038   send_x_to_slaves(vx);
00039   *objective_function_value::pobjfun=0.0;
00040   userfunction();
00041   dvariable tv=likeprof_params::likeprofptr[iprof]->variable();
00042   vf+=weight*square(new_value-tv);
00043   vf+=*objective_function_value::pobjfun;
00044   imatrix im=get_int_from_slaves();
00045   if (im(1,1)!=67)
00046   {
00047     cout << "Error in im" << endl;
00048     ad_exit(1);
00049   }
00050 
00051   dvar_matrix fslave=get_f_from_slaves();
00052   vf+=sum(fslave);
00053   f=value(vf);
00054   gradcalc(nvar,g);
00055   int arrived=check_pvm_message(1,1);
00056   if (arrived)
00057     cout << arrived << endl;
00058   // !!! dave
00059   grad_chk();
00060 }
00061 
00062 
00063   void function_minimizer::pvm_master_prof_minimize(int iprof, double sigma,
00064     double new_value, const double& _fprof,const int underflow_flag,
00065     double global_min, const double& _penalties,
00066     const double& _final_weight)
00067   {
00068     double& penalties=(double&) _penalties;
00069     double& fprof=(double&) _fprof;
00070     double& final_weight=(double&) _final_weight;
00071     int arrived=check_pvm_message(1,1);
00072    send_int_to_slaves(underflow_flag);
00073    if (!underflow_flag)
00074    {
00075     //int max_profile_phases=1;
00076     int max_profile_phases=3;
00077     int profile_phase=1;
00078     initial_params::current_phase = initial_params::max_number_phases;
00079     while (profile_phase <= max_profile_phases)
00080     {
00081      {
00082       send_int_to_slaves(5);
00083       // get the number of active parameters
00084       int nvar=initial_params::nvarcalc();
00085       dvector g(1,nvar);
00086       independent_variables x(1,nvar);
00087       // get the initial values into the x vector
00088       initial_params::xinit(x);
00089       fmm fmc(nvar);
00090       fmc.maxfn= maxfn;
00091       fmc.iprint= iprint;
00092       fmc.crit = crit;
00093       fmc.imax = imax;
00094       fmc.dfn= dfn;
00095       fmc.scroll_flag= scroll_flag;
00096       fmc.min_improve=min_improve;
00097       double f=0.0;
00098       // set convergence criterion for this phase
00099       if (!(!convergence_criteria))
00100       {
00101         int ind=min(convergence_criteria.indexmax(),
00102           initial_params::current_phase);
00103         fmc.crit=convergence_criteria(ind);
00104       }
00105       if (!(!maximum_function_evaluations))
00106       {
00107         int ind=min(maximum_function_evaluations.indexmax(),
00108           initial_params::current_phase);
00109         fmc.maxfn=int(maximum_function_evaluations(ind));
00110       }
00111       int itnsave=0;
00112       //double weight=pow(50.0,profile_phase)/(sigma*sigma);
00113       double weight;
00114       if (sigma)
00115       {
00116         weight=pow(120.0,profile_phase)/(sigma*sigma);
00117       }
00118       else
00119       {
00120         weight=pow(120.0,profile_phase);
00121       }
00122       final_weight=weight;
00123       while (fmc.ireturn>=0)
00124       {
00125         fmc.fmin(f,x,g);
00126         double oldvalue =
00127           value(likeprof_params::likeprofptr[iprof]->variable());
00128         double diff = new_value-oldvalue;
00129         if (fmc.itn>itnsave && diff < pow(.1,iprof)*sigma)
00130         {
00131           fmc.ifn=fmc.imax;
00132         }
00133         check_pvm_message(1,1);
00134         send_int_to_slaves(fmc.ireturn);
00135         if (fmc.ireturn>0)
00136         {
00137           check_pvm_message(1,1);
00138           gradient_structure::set_YES_DERIVATIVES();
00139           pvm_master_function_evaluation_profile(f,x,g,nvar,iprof,weight,
00140             new_value);
00141           gradient_structure::set_NO_DERIVATIVES();
00142           check_pvm_message(1,1);
00143         }
00144       }
00145 
00146       gradient_structure::set_NO_DERIVATIVES();
00147       check_pvm_message(1,1);
00148 
00149       int break_flag=38;
00150       send_int_to_slaves(break_flag);
00151       iexit=fmc.iexit;
00152       ihflag=fmc.ihflag;
00153       ihang=fmc.ihang;
00154       maxfn_flag=fmc.maxfn_flag;
00155       quit_flag=fmc.quit_flag;
00156       dvar_vector vx=dvar_vector(x);
00157       fprof=value(initial_params::reset(vx));
00158       //send_int_to_slaves(1);
00159       send_x_to_slaves(vx);
00160       *objective_function_value::pobjfun=0.0;
00161       userfunction();
00162       imatrix im2=get_int_from_slaves();
00163       if (im2(1,1)!=67)
00164       {
00165         cout << "Error in im" << endl;
00166         ad_exit(1);
00167       }
00168 
00169       dvar_matrix fslave=get_f_from_slaves();
00170       double tv=value(likeprof_params::likeprofptr[iprof]->variable());
00171       fprof+=value(*objective_function_value::pobjfun);
00172       fprof+=value(sum(fslave));
00173       penalties=weight*(new_value-tv)*(new_value-tv);
00174       fprof+=penalties;
00175 
00176       //send_int_to_slaves(0);
00177 
00178       if (quit_flag=='Q') break;
00179       if (!quit_flag || quit_flag == 'N')
00180       {
00181         profile_phase++;
00182       }
00183      }
00184     }
00185     send_int_to_slaves(0);
00186    }
00187    else
00188    {
00189      fprof=global_min+20.0;
00190    }
00191   }
00192 
00193   void function_minimizer::pvm_slave_prof_minimize(int underflow_flag)
00194   {
00195    if (!underflow_flag)
00196    {
00197     int max_profile_phases=3;
00198     int profile_phase=1;
00199     initial_params::current_phase = initial_params::max_number_phases;
00200     do
00201     {
00202       int prof_flag=get_int_from_master();
00203       if (prof_flag && prof_flag !=5)
00204       {
00205         cerr << "Error in prof_flag " << prof_flag << endl;
00206         ad_exit(1);
00207       }
00208       grad_chk();
00209       if (!prof_flag) break;
00210       {
00211         gradient_structure::set_YES_DERIVATIVES();
00212         do
00213         {
00214           int ireturn=get_int_from_master();
00215           grad_chk();
00216           if (ireturn && ireturn ==5)
00217           {
00218             cerr << "Error in ireturn " << ireturn << endl;
00219             ad_exit(1);
00220           }
00221           if (ireturn<=0) break;
00222             pvm_slave_function_evaluation();
00223           grad_chk();
00224         }
00225         while(1);
00226 
00227         int break_flag=get_int_from_master();
00228         if (break_flag)
00229         {
00230           cout << break_flag << endl;
00231         }
00232         gradient_structure::set_NO_DERIVATIVES();
00233 
00234         pvm_slave_function_evaluation_noder();
00235 
00236       }
00237     }
00238     while(1);
00239    }
00240   }
00241 #endif