FIMS  v0.9.3
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
71 virtual double evaluate_mean(double spawners, double phi_0) = 0;
72
78 virtual double evaluate_process(size_t pos) = 0;
79};
80
86 public:
125
131 std::make_shared<BevertonHoltRecruitmentInterface>(*this);
134 }
135
153
158
163 virtual uint32_t get_id() { return this->id; }
164
170 this->process_id.set(process_id);
171 }
172
176 virtual double evaluate_mean(double spawners, double phi_0) {
178 BevHolt.logit_steep.resize(1);
179 BevHolt.logit_steep[0] = this->logit_steep[0].initial_value_m;
180 if (this->logit_steep[0].initial_value_m == 1.0) {
182 "Steepness is subject to a logit transformation. "
183 "Fixing it at 1.0 is not currently possible.");
184 }
185 BevHolt.log_rzero.resize(1);
186 BevHolt.log_rzero[0] = this->log_rzero[0].initial_value_m;
187
188 return BevHolt.evaluate_mean(spawners, phi_0);
189 }
190
195 virtual double evaluate_process(size_t pos) { return 0; }
196
201 virtual void finalize() {
202 if (this->finalized) {
203 // log warning that finalize has been called more than once.
204 FIMS_WARNING_LOG("Beverton-Holt Recruitment " +
205 fims::to_string(this->id) +
206 " has been finalized already.");
207 }
208
209 this->finalized = true; // indicate this has been called already
210
211 std::shared_ptr<fims_info::Information<double>> info =
213
215
216 it = info->recruitment_models.find(this->id);
217
218 if (it == info->recruitment_models.end()) {
219 FIMS_WARNING_LOG("Beverton-Holt Recruitment " +
220 fims::to_string(this->id) +
221 " not found in Information.");
222 return;
223 } else {
224 std::shared_ptr<fims_popdy::SRBevertonHolt<double>> recr =
225 std::dynamic_pointer_cast<fims_popdy::SRBevertonHolt<double>>(
226 it->second);
227
228 for (size_t i = 0; i < this->logit_steep.size(); i++) {
229 if (this->logit_steep[i].estimation_type_m.get() == "constant") {
230 this->logit_steep[i].final_value_m =
231 this->logit_steep[i].initial_value_m;
232 } else {
233 this->logit_steep[i].final_value_m = recr->logit_steep[i];
234 }
235 }
236
237 for (size_t i = 0; i < log_rzero.size(); i++) {
238 if (log_rzero[i].estimation_type_m.get() == "constant") {
239 this->log_rzero[i].final_value_m = this->log_rzero[i].initial_value_m;
240 } else {
241 this->log_rzero[i].final_value_m = recr->log_rzero[i];
242 }
243 }
244
245 for (size_t i = 0; i < this->log_devs.size(); i++) {
246 if (this->log_devs[i].estimation_type_m.get() == "constant") {
247 this->log_devs[i].final_value_m = this->log_devs[i].initial_value_m;
248 } else {
249 this->log_devs[i].final_value_m = recr->log_recruit_devs[i];
250 }
251 }
252
253 for (size_t i = 0; i < this->log_r.size(); i++) {
254 if (this->log_r[i].estimation_type_m.get() == "constant") {
255 this->log_r[i].final_value_m = this->log_r[i].initial_value_m;
256 } else {
257 this->log_r[i].final_value_m = recr->log_r[i];
258 }
259 }
260 }
261 }
262
270 virtual std::string to_json() {
271 std::stringstream ss;
272
273 ss << "{\n";
274 ss << " \"module_name\": \"Recruitment\",\n";
275 ss << " \"module_type\": \"Beverton-Holt\",\n";
276 ss << " \"module_id\": " << this->id << ",\n";
277
278 ss << " \"parameters\": [\n{\n";
279 ss << " \"name\": \"logit_steep\",\n";
280 ss << " \"id\":" << this->logit_steep.id_m << ",\n";
281 ss << " \"type\": \"vector\",\n";
282 ss << " \"dimensionality\": {\n";
283 ss << " \"header\": [null],\n";
284 ss << " \"dimensions\": [" << this->logit_steep.size() << "]\n},\n";
285 ss << " \"values\":" << this->logit_steep << "},\n";
286
287 ss << "{\n";
288 ss << " \"name\": \"log_rzero\",\n";
289 ss << " \"id\":" << this->log_rzero.id_m << ",\n";
290 ss << " \"type\": \"vector\",\n";
291 ss << " \"dimensionality\": {\n";
292 ss << " \"header\": [null],\n";
293 ss << " \"dimensions\": [" << this->log_rzero.size() << "]\n},\n";
294 ss << " \"values\":" << this->log_rzero << "},\n";
295
296 ss << "{\n";
297 ss << " \"name\": \"log_devs\",\n";
298 ss << " \"id\":" << this->log_devs.id_m << ",\n";
299 ss << " \"type\": \"vector\",\n";
300 ss << " \"dimensionality\": {\n";
301 ss << " \"header\": [\"n_years-1\"],\n";
302 ss << " \"dimensions\": [" << this->log_devs.size() << "]\n},\n";
303 ss << " \"values\":" << this->log_devs << "}]\n";
304 ss << "}";
305 return ss.str();
306 }
307
308#ifdef TMB_MODEL
309
310 template <typename Type>
312 std::shared_ptr<fims_info::Information<Type>> info =
314
315 std::shared_ptr<fims_popdy::SRBevertonHolt<Type>> recruitment =
316 std::make_shared<fims_popdy::SRBevertonHolt<Type>>();
317
318 std::stringstream ss;
319
320 // set relative info
321 recruitment->id = this->id;
322 recruitment->process_id = this->process_id.get();
323 // set logit_steep
324 recruitment->logit_steep.resize(this->logit_steep.size());
325 for (size_t i = 0; i < this->logit_steep.size(); i++) {
326 recruitment->logit_steep[i] = this->logit_steep[i].initial_value_m;
327
328 if (this->logit_steep[i].estimation_type_m.get() == "fixed_effects") {
329 ss.str("");
330 ss << "Recruitment." << this->id << ".logit_steep."
331 << this->logit_steep[i].id_m;
332 info->RegisterParameterName(ss.str());
333 info->RegisterParameter(recruitment->logit_steep[i]);
334 }
335 if (this->logit_steep[i].estimation_type_m.get() == "random_effects") {
336 ss.str("");
337 ss << "Recruitment." << this->id << ".logit_steep."
338 << this->logit_steep[i].id_m;
339 info->RegisterRandomEffectName(ss.str());
340 info->RegisterRandomEffect(recruitment->logit_steep[i]);
341 }
342 }
343 info->variable_map[this->logit_steep.id_m] = &(recruitment)->logit_steep;
344
345 // set log_rzero
346 recruitment->log_rzero.resize(this->log_rzero.size());
347 for (size_t i = 0; i < this->log_rzero.size(); i++) {
348 recruitment->log_rzero[i] = this->log_rzero[i].initial_value_m;
349
350 if (this->log_rzero[i].estimation_type_m.get() == "fixed_effects") {
351 ss.str("");
352 ss << "Recruitment." << this->id << ".log_rzero."
353 << this->log_rzero[i].id_m;
354 info->RegisterParameterName(ss.str());
355 info->RegisterParameter(recruitment->log_rzero[i]);
356 }
357 if (this->log_rzero[i].estimation_type_m.get() == "random_effects") {
358 ss.str("");
359 ss << "Recruitment." << this->id << ".log_rzero."
360 << this->log_rzero[i].id_m;
361 info->RegisterRandomEffectName(ss.str());
362 info->RegisterRandomEffect(recruitment->log_rzero[i]);
363 }
364 }
365 info->variable_map[this->log_rzero.id_m] = &(recruitment)->log_rzero;
366 // set log_recruit_devs
367 recruitment->log_recruit_devs.resize(this->log_devs.size());
368 for (size_t i = 0; i < this->log_devs.size(); i++) {
369 recruitment->log_recruit_devs[i] = this->log_devs[i].initial_value_m;
370
371 if (this->log_devs[i].estimation_type_m.get() == "fixed_effects") {
372 ss.str("");
373 ss << "Recruitment." << this->id << ".log_devs."
374 << this->log_devs[i].id_m;
375 info->RegisterParameterName(ss.str());
376 info->RegisterParameter(recruitment->log_recruit_devs[i]);
377 }
378 if (this->log_devs[i].estimation_type_m.get() == "random_effects") {
379 ss.str("");
380 ss << "Recruitment." << this->id << ".log_devs."
381 << this->log_devs[i].id_m;
382 info->RegisterRandomEffectName(ss.str());
383 info->RegisterRandomEffect(recruitment->log_recruit_devs[i]);
384 }
385 }
386
387 info->variable_map[this->log_devs.id_m] = &(recruitment)->log_recruit_devs;
388
389 // set log_r
390 recruitment->log_r.resize(this->log_r.size());
391 for (size_t i = 0; i < log_r.size(); i++) {
392 recruitment->log_r[i] = this->log_r[i].initial_value_m;
393
394 if (this->log_r[i].estimation_type_m.get() == "fixed_effects") {
395 ss.str("");
396 ss << "Recruitment." << this->id << ".log_r." << this->log_r[i].id_m;
397 info->RegisterParameterName(ss.str());
398 info->RegisterParameter(recruitment->log_r[i]);
399 }
400 if (this->log_r[i].estimation_type_m.get() == "random_effects") {
401 ss.str("");
402 ss << "Recruitment." << this->id << ".log_r." << this->log_r[i].id_m;
403 info->RegisterRandomEffectName(ss.str());
404 info->RegisterRandomEffect(recruitment->log_r[i]);
405 }
406 }
407
408 info->variable_map[this->log_r.id_m] = &(recruitment)->log_r;
409 // set log_expected_recruitment
410 recruitment->log_expected_recruitment.resize(this->n_years.get() - 1);
411 for (size_t i = 0; i < static_cast<size_t>(this->n_years.get() - 1); i++) {
412 recruitment->log_expected_recruitment[i] = 0;
413 }
414 info->variable_map[this->log_expected_recruitment.id_m] =
415 &(recruitment)->log_expected_recruitment;
416
417 // add to Information
418 info->recruitment_models[recruitment->id] = recruitment;
419
420 return true;
421 }
422
427 virtual bool add_to_fims_tmb() {
430
431 return true;
432 }
433
434#endif
435};
436
442 public:
448 std::make_shared<LogDevsRecruitmentInterface>(*this));
449 }
450
455
460 virtual uint32_t get_id() { return this->id; }
461
465 virtual double evaluate_mean(double spawners, double phi_0) { return 0; }
466
471 virtual double evaluate_process(size_t pos) {
473
474 return LogDevs.evaluate_process(pos);
475 }
476
477#ifdef TMB_MODEL
478
479 template <typename Type>
481 std::shared_ptr<fims_info::Information<Type>> info =
483
484 std::shared_ptr<fims_popdy::LogDevs<Type>> recruitment_process =
485 std::make_shared<fims_popdy::LogDevs<Type>>();
486
487 recruitment_process->id = this->id;
488
489 // add to Information
490 info->recruitment_process_models[recruitment_process->id] =
492
493 return true;
494 }
495
500 virtual bool add_to_fims_tmb() {
503
504 return true;
505 }
506
507#endif
508};
509
515 public:
521 std::make_shared<LogRRecruitmentInterface>(*this));
522 }
523
528
533 virtual uint32_t get_id() { return this->id; }
534
538 virtual double evaluate_mean(double spawners, double phi_0) { return 0; }
539
544 virtual double evaluate_process(size_t pos) {
546
547 return LogR.evaluate_process(pos);
548 }
549
550#ifdef TMB_MODEL
551
552 template <typename Type>
554 std::shared_ptr<fims_info::Information<Type>> info =
556
557 std::shared_ptr<fims_popdy::LogR<Type>> recruitment_process =
558 std::make_shared<fims_popdy::LogR<Type>>();
559
560 recruitment_process->id = this->id;
561
562 // add to Information
563 info->recruitment_process_models[recruitment_process->id] =
565
566 return true;
567 }
568
573 virtual bool add_to_fims_tmb() {
576
577 return true;
578 }
579
580#endif
581};
582
583#endif
Rcpp interface for Beverton–Holt to instantiate from R: beverton_holt <- methods::new(beverton_holt).
Definition rcpp_recruitment.hpp:85
ParameterVector log_devs
The natural log of recruitment deviations.
Definition rcpp_recruitment.hpp:104
ParameterVector logit_steep
The logistic transformation of steepness (h; productivity of the population), where the parameter is ...
Definition rcpp_recruitment.hpp:96
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_recruitment.hpp:270
fims_double estimated_log_rzero
The estimate of the natural log of recruitment at unfished biomass.
Definition rcpp_recruitment.hpp:120
SharedInt n_years
The number of years.
Definition rcpp_recruitment.hpp:90
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_recruitment.hpp:201
virtual ~BevertonHoltRecruitmentInterface()
The destructor.
Definition rcpp_recruitment.hpp:157
void SetRecruitmentProcessID(uint32_t process_id)
Set the unique ID for the recruitment process object.
Definition rcpp_recruitment.hpp:169
BevertonHoltRecruitmentInterface(const BevertonHoltRecruitmentInterface &other)
Construct a new Beverton–Holt Recruitment Interface object.
Definition rcpp_recruitment.hpp:141
ParameterVector log_r
The recruitment random effect parameter on the natural log scale.
Definition rcpp_recruitment.hpp:108
ParameterVector log_rzero
The natural log of recruitment at unfished biomass.
Definition rcpp_recruitment.hpp:100
virtual double evaluate_process(size_t pos)
Evaluate recruitment process - returns 0 in this module.
Definition rcpp_recruitment.hpp:195
RealVector estimated_log_devs
The estimates of the natural log of recruitment deviations.
Definition rcpp_recruitment.hpp:124
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_recruitment.hpp:163
fims_double estimated_logit_steep
The estimate of the logit transformation of steepness.
Definition rcpp_recruitment.hpp:116
BevertonHoltRecruitmentInterface()
The constructor.
Definition rcpp_recruitment.hpp:129
virtual double evaluate_mean(double spawners, double phi_0)
Evaluates expected recruitment from the stock–recruitment relationship before recruitment-process dev...
Definition rcpp_recruitment.hpp:176
ParameterVector log_expected_recruitment
Expectation of the recruitment process.
Definition rcpp_recruitment.hpp:112
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 for Log–Devs to instantiate from R: log_devs <- methods::new(log_devs).
Definition rcpp_recruitment.hpp:441
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_recruitment.hpp:460
virtual double evaluate_mean(double spawners, double phi_0)
Evaluates expected recruitment from the stock–recruitment relationship before recruitment-process dev...
Definition rcpp_recruitment.hpp:465
virtual ~LogDevsRecruitmentInterface()
The destructor.
Definition rcpp_recruitment.hpp:454
virtual double evaluate_process(size_t pos)
Evaluate recruitment process using the Log–Devs approach.
Definition rcpp_recruitment.hpp:471
LogDevsRecruitmentInterface()
The constructor.
Definition rcpp_recruitment.hpp:446
Rcpp interface for Log–R to instantiate from R: log_r <- methods::new(log_r).
Definition rcpp_recruitment.hpp:514
virtual ~LogRRecruitmentInterface()
The destructor.
Definition rcpp_recruitment.hpp:527
LogRRecruitmentInterface()
The constructor.
Definition rcpp_recruitment.hpp:519
virtual double evaluate_process(size_t pos)
Evaluate recruitment process using the Log–R approach.
Definition rcpp_recruitment.hpp:544
virtual double evaluate_mean(double spawners, double phi_0)
Evaluates expected recruitment from the stock–recruitment relationship before recruitment-process dev...
Definition rcpp_recruitment.hpp:538
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_recruitment.hpp:533
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
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:413
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 double evaluate_mean(double spawners, double phi_0)=0
Evaluates expected recruitment from the stock–recruitment relationship before recruitment-process dev...
virtual ~RecruitmentInterfaceBase()
The destructor.
Definition rcpp_recruitment.hpp:63
SharedInt process_id
The process id of the RecruitmentInterfaceBase object.
Definition rcpp_recruitment.hpp:33
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:29
int get() const
Retrieve the value of the integer.
Definition rcpp_shared_primitive.hpp:127
A class that provides shared ownership of an integer value.
Definition rcpp_shared_primitive.hpp:789
std::map< uint32_t, std::shared_ptr< fims_popdy::RecruitmentBase< Type > > >::iterator recruitment_models_iterator
Definition information.hpp:67
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:237
std::map< uint32_t, std::shared_ptr< fims_popdy::RecruitmentBase< Type > > > recruitment_models
Definition information.hpp:63
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:648
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,...
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