FIMS  v0.8.0
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 }
281 virtual void set_uncertainty(
282 std::map<std::string, std::vector<double>> &se_values) {
284 -99999999);
285 this->get_se_values("logit_steep", se_values, logit_steep_uncertainty);
287 -999999999);
288 this->get_se_values("log_rzero", se_values, log_rzero_uncertainty);
290 -999999999);
291 this->get_se_values("log_devs", se_values, log_devs_uncertainty);
292 for (size_t i = 0; i < this->logit_steep.size(); i++) {
293 this->logit_steep[i].uncertainty_m = logit_steep_uncertainty[i];
294 }
295 for (size_t i = 0; i < this->log_rzero.size(); i++) {
296 this->log_rzero[i].uncertainty_m = log_rzero_uncertainty[i];
297 }
298 for (size_t i = 0; i < this->log_devs.size(); i++) {
299 this->log_devs[i].uncertainty_m = log_devs_uncertainty[i];
300 }
301 }
302
310 virtual std::string to_json() {
311 std::stringstream ss;
312
313 ss << "{\n";
314 ss << " \"module_name\": \"Recruitment\",\n";
315 ss << " \"module_type\": \"Beverton-Holt\",\n";
316 ss << " \"module_id\": " << this->id << ",\n";
317
318 ss << " \"parameters\": [\n{\n";
319 ss << " \"name\": \"logit_steep\",\n";
320 ss << " \"id\":" << this->logit_steep.id_m << ",\n";
321 ss << " \"type\": \"vector\",\n";
322 ss << " \"dimensionality\": {\n";
323 ss << " \"header\": [null],\n";
324 ss << " \"dimensions\": [" << this->logit_steep.size() << "]\n},\n";
325 ss << " \"values\":" << this->logit_steep << "},\n";
326
327 ss << "{\n";
328 ss << " \"name\": \"log_rzero\",\n";
329 ss << " \"id\":" << this->log_rzero.id_m << ",\n";
330 ss << " \"type\": \"vector\",\n";
331 ss << " \"dimensionality\": {\n";
332 ss << " \"header\": [null],\n";
333 ss << " \"dimensions\": [" << this->log_rzero.size() << "]\n},\n";
334 ss << " \"values\":" << this->log_rzero << "},\n";
335
336 ss << "{\n";
337 ss << " \"name\": \"log_devs\",\n";
338 ss << " \"id\":" << this->log_devs.id_m << ",\n";
339 ss << " \"type\": \"vector\",\n";
340 ss << " \"dimensionality\": {\n";
341 ss << " \"header\": [\"n_years-1\"],\n";
342 ss << " \"dimensions\": [" << this->log_devs.size() << "]\n},\n";
343 ss << " \"values\":" << this->log_devs << "}]\n";
344 ss << "}";
345 return ss.str();
346 }
347
348#ifdef TMB_MODEL
349
350 template <typename Type>
352 std::shared_ptr<fims_info::Information<Type>> info =
354
355 std::shared_ptr<fims_popdy::SRBevertonHolt<Type>> recruitment =
356 std::make_shared<fims_popdy::SRBevertonHolt<Type>>();
357
358 std::stringstream ss;
359
360 // set relative info
361 recruitment->id = this->id;
362 recruitment->process_id = this->process_id.get();
363 // set logit_steep
364 recruitment->logit_steep.resize(this->logit_steep.size());
365 for (size_t i = 0; i < this->logit_steep.size(); i++) {
366 recruitment->logit_steep[i] = this->logit_steep[i].initial_value_m;
367
368 if (this->logit_steep[i].estimation_type_m.get() == "fixed_effects") {
369 ss.str("");
370 ss << "Recruitment." << this->id << ".logit_steep."
371 << this->logit_steep[i].id_m;
372 info->RegisterParameterName(ss.str());
373 info->RegisterParameter(recruitment->logit_steep[i]);
374 }
375 if (this->logit_steep[i].estimation_type_m.get() == "random_effects") {
376 ss.str("");
377 ss << "Recruitment." << this->id << ".logit_steep."
378 << this->logit_steep[i].id_m;
379 info->RegisterRandomEffectName(ss.str());
380 info->RegisterRandomEffect(recruitment->logit_steep[i]);
381 }
382 }
383 info->variable_map[this->logit_steep.id_m] = &(recruitment)->logit_steep;
384
385 // set log_rzero
386 recruitment->log_rzero.resize(this->log_rzero.size());
387 for (size_t i = 0; i < this->log_rzero.size(); i++) {
388 recruitment->log_rzero[i] = this->log_rzero[i].initial_value_m;
389
390 if (this->log_rzero[i].estimation_type_m.get() == "fixed_effects") {
391 ss.str("");
392 ss << "Recruitment." << this->id << ".log_rzero."
393 << this->log_rzero[i].id_m;
394 info->RegisterParameterName(ss.str());
395 info->RegisterParameter(recruitment->log_rzero[i]);
396 }
397 if (this->log_rzero[i].estimation_type_m.get() == "random_effects") {
398 ss.str("");
399 ss << "Recruitment." << this->id << ".log_rzero."
400 << this->log_rzero[i].id_m;
401 info->RegisterRandomEffectName(ss.str());
402 info->RegisterRandomEffect(recruitment->log_rzero[i]);
403 }
404 }
405 info->variable_map[this->log_rzero.id_m] = &(recruitment)->log_rzero;
406 // set log_recruit_devs
407 recruitment->log_recruit_devs.resize(this->log_devs.size());
408 for (size_t i = 0; i < this->log_devs.size(); i++) {
409 recruitment->log_recruit_devs[i] = this->log_devs[i].initial_value_m;
410
411 if (this->log_devs[i].estimation_type_m.get() == "fixed_effects") {
412 ss.str("");
413 ss << "Recruitment." << this->id << ".log_devs."
414 << this->log_devs[i].id_m;
415 info->RegisterParameterName(ss.str());
416 info->RegisterParameter(recruitment->log_recruit_devs[i]);
417 }
418 if (this->log_devs[i].estimation_type_m.get() == "random_effects") {
419 ss.str("");
420 ss << "Recruitment." << this->id << ".log_devs."
421 << this->log_devs[i].id_m;
422 info->RegisterRandomEffectName(ss.str());
423 info->RegisterRandomEffect(recruitment->log_recruit_devs[i]);
424 }
425 }
426
427 info->variable_map[this->log_devs.id_m] = &(recruitment)->log_recruit_devs;
428
429 // set log_r
430 recruitment->log_r.resize(this->log_r.size());
431 for (size_t i = 0; i < log_r.size(); i++) {
432 recruitment->log_r[i] = this->log_r[i].initial_value_m;
433
434 if (this->log_r[i].estimation_type_m.get() == "fixed_effects") {
435 ss.str("");
436 ss << "Recruitment." << this->id << ".log_r." << this->log_r[i].id_m;
437 info->RegisterParameterName(ss.str());
438 info->RegisterParameter(recruitment->log_r[i]);
439 }
440 if (this->log_r[i].estimation_type_m.get() == "random_effects") {
441 ss.str("");
442 ss << "Recruitment." << this->id << ".log_r." << this->log_r[i].id_m;
443 info->RegisterRandomEffectName(ss.str());
444 info->RegisterRandomEffect(recruitment->log_r[i]);
445 }
446 }
447
448 info->variable_map[this->log_r.id_m] = &(recruitment)->log_r;
449 // set log_expected_recruitment
450 recruitment->log_expected_recruitment.resize(this->n_years.get() + 1);
451 for (size_t i = 0; i < static_cast<size_t>(this->n_years.get() + 1); i++) {
452 recruitment->log_expected_recruitment[i] = 0;
453 }
454 info->variable_map[this->log_expected_recruitment.id_m] =
455 &(recruitment)->log_expected_recruitment;
456
457 // add to Information
458 info->recruitment_models[recruitment->id] = recruitment;
459
460 return true;
461 }
462
467 virtual bool add_to_fims_tmb() {
468#ifdef TMBAD_FRAMEWORK
471#else
476#endif
477
478 return true;
479 }
480
481#endif
482};
483
489 public:
495 std::make_shared<LogDevsRecruitmentInterface>(*this));
496 }
497
502
507 virtual uint32_t get_id() { return this->id; }
508
514 virtual double evaluate_mean(double spawners, double ssbzero) { return 0; }
515
520 virtual double evaluate_process(size_t pos) {
522
523 return LogDevs.evaluate_process(pos);
524 }
525
526#ifdef TMB_MODEL
527
528 template <typename Type>
530 std::shared_ptr<fims_info::Information<Type>> info =
532
533 std::shared_ptr<fims_popdy::LogDevs<Type>> recruitment_process =
534 std::make_shared<fims_popdy::LogDevs<Type>>();
535
536 recruitment_process->id = this->id;
537
538 // add to Information
539 info->recruitment_process_models[recruitment_process->id] =
541
542 return true;
543 }
544
549 virtual bool add_to_fims_tmb() {
550#ifdef TMBAD_FRAMEWORK
553#else
558#endif
559
560 return true;
561 }
562
563#endif
564};
565
571 public:
577 std::make_shared<LogRRecruitmentInterface>(*this));
578 }
579
584
589 virtual uint32_t get_id() { return this->id; }
590
596 virtual double evaluate_mean(double spawners, double ssbzero) { return 0; }
597
602 virtual double evaluate_process(size_t pos) {
604
605 return LogR.evaluate_process(pos);
606 }
607
608#ifdef TMB_MODEL
609
610 template <typename Type>
612 std::shared_ptr<fims_info::Information<Type>> info =
614
615 std::shared_ptr<fims_popdy::LogR<Type>> recruitment_process =
616 std::make_shared<fims_popdy::LogR<Type>>();
617
618 recruitment_process->id = this->id;
619
620 // add to Information
621 info->recruitment_process_models[recruitment_process->id] =
623
624 return true;
625 }
626
631 virtual bool add_to_fims_tmb() {
632#ifdef TMBAD_FRAMEWORK
635#else
640#endif
641
642 return true;
643 }
644
645#endif
646};
647
648#endif
Rcpp interface for Beverton–Holt to instantiate from R: beverton_holt <- methods::new(beverton_holt).
Definition rcpp_recruitment.hpp:94
virtual void set_uncertainty(std::map< std::string, std::vector< double > > &se_values)
Sets the uncertainty values for the parameters from the standard error values passed from R.
Definition rcpp_recruitment.hpp:281
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:310
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: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
void get_se_values(std::string name, std::map< std::string, std::vector< double > > &se_values, fims::Vector< double > &values)
Method to extract standard error values from the se_values working map.
Definition rcpp_interface_base.hpp:672
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 for Log–Devs to instantiate from R: log_devs <- methods::new(log_devs).
Definition rcpp_recruitment.hpp:488
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_recruitment.hpp:507
virtual ~LogDevsRecruitmentInterface()
The destructor.
Definition rcpp_recruitment.hpp:501
virtual double evaluate_process(size_t pos)
Evaluate recruitment process using the Log–Devs approach.
Definition rcpp_recruitment.hpp:520
LogDevsRecruitmentInterface()
The constructor.
Definition rcpp_recruitment.hpp:493
virtual double evaluate_mean(double spawners, double ssbzero)
Evaluate mean - returns empty function for this module.
Definition rcpp_recruitment.hpp:514
Rcpp interface for Log–R to instantiate from R: log_r <- methods::new(log_r).
Definition rcpp_recruitment.hpp:570
virtual ~LogRRecruitmentInterface()
The destructor.
Definition rcpp_recruitment.hpp:583
LogRRecruitmentInterface()
The constructor.
Definition rcpp_recruitment.hpp:575
virtual double evaluate_process(size_t pos)
Evaluate recruitment process using the Log–R approach.
Definition rcpp_recruitment.hpp:602
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_recruitment.hpp:589
virtual double evaluate_mean(double spawners, double ssbzero)
Evaluate mean - returns empty function for this module.
Definition rcpp_recruitment.hpp:596
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
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:434
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
Definition fims_vector.hpp:27
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:598
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,...
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:26
fims::Vector< Type > logit_steep
Definition sr_beverton_holt.hpp:31