FIMS  v0.9.3
Loading...
Searching...
No Matches
rcpp_selectivity.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_SELECTIVITY_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_SELECTIVITY_HPP
11
12#include "../../../population_dynamics/selectivity/selectivity.hpp"
14
20 public:
24 static uint32_t id_g;
34 static std::map<uint32_t, std::shared_ptr<SelectivityInterfaceBase>>
36
42 /* Create instance of map: key is id and value is pointer to
43 SelectivityInterfaceBase */
44 // SelectivityInterfaceBase::live_objects[this->id] = this;
45 }
46
54
59
63 virtual uint32_t get_id() = 0;
64
69 virtual double evaluate(double x) = 0;
70};
71
78 public:
87
93 std::make_shared<LogisticSelectivityInterface>(*this);
96 }
97
107
112
117 virtual uint32_t get_id() { return this->id; }
118
124 virtual double evaluate(double x) {
127 LogisticSel.inflection_point[0] = this->inflection_point[0].initial_value_m;
128 LogisticSel.slope.resize(1);
129 LogisticSel.slope[0] = this->slope[0].initial_value_m;
130 return LogisticSel.evaluate(x);
131 }
132
137 virtual void finalize() {
138 if (this->finalized) {
139 // log warning that finalize has been called more than once.
140 FIMS_WARNING_LOG("Logistic Selectivity " + fims::to_string(this->id) +
141 " has been finalized already.");
142 }
143
144 this->finalized = true; // indicate this has been called already
145
146 std::shared_ptr<fims_info::Information<double>> info =
148
150
151 // search for maturity in Information
152 it = info->selectivity_models.find(this->id);
153 // if not found, just return
154 if (it == info->selectivity_models.end()) {
155 FIMS_WARNING_LOG("Logistic Selectivity " + fims::to_string(this->id) +
156 " not found in Information.");
157 return;
158 } else {
159 std::shared_ptr<fims_popdy::LogisticSelectivity<double>> sel =
160 std::dynamic_pointer_cast<fims_popdy::LogisticSelectivity<double>>(
161 it->second);
162
163 for (size_t i = 0; i < inflection_point.size(); i++) {
164 if (this->inflection_point[i].estimation_type_m.get() == "constant") {
165 this->inflection_point[i].final_value_m =
166 this->inflection_point[i].initial_value_m;
167 } else {
168 this->inflection_point[i].final_value_m = sel->inflection_point[i];
169 }
170 }
171
172 for (size_t i = 0; i < slope.size(); i++) {
173 if (this->slope[i].estimation_type_m.get() == "constant") {
174 this->slope[i].final_value_m = this->slope[i].initial_value_m;
175 } else {
176 this->slope[i].final_value_m = sel->slope[i];
177 }
178 }
179 }
180 }
181
188 virtual std::string to_json() {
189 std::stringstream ss;
190
191 ss << "{\n";
192 ss << " \"module_name\":\"Selectivity\",\n";
193 ss << " \"module_type\": \"Logistic\",\n";
194 ss << " \"module_id\": " << this->id << ",\n";
195
196 ss << " \"parameters\": [\n{\n";
197 ss << " \"name\": \"inflection_point\",\n";
198 ss << " \"id\":" << this->inflection_point.id_m << ",\n";
199 ss << " \"type\": \"vector\",\n";
200 ss << " \"dimensionality\": {\n";
201 ss << " \"header\": [null],\n";
202 ss << " \"dimensions\": [1]\n},\n";
203 ss << " \"values\":" << this->inflection_point << "},\n ";
204
205 ss << "{\n";
206 ss << " \"name\": \"slope\",\n";
207 ss << " \"id\":" << this->slope.id_m << ",\n";
208 ss << " \"type\": \"vector\",\n";
209 ss << " \"dimensionality\": {\n";
210 ss << " \"header\": [null],\n";
211 ss << " \"dimensions\": [1]\n},\n";
212 ss << " \"values\":" << this->slope << "}]\n";
213
214 ss << "}";
215
216 return ss.str();
217 }
218
219#ifdef TMB_MODEL
220
221 template <typename Type>
223 std::shared_ptr<fims_info::Information<Type>> info =
225
226 std::shared_ptr<fims_popdy::LogisticSelectivity<Type>> selectivity =
227 std::make_shared<fims_popdy::LogisticSelectivity<Type>>();
228 std::stringstream ss;
229 // set relative info
230 selectivity->id = this->id;
231 selectivity->inflection_point.resize(this->inflection_point.size());
232 for (size_t i = 0; i < this->inflection_point.size(); i++) {
233 selectivity->inflection_point[i] =
234 this->inflection_point[i].initial_value_m;
235 if (this->inflection_point[i].estimation_type_m.get() ==
236 "fixed_effects") {
237 ss.str("");
238 ss << "Selectivity." << this->id << ".inflection_point."
239 << this->inflection_point[i].id_m;
240 info->RegisterParameterName(ss.str());
241 info->RegisterParameter(selectivity->inflection_point[i]);
242 }
243 if (this->inflection_point[i].estimation_type_m.get() ==
244 "random_effects") {
245 ss.str("");
246 ss << "Selectivity." << this->id << ".inflection_point."
247 << this->inflection_point[i].id_m;
248 info->RegisterRandomEffect(selectivity->inflection_point[i]);
249 info->RegisterRandomEffectName(ss.str());
250 }
251 }
252 info->variable_map[this->inflection_point.id_m] =
253 &(selectivity)->inflection_point;
254
255 selectivity->slope.resize(this->slope.size());
256 for (size_t i = 0; i < this->slope.size(); i++) {
257 selectivity->slope[i] = this->slope[i].initial_value_m;
258 if (this->slope[i].estimation_type_m.get() == "fixed_effects") {
259 ss.str("");
260 ss << "Selectivity." << this->id << ".slope." << this->slope[i].id_m;
261 info->RegisterParameterName(ss.str());
262 info->RegisterParameter(selectivity->slope[i]);
263 }
264 if (this->slope[i].estimation_type_m.get() == "random_effects") {
265 ss.str("");
266 ss << "Selectivity." << this->id << ".slope." << this->slope[i].id_m;
267 info->RegisterRandomEffectName(ss.str());
268 info->RegisterRandomEffect(selectivity->slope[i]);
269 }
270 }
271 info->variable_map[this->slope.id_m] = &(selectivity)->slope;
272
273 // add to Information
274 info->selectivity_models[selectivity->id] = selectivity;
275
276 return true;
277 }
278
283 virtual bool add_to_fims_tmb() {
286
287 return true;
288 }
289
290#endif
291};
292
299 public:
311 std::make_shared<DoubleLogisticSelectivityInterface>(*this);
314 }
315
328
330
332 virtual uint32_t get_id() { return this->id; }
333
338 virtual double evaluate(double x) {
341 DoubleLogisticSel.inflection_point_asc[0] =
342 this->inflection_point_asc[0].initial_value_m;
343 DoubleLogisticSel.slope_asc.resize(1);
344 DoubleLogisticSel.slope_asc[0] = this->slope_asc[0].initial_value_m;
345 DoubleLogisticSel.inflection_point_desc.resize(1);
346 DoubleLogisticSel.inflection_point_desc[0] =
347 this->inflection_point_desc[0].initial_value_m;
348 DoubleLogisticSel.slope_desc.resize(1);
349 DoubleLogisticSel.slope_desc[0] = this->slope_desc[0].initial_value_m;
350 return DoubleLogisticSel.evaluate(x);
351 }
356 virtual void finalize() {
357 if (this->finalized) {
358 // log warning that finalize has been called more than once.
359 FIMS_WARNING_LOG("Double Logistic Selectivity " +
360 fims::to_string(this->id) +
361 " has been finalized already.");
362 }
363
364 this->finalized = true; // indicate this has been called already
365
366 std::shared_ptr<fims_info::Information<double>> info =
368
370
371 // search for maturity in Information
372 it = info->selectivity_models.find(this->id);
373 // if not found, just return
374 if (it == info->selectivity_models.end()) {
375 FIMS_WARNING_LOG("Double Logistic Selectivity " +
376 fims::to_string(this->id) +
377 " not found in Information.");
378 return;
379 } else {
380 std::shared_ptr<fims_popdy::DoubleLogisticSelectivity<double>> sel =
381 std::dynamic_pointer_cast<
383
384 for (size_t i = 0; i < inflection_point_asc.size(); i++) {
385 if (this->inflection_point_asc[i].estimation_type_m.get() ==
386 "constant") {
387 this->inflection_point_asc[i].final_value_m =
388 this->inflection_point_asc[i].initial_value_m;
389 } else {
390 this->inflection_point_asc[i].final_value_m =
391 sel->inflection_point_asc[i];
392 }
393 }
394
395 for (size_t i = 0; i < slope_asc.size(); i++) {
396 if (this->slope_asc[i].estimation_type_m.get() == "constant") {
397 this->slope_asc[i].final_value_m = this->slope_asc[i].initial_value_m;
398 } else {
399 this->slope_asc[i].final_value_m = sel->slope_asc[i];
400 }
401 }
402
403 for (size_t i = 0; i < inflection_point_desc.size(); i++) {
404 if (this->inflection_point_desc[i].estimation_type_m.get() ==
405 "constant") {
406 this->inflection_point_desc[i].final_value_m =
407 this->inflection_point_desc[i].initial_value_m;
408 } else {
409 this->inflection_point_desc[i].final_value_m =
410 sel->inflection_point_desc[i];
411 }
412 }
413
414 for (size_t i = 0; i < slope_desc.size(); i++) {
415 if (this->slope_desc[i].estimation_type_m.get() == "constant") {
416 this->slope_desc[i].final_value_m =
417 this->slope_desc[i].initial_value_m;
418 } else {
419 this->slope_desc[i].final_value_m = sel->slope_desc[i];
420 }
421 }
422 }
423 }
424
428 virtual std::string to_json() {
429 std::stringstream ss;
430
431 ss << "{\n";
432 ss << " \"module_name\": \"Selectivity\",\n";
433 ss << " \"module_type\": \"DoubleLogistic\",\n";
434 ss << " \"module_id\": " << this->id << ",\n";
435
436 ss << " \"parameters\":[\n{\n";
437 ss << " \"name\": \"inflection_point_asc\",\n";
438 ss << " \"id\":" << this->inflection_point_asc.id_m << ",\n";
439 ss << " \"type\": \"vector\",\n";
440 ss << " \"dimensionality\": {\n";
441 ss << " \"header\": [null],\n";
442 ss << " \"dimensions\": [1]\n},\n";
443 ss << " \"values\":" << this->inflection_point_asc << "},\n";
444
445 ss << "{\n";
446 ss << " \"name\": \"slope_asc\",\n";
447 ss << " \"id\":" << this->slope_asc.id_m << ",\n";
448 ss << " \"type\": \"vector\",\n";
449 ss << " \"dimensionality\": {\n";
450 ss << " \"header\": [null],\n";
451 ss << " \"dimensions\": [1]\n},\n";
452 ss << " \"values\":" << this->slope_asc << "},\n";
453
454 ss << " {\n";
455 ss << " \"name\": \"inflection_point_desc\",\n";
456 ss << " \"id\":" << this->inflection_point_desc.id_m << ",\n";
457 ss << " \"type\": \"vector\",\n";
458 ss << " \"dimensionality\": {\n";
459 ss << " \"header\": [null],\n";
460 ss << " \"dimensions\": [1]\n},\n";
461 ss << " \"values\":" << this->inflection_point_desc << "},\n";
462
463 ss << "{\n";
464 ss << " \"name\": \"slope_desc\",\n";
465 ss << " \"id\":" << this->slope_desc.id_m << ",\n";
466 ss << " \"type\": \"vector\",\n";
467 ss << " \"dimensionality\": {\n";
468 ss << " \"header\": [null],\n";
469 ss << " \"dimensions\": [1]\n},\n";
470 ss << " \"values\":" << this->slope_desc << "}]\n";
471
472 ss << "}";
473
474 return ss.str();
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::DoubleLogisticSelectivity<Type>> selectivity =
485 std::make_shared<fims_popdy::DoubleLogisticSelectivity<Type>>();
486
487 std::stringstream ss;
488 // set relative info
489 selectivity->id = this->id;
490 selectivity->inflection_point_asc.resize(this->inflection_point_asc.size());
491 for (size_t i = 0; i < this->inflection_point_asc.size(); i++) {
492 selectivity->inflection_point_asc[i] =
493 this->inflection_point_asc[i].initial_value_m;
494 if (this->inflection_point_asc[i].estimation_type_m.get() ==
495 "fixed_effects") {
496 ss.str("");
497 ss << "Selectivity." << this->id << ".inflection_point_asc."
498 << this->inflection_point_asc[i].id_m;
499 info->RegisterParameterName(ss.str());
500 info->RegisterParameter(selectivity->inflection_point_asc[i]);
501 }
502 if (this->inflection_point_asc[i].estimation_type_m.get() ==
503 "random_effects") {
504 ss.str("");
505 ss << "Selectivity." << this->id << ".inflection_point_asc."
506 << this->inflection_point_asc[i].id_m;
507 info->RegisterRandomEffectName(ss.str());
508 info->RegisterRandomEffect(selectivity->inflection_point_asc[i]);
509 }
510 }
511 info->variable_map[this->inflection_point_asc.id_m] =
512 &(selectivity)->inflection_point_asc;
513
514 selectivity->slope_asc.resize(this->slope_asc.size());
515 for (size_t i = 0; i < this->slope_asc.size(); i++) {
516 selectivity->slope_asc[i] = this->slope_asc[i].initial_value_m;
517
518 if (this->slope_asc[i].estimation_type_m.get() == "fixed_effects") {
519 ss.str("");
520 ss << "Selectivity." << this->id << ".slope_asc."
521 << this->slope_asc[i].id_m;
522 info->RegisterParameterName(ss.str());
523 info->RegisterParameter(selectivity->slope_asc[i]);
524 }
525 if (this->slope_asc[i].estimation_type_m.get() == "random_effects") {
526 ss.str("");
527 ss << "Selectivity." << this->id << ".slope_asc."
528 << this->slope_asc[i].id_m;
529 info->RegisterRandomEffectName(ss.str());
530 info->RegisterRandomEffect(selectivity->slope_asc[i]);
531 }
532 }
533 info->variable_map[this->slope_asc.id_m] = &(selectivity)->slope_asc;
534
535 selectivity->inflection_point_desc.resize(
536 this->inflection_point_desc.size());
537 for (size_t i = 0; i < this->inflection_point_desc.size(); i++) {
538 selectivity->inflection_point_desc[i] =
539 this->inflection_point_desc[i].initial_value_m;
540
541 if (this->inflection_point_desc[i].estimation_type_m.get() ==
542 "fixed_effects") {
543 ss.str("");
544 ss << "Selectivity." << this->id << ".inflection_point_desc."
545 << this->inflection_point_desc[i].id_m;
546 info->RegisterParameterName(ss.str());
547 info->RegisterParameter(selectivity->inflection_point_desc[i]);
548 }
549 if (this->inflection_point_desc[i].estimation_type_m.get() ==
550 "random_effects") {
551 ss.str("");
552 ss << "Selectivity." << this->id << ".inflection_point_desc."
553 << this->inflection_point_desc[i].id_m;
554 info->RegisterRandomEffectName(ss.str());
555 info->RegisterRandomEffect(selectivity->inflection_point_desc[i]);
556 }
557 }
558 info->variable_map[this->inflection_point_desc.id_m] =
559 &(selectivity)->inflection_point_desc;
560
561 selectivity->slope_desc.resize(this->slope_desc.size());
562 for (size_t i = 0; i < this->slope_desc.size(); i++) {
563 selectivity->slope_desc[i] = this->slope_desc[i].initial_value_m;
564
565 if (this->slope_desc[i].estimation_type_m.get() == "fixed_effects") {
566 ss.str("");
567 ss << "Selectivity." << this->id << ".slope_desc."
568 << this->slope_desc[i].id_m;
569 info->RegisterParameterName(ss.str());
570 info->RegisterParameter(selectivity->slope_desc[i]);
571 }
572 if (this->slope_desc[i].estimation_type_m.get() == "random_effects") {
573 ss.str("");
574 ss << "Selectivity." << this->id << ".slope_desc."
575 << this->slope_desc[i].id_m;
576 info->RegisterRandomEffectName(ss.str());
577 info->RegisterRandomEffect(selectivity->slope_desc[i]);
578 }
579 }
580
581 info->variable_map[this->slope_desc.id_m] = &(selectivity)->slope_desc;
582
583 // add to Information
584 info->selectivity_models[selectivity->id] = selectivity;
585
586 return true;
587 }
588
593 virtual bool add_to_fims_tmb() {
596
597 return true;
598 }
599
600#endif
601};
602
603#endif
Rcpp interface for logistic selectivity as an S4 object. To instantiate from R: logistic_selectivity ...
Definition rcpp_selectivity.hpp:298
ParameterVector slope_desc
Definition rcpp_selectivity.hpp:307
ParameterVector inflection_point_desc
Definition rcpp_selectivity.hpp:304
DoubleLogisticSelectivityInterface(const DoubleLogisticSelectivityInterface &other)
Construct a new Double Logistic Selectivity Interface object.
Definition rcpp_selectivity.hpp:321
ParameterVector slope_asc
Definition rcpp_selectivity.hpp:303
virtual double evaluate(double x)
evaluate the double logistic selectivity function
Definition rcpp_selectivity.hpp:338
ParameterVector inflection_point_asc
Definition rcpp_selectivity.hpp:300
virtual uint32_t get_id()
returns the id for the double logistic selectivity interface
Definition rcpp_selectivity.hpp:332
virtual std::string to_json()
Convert the data to json representation for the output.
Definition rcpp_selectivity.hpp:428
virtual void finalize()
finalize function. Extracts derived quantities back to the Rcpp interface object from the Information...
Definition rcpp_selectivity.hpp:356
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 logistic selectivity to instantiate the object from R: logistic_selectivity <- met...
Definition rcpp_selectivity.hpp:77
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_selectivity.hpp:137
ParameterVector inflection_point
The index value at which the response reaches 0.5.
Definition rcpp_selectivity.hpp:82
LogisticSelectivityInterface()
The constructor.
Definition rcpp_selectivity.hpp:91
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_selectivity.hpp:188
virtual ~LogisticSelectivityInterface()
The destructor.
Definition rcpp_selectivity.hpp:111
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_selectivity.hpp:117
ParameterVector slope
The width of the curve at the inflection point.
Definition rcpp_selectivity.hpp:86
LogisticSelectivityInterface(const LogisticSelectivityInterface &other)
Construct a new Logistic Selectivity Interface object.
Definition rcpp_selectivity.hpp:103
virtual double evaluate(double x)
Evaluate selectivity using the logistic function.
Definition rcpp_selectivity.hpp:124
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
Rcpp interface that serves as the parent class for Rcpp selectivity interfaces. This type should be i...
Definition rcpp_selectivity.hpp:19
virtual ~SelectivityInterfaceBase()
The destructor.
Definition rcpp_selectivity.hpp:58
SelectivityInterfaceBase()
The constructor.
Definition rcpp_selectivity.hpp:40
virtual uint32_t get_id()=0
Get the ID for the child selectivity interface objects to inherit.
SelectivityInterfaceBase(const SelectivityInterfaceBase &other)
Construct a new Selectivity Interface Base object.
Definition rcpp_selectivity.hpp:52
static uint32_t id_g
The static id of the SelectivityInterfaceBase.
Definition rcpp_selectivity.hpp:24
virtual double evaluate(double x)=0
A method for each child selectivity interface object to inherit so each selectivity option can have a...
uint32_t id
The local id of the SelectivityInterfaceBase object.
Definition rcpp_selectivity.hpp:28
static std::map< uint32_t, std::shared_ptr< SelectivityInterfaceBase > > live_objects
The map associating the IDs of SelectivityInterfaceBase to the objects. This is a live object,...
Definition rcpp_selectivity.hpp:35
std::map< uint32_t, std::shared_ptr< fims_popdy::SelectivityBase< Type > > > selectivity_models
Definition information.hpp:79
std::map< uint32_t, std::shared_ptr< fims_popdy::SelectivityBase< Type > > >::iterator selectivity_models_iterator
Definition information.hpp:83
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
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,...
DoubleLogisticSelectivity class that returns the double logistic function value from fims_math.
Definition double_logistic.hpp:23
fims::Vector< Type > inflection_point_asc
Definition double_logistic.hpp:24
fims::Vector< Type > inflection_point_desc
Definition double_logistic.hpp:30
virtual const Type evaluate(const Type &x)
Method of the double logistic selectivity class that implements the double logistic function from FIM...
Definition double_logistic.hpp:52
LogisticSelectivity class that returns the logistic function value from fims_math.
Definition logistic.hpp:29
fims::Vector< Type > inflection_point
Definition logistic.hpp:31