FIMS  v0.10.0
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;
28 uint32_t id;
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
51 FleetInterfaceBase(const FleetInterfaceBase &other) : id(other.id) {}
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;
90 SharedBoolean interface_requires_age_length_mapping = false;
94 SharedInt interface_observed_index_data_id_m = -999;
98 SharedInt interface_observed_catch_data_id_m = -999;
102 SharedInt interface_selectivity_id_m = -999;
103
104 public:
108 SharedString name = fims::to_string("NA");
129 SharedString observed_catch_units = fims::to_string("weight");
134 SharedString observed_index_units = fims::to_string("weight");
145
151
152 // Fleet based derived quantities
231
236 std::shared_ptr<FleetInterface> fleet =
237 std::make_shared<FleetInterface>(*this);
239 /* Create instance of map: key is id and value is pointer to
240 FleetInterfaceBase */
242 }
243
250 : FleetInterfaceBase(other),
251 interface_observed_agecomp_data_id_m(
252 other.interface_observed_agecomp_data_id_m),
253 interface_observed_lengthcomp_data_id_m(
254 other.interface_observed_lengthcomp_data_id_m),
255 interface_requires_age_length_mapping(
256 other.interface_requires_age_length_mapping),
257 interface_observed_index_data_id_m(
258 other.interface_observed_index_data_id_m),
259 interface_observed_catch_data_id_m(
260 other.interface_observed_catch_data_id_m),
261 interface_selectivity_id_m(other.interface_selectivity_id_m),
262 name(other.name),
263 n_ages(other.n_ages),
264 n_lengths(other.n_lengths),
265 lengths(other.lengths),
266 n_years(other.n_years),
269 log_q(other.log_q),
270 log_Fmort(other.log_Fmort),
290
294 virtual ~FleetInterface() {}
295
300 virtual uint32_t get_id() { return this->id; }
301
306 void SetName(const std::string &name) { this->name.set(name); }
307
312 std::string GetName() const { return this->name.get(); }
313
318 void SetObservedAgeCompDataID(int observed_agecomp_data_id) {
319 interface_observed_agecomp_data_id_m.set(observed_agecomp_data_id);
320 }
321
326 void SetObservedLengthCompDataID(int observed_lengthcomp_data_id) {
327 interface_observed_lengthcomp_data_id_m.set(observed_lengthcomp_data_id);
328 }
329
342 void SetRequiresAgeLengthMapping(bool requires_age_length_mapping) {
343 interface_requires_age_length_mapping.set(requires_age_length_mapping);
344 }
345
350 void SetObservedIndexDataID(int observed_index_data_id) {
351 interface_observed_index_data_id_m.set(observed_index_data_id);
352 }
353
358 void SetObservedCatchDataID(int observed_catch_data_id) {
359 interface_observed_catch_data_id_m.set(observed_catch_data_id);
360 }
365 void SetSelectivityID(int selectivity_id) {
366 interface_selectivity_id_m.set(selectivity_id);
367 }
368
374 uint32_t GetSelectivityID() { return interface_selectivity_id_m.get(); }
375
380 return interface_observed_agecomp_data_id_m.get();
381 }
382
388 return interface_observed_lengthcomp_data_id_m.get();
389 }
390
402 return interface_requires_age_length_mapping.get();
403 }
404
409 return interface_observed_index_data_id_m.get();
410 }
411
416 return interface_observed_catch_data_id_m.get();
417 }
422 virtual void finalize() {
423 if (this->finalized) {
424 // log warning that finalize has been called more than once.
425 FIMS_WARNING_LOG("Fleet " + fims::to_string(this->id) +
426 " has been finalized already.");
427 }
428
429 this->finalized = true; // indicate this has been called already
430
431 std::shared_ptr<fims_info::Information<double>> info =
433
435
436 it = info->fleets.find(this->id);
437
438 if (it == info->fleets.end()) {
439 FIMS_WARNING_LOG("Fleet " + fims::to_string(this->id) +
440 " not found in Information.");
441 return;
442 } else {
443 std::shared_ptr<fims_popdy::Fleet<double>> fleet =
444 std::dynamic_pointer_cast<fims_popdy::Fleet<double>>(it->second);
445
446 for (size_t i = 0; i < this->log_Fmort.size(); i++) {
447 if (this->log_Fmort[i].estimation_type_m.get() == "constant") {
448 this->log_Fmort[i].final_value_m = this->log_Fmort[i].initial_value_m;
449 } else {
450 this->log_Fmort[i].final_value_m = fleet->log_Fmort[i];
451 }
452 }
453
454 for (size_t i = 0; i < this->log_q.size(); i++) {
455 if (this->log_q[i].estimation_type_m.get() == "constant") {
456 this->log_q[i].final_value_m = this->log_q[i].initial_value_m;
457 } else {
458 this->log_q[i].final_value_m = fleet->log_q[i];
459 }
460 }
461
462 for (size_t i = 0; i < fleet->age_to_length_conversion.size(); i++) {
463 if (this->age_to_length_conversion[i].estimation_type_m.get() ==
464 "constant") {
465 this->age_to_length_conversion[i].final_value_m =
466 this->age_to_length_conversion[i].initial_value_m;
467 } else {
468 this->age_to_length_conversion[i].final_value_m =
469 fleet->age_to_length_conversion[i];
470 }
471 }
472 }
473 }
474
475#ifdef TMB_MODEL
476
477 template <typename Type>
478 bool add_to_fims_tmb_internal() {
479 std::shared_ptr<fims_info::Information<Type>> info =
481
482 std::shared_ptr<fims_popdy::Fleet<Type>> fleet =
483 std::make_shared<fims_popdy::Fleet<Type>>();
484
485 std::stringstream ss;
486
487 // set relative info
488 fleet->id = this->id;
489 fleet->n_ages = this->n_ages.get();
490 fleet->n_lengths = this->n_lengths.get();
491 fleet->n_years = this->n_years.get();
492 fleet->observed_catch_units = this->observed_catch_units;
493 fleet->observed_index_units = this->observed_index_units;
494
495 fleet->fleet_observed_agecomp_data_id_m =
496 interface_observed_agecomp_data_id_m.get();
497
498 fleet->fleet_observed_lengthcomp_data_id_m =
499 interface_observed_lengthcomp_data_id_m.get();
500 fleet->requires_age_length_mapping =
501 interface_requires_age_length_mapping.get();
502
503 fleet->fleet_observed_index_data_id_m =
504 interface_observed_index_data_id_m.get();
505 fleet->fleet_observed_catch_data_id_m =
506 interface_observed_catch_data_id_m.get();
507
508 fleet->fleet_selectivity_id_m = interface_selectivity_id_m.get();
509
510 fleet->log_q.resize(this->log_q.size());
511 for (size_t i = 0; i < this->log_q.size(); i++) {
512 fleet->log_q[i] = this->log_q[i].initial_value_m;
513
514 if (this->log_q[i].estimation_type_m.get() == "fixed_effects") {
515 ss.str("");
516 ss << "Fleet." << this->id << ".log_q." << this->log_q[i].id_m;
517 info->RegisterParameterName(ss.str());
518 info->RegisterParameter(fleet->log_q[i]);
519 }
520 if (this->log_q[i].estimation_type_m.get() == "random_effects") {
521 ss.str("");
522 ss << "Fleet." << this->id << ".log_q." << this->log_q[i].id_m;
523 info->RegisterRandomEffectName(ss.str());
524 info->RegisterRandomEffect(fleet->log_q[i]);
525 }
526 }
527
528 if (this->log_Fmort.size() != static_cast<size_t>(this->n_years.get())) {
529 FIMS_ERROR_LOG("The size of `log_Fmort` does not match `n_years`: " +
530 fims::to_string(this->log_Fmort.size()) +
531 " != " + fims::to_string(this->n_years.get()));
532 throw std::invalid_argument(
533 "Fleet log_Fmort size mismatch."
534 "Fleet log_Fmort is of size " +
535 fims::to_string(this->log_Fmort.size()) +
536 " and the number of years is " +
537 fims::to_string(this->n_years.get()));
538 }
539 fleet->log_Fmort.resize(static_cast<size_t>(this->log_Fmort.size()));
540 for (size_t i = 0; i < log_Fmort.size(); i++) {
541 fleet->log_Fmort[i] = this->log_Fmort[i].initial_value_m;
542
543 if (this->log_Fmort[i].estimation_type_m.get() == "fixed_effects") {
544 ss.str("");
545 ss << "Fleet." << this->id << ".log_Fmort." << this->log_Fmort[i].id_m;
546 info->RegisterParameterName(ss.str());
547 info->RegisterParameter(fleet->log_Fmort[i]);
548 }
549 if (this->log_Fmort[i].estimation_type_m.get() == "random_effects") {
550 ss.str("");
551 ss << "Fleet." << this->id << ".log_Fmort." << this->log_Fmort[i].id_m;
552 info->RegisterRandomEffectName(ss.str());
553 info->RegisterRandomEffect(fleet->log_Fmort[i]);
554 }
555 }
556 // add to variable_map
557 info->variable_map[this->log_Fmort.id_m] = &(fleet)->log_Fmort;
558
559 if (this->n_lengths.get() > 0) {
560 if (this->lengths.size() != static_cast<size_t>(this->n_lengths.get())) {
561 FIMS_ERROR_LOG("The size of `lengths` does not match `n_lengths`: " +
562 fims::to_string(this->lengths.size()) +
563 " != " + fims::to_string(this->n_lengths.get()));
564 throw std::invalid_argument(
565 "Fleet lengths size mismatch. Fleet lengths is of size " +
566 fims::to_string(this->lengths.size()) +
567 " and the number of lengths is " +
568 fims::to_string(this->n_lengths.get()));
569 }
570
571 fleet->lengths.resize(this->lengths.size());
572 for (size_t i = 0; i < fleet->lengths.size(); i++) {
573 fleet->lengths[i] = this->lengths[i];
574 }
575
576 fleet->age_to_length_conversion.resize(
577 this->age_to_length_conversion.size());
578
579 const size_t expected_age_to_length_conversion_size =
580 static_cast<size_t>(this->n_ages.get() * this->n_lengths.get());
581 const size_t supplied_age_to_length_conversion_size =
582 this->age_to_length_conversion.size();
583
584 if (supplied_age_to_length_conversion_size != 0 &&
585 supplied_age_to_length_conversion_size !=
586 expected_age_to_length_conversion_size) {
588 "age_to_length_conversion size mismatch, " +
589 fims::to_string(supplied_age_to_length_conversion_size) +
590 " != " + fims::to_string(expected_age_to_length_conversion_size));
591 throw std::invalid_argument(
592 "Fleet age_to_length_conversion size mismatch. Expected " +
593 fims::to_string(expected_age_to_length_conversion_size) +
594 " values but received " +
595 fims::to_string(supplied_age_to_length_conversion_size) + ".");
596 }
597
598 for (size_t i = 0; i < fleet->age_to_length_conversion.size(); i++) {
599 fleet->age_to_length_conversion[i] =
600 this->age_to_length_conversion[i].initial_value_m;
601
602 if (this->age_to_length_conversion[i].estimation_type_m.get() ==
603 "fixed_effects") {
604 ss.str("");
605 ss << "Fleet." << this->id << ".age_to_length_conversion."
606 << this->age_to_length_conversion[i].id_m;
607 info->RegisterParameterName(ss.str());
608 info->RegisterParameter(fleet->age_to_length_conversion[i]);
609 }
610 if (this->age_to_length_conversion[i].estimation_type_m.get() ==
611 "random_effects") {
613 "age_to_length_conversion cannot be set to random effects");
614 }
615 }
616
617 info->variable_map[this->age_to_length_conversion.id_m] =
618 &(fleet)->age_to_length_conversion;
619 }
620
621 // add to Information
622 info->fleets[fleet->id] = fleet;
623
624 return true;
625 }
626
631 virtual bool add_to_fims_tmb() {
632 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
633 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
634
635 return true;
636 }
637
638#endif
639};
640
641#endif
Base class for all interface objects.
Definition rcpp_interface_base.hpp:656
bool finalized
Is the object already finalized? The default is false.
Definition rcpp_interface_base.hpp:661
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:666
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:671
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
bool GetRequiresAgeLengthMapping()
Return whether this fleet requires age-to-length mapping.
Definition rcpp_fleet.hpp:401
SharedInt n_years
The number of years in the fleet data.
Definition rcpp_fleet.hpp:124
uint32_t GetSelectivityID()
Get the unique ID for the selectivity object.
Definition rcpp_fleet.hpp:374
VariableVector catch_numbers_at_age
Annual catch at age in numbers for a specific fleet.
Definition rcpp_fleet.hpp:156
int GetObservedIndexDataID()
Get the unique id for the observed index data object.
Definition rcpp_fleet.hpp:408
VariableVector catch_weight
Total catch in weight for a specific fleet.
Definition rcpp_fleet.hpp:168
VariableVector index_numbers_at_length
Numbers at length for a fleet index such as survey or CPUE.
Definition rcpp_fleet.hpp:204
int GetObservedCatchDataID()
Get the unique id for the observed catch data object.
Definition rcpp_fleet.hpp:415
VariableVector agecomp_proportion
Proportion of total catch by age used to specify the age composition.
Definition rcpp_fleet.hpp:187
FleetInterface(const FleetInterface &other)
Construct a new Fleet Interface object.
Definition rcpp_fleet.hpp:249
void SetObservedLengthCompDataID(int observed_lengthcomp_data_id)
Set the unique ID for the observed length-composition data object.
Definition rcpp_fleet.hpp:326
virtual void finalize()
Extracts the derived quantities from Information to the Rcpp object.
Definition rcpp_fleet.hpp:422
int GetObservedAgeCompDataID()
Get the unique ID for the observed age-composition data object.
Definition rcpp_fleet.hpp:379
void SetObservedAgeCompDataID(int observed_agecomp_data_id)
Set the unique ID for the observed age-composition data object.
Definition rcpp_fleet.hpp:318
void SetRequiresAgeLengthMapping(bool requires_age_length_mapping)
Set whether this fleet requires age-to-length mapping.
Definition rcpp_fleet.hpp:342
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_fleet.hpp:300
void SetSelectivityID(int selectivity_id)
Set the unique ID for the selectivity object.
Definition rcpp_fleet.hpp:365
virtual ~FleetInterface()
The destructor.
Definition rcpp_fleet.hpp:294
VariableVector catch_weight_at_age
Annual catch at age in weight for a specific fleet.
Definition rcpp_fleet.hpp:160
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:217
VariableVector lengthcomp_expected
Expected length-composition proportions for the fleet.
Definition rcpp_fleet.hpp:230
void SetObservedCatchDataID(int observed_catch_data_id)
Set the unique ID for the observed catch data object.
Definition rcpp_fleet.hpp:358
int GetObservedLengthCompDataID()
Get the unique ID for the observed length-composition data object.
Definition rcpp_fleet.hpp:387
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:150
SharedString name
The name of the fleet.
Definition rcpp_fleet.hpp:108
SharedInt n_ages
The number of age bins in the fleet data.
Definition rcpp_fleet.hpp:112
void SetName(const std::string &name)
Sets the name of the fleet.
Definition rcpp_fleet.hpp:306
VariableVector lengthcomp_proportion
Proportion of total catch by length used to specify the length composition.
Definition rcpp_fleet.hpp:192
VariableVector log_q
The natural log of the index of abundance scaling parameter for this fleet.
Definition rcpp_fleet.hpp:139
std::string GetName() const
Gets the name of the fleet.
Definition rcpp_fleet.hpp:312
VariableVector index_weight_at_age
Weight at age for a fleet index such as survey or CPUE.
Definition rcpp_fleet.hpp:200
VariableVector index_weight
Total weight for a fleet index such as survey or CPUE.
Definition rcpp_fleet.hpp:208
VariableVector log_catch_expected
Log total expected catch for a specific fleet, where the units of the measurement depend on the speci...
Definition rcpp_fleet.hpp:182
SharedString observed_catch_units
What units are the observed catch for this fleet measured in. Options are weight or numbers,...
Definition rcpp_fleet.hpp:129
VariableVector log_Fmort
The vector of the natural log of fishing mortality rates for this fleet.
Definition rcpp_fleet.hpp:144
FleetInterface()
The constructor.
Definition rcpp_fleet.hpp:235
SharedInt n_lengths
The number of length bins in the fleet data.
Definition rcpp_fleet.hpp:116
VariableVector index_numbers
Total numbers for a fleet index such as survey or CPUE.
Definition rcpp_fleet.hpp:212
VariableVector catch_numbers
Total catch in numbers for a specific fleet.
Definition rcpp_fleet.hpp:172
VariableVector agecomp_expected
Expected age-composition proportions for the fleet.
Definition rcpp_fleet.hpp:226
void SetObservedIndexDataID(int observed_index_data_id)
Set the unique ID for the observed index data object.
Definition rcpp_fleet.hpp:350
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:134
RealVector lengths
Fleet-specific observation-bin centers.
Definition rcpp_fleet.hpp:120
VariableVector index_numbers_at_age
Numbers at age for a fleet index such as survey or CPUE.
Definition rcpp_fleet.hpp:196
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:222
VariableVector catch_expected
Total expected catch for a specific fleet, where the units of the measurement depend on the specified...
Definition rcpp_fleet.hpp:177
VariableVector catch_numbers_at_length
Annual catch at length in numbers for a specific fleet.
Definition rcpp_fleet.hpp:164
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:432
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:603
void resize(size_t size)
Resizes a RealVector to the desired length.
Definition rcpp_interface_base.hpp:610
A class that provides shared ownership of a boolean value.
Definition rcpp_shared_primitive.hpp:1704
void set(bool val)
Sets the boolean value of the object.
Definition rcpp_shared_primitive.hpp:1818
bool get() const
Retrieves the boolean value managed by the object.
Definition rcpp_shared_primitive.hpp:1809
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
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:113
std::map< uint32_t, std::shared_ptr< fims_popdy::Fleet< Type > > > fleets
Definition information.hpp:109
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:269
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:648
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:664
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...