diff --git a/inst/include/interface/rcpp/rcpp_interface.hpp b/inst/include/interface/rcpp/rcpp_interface.hpp index 06690d81..ae3e7887 100644 --- a/inst/include/interface/rcpp/rcpp_interface.hpp +++ b/inst/include/interface/rcpp/rcpp_interface.hpp @@ -95,16 +95,24 @@ void clear_internal() { void clear() { // rcpp_interface_base.hpp FIMSRcppInterfaceBase::fims_interface_objects.clear(); + // rcpp_data.hpp - - DataInterface::id_g = 1; - DataInterface::fims_interface_objects.clear(); + DataInterfaceBase::id_g = 1; + DataInterfaceBase::live_objects.clear(); + AgeCompDataInterface::id_g = 1; + AgeCompDataInterface::live_objects.clear(); + IndexDataInterface::id_g = 1; + IndexDataInterface::live_objects.clear(); // rcpp_fleets.hpp + FleetInterfaceBase::id_g = 1; + FleetInterfaceBase::live_objects.clear(); + FleetInterface::id_g = 1; - FleetInterface::fims_interface_objects.clear(); + FleetInterface::live_objects.clear(); + // rcpp_growth.hpp GrowthInterfaceBase::id_g = 1; GrowthInterfaceBase::live_objects.clear(); @@ -114,14 +122,14 @@ void clear() { // rcpp_maturity.hpp MaturityInterfaceBase::id_g = 1; - MaturityInterfaceBase::maturity_objects.clear(); + MaturityInterfaceBase::live_objects.clear(); LogisticMaturityInterface::id_g = 1; - LogisticMaturityInterface::maturity_objects.clear(); + LogisticMaturityInterface::live_objects.clear(); // rcpp_population.hpp - // PopulationInterfaceBase::id_g = 1; - // PopulationInterfaceBase::live_objects.clear(); + PopulationInterfaceBase::id_g = 1; + PopulationInterfaceBase::live_objects.clear(); PopulationInterface::id_g = 1; PopulationInterface::live_objects.clear(); @@ -135,13 +143,13 @@ void clear() { // rcpp_selectivity.hpp SelectivityInterfaceBase::id_g = 1; - SelectivityInterfaceBase::selectivity_objects.clear(); + SelectivityInterfaceBase::live_objects.clear(); LogisticSelectivityInterface::id_g = 1; - LogisticSelectivityInterface::selectivity_objects.clear(); + LogisticSelectivityInterface::live_objects.clear(); DoubleLogisticSelectivityInterface::id_g = 1; - DoubleLogisticSelectivityInterface::selectivity_objects.clear(); + DoubleLogisticSelectivityInterface::live_objects.clear(); // rcpp_tmb_distribution.hpp DistributionsInterfaceBase::id_g = 1; @@ -156,7 +164,6 @@ void clear() { DmultinomDistributionsInterface::id_g = 1; DmultinomDistributionsInterface::live_objects.clear(); - FIMSRcppInterfaceBase::fims_interface_objects.clear(); clear_internal(); clear_internal(); clear_internal(); @@ -211,11 +218,6 @@ RCPP_MODULE(fims) { .method("SetObservedIndexData", &FleetInterface::SetObservedIndexData) .method("SetSelectivity", &FleetInterface::SetSelectivity); - Rcpp::class_("Data") - .constructor() - .field("observed_data", &DataInterface::observed_data) - .method("get_id", &DataInterface::get_id); - Rcpp::class_("AgeComp") .constructor() .field("age_comp_data", &AgeCompDataInterface::age_comp_data) diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_data.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_data.hpp index 0c5ab585..10d57e99 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_data.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_data.hpp @@ -17,122 +17,115 @@ * fleet <- new(fims$Data) * */ -class DataInterface : public FIMSRcppInterfaceBase { - public: - Rcpp::NumericVector observed_data; /*!< The data */ - static uint32_t id_g; /**< static id of the DataInterface object */ - uint32_t id; /**< local id of the DataInterface object */ - static std::map - live_objects; /**< map associating the ids of DataInterface to +class DataInterfaceBase : public FIMSRcppInterfaceBase { +public: + Rcpp::NumericVector observed_data; /*!< The data */ + static uint32_t id_g; /**< static id of the DataInterfaceBase object */ + uint32_t id; /**< local id of the DataInterfaceBase object */ + //live objects in C++ are objects that have been created and live in memory + static std::map + live_objects; /**< map associating the ids of DataInterfaceBase to the objects */ - /** @brief constructor - */ - DataInterface() { - this->id = DataInterface::id_g++; - DataInterface::live_objects[this->id] = this; - FIMSRcppInterfaceBase::fims_interface_objects.push_back(this); - } - - /** @brief destructor - */ - virtual ~DataInterface() {} - - /** @brief get the ID of the interface base object - **/ - virtual uint32_t get_id() { return this->id; } - - /**@brief add_to_fims_tmb dummy method - * - */ - virtual bool add_to_fims_tmb() { return true; }; + /** @brief constructor + */ + DataInterfaceBase() { + this->id = DataInterfaceBase::id_g++; + //Create instance of map: key is id and value is pointer to DataInterfaceBase + DataInterfaceBase::live_objects[this->id] = this; + FIMSRcppInterfaceBase::fims_interface_objects.push_back(this); + } + + /** @brief destructor + */ + virtual ~DataInterfaceBase() { + } + + /** @brief get the ID of the interface base object + **/ + virtual uint32_t get_id() { + return this->id; + } + + /**@brief add_to_fims_tmb dummy method + * + */ + virtual bool add_to_fims_tmb() { + return true; + }; }; -uint32_t DataInterface::id_g = 1; -std::map DataInterface::live_objects; +uint32_t DataInterfaceBase::id_g = 1; +std::map DataInterfaceBase::live_objects; /** * @brief Rcpp interface for age comp data as an S4 object. To instantiate * from R: * acomp <- new(fims$AgeComp) */ -class AgeCompDataInterface : public DataInterface { - public: - int amax; /*!< first dimension of the data */ - int ymax; /*!< second dimension of the data */ - Rcpp::NumericVector age_comp_data; /*!amax = amax; - this->ymax = ymax; - } - - /** - * @brief destructor - */ - virtual ~AgeCompDataInterface() {} - - /** @brief get the ID of the interface base object - **/ - virtual uint32_t get_id() { return this->id; } - - /** - * @brief adds parameters to the model - */ - virtual bool add_to_fims_tmb() { - std::shared_ptr> age_comp_data_0 = - std::make_shared>(this->ymax, - this->amax); - std::shared_ptr> age_comp_data_1 = - std::make_shared>(this->ymax, - this->amax); - std::shared_ptr> age_comp_data_2 = - std::make_shared>(this->ymax, - this->amax); - std::shared_ptr> age_comp_data_3 = - std::make_shared>(this->ymax, - this->amax); - - age_comp_data_0->id = this->id; - - age_comp_data_1->id = this->id; - - age_comp_data_2->id = this->id; - - age_comp_data_3->id = this->id; - - for (int y = 0; y < ymax; y++) { - for (int a = 0; a < amax; a++) { - int index_ya = y * amax + a; - age_comp_data_0->at(y, a) = this->age_comp_data[index_ya]; - age_comp_data_1->at(y, a) = this->age_comp_data[index_ya]; - age_comp_data_2->at(y, a) = this->age_comp_data[index_ya]; - age_comp_data_3->at(y, a) = this->age_comp_data[index_ya]; - } +class AgeCompDataInterface : public DataInterfaceBase { +public: + int amax; /*!< first dimension of the data */ + int ymax; /*!< second dimension of the data */ + Rcpp::NumericVector age_comp_data; /*!amax = amax; + this->ymax = ymax; + } + + /** + * @brief destructor + */ + virtual ~AgeCompDataInterface() { + } + + /** @brief get the ID of the interface base object + **/ + virtual uint32_t get_id() { + return this->id; } - std::shared_ptr> d0 = - fims::Information::GetInstance(); - d0->data_objects[this->id] = age_comp_data_0; +#ifdef TMB_MODEL - std::shared_ptr> d1 = - fims::Information::GetInstance(); + template + bool add_to_fims_tmb_internal() { + std::shared_ptr> age_comp_data = + std::make_shared> (this->ymax, + this->amax); - d1->data_objects[this->id] = age_comp_data_1; - std::shared_ptr> d2 = - fims::Information::GetInstance(); + age_comp_data->id = this->id; + for (int y = 0; y < ymax; y++) { + for (int a = 0; a < amax; a++) { + int index_ya = y * amax + a; + age_comp_data->at(y, a) = this->age_comp_data[index_ya]; + } + } - d2->data_objects[this->id] = age_comp_data_2; - std::shared_ptr> d3 = - fims::Information::GetInstance(); + std::shared_ptr> info = + fims::Information::GetInstance(); - d3->data_objects[this->id] = age_comp_data_3; + info->data_objects[this->id] = age_comp_data; + + return true; + } + + /** + * @brief adds parameters to the model + */ + virtual bool add_to_fims_tmb() { + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + + return true; + } - return true; - } +#endif }; /** @@ -140,70 +133,65 @@ class AgeCompDataInterface : public DataInterface { * from R: * fleet <- new(fims$Index) */ -class IndexDataInterface : public DataInterface { - public: - int ymax; /*!< second dimension of the data */ - Rcpp::NumericVector index_data; /*!ymax = ymax; } - - /** - * @brief destructor - */ - virtual ~IndexDataInterface() {} - /** @brief get the ID of the interface base object - **/ - virtual uint32_t get_id() { return this->id; } - - /** - *@brief function to add to TMB - */ - virtual bool add_to_fims_tmb() { - std::shared_ptr> index_data_0 = - std::make_shared>(this->ymax); - std::shared_ptr> index_data_1 = - std::make_shared>(this->ymax); - std::shared_ptr> index_data_2 = - std::make_shared>(this->ymax); - std::shared_ptr> index_data_3 = - std::make_shared>(this->ymax); - index_data_0->id = this->id; - - index_data_1->id = this->id; - - index_data_2->id = this->id; - - index_data_3->id = this->id; - - for (int y = 0; y < ymax; y++) { - index_data_0->at(y) = this->index_data[y]; - index_data_1->at(y) = this->index_data[y]; - index_data_2->at(y) = this->index_data[y]; - index_data_3->at(y) = this->index_data[y]; +class IndexDataInterface : public DataInterfaceBase { +public: + int ymax; /*!< second dimension of the data */ + Rcpp::NumericVector index_data; /*!ymax = ymax; + } + + /** + * @brief destructor + */ + virtual ~IndexDataInterface() { } - std::shared_ptr> d0 = - fims::Information::GetInstance(); + /** @brief get the ID of the interface base object + **/ + virtual uint32_t get_id() { + return this->id; + } - d0->data_objects[this->id] = index_data_0; - std::shared_ptr> d1 = - fims::Information::GetInstance(); +#ifdef TMB_MODEL - d1->data_objects[this->id] = index_data_1; - std::shared_ptr> d2 = - fims::Information::GetInstance(); + template + bool add_to_fims_tmb_internal() { + std::shared_ptr> data = + std::make_shared> (this->ymax); - d2->data_objects[this->id] = index_data_2; - std::shared_ptr> d3 = - fims::Information::GetInstance(); + data->id = this->id; - d3->data_objects[this->id] = index_data_3; - return true; - } + for (int y = 0; y < ymax; y++) { + data->at(y) = this->index_data[y]; + } + + std::shared_ptr> info = + fims::Information::GetInstance(); + + info->data_objects[this->id] = data; + return true; + } + + /** + *@brief function to add to TMB + */ + virtual bool add_to_fims_tmb() { + + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + + return true; + } + +#endif }; #endif diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_fleet.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_fleet.hpp index afb0a804..f0df1571 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_fleet.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_fleet.hpp @@ -13,272 +13,184 @@ #include "rcpp_interface_base.hpp" /** - * @brief Rcpp interface for Fleet as an S4 object. To instantiate - * from R: - * fleet <- new(fims$Fleet) + * @brief Rcpp interface that serves as the parent class for + * Rcpp fleet interfaces. This type should be inherited and not + * called from R directly. * */ -class FleetInterface : public FIMSRcppInterfaceBase { - int agecomp_likelihood_id = -999; /*!< id of agecomp likelihood component*/ - int index_likelihood_id = -999; /*!< id of index likelihood component*/ - int observed_agecomp_data_id = -999; /*!< id of observed agecomp data object*/ - int observed_index_data_id = -999; /*!< id of observed index data object*/ - int selectivity_id = -999; /*!< id of selectivity component*/ - - public: - bool is_survey = false; /*!< whether this is a survey fleet */ - int nages; /*!< number of ages in the fleet data*/ - int nyears; /*!< number of years in the fleet data */ - double log_q; /*!< log of catchability for the fleet*/ - Rcpp::NumericVector - log_Fmort; /*!< log of fishing mortality rate for the fleet*/ - bool estimate_F = false; /*!< whether the parameter F should be estimated*/ - bool estimate_q = false; /*!< whether the parameter q should be estimated*/ - bool random_q = false; /*!< whether q should be a random effect*/ - bool random_F = false; /*!< whether F should be a random effect*/ - Parameter log_obs_error; /*!< the log of the observation error */ - - public: - static uint32_t id_g; /**< static id of the FleetInterface object */ - uint32_t id; /**< local id of the FleetInterface object */ - - FleetInterface() { - this->id = FleetInterface::id_g++; - FIMSRcppInterfaceBase::fims_interface_objects.push_back(this); - } - - virtual ~FleetInterface() {} - - /** - * @brief Set the unique id for the Age Comp Likelihood object - * - * @param agecomp_likelihood_id Unique id for the Age Comp Likelihood object - */ - void SetAgeCompLikelihood(int agecomp_likelihood_id) { - this->agecomp_likelihood_id = agecomp_likelihood_id; - } - - /** - * @brief Set the unique id for the Index Likelihood object - * - * @param index_likelihood_id Unique id for the Index Likelihood object - */ - void SetIndexLikelihood(int index_likelihood_id) { - this->index_likelihood_id = index_likelihood_id; - } - - /** - * @brief Set the unique id for the Observed Age Comp Data object - * - * @param observed_agecomp_data_id Unique id for the Observed Age Comp Data - * object - */ - void SetObservedAgeCompData(int observed_agecomp_data_id) { - this->observed_agecomp_data_id = observed_agecomp_data_id; - } - - /** - * @brief Set the unique id for the Observed Index Data object - * - * @param observed_index_data_id Unique id for the Observed Index Data object - */ - void SetObservedIndexData(int observed_index_data_id) { - this->observed_index_data_id = observed_index_data_id; - } - - /** - * @brief Set the unique id for the Selectivity object - * - * @param selectivity_id Unique id for the Selectivity object - */ - void SetSelectivity(int selectivity_id) { - this->selectivity_id = selectivity_id; - } - - /** @brief this adds the values to the TMB model object */ - virtual bool add_to_fims_tmb() { - // base model - std::shared_ptr> d0 = - fims::Information::GetInstance(); - - std::shared_ptr> f0 = - std::make_shared>(); - - // set relative info - f0->id = this->id; - f0->is_survey = this->is_survey; - f0->nages = this->nages; - f0->nyears = this->nyears; - f0->agecomp_likelihood_id = this->agecomp_likelihood_id; - f0->index_likelihood_id = this->index_likelihood_id; - f0->observed_agecomp_data_id = this->observed_agecomp_data_id; - f0->observed_index_data_id = this->observed_index_data_id; - f0->selectivity_id = this->selectivity_id; - - f0->log_obs_error = this->log_obs_error.value_m; - if (this->log_obs_error.estimated_m) { - d0->RegisterParameter(f0->log_obs_error); +class FleetInterfaceBase : public FIMSRcppInterfaceBase { +public: + static uint32_t id_g; /**< static id of the FleetInterfaceBase object */ + uint32_t id; /**< local id of the FleetInterfaceBase object */ + //live objects in C++ are objects that have been created and live in memory + static std::map live_objects; /**< + map relating the ID of the FleetInterfaceBase to the FleetInterfaceBase + objects */ + + FleetInterfaceBase() { + this->id = FleetInterfaceBase::id_g++; + //Create instance of map: key is id and value is pointer to FleetInterfaceBase + FleetInterfaceBase::live_objects[this->id] = this; + FIMSRcppInterfaceBase::fims_interface_objects.push_back(this); } - f0->log_q = this->log_q; - if (this->estimate_q) { - if (this->random_q) { - d0->RegisterRandomEffect(f0->log_q); - } else { - d0->RegisterParameter(f0->log_q); - } + + virtual ~FleetInterfaceBase() { } - f0->log_Fmort.resize(this->log_Fmort.size()); - for (int i = 0; i < log_Fmort.size(); i++) { - f0->log_Fmort[i] = this->log_Fmort[i]; + /** @brief get_id method for child fleet interface objects to inherit **/ + virtual uint32_t get_id() = 0; - if (this->estimate_F) { - if (this->random_F) { - d0->RegisterRandomEffect(f0->log_Fmort[i]); - } else { - d0->RegisterParameter(f0->log_Fmort[i]); - } - } - } - // add to Information - d0->fleets[f0->id] = f0; +}; - // 1st derivative model - std::shared_ptr> d1 = - fims::Information::GetInstance(); +uint32_t FleetInterfaceBase::id_g = 1; +std::map FleetInterfaceBase::live_objects; - std::shared_ptr> f1 = - std::make_shared>(); +/** + * @brief Rcpp interface for Fleet as an S4 object. To instantiate + * from R: + * fleet <- new(fims$Fleet) + * + */ +class FleetInterface : public FleetInterfaceBase { + int agecomp_likelihood_id = -999; /*!< id of agecomp likelihood component*/ + int index_likelihood_id = -999; /*!< id of index likelihood component*/ + int observed_agecomp_data_id = -999; /*!< id of observed agecomp data object*/ + int observed_index_data_id = -999; /*!< id of observed index data object*/ + int selectivity_id = -999; /*!< id of selectivity component*/ + +public: + bool is_survey = false; /*!< whether this is a survey fleet */ + int nages; /*!< number of ages in the fleet data*/ + int nyears; /*!< number of years in the fleet data */ + double log_q; /*!< log of catchability for the fleet*/ + Rcpp::NumericVector + log_Fmort; /*!< log of fishing mortality rate for the fleet*/ + bool estimate_F = false; /*!< whether the parameter F should be estimated*/ + bool estimate_q = false; /*!< whether the parameter q should be estimated*/ + bool random_q = false; /*!< whether q should be a random effect*/ + bool random_F = false; /*!< whether F should be a random effect*/ + Parameter log_obs_error; /*!< the log of the observation error */ + + FleetInterface() : FleetInterfaceBase() { - f1->id = this->id; - f1->is_survey = this->is_survey; - f1->nages = this->nages; - f1->nyears = this->nyears; - f1->agecomp_likelihood_id = this->agecomp_likelihood_id; - f1->index_likelihood_id = this->index_likelihood_id; - f1->observed_agecomp_data_id = this->observed_agecomp_data_id; - f1->observed_index_data_id = this->observed_index_data_id; - f1->log_obs_error = this->log_obs_error.value_m; - if (this->log_obs_error.estimated_m) { - d1->RegisterParameter(f1->log_obs_error); } - f1->selectivity_id = this->selectivity_id; - f1->log_q = this->log_q; - if (this->estimate_q) { - if (this->random_q) { - d1->RegisterRandomEffect(f1->log_q); - } else { - d1->RegisterParameter(f1->log_q); - } + virtual ~FleetInterface() { } - f1->log_Fmort.resize(this->log_Fmort.size()); - for (int i = 0; i < log_Fmort.size(); i++) { - f1->log_Fmort[i] = this->log_Fmort[i]; - if (this->estimate_F) { - if (this->random_F) { - d1->RegisterRandomEffect(f1->log_Fmort[i]); - } else { - d1->RegisterParameter(f1->log_Fmort[i]); - } - } + /** @brief returns the id for the fleet interface */ + virtual uint32_t get_id() { + return this->id; } - // add to Information - d1->fleets[f1->id] = f1; - - // 2nd derivative model - std::shared_ptr> d2 = - fims::Information::GetInstance(); - - std::shared_ptr> f2 = - std::make_shared>(); + /** + * @brief Set the unique id for the Age Comp Likelihood object + * + * @param agecomp_likelihood_id Unique id for the Age Comp Likelihood object + */ + void SetAgeCompLikelihood(int agecomp_likelihood_id) { + this->agecomp_likelihood_id = agecomp_likelihood_id; + } - f2->id = this->id; - f2->is_survey = this->is_survey; - f2->nages = this->nages; - f2->nyears = this->nyears; - f2->agecomp_likelihood_id = this->agecomp_likelihood_id; - f2->index_likelihood_id = this->index_likelihood_id; - f2->observed_agecomp_data_id = this->observed_agecomp_data_id; - f2->observed_index_data_id = this->observed_index_data_id; - f2->log_obs_error = this->log_obs_error.value_m; - if (this->log_obs_error.estimated_m) { - d2->RegisterParameter(f2->log_obs_error); + /** + * @brief Set the unique id for the Index Likelihood object + * + * @param index_likelihood_id Unique id for the Index Likelihood object + */ + void SetIndexLikelihood(int index_likelihood_id) { + this->index_likelihood_id = index_likelihood_id; } - f2->selectivity_id = this->selectivity_id; - f2->log_q = this->log_q; - if (this->estimate_q) { - if (this->random_q) { - d2->RegisterRandomEffect(f2->log_q); - } else { - d2->RegisterParameter(f2->log_q); - } + + /** + * @brief Set the unique id for the Observed Age Comp Data object + * + * @param observed_agecomp_data_id Unique id for the Observed Age Comp Data + * object + */ + void SetObservedAgeCompData(int observed_agecomp_data_id) { + this->observed_agecomp_data_id = observed_agecomp_data_id; } - f2->log_Fmort.resize(this->log_Fmort.size()); - for (int i = 0; i < log_Fmort.size(); i++) { - f2->log_Fmort[i] = this->log_Fmort[i]; - if (this->estimate_F) { - if (this->random_F) { - d2->RegisterRandomEffect(f2->log_Fmort[i]); - } else { - d2->RegisterParameter(f2->log_Fmort[i]); - } - } + /** + * @brief Set the unique id for the Observed Index Data object + * + * @param observed_index_data_id Unique id for the Observed Index Data object + */ + void SetObservedIndexData(int observed_index_data_id) { + this->observed_index_data_id = observed_index_data_id; } - // add to Information - d2->fleets[f2->id] = f2; - - // 3rd derivative model - std::shared_ptr> d3 = - fims::Information::GetInstance(); + /** + * @brief Set the unique id for the Selectivity object + * + * @param selectivity_id Unique id for the Selectivity object + */ + void SetSelectivity(int selectivity_id) { + this->selectivity_id = selectivity_id; + } + +#ifdef TMB_MODEL + template + bool add_to_fims_tmb_internal() { + std::shared_ptr > info = + fims::Information::GetInstance(); + + std::shared_ptr > fleet = + std::make_shared > (); + + // set relative info + fleet->id = this->id; + fleet->is_survey = this->is_survey; + fleet->nages = this->nages; + fleet->nyears = this-> nyears; + fleet->agecomp_likelihood_id = this->agecomp_likelihood_id; + fleet->index_likelihood_id = this->index_likelihood_id; + fleet->observed_agecomp_data_id = this->observed_agecomp_data_id; + fleet->observed_index_data_id = this->observed_index_data_id; + fleet->selectivity_id = this->selectivity_id; + + fleet->log_obs_error = this->log_obs_error.value_m; + if (this->log_obs_error.estimated_m) { + info->RegisterParameter(fleet->log_obs_error); + } + fleet->log_q = this->log_q; + if (this->estimate_q) { + if (this->random_q) { + info->RegisterRandomEffect(fleet->log_q); + } else { + info->RegisterParameter(fleet->log_q); + } + } - std::shared_ptr> f3 = - std::make_shared>(); + fleet->log_Fmort.resize(this->log_Fmort.size()); + for (int i = 0; i < log_Fmort.size(); i++) { + fleet->log_Fmort[i] = this->log_Fmort[i]; + + if (this->estimate_F) { + if (this->random_F) { + info->RegisterRandomEffect(fleet->log_Fmort[i]); + } else { + info->RegisterParameter(fleet->log_Fmort[i]); + } + } + } + // add to Information + info->fleets[fleet->id] = fleet; - f3->id = this->id; - f3->is_survey = this->is_survey; - f3->nages = this->nages; - f3->nyears = this->nyears; - f3->agecomp_likelihood_id = this->agecomp_likelihood_id; - f3->index_likelihood_id = this->index_likelihood_id; - f3->observed_agecomp_data_id = this->observed_agecomp_data_id; - f3->observed_index_data_id = this->observed_index_data_id; - f3->selectivity_id = this->selectivity_id; - f3->log_obs_error = this->log_obs_error.value_m; - if (this->log_obs_error.estimated_m) { - d3->RegisterParameter(f3->log_obs_error); - } - f3->log_q = this->log_q; - if (this->estimate_q) { - if (this->random_q) { - d3->RegisterRandomEffect(f3->log_q); - } else { - d3->RegisterParameter(f3->log_q); - } + return true; } - f3->log_Fmort.resize(this->log_Fmort.size()); - for (int i = 0; i < log_Fmort.size(); i++) { - f3->log_Fmort[i] = this->log_Fmort[i]; - if (this->estimate_F) { - if (this->random_F) { - d3->RegisterRandomEffect(f3->log_Fmort[i]); - } else { - d3->RegisterParameter(f3->log_Fmort[i]); - } - } + /** @brief this adds the values to the TMB model object */ + virtual bool add_to_fims_tmb() { + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + + return true; } - // add to Information - d3->fleets[f3->id] = f3; - return true; - } +#endif }; -uint32_t FleetInterface::id_g = 1; - #endif diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_growth.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_growth.hpp index 3829281f..5fa70cf4 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_growth.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_growth.hpp @@ -14,6 +14,7 @@ /**************************************************************** * Growth Rcpp interface * ***************************************************************/ + /** * @brief Rcpp interface that serves as the parent class for * Rcpp growth interfaces. This type should be inherited and not @@ -21,26 +22,29 @@ * */ class GrowthInterfaceBase : public FIMSRcppInterfaceBase { - public: - static uint32_t id_g; /**< static id of the GrowthInterfaceBase object */ - uint32_t id; /**< local id of the GrowthInterfaceBase object */ - static std::map live_objects; /**< +public: + static uint32_t id_g; /**< static id of the GrowthInterfaceBase object */ + uint32_t id; /**< local id of the GrowthInterfaceBase object */ + //live objects in C++ are objects that have been created and live in memory + static std::map live_objects; /**< map relating the ID of the GrowthInterfaceBase to the GrowthInterfaceBase objects */ - GrowthInterfaceBase() { - this->id = GrowthInterfaceBase::id_g++; - GrowthInterfaceBase::live_objects[this->id] = this; - FIMSRcppInterfaceBase::fims_interface_objects.push_back(this); - } + GrowthInterfaceBase() { + this->id = GrowthInterfaceBase::id_g++; + //Create instance of map: key is id and value is pointer to GrowthInterfaceBase + GrowthInterfaceBase::live_objects[this->id] = this; + FIMSRcppInterfaceBase::fims_interface_objects.push_back(this); + } - virtual ~GrowthInterfaceBase() {} + virtual ~GrowthInterfaceBase() { + } - /** @brief get_id method for child growth interface objects to inherit **/ - virtual uint32_t get_id() = 0; + /** @brief get_id method for child growth interface objects to inherit **/ + virtual uint32_t get_id() = 0; - /** @brief evaluate method for child growth interface objects to inherit **/ - virtual double evaluate(double age) = 0; + /** @brief evaluate method for child growth interface objects to inherit **/ + virtual double evaluate(double age) = 0; }; uint32_t GrowthInterfaceBase::id_g = 1; @@ -53,117 +57,92 @@ std::map GrowthInterfaceBase::live_objects; * */ class EWAAGrowthInterface : public GrowthInterfaceBase { - public: - std::vector weights; /**< weights for each age class */ - std::vector ages; /**< ages for each age class */ - std::map ewaa; /**< map of ewaa values */ +public: + std::vector weights; /**< weights for each age class */ + std::vector ages; /**< ages for each age class */ + std::map ewaa; /**< map of ewaa values */ - bool initialized = false; /**< boolean tracking if weights and ages + bool initialized = false; /**< boolean tracking if weights and ages vectors have been set */ - EWAAGrowthInterface() : GrowthInterfaceBase() {} - - virtual ~EWAAGrowthInterface() {} - - /** @brief get the id of the GrowthInterfaceBase object */ - virtual uint32_t get_id() { return this->id; } - - /** - * @brief Create a map of input numeric vectors - * @param weights T vector of weights - * @param ages T vector of ages - * @return std::map - * - * */ - inline std::map make_map(std::vector ages, - std::vector weights) { - std::map mymap; - for (uint32_t i = 0; i < ages.size(); i++) { - mymap.insert(std::pair(ages[i], weights[i])); - } - return mymap; - } - - /** @brief Rcpp interface to the EWAAgrowth evaluate method - * you can call from R using - * ewaagrowth.evaluate(age) - * */ - virtual double evaluate(double age) { - fims::EWAAgrowth EWAAGrowth; - - if (initialized == false) { - this->ewaa = make_map(this->ages, this->weights); - // Check that ages and weights vector are the same length - if (this->ages.size() != this->weights.size()) { - Rcpp::stop("ages and weights must be the same length"); - } - initialized = true; - } else { - Rcpp::stop("this empirical weight at age object is already initialized"); + EWAAGrowthInterface() : GrowthInterfaceBase() { } - EWAAGrowth.ewaa = this->ewaa; - return EWAAGrowth.evaluate(age); - } - - /** @brief this adds the values to the TMB model object */ - virtual bool add_to_fims_tmb() { - // base model - std::shared_ptr > d0 = - fims::Information::GetInstance(); - - std::shared_ptr > b0 = - std::make_shared >(); - - // set relative info - b0->id = this->id; - b0->ewaa = make_map(this->ages, this->weights); // this->ewaa; - // add to Information - d0->growth_models[b0->id] = b0; - - // base model - std::shared_ptr > d1 = - fims::Information::GetInstance(); - std::shared_ptr > b1 = - std::make_shared >(); + virtual ~EWAAGrowthInterface() { + } - // set relative info - b1->id = this->id; - b1->ewaa = make_map(this->ages, this->weights); // this->ewaa; + /** @brief get the id of the GrowthInterfaceBase object */ + virtual uint32_t get_id() { + return this->id; + } - // add to Information - d1->growth_models[b0->id] = b1; + /** + * @brief Create a map of input numeric vectors + * @param weights T vector of weights + * @param ages T vector of ages + * @return std::map + * + * */ + inline std::map make_map(std::vector ages, + std::vector weights) { + std::map mymap; + for (uint32_t i = 0; i < ages.size(); i++) { + mymap.insert(std::pair(ages[i], weights[i])); + } + return mymap; + } - // base model - std::shared_ptr > d2 = - fims::Information::GetInstance(); + /** @brief Rcpp interface to the EWAAgrowth evaluate method + * you can call from R using + * ewaagrowth.evaluate(age) + * */ + virtual double evaluate(double age) { + fims::EWAAgrowth EWAAGrowth; + + if (initialized == false) { + this->ewaa = make_map(this->ages, this->weights); + // Check that ages and weights vector are the same length + if (this->ages.size() != this->weights.size()) { + Rcpp::stop("ages and weights must be the same length"); + } + initialized = true; + } else { + Rcpp::stop("this empirical weight at age object is already initialized"); + } + EWAAGrowth.ewaa = this->ewaa; + return EWAAGrowth.evaluate(age); + } +#ifdef TMB_MODEL - std::shared_ptr > b2 = - std::make_shared >(); + template + bool add_to_fims_tmb_internal() { + std::shared_ptr > info = + fims::Information::GetInstance(); - // set relative info - b2->id = this->id; - b2->ewaa = make_map(this->ages, this->weights); // this->ewaa; + std::shared_ptr > ewaa_growth = + std::make_shared >(); - // add to Information - d2->growth_models[b2->id] = b2; + // set relative info + ewaa_growth->id = this->id; + ewaa_growth->ewaa = make_map(this->ages, this->weights); //this->ewaa; + // add to Information + info->growth_models[ewaa_growth->id] = ewaa_growth; - // base model - std::shared_ptr > d3 = - fims::Information::GetInstance(); + return true; + } - std::shared_ptr > b3 = - std::make_shared >(); + /** @brief this adds the values to the TMB model object */ + virtual bool add_to_fims_tmb() { + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); - // set relative info - b3->id = this->id; - b3->ewaa = make_map(this->ages, this->weights); // this->ewaa; - // add to Information - d3->growth_models[b3->id] = b3; + return true; + } - return true; - } +#endif }; #endif diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_maturity.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_maturity.hpp index f2cc78ff..660b723b 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_maturity.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_maturity.hpp @@ -16,186 +16,124 @@ /**************************************************************** * maturity Rcpp interface * ***************************************************************/ + /** * @brief MaturityInterfaceBase class should be inherited to * define different Rcpp interfaces for each possible maturity function * */ class MaturityInterfaceBase : public FIMSRcppInterfaceBase { - public: - static uint32_t id_g; /**< static id of the recruitment interface base*/ - uint32_t id; /**< id of the recruitment interface base */ - static std::map - maturity_objects; /**< map associating the ids of +public: + static uint32_t id_g; /**< static id of the recruitment interface base*/ + uint32_t id; /**< id of the recruitment interface base */ + //live objects in C++ are objects that have been created and live in memory + static std::map + live_objects; /**< map associating the ids of MaturityInterfaceBase to the objects */ - MaturityInterfaceBase() { - this->id = MaturityInterfaceBase::id_g++; - MaturityInterfaceBase::maturity_objects[this->id] = this; - FIMSRcppInterfaceBase::fims_interface_objects.push_back(this); - } + MaturityInterfaceBase() { + this->id = MaturityInterfaceBase::id_g++; + //Create instance of map: key is id and value is pointer to MaturityInterfaceBase + MaturityInterfaceBase::live_objects[this->id] = this; + FIMSRcppInterfaceBase::fims_interface_objects.push_back(this); + } - virtual ~MaturityInterfaceBase() {} + virtual ~MaturityInterfaceBase() { + } - /** @brief get the ID of the interface base object - **/ - virtual uint32_t get_id() = 0; + /** @brief get the ID of the interface base object + **/ + virtual uint32_t get_id() = 0; - /** - * @brief evaluate the function - * - */ - virtual double evaluate(double x) = 0; + /** + * @brief evaluate the function + * + */ + virtual double evaluate(double x) = 0; }; uint32_t MaturityInterfaceBase::id_g = 1; std::map - MaturityInterfaceBase::maturity_objects; +MaturityInterfaceBase::live_objects; /** * @brief Rcpp interface for logistic maturity as an S4 object. To * instantiate from R: logistic_maturity <- new(fims$logistic_maturity) */ class LogisticMaturityInterface : public MaturityInterfaceBase { - public: - Parameter median; /**< the index value at which the response reaches .5 */ - Parameter slope; /**< the width of the curve at the median */ - - LogisticMaturityInterface() : MaturityInterfaceBase() {} - - virtual ~LogisticMaturityInterface() {} - - /** @brief returns the id for the logistic maturity interface */ - virtual uint32_t get_id() { return this->id; } - - /** @brief evaluate the logistic maturity function - * @param x The independent variable in the logistic function (e.g., age or - * size in maturity). - */ - virtual double evaluate(double x) { - fims::LogisticMaturity LogisticMat; - - LogisticMat.median = this->median.value_m; - LogisticMat.slope = this->slope.value_m; - return LogisticMat.evaluate(x); - } - - /** @brief this adds the parameter values and derivatives to the TMB model - * object */ - virtual bool add_to_fims_tmb() { - std::shared_ptr > d0 = - fims::Information::GetInstance(); - - std::shared_ptr > lm0 = - std::make_shared >(); - - // set relative info - lm0->id = this->id; - lm0->median = this->median.value_m; - if (this->median.estimated_m) { - if (this->median.is_random_effect_m) { - d0->RegisterRandomEffect(lm0->median); - } else { - d0->RegisterParameter(lm0->median); - } - } - lm0->slope = this->slope.value_m; - if (this->slope.estimated_m) { - if (this->slope.is_random_effect_m) { - d0->RegisterRandomEffect(lm0->slope); - } else { - d0->RegisterParameter(lm0->slope); - } - } - - // add to Information - d0->maturity_models[lm0->id] = lm0; - - std::shared_ptr > d1 = - fims::Information::GetInstance(); - - std::shared_ptr > lm1 = - std::make_shared >(); +public: + Parameter median; /**< the index value at which the response reaches .5 */ + Parameter slope; /**< the width of the curve at the median */ - // set relative info - lm1->id = this->id; - lm1->median = this->median.value_m; - if (this->median.estimated_m) { - if (this->median.is_random_effect_m) { - d1->RegisterRandomEffect(lm1->median); - } else { - d1->RegisterParameter(lm1->median); - } + LogisticMaturityInterface() : MaturityInterfaceBase() { } - lm1->slope = this->slope.value_m; - if (this->slope.estimated_m) { - if (this->slope.is_random_effect_m) { - d1->RegisterRandomEffect(lm1->slope); - } else { - d1->RegisterParameter(lm1->slope); - } - } - - // add to Information - d1->maturity_models[lm1->id] = lm1; - std::shared_ptr > d2 = - fims::Information::GetInstance(); - - std::shared_ptr > lm2 = - std::make_shared >(); + virtual ~LogisticMaturityInterface() { + } - // set relative info - lm2->id = this->id; - lm2->median = this->median.value_m; - if (this->median.estimated_m) { - if (this->median.is_random_effect_m) { - d2->RegisterRandomEffect(lm2->median); - } else { - d2->RegisterParameter(lm2->median); - } + /** @brief returns the id for the logistic maturity interface */ + virtual uint32_t get_id() { + return this->id; } - lm2->slope = this->slope.value_m; - if (this->slope.estimated_m) { - if (this->slope.is_random_effect_m) { - d2->RegisterRandomEffect(lm2->slope); - } else { - d2->RegisterParameter(lm2->slope); - } + + /** @brief evaluate the logistic maturity function + * @param x The independent variable in the logistic function (e.g., age or + * size in maturity). + */ + virtual double evaluate(double x) { + fims::LogisticMaturity LogisticMat; + LogisticMat.median = this->median.value_m; + LogisticMat.slope = this->slope.value_m; + return LogisticMat.evaluate(x); } - // add to Information - d2->maturity_models[lm2->id] = lm2; +#ifdef TMB_MODEL + + template + bool add_to_fims_tmb_internal() { + std::shared_ptr > info = + fims::Information::GetInstance(); + + std::shared_ptr > maturity = + std::make_shared >(); + + // set relative info + maturity->id = this->id; + maturity->median = this->median.value_m; + if (this->median.estimated_m) { + if (this->median.is_random_effect_m) { + info->RegisterRandomEffect(maturity->median); + } else { + info->RegisterParameter(maturity->median); + } + } + maturity->slope = this->slope.value_m; + if (this->slope.estimated_m) { + if (this->slope.is_random_effect_m) { + info->RegisterRandomEffect(maturity->slope); + } else { + info->RegisterParameter(maturity->slope); + } + } + + // add to Information + info->maturity_models[maturity->id] = maturity; + + return true; + } - std::shared_ptr > d3 = - fims::Information::GetInstance(); + /** @brief this adds the parameter values and derivatives to the TMB model + * object */ + virtual bool add_to_fims_tmb() { - std::shared_ptr > lm3 = - std::make_shared >(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); - // set relative info - lm3->id = this->id; - lm3->median = this->median.value_m; - if (this->median.estimated_m) { - if (this->median.is_random_effect_m) { - d3->RegisterRandomEffect(lm3->median); - } else { - d3->RegisterParameter(lm3->median); - } + return true; } - lm3->slope = this->slope.value_m; - if (this->slope.estimated_m) { - if (this->slope.is_random_effect_m) { - d3->RegisterRandomEffect(lm3->slope); - } else { - d3->RegisterParameter(lm3->slope); - } - } - - // add to Information - d3->maturity_models[lm3->id] = lm3; - return true; - } +#endif }; #endif \ No newline at end of file diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_nll.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_nll.hpp index d89a2532..70d52791 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_nll.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_nll.hpp @@ -21,11 +21,13 @@ class NLLInterfaceBase : public FIMSRcppInterfaceBase { public: static uint32_t id_g; /**< static id of the recruitment interface base*/ uint32_t id; /**< id of the recruitment interface base */ + //live objects in C++ are objects that have been created and live in memory static std::map live_objects; /**< map associating the ids of NLLInterfaceBase to the objects */ NLLInterfaceBase() { this->id = NLLInterfaceBase::id_g++; + //Create instance of map: key is id and value is pointer to NLLInterfaceBase NLLInterfaceBase::live_objects[this->id] = this; NLLInterfaceBase::fims_interface_objects.push_back(this); } diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_population.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_population.hpp index a9e7593b..9ecdbcb1 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_population.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_population.hpp @@ -14,33 +14,37 @@ /**************************************************************** * Population Rcpp interface * ***************************************************************/ + /** * @brief PopulationInterfaceBase class should be inherited to * define different Rcpp interfaces for each possible Population function * */ class PopulationInterfaceBase : public FIMSRcppInterfaceBase { - public: - static uint32_t id_g; /**< static id of the population interface base*/ - uint32_t id; /**< id of the population interface base */ - static std::map - live_objects; /**< map associating the ids of PopulationInterfaceBase to +public: + static uint32_t id_g; /**< static id of the population interface base*/ + uint32_t id; /**< id of the population interface base */ + //live objects in C++ are objects that have been created and live in memory + static std::map + live_objects; /**< map associating the ids of PopulationInterfaceBase to the objects */ - PopulationInterfaceBase() { - this->id = PopulationInterfaceBase::id_g++; - PopulationInterfaceBase::live_objects[this->id] = this; - PopulationInterfaceBase::fims_interface_objects.push_back(this); - } + PopulationInterfaceBase() { + this->id = PopulationInterfaceBase::id_g++; + //Create instance of map: key is id and value is pointer to PopulationInterfaceBase + PopulationInterfaceBase::live_objects[this->id] = this; + PopulationInterfaceBase::fims_interface_objects.push_back(this); + } - virtual ~PopulationInterfaceBase() {} + virtual ~PopulationInterfaceBase() { + } - /** @brief get_id method for child classes to inherit */ - virtual uint32_t get_id() = 0; + /** @brief get_id method for child classes to inherit */ + virtual uint32_t get_id() = 0; }; uint32_t PopulationInterfaceBase::id_g = 1; std::map - PopulationInterfaceBase::live_objects; +PopulationInterfaceBase::live_objects; /** * @brief Rcpp interface for a new Population. To instantiate @@ -48,227 +52,128 @@ std::map * population <- new(fims$population) */ class PopulationInterface : public PopulationInterfaceBase { - public: - uint32_t nages; /**< number of ages */ - uint32_t nfleets; /**< number of fleets */ - uint32_t nseasons; /**< number of seasons */ - uint32_t nyears; /**< number of years */ - uint32_t maturity_id; /**< id of the maturity function*/ - uint32_t growth_id; /**< id of the growth function*/ - uint32_t recruitment_id; /**< id of the recruitment function*/ - Rcpp::NumericVector log_M; /**< log of the natural mortality of the stock*/ - Rcpp::NumericVector log_init_naa; /**id; } - - /** - * @brief Set the unique id for the Maturity object - * - * @param maturity_id Unique id for the Maturity object - */ - void SetMaturity(uint32_t maturity_id) { this->maturity_id = maturity_id; } - - /** - * @brief Set the unique id for the growth object - * - * @param growth_id Unique id for the growth object - */ - void SetGrowth(uint32_t growth_id) { this->growth_id = growth_id; } - - /** - * @brief Set the unique id for the Maturity object - * - * @param recruitment_id Unique id for the Maturity object - */ - void SetRecruitment(uint32_t recruitment_id) { - this->recruitment_id = recruitment_id; - } - - /** @brief evaluate the population function */ - virtual void evaluate() { - fims::Population population; - return population.Evaluate(); - } - - /** @brief this adds the parameter values and derivatives to the TMB model - * object */ - virtual bool add_to_fims_tmb() { - // base model - std::shared_ptr > d0 = - fims::Information::GetInstance(); - - std::shared_ptr > b0 = - std::make_shared >(); - - // set relative info - b0->id = this->id; - b0->nyears = this->nyears; - b0->nfleets = this->nfleets; - b0->nseasons = this->nseasons; - b0->nages = this->nages; - if (this->nages == this->ages.size()) { - b0->ages.resize(this->nages); - } else { - warning("The ages vector is not of size nages."); +public: + uint32_t nages; /**< number of ages */ + uint32_t nfleets; /**< number of fleets */ + uint32_t nseasons; /**< number of seasons */ + uint32_t nyears; /**< number of years */ + uint32_t maturity_id; /**< id of the maturity function*/ + uint32_t growth_id; /**< id of the growth function*/ + uint32_t recruitment_id; /**< id of the recruitment function*/ + Rcpp::NumericVector log_M; /**< log of the natural mortality of the stock*/ + Rcpp::NumericVector log_init_naa; /**growth_id = this->growth_id; - b0->recruitment_id = this->recruitment_id; - b0->maturity_id = this->maturity_id; - b0->log_M.resize(this->log_M.size()); - b0->log_init_naa.resize(this->log_init_naa.size()); - for (int i = 0; i < log_M.size(); i++) { - b0->log_M[i] = this->log_M[i]; - if (estimate_M) { - d0->RegisterParameter(b0->log_M[i]); - } + virtual ~PopulationInterface() { } - for (int i = 0; i < log_init_naa.size(); i++) { - b0->log_init_naa[i] = this->log_init_naa[i]; - if (estimate_initNAA) { - d0->RegisterParameter(b0->log_init_naa[i]); - } - } - for (int i = 0; i < ages.size(); i++) { - b0->ages[i] = this->ages[i]; + virtual uint32_t get_id() { + return this->id; } - // add to Information - d0->populations[b0->id] = b0; - - // first-order derivative - std::shared_ptr > d1 = - fims::Information::GetInstance(); - - std::shared_ptr > b1 = - std::make_shared >(); - - // set relative info - b1->id = this->id; - b1->nyears = this->nyears; - b1->nfleets = this->nfleets; - b1->nseasons = this->nseasons; - b1->nages = this->nages; - b1->ages.resize(this->nages); - - b1->growth_id = this->growth_id; - b1->recruitment_id = this->recruitment_id; - b1->maturity_id = this->maturity_id; - - b1->log_M.resize(this->log_M.size()); - b1->log_init_naa.resize(this->log_init_naa.size()); - for (int i = 0; i < log_M.size(); i++) { - b1->log_M[i] = this->log_M[i]; - if (estimate_M) { - d1->RegisterParameter(b1->log_M[i]); - } - } - for (int i = 0; i < log_init_naa.size(); i++) { - b1->log_init_naa[i] = this->log_init_naa[i]; - if (estimate_initNAA) { - d1->RegisterParameter(b1->log_init_naa[i]); - } - } - for (int i = 0; i < ages.size(); i++) { - b1->ages[i] = this->ages[i]; + /** + * @brief Set the unique id for the Maturity object + * + * @param maturity_id Unique id for the Maturity object + */ + void SetMaturity(uint32_t maturity_id) { + this->maturity_id = maturity_id; } - // add to Information - d1->populations[b1->id] = b1; - - // second-order derivative - std::shared_ptr > d2 = - fims::Information::GetInstance(); - - std::shared_ptr > b2 = - std::make_shared >(); - - // set relative info - b2->id = this->id; - b2->nyears = this->nyears; - b2->nfleets = this->nfleets; - b2->nseasons = this->nseasons; - b2->nages = this->nages; - b2->log_M.resize(this->log_M.size()); - b2->ages.resize(nages); - - b2->growth_id = this->growth_id; - b2->recruitment_id = this->recruitment_id; - b2->maturity_id = this->maturity_id; - - b2->log_init_naa.resize(this->log_init_naa.size()); - for (int i = 0; i < log_M.size(); i++) { - b2->log_M[i] = this->log_M[i]; - if (estimate_M) { - d2->RegisterParameter(b2->log_M[i]); - } - } - for (int i = 0; i < log_init_naa.size(); i++) { - b2->log_init_naa[i] = this->log_init_naa[i]; - if (estimate_initNAA) { - d2->RegisterParameter(b2->log_init_naa[i]); - } + /** + * @brief Set the unique id for the growth object + * + * @param growth_id Unique id for the growth object + */ + void SetGrowth(uint32_t growth_id) { + this->growth_id = growth_id; } - for (int i = 0; i < ages.size(); i++) { - b2->ages[i] = this->ages[i]; + /** + * @brief Set the unique id for the Maturity object + * + * @param recruitment_id Unique id for the Maturity object + */ + void SetRecruitment(uint32_t recruitment_id) { + this->recruitment_id = recruitment_id; } - // add to Information - d2->populations[b2->id] = b2; - - // third-order derivative - std::shared_ptr > d3 = - fims::Information::GetInstance(); - - std::shared_ptr > b3 = - std::make_shared >(); + /** @brief evaluate the population function */ + virtual void evaluate() { + fims::Population population; + return population.Evaluate(); + } - // set relative info - b3->id = this->id; - b3->nyears = this->nyears; - b3->nfleets = this->nfleets; - b3->nseasons = this->nseasons; - b3->nages = this->nages; - b3->log_M.resize(this->log_M.size()); - b3->log_init_naa.resize(this->log_init_naa.size()); - b3->ages.resize(this->nages); +#ifdef TMB_MODEL + + template + bool add_to_fims_tmb_internal() { + std::shared_ptr > info = + fims::Information::GetInstance(); + + std::shared_ptr > population = + std::make_shared >(); + + // set relative info + population->id = this->id; + population->nyears = this->nyears; + population->nfleets = this->nfleets; + population->nseasons = this->nseasons; + population->nages = this->nages; + if (this->nages == this->ages.size()) { + population->ages.resize(this->nages); + } else { + warning("The ages vector is not of size nages."); + } + + population->growth_id = this->growth_id; + population->recruitment_id = this->recruitment_id; + population->maturity_id = this->maturity_id; + population->log_M.resize(this->log_M.size()); + population->log_init_naa.resize(this->log_init_naa.size()); + for (size_t i = 0; i < log_M.size(); i++) { + population->log_M[i] = this->log_M[i]; + if (estimate_M) { + info->RegisterParameter(population->log_M[i]); + } + } + + for (size_t i = 0; i < log_init_naa.size(); i++) { + population->log_init_naa[i] = this->log_init_naa[i]; + if (estimate_initNAA) { + info->RegisterParameter(population->log_init_naa[i]); + } + + } + for (size_t i = 0; i < ages.size(); i++) { + population->ages[i] = this->ages[i]; + } + + // add to Information + info->populations[population->id] = population; + + return true; + } - b3->growth_id = this->growth_id; - b3->recruitment_id = this->recruitment_id; - b3->maturity_id = this->maturity_id; + /** @brief this adds the parameter values and derivatives to the TMB model + * object */ + virtual bool add_to_fims_tmb() { - for (int i = 0; i < log_M.size(); i++) { - b3->log_M[i] = this->log_M[i]; - if (estimate_M) { - d3->RegisterParameter(b3->log_M[i]); - } - } - for (int i = 0; i < log_init_naa.size(); i++) { - b3->log_init_naa[i] = this->log_init_naa[i]; - if (estimate_initNAA) { - d3->RegisterParameter(b3->log_init_naa[i]); - } - } + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); - for (int i = 0; i < ages.size(); i++) { - b3->ages[i] = this->ages[i]; + return true; } - // add to Information - d3->populations[b3->id] = b3; - - return true; - } +#endif }; #endif diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_recruitment.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_recruitment.hpp index a9ae8d57..86b45267 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_recruitment.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_recruitment.hpp @@ -21,44 +21,47 @@ * define different Rcpp interfaces for each possible Recruitment function * */ class RecruitmentInterfaceBase : public FIMSRcppInterfaceBase { - public: - static uint32_t id_g; /**< static id of the recruitment interface base*/ - uint32_t id; /**< id of the recruitment interface base */ - static std::map live_objects; - /**< map associating the ids of RecruitmentInterfaceBase to the objects */ - - // static std::vector recruit_deviations; /**< vector of recruitment - // deviations*/ - /// static bool constrain_deviations; /**< whether or not the rec devs are - /// constrained*/ - - RecruitmentInterfaceBase() { - this->id = RecruitmentInterfaceBase::id_g++; - RecruitmentInterfaceBase::live_objects[this->id] = this; - FIMSRcppInterfaceBase::fims_interface_objects.push_back(this); - } - - virtual ~RecruitmentInterfaceBase() {} - - /** @brief get the ID of the interface base object - **/ - virtual uint32_t get_id() = 0; - - /** @brief evaluate method for child recruitment interface objects to inherit - * **/ - virtual double evaluate(double spawners, double ssbzero) = 0; - - /** - * @brief evaluate recruitment nll - * - * @return double - */ - virtual double evaluate_nll() = 0; +public: + static uint32_t id_g; /**< static id of the recruitment interface base*/ + uint32_t id; /**< id of the recruitment interface base */ + //live objects in C++ are objects that have been created and live in memory + static std::map live_objects; + /**< map associating the ids of RecruitmentInterfaceBase to the objects */ + + // static std::vector recruit_deviations; /**< vector of recruitment + // deviations*/ + /// static bool constrain_deviations; /**< whether or not the rec devs are + /// constrained*/ + + RecruitmentInterfaceBase() { + this->id = RecruitmentInterfaceBase::id_g++; + //Create instance of map: key is id and value is pointer to RecruitmentInterfaceBase + RecruitmentInterfaceBase::live_objects[this->id] = this; + FIMSRcppInterfaceBase::fims_interface_objects.push_back(this); + } + + virtual ~RecruitmentInterfaceBase() { + } + + /** @brief get the ID of the interface base object + **/ + virtual uint32_t get_id() = 0; + + /** @brief evaluate method for child recruitment interface objects to inherit + * **/ + virtual double evaluate(double spawners, double ssbzero) = 0; + + /** + * @brief evaluate recruitment nll + * + * @return double + */ + virtual double evaluate_nll() = 0; }; uint32_t RecruitmentInterfaceBase::id_g = 1; std::map - RecruitmentInterfaceBase::live_objects; +RecruitmentInterfaceBase::live_objects; /** * @brief Rcpp interface for Beverton-Holt as an S4 object. To instantiate @@ -66,250 +69,122 @@ std::map * beverton_holt <- new(fims$beverton_holt) */ class BevertonHoltRecruitmentInterface : public RecruitmentInterfaceBase { - public: - Parameter logit_steep; /**< steepness or the productivity of the stock*/ - Parameter log_rzero; /**< recruitment at unfished biomass */ - Parameter log_sigma_recruit; /**< the log of the stock recruit deviations */ - Rcpp::NumericVector deviations; /**< recruitment deviations*/ - bool estimate_deviations = - false; /**< boolean describing whether to estimate */ +public: + Parameter logit_steep; /**< steepness or the productivity of the stock*/ + Parameter log_rzero; /**< recruitment at unfished biomass */ + Parameter log_sigma_recruit; /**< the log of the stock recruit deviations */ + Rcpp::NumericVector deviations; /**< recruitment deviations*/ + bool estimate_deviations = + false; /**< boolean describing whether to estimate */ + + BevertonHoltRecruitmentInterface() : RecruitmentInterfaceBase() { + } + + virtual ~BevertonHoltRecruitmentInterface() { + } - BevertonHoltRecruitmentInterface() : RecruitmentInterfaceBase() {} + virtual uint32_t get_id() { + return this->id; + } - virtual ~BevertonHoltRecruitmentInterface() {} + virtual double evaluate(double spawners, double ssbzero) { + fims::SRBevertonHolt BevHolt; - virtual uint32_t get_id() { return this->id; } + BevHolt.logit_steep = this->logit_steep.value_m; + if (this->logit_steep.value_m == 1.0) { + warning( + "Steepness is subject to a logit transformation, so its value is " + "0.7848469. Fixing it at 1.0 is not currently possible."); + } - virtual double evaluate(double spawners, double ssbzero) { - fims::SRBevertonHolt BevHolt; + BevHolt.log_rzero = this->log_rzero.value_m; - BevHolt.logit_steep = this->logit_steep.value_m; - if (this->logit_steep.value_m == 1.0) { - warning( - "Steepness is subject to a logit transformation, so its value is " - "0.7848469. Fixing it at 1.0 is not currently possible."); + return BevHolt.evaluate(spawners, ssbzero); } - BevHolt.log_rzero = this->log_rzero.value_m; + virtual double evaluate_nll() { + fims::SRBevertonHolt NLL; - return BevHolt.evaluate(spawners, ssbzero); - } + NLL.log_sigma_recruit = this->log_sigma_recruit.value_m; + NLL.recruit_deviations.resize(deviations.size()); // Vector from TMB + for (int i = 0; i < deviations.size(); i++) { + NLL.recruit_deviations[i] = deviations[i]; + } + FIMS_LOG << "Rec devs being passed to C++ are " << deviations << std::endl; + NLL.estimate_recruit_deviations = this->estimate_deviations; + return NLL.evaluate_nll(); + } - virtual double evaluate_nll() { - fims::SRBevertonHolt NLL; +#ifdef TMB_MODEL - NLL.log_sigma_recruit = this->log_sigma_recruit.value_m; - NLL.recruit_deviations.resize(deviations.size()); // Vector from TMB - for (int i = 0; i < deviations.size(); i++) { - NLL.recruit_deviations[i] = deviations[i]; - } - FIMS_LOG << "Rec devs being passed to C++ are " << deviations << std::endl; - - NLL.estimate_recruit_deviations = this->estimate_deviations; - return NLL.evaluate_nll(); - } - - /** @brief this adds the parameter values and derivatives to the TMB model - * object */ - virtual bool add_to_fims_tmb() { - // base model - std::shared_ptr > d0 = - fims::Information::GetInstance(); - - std::shared_ptr > b0 = - std::make_shared >(); - - // set relative info - b0->id = this->id; - b0->logit_steep = this->logit_steep.value_m; - if (this->logit_steep.estimated_m) { - if (this->logit_steep.is_random_effect_m) { - d0->RegisterRandomEffect(b0->logit_steep); - } else { - d0->RegisterParameter(b0->logit_steep); - } - } - b0->log_rzero = this->log_rzero.value_m; - if (this->log_rzero.estimated_m) { - if (this->log_rzero.is_random_effect_m) { - d0->RegisterRandomEffect(b0->log_rzero); - } else { - d0->RegisterParameter(b0->log_rzero); - } - } - b0->log_sigma_recruit = this->log_sigma_recruit.value_m; - if (this->log_sigma_recruit.estimated_m) { - if (this->log_sigma_recruit.is_random_effect_m) { - d0->RegisterRandomEffect(b0->log_sigma_recruit); - } else { - d0->RegisterParameter(b0->log_sigma_recruit); - } - } + template + bool add_to_fims_tmb_internal() { + std::shared_ptr > info = + fims::Information::GetInstance(); - b0->recruit_deviations.resize(this->deviations.size()); - if (this->estimate_deviations) { - for (size_t i = 0; i < b0->recruit_deviations.size(); i++) { - b0->recruit_deviations[i] = this->deviations[i]; - d0->RegisterParameter(b0->recruit_deviations[i]); - } - } else { - for (size_t i = 0; i < b0->recruit_deviations.size(); i++) { - b0->recruit_deviations[i] = this->deviations[i]; - } - } + std::shared_ptr > recruitment = + std::make_shared >(); - // add to Information - d0->recruitment_models[b0->id] = b0; - - // first-order derivative - std::shared_ptr > d1 = - fims::Information::GetInstance(); - - std::shared_ptr > b1 = - std::make_shared >(); - - // set relative info - b1->id = this->id; - b1->logit_steep = this->logit_steep.value_m; - if (this->logit_steep.estimated_m) { - if (this->logit_steep.is_random_effect_m) { - d1->RegisterRandomEffect(b1->logit_steep); - } else { - d1->RegisterParameter(b1->logit_steep); - } - } - b1->log_rzero = this->log_rzero.value_m; - if (this->log_rzero.estimated_m) { - if (this->log_rzero.is_random_effect_m) { - d1->RegisterRandomEffect(b1->log_rzero); - } else { - d1->RegisterParameter(b1->log_rzero); - } - } - b1->log_sigma_recruit = this->log_sigma_recruit.value_m; - if (this->log_sigma_recruit.estimated_m) { - if (this->log_sigma_recruit.is_random_effect_m) { - d1->RegisterRandomEffect(b1->log_sigma_recruit); - } else { - d1->RegisterParameter(b1->log_sigma_recruit); - } - } + // set relative info + recruitment->id = this->id; + recruitment->logit_steep = this->logit_steep.value_m; + if (this->logit_steep.estimated_m) { + if (this->logit_steep.is_random_effect_m) { + info->RegisterRandomEffect(recruitment->logit_steep); + } else { + info->RegisterParameter(recruitment->logit_steep); + } + } + recruitment->log_rzero = this->log_rzero.value_m; + if (this->log_rzero.estimated_m) { + if (this->log_rzero.is_random_effect_m) { + info->RegisterRandomEffect(recruitment->log_rzero); + } else { + info->RegisterParameter(recruitment->log_rzero); + } + } + recruitment->log_sigma_recruit = this->log_sigma_recruit.value_m; + if (this->log_sigma_recruit.estimated_m) { + if (this->log_sigma_recruit.is_random_effect_m) { + info->RegisterRandomEffect(recruitment->log_sigma_recruit); + } else { + info->RegisterParameter(recruitment->log_sigma_recruit); + } + } - b1->recruit_deviations.resize(this->deviations.size()); - if (this->estimate_deviations) { - for (size_t i = 0; i < b1->recruit_deviations.size(); i++) { - b1->recruit_deviations[i] = this->deviations[i]; - d1->RegisterParameter(b1->recruit_deviations[i]); - } - } else { - for (size_t i = 0; i < b1->recruit_deviations.size(); i++) { - b1->recruit_deviations[i] = this->deviations[i]; - } - } + recruitment->recruit_deviations.resize(this->deviations.size()); + if (this->estimate_deviations) { + for (size_t i = 0; i < recruitment->recruit_deviations.size(); i++) { + recruitment->recruit_deviations[i] = this->deviations[i]; + info->RegisterParameter(recruitment->recruit_deviations[i]); + } + } else { + for (size_t i = 0; i < recruitment->recruit_deviations.size(); i++) { + recruitment->recruit_deviations[i] = this->deviations[i]; + } + } - // add to Information - d1->recruitment_models[b1->id] = b1; - - // second-order derivative - std::shared_ptr > d2 = - fims::Information::GetInstance(); - - std::shared_ptr > b2 = - std::make_shared >(); - - // set relative info - b2->id = this->id; - b2->logit_steep = this->logit_steep.value_m; - if (this->logit_steep.estimated_m) { - if (this->logit_steep.is_random_effect_m) { - d2->RegisterRandomEffect(b2->logit_steep); - } else { - d2->RegisterParameter(b2->logit_steep); - } - } - b2->log_rzero = this->log_rzero.value_m; - if (this->log_rzero.estimated_m) { - if (this->log_rzero.is_random_effect_m) { - d2->RegisterRandomEffect(b2->log_rzero); - } else { - d2->RegisterParameter(b2->log_rzero); - } - } - b2->log_sigma_recruit = this->log_sigma_recruit.value_m; - if (this->log_sigma_recruit.estimated_m) { - if (this->log_sigma_recruit.is_random_effect_m) { - d2->RegisterRandomEffect(b2->log_sigma_recruit); - } else { - d2->RegisterParameter(b2->log_sigma_recruit); - } - } + // add to Information + info->recruitment_models[recruitment->id] = recruitment; - b2->recruit_deviations.resize(this->deviations.size()); - if (this->estimate_deviations) { - for (size_t i = 0; i < b2->recruit_deviations.size(); i++) { - b2->recruit_deviations[i] = this->deviations[i]; - d2->RegisterParameter(b2->recruit_deviations[i]); - } - } else { - for (size_t i = 0; i < b2->recruit_deviations.size(); i++) { - b2->recruit_deviations[i] = this->deviations[i]; - } - } - // add to Information - d2->recruitment_models[b2->id] = b2; - - // third-order derivative - std::shared_ptr > d3 = - fims::Information::GetInstance(); - - std::shared_ptr > b3 = - std::make_shared >(); - - // set relative info - b3->id = this->id; - b3->logit_steep = this->logit_steep.value_m; - if (this->logit_steep.estimated_m) { - if (this->logit_steep.is_random_effect_m) { - d3->RegisterRandomEffect(b3->logit_steep); - } else { - d3->RegisterParameter(b3->logit_steep); - } - } - b3->log_rzero = this->log_rzero.value_m; - if (this->log_rzero.estimated_m) { - if (this->log_rzero.is_random_effect_m) { - d3->RegisterRandomEffect(b3->log_rzero); - } else { - d3->RegisterParameter(b3->log_rzero); - } - } - b3->log_sigma_recruit = this->log_sigma_recruit.value_m; - if (this->log_sigma_recruit.estimated_m) { - if (this->log_sigma_recruit.is_random_effect_m) { - d3->RegisterRandomEffect(b3->log_sigma_recruit); - } else { - d3->RegisterParameter(b3->log_sigma_recruit); - } + return true; } - b3->recruit_deviations.resize(this->deviations.size()); - if (this->estimate_deviations) { - for (size_t i = 0; i < b3->recruit_deviations.size(); i++) { - b3->recruit_deviations[i] = this->deviations[i]; - d3->RegisterParameter(b3->recruit_deviations[i]); - } - } else { - for (size_t i = 0; i < b3->recruit_deviations.size(); i++) { - b3->recruit_deviations[i] = this->deviations[i]; - } + /** @brief this adds the parameter values and derivatives to the TMB model + * object */ + virtual bool add_to_fims_tmb() { + + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + + return true; } - // add to Information - d3->recruitment_models[b3->id] = b3; +#endif - return true; - } }; #endif diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_selectivity.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_selectivity.hpp index 1cba5585..909c9bfa 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_selectivity.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_selectivity.hpp @@ -15,186 +15,126 @@ /**************************************************************** * Selectivity Rcpp interface * ***************************************************************/ + /** * @brief SelectivityInterfaceBase class should be inherited to * define different Rcpp interfaces for each possible Selectivity function * */ class SelectivityInterfaceBase : public FIMSRcppInterfaceBase { - public: - static uint32_t id_g; /**< static id of the recruitment interface base*/ - uint32_t id; /**< id of the recruitment interface base */ - static std::map - selectivity_objects; /**< map associating the ids of +public: + static uint32_t id_g; /**< static id of the recruitment interface base*/ + uint32_t id; /**< id of the recruitment interface base */ + //live objects in C++ are objects that have been created and live in memory + static std::map + live_objects; /**< map associating the ids of SelectivityInterfaceBase to the objects */ - SelectivityInterfaceBase() { - this->id = SelectivityInterfaceBase::id_g++; - SelectivityInterfaceBase::selectivity_objects[this->id] = this; - FIMSRcppInterfaceBase::fims_interface_objects.push_back(this); - } + SelectivityInterfaceBase() { + this->id = SelectivityInterfaceBase::id_g++; + //Create instance of map: key is id and value is pointer to SelectivityInterfaceBase + SelectivityInterfaceBase::live_objects[this->id] = this; + FIMSRcppInterfaceBase::fims_interface_objects.push_back(this); + } - virtual ~SelectivityInterfaceBase() {} + virtual ~SelectivityInterfaceBase() { + } - /** @brief get the ID of the interface base object - **/ - virtual uint32_t get_id() = 0; + /** @brief get the ID of the interface base object + **/ + virtual uint32_t get_id() = 0; - /** - * @brief evaluate the function - * - */ - virtual double evaluate(double x) = 0; + /** + * @brief evaluate the function + * + */ + virtual double evaluate(double x) = 0; }; uint32_t SelectivityInterfaceBase::id_g = 1; std::map - SelectivityInterfaceBase::selectivity_objects; +SelectivityInterfaceBase::live_objects; /** * @brief Rcpp interface for logistic selectivity as an S4 object. To * instantiate from R: logistic_selectivity <- new(fims$logistic_selectivity) */ class LogisticSelectivityInterface : public SelectivityInterfaceBase { - public: - Parameter median; /**< the index value at which the response reaches .5 */ - Parameter slope; /**< the width of the curve at the median */ - - LogisticSelectivityInterface() : SelectivityInterfaceBase() {} - - virtual ~LogisticSelectivityInterface() {} - - /** @brief returns the id for the logistic selectivity interface */ - virtual uint32_t get_id() { return this->id; } - - /** @brief evaluate the logistic selectivity function - * @param x The independent variable in the logistic function (e.g., age or - * size in selectivity). - */ - virtual double evaluate(double x) { - fims::LogisticSelectivity LogisticSel; - - LogisticSel.median = this->median.value_m; - LogisticSel.slope = this->slope.value_m; - return LogisticSel.evaluate(x); - } - - /** @brief this adds the parameter values and derivatives to the TMB model - * object */ - virtual bool add_to_fims_tmb() { - std::shared_ptr > d0 = - fims::Information::GetInstance(); - - std::shared_ptr > ls0 = - std::make_shared >(); - - // set relative info - ls0->id = this->id; - ls0->median = this->median.value_m; - if (this->median.estimated_m) { - if (this->median.is_random_effect_m) { - d0->RegisterRandomEffect(ls0->median); - } else { - d0->RegisterParameter(ls0->median); - } - } - ls0->slope = this->slope.value_m; - if (this->slope.estimated_m) { - if (this->slope.is_random_effect_m) { - d0->RegisterRandomEffect(ls0->slope); - } else { - d0->RegisterParameter(ls0->slope); - } - } - // add to Information - d0->selectivity_models[ls0->id] = ls0; +public: + Parameter median; /**< the index value at which the response reaches .5 */ + Parameter slope; /**< the width of the curve at the median */ - std::shared_ptr > d1 = - fims::Information::GetInstance(); + LogisticSelectivityInterface() : SelectivityInterfaceBase() { - std::shared_ptr > ls1 = - std::make_shared >(); + } - // set relative info - ls1->id = this->id; - ls1->median = this->median.value_m; - if (this->median.estimated_m) { - if (this->median.is_random_effect_m) { - d1->RegisterRandomEffect(ls1->median); - } else { - d1->RegisterParameter(ls1->median); - } + virtual ~LogisticSelectivityInterface() { } - ls1->slope = this->slope.value_m; - if (this->slope.estimated_m) { - if (this->slope.is_random_effect_m) { - d1->RegisterRandomEffect(ls1->slope); - } else { - d1->RegisterParameter(ls1->slope); - } + + /** @brief returns the id for the logistic selectivity interface */ + virtual uint32_t get_id() { + return this->id; } - // add to Information - d1->selectivity_models[ls1->id] = ls1; + /** @brief evaluate the logistic selectivity function + * @param x The independent variable in the logistic function (e.g., age or + * size in selectivity). + */ + virtual double evaluate(double x) { + fims::LogisticSelectivity LogisticSel; + LogisticSel.median = this->median.value_m; + LogisticSel.slope = this->slope.value_m; + return LogisticSel.evaluate(x); + } - std::shared_ptr > d2 = - fims::Information::GetInstance(); +#ifdef TMB_MODEL - std::shared_ptr > ls2 = - std::make_shared >(); + template + bool add_to_fims_tmb_internal() { + std::shared_ptr > info = + fims::Information::GetInstance(); - // set relative info - ls2->id = this->id; - ls2->median = this->median.value_m; - if (this->median.estimated_m) { - if (this->median.is_random_effect_m) { - d2->RegisterRandomEffect(ls2->median); - } else { - d2->RegisterParameter(ls2->median); - } - } - ls2->slope = this->slope.value_m; - if (this->slope.estimated_m) { - if (this->slope.is_random_effect_m) { - d2->RegisterRandomEffect(ls2->slope); - } else { - d2->RegisterParameter(ls2->slope); - } - } + std::shared_ptr > selectivity = + std::make_shared >(); - // add to Information - d2->selectivity_models[ls2->id] = ls2; + // set relative info + selectivity->id = this->id; + selectivity->median = this->median.value_m; + if (this->median.estimated_m) { + if (this->median.is_random_effect_m) { + info->RegisterRandomEffect(selectivity->median); + } else { + info->RegisterParameter(selectivity->median); + } + } + selectivity->slope = this->slope.value_m; + if (this->slope.estimated_m) { + if (this->slope.is_random_effect_m) { + info->RegisterRandomEffect(selectivity->slope); + } else { + info->RegisterParameter(selectivity->slope); + } + } - std::shared_ptr > d3 = - fims::Information::GetInstance(); + // add to Information + info->selectivity_models[selectivity->id] = selectivity; - std::shared_ptr > ls3 = - std::make_shared >(); - - // set relative info - ls3->id = this->id; - ls3->median = this->median.value_m; - if (this->median.estimated_m) { - if (this->median.is_random_effect_m) { - d3->RegisterRandomEffect(ls3->median); - } else { - d3->RegisterParameter(ls3->median); - } - } - ls3->slope = this->slope.value_m; - if (this->slope.estimated_m) { - if (this->slope.is_random_effect_m) { - d3->RegisterRandomEffect(ls3->slope); - } else { - d3->RegisterParameter(ls3->slope); - } + return true; } - // add to Information - d3->selectivity_models[ls3->id] = ls3; + /** @brief this adds the parameter values and derivatives to the TMB model + * object */ + virtual bool add_to_fims_tmb() { + + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + + return true; + } - return true; - } +#endif }; /** @@ -202,221 +142,103 @@ class LogisticSelectivityInterface : public SelectivityInterfaceBase { * instantiate from R: logistic_selectivity <- new(fims$logistic_selectivity) */ class DoubleLogisticSelectivityInterface : public SelectivityInterfaceBase { - public: - Parameter median_asc; /**< the index value at which the response reaches .5 */ - Parameter slope_asc; /**< the width of the curve at the median */ - Parameter - median_desc; /**< the index value at which the response reaches .5 */ - Parameter slope_desc; /**< the width of the curve at the median */ - - DoubleLogisticSelectivityInterface() : SelectivityInterfaceBase() {} - - virtual ~DoubleLogisticSelectivityInterface() {} - - /** @brief returns the id for the double logistic selectivity interface */ - virtual uint32_t get_id() { return this->id; } - - /** @brief evaluate the double logistic selectivity function - * @param x The independent variable in the logistic function (e.g., age or - * size in selectivity). - */ - virtual double evaluate(double x) { - fims::DoubleLogisticSelectivity DoubleLogisticSel; - - DoubleLogisticSel.median_asc = this->median_asc.value_m; - DoubleLogisticSel.slope_asc = this->slope_asc.value_m; - DoubleLogisticSel.median_desc = this->median_desc.value_m; - DoubleLogisticSel.slope_desc = this->slope_desc.value_m; - return DoubleLogisticSel.evaluate(x); - } - - /** @brief this adds the parameter values and derivatives to the TMB model - * object */ - virtual bool add_to_fims_tmb() { - std::shared_ptr > d0 = - fims::Information::GetInstance(); - - std::shared_ptr > ls0 = - std::make_shared< - fims::DoubleLogisticSelectivity >(); - - // set relative info - ls0->id = this->id; - ls0->median_asc = this->median_asc.value_m; - if (this->median_asc.estimated_m) { - if (this->median_asc.is_random_effect_m) { - d0->RegisterRandomEffect(ls0->median_asc); - } else { - d0->RegisterParameter(ls0->median_asc); - } - } - ls0->slope_asc = this->slope_asc.value_m; - if (this->slope_asc.estimated_m) { - if (this->slope_asc.is_random_effect_m) { - d0->RegisterRandomEffect(ls0->slope_asc); - } else { - d0->RegisterParameter(ls0->slope_asc); - } - } - ls0->median_desc = this->median_desc.value_m; - if (this->median_desc.estimated_m) { - if (this->median_desc.is_random_effect_m) { - d0->RegisterRandomEffect(ls0->median_desc); - } else { - d0->RegisterParameter(ls0->median_desc); - } - } - ls0->slope_desc = this->slope_desc.value_m; - if (this->slope_desc.estimated_m) { - if (this->slope_desc.is_random_effect_m) { - d0->RegisterRandomEffect(ls0->slope_desc); - } else { - d0->RegisterParameter(ls0->slope_desc); - } - } - - // add to Information - d0->selectivity_models[ls0->id] = ls0; - - std::shared_ptr > d1 = - fims::Information::GetInstance(); - - std::shared_ptr > - ls1 = std::make_shared< - fims::DoubleLogisticSelectivity >(); - - // set relative info - ls1->id = this->id; - ls1->median_asc = this->median_asc.value_m; - if (this->median_asc.estimated_m) { - if (this->median_asc.is_random_effect_m) { - d1->RegisterRandomEffect(ls1->median_asc); - } else { - d1->RegisterParameter(ls1->median_asc); - } - } - ls1->slope_asc = this->slope_asc.value_m; - if (this->slope_asc.estimated_m) { - if (this->slope_asc.is_random_effect_m) { - d1->RegisterRandomEffect(ls1->slope_asc); - } else { - d1->RegisterParameter(ls1->slope_asc); - } - } - ls1->median_desc = this->median_desc.value_m; - if (this->median_desc.estimated_m) { - if (this->median_desc.is_random_effect_m) { - d1->RegisterRandomEffect(ls1->median_desc); - } else { - d1->RegisterParameter(ls1->median_desc); - } - } - ls1->slope_desc = this->slope_desc.value_m; - if (this->slope_desc.estimated_m) { - if (this->slope_desc.is_random_effect_m) { - d1->RegisterRandomEffect(ls1->slope_desc); - } else { - d1->RegisterParameter(ls1->slope_desc); - } - } - - // add to Information - d1->selectivity_models[ls1->id] = ls1; - - std::shared_ptr > d2 = - fims::Information::GetInstance(); - - std::shared_ptr > - ls2 = std::make_shared< - fims::DoubleLogisticSelectivity >(); - - // set relative info - ls2->id = this->id; - ls2->median_asc = this->median_asc.value_m; - if (this->median_asc.estimated_m) { - if (this->median_asc.is_random_effect_m) { - d2->RegisterRandomEffect(ls2->median_asc); - } else { - d2->RegisterParameter(ls2->median_asc); - } - } - ls2->slope_asc = this->slope_asc.value_m; - if (this->slope_asc.estimated_m) { - if (this->slope_asc.is_random_effect_m) { - d2->RegisterRandomEffect(ls2->slope_asc); - } else { - d2->RegisterParameter(ls2->slope_asc); - } - } - - ls2->median_desc = this->median_desc.value_m; - if (this->median_desc.estimated_m) { - if (this->median_desc.is_random_effect_m) { - d2->RegisterRandomEffect(ls2->median_desc); - } else { - d2->RegisterParameter(ls2->median_desc); - } - } - ls2->slope_desc = this->slope_desc.value_m; - if (this->slope_desc.estimated_m) { - if (this->slope_desc.is_random_effect_m) { - d2->RegisterRandomEffect(ls2->slope_desc); - } else { - d2->RegisterParameter(ls2->slope_desc); - } - } - - // add to Information - d2->selectivity_models[ls2->id] = ls2; - - std::shared_ptr > d3 = - fims::Information::GetInstance(); - - std::shared_ptr > - ls3 = std::make_shared< - fims::DoubleLogisticSelectivity >(); - - // set relative info - ls3->id = this->id; - ls3->median_asc = this->median_asc.value_m; - if (this->median_asc.estimated_m) { - if (this->median_asc.is_random_effect_m) { - d3->RegisterRandomEffect(ls3->median_asc); - } else { - d3->RegisterParameter(ls3->median_asc); - } - } - ls3->slope_asc = this->slope_asc.value_m; - if (this->slope_asc.estimated_m) { - if (this->slope_asc.is_random_effect_m) { - d3->RegisterRandomEffect(ls3->slope_asc); - } else { - d3->RegisterParameter(ls3->slope_asc); - } - } - - ls3->median_desc = this->median_desc.value_m; - if (this->median_desc.estimated_m) { - if (this->median_desc.is_random_effect_m) { - d3->RegisterRandomEffect(ls3->median_desc); - } else { - d3->RegisterParameter(ls3->median_desc); - } - } - ls3->slope_desc = this->slope_desc.value_m; - if (this->slope_desc.estimated_m) { - if (this->slope_desc.is_random_effect_m) { - d3->RegisterRandomEffect(ls3->slope_desc); - } else { - d3->RegisterParameter(ls3->slope_desc); - } - } - - // add to Information - d3->selectivity_models[ls3->id] = ls3; +public: + Parameter median_asc; /**< the index value at which the response reaches .5 */ + Parameter slope_asc; /**< the width of the curve at the median */ + Parameter + median_desc; /**< the index value at which the response reaches .5 */ + Parameter slope_desc; /**< the width of the curve at the median */ + + DoubleLogisticSelectivityInterface() : SelectivityInterfaceBase() { + } + + virtual ~DoubleLogisticSelectivityInterface() { + } + + /** @brief returns the id for the double logistic selectivity interface */ + virtual uint32_t get_id() { + return this->id; + } + + /** @brief evaluate the double logistic selectivity function + * @param x The independent variable in the logistic function (e.g., age or + * size in selectivity). + */ + virtual double evaluate(double x) { + fims::DoubleLogisticSelectivity DoubleLogisticSel; + DoubleLogisticSel.median_asc = this->median_asc.value_m; + DoubleLogisticSel.slope_asc = this->slope_asc.value_m; + DoubleLogisticSel.median_desc = this->median_desc.value_m; + DoubleLogisticSel.slope_desc = this->slope_desc.value_m; + return DoubleLogisticSel.evaluate(x); + } + +#ifdef TMB_MODEL + + template + bool add_to_fims_tmb_internal() { + std::shared_ptr > info = + fims::Information::GetInstance(); + + std::shared_ptr > selectivity = + std::make_shared< + fims::DoubleLogisticSelectivity >(); + + // set relative info + selectivity->id = this->id; + selectivity->median_asc = this->median_asc.value_m; + if (this->median_asc.estimated_m) { + if (this->median_asc.is_random_effect_m) { + info->RegisterRandomEffect(selectivity->median_asc); + } else { + info->RegisterParameter(selectivity->median_asc); + } + } + selectivity->slope_asc = this->slope_asc.value_m; + if (this->slope_asc.estimated_m) { + if (this->slope_asc.is_random_effect_m) { + info->RegisterRandomEffect(selectivity->slope_asc); + } else { + info->RegisterParameter(selectivity->slope_asc); + } + } + selectivity->median_desc = this->median_desc.value_m; + if (this->median_desc.estimated_m) { + if (this->median_desc.is_random_effect_m) { + info->RegisterRandomEffect(selectivity->median_desc); + } else { + info->RegisterParameter(selectivity->median_desc); + } + } + selectivity->slope_desc = this->slope_desc.value_m; + if (this->slope_desc.estimated_m) { + if (this->slope_desc.is_random_effect_m) { + info->RegisterRandomEffect(selectivity->slope_desc); + } else { + info->RegisterParameter(selectivity->slope_desc); + } + } + + // add to Information + info->selectivity_models[selectivity->id] = selectivity; + + return true; + } + + /** @brief this adds the parameter values and derivatives to the TMB model + * object */ + virtual bool add_to_fims_tmb() { + + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + + return true; + } + +#endif - return true; - } }; #endif \ No newline at end of file diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp index df31ab35..7928d7f5 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp @@ -18,37 +18,40 @@ * */ class DistributionsInterfaceBase : public FIMSRcppInterfaceBase { - public: - static uint32_t - id_g; /**< static id of the DistributionsInterfaceBase object */ - uint32_t id; /**< local id of the DistributionsInterfaceBase object */ - static std::map live_objects; /**< +public: + static uint32_t + id_g; /**< static id of the DistributionsInterfaceBase object */ + uint32_t id; /**< local id of the DistributionsInterfaceBase object */ + //live objects in C++ are objects that have been created and live in memory + static std::map live_objects; /**< map relating the ID of the DistributionsInterfaceBase to the DistributionsInterfaceBase objects */ - DistributionsInterfaceBase() { - this->id = DistributionsInterfaceBase::id_g++; - DistributionsInterfaceBase::live_objects[this->id] = this; - FIMSRcppInterfaceBase::fims_interface_objects.push_back(this); - } + DistributionsInterfaceBase() { + this->id = DistributionsInterfaceBase::id_g++; + //Create instance of map: key is id and value is pointer to DistributionsInterfaceBase + DistributionsInterfaceBase::live_objects[this->id] = this; + FIMSRcppInterfaceBase::fims_interface_objects.push_back(this); + } - virtual ~DistributionsInterfaceBase() {} + virtual ~DistributionsInterfaceBase() { + } - /** @brief get_id method for child distribution interface objects to inherit - * **/ - virtual uint32_t get_id() = 0; + /** @brief get_id method for child distribution interface objects to inherit + * **/ + virtual uint32_t get_id() = 0; - /** @brief evaluate method for child distribution interface objects to inherit - * **/ - virtual double evaluate(bool do_log) = 0; + /** @brief evaluate method for child distribution interface objects to inherit + * **/ + virtual double evaluate(bool do_log) = 0; }; uint32_t DistributionsInterfaceBase::id_g = - 1; /**< static id of the DistributionsInterfaceBase object */ + 1; /**< static id of the DistributionsInterfaceBase object */ std::map /**< local id of the DistributionsInterfaceBase object */ - DistributionsInterfaceBase::live_objects; /**< +DistributionsInterfaceBase +*> /**< local id of the DistributionsInterfaceBase object */ +DistributionsInterfaceBase::live_objects; /**< map relating the ID of the DistributionsInterfaceBase to the DistributionsInterfaceBase objects */ @@ -59,95 +62,73 @@ std::mapid; } - - virtual ~DnormDistributionsInterface() {} - - /** - * @brief Evaluate normal probability density function, default returns the - * log of the pdf - * - * @tparam T - * @return log pdf - */ - virtual double evaluate(bool do_log) { - fims::Dnorm dnorm; - dnorm.x = this->x.value_m; - dnorm.mean = this->mean.value_m; - dnorm.sd = this->sd.value_m; - return dnorm.evaluate(do_log); - } - - /** - * @brief adds the dnorm distribution and its parameters to the TMB model - */ - virtual bool add_to_fims_tmb() { - std::shared_ptr> d0 = - fims::Information::GetInstance(); - - std::shared_ptr> model0 = - std::make_shared>(); - - // interface to data/parameter value - model0->id = this->id; - model0->x = this->x.value_m; - // set relative info - model0->mean = this->mean.value_m; - model0->sd = this->sd.value_m; - - d0->distribution_models[model0->id] = model0; - - std::shared_ptr> d1 = - fims::Information::GetInstance(); - - std::shared_ptr> model1 = - std::make_shared>(); - - // interface to data/parameter first derivative - model1->id = this->id; - model1->x = this->x.value_m; - model1->mean = this->mean.value_m; - model1->sd = this->sd.value_m; - - d1->distribution_models[model1->id] = model1; - - std::shared_ptr> d2 = - fims::Information::GetInstance(); - - std::shared_ptr> model2 = - std::make_shared>(); - - // interface to data/parameter second derivative - model2->id = this->id; - model2->x = this->x.value_m; - model2->mean = this->mean.value_m; - model2->sd = this->sd.value_m; - - d2->distribution_models[model2->id] = model2; - - std::shared_ptr> d3 = - fims::Information::GetInstance(); - - std::shared_ptr> model3 = - std::make_shared>(); - - // interface to data/parameter third derivative - model3->id = this->id; - model3->x = this->x.value_m; - model3->mean = this->mean.value_m; - model3->sd = this->sd.value_m; - - d3->distribution_models[model3->id] = model3; - - return true; - } +public: + Parameter x; /**< observed data */ + Parameter mean; /**< mean of x for the normal distribution **/ + Parameter sd; /**< sd of x for the normal distribution **/ + + DnormDistributionsInterface() : DistributionsInterfaceBase() { + } + + virtual uint32_t get_id() { + return this->id; + } + + virtual ~DnormDistributionsInterface() { + } + + /** + * @brief Evaluate normal probability density function, default returns the + * log of the pdf + * + * @tparam T + * @return log pdf + */ + virtual double evaluate(bool do_log) { + fims::Dnorm dnorm; + dnorm.x = this->x.value_m; + dnorm.mean = this->mean.value_m; + dnorm.sd = this->sd.value_m; + return dnorm.evaluate(do_log); + } + +#ifdef TMB_MODEL + + template + bool add_to_fims_tmb_internal() { + std::shared_ptr> info = + fims::Information::GetInstance(); + + std::shared_ptr> distribution = + std::make_shared> (); + + // interface to data/parameter value + distribution->id = this->id; + distribution->x = this->x.value_m; + // set relative info + distribution->mean = this->mean.value_m; + distribution->sd = this->sd.value_m; + + info->distribution_models[distribution->id] = distribution; + + return true; + + } + + /** + * @brief adds the dnorm distribution and its parameters to the TMB model + */ + virtual bool add_to_fims_tmb() { + + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + + return true; + } + +#endif }; /** @@ -157,100 +138,74 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase { * */ class DlnormDistributionsInterface : public DistributionsInterfaceBase { - public: - Parameter x; /*!< observation */ - Parameter meanlog; /*!< mean of the distribution of log(x) */ - Parameter sdlog; /*!< standard deviation of the distribution of log(x) */ - - DlnormDistributionsInterface() : DistributionsInterfaceBase() {} - - virtual ~DlnormDistributionsInterface() {} - - /** - * @brief get the id of the Dlnorm distributions interface class object - */ - virtual uint32_t get_id() { return this->id; } - - /** - * @brief Evaluate lognormal probability density function, default returns the - * log of the pdf - * - * @tparam T - * @return log pdf - */ - virtual double evaluate(bool do_log) { - fims::Dlnorm dlnorm; - dlnorm.x = this->x.value_m; - dlnorm.meanlog = this->meanlog.value_m; - dlnorm.sdlog = this->sdlog.value_m; - return dlnorm.evaluate(do_log); - } - - /** - * @brief adds the dlnorm distribution and its parameters to the TMB model - */ - virtual bool add_to_fims_tmb() { - std::shared_ptr> d0 = - fims::Information::GetInstance(); - - std::shared_ptr> model0 = - std::make_shared>(); - - // set relative info - model0->id = this->id; - model0->x = this->x.value_m; - model0->meanlog = this->meanlog.value_m; - model0->sdlog = this->sdlog.value_m; - - d0->distribution_models[model0->id] = model0; - - // base model - std::shared_ptr> d1 = - fims::Information::GetInstance(); - - std::shared_ptr> model1 = - std::make_shared>(); - - // set relative info - model1->id = this->id; - model1->x = this->x.value_m; - model1->meanlog = this->meanlog.value_m; - model1->sdlog = this->sdlog.value_m; - - d1->distribution_models[model1->id] = model1; - - // base model - std::shared_ptr> d2 = - fims::Information::GetInstance(); - - std::shared_ptr> model2 = - std::make_shared>(); - - // set relative info - model2->id = this->id; - model2->x = this->x.value_m; - model2->meanlog = this->meanlog.value_m; - model2->sdlog = this->sdlog.value_m; - - d2->distribution_models[model2->id] = model2; - - // base model - std::shared_ptr> d3 = - fims::Information::GetInstance(); - - std::shared_ptr> model3 = - std::make_shared>(); - - // set relative info - model3->id = this->id; - model3->x = this->x.value_m; - model3->meanlog = this->meanlog.value_m; - model3->sdlog = this->sdlog.value_m; - - d3->distribution_models[model3->id] = model3; - - return true; - } +public: + Parameter x; /*!< observation */ + Parameter meanlog; /*!< mean of the distribution of log(x) */ + Parameter sdlog; /*!< standard deviation of the distribution of log(x) */ + + DlnormDistributionsInterface() : DistributionsInterfaceBase() { + } + + virtual ~DlnormDistributionsInterface() { + } + + /** + * @brief get the id of the Dlnorm distributions interface class object + */ + virtual uint32_t get_id() { + return this->id; + } + + /** + * @brief Evaluate lognormal probability density function, default returns the + * log of the pdf + * + * @tparam T + * @return log pdf + */ + virtual double evaluate(bool do_log) { + fims::Dlnorm dlnorm; + dlnorm.x = this->x.value_m; + dlnorm.meanlog = this->meanlog.value_m; + dlnorm.sdlog = this->sdlog.value_m; + return dlnorm.evaluate(do_log); + } + +#ifdef TMB_MODEL + + template + bool add_to_fims_tmb_internal() { + std::shared_ptr > info = + fims::Information::GetInstance(); + + std::shared_ptr < fims::Dlnorm > distribution = + std::make_shared > (); + + // set relative info + distribution->id = this->id; + distribution->x = this->x.value_m; + distribution->meanlog = this->meanlog.value_m; + distribution->sdlog = this->sdlog.value_m; + + info->distribution_models[distribution->id] = distribution; + + return true; + } + + /** + * @brief adds the dlnorm distribution and its parameters to the TMB model + */ + virtual bool add_to_fims_tmb() { + + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + + return true; + } + +#endif }; /** @@ -260,116 +215,79 @@ class DlnormDistributionsInterface : public DistributionsInterfaceBase { * */ // template + class DmultinomDistributionsInterface : public DistributionsInterfaceBase { - public: - Rcpp::NumericVector x; /*!< Vector of length K of integers */ - Rcpp::NumericVector p; /*!< Vector of length K, specifying the probability +public: + Rcpp::NumericVector x; /*!< Vector of length K of integers */ + Rcpp::NumericVector p; /*!< Vector of length K, specifying the probability for the K classes (note, unlike in R these must sum to 1). */ - DmultinomDistributionsInterface() : DistributionsInterfaceBase() {} - - virtual ~DmultinomDistributionsInterface() {} - - virtual uint32_t get_id() { return this->id; } - - /** - * @brief Evaluate multinom probability density function, default returns the - * log of the pdf - * - * @tparam T - * @return log pdf - */ - virtual double evaluate(bool do_log) { - fims::Dmultinom dmultinom; - // Decale TMBVector in this scope - typedef - typename fims::ModelTraits::EigenVector TMBVector; - dmultinom.x = TMBVector(x.size()); // Vector from TMB - dmultinom.p = TMBVector(p.size()); // Vector from TMB - for (int i = 0; i < x.size(); i++) { - dmultinom.x[i] = x[i]; - dmultinom.p[i] = p[i]; + DmultinomDistributionsInterface() : DistributionsInterfaceBase() { } - return dmultinom.evaluate(do_log); - } - - virtual bool add_to_fims_tmb() { - typedef typename fims::ModelTraits::EigenVector Vector0; - std::shared_ptr> d0 = - fims::Information::GetInstance(); - std::shared_ptr> model0 = - std::make_shared>(); - - model0->id = this->id; - model0->x = Vector0(x.size()); - model0->p = Vector0(p.size()); - - for (int i = 0; i < x.size(); i++) { - model0->x[i] = x[i]; - model0->p[i] = p[i]; + virtual ~DmultinomDistributionsInterface() { } - d0->distribution_models[model0->id] = model0; - - typedef - typename fims::ModelTraits::EigenVector Vector1; - std::shared_ptr> d1 = - fims::Information::GetInstance(); - - std::shared_ptr> model1 = - std::make_shared>(); - - model1->id = this->id; - model1->x = Vector1(x.size()); - model1->p = Vector1(p.size()); + virtual uint32_t get_id() { + return this->id; + } - for (int i = 0; i < x.size(); i++) { - model1->x[i] = x[i]; - model1->p[i] = p[i]; + /** + * @brief Evaluate multinom probability density function, default returns the + * log of the pdf + * + * @tparam T + * @return log pdf + */ + virtual double evaluate(bool do_log) { + fims::Dmultinom dmultinom; + // Decale TMBVector in this scope + typedef + typename fims::ModelTraits::EigenVector Vector; + dmultinom.x = Vector(x.size()); // Vector from TMB + dmultinom.p = Vector(p.size()); // Vector from TMB + for (int i = 0; i < x.size(); i++) { + dmultinom.x[i] = x[i]; + dmultinom.p[i] = p[i]; + } + return dmultinom.evaluate(do_log); } - d1->distribution_models[model1->id] = model1; +#ifdef TMB_MODEL - typedef - typename fims::ModelTraits::EigenVector Vector2; - std::shared_ptr> d2 = - fims::Information::GetInstance(); + template + bool add_to_fims_tmb_internal() { + typedef typename fims::ModelTraits::EigenVector Vector; + std::shared_ptr > info = + fims::Information::GetInstance(); - std::shared_ptr> model2 = - std::make_shared>(); + std::shared_ptr> distribution = + std::make_shared > (); - model2->id = this->id; - model2->x = Vector2(x.size()); - model2->p = Vector2(p.size()); + distribution->id = this->id; + distribution->x = Vector(x.size()); + distribution->p = Vector(p.size()); - for (int i = 0; i < x.size(); i++) { - model2->x[i] = x[i]; - model2->p[i] = p[i]; - } + for (int i = 0; i < x.size(); i++) { + distribution->x[i] = x[i]; + distribution->p[i] = p[i]; + } - d2->distribution_models[model2->id] = model2; + info->distribution_models[distribution->id] = distribution; - typedef - typename fims::ModelTraits::EigenVector Vector3; - std::shared_ptr> d3 = - fims::Information::GetInstance(); + return true; + } - std::shared_ptr> model3 = - std::make_shared>(); + virtual bool add_to_fims_tmb() { - model3->id = this->id; - model3->x = Vector3(x.size()); - model3->p = Vector3(p.size()); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); + this->add_to_fims_tmb_internal(); - for (int i = 0; i < x.size(); i++) { - model3->x[i] = x[i]; - model3->p[i] = p[i]; + return true; } - d3->distribution_models[model3->id] = model3; - - return true; - } +#endif }; #endif