From 88b04383a5f5ec65771bfc6dfe93e36d75a2e23b Mon Sep 17 00:00:00 2001 From: Andrea-Havron-NOAA <85530309+Andrea-Havron-NOAA@users.noreply.github.com> Date: Thu, 1 Aug 2024 15:49:59 +0000 Subject: [PATCH] fix bugs --- inst/include/common/information.hpp | 8 +- inst/include/common/model.hpp | 119 +++++++++--------- .../distributions/functors/lognormal_lpdf.hpp | 2 +- .../functors/multinomial_lpmf.hpp | 1 + .../distributions/functors/normal_lpdf.hpp | 2 +- .../population_dynamics/fleet/fleet.hpp | 4 +- 6 files changed, 70 insertions(+), 66 deletions(-) diff --git a/inst/include/common/information.hpp b/inst/include/common/information.hpp index f93ad4999..8ad4ad8e9 100644 --- a/inst/include/common/information.hpp +++ b/inst/include/common/information.hpp @@ -158,7 +158,7 @@ class Information { void setup_priors(){ for(density_components_iterator it = density_components.begin(); it!= density_components.end(); ++it){ - std::shared_ptr > n = (*it).second; + std::shared_ptr > n = (*it).second; if(n->input_type == "prior"){ variable_map_iterator vmit; vmit = this->variable_map.find(n->key[0]); @@ -173,7 +173,7 @@ class Information { } void setup_random_effects(){ for(density_components_iterator it = this->density_components.begin(); it!= this->density_components.end(); ++it){ - std::shared_ptr > n = (*it).second; + std::shared_ptr > n = (*it).second; if(n->input_type == "re"){ variable_map_iterator vmit; vmit = this->variable_map.find(n->key[0]); @@ -188,7 +188,7 @@ class Information { } void setup_data(){ for(density_components_iterator it = this->density_components.begin(); it!= this->density_components.end(); ++it){ - std::shared_ptr > n = (*it).second; + std::shared_ptr > n = (*it).second; if(n->input_type == "data"){ variable_map_iterator vmit; vmit = this->variable_map.find(n->key[0]); @@ -333,7 +333,7 @@ class Information { exit(1); } // end set selectivity - + } INFO_LOG << "Initializing population objects for " << this->populations.size() << " populations." << std::endl; diff --git a/inst/include/common/model.hpp b/inst/include/common/model.hpp index d295011cd..a132b4e4b 100644 --- a/inst/include/common/model.hpp +++ b/inst/include/common/model.hpp @@ -34,10 +34,10 @@ class Model { // may need singleton std::shared_ptr > fims_information; /**< Create a shared fims_information as a pointer to Information*/ - std::map > > + std::map > > density_components; typedef typename std::map< - uint32_t, std::shared_ptr > >::iterator + uint32_t, std::shared_ptr > >::iterator density_components_iterator; #ifdef TMB_MODEL @@ -92,13 +92,14 @@ class Model { // may need singleton #endif // Loop over densities and evaluate joint negative log densities for priors size_t n_priors = 0; - for(density_components_iterator it = this->density_components.begin(); it!= this->density_components.end(); ++it){ - std::shared_ptr > n = (*it).second; + typename density_components_iterator d_it; + for(d_it = this->density_components.begin(); d_it!= this->density_components.end(); ++d_it){ + std::shared_ptr > d = (*d_it).second; #ifdef TMB_MODEL - n->of = this->of; + d->of = this->of; #endif - if(n->input_type == "prior"){ - jnll -= n->evaluate(); + if(d->input_type == "prior"){ + jnll -= d->evaluate(); n_priors += 1; } } @@ -109,37 +110,32 @@ class Model { // may need singleton // Loop over populations and evaluate recruitment component - typename fims_info::Information::population_iterator it; + typename fims_info::Information::population_iterator p_it; MODEL_LOG << "Evaluating recruitment expected values for " << this->fims_information->populations.size() << " populations." << std::endl; - for (it = this->fims_information->populations.begin(); - it != this->fims_information->populations.end(); ++it) { - //(*it).second points to the Population module - MODEL_LOG << "Setting up pointer to population " << (*it).second->id + for (p_it = this->fims_information->populations.begin(); + p_it != this->fims_information->populations.end(); ++p_it) { + //(*p_it).second points to the Population module + std::shared_ptr > p = (*p_it).second; + MODEL_LOG << "Setting up pointer to population " << p->id << "." << std::endl; // Prepare recruitment - (*it).second->recruitment->Prepare(); + p->recruitment->Prepare(); MODEL_LOG << "Recruitment for population successfully prepared" << std::endl; -// link to TMB objective function -#ifdef TMB_MODEL - (*it).second->of = this->of; -#endif - // Evaluate population - (*it).second->Evaluate(); } // Loop over densities and evaluate joint negative log-likelihoods for random effects size_t n_random_effects = 0; - for(density_components_iterator it = this->density_components.begin(); it!= this->density_components.end(); ++it){ - std::shared_ptr > n = (*it).second; + for(d_it = this->density_components.begin(); d_it!= this->density_components.end(); ++d_it){ + std::shared_ptr > d = (*d_it).second; #ifdef TMB_MODEL - n->of = this->of; + d->of = this->of; #endif - if(n->input_type == "re"){ - jnll -= n->evaluate(); + if(d->input_type == "re"){ + jnll -= d->evaluate(); n_random_effects += 1; } } @@ -148,69 +144,76 @@ class Model { // may need singleton << std::endl; // Loop over and evaluate populations - typename fims_info::Information::population_iterator it; MODEL_LOG << "Evaluating expected values for " << this->fims_information->populations.size() << " populations." << std::endl; - for (it = this->fims_information->populations.begin(); - it != this->fims_information->populations.end(); ++it) { - //(*it).second points to the Population module + for (p_it = this->fims_information->populations.begin(); + p_it != this->fims_information->populations.end(); ++it) { + //(*p_it).second points to the Population module + std::shared_ptr > p = (*p_it).second; + // link to TMB objective function +#ifdef TMB_MODEL + p->of = this->of; +#endif // Evaluate population - (*it).second->Evaluate(); + p->Evaluate(); } - + typename fims_info::Information::population_iterator f_it; // Loop over fleets/surveys, and evaluate age comp and index expected values - for (jt = this->fims_information->fleets.begin(); - jt != this->fims_information->fleets.end(); ++jt) { - //(*jt).second points to each individual Fleet module + for (f_it = this->fims_information->fleets.begin(); + f_it != this->fims_information->fleets.end(); ++f_it) { + //(*f_it).second points to each individual Fleet module + std::shared_ptr > f = (*f_it).second; #ifdef TMB_MODEL - (*jt).second->of = this->of; + f->of = this->of; #endif - MODEL_LOG << "Setting up pointer to fleet " << (*jt).second->id << "." + MODEL_LOG << "Setting up pointer to fleet " << f->id << "." << std::endl; - (*jt).second->evaluate_age_comp(); - (*jt).second->evaluate_index() + f->evaluate_age_comp(); + f->evaluate_index() } // Loop over and evaluate data joint negative log-likelihoods - for(density_components_iterator it = this->density_components.begin(); it!= this->density_components.end(); ++it){ - std::shared_ptr > n = (*it).second; + for(d_it = this->density_components.begin(); d_it!= this->density_components.end(); ++d_it){ + std::shared_ptr > d = (*d_it).second; #ifdef TMB_MODEL - n->of = this->of; - n->keep = this->keep; + d->of = this->of; + d->keep = this->keep; #endif - if(n->input_type == "data"){ - jnll -= n->evaluate(); + if(d->input_type == "data"){ + jnll -= d->evaluate(); } } // initiate population index for structuring report out objects int pop_idx = 0; - for (it = this->fims_information->populations.begin(); - it != this->fims_information->populations.end(); ++it) { + for (p_it = this->fims_information->populations.begin(); + p_it != this->fims_information->populations.end(); ++p_it) { + std::shared_ptr > p = (*p_it).second; #ifdef TMB_MODEL - naa(pop_idx) = vector((*it).second->numbers_at_age); - ssb(pop_idx) = vector((*it).second->spawning_biomass); + naa(pop_idx) = vector(p->numbers_at_age); + ssb(pop_idx) = vector(p->spawning_biomass); log_recruit_dev(pop_idx) = - vector((*it).second->recruitment->log_recruit_devs); - recruitment(pop_idx) = vector((*it).second->expected_recruitment); - biomass(pop_idx) = vector((*it).second->biomass); - M(pop_idx) = vector((*it).second->M); + vector(p->recruitment->log_recruit_devs); + recruitment(pop_idx) = vector(p->expected_recruitment); + biomass(pop_idx) = vector(p->biomass); + M(pop_idx) = vector(p->M); #endif pop_idx += 1; } // initiate fleet index for structuring report out objects int fleet_idx = 0; - for (jt = this->fims_information->fleets.begin(); - jt != this->fims_information->fleets.end(); ++jt) { + for (f_it = this->fims_information->fleets.begin(); + f_it != this->fims_information->fleets.end(); ++f_it) { + std::shared_ptr > f = (*f_it).second; #ifdef TMB_MODEL - exp_index(fleet_idx) = (*jt).second->expected_index; - exp_catch(fleet_idx) = (*jt).second->expected_catch; - F_mort(fleet_idx) = (*jt).second->Fmort; - cnaa(fleet_idx) = (*jt).second->catch_numbers_at_age; - cwaa(fleet_idx) = (*jt).second->catch_weight_at_age; + exp_index(fleet_idx) = f->expected_index; + exp_catch(fleet_idx) = f->expected_catch; + F_mort(fleet_idx) = f->Fmort; + cnaa(fleet_idx) = f->catch_numbers_at_age; + cwaa(fleet_idx) = f->catch_weight_at_age; #endif fleet_idx += 1; } diff --git a/inst/include/distributions/functors/lognormal_lpdf.hpp b/inst/include/distributions/functors/lognormal_lpdf.hpp index aa0a67d0e..ce6ab6e01 100644 --- a/inst/include/distributions/functors/lognormal_lpdf.hpp +++ b/inst/include/distributions/functors/lognormal_lpdf.hpp @@ -86,7 +86,7 @@ namespace fims_distributions if(this->x->at(i) != this->x->na_value){ this->lpdf_vec[i] = dnorm(this->x->at(i), logmu[i], logsd[i], true) - this->x->at(i); } else { - this->lpdf_vec[i] = 0 + this->lpdf_vec[i] = 0; } } else { // this->lpdf_vec[i] = this->keep[i] * -dnorm(this->x[i], logmu[i], logsd[i], true); diff --git a/inst/include/distributions/functors/multinomial_lpmf.hpp b/inst/include/distributions/functors/multinomial_lpmf.hpp index 3b96b914e..914e14c55 100644 --- a/inst/include/distributions/functors/multinomial_lpmf.hpp +++ b/inst/include/distributions/functors/multinomial_lpmf.hpp @@ -84,6 +84,7 @@ namespace fims_distributions } if(!containsNA){ for (size_t j = 0; j < dims[1]; j++){ + size_t idx = (i * dims[1]) + j; x_vector[j] = this->x->at(i, j); prob_vector[j] = this->expected_value[idx]; } diff --git a/inst/include/distributions/functors/normal_lpdf.hpp b/inst/include/distributions/functors/normal_lpdf.hpp index 57c18e04e..c671e350f 100644 --- a/inst/include/distributions/functors/normal_lpdf.hpp +++ b/inst/include/distributions/functors/normal_lpdf.hpp @@ -79,7 +79,7 @@ struct NormalLPDF : public DensityComponentBase { if(this->x->at(i) != this->x->na_value){ this->lpdf_vec[i] = dnorm(this->x->at(i), mu[i], sd[i], true); } else { - this->lpdf_vec[i] = 0 + this->lpdf_vec[i] = 0; } } else { diff --git a/inst/include/population_dynamics/fleet/fleet.hpp b/inst/include/population_dynamics/fleet/fleet.hpp index 63b3b4c7b..75870c7f8 100644 --- a/inst/include/population_dynamics/fleet/fleet.hpp +++ b/inst/include/population_dynamics/fleet/fleet.hpp @@ -154,7 +154,7 @@ struct Fleet : public fims_model_object::FIMSObject { } } - void const Type evaluate_age_comp() { + void evaluate_age_comp() { for (size_t y = 0; y < this->nyears; y++) { Type sum = 0.0; for (size_t a = 0; a < this->nages; a++) { @@ -170,7 +170,7 @@ struct Fleet : public fims_model_object::FIMSObject { } } - void const Type evaluate_index() { + void evaluate_index() { for(size_t i=0; iexpected_index.size(); i++){ expected_index[i] = log(this->expected_index[i]); }