FIMS  v0.8.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
12#include "../../../common/def.hpp"
13#include "../../../population_dynamics/fleet/fleet.hpp"
15
21 public:
25 static uint32_t id_g;
35 static std::map<uint32_t, std::shared_ptr<FleetInterfaceBase>> live_objects;
36
41 this->id = FleetInterfaceBase::id_g++;
42 /* Create instance of map: key is id and value is pointer to
43 FleetInterfaceBase */
44 // FleetInterfaceBase::live_objects[this->id] = this;
45 }
46
53
58
62 virtual uint32_t get_id() = 0;
63};
64// static id of the FleetInterfaceBase object
66// local id of the FleetInterfaceBase object map relating the ID of the
67// FleetInterfaceBase to the FleetInterfaceBase objects
68std::map<uint32_t, std::shared_ptr<FleetInterfaceBase>>
78 SharedInt interface_observed_agecomp_data_id_m = -999;
82 SharedInt interface_observed_lengthcomp_data_id_m = -999;
86 SharedInt interface_observed_index_data_id_m = -999;
90 SharedInt interface_observed_landings_data_id_m = -999;
94 SharedInt interface_selectivity_id_m = -999;
95
96 public:
100 SharedString name = fims::to_string("NA");
117 SharedString observed_landings_units = fims::to_string("weight");
122 SharedString observed_index_units = fims::to_string("weight");
164
165 // derived quantities
169 Rcpp::NumericVector derived_landings_naa;
173 Rcpp::NumericVector derived_landings_nal;
177 Rcpp::NumericVector derived_landings_waa;
181 Rcpp::NumericVector derived_landings_expected;
185 Rcpp::NumericVector derived_landings_w;
189 Rcpp::NumericVector derived_landings_n;
193 Rcpp::NumericVector derived_index_naa;
197 Rcpp::NumericVector derived_index_nal;
201 Rcpp::NumericVector derived_index_waa;
205 Rcpp::NumericVector derived_index_expected;
209 Rcpp::NumericVector derived_index_w;
213 Rcpp::NumericVector derived_index_n;
217 Rcpp::NumericVector derived_agecomp_proportion;
221 Rcpp::NumericVector derived_lengthcomp_proportion;
225 Rcpp::NumericVector derived_agecomp_expected;
229 Rcpp::NumericVector derived_lengthcomp_expected;
230
235 std::shared_ptr<FleetInterface> fleet =
236 std::make_shared<FleetInterface>(*this);
238 /* Create instance of map: key is id and value is pointer to
239 FleetInterfaceBase */
241 }
242
250 interface_observed_agecomp_data_id_m(
251 other.interface_observed_agecomp_data_id_m),
252 interface_observed_lengthcomp_data_id_m(
253 other.interface_observed_lengthcomp_data_id_m),
254 interface_observed_index_data_id_m(
255 other.interface_observed_index_data_id_m),
256 interface_observed_landings_data_id_m(
257 other.interface_observed_landings_data_id_m),
258 interface_selectivity_id_m(other.interface_selectivity_id_m),
259 name(other.name),
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
319 interface_observed_agecomp_data_id_m.set(observed_agecomp_data_id);
320 }
321
327 interface_observed_lengthcomp_data_id_m.set(observed_lengthcomp_data_id);
328 }
329
335 interface_observed_index_data_id_m.set(observed_index_data_id);
336 }
337
343 interface_observed_landings_data_id_m.set(observed_landings_data_id);
344 }
350 interface_selectivity_id_m.set(selectivity_id);
351 }
352
358 uint32_t GetSelectivityID() { return interface_selectivity_id_m.get(); }
359
364 return interface_observed_agecomp_data_id_m.get();
365 }
366
372 return interface_observed_lengthcomp_data_id_m.get();
373 }
374
379 return interface_observed_index_data_id_m.get();
380 }
381
386 return interface_observed_landings_data_id_m.get();
387 }
392 virtual void finalize() {
393 if (this->finalized) {
394 // log warning that finalize has been called more than once.
395 FIMS_WARNING_LOG("Fleet " + fims::to_string(this->id) +
396 " has been finalized already.");
397 }
398
399 this->finalized = true; // indicate this has been called already
400
401 std::shared_ptr<fims_info::Information<double>> info =
403
405
406 it = info->fleets.find(this->id);
407
408 if (it == info->fleets.end()) {
409 FIMS_WARNING_LOG("Fleet " + fims::to_string(this->id) +
410 " not found in Information.");
411 return;
412 } else {
413 std::shared_ptr<fims_popdy::Fleet<double>> fleet =
414 std::dynamic_pointer_cast<fims_popdy::Fleet<double>>(it->second);
415
416 for (size_t i = 0; i < this->log_Fmort.size(); i++) {
417 if (this->log_Fmort[i].estimation_type_m.get() == "constant") {
418 this->log_Fmort[i].final_value_m = this->log_Fmort[i].initial_value_m;
419 } else {
420 this->log_Fmort[i].final_value_m = fleet->log_Fmort[i];
421 }
422 }
423
424 for (size_t i = 0; i < this->log_q.size(); i++) {
425 if (this->log_q[i].estimation_type_m.get() == "constant") {
426 this->log_q[i].final_value_m = this->log_q[i].initial_value_m;
427 } else {
428 this->log_q[i].final_value_m = fleet->log_q[i];
429 }
430 }
431
432 for (size_t i = 0; i < fleet->age_to_length_conversion.size(); i++) {
433 if (this->age_to_length_conversion[i].estimation_type_m.get() ==
434 "constant") {
435 this->age_to_length_conversion[i].final_value_m =
436 this->age_to_length_conversion[i].initial_value_m;
437 } else {
438 this->age_to_length_conversion[i].final_value_m =
439 fleet->age_to_length_conversion[i];
440 }
441 }
442 }
443 }
444
445#ifdef TMB_MODEL
446
447 template <typename Type>
449 std::shared_ptr<fims_info::Information<Type>> info =
451
452 std::shared_ptr<fims_popdy::Fleet<Type>> fleet =
453 std::make_shared<fims_popdy::Fleet<Type>>();
454
455 std::stringstream ss;
456
457 // set relative info
458 fleet->id = this->id;
459 fleet->n_ages = this->n_ages.get();
460 fleet->n_lengths = this->n_lengths.get();
461 fleet->n_years = this->n_years.get();
462 fleet->observed_landings_units = this->observed_landings_units;
463 fleet->observed_index_units = this->observed_index_units;
464
465 fleet->fleet_observed_agecomp_data_id_m =
466 interface_observed_agecomp_data_id_m.get();
467
468 fleet->fleet_observed_lengthcomp_data_id_m =
469 interface_observed_lengthcomp_data_id_m.get();
470
471 fleet->fleet_observed_index_data_id_m =
472 interface_observed_index_data_id_m.get();
473 fleet->fleet_observed_landings_data_id_m =
474 interface_observed_landings_data_id_m.get();
475
476 fleet->fleet_selectivity_id_m = interface_selectivity_id_m.get();
477
478 fleet->log_q.resize(this->log_q.size());
479 for (size_t i = 0; i < this->log_q.size(); i++) {
480 fleet->log_q[i] = this->log_q[i].initial_value_m;
481
482 if (this->log_q[i].estimation_type_m.get() == "fixed_effects") {
483 ss.str("");
484 ss << "Fleet." << this->id << ".log_q." << this->log_q[i].id_m;
485 info->RegisterParameterName(ss.str());
486 info->RegisterParameter(fleet->log_q[i]);
487 }
488 if (this->log_q[i].estimation_type_m.get() == "random_effects") {
489 ss.str("");
490 ss << "Fleet." << this->id << ".log_q." << this->log_q[i].id_m;
491 info->RegisterRandomEffectName(ss.str());
492 info->RegisterRandomEffect(fleet->log_q[i]);
493 }
494 }
495
496 FIMS_INFO_LOG("adding Fleet fmort object to TMB");
497 if (this->log_Fmort.size() != static_cast<size_t>(this->n_years.get())) {
498 FIMS_ERROR_LOG("The size of `log_Fmort` does not match `n_years`: " +
499 fims::to_string(this->log_Fmort.size()) +
500 " != " + fims::to_string(this->n_years.get()));
501 throw std::invalid_argument(
502 "Fleet log_Fmort size mismatch."
503 "Fleet log_Fmort is of size " +
504 fims::to_string(this->log_Fmort.size()) +
505 " and the number of years is " +
506 fims::to_string(this->n_years.get()));
507 }
508 fleet->log_Fmort.resize(static_cast<size_t>(this->log_Fmort.size()));
509 for (size_t i = 0; i < log_Fmort.size(); i++) {
510 fleet->log_Fmort[i] = this->log_Fmort[i].initial_value_m;
511
512 if (this->log_Fmort[i].estimation_type_m.get() == "fixed_effects") {
513 ss.str("");
514 ss << "Fleet." << this->id << ".log_Fmort." << this->log_Fmort[i].id_m;
515 info->RegisterParameterName(ss.str());
516 info->RegisterParameter(fleet->log_Fmort[i]);
517 }
518 if (this->log_Fmort[i].estimation_type_m.get() == "random_effects") {
519 ss.str("");
520 ss << "Fleet." << this->id << ".log_Fmort." << this->log_Fmort[i].id_m;
521 info->RegisterRandomEffectName(ss.str());
522 info->RegisterRandomEffect(fleet->log_Fmort[i]);
523 }
524 }
525 // add to variable_map
526 info->variable_map[this->log_Fmort.id_m] = &(fleet)->log_Fmort;
527
528 if (this->n_lengths.get() > 0) {
529 fleet->age_to_length_conversion.resize(
530 this->age_to_length_conversion.size());
531
532 if (this->age_to_length_conversion.size() !=
533 static_cast<size_t>(this->n_ages.get() * this->n_lengths.get())) {
535 "age_to_length_conversion don't match, " +
536 fims::to_string(this->age_to_length_conversion.size()) + " != " +
537 fims::to_string((this->n_ages.get() * this->n_lengths.get())));
538 }
539
540 for (size_t i = 0; i < fleet->age_to_length_conversion.size(); i++) {
541 fleet->age_to_length_conversion[i] =
542 this->age_to_length_conversion[i].initial_value_m;
543 FIMS_INFO_LOG(" adding Fleet length object to TMB in loop " +
544 fims::to_string(i) + " of " +
545 fims::to_string(fleet->age_to_length_conversion.size()));
546
547 if (this->age_to_length_conversion[i].estimation_type_m.get() ==
548 "fixed_effects") {
549 ss.str("");
550 ss << "Fleet." << this->id << ".age_to_length_conversion."
551 << this->age_to_length_conversion[i].id_m;
552 info->RegisterParameterName(ss.str());
553 info->RegisterParameter(fleet->age_to_length_conversion[i]);
554 }
555 if (this->age_to_length_conversion[i].estimation_type_m.get() ==
556 "random_effects") {
558 "age_to_length_conversion cannot be set to random effects");
559 }
560 }
561
562 info->variable_map[this->age_to_length_conversion.id_m] =
564 }
565
566 // add to Information
567 info->fleets[fleet->id] = fleet;
568 FIMS_INFO_LOG("done adding Fleet object to TMB");
569 return true;
570 }
571
576 virtual bool add_to_fims_tmb() {
577#ifdef TMBAD_FRAMEWORK
580#else
581 FIMS_INFO_LOG("adding Fleet object to TMB");
586#endif
587
588 return true;
589 }
590
591#endif
592};
593
594#endif
Base class for all interface objects.
Definition rcpp_interface_base.hpp:634
bool finalized
Is the object already finalized? The default is false.
Definition rcpp_interface_base.hpp:639
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:644
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:649
Rcpp interface that serves as the parent class for Rcpp fleet interfaces. This type should be inherit...
Definition rcpp_fleet.hpp:20
static uint32_t id_g
The static id of the FleetInterfaceBase object.
Definition rcpp_fleet.hpp:25
FleetInterfaceBase()
The constructor.
Definition rcpp_fleet.hpp:40
uint32_t id
The local id of the FleetInterfaceBase object.
Definition rcpp_fleet.hpp:29
virtual ~FleetInterfaceBase()
The destructor.
Definition rcpp_fleet.hpp:57
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:52
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:35
The Rcpp interface for Fleet to instantiate from R: fleet <- methods::new(Fleet)
Definition rcpp_fleet.hpp:74
ParameterVector agecomp_expected
The vector of expected landings-at-age in numbers for the fleet.
Definition rcpp_fleet.hpp:146
ParameterVector 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:163
ParameterVector log_index_expected
The vector of natural log of the expected index of abundance for the fleet.
Definition rcpp_fleet.hpp:142
Rcpp::NumericVector derived_index_expected
Derived index in observed units.
Definition rcpp_fleet.hpp:205
SharedInt n_years
The number of years in the fleet data.
Definition rcpp_fleet.hpp:112
Rcpp::NumericVector derived_landings_w
Derived landings in weight.
Definition rcpp_fleet.hpp:185
ParameterVector agecomp_proportion
The vector of expected landings-at-age in numbers for the fleet.
Definition rcpp_fleet.hpp:154
Rcpp::NumericVector derived_landings_naa
Derived landings-at-age in numbers.
Definition rcpp_fleet.hpp:169
uint32_t GetSelectivityID()
Get the unique ID for the selectivity object.
Definition rcpp_fleet.hpp:358
Rcpp::NumericVector derived_landings_n
Derived landings in numbers.
Definition rcpp_fleet.hpp:189
int GetObservedIndexDataID()
Get the unique id for the observed index data object.
Definition rcpp_fleet.hpp:378
Rcpp::NumericVector derived_index_w
Derived index in weight.
Definition rcpp_fleet.hpp:209
ParameterVector log_landings_expected
The vector of natural log of the expected total landings for the fleet.
Definition rcpp_fleet.hpp:137
ParameterVector lengthcomp_expected
The vector of expected landings-at-length in numbers for the fleet.
Definition rcpp_fleet.hpp:150
FleetInterface(const FleetInterface &other)
Construct a new Fleet Interface object.
Definition rcpp_fleet.hpp:248
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:392
int GetObservedAgeCompDataID()
Get the unique ID for the observed age-composition data object.
Definition rcpp_fleet.hpp:363
void SetObservedAgeCompDataID(int observed_agecomp_data_id)
Set the unique ID for the observed age-composition data object.
Definition rcpp_fleet.hpp:318
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:349
ParameterVector log_q
The natural log of the index of abundance scaling parameter for this fleet.
Definition rcpp_fleet.hpp:127
virtual ~FleetInterface()
The destructor.
Definition rcpp_fleet.hpp:294
SharedString observed_landings_units
What units are the observed landings for this fleet measured in. Options are weight or numbers,...
Definition rcpp_fleet.hpp:117
int GetObservedLengthCompDataID()
Get the unique ID for the observed length-composition data object.
Definition rcpp_fleet.hpp:371
Rcpp::NumericVector derived_index_naa
Derived landings-at-age in numbers.
Definition rcpp_fleet.hpp:193
ParameterVector log_Fmort
The vector of the natural log of fishing mortality rates for this fleet.
Definition rcpp_fleet.hpp:132
Rcpp::NumericVector derived_landings_nal
Derived landings-at-length in numbers.
Definition rcpp_fleet.hpp:173
SharedString name
The name of the fleet.
Definition rcpp_fleet.hpp:100
Rcpp::NumericVector derived_lengthcomp_proportion
Derived length composition proportions.
Definition rcpp_fleet.hpp:221
Rcpp::NumericVector derived_landings_waa
Derived landings-at-age in weight (mt).
Definition rcpp_fleet.hpp:177
SharedInt n_ages
The number of age bins in the fleet data.
Definition rcpp_fleet.hpp:104
Rcpp::NumericVector derived_index_waa
Derived landings-at-age in weight (mt).
Definition rcpp_fleet.hpp:201
void SetName(const std::string &name)
Sets the name of the fleet.
Definition rcpp_fleet.hpp:306
std::string GetName() const
Gets the name of the fleet.
Definition rcpp_fleet.hpp:312
Rcpp::NumericVector derived_agecomp_proportion
Derived age composition proportions.
Definition rcpp_fleet.hpp:217
Rcpp::NumericVector derived_landings_expected
Derived landings in observed units.
Definition rcpp_fleet.hpp:181
Rcpp::NumericVector derived_index_n
Derived index in numbers.
Definition rcpp_fleet.hpp:213
Rcpp::NumericVector derived_agecomp_expected
Derived age compositions.
Definition rcpp_fleet.hpp:225
Rcpp::NumericVector derived_lengthcomp_expected
Derived length compositions.
Definition rcpp_fleet.hpp:229
FleetInterface()
The constructor.
Definition rcpp_fleet.hpp:234
SharedInt n_lengths
The number of length bins in the fleet data.
Definition rcpp_fleet.hpp:108
void SetObservedIndexDataID(int observed_index_data_id)
Set the unique ID for the observed index data object.
Definition rcpp_fleet.hpp:334
void SetObservedLandingsDataID(int observed_landings_data_id)
Set the unique ID for the observed landings data object.
Definition rcpp_fleet.hpp:342
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:122
Rcpp::NumericVector derived_index_nal
Derived landings-at-length in numbers.
Definition rcpp_fleet.hpp:197
ParameterVector lengthcomp_proportion
The vector of expected landings-at-length in numbers for the fleet.
Definition rcpp_fleet.hpp:158
int GetObservedLandingsDataID()
Get the unique id for the observed landings data object.
Definition rcpp_fleet.hpp:385
An Rcpp interface class that defines the ParameterVector class.
Definition rcpp_interface_base.hpp:178
void resize(size_t size)
Resizes a ParameterVector to the desired length.
Definition rcpp_interface_base.hpp:323
size_t size()
Returns the size of a ParameterVector.
Definition rcpp_interface_base.hpp:316
uint32_t id_m
The local ID of the Parameter object.
Definition rcpp_interface_base.hpp:191
Parameter & get(size_t pos)
An internal accessor for calling a position of a ParameterVector from R.
Definition rcpp_interface_base.hpp:295
A class that provides shared ownership of an integer value.
Definition rcpp_shared_primitive.hpp:24
void set(int val)
Change the value of the integer.
Definition rcpp_shared_primitive.hpp:129
int get() const
Retrieve the value of the integer.
Definition rcpp_shared_primitive.hpp:122
A class that provides shared ownership of a string.
Definition rcpp_shared_primitive.hpp:1508
std::string get() const
Retrieves the string value managed by the object.
Definition rcpp_shared_primitive.hpp:1612
std::map< uint32_t, std::shared_ptr< fims_popdy::Fleet< Type > > >::iterator fleet_iterator
Definition information.hpp:110
std::map< uint32_t, std::shared_ptr< fims_popdy::Fleet< Type > > > fleets
Definition information.hpp:106
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:238
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:598
#define FIMS_INFO_LOG(MESSAGE)
Definition def.hpp:590
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:606
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:279
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...