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