FIMS  v0.9.3
Loading...
Searching...
No Matches
rcpp_fleet.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_FLEET_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_FLEET_HPP
11
13#include "../../../population_dynamics/fleet/fleet.hpp"
14
20 public:
24 static uint32_t id_g;
34 static std::map<uint32_t, std::shared_ptr<FleetInterfaceBase>> live_objects;
35
40 this->id = FleetInterfaceBase::id_g++;
41 /* Create instance of map: key is id and value is pointer to
42 FleetInterfaceBase */
43 // FleetInterfaceBase::live_objects[this->id] = this;
44 }
45
52
57
61 virtual uint32_t get_id() = 0;
62};
63
72 SharedInt interface_observed_agecomp_data_id_m = -999;
76 SharedInt interface_observed_lengthcomp_data_id_m = -999;
80 SharedInt interface_observed_index_data_id_m = -999;
84 SharedInt interface_observed_landings_data_id_m = -999;
88 SharedInt interface_selectivity_id_m = -999;
89
90 public:
94 SharedString name = fims::to_string("NA");
111 SharedString observed_landings_units = fims::to_string("weight");
116 SharedString observed_index_units = fims::to_string("weight");
127
133
134 // Fleet based derived quantities
213
218 std::shared_ptr<FleetInterface> fleet =
219 std::make_shared<FleetInterface>(*this);
221 /* Create instance of map: key is id and value is pointer to
222 FleetInterfaceBase */
224 }
225
233 interface_observed_agecomp_data_id_m(
234 other.interface_observed_agecomp_data_id_m),
235 interface_observed_lengthcomp_data_id_m(
236 other.interface_observed_lengthcomp_data_id_m),
237 interface_observed_index_data_id_m(
238 other.interface_observed_index_data_id_m),
239 interface_observed_landings_data_id_m(
240 other.interface_observed_landings_data_id_m),
241 interface_selectivity_id_m(other.interface_selectivity_id_m),
242 name(other.name),
269
273 virtual ~FleetInterface() {}
274
279 virtual uint32_t get_id() { return this->id; }
280
285 void SetName(const std::string &name) { this->name.set(name); }
286
291 std::string GetName() const { return this->name.get(); }
292
298 interface_observed_agecomp_data_id_m.set(observed_agecomp_data_id);
299 }
300
306 interface_observed_lengthcomp_data_id_m.set(observed_lengthcomp_data_id);
307 }
308
314 interface_observed_index_data_id_m.set(observed_index_data_id);
315 }
316
322 interface_observed_landings_data_id_m.set(observed_landings_data_id);
323 }
329 interface_selectivity_id_m.set(selectivity_id);
330 }
331
337 uint32_t GetSelectivityID() { return interface_selectivity_id_m.get(); }
338
343 return interface_observed_agecomp_data_id_m.get();
344 }
345
351 return interface_observed_lengthcomp_data_id_m.get();
352 }
353
358 return interface_observed_index_data_id_m.get();
359 }
360
365 return interface_observed_landings_data_id_m.get();
366 }
371 virtual void finalize() {
372 if (this->finalized) {
373 // log warning that finalize has been called more than once.
374 FIMS_WARNING_LOG("Fleet " + fims::to_string(this->id) +
375 " has been finalized already.");
376 }
377
378 this->finalized = true; // indicate this has been called already
379
380 std::shared_ptr<fims_info::Information<double>> info =
382
384
385 it = info->fleets.find(this->id);
386
387 if (it == info->fleets.end()) {
388 FIMS_WARNING_LOG("Fleet " + fims::to_string(this->id) +
389 " not found in Information.");
390 return;
391 } else {
392 std::shared_ptr<fims_popdy::Fleet<double>> fleet =
393 std::dynamic_pointer_cast<fims_popdy::Fleet<double>>(it->second);
394
395 for (size_t i = 0; i < this->log_Fmort.size(); i++) {
396 if (this->log_Fmort[i].estimation_type_m.get() == "constant") {
397 this->log_Fmort[i].final_value_m = this->log_Fmort[i].initial_value_m;
398 } else {
399 this->log_Fmort[i].final_value_m = fleet->log_Fmort[i];
400 }
401 }
402
403 for (size_t i = 0; i < this->log_q.size(); i++) {
404 if (this->log_q[i].estimation_type_m.get() == "constant") {
405 this->log_q[i].final_value_m = this->log_q[i].initial_value_m;
406 } else {
407 this->log_q[i].final_value_m = fleet->log_q[i];
408 }
409 }
410
411 for (size_t i = 0; i < fleet->age_to_length_conversion.size(); i++) {
412 if (this->age_to_length_conversion[i].estimation_type_m.get() ==
413 "constant") {
414 this->age_to_length_conversion[i].final_value_m =
415 this->age_to_length_conversion[i].initial_value_m;
416 } else {
417 this->age_to_length_conversion[i].final_value_m =
418 fleet->age_to_length_conversion[i];
419 }
420 }
421 }
422 }
423
424#ifdef TMB_MODEL
425
426 template <typename Type>
428 std::shared_ptr<fims_info::Information<Type>> info =
430
431 std::shared_ptr<fims_popdy::Fleet<Type>> fleet =
432 std::make_shared<fims_popdy::Fleet<Type>>();
433
434 std::stringstream ss;
435
436 // set relative info
437 fleet->id = this->id;
438 fleet->n_ages = this->n_ages.get();
439 fleet->n_lengths = this->n_lengths.get();
440 fleet->n_years = this->n_years.get();
441 fleet->observed_landings_units = this->observed_landings_units;
442 fleet->observed_index_units = this->observed_index_units;
443
444 fleet->fleet_observed_agecomp_data_id_m =
445 interface_observed_agecomp_data_id_m.get();
446
447 fleet->fleet_observed_lengthcomp_data_id_m =
448 interface_observed_lengthcomp_data_id_m.get();
449
450 fleet->fleet_observed_index_data_id_m =
451 interface_observed_index_data_id_m.get();
452 fleet->fleet_observed_landings_data_id_m =
453 interface_observed_landings_data_id_m.get();
454
455 fleet->fleet_selectivity_id_m = interface_selectivity_id_m.get();
456
457 fleet->log_q.resize(this->log_q.size());
458 for (size_t i = 0; i < this->log_q.size(); i++) {
459 fleet->log_q[i] = this->log_q[i].initial_value_m;
460
461 if (this->log_q[i].estimation_type_m.get() == "fixed_effects") {
462 ss.str("");
463 ss << "Fleet." << this->id << ".log_q." << this->log_q[i].id_m;
464 info->RegisterParameterName(ss.str());
465 info->RegisterParameter(fleet->log_q[i]);
466 }
467 if (this->log_q[i].estimation_type_m.get() == "random_effects") {
468 ss.str("");
469 ss << "Fleet." << this->id << ".log_q." << this->log_q[i].id_m;
470 info->RegisterRandomEffectName(ss.str());
471 info->RegisterRandomEffect(fleet->log_q[i]);
472 }
473 }
474
475 if (this->log_Fmort.size() != static_cast<size_t>(this->n_years.get())) {
476 FIMS_ERROR_LOG("The size of `log_Fmort` does not match `n_years`: " +
477 fims::to_string(this->log_Fmort.size()) +
478 " != " + fims::to_string(this->n_years.get()));
479 throw std::invalid_argument(
480 "Fleet log_Fmort size mismatch."
481 "Fleet log_Fmort is of size " +
482 fims::to_string(this->log_Fmort.size()) +
483 " and the number of years is " +
484 fims::to_string(this->n_years.get()));
485 }
486 fleet->log_Fmort.resize(static_cast<size_t>(this->log_Fmort.size()));
487 for (size_t i = 0; i < log_Fmort.size(); i++) {
488 fleet->log_Fmort[i] = this->log_Fmort[i].initial_value_m;
489
490 if (this->log_Fmort[i].estimation_type_m.get() == "fixed_effects") {
491 ss.str("");
492 ss << "Fleet." << this->id << ".log_Fmort." << this->log_Fmort[i].id_m;
493 info->RegisterParameterName(ss.str());
494 info->RegisterParameter(fleet->log_Fmort[i]);
495 }
496 if (this->log_Fmort[i].estimation_type_m.get() == "random_effects") {
497 ss.str("");
498 ss << "Fleet." << this->id << ".log_Fmort." << this->log_Fmort[i].id_m;
499 info->RegisterRandomEffectName(ss.str());
500 info->RegisterRandomEffect(fleet->log_Fmort[i]);
501 }
502 }
503 // add to variable_map
504 info->variable_map[this->log_Fmort.id_m] = &(fleet)->log_Fmort;
505
506 if (this->n_lengths.get() > 0) {
507 fleet->age_to_length_conversion.resize(
508 this->age_to_length_conversion.size());
509
510 if (this->age_to_length_conversion.size() !=
511 static_cast<size_t>(this->n_ages.get() * this->n_lengths.get())) {
513 "age_to_length_conversion don't match, " +
514 fims::to_string(this->age_to_length_conversion.size()) + " != " +
515 fims::to_string((this->n_ages.get() * this->n_lengths.get())));
516 }
517
518 for (size_t i = 0; i < fleet->age_to_length_conversion.size(); i++) {
519 fleet->age_to_length_conversion[i] =
520 this->age_to_length_conversion[i].initial_value_m;
521
522 if (this->age_to_length_conversion[i].estimation_type_m.get() ==
523 "fixed_effects") {
524 ss.str("");
525 ss << "Fleet." << this->id << ".age_to_length_conversion."
526 << this->age_to_length_conversion[i].id_m;
527 info->RegisterParameterName(ss.str());
528 info->RegisterParameter(fleet->age_to_length_conversion[i]);
529 }
530 if (this->age_to_length_conversion[i].estimation_type_m.get() ==
531 "random_effects") {
533 "age_to_length_conversion cannot be set to random effects");
534 }
535 }
536
537 info->variable_map[this->age_to_length_conversion.id_m] =
539 }
540
541 // add to Information
542 info->fleets[fleet->id] = fleet;
543
544 return true;
545 }
546
551 virtual bool add_to_fims_tmb() {
554
555 return true;
556 }
557
558#endif
559};
560
561#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
Rcpp interface that serves as the parent class for Rcpp fleet interfaces. This type should be inherit...
Definition rcpp_fleet.hpp:19
static uint32_t id_g
The static id of the FleetInterfaceBase object.
Definition rcpp_fleet.hpp:24
FleetInterfaceBase()
The constructor.
Definition rcpp_fleet.hpp:39
uint32_t id
The local id of the FleetInterfaceBase object.
Definition rcpp_fleet.hpp:28
virtual ~FleetInterfaceBase()
The destructor.
Definition rcpp_fleet.hpp:56
virtual uint32_t get_id()=0
Get the ID for the child fleet interface objects to inherit.
FleetInterfaceBase(const FleetInterfaceBase &other)
Construct a new Fleet Interface Base object.
Definition rcpp_fleet.hpp:51
static std::map< uint32_t, std::shared_ptr< FleetInterfaceBase > > live_objects
The map associating the IDs of FleetInterfaceBase to the objects. This is a live object,...
Definition rcpp_fleet.hpp:34
The Rcpp interface for Fleet to instantiate from R: fleet <- methods::new(Fleet)
Definition rcpp_fleet.hpp:68
VariableVector landings_expected
Total expected landings for a specific fleet, where the units of the measurement depend on the specif...
Definition rcpp_fleet.hpp:159
SharedInt n_years
The number of years in the fleet data.
Definition rcpp_fleet.hpp:106
uint32_t GetSelectivityID()
Get the unique ID for the selectivity object.
Definition rcpp_fleet.hpp:337
int GetObservedIndexDataID()
Get the unique id for the observed index data object.
Definition rcpp_fleet.hpp:357
VariableVector index_numbers_at_length
Numbers at length for a fleet index such as survey or CPUE.
Definition rcpp_fleet.hpp:186
VariableVector agecomp_proportion
Proportion of total landings by age used to specify the age composition.
Definition rcpp_fleet.hpp:169
FleetInterface(const FleetInterface &other)
Construct a new Fleet Interface object.
Definition rcpp_fleet.hpp:231
void SetObservedLengthCompDataID(int observed_lengthcomp_data_id)
Set the unique ID for the observed length-composition data object.
Definition rcpp_fleet.hpp:305
virtual void finalize()
Extracts the derived quantities from Information to the Rcpp object.
Definition rcpp_fleet.hpp:371
int GetObservedAgeCompDataID()
Get the unique ID for the observed age-composition data object.
Definition rcpp_fleet.hpp:342
void SetObservedAgeCompDataID(int observed_agecomp_data_id)
Set the unique ID for the observed age-composition data object.
Definition rcpp_fleet.hpp:297
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_fleet.hpp:279
void SetSelectivityID(int selectivity_id)
Set the unique ID for the selectivity object.
Definition rcpp_fleet.hpp:328
virtual ~FleetInterface()
The destructor.
Definition rcpp_fleet.hpp:273
VariableVector landings_numbers_at_length
Annual landings at length in numbers for a specific fleet.
Definition rcpp_fleet.hpp:146
VariableVector index_expected
Expected value for a fleet index, where the units of the measurement depend on the specified units fo...
Definition rcpp_fleet.hpp:199
VariableVector lengthcomp_expected
Expected length-composition proportions for the fleet.
Definition rcpp_fleet.hpp:212
SharedString observed_landings_units
What units are the observed landings for this fleet measured in. Options are weight or numbers,...
Definition rcpp_fleet.hpp:111
int GetObservedLengthCompDataID()
Get the unique ID for the observed length-composition data object.
Definition rcpp_fleet.hpp:350
VariableVector age_to_length_conversion
The vector of conversions to go from age to length, i.e., the age-to-length-conversion matrix.
Definition rcpp_fleet.hpp:132
SharedString name
The name of the fleet.
Definition rcpp_fleet.hpp:94
SharedInt n_ages
The number of age bins in the fleet data.
Definition rcpp_fleet.hpp:98
void SetName(const std::string &name)
Sets the name of the fleet.
Definition rcpp_fleet.hpp:285
VariableVector lengthcomp_proportion
Proportion of total landings by length used to specify the length composition.
Definition rcpp_fleet.hpp:174
VariableVector log_q
The natural log of the index of abundance scaling parameter for this fleet.
Definition rcpp_fleet.hpp:121
std::string GetName() const
Gets the name of the fleet.
Definition rcpp_fleet.hpp:291
VariableVector index_weight_at_age
Weight at age for a fleet index such as survey or CPUE.
Definition rcpp_fleet.hpp:182
VariableVector index_weight
Total weight for a fleet index such as survey or CPUE.
Definition rcpp_fleet.hpp:190
VariableVector landings_numbers_at_age
Annual landings at age in numbers for a specific fleet.
Definition rcpp_fleet.hpp:138
VariableVector log_Fmort
The vector of the natural log of fishing mortality rates for this fleet.
Definition rcpp_fleet.hpp:126
FleetInterface()
The constructor.
Definition rcpp_fleet.hpp:217
SharedInt n_lengths
The number of length bins in the fleet data.
Definition rcpp_fleet.hpp:102
VariableVector index_numbers
Total numbers for a fleet index such as survey or CPUE.
Definition rcpp_fleet.hpp:194
VariableVector landings_weight
Total landings in weight for a specific fleet.
Definition rcpp_fleet.hpp:150
VariableVector agecomp_expected
Expected age-composition proportions for the fleet.
Definition rcpp_fleet.hpp:208
VariableVector landings_numbers
Total landings in numbers for a specific fleet.
Definition rcpp_fleet.hpp:154
VariableVector landings_weight_at_age
Annual landings at age in weight for a specific fleet.
Definition rcpp_fleet.hpp:142
void SetObservedIndexDataID(int observed_index_data_id)
Set the unique ID for the observed index data object.
Definition rcpp_fleet.hpp:313
void SetObservedLandingsDataID(int observed_landings_data_id)
Set the unique ID for the observed landings data object.
Definition rcpp_fleet.hpp:321
SharedString observed_index_units
What units is the observed index of abundance for this fleet measured in. Options are weight or numbe...
Definition rcpp_fleet.hpp:116
VariableVector index_numbers_at_age
Numbers at age for a fleet index such as survey or CPUE.
Definition rcpp_fleet.hpp:178
VariableVector log_index_expected
Log expected value for a fleet index, where the units of the measurement depend on the specified unit...
Definition rcpp_fleet.hpp:204
int GetObservedLandingsDataID()
Get the unique id for the observed landings data object.
Definition rcpp_fleet.hpp:364
VariableVector log_landings_expected
Log total expected landings for a specific fleet, where the units of the measurement depend on the sp...
Definition rcpp_fleet.hpp:164
A class that provides shared ownership of an integer value.
Definition rcpp_shared_primitive.hpp:29
void set(int val)
Change the value of the integer.
Definition rcpp_shared_primitive.hpp:134
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
An Rcpp interface class that defines the VariableVector class.
Definition rcpp_interface_base.hpp:144
Variable & get(size_t pos)
An internal accessor for calling a position of a VariableVector from R.
Definition rcpp_interface_base.hpp:267
uint32_t id_m
The local ID of the Variable object.
Definition rcpp_interface_base.hpp:157
void resize(size_t size)
Resizes a VariableVector to the desired length.
Definition rcpp_interface_base.hpp:295
size_t size()
Returns the size of a VariableVector.
Definition rcpp_interface_base.hpp:288
std::map< uint32_t, std::shared_ptr< fims_popdy::Fleet< Type > > >::iterator fleet_iterator
Definition information.hpp:109
std::map< uint32_t, std::shared_ptr< fims_popdy::Fleet< Type > > > fleets
Definition information.hpp:105
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:263
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:648
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:664
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:236
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...