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");
158
163 std::shared_ptr<FleetInterface> fleet =
164 std::make_shared<FleetInterface>(*this);
166 /* Create instance of map: key is id and value is pointer to
167 FleetInterfaceBase */
169 }
170
178 interface_observed_agecomp_data_id_m(
179 other.interface_observed_agecomp_data_id_m),
180 interface_observed_lengthcomp_data_id_m(
181 other.interface_observed_lengthcomp_data_id_m),
182 interface_observed_index_data_id_m(
183 other.interface_observed_index_data_id_m),
184 interface_observed_landings_data_id_m(
185 other.interface_observed_landings_data_id_m),
186 interface_selectivity_id_m(other.interface_selectivity_id_m),
187 name(other.name),
202
206 virtual ~FleetInterface() {}
207
212 virtual uint32_t get_id() { return this->id; }
213
218 void SetName(const std::string &name) { this->name.set(name); }
219
224 std::string GetName() const { return this->name.get(); }
225
231 interface_observed_agecomp_data_id_m.set(observed_agecomp_data_id);
232 }
233
239 interface_observed_lengthcomp_data_id_m.set(observed_lengthcomp_data_id);
240 }
241
247 interface_observed_index_data_id_m.set(observed_index_data_id);
248 }
249
255 interface_observed_landings_data_id_m.set(observed_landings_data_id);
256 }
262 interface_selectivity_id_m.set(selectivity_id);
263 }
264
270 uint32_t GetSelectivityID() { return interface_selectivity_id_m.get(); }
271
276 return interface_observed_agecomp_data_id_m.get();
277 }
278
284 return interface_observed_lengthcomp_data_id_m.get();
285 }
286
291 return interface_observed_index_data_id_m.get();
292 }
293
298 return interface_observed_landings_data_id_m.get();
299 }
304 virtual void finalize() {
305 if (this->finalized) {
306 // log warning that finalize has been called more than once.
307 FIMS_WARNING_LOG("Fleet " + fims::to_string(this->id) +
308 " has been finalized already.");
309 }
310
311 this->finalized = true; // indicate this has been called already
312
313 std::shared_ptr<fims_info::Information<double>> info =
315
317
318 it = info->fleets.find(this->id);
319
320 if (it == info->fleets.end()) {
321 FIMS_WARNING_LOG("Fleet " + fims::to_string(this->id) +
322 " not found in Information.");
323 return;
324 } else {
325 std::shared_ptr<fims_popdy::Fleet<double>> fleet =
326 std::dynamic_pointer_cast<fims_popdy::Fleet<double>>(it->second);
327
328 for (size_t i = 0; i < this->log_Fmort.size(); i++) {
329 if (this->log_Fmort[i].estimation_type_m.get() == "constant") {
330 this->log_Fmort[i].final_value_m = this->log_Fmort[i].initial_value_m;
331 } else {
332 this->log_Fmort[i].final_value_m = fleet->log_Fmort[i];
333 }
334 }
335
336 for (size_t i = 0; i < this->log_q.size(); i++) {
337 if (this->log_q[i].estimation_type_m.get() == "constant") {
338 this->log_q[i].final_value_m = this->log_q[i].initial_value_m;
339 } else {
340 this->log_q[i].final_value_m = fleet->log_q[i];
341 }
342 }
343
344 for (size_t i = 0; i < fleet->age_to_length_conversion.size(); i++) {
345 if (this->age_to_length_conversion[i].estimation_type_m.get() ==
346 "constant") {
347 this->age_to_length_conversion[i].final_value_m =
348 this->age_to_length_conversion[i].initial_value_m;
349 } else {
350 this->age_to_length_conversion[i].final_value_m =
351 fleet->age_to_length_conversion[i];
352 }
353 }
354 }
355 }
356
357#ifdef TMB_MODEL
358
359 template <typename Type>
361 std::shared_ptr<fims_info::Information<Type>> info =
363
364 std::shared_ptr<fims_popdy::Fleet<Type>> fleet =
365 std::make_shared<fims_popdy::Fleet<Type>>();
366
367 std::stringstream ss;
368
369 // set relative info
370 fleet->id = this->id;
371 fleet->n_ages = this->n_ages.get();
372 fleet->n_lengths = this->n_lengths.get();
373 fleet->n_years = this->n_years.get();
374 fleet->observed_landings_units = this->observed_landings_units;
375 fleet->observed_index_units = this->observed_index_units;
376
377 fleet->fleet_observed_agecomp_data_id_m =
378 interface_observed_agecomp_data_id_m.get();
379
380 fleet->fleet_observed_lengthcomp_data_id_m =
381 interface_observed_lengthcomp_data_id_m.get();
382
383 fleet->fleet_observed_index_data_id_m =
384 interface_observed_index_data_id_m.get();
385 fleet->fleet_observed_landings_data_id_m =
386 interface_observed_landings_data_id_m.get();
387
388 fleet->fleet_selectivity_id_m = interface_selectivity_id_m.get();
389
390 fleet->log_q.resize(this->log_q.size());
391 for (size_t i = 0; i < this->log_q.size(); i++) {
392 fleet->log_q[i] = this->log_q[i].initial_value_m;
393
394 if (this->log_q[i].estimation_type_m.get() == "fixed_effects") {
395 ss.str("");
396 ss << "Fleet." << this->id << ".log_q." << this->log_q[i].id_m;
397 info->RegisterParameterName(ss.str());
398 info->RegisterParameter(fleet->log_q[i]);
399 }
400 if (this->log_q[i].estimation_type_m.get() == "random_effects") {
401 ss.str("");
402 ss << "Fleet." << this->id << ".log_q." << this->log_q[i].id_m;
403 info->RegisterRandomEffectName(ss.str());
404 info->RegisterRandomEffect(fleet->log_q[i]);
405 }
406 }
407
408 if (this->log_Fmort.size() != static_cast<size_t>(this->n_years.get())) {
409 FIMS_ERROR_LOG("The size of `log_Fmort` does not match `n_years`: " +
410 fims::to_string(this->log_Fmort.size()) +
411 " != " + fims::to_string(this->n_years.get()));
412 throw std::invalid_argument(
413 "Fleet log_Fmort size mismatch."
414 "Fleet log_Fmort is of size " +
415 fims::to_string(this->log_Fmort.size()) +
416 " and the number of years is " +
417 fims::to_string(this->n_years.get()));
418 }
419 fleet->log_Fmort.resize(static_cast<size_t>(this->log_Fmort.size()));
420 for (size_t i = 0; i < log_Fmort.size(); i++) {
421 fleet->log_Fmort[i] = this->log_Fmort[i].initial_value_m;
422
423 if (this->log_Fmort[i].estimation_type_m.get() == "fixed_effects") {
424 ss.str("");
425 ss << "Fleet." << this->id << ".log_Fmort." << this->log_Fmort[i].id_m;
426 info->RegisterParameterName(ss.str());
427 info->RegisterParameter(fleet->log_Fmort[i]);
428 }
429 if (this->log_Fmort[i].estimation_type_m.get() == "random_effects") {
430 ss.str("");
431 ss << "Fleet." << this->id << ".log_Fmort." << this->log_Fmort[i].id_m;
432 info->RegisterRandomEffectName(ss.str());
433 info->RegisterRandomEffect(fleet->log_Fmort[i]);
434 }
435 }
436 // add to variable_map
437 info->variable_map[this->log_Fmort.id_m] = &(fleet)->log_Fmort;
438
439 if (this->n_lengths.get() > 0) {
440 fleet->age_to_length_conversion.resize(
441 this->age_to_length_conversion.size());
442
443 if (this->age_to_length_conversion.size() !=
444 static_cast<size_t>(this->n_ages.get() * this->n_lengths.get())) {
446 "age_to_length_conversion don't match, " +
447 fims::to_string(this->age_to_length_conversion.size()) + " != " +
448 fims::to_string((this->n_ages.get() * this->n_lengths.get())));
449 }
450
451 for (size_t i = 0; i < fleet->age_to_length_conversion.size(); i++) {
452 fleet->age_to_length_conversion[i] =
453 this->age_to_length_conversion[i].initial_value_m;
454
455 if (this->age_to_length_conversion[i].estimation_type_m.get() ==
456 "fixed_effects") {
457 ss.str("");
458 ss << "Fleet." << this->id << ".age_to_length_conversion."
459 << this->age_to_length_conversion[i].id_m;
460 info->RegisterParameterName(ss.str());
461 info->RegisterParameter(fleet->age_to_length_conversion[i]);
462 }
463 if (this->age_to_length_conversion[i].estimation_type_m.get() ==
464 "random_effects") {
466 "age_to_length_conversion cannot be set to random effects");
467 }
468 }
469
470 info->variable_map[this->age_to_length_conversion.id_m] =
472 }
473
474 // add to Information
475 info->fleets[fleet->id] = fleet;
476
477 return true;
478 }
479
484 virtual bool add_to_fims_tmb() {
487
488 return true;
489 }
490
491#endif
492};
493
494#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
ParameterVector agecomp_expected
The vector of expected landings-at-age in numbers for the fleet.
Definition rcpp_fleet.hpp:140
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:157
ParameterVector log_index_expected
The vector of natural log of the expected index of abundance for the fleet.
Definition rcpp_fleet.hpp:136
SharedInt n_years
The number of years in the fleet data.
Definition rcpp_fleet.hpp:106
ParameterVector agecomp_proportion
The vector of expected landings-at-age in numbers for the fleet.
Definition rcpp_fleet.hpp:148
uint32_t GetSelectivityID()
Get the unique ID for the selectivity object.
Definition rcpp_fleet.hpp:270
int GetObservedIndexDataID()
Get the unique id for the observed index data object.
Definition rcpp_fleet.hpp:290
ParameterVector log_landings_expected
The vector of natural log of the expected total landings for the fleet.
Definition rcpp_fleet.hpp:131
ParameterVector lengthcomp_expected
The vector of expected landings-at-length in numbers for the fleet.
Definition rcpp_fleet.hpp:144
FleetInterface(const FleetInterface &other)
Construct a new Fleet Interface object.
Definition rcpp_fleet.hpp:176
void SetObservedLengthCompDataID(int observed_lengthcomp_data_id)
Set the unique ID for the observed length-composition data object.
Definition rcpp_fleet.hpp:238
virtual void finalize()
Extracts the derived quantities from Information to the Rcpp object.
Definition rcpp_fleet.hpp:304
int GetObservedAgeCompDataID()
Get the unique ID for the observed age-composition data object.
Definition rcpp_fleet.hpp:275
void SetObservedAgeCompDataID(int observed_agecomp_data_id)
Set the unique ID for the observed age-composition data object.
Definition rcpp_fleet.hpp:230
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_fleet.hpp:212
void SetSelectivityID(int selectivity_id)
Set the unique ID for the selectivity object.
Definition rcpp_fleet.hpp:261
ParameterVector log_q
The natural log of the index of abundance scaling parameter for this fleet.
Definition rcpp_fleet.hpp:121
virtual ~FleetInterface()
The destructor.
Definition rcpp_fleet.hpp:206
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:283
ParameterVector log_Fmort
The vector of the natural log of fishing mortality rates for this fleet.
Definition rcpp_fleet.hpp:126
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:218
std::string GetName() const
Gets the name of the fleet.
Definition rcpp_fleet.hpp:224
FleetInterface()
The constructor.
Definition rcpp_fleet.hpp:162
SharedInt n_lengths
The number of length bins in the fleet data.
Definition rcpp_fleet.hpp:102
void SetObservedIndexDataID(int observed_index_data_id)
Set the unique ID for the observed index data object.
Definition rcpp_fleet.hpp:246
void SetObservedLandingsDataID(int observed_landings_data_id)
Set the unique ID for the observed landings data object.
Definition rcpp_fleet.hpp:254
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
ParameterVector lengthcomp_proportion
The vector of expected landings-at-length in numbers for the fleet.
Definition rcpp_fleet.hpp:152
int GetObservedLandingsDataID()
Get the unique id for the observed landings data object.
Definition rcpp_fleet.hpp:297
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
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
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:237
#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:235
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...