FIMS  v0.11.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
15
21 public:
25 static uint32_t id_g;
29 uint32_t id;
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};
73
79 public:
118
124 std::make_shared<BevertonHoltRecruitmentInterface>(*this);
127 }
128
146
151
156 virtual uint32_t get_id() { return this->id; }
157
163 this->process_id.set(process_id);
164 }
165
169 virtual double evaluate_mean(double spawners, double phi_0) {
171 BevHolt.logit_steep.resize(1);
172 BevHolt.logit_steep[0] = this->logit_steep[0].initial_value_m;
173 if (this->logit_steep[0].initial_value_m == 1.0) {
174 Rf_warning(
175 "Steepness is subject to a logit transformation. "
176 "Fixing it at 1.0 is not currently possible.");
177 }
178 BevHolt.log_rzero.resize(1);
179 BevHolt.log_rzero[0] = this->log_rzero[0].initial_value_m;
180
181 return BevHolt.evaluate_mean(spawners, phi_0);
182 }
183
188 virtual void finalize() {
189 if (this->finalized) {
190 // log warning that finalize has been called more than once.
191 FIMS_WARNING_LOG("Beverton-Holt Recruitment " +
192 fims::to_string(this->id) +
193 " has been finalized already.");
194 }
195
196 this->finalized = true; // indicate this has been called already
197
198 std::shared_ptr<fims_info::Information<double>> info =
200
202
203 it = info->recruitment_models.find(this->id);
204
205 if (it == info->recruitment_models.end()) {
206 FIMS_WARNING_LOG("Beverton-Holt Recruitment " +
207 fims::to_string(this->id) +
208 " not found in Information.");
209 return;
210 } else {
211 std::shared_ptr<fims_popdy::SRBevertonHolt<double>> recr =
212 std::dynamic_pointer_cast<fims_popdy::SRBevertonHolt<double>>(
213 it->second);
214
215 for (size_t i = 0; i < this->logit_steep.size(); i++) {
216 if (this->logit_steep[i].estimation_type_m.get() == "constant") {
217 this->logit_steep[i].final_value_m =
218 this->logit_steep[i].initial_value_m;
219 } else {
220 this->logit_steep[i].final_value_m = recr->logit_steep[i];
221 }
222 }
223
224 for (size_t i = 0; i < log_rzero.size(); i++) {
225 if (log_rzero[i].estimation_type_m.get() == "constant") {
226 this->log_rzero[i].final_value_m = this->log_rzero[i].initial_value_m;
227 } else {
228 this->log_rzero[i].final_value_m = recr->log_rzero[i];
229 }
230 }
231
232 for (size_t i = 0; i < this->log_devs.size(); i++) {
233 if (this->log_devs[i].estimation_type_m.get() == "constant") {
234 this->log_devs[i].final_value_m = this->log_devs[i].initial_value_m;
235 } else {
236 this->log_devs[i].final_value_m = recr->log_recruit_devs[i];
237 }
238 }
239
240 for (size_t i = 0; i < this->log_r.size(); i++) {
241 if (this->log_r[i].estimation_type_m.get() == "constant") {
242 this->log_r[i].final_value_m = this->log_r[i].initial_value_m;
243 } else {
244 this->log_r[i].final_value_m = recr->log_r[i];
245 }
246 }
247 }
248 }
249
257 virtual std::string to_json() {
258 std::stringstream ss;
259
260 ss << "{\n";
261 ss << " \"module_name\": \"Recruitment\",\n";
262 ss << " \"module_type\": \"Beverton-Holt\",\n";
263 ss << " \"module_id\": " << this->id << ",\n";
264
265 ss << " \"parameters\": [\n{\n";
266 ss << " \"name\": \"logit_steep\",\n";
267 ss << " \"id\":" << this->logit_steep.id_m << ",\n";
268 ss << " \"type\": \"vector\",\n";
269 ss << " \"dimensionality\": {\n";
270 ss << " \"header\": [null],\n";
271 ss << " \"dimensions\": [" << this->logit_steep.size() << "]\n},\n";
272 ss << " \"values\":" << this->logit_steep << "},\n";
273
274 ss << "{\n";
275 ss << " \"name\": \"log_rzero\",\n";
276 ss << " \"id\":" << this->log_rzero.id_m << ",\n";
277 ss << " \"type\": \"vector\",\n";
278 ss << " \"dimensionality\": {\n";
279 ss << " \"header\": [null],\n";
280 ss << " \"dimensions\": [" << this->log_rzero.size() << "]\n},\n";
281 ss << " \"values\":" << this->log_rzero << "},\n";
282
283 ss << "{\n";
284 ss << " \"name\": \"log_devs\",\n";
285 ss << " \"id\":" << this->log_devs.id_m << ",\n";
286 ss << " \"type\": \"vector\",\n";
287 ss << " \"dimensionality\": {\n";
288 ss << " \"header\": [\"n_years-1\"],\n";
289 ss << " \"dimensions\": [" << this->log_devs.size() << "]\n},\n";
290 ss << " \"values\":" << this->log_devs << "}]\n";
291 ss << "}";
292 return ss.str();
293 }
294
295#ifdef TMB_MODEL
296
297 template <typename Type>
298 bool add_to_fims_tmb_internal() {
299 std::shared_ptr<fims_info::Information<Type>> info =
301
302 std::shared_ptr<fims_popdy::SRBevertonHolt<Type>> recruitment =
303 std::make_shared<fims_popdy::SRBevertonHolt<Type>>();
304
305 std::stringstream ss;
306
307 // set relative info
308 recruitment->id = this->id;
309 recruitment->process_id = this->process_id.get();
310 // set logit_steep
311 recruitment->logit_steep.resize(this->logit_steep.size());
312 for (size_t i = 0; i < this->logit_steep.size(); i++) {
313 recruitment->logit_steep[i] = this->logit_steep[i].initial_value_m;
314
315 if (this->logit_steep[i].estimation_type_m.get() == "fixed_effects") {
316 ss.str("");
317 ss << "Recruitment." << this->id << ".logit_steep."
318 << this->logit_steep[i].id_m;
319 info->RegisterParameterName(ss.str());
320 info->RegisterParameter(recruitment->logit_steep[i]);
321 }
322 if (this->logit_steep[i].estimation_type_m.get() == "random_effects") {
323 ss.str("");
324 ss << "Recruitment." << this->id << ".logit_steep."
325 << this->logit_steep[i].id_m;
326 info->RegisterRandomEffectName(ss.str());
327 info->RegisterRandomEffect(recruitment->logit_steep[i]);
328 }
329 }
330 info->variable_map[this->logit_steep.id_m] = &(recruitment)->logit_steep;
331
332 // set log_rzero
333 recruitment->log_rzero.resize(this->log_rzero.size());
334 for (size_t i = 0; i < this->log_rzero.size(); i++) {
335 recruitment->log_rzero[i] = this->log_rzero[i].initial_value_m;
336
337 if (this->log_rzero[i].estimation_type_m.get() == "fixed_effects") {
338 ss.str("");
339 ss << "Recruitment." << this->id << ".log_rzero."
340 << this->log_rzero[i].id_m;
341 info->RegisterParameterName(ss.str());
342 info->RegisterParameter(recruitment->log_rzero[i]);
343 }
344 if (this->log_rzero[i].estimation_type_m.get() == "random_effects") {
345 ss.str("");
346 ss << "Recruitment." << this->id << ".log_rzero."
347 << this->log_rzero[i].id_m;
348 info->RegisterRandomEffectName(ss.str());
349 info->RegisterRandomEffect(recruitment->log_rzero[i]);
350 }
351 }
352 info->variable_map[this->log_rzero.id_m] = &(recruitment)->log_rzero;
353 // set log_recruit_devs
354 recruitment->log_recruit_devs.resize(this->log_devs.size());
355 for (size_t i = 0; i < this->log_devs.size(); i++) {
356 recruitment->log_recruit_devs[i] = this->log_devs[i].initial_value_m;
357
358 if (this->log_devs[i].estimation_type_m.get() == "fixed_effects") {
359 ss.str("");
360 ss << "Recruitment." << this->id << ".log_devs."
361 << this->log_devs[i].id_m;
362 info->RegisterParameterName(ss.str());
363 info->RegisterParameter(recruitment->log_recruit_devs[i]);
364 }
365 if (this->log_devs[i].estimation_type_m.get() == "random_effects") {
366 ss.str("");
367 ss << "Recruitment." << this->id << ".log_devs."
368 << this->log_devs[i].id_m;
369 info->RegisterRandomEffectName(ss.str());
370 info->RegisterRandomEffect(recruitment->log_recruit_devs[i]);
371 }
372 }
373
374 info->variable_map[this->log_devs.id_m] = &(recruitment)->log_recruit_devs;
375
376 // set log_r
377 recruitment->log_r.resize(this->log_r.size());
378 for (size_t i = 0; i < log_r.size(); i++) {
379 recruitment->log_r[i] = this->log_r[i].initial_value_m;
380
381 if (this->log_r[i].estimation_type_m.get() == "fixed_effects") {
382 ss.str("");
383 ss << "Recruitment." << this->id << ".log_r." << this->log_r[i].id_m;
384 info->RegisterParameterName(ss.str());
385 info->RegisterParameter(recruitment->log_r[i]);
386 }
387 if (this->log_r[i].estimation_type_m.get() == "random_effects") {
388 ss.str("");
389 ss << "Recruitment." << this->id << ".log_r." << this->log_r[i].id_m;
390 info->RegisterRandomEffectName(ss.str());
391 info->RegisterRandomEffect(recruitment->log_r[i]);
392 }
393 }
394
395 info->variable_map[this->log_r.id_m] = &(recruitment)->log_r;
396 // set log_expected_recruitment
397 recruitment->log_expected_recruitment.resize(this->n_years.get() - 1);
398 for (size_t i = 0; i < static_cast<size_t>(this->n_years.get() - 1); i++) {
399 recruitment->log_expected_recruitment[i] = 0;
400 }
401 info->variable_map[this->log_expected_recruitment.id_m] =
402 &(recruitment)->log_expected_recruitment;
403
404 // add to Information
405 info->recruitment_models[recruitment->id] = recruitment;
406
407 return true;
408 }
409
414 virtual bool add_to_fims_tmb() {
415 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
416 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
417
418 return true;
419 }
420
421#endif
422};
423
429 public:
435 std::make_shared<LogDevsRecruitmentInterface>(*this));
436 }
437
442
447 virtual uint32_t get_id() { return this->id; }
448
452 virtual double evaluate_mean(double spawners, double phi_0) { return 0; }
453
454#ifdef TMB_MODEL
455
456 template <typename Type>
457 bool add_to_fims_tmb_internal() {
458 std::shared_ptr<fims_info::Information<Type>> info =
460
461 std::shared_ptr<fims_popdy::LogDevs<Type>> recruitment_process =
462 std::make_shared<fims_popdy::LogDevs<Type>>();
463
464 recruitment_process->id = this->id;
465
466 // add to Information
467 info->recruitment_process_models[recruitment_process->id] =
468 recruitment_process;
469
470 return true;
471 }
472
477 virtual bool add_to_fims_tmb() {
478 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
479 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
480
481 return true;
482 }
483
484#endif
485};
486
492 public:
498 std::make_shared<LogRRecruitmentInterface>(*this));
499 }
500
505
510 virtual uint32_t get_id() { return this->id; }
511
515 virtual double evaluate_mean(double spawners, double phi_0) { return 0; }
516
517#ifdef TMB_MODEL
518
519 template <typename Type>
520 bool add_to_fims_tmb_internal() {
521 std::shared_ptr<fims_info::Information<Type>> info =
523
524 std::shared_ptr<fims_popdy::LogR<Type>> recruitment_process =
525 std::make_shared<fims_popdy::LogR<Type>>();
526
527 recruitment_process->id = this->id;
528
529 // add to Information
530 info->recruitment_process_models[recruitment_process->id] =
531 recruitment_process;
532
533 return true;
534 }
535
540 virtual bool add_to_fims_tmb() {
541 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
542 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
543
544 return true;
545 }
546
547#endif
548};
549
550#endif
Rcpp interface for Beverton–Holt to instantiate from R: beverton_holt <- methods::new(beverton_holt).
Definition rcpp_recruitment.hpp:78
VariableVector log_rzero
The natural log of recruitment at unfished biomass.
Definition rcpp_recruitment.hpp:93
VariableVector log_expected_recruitment
Expectation of the recruitment process.
Definition rcpp_recruitment.hpp:105
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_recruitment.hpp:257
fims_double estimated_log_rzero
The estimate of the natural log of recruitment at unfished biomass.
Definition rcpp_recruitment.hpp:113
SharedInt n_years
The number of years.
Definition rcpp_recruitment.hpp:83
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_recruitment.hpp:188
VariableVector log_devs
The natural log of recruitment deviations.
Definition rcpp_recruitment.hpp:97
virtual ~BevertonHoltRecruitmentInterface()
The destructor.
Definition rcpp_recruitment.hpp:150
void SetRecruitmentProcessID(uint32_t process_id)
Set the unique ID for the recruitment process object.
Definition rcpp_recruitment.hpp:162
BevertonHoltRecruitmentInterface(const BevertonHoltRecruitmentInterface &other)
Construct a new Beverton–Holt Recruitment Interface object.
Definition rcpp_recruitment.hpp:134
RealVector estimated_log_devs
The estimates of the natural log of recruitment deviations.
Definition rcpp_recruitment.hpp:117
VariableVector log_r
The recruitment random effect parameter on the natural log scale.
Definition rcpp_recruitment.hpp:101
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_recruitment.hpp:156
fims_double estimated_logit_steep
The estimate of the logit transformation of steepness.
Definition rcpp_recruitment.hpp:109
BevertonHoltRecruitmentInterface()
The constructor.
Definition rcpp_recruitment.hpp:122
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:169
VariableVector logit_steep
The logistic transformation of steepness (h; productivity of the population), where the parameter is ...
Definition rcpp_recruitment.hpp:89
Base class for all interface objects.
Definition rcpp_interface_base.hpp:655
bool finalized
Is the object already finalized? The default is false.
Definition rcpp_interface_base.hpp:660
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:665
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:670
Rcpp interface for Log–Devs to instantiate from R: log_devs <- methods::new(log_devs).
Definition rcpp_recruitment.hpp:428
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_recruitment.hpp:447
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:452
virtual ~LogDevsRecruitmentInterface()
The destructor.
Definition rcpp_recruitment.hpp:441
LogDevsRecruitmentInterface()
The constructor.
Definition rcpp_recruitment.hpp:433
Rcpp interface for Log–R to instantiate from R: log_r <- methods::new(log_r).
Definition rcpp_recruitment.hpp:491
virtual ~LogRRecruitmentInterface()
The destructor.
Definition rcpp_recruitment.hpp:504
LogRRecruitmentInterface()
The constructor.
Definition rcpp_recruitment.hpp:496
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:515
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_recruitment.hpp:510
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:431
Rcpp interface that serves as the parent class for Rcpp recruitment interfaces. This type should be i...
Definition rcpp_recruitment.hpp:20
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
An Rcpp interface class that defines the VariableVector class.
Definition rcpp_interface_base.hpp:143
Variable & get(size_t pos)
An internal accessor for calling a position of a VariableVector from R.
Definition rcpp_interface_base.hpp:266
uint32_t id_m
The local ID of the Variable object.
Definition rcpp_interface_base.hpp:156
void resize(size_t size)
Resizes a VariableVector to the desired length.
Definition rcpp_interface_base.hpp:294
size_t size()
Returns the size of a VariableVector.
Definition rcpp_interface_base.hpp:287
std::map< uint32_t, std::shared_ptr< fims_popdy::RecruitmentBase< Type > > >::iterator recruitment_models_iterator
Definition information.hpp:71
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:262
std::map< uint32_t, std::shared_ptr< fims_popdy::RecruitmentBase< Type > > > recruitment_models
Definition information.hpp:67
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:648
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...
Includes any .hpp files within the subfolders so that only this file needs to included in include sta...
fims::Vector< Type > log_rzero
Definition recruitment_base.hpp:35
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
virtual const Type evaluate_mean(const Type &spawners, const Type &phi_0)
Evaluates expected recruitment from the stock–recruitment relationship before recruitment-process dev...
Definition sr_beverton_holt.hpp:49