ADMB Documentation  11.1.1632
 All Classes Files Functions Variables Typedefs Friends Defines
para3.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: para3.cpp 1594 2014-01-31 21:27:41Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00007 #include <admodel.h>
00008 //#include <iomanip.h>
00009 #ifdef __GNUDOS__
00010   #include <gccmanip.h>
00011 #endif
00012 
00013 void hess_calcreport(int i,int nvar);
00014 void hess_errorreport(void);
00015 void set_labels_for_hess(int);
00016 
00017 class admb_javapointers;
00018 extern admb_javapointers * adjm_ptr;
00019 void useless(const double& sdelta2);
00020 // estimate the matrix of second derivatives
00021 void ad_update_hess_stats_report(int i,int nvar);
00022 
00023 #if defined(USE_ADPVM)
00024 void function_minimizer::hess_routine_master()
00025 {
00026 
00027   int nvar=initial_params::nvarcalc(); // get the number of active parameters
00028   //if (adjm_ptr) set_labels_for_hess(nvar);
00029   independent_variables x(1,nvar);
00030   initial_params::xinit(x);        // get the initial values into the x vector
00031   double f;
00032   double delta=1.e-6;
00033   dvector g1(1,nvar);
00034   dvector g2(1,nvar);
00035   dvector gbest(1,nvar);
00036   dvector hess(1,nvar);
00037   dvector hess1(1,nvar);
00038   dvector hess2(1,nvar);
00039   double eps=.1;
00040   gradient_structure::set_YES_DERIVATIVES();
00041   gbest.fill_seqadd(1.e+50,0.);
00042 
00043   adstring tmpstring="admodel.hes";
00044   if (ad_comm::wd_flag)
00045      tmpstring = ad_comm::adprogram_name + ".hes";
00046   uostream ofs((char*)tmpstring);
00047 
00048   ofs << nvar;
00049   {
00050     pvm_master_function_evaluation(f,x,g1,nvar);
00051     double sdelta1;
00052     double sdelta2;
00053     for (int i=1;i<=nvar;i++)
00054     {
00055       hess_calcreport(i,nvar);
00056 
00057       double f=0.0;
00058       double xsave=x(i);
00059       sdelta1=x(i)+delta;
00060       useless(sdelta1);
00061       sdelta1-=x(i);
00062       x(i)=xsave+sdelta1;
00063 
00064       pvm_master_function_evaluation(f,x,g1,nvar);
00065 
00066       sdelta2=x(i)-delta;
00067       useless(sdelta2);
00068       sdelta2-=x(i);
00069       x(i)=xsave+sdelta2;
00070 
00071       pvm_master_function_evaluation(f,x,g2,nvar);
00072 
00073       x(i)=xsave;
00074       hess1=(g1-g2)/(sdelta1-sdelta2);
00075 
00076       sdelta1=x(i)+eps*delta;
00077       useless(sdelta1);
00078       sdelta1-=x(i);
00079       x(i)=xsave+sdelta1;
00080 
00081       pvm_master_function_evaluation(f,x,g1,nvar);
00082 
00083       x(i)=xsave-eps*delta;
00084       sdelta2=x(i)-eps*delta;
00085       useless(sdelta2);
00086       sdelta2-=x(i);
00087       x(i)=xsave+sdelta2;
00088 
00089       pvm_master_function_evaluation(f,x,g2,nvar);
00090 
00091       x(i)=xsave;
00092 
00093       dvariable vf=initial_params::reset(dvar_vector(x));
00094       double eps2=eps*eps;
00095       hess2=(g1-g2)/(sdelta1-sdelta2);
00096       hess=(eps2*hess1-hess2) /(eps2-1.);
00097 
00098       ofs << hess;
00099       //if (adjm_ptr) ad_update_hess_stats_report(nvar,i);
00100     }
00101   }
00102   gradient_structure::set_NO_DERIVATIVES();
00103 }
00104 
00105 void function_minimizer::hess_routine_slave()
00106 {
00107   int nvar=initial_params::nvarcalc(); // get the number of active parameters
00108   gradient_structure::set_YES_DERIVATIVES();
00109 
00110   pvm_slave_function_evaluation();
00111   for (int i=1;i<=nvar;i++)
00112   {
00113     for (int it=1;it<=4;it++) pvm_slave_function_evaluation();
00114   }
00115   gradient_structure::set_NO_DERIVATIVES();
00116 }
00117 #endif // #if defined(USE_ADPVM)