ADMB Documentation  11.1.1890
 All Classes Files Functions Variables Typedefs Friends Defines
mod_mc2.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: mod_mc2.cpp 1827 2014-04-03 20:24:43Z 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 #endif
00011 #include <admodel.h>
00012 
00013 double better_rand(long int&);
00014 
00015 double function_minimizer::get_monte_carlo_value(int nvar,
00016   const independent_variables& x)
00017 {
00018   //initial_params::xinit(x);
00019   double f=0.0;
00020 #if defined(USE_LAPLACE)
00021   if (mcmc2_flag==0 && lapprox)
00022   {
00023     dvector g=(*lapprox)(x,f,this);
00024   }
00025   else
00026   {
00027 #endif
00028     dvariable vf=0.0;
00029     vf=initial_params::reset(dvar_vector(x));
00030     *objective_function_value::pobjfun=0.0;
00031     userfunction();
00032     vf+=*objective_function_value::pobjfun;
00033     f=value(vf);
00034 #if defined(USE_LAPLACE)
00035   }
00036 #endif
00037   return f;
00038 }
00039 
00040 #if !defined(USE_ADPVM)
00041 double function_minimizer::pvm_master_get_monte_carlo_value(int nvar,
00042   const dvector& x){return 0.0;}
00043 #else
00044 double function_minimizer::pvm_master_get_monte_carlo_value(int nvar,
00045   const dvector& x)
00046 {
00047   double f=0.0;
00048   {
00049     dvariable vf=0.0;
00050     dvar_vector vx=dvar_vector(x);
00051     vf=initial_params::reset(vx);
00052     send_x_to_slaves(vx);
00053     *objective_function_value::pobjfun=0.0;
00054     userfunction();
00055     vf+=*objective_function_value::pobjfun;
00056     dvar_matrix fslave=get_f_from_slaves();
00057     vf+=sum(fslave);
00058     f=value(vf);
00059   }
00060   return f;
00061 }
00062 #endif
00063 
00064 #if !defined(USE_ADPVM)
00065 void function_minimizer::pvm_slave_get_monte_carlo_value(int nvar){}
00066 #else
00067 void function_minimizer::pvm_slave_get_monte_carlo_value(int nvar)
00068 {
00069   double f=0.0;
00070   {
00071     dvariable vf=0.0;
00072     dvar_vector vx=get_x_from_master();
00073     vf=initial_params::reset(vx);
00074     *objective_function_value::pobjfun=0.0;
00075     userfunction();
00076     vf+=*objective_function_value::pobjfun;
00077     send_int_to_master(67);
00078     send_f_to_master(vf);
00079   }
00080 }
00081 #endif
00082 
00083 double function_minimizer::get_monte_carlo_value(int nvar,
00084   const independent_variables& x,dvector& g)
00085 {
00086   //initial_params::xinit(x);
00087   double f=0.0;
00088 #if defined(USE_LAPLACE)
00089   if (mcmc2_flag==0 && lapprox)
00090   {
00091     g=(*lapprox)(x,f,this);
00092   }
00093   else
00094   {
00095 #endif
00096     dvariable vf=0.0;
00097     vf=initial_params::reset(dvar_vector(x));
00098     *objective_function_value::pobjfun=0.0;
00099     userfunction();
00100     vf+=*objective_function_value::pobjfun;
00101     f=value(vf);
00102     gradcalc(nvar,g);
00103 #if defined(USE_LAPLACE)
00104   }
00105 #endif
00106   return f;
00107 }