FIMS  v0.9.3
Loading...
Searching...
No Matches
rcpp_population.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_POPULATION_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_POPULATION_HPP
11
13#include "../../../population_dynamics/population/population.hpp"
14
20 public:
24 static uint32_t id_g;
34 static std::map<uint32_t, std::shared_ptr<PopulationInterfaceBase>>
36
52 /* Create instance of map: key is id and value is pointer to
53 PopulationInterfaceBase */
54 // PopulationInterfaceBase::live_objects[this->id] = this;
55 }
56
64
69
73 virtual uint32_t get_id() = 0;
74};
75
81 public:
93 std::shared_ptr<std::set<uint32_t>> fleet_ids;
97 typedef typename std::set<uint32_t>::iterator fleet_ids_iterator;
155 SharedString name = fims::to_string("NA");
156
161 this->proportion_female[0].initial_value_m = static_cast<double>(0.5);
162 this->proportion_female[0].estimation_type_m.set("constant");
163 this->fleet_ids = std::make_shared<std::set<uint32_t>>();
164 std::shared_ptr<PopulationInterface> population =
165 std::make_shared<PopulationInterface>(*this);
168 }
169
193
198
203 virtual uint32_t get_id() { return this->id; }
204
209 void SetName(const std::string &name) { this->name.set(name); }
210
215 std::string GetName() const { return this->name.get(); }
216
222 this->maturity_id.set(maturity_id);
223 }
224
229 void SetGrowthID(uint32_t growth_id) { this->growth_id.set(growth_id); }
230
236 this->recruitment_id.set(recruitment_id);
237 }
238
243 void AddFleet(uint32_t fleet_id) { this->fleet_ids->insert(fleet_id); }
244
249 virtual void finalize() {
250 if (this->finalized) {
251 // log warning that finalize has been called more than once.
252 FIMS_WARNING_LOG("Population " + fims::to_string(this->id) +
253 " has been finalized already.");
254 }
255
256 this->finalized = true; // indicate this has been called already
257
258 std::shared_ptr<fims_info::Information<double>> info =
260
262
263 it = info->populations.find(this->id);
264
265 std::shared_ptr<fims_popdy::Population<double>> pop =
266 info->populations[this->id];
267 it = info->populations.find(this->id);
268 if (it == info->populations.end()) {
269 FIMS_WARNING_LOG("Population " + fims::to_string(this->id) +
270 " not found in Information.");
271 return;
272 } else {
273 for (size_t i = 0; i < this->log_M.size(); i++) {
274 if (this->log_M[i].estimation_type_m.get() == "constant") {
275 this->log_M[i].final_value_m = this->log_M[i].initial_value_m;
276 } else {
277 this->log_M[i].final_value_m = pop->log_M[i];
278 }
279 }
280
281 for (size_t i = 0; i < this->log_f_multiplier.size(); i++) {
282 if (this->log_f_multiplier[i].estimation_type_m.get() == "constant") {
283 this->log_f_multiplier[i].final_value_m =
284 this->log_f_multiplier[i].initial_value_m;
285 } else {
286 this->log_f_multiplier[i].final_value_m = pop->log_f_multiplier[i];
287 }
288 }
289
290 for (size_t i = 0; i < this->log_init_naa.size(); i++) {
291 if (this->log_init_naa[i].estimation_type_m.get() == "constant") {
292 this->log_init_naa[i].final_value_m =
293 this->log_init_naa[i].initial_value_m;
294 } else {
295 this->log_init_naa[i].final_value_m = pop->log_init_naa[i];
296 }
297 }
298
299 for (size_t i = 0; i < this->proportion_female.size(); i++) {
300 if (this->proportion_female[i].estimation_type_m.get() == "constant") {
301 this->proportion_female[i].final_value_m =
302 this->proportion_female[i].initial_value_m;
303 } else {
304 this->proportion_female[i].final_value_m =
305 pop->proportion_female.get_force_scalar(i);
306 }
307 }
308 }
309 }
310
311#ifdef TMB_MODEL
312
313 template <typename Type>
315 std::shared_ptr<fims_info::Information<Type>> info =
317
318 std::shared_ptr<fims_popdy::Population<Type>> population =
319 std::make_shared<fims_popdy::Population<Type>>();
320
321 std::stringstream ss;
322
323 // set relative info
324 population->id = this->id;
325 population->n_years = this->n_years.get();
326 population->n_fleets = this->n_fleets.get();
327 // only define ages if n_ages greater than 0
328 if (this->n_ages.get() > 0) {
329 population->n_ages = this->n_ages.get();
330 if (static_cast<size_t>(this->n_ages.get()) == this->ages.size()) {
331 population->ages.resize(this->n_ages.get());
332 } else {
333 throw std::invalid_argument(
334 "The size of the ages vector for population " +
335 fims::to_string(this->id) + " is not equal to n_ages.");
336 }
337 }
338
339 fleet_ids_iterator it;
340 for (it = this->fleet_ids->begin(); it != this->fleet_ids->end(); it++) {
341 population->fleet_ids.insert(*it);
342 }
343
344 population->growth_id = this->growth_id.get();
345 population->recruitment_id = this->recruitment_id.get();
346 population->maturity_id = this->maturity_id.get();
347 population->log_M.resize(this->log_M.size());
348
349 if (this->log_f_multiplier.size() ==
350 static_cast<size_t>(this->n_years.get())) {
351 population->log_f_multiplier.resize(this->log_f_multiplier.size());
352 } else {
354 "The log_f_multiplier vector is not of size n_years. Filling with "
355 "zeros.");
356 this->log_f_multiplier.resize((this->n_years.get()));
357 for (size_t i = 0; i < log_f_multiplier.size(); i++) {
358 this->log_f_multiplier[i].initial_value_m = static_cast<double>(0.0);
359 this->log_f_multiplier[i].estimation_type_m.set("constant");
360 }
361 population->log_f_multiplier.resize(this->log_f_multiplier.size());
362 }
363
364 if (this->spawning_biomass_ratio.size() ==
365 static_cast<size_t>(this->n_years.get() + 1)) {
366 population->spawning_biomass_ratio.resize(
367 this->spawning_biomass_ratio.size());
368 } else {
370 "Setting spawning_biomass_ratio vector to size n_years + 1.");
371 this->spawning_biomass_ratio.resize((this->n_years.get() + 1));
372 population->spawning_biomass_ratio.resize(
373 this->spawning_biomass_ratio.size());
374 }
375 info->variable_map[this->spawning_biomass_ratio.id_m] =
377
378 population->log_init_naa.resize(this->log_init_naa.size());
379 for (size_t i = 0; i < log_M.size(); i++) {
380 population->log_M[i] = this->log_M[i].initial_value_m;
381 if (this->log_M[i].estimation_type_m.get() == "fixed_effects") {
382 ss.str("");
383 ss << "Population." << this->id << ".log_M." << this->log_M[i].id_m;
384 info->RegisterParameterName(ss.str());
385 info->RegisterParameter(population->log_M[i]);
386 }
387 if (this->log_M[i].estimation_type_m.get() == "random_effects") {
388 ss.str("");
389 ss << "Population." << this->id << ".log_M." << this->log_M[i].id_m;
390 info->RegisterRandomEffectName(ss.str());
391 info->RegisterRandomEffect(population->log_M[i]);
392 }
393 }
394 info->variable_map[this->log_M.id_m] = &(population)->log_M;
395
396 for (size_t i = 0; i < log_f_multiplier.size(); i++) {
397 population->log_f_multiplier[i] =
398 this->log_f_multiplier[i].initial_value_m;
399 if (this->log_f_multiplier[i].estimation_type_m.get() ==
400 "fixed_effects") {
401 ss.str("");
402 ss << "Population." << this->id << ".log_f_multiplier."
403 << this->log_f_multiplier[i].id_m;
404 info->RegisterParameterName(ss.str());
405 info->RegisterParameter(population->log_f_multiplier[i]);
406 }
407 if (this->log_f_multiplier[i].estimation_type_m.get() ==
408 "random_effects") {
409 ss.str("");
410 ss << "Population." << this->id << ".log_f_multiplier."
411 << this->log_f_multiplier[i].id_m;
412 info->RegisterRandomEffectName(ss.str());
413 info->RegisterRandomEffect(population->log_f_multiplier[i]);
414 }
415 }
416 info->variable_map[this->log_f_multiplier.id_m] =
418
419 for (size_t i = 0; i < log_init_naa.size(); i++) {
420 population->log_init_naa[i] = this->log_init_naa[i].initial_value_m;
421 if (this->log_init_naa[i].estimation_type_m.get() == "fixed_effects") {
422 ss.str("");
423 ss << "Population." << this->id << ".log_init_naa."
424 << this->log_init_naa[i].id_m;
425 info->RegisterParameterName(ss.str());
426 info->RegisterParameter(population->log_init_naa[i]);
427 }
428 if (this->log_init_naa[i].estimation_type_m.get() == "random_effects") {
429 ss.str("");
430 ss << "Population." << this->id << ".log_init_naa."
431 << this->log_init_naa[i].id_m;
432 info->RegisterRandomEffectName(ss.str());
433 info->RegisterRandomEffect(population->log_init_naa[i]);
434 }
435 }
436 info->variable_map[this->log_init_naa.id_m] = &(population)->log_init_naa;
437
438 if (this->proportion_female.size() == 1 ||
439 this->proportion_female.size() ==
440 static_cast<size_t>(this->n_ages.get())) {
441 population->proportion_female.resize(this->proportion_female.size());
442 } else {
444 "The proportion_female vector is not of size 1 or n_ages. Filling "
445 "with 0.5.");
446 this->proportion_female.resize(1);
447 this->proportion_female[0].initial_value_m = static_cast<double>(0.5);
448 this->proportion_female[0].estimation_type_m.set("constant");
449 population->proportion_female.resize(this->proportion_female.size());
450 }
451
452 for (size_t i = 0; i < this->proportion_female.size(); i++) {
453 if (this->proportion_female[i].initial_value_m < 0.0 ||
454 this->proportion_female[i].initial_value_m > 1.0) {
456 "proportion_female should be in [0, 1]; got " +
457 fims::to_string(this->proportion_female[i].initial_value_m) +
458 " at index " + fims::to_string(i) + ".");
459 }
460 population->proportion_female[i] =
461 this->proportion_female[i].initial_value_m;
462 if (this->proportion_female[i].estimation_type_m.get() ==
463 "fixed_effects") {
464 ss.str("");
465 ss << "Population." << this->id << ".proportion_female."
466 << this->proportion_female[i].id_m;
467 info->RegisterParameterName(ss.str());
468 info->RegisterParameter(population->proportion_female[i]);
469 }
470 if (this->proportion_female[i].estimation_type_m.get() ==
471 "random_effects") {
472 ss.str("");
473 ss << "Population." << this->id << ".proportion_female."
474 << this->proportion_female[i].id_m;
475 info->RegisterRandomEffectName(ss.str());
476 info->RegisterRandomEffect(population->proportion_female[i]);
477 }
478 }
479 info->variable_map[this->proportion_female.id_m] =
481
482 for (size_t i = 0; i < ages.size(); i++) {
483 population->ages[i] = this->ages[i];
484 }
485
486 // add to Information
487 info->populations[population->id] = population;
488
489 return true;
490 }
491
496 virtual bool add_to_fims_tmb() {
499
500 return true;
501 }
502
503#endif
504};
505
506#endif
Base class for all interface objects.
Definition rcpp_interface_base.hpp:628
bool finalized
Is the object already finalized? The default is false.
Definition rcpp_interface_base.hpp:633
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:638
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:643
An Rcpp interface class that defines the ParameterVector class.
Definition rcpp_interface_base.hpp:144
void resize(size_t size)
Resizes a ParameterVector to the desired length.
Definition rcpp_interface_base.hpp:295
size_t size()
Returns the size of a ParameterVector.
Definition rcpp_interface_base.hpp:288
uint32_t id_m
The local ID of the Parameter object.
Definition rcpp_interface_base.hpp:157
Parameter & get(size_t pos)
An internal accessor for calling a position of a ParameterVector from R.
Definition rcpp_interface_base.hpp:267
void set(size_t pos, const Parameter &p)
An internal setter for setting a position of a ParameterVector from R.
Definition rcpp_interface_base.hpp:283
Rcpp interface that serves as the parent class for Rcpp population interfaces. This type should be in...
Definition rcpp_population.hpp:19
virtual ~PopulationInterfaceBase()
The destructor.
Definition rcpp_population.hpp:68
PopulationInterfaceBase()
The constructor.
Definition rcpp_population.hpp:50
SharedBoolean initialize_surplus_production
Initialize the surplus production model.
Definition rcpp_population.hpp:46
uint32_t id
The local id of the PopulationInterfaceBase object.
Definition rcpp_population.hpp:28
virtual uint32_t get_id()=0
Get the ID for the child population interface objects to inherit.
PopulationInterfaceBase(const PopulationInterfaceBase &other)
Construct a new Population Interface Base object.
Definition rcpp_population.hpp:62
static uint32_t id_g
The static id of the PopulationInterfaceBase object.
Definition rcpp_population.hpp:24
static std::map< uint32_t, std::shared_ptr< PopulationInterfaceBase > > live_objects
The map associating the IDs of PopulationInterfaceBase to the objects. This is a live object,...
Definition rcpp_population.hpp:35
SharedBoolean initialize_catch_at_age
Initialize the catch at age model.
Definition rcpp_population.hpp:41
Rcpp interface for a new Population to instantiate from R: population <- methods::new(population)
Definition rcpp_population.hpp:80
std::set< uint32_t >::iterator fleet_ids_iterator
Definition rcpp_population.hpp:97
std::shared_ptr< std::set< uint32_t > > fleet_ids
Definition rcpp_population.hpp:93
ParameterVector proportion_female
Proportion of females in the population.
Definition rcpp_population.hpp:146
ParameterVector spawning_biomass_ratio
The population spawning biomass ratio for each year.
Definition rcpp_population.hpp:129
PopulationInterface()
The constructor.
Definition rcpp_population.hpp:160
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_population.hpp:203
void SetMaturityID(uint32_t maturity_id)
Sets the unique ID for the Maturity object.
Definition rcpp_population.hpp:221
ParameterVector log_init_naa
The natural log of the initial numbers at age.
Definition rcpp_population.hpp:137
SharedString name
The name for the population.
Definition rcpp_population.hpp:155
SharedInt n_ages
The number of age bins.
Definition rcpp_population.hpp:85
SharedInt maturity_id
The ID of the maturity module.
Definition rcpp_population.hpp:109
ParameterVector log_f_multiplier
Log of the population annual fishing mortality multiplier.
Definition rcpp_population.hpp:133
SharedInt n_years
The number of years.
Definition rcpp_population.hpp:101
void SetName(const std::string &name)
Sets the name of the population.
Definition rcpp_population.hpp:209
std::string GetName() const
Gets the name of the population.
Definition rcpp_population.hpp:215
RealVector ages
Ages that are modeled in the population, the length of this vector should equal "n_ages".
Definition rcpp_population.hpp:151
SharedInt recruitment_err_id
The ID of the recruitment process module.
Definition rcpp_population.hpp:121
ParameterVector log_M
The natural log of the natural mortality for each year.
Definition rcpp_population.hpp:125
virtual ~PopulationInterface()
The destructor.
Definition rcpp_population.hpp:197
SharedInt n_lengths
The number of length bins.
Definition rcpp_population.hpp:105
void SetGrowthID(uint32_t growth_id)
Set the unique ID for the growth object.
Definition rcpp_population.hpp:229
void AddFleet(uint32_t fleet_id)
Add a fleet id to the list of fleets operating on this population.
Definition rcpp_population.hpp:243
SharedInt growth_id
The ID of the growth module.
Definition rcpp_population.hpp:113
SharedInt recruitment_id
The ID of the recruitment module.
Definition rcpp_population.hpp:117
PopulationInterface(const PopulationInterface &other)
Construct a new Population Interface object.
Definition rcpp_population.hpp:175
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_population.hpp:249
SharedInt n_fleets
The number of fleets.
Definition rcpp_population.hpp:89
void SetRecruitmentID(uint32_t recruitment_id)
Set the unique ID for the recruitment object.
Definition rcpp_population.hpp:235
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:413
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:584
A class that provides shared ownership of a boolean value.
Definition rcpp_shared_primitive.hpp:1704
A class that provides shared ownership of an integer value.
Definition rcpp_shared_primitive.hpp:29
int get() const
Retrieve the value of the integer.
Definition rcpp_shared_primitive.hpp:127
A class that provides shared ownership of a string.
Definition rcpp_shared_primitive.hpp:1513
std::string get() const
Retrieves the string value managed by the object.
Definition rcpp_shared_primitive.hpp:1617
std::map< uint32_t, std::shared_ptr< fims_popdy::Population< Type > > > populations
Definition information.hpp:114
std::map< uint32_t, std::shared_ptr< fims_popdy::Population< Type > > >::iterator population_iterator
Definition information.hpp:119
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:237
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:648
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:235
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...