FIMS  v0.8.1
Loading...
Searching...
No Matches
rcpp_recruitment.hpp
Go to the documentation of this file.
1
10#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_RECRUITMENT_HPP
11#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_RECRUITMENT_HPP
12
13#include "../../../population_dynamics/recruitment/recruitment.hpp"
15
21 public:
25 static uint32_t id_g;
39 static std::map<uint32_t, std::shared_ptr<RecruitmentInterfaceBase>>
41
47 /* Create instance of map: key is id and value is pointer to
48 RecruitmentInterfaceBase */
49 // RecruitmentInterfaceBase::live_objects[this->id] = this;
50 }
51
59
64
68 virtual uint32_t get_id() = 0;
69
74 virtual double evaluate_mean(double spawners, double ssbzero) = 0;
75
81 virtual double evaluate_process(size_t pos) = 0;
82};
83// static id of the RecruitmentInterfaceBase object
85// local id of the RecruitmentInterfaceBase object map relating the ID of the
86// RecruitmentInterfaceBase to the RecruitmentInterfaceBase objects
87std::map<uint32_t, std::shared_ptr<RecruitmentInterfaceBase>>
89
95 public:
134
140 std::make_shared<BevertonHoltRecruitmentInterface>(*this);
143 }
144
162
167
172 virtual uint32_t get_id() { return this->id; }
173
179 this->process_id.set(process_id);
180 }
181
189 virtual double evaluate_mean(double spawners, double ssbzero) {
191 BevHolt.logit_steep.resize(1);
192 BevHolt.logit_steep[0] = this->logit_steep[0].initial_value_m;
193 if (this->logit_steep[0].initial_value_m == 1.0) {
194 warning(
195 "Steepness is subject to a logit transformation. "
196 "Fixing it at 1.0 is not currently possible.");
197 }
198 BevHolt.log_rzero.resize(1);
199 BevHolt.log_rzero[0] = this->log_rzero[0].initial_value_m;
200
201 return BevHolt.evaluate_mean(spawners, ssbzero);
202 }
203
208 virtual double evaluate_process(size_t pos) { return 0; }
209
214 virtual void finalize() {
215 if (this->finalized) {
216 // log warning that finalize has been called more than once.
217 FIMS_WARNING_LOG("Beverton-Holt Recruitment " +
218 fims::to_string(this->id) +
219 " has been finalized already.");
220 }
221
222 this->finalized = true; // indicate this has been called already
223
224 std::shared_ptr<fims_info::Information<double>> info =
226
228
229 it = info->recruitment_models.find(this->id);
230
231 if (it == info->recruitment_models.end()) {
232 FIMS_WARNING_LOG("Beverton-Holt Recruitment " +
233 fims::to_string(this->id) +
234 " not found in Information.");
235 return;
236 } else {
237 std::shared_ptr<fims_popdy::SRBevertonHolt<double>> recr =
238 std::dynamic_pointer_cast<fims_popdy::SRBevertonHolt<double>>(
239 it->second);
240
241 for (size_t i = 0; i < this->logit_steep.size(); i++) {
242 if (this->logit_steep[i].estimation_type_m.get() == "constant") {
243 this->logit_steep[i].final_value_m =
244 this->logit_steep[i].initial_value_m;
245 } else {
246 this->logit_steep[i].final_value_m = recr->logit_steep[i];
247 }
248 }
249
250 for (size_t i = 0; i < log_rzero.size(); i++) {
251 if (log_rzero[i].estimation_type_m.get() == "constant") {
252 this->log_rzero[i].final_value_m = this->log_rzero[i].initial_value_m;
253 } else {
254 this->log_rzero[i].final_value_m = recr->log_rzero[i];
255 }
256 }
257
258 for (size_t i = 0; i < this->log_devs.size(); i++) {
259 if (this->log_devs[i].estimation_type_m.get() == "constant") {
260 this->log_devs[i].final_value_m = this->log_devs[i].initial_value_m;
261 } else {
262 this->log_devs[i].final_value_m = recr->log_recruit_devs[i];
263 }
264 }
265
266 for (size_t i = 0; i < this->log_r.size(); i++) {
267 if (this->log_r[i].estimation_type_m.get() == "constant") {
268 this->log_r[i].final_value_m = this->log_r[i].initial_value_m;
269 } else {
270 this->log_r[i].final_value_m = recr->log_r[i];
271 }
272 }
273 }
274 }
275
283 virtual std::string to_json() {
284 std::stringstream ss;
285
286 ss << "{\n";
287 ss << " \"module_name\": \"Recruitment\",\n";
288 ss << " \"module_type\": \"Beverton-Holt\",\n";
289 ss << " \"module_id\": " << this->id << ",\n";
290
291 ss << " \"parameters\": [\n{\n";
292 ss << " \"name\": \"logit_steep\",\n";
293 ss << " \"id\":" << this->logit_steep.id_m << ",\n";
294 ss << " \"type\": \"vector\",\n";
295 ss << " \"dimensionality\": {\n";
296 ss << " \"header\": [null],\n";
297 ss << " \"dimensions\": [" << this->logit_steep.size() << "]\n},\n";
298 ss << " \"values\":" << this->logit_steep << "},\n";
299
300 ss << "{\n";
301 ss << " \"name\": \"log_rzero\",\n";
302 ss << " \"id\":" << this->log_rzero.id_m << ",\n";
303 ss << " \"type\": \"vector\",\n";
304 ss << " \"dimensionality\": {\n";
305 ss << " \"header\": [null],\n";
306 ss << " \"dimensions\": [" << this->log_rzero.size() << "]\n},\n";
307 ss << " \"values\":" << this->log_rzero << "},\n";
308
309 ss << "{\n";
310 ss << " \"name\": \"log_devs\",\n";
311 ss << " \"id\":" << this->log_devs.id_m << ",\n";
312 ss << " \"type\": \"vector\",\n";
313 ss << " \"dimensionality\": {\n";
314 ss << " \"header\": [\"n_years-1\"],\n";
315 ss << " \"dimensions\": [" << this->log_devs.size() << "]\n},\n";
316 ss << " \"values\":" << this->log_devs << "}]\n";
317 ss << "}";
318 return ss.str();
319 }
320
321#ifdef TMB_MODEL
322
323 template <typename Type>
325 std::shared_ptr<fims_info::Information<Type>> info =
327
328 std::shared_ptr<fims_popdy::SRBevertonHolt<Type>> recruitment =
329 std::make_shared<fims_popdy::SRBevertonHolt<Type>>();
330
331 std::stringstream ss;
332
333 // set relative info
334 recruitment->id = this->id;
335 recruitment->process_id = this->process_id.get();
336 // set logit_steep
337 recruitment->logit_steep.resize(this->logit_steep.size());
338 for (size_t i = 0; i < this->logit_steep.size(); i++) {
339 recruitment->logit_steep[i] = this->logit_steep[i].initial_value_m;
340
341 if (this->logit_steep[i].estimation_type_m.get() == "fixed_effects") {
342 ss.str("");
343 ss << "Recruitment." << this->id << ".logit_steep."
344 << this->logit_steep[i].id_m;
345 info->RegisterParameterName(ss.str());
346 info->RegisterParameter(recruitment->logit_steep[i]);
347 }
348 if (this->logit_steep[i].estimation_type_m.get() == "random_effects") {
349 ss.str("");
350 ss << "Recruitment." << this->id << ".logit_steep."
351 << this->logit_steep[i].id_m;
352 info->RegisterRandomEffectName(ss.str());
353 info->RegisterRandomEffect(recruitment->logit_steep[i]);
354 }
355 }
356 info->variable_map[this->logit_steep.id_m] = &(recruitment)->logit_steep;
357
358 // set log_rzero
359 recruitment->log_rzero.resize(this->log_rzero.size());
360 for (size_t i = 0; i < this->log_rzero.size(); i++) {
361 recruitment->log_rzero[i] = this->log_rzero[i].initial_value_m;
362
363 if (this->log_rzero[i].estimation_type_m.get() == "fixed_effects") {
364 ss.str("");
365 ss << "Recruitment." << this->id << ".log_rzero."
366 << this->log_rzero[i].id_m;
367 info->RegisterParameterName(ss.str());
368 info->RegisterParameter(recruitment->log_rzero[i]);
369 }
370 if (this->log_rzero[i].estimation_type_m.get() == "random_effects") {
371 ss.str("");
372 ss << "Recruitment." << this->id << ".log_rzero."
373 << this->log_rzero[i].id_m;
374 info->RegisterRandomEffectName(ss.str());
375 info->RegisterRandomEffect(recruitment->log_rzero[i]);
376 }
377 }
378 info->variable_map[this->log_rzero.id_m] = &(recruitment)->log_rzero;
379 // set log_recruit_devs
380 recruitment->log_recruit_devs.resize(this->log_devs.size());
381 for (size_t i = 0; i < this->log_devs.size(); i++) {
382 recruitment->log_recruit_devs[i] = this->log_devs[i].initial_value_m;
383
384 if (this->log_devs[i].estimation_type_m.get() == "fixed_effects") {
385 ss.str("");
386 ss << "Recruitment." << this->id << ".log_devs."
387 << this->log_devs[i].id_m;
388 info->RegisterParameterName(ss.str());
389 info->RegisterParameter(recruitment->log_recruit_devs[i]);
390 }
391 if (this->log_devs[i].estimation_type_m.get() == "random_effects") {
392 ss.str("");
393 ss << "Recruitment." << this->id << ".log_devs."
394 << this->log_devs[i].id_m;
395 info->RegisterRandomEffectName(ss.str());
396 info->RegisterRandomEffect(recruitment->log_recruit_devs[i]);
397 }
398 }
399
400 info->variable_map[this->log_devs.id_m] = &(recruitment)->log_recruit_devs;
401
402 // set log_r
403 recruitment->log_r.resize(this->log_r.size());
404 for (size_t i = 0; i < log_r.size(); i++) {
405 recruitment->log_r[i] = this->log_r[i].initial_value_m;
406
407 if (this->log_r[i].estimation_type_m.get() == "fixed_effects") {
408 ss.str("");
409 ss << "Recruitment." << this->id << ".log_r." << this->log_r[i].id_m;
410 info->RegisterParameterName(ss.str());
411 info->RegisterParameter(recruitment->log_r[i]);
412 }
413 if (this->log_r[i].estimation_type_m.get() == "random_effects") {
414 ss.str("");
415 ss << "Recruitment." << this->id << ".log_r." << this->log_r[i].id_m;
416 info->RegisterRandomEffectName(ss.str());
417 info->RegisterRandomEffect(recruitment->log_r[i]);
418 }
419 }
420
421 info->variable_map[this->log_r.id_m] = &(recruitment)->log_r;
422 // set log_expected_recruitment
423 recruitment->log_expected_recruitment.resize(this->n_years.get() + 1);
424 for (size_t i = 0; i < static_cast<size_t>(this->n_years.get() + 1); i++) {
425 recruitment->log_expected_recruitment[i] = 0;
426 }
427 info->variable_map[this->log_expected_recruitment.id_m] =
428 &(recruitment)->log_expected_recruitment;
429
430 // add to Information
431 info->recruitment_models[recruitment->id] = recruitment;
432
433 return true;
434 }
435
440 virtual bool add_to_fims_tmb() {
443
444 return true;
445 }
446
447#endif
448};
449
455 public:
461 std::make_shared<LogDevsRecruitmentInterface>(*this));
462 }
463
468
473 virtual uint32_t get_id() { return this->id; }
474
480 virtual double evaluate_mean(double spawners, double ssbzero) { return 0; }
481
486 virtual double evaluate_process(size_t pos) {
488
489 return LogDevs.evaluate_process(pos);
490 }
491
492#ifdef TMB_MODEL
493
494 template <typename Type>
496 std::shared_ptr<fims_info::Information<Type>> info =
498
499 std::shared_ptr<fims_popdy::LogDevs<Type>> recruitment_process =
500 std::make_shared<fims_popdy::LogDevs<Type>>();
501
502 recruitment_process->id = this->id;
503
504 // add to Information
505 info->recruitment_process_models[recruitment_process->id] =
507
508 return true;
509 }
510
515 virtual bool add_to_fims_tmb() {
518
519 return true;
520 }
521
522#endif
523};
524
530 public:
536 std::make_shared<LogRRecruitmentInterface>(*this));
537 }
538
543
548 virtual uint32_t get_id() { return this->id; }
549
555 virtual double evaluate_mean(double spawners, double ssbzero) { return 0; }
556
561 virtual double evaluate_process(size_t pos) {
563
564 return LogR.evaluate_process(pos);
565 }
566
567#ifdef TMB_MODEL
568
569 template <typename Type>
571 std::shared_ptr<fims_info::Information<Type>> info =
573
574 std::shared_ptr<fims_popdy::LogR<Type>> recruitment_process =
575 std::make_shared<fims_popdy::LogR<Type>>();
576
577 recruitment_process->id = this->id;
578
579 // add to Information
580 info->recruitment_process_models[recruitment_process->id] =
582
583 return true;
584 }
585
590 virtual bool add_to_fims_tmb() {
593
594 return true;
595 }
596
597#endif
598};
599
600#endif
Rcpp interface for Beverton–Holt to instantiate from R: beverton_holt <- methods::new(beverton_holt).
Definition rcpp_recruitment.hpp:94
ParameterVector log_devs
The natural log of recruitment deviations.
Definition rcpp_recruitment.hpp:113
ParameterVector logit_steep
The logistic transformation of steepness (h; productivity of the population), where the parameter is ...
Definition rcpp_recruitment.hpp:105
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_recruitment.hpp:283
fims_double estimated_log_rzero
The estimate of the natural log of recruitment at unfished biomass.
Definition rcpp_recruitment.hpp:129
SharedInt n_years
The number of years.
Definition rcpp_recruitment.hpp:99
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_recruitment.hpp:214
virtual ~BevertonHoltRecruitmentInterface()
The destructor.
Definition rcpp_recruitment.hpp:166
void SetRecruitmentProcessID(uint32_t process_id)
Set the unique ID for the recruitment process object.
Definition rcpp_recruitment.hpp:178
BevertonHoltRecruitmentInterface(const BevertonHoltRecruitmentInterface &other)
Construct a new Beverton–Holt Recruitment Interface object.
Definition rcpp_recruitment.hpp:150
ParameterVector log_r
The recruitment random effect parameter on the natural log scale.
Definition rcpp_recruitment.hpp:117
ParameterVector log_rzero
The natural log of recruitment at unfished biomass.
Definition rcpp_recruitment.hpp:109
virtual double evaluate_process(size_t pos)
Evaluate recruitment process - returns 0 in this module.
Definition rcpp_recruitment.hpp:208
RealVector estimated_log_devs
The estimates of the natural log of recruitment deviations.
Definition rcpp_recruitment.hpp:133
virtual double evaluate_mean(double spawners, double ssbzero)
Evaluate recruitment using the Beverton–Holt stock–recruitment relationship.
Definition rcpp_recruitment.hpp:189
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_recruitment.hpp:172
fims_double estimated_logit_steep
The estimate of the logit transformation of steepness.
Definition rcpp_recruitment.hpp:125
BevertonHoltRecruitmentInterface()
The constructor.
Definition rcpp_recruitment.hpp:138
ParameterVector log_expected_recruitment
Expectation of the recruitment process.
Definition rcpp_recruitment.hpp:121
Base class for all interface objects.
Definition rcpp_interface_base.hpp:574
bool finalized
Is the object already finalized? The default is false.
Definition rcpp_interface_base.hpp:579
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:584
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:589
Rcpp interface for Log–Devs to instantiate from R: log_devs <- methods::new(log_devs).
Definition rcpp_recruitment.hpp:454
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_recruitment.hpp:473
virtual ~LogDevsRecruitmentInterface()
The destructor.
Definition rcpp_recruitment.hpp:467
virtual double evaluate_process(size_t pos)
Evaluate recruitment process using the Log–Devs approach.
Definition rcpp_recruitment.hpp:486
LogDevsRecruitmentInterface()
The constructor.
Definition rcpp_recruitment.hpp:459
virtual double evaluate_mean(double spawners, double ssbzero)
Evaluate mean - returns empty function for this module.
Definition rcpp_recruitment.hpp:480
Rcpp interface for Log–R to instantiate from R: log_r <- methods::new(log_r).
Definition rcpp_recruitment.hpp:529
virtual ~LogRRecruitmentInterface()
The destructor.
Definition rcpp_recruitment.hpp:542
LogRRecruitmentInterface()
The constructor.
Definition rcpp_recruitment.hpp:534
virtual double evaluate_process(size_t pos)
Evaluate recruitment process using the Log–R approach.
Definition rcpp_recruitment.hpp:561
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_recruitment.hpp:548
virtual double evaluate_mean(double spawners, double ssbzero)
Evaluate mean - returns empty function for this module.
Definition rcpp_recruitment.hpp:555
An Rcpp interface class that defines the ParameterVector class.
Definition rcpp_interface_base.hpp:142
void resize(size_t size)
Resizes a ParameterVector to the desired length.
Definition rcpp_interface_base.hpp:287
size_t size()
Returns the size of a ParameterVector.
Definition rcpp_interface_base.hpp:280
uint32_t id_m
The local ID of the Parameter object.
Definition rcpp_interface_base.hpp:155
Parameter & get(size_t pos)
An internal accessor for calling a position of a ParameterVector from R.
Definition rcpp_interface_base.hpp:259
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:374
Rcpp interface that serves as the parent class for Rcpp recruitment interfaces. This type should be i...
Definition rcpp_recruitment.hpp:20
virtual double evaluate_process(size_t pos)=0
A method for each child recruitment process interface object to inherit so each recruitment process o...
uint32_t id
The local id of the RecruitmentInterfaceBase object.
Definition rcpp_recruitment.hpp:29
virtual uint32_t get_id()=0
Get the ID for the child recruitment interface objects to inherit.
virtual ~RecruitmentInterfaceBase()
The destructor.
Definition rcpp_recruitment.hpp:63
SharedInt process_id
The process id of the RecruitmentInterfaceBase object.
Definition rcpp_recruitment.hpp:33
virtual double evaluate_mean(double spawners, double ssbzero)=0
A method for each child recruitment interface object to inherit so each recruitment option can have a...
RecruitmentInterfaceBase(const RecruitmentInterfaceBase &other)
Construct a new Recruitment Interface Base object.
Definition rcpp_recruitment.hpp:57
RecruitmentInterfaceBase()
The constructor.
Definition rcpp_recruitment.hpp:45
static uint32_t id_g
The static id of the RecruitmentInterfaceBase object.
Definition rcpp_recruitment.hpp:25
static std::map< uint32_t, std::shared_ptr< RecruitmentInterfaceBase > > live_objects
The map associating the IDs of RecruitmentInterfaceBase to the objects. This is a live object,...
Definition rcpp_recruitment.hpp:40
A class that provides shared ownership of an integer value.
Definition rcpp_shared_primitive.hpp:24
int get() const
Retrieve the value of the integer.
Definition rcpp_shared_primitive.hpp:122
A class that provides shared ownership of an integer value.
Definition rcpp_shared_primitive.hpp:784
std::map< uint32_t, std::shared_ptr< fims_popdy::RecruitmentBase< Type > > >::iterator recruitment_models_iterator
Definition information.hpp:68
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:238
std::map< uint32_t, std::shared_ptr< fims_popdy::RecruitmentBase< Type > > > recruitment_models
Definition information.hpp:64
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:545
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:239
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...
Log Devs class that returns the log of the input added to the log of the recruitment deviations.
Definition log_devs.hpp:21
virtual const Type evaluate_process(size_t pos)
Log of the recruitment deviations approach to adding error to expected recruitment.
Definition log_devs.hpp:34
Log Devs class that returns the log of the input added to the log of the recruitment deviations.
Definition log_r.hpp:21
virtual const Type evaluate_process(size_t pos)
Log of recruitment approach to adding error to expected recruitment.
Definition log_r.hpp:34
BevertonHolt class that returns the Beverton–Holt stock–recruitment from fims_math.
Definition sr_beverton_holt.hpp:28
fims::Vector< Type > logit_steep
Definition sr_beverton_holt.hpp:33