ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2lp9.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include <admodel.h>
00012 #include <df1b2fun.h>
00013 #include <adrndeff.h>
00014 
00015 static int no_stuff=0;
00016 /*
00017 static void print(double ff,dvector& uuu,dvector& gg)
00018 {
00019   cout << setprecision(10) << setw(19) << ff << " "
00020        << setw(19) << uuu   << "  "  << setw(19) << gg << endl;
00021 }
00022 */
00023 
00024 typedef fmm* pfmm;
00025 
00030 dvector laplace_approximation_calculator::get_uhat_quasi_newton_block_diagonal
00031   (const dvector& x,function_minimizer * pfmin)
00032 {
00033   if (separable_function_difference)
00034   {
00035     delete separable_function_difference;
00036     separable_function_difference=0;
00037   }
00038   separable_function_difference = new dvector(1,num_separable_calls);
00039 
00040   fmm** pfmc1 = new pfmm[num_separable_calls];
00041   pfmc1--;
00042   ivector ishape(1,num_separable_calls);
00043   dvector gmax(1,num_separable_calls);
00044   gmax.initialize();
00045 
00046   for (int i=1;i<=num_separable_calls;i++)
00047   {
00048     int m=(*derindex)(i).indexmax();
00049     ishape(i)=m;
00050     if (m>0)
00051     {
00052     pfmc1[i] = new fmm(m);
00053     pfmc1[i]->iprint=0;
00054     pfmc1[i]->crit=inner_crit;
00055     pfmc1[i]->ireturn=0;
00056     pfmc1[i]->itn=0;
00057     pfmc1[i]->ifn=0;
00058     pfmc1[i]->ialph=0;
00059     pfmc1[i]->ihang=0;
00060     pfmc1[i]->ihflag=0;
00061     pfmc1[i]->maxfn=100;
00062     pfmc1[i]->gmax=1.e+100;
00063     pfmc1[i]->use_control_c=0;
00064     }
00065     else
00066     {
00067       pfmc1[i]= (fmm *)(0);
00068     }
00069   }
00070   dmatrix gg(1,num_separable_calls,1,ishape);
00071   dmatrix ggb(1,num_separable_calls,1,ishape);
00072   dmatrix uu(1,num_separable_calls,1,ishape);
00073   dmatrix uub(1,num_separable_calls,1,ishape);
00074   dvector ff(1,num_separable_calls);
00075   dvector ffb(1,num_separable_calls);
00076   ivector icon(1,num_separable_calls);
00077   icon.initialize();
00078   ffb=1.e+100;
00079 
00080   double f=0.0;
00081   double fb=1.e+100;
00082   dvector g(1,usize);
00083   dvector ub(1,usize);
00084   independent_variables u(1,usize);
00085   gradcalc(0,g);
00086   fmc1.itn=0;
00087   fmc1.ifn=0;
00088   fmc1.ireturn=0;
00089   initial_params::xinit(u);    // get the initial values into the
00090   fmc1.ialph=0;
00091   fmc1.ihang=0;
00092   fmc1.ihflag=0;
00093 
00094   if (init_switch)
00095   {
00096     u.initialize();
00097   }
00098 
00099   for (int ii=1;ii<=2;ii++)
00100   {
00101     // get the initial u into the uu's
00102     for (int i=1;i<=num_separable_calls;i++)
00103     {
00104       int m=(*derindex)(i).indexmax();
00105       for (int j=1;j<=m;j++)
00106       {
00107         uu(i,j)=u((*derindex)(i)(j));
00108       }
00109     }
00110 
00111 #ifdef DIAG
00112     bool loop_flag = false;
00113     int loop_counter = 0;
00114 #endif
00115 
00116     fmc1.dfn=1.e-2;
00117     dvariable pen=0.0;
00118     int converged=0;
00119     int initrun_flag=1;
00120     while (converged==0)
00121     {
00122 #ifdef DIAG
00123       if (loop_flag) loop_counter++;
00124       if (loop_counter > 18)
00125       {
00126         cout << loop_counter;
00127       }
00128 #endif
00129       if (!initrun_flag)
00130       {
00131         converged=1;
00132       }
00133       for (int i=1;i<=num_separable_calls;i++)
00134       {
00135         if (ishape(i)>0) //check to see if there are any active randoem effects
00136         {               // in this function call
00137           if (!icon(i))
00138           {
00139             independent_variables& uuu=*(independent_variables*)(&(uu(i)));
00140             (pfmc1[i])->fmin(ff[i],uuu,gg(i));
00141             gmax(i)=fabs(pfmc1[i]->gmax);
00142             if (!initrun_flag)
00143             {
00144               if (gmax(i)<1.e-4  || pfmc1[i]->ireturn<=0)
00145               {
00146                 icon(i)=1;
00147               }
00148               else
00149               {
00150                 converged=0;
00151               }
00152             }
00153           }
00154         }
00155       }
00156       initrun_flag=0;
00157       for (int i2=1;i2<=num_separable_calls;i2++)
00158       {
00159         int m=(*derindex)(i2).indexmax();
00160         for (int j=1;j<=m;j++)
00161         {
00162           u((*derindex)(i2)(j))=uu(i2,j);
00163         }
00164       }
00165       // put the
00166       //if (fmc1.ireturn>0)
00167       {
00168         dvariable vf=0.0;
00169         pen=initial_params::reset(dvar_vector(u));
00170         *objective_function_value::pobjfun=0.0;
00171 
00172         //num_separable_calls=0;
00173 
00174         pmin->inner_opt_flag=1;
00175         pfmin->AD_uf_inner();
00176         pmin->inner_opt_flag=0;
00177 
00178         if (saddlepointflag)
00179         {
00180           *objective_function_value::pobjfun*=-1.0;
00181         }
00182         if ( no_stuff==0 && quadratic_prior::get_num_quadratic_prior()>0)
00183         {
00184           quadratic_prior::get_M_calculations();
00185         }
00186         vf+=*objective_function_value::pobjfun;
00187 
00188         objective_function_value::fun_without_pen=value(vf);
00189         vf+=pen;
00190 
00191         gradcalc(usize,g);
00192         for (int i=1;i<=num_separable_calls;i++)
00193         {
00194           int m=(*derindex)(i).indexmax();
00195           for (int j=1;j<=m;j++)
00196           {
00197             gg(i,j)=g((*derindex)(i)(j));
00198           }
00199         }
00200         {
00201           ofstream ofs("l:/temp1.dat");
00202           ofs << g.indexmax() << " " << setprecision(15) << g << endl;
00203         }
00204         if (saddlepointflag==2)
00205         {
00206           ff[1]=-(*separable_function_difference)(1);
00207           for (int i=2;i<=num_separable_calls;i++)
00208           {
00209             ff[i]=-(*separable_function_difference)(i);
00210             //ff[i]=-(*separable_function_difference)(i)
00211              // +(*separable_function_difference)(i-1);
00212 
00213             if (ff[i] < ffb[i])
00214             {
00215               ffb[i]=ff[i];
00216               uub[i]=uu[i];
00217               ggb[i]=gg[i];
00218             }
00219           }
00220         }
00221         else
00222         {
00223           ff[1]=(*separable_function_difference)(1);
00224           for (int i=2;i<=num_separable_calls;i++)
00225           {
00226             ff[i]=(*separable_function_difference)(i);
00227             //ff[i]=(*separable_function_difference)(i)
00228              // -(*separable_function_difference)(i-1);
00229 
00230             if (ff[i] < ffb[i])
00231             {
00232               ffb[i]=ff[i];
00233               uub[i]=uu[i];
00234               ggb[i]=gg[i];
00235             }
00236           }
00237         }
00238         f=0.0;
00239         for (int i2=1;i2<=num_separable_calls;i2++)
00240         {
00241           f+=ff[i2];
00242         }
00243         if (f<fb)
00244         {
00245           fb=f;
00246           ub=u;
00247         }
00248       }
00249       u=ub;
00250     }
00251     double tmax=max(gmax);
00252     cout <<  " inner maxg = " << tmax << endl;
00253 
00254     if (tmax< 1.e-4) break;
00255   }
00256   fmc1.ireturn=0;
00257   fmc1.fbest=fb;
00258   gradient_structure::set_NO_DERIVATIVES();
00259   //num_separable_calls=0;
00260   pmin->inner_opt_flag=1;
00261   pfmin->AD_uf_inner();
00262   pmin->inner_opt_flag=0;
00263   if ( no_stuff==0 && quadratic_prior::get_num_quadratic_prior()>0)
00264   {
00265     quadratic_prior::get_M_calculations();
00266   }
00267   gradient_structure::set_YES_DERIVATIVES();
00268   for (int i=1;i<=num_separable_calls;i++)
00269   {
00270     if (pfmc1[i])
00271     {
00272       delete pfmc1[i];
00273     }
00274   }
00275   pfmc1++;
00276   delete [] pfmc1;
00277   pfmc1 = 0;
00278   return u;
00279 }