FIMS  v0.8.1
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// static id of the SelectivityInterfaceBase object
73// local id of the SelectivityInterfaceBase object map relating the ID of the
74// SelectivityInterfaceBase to the SelectivityInterfaceBase objects
75std::map<uint32_t, std::shared_ptr<SelectivityInterfaceBase>>
77
84 public:
93
99 std::make_shared<LogisticSelectivityInterface>(*this);
102 }
103
113
118
123 virtual uint32_t get_id() { return this->id; }
124
130 virtual double evaluate(double x) {
133 LogisticSel.inflection_point[0] = this->inflection_point[0].initial_value_m;
134 LogisticSel.slope.resize(1);
135 LogisticSel.slope[0] = this->slope[0].initial_value_m;
136 return LogisticSel.evaluate(x);
137 }
138
143 virtual void finalize() {
144 if (this->finalized) {
145 // log warning that finalize has been called more than once.
146 FIMS_WARNING_LOG("Logistic Selectivity " + fims::to_string(this->id) +
147 " has been finalized already.");
148 }
149
150 this->finalized = true; // indicate this has been called already
151
152 std::shared_ptr<fims_info::Information<double>> info =
154
156
157 // search for maturity in Information
158 it = info->selectivity_models.find(this->id);
159 // if not found, just return
160 if (it == info->selectivity_models.end()) {
161 FIMS_WARNING_LOG("Logistic Selectivity " + fims::to_string(this->id) +
162 " not found in Information.");
163 return;
164 } else {
165 std::shared_ptr<fims_popdy::LogisticSelectivity<double>> sel =
166 std::dynamic_pointer_cast<fims_popdy::LogisticSelectivity<double>>(
167 it->second);
168
169 for (size_t i = 0; i < inflection_point.size(); i++) {
170 if (this->inflection_point[i].estimation_type_m.get() == "constant") {
171 this->inflection_point[i].final_value_m =
172 this->inflection_point[i].initial_value_m;
173 } else {
174 this->inflection_point[i].final_value_m = sel->inflection_point[i];
175 }
176 }
177
178 for (size_t i = 0; i < slope.size(); i++) {
179 if (this->slope[i].estimation_type_m.get() == "constant") {
180 this->slope[i].final_value_m = this->slope[i].initial_value_m;
181 } else {
182 this->slope[i].final_value_m = sel->slope[i];
183 }
184 }
185 }
186 }
187
194 virtual std::string to_json() {
195 std::stringstream ss;
196
197 ss << "{\n";
198 ss << " \"module_name\":\"Selectivity\",\n";
199 ss << " \"module_type\": \"Logistic\",\n";
200 ss << " \"module_id\": " << this->id << ",\n";
201
202 ss << " \"parameters\": [\n{\n";
203 ss << " \"name\": \"inflection_point\",\n";
204 ss << " \"id\":" << this->inflection_point.id_m << ",\n";
205 ss << " \"type\": \"vector\",\n";
206 ss << " \"dimensionality\": {\n";
207 ss << " \"header\": [null],\n";
208 ss << " \"dimensions\": [1]\n},\n";
209 ss << " \"values\":" << this->inflection_point << "},\n ";
210
211 ss << "{\n";
212 ss << " \"name\": \"slope\",\n";
213 ss << " \"id\":" << this->slope.id_m << ",\n";
214 ss << " \"type\": \"vector\",\n";
215 ss << " \"dimensionality\": {\n";
216 ss << " \"header\": [null],\n";
217 ss << " \"dimensions\": [1]\n},\n";
218 ss << " \"values\":" << this->slope << "}]\n";
219
220 ss << "}";
221
222 return ss.str();
223 }
224
225#ifdef TMB_MODEL
226
227 template <typename Type>
229 std::shared_ptr<fims_info::Information<Type>> info =
231
232 std::shared_ptr<fims_popdy::LogisticSelectivity<Type>> selectivity =
233 std::make_shared<fims_popdy::LogisticSelectivity<Type>>();
234 std::stringstream ss;
235 // set relative info
236 selectivity->id = this->id;
237 selectivity->inflection_point.resize(this->inflection_point.size());
238 for (size_t i = 0; i < this->inflection_point.size(); i++) {
239 selectivity->inflection_point[i] =
240 this->inflection_point[i].initial_value_m;
241 if (this->inflection_point[i].estimation_type_m.get() ==
242 "fixed_effects") {
243 ss.str("");
244 ss << "Selectivity." << this->id << ".inflection_point."
245 << this->inflection_point[i].id_m;
246 info->RegisterParameterName(ss.str());
247 info->RegisterParameter(selectivity->inflection_point[i]);
248 }
249 if (this->inflection_point[i].estimation_type_m.get() ==
250 "random_effects") {
251 ss.str("");
252 ss << "Selectivity." << this->id << ".inflection_point."
253 << this->inflection_point[i].id_m;
254 info->RegisterRandomEffect(selectivity->inflection_point[i]);
255 info->RegisterRandomEffectName(ss.str());
256 }
257 }
258 info->variable_map[this->inflection_point.id_m] =
259 &(selectivity)->inflection_point;
260
261 selectivity->slope.resize(this->slope.size());
262 for (size_t i = 0; i < this->slope.size(); i++) {
263 selectivity->slope[i] = this->slope[i].initial_value_m;
264 if (this->slope[i].estimation_type_m.get() == "fixed_effects") {
265 ss.str("");
266 ss << "Selectivity." << this->id << ".slope." << this->slope[i].id_m;
267 info->RegisterParameterName(ss.str());
268 info->RegisterParameter(selectivity->slope[i]);
269 }
270 if (this->slope[i].estimation_type_m.get() == "random_effects") {
271 ss.str("");
272 ss << "Selectivity." << this->id << ".slope." << this->slope[i].id_m;
273 info->RegisterRandomEffectName(ss.str());
274 info->RegisterRandomEffect(selectivity->slope[i]);
275 }
276 }
277 info->variable_map[this->slope.id_m] = &(selectivity)->slope;
278
279 // add to Information
280 info->selectivity_models[selectivity->id] = selectivity;
281
282 return true;
283 }
284
289 virtual bool add_to_fims_tmb() {
292
293 return true;
294 }
295
296#endif
297};
298
305 public:
317 std::make_shared<DoubleLogisticSelectivityInterface>(*this);
320 }
321
334
336
338 virtual uint32_t get_id() { return this->id; }
339
344 virtual double evaluate(double x) {
347 DoubleLogisticSel.inflection_point_asc[0] =
348 this->inflection_point_asc[0].initial_value_m;
349 DoubleLogisticSel.slope_asc.resize(1);
350 DoubleLogisticSel.slope_asc[0] = this->slope_asc[0].initial_value_m;
351 DoubleLogisticSel.inflection_point_desc.resize(1);
352 DoubleLogisticSel.inflection_point_desc[0] =
353 this->inflection_point_desc[0].initial_value_m;
354 DoubleLogisticSel.slope_desc.resize(1);
355 DoubleLogisticSel.slope_desc[0] = this->slope_desc[0].initial_value_m;
356 return DoubleLogisticSel.evaluate(x);
357 }
362 virtual void finalize() {
363 if (this->finalized) {
364 // log warning that finalize has been called more than once.
365 FIMS_WARNING_LOG("Double Logistic Selectivity " +
366 fims::to_string(this->id) +
367 " has been finalized already.");
368 }
369
370 this->finalized = true; // indicate this has been called already
371
372 std::shared_ptr<fims_info::Information<double>> info =
374
376
377 // search for maturity in Information
378 it = info->selectivity_models.find(this->id);
379 // if not found, just return
380 if (it == info->selectivity_models.end()) {
381 FIMS_WARNING_LOG("Double Logistic Selectivity " +
382 fims::to_string(this->id) +
383 " not found in Information.");
384 return;
385 } else {
386 std::shared_ptr<fims_popdy::DoubleLogisticSelectivity<double>> sel =
387 std::dynamic_pointer_cast<
389
390 for (size_t i = 0; i < inflection_point_asc.size(); i++) {
391 if (this->inflection_point_asc[i].estimation_type_m.get() ==
392 "constant") {
393 this->inflection_point_asc[i].final_value_m =
394 this->inflection_point_asc[i].initial_value_m;
395 } else {
396 this->inflection_point_asc[i].final_value_m =
397 sel->inflection_point_asc[i];
398 }
399 }
400
401 for (size_t i = 0; i < slope_asc.size(); i++) {
402 if (this->slope_asc[i].estimation_type_m.get() == "constant") {
403 this->slope_asc[i].final_value_m = this->slope_asc[i].initial_value_m;
404 } else {
405 this->slope_asc[i].final_value_m = sel->slope_asc[i];
406 }
407 }
408
409 for (size_t i = 0; i < inflection_point_desc.size(); i++) {
410 if (this->inflection_point_desc[i].estimation_type_m.get() ==
411 "constant") {
412 this->inflection_point_desc[i].final_value_m =
413 this->inflection_point_desc[i].initial_value_m;
414 } else {
415 this->inflection_point_desc[i].final_value_m =
416 sel->inflection_point_desc[i];
417 }
418 }
419
420 for (size_t i = 0; i < slope_desc.size(); i++) {
421 if (this->slope_desc[i].estimation_type_m.get() == "constant") {
422 this->slope_desc[i].final_value_m =
423 this->slope_desc[i].initial_value_m;
424 } else {
425 this->slope_desc[i].final_value_m = sel->slope_desc[i];
426 }
427 }
428 }
429 }
430
434 virtual std::string to_json() {
435 std::stringstream ss;
436
437 ss << "{\n";
438 ss << " \"module_name\": \"Selectivity\",\n";
439 ss << " \"module_type\": \"DoubleLogistic\",\n";
440 ss << " \"module_id\": " << this->id << ",\n";
441
442 ss << " \"parameters\":[\n{\n";
443 ss << " \"name\": \"inflection_point_asc\",\n";
444 ss << " \"id\":" << this->inflection_point_asc.id_m << ",\n";
445 ss << " \"type\": \"vector\",\n";
446 ss << " \"dimensionality\": {\n";
447 ss << " \"header\": [null],\n";
448 ss << " \"dimensions\": [1]\n},\n";
449 ss << " \"values\":" << this->inflection_point_asc << "},\n";
450
451 ss << "{\n";
452 ss << " \"name\": \"slope_asc\",\n";
453 ss << " \"id\":" << this->slope_asc.id_m << ",\n";
454 ss << " \"type\": \"vector\",\n";
455 ss << " \"dimensionality\": {\n";
456 ss << " \"header\": [null],\n";
457 ss << " \"dimensions\": [1]\n},\n";
458 ss << " \"values\":" << this->slope_asc << "},\n";
459
460 ss << " {\n";
461 ss << " \"name\": \"inflection_point_desc\",\n";
462 ss << " \"id\":" << this->inflection_point_desc.id_m << ",\n";
463 ss << " \"type\": \"vector\",\n";
464 ss << " \"dimensionality\": {\n";
465 ss << " \"header\": [null],\n";
466 ss << " \"dimensions\": [1]\n},\n";
467 ss << " \"values\":" << this->inflection_point_desc << "},\n";
468
469 ss << "{\n";
470 ss << " \"name\": \"slope_desc\",\n";
471 ss << " \"id\":" << this->slope_desc.id_m << ",\n";
472 ss << " \"type\": \"vector\",\n";
473 ss << " \"dimensionality\": {\n";
474 ss << " \"header\": [null],\n";
475 ss << " \"dimensions\": [1]\n},\n";
476 ss << " \"values\":" << this->slope_desc << "}]\n";
477
478 ss << "}";
479
480 return ss.str();
481 }
482
483#ifdef TMB_MODEL
484
485 template <typename Type>
487 std::shared_ptr<fims_info::Information<Type>> info =
489
490 std::shared_ptr<fims_popdy::DoubleLogisticSelectivity<Type>> selectivity =
491 std::make_shared<fims_popdy::DoubleLogisticSelectivity<Type>>();
492
493 std::stringstream ss;
494 // set relative info
495 selectivity->id = this->id;
496 selectivity->inflection_point_asc.resize(this->inflection_point_asc.size());
497 for (size_t i = 0; i < this->inflection_point_asc.size(); i++) {
498 selectivity->inflection_point_asc[i] =
499 this->inflection_point_asc[i].initial_value_m;
500 if (this->inflection_point_asc[i].estimation_type_m.get() ==
501 "fixed_effects") {
502 ss.str("");
503 ss << "Selectivity." << this->id << ".inflection_point_asc."
504 << this->inflection_point_asc[i].id_m;
505 info->RegisterParameterName(ss.str());
506 info->RegisterParameter(selectivity->inflection_point_asc[i]);
507 }
508 if (this->inflection_point_asc[i].estimation_type_m.get() ==
509 "random_effects") {
510 ss.str("");
511 ss << "Selectivity." << this->id << ".inflection_point_asc."
512 << this->inflection_point_asc[i].id_m;
513 info->RegisterRandomEffectName(ss.str());
514 info->RegisterRandomEffect(selectivity->inflection_point_asc[i]);
515 }
516 }
517 info->variable_map[this->inflection_point_asc.id_m] =
518 &(selectivity)->inflection_point_asc;
519
520 selectivity->slope_asc.resize(this->slope_asc.size());
521 for (size_t i = 0; i < this->slope_asc.size(); i++) {
522 selectivity->slope_asc[i] = this->slope_asc[i].initial_value_m;
523
524 if (this->slope_asc[i].estimation_type_m.get() == "fixed_effects") {
525 ss.str("");
526 ss << "Selectivity." << this->id << ".slope_asc."
527 << this->slope_asc[i].id_m;
528 info->RegisterParameterName(ss.str());
529 info->RegisterParameter(selectivity->slope_asc[i]);
530 }
531 if (this->slope_asc[i].estimation_type_m.get() == "random_effects") {
532 ss.str("");
533 ss << "Selectivity." << this->id << ".slope_asc."
534 << this->slope_asc[i].id_m;
535 info->RegisterRandomEffectName(ss.str());
536 info->RegisterRandomEffect(selectivity->slope_asc[i]);
537 }
538 }
539 info->variable_map[this->slope_asc.id_m] = &(selectivity)->slope_asc;
540
541 selectivity->inflection_point_desc.resize(
542 this->inflection_point_desc.size());
543 for (size_t i = 0; i < this->inflection_point_desc.size(); i++) {
544 selectivity->inflection_point_desc[i] =
545 this->inflection_point_desc[i].initial_value_m;
546
547 if (this->inflection_point_desc[i].estimation_type_m.get() ==
548 "fixed_effects") {
549 ss.str("");
550 ss << "Selectivity." << this->id << ".inflection_point_desc."
551 << this->inflection_point_desc[i].id_m;
552 info->RegisterParameterName(ss.str());
553 info->RegisterParameter(selectivity->inflection_point_desc[i]);
554 }
555 if (this->inflection_point_desc[i].estimation_type_m.get() ==
556 "random_effects") {
557 ss.str("");
558 ss << "Selectivity." << this->id << ".inflection_point_desc."
559 << this->inflection_point_desc[i].id_m;
560 info->RegisterRandomEffectName(ss.str());
561 info->RegisterRandomEffect(selectivity->inflection_point_desc[i]);
562 }
563 }
564 info->variable_map[this->inflection_point_desc.id_m] =
565 &(selectivity)->inflection_point_desc;
566
567 selectivity->slope_desc.resize(this->slope_desc.size());
568 for (size_t i = 0; i < this->slope_desc.size(); i++) {
569 selectivity->slope_desc[i] = this->slope_desc[i].initial_value_m;
570
571 if (this->slope_desc[i].estimation_type_m.get() == "fixed_effects") {
572 ss.str("");
573 ss << "Selectivity." << this->id << ".slope_desc."
574 << this->slope_desc[i].id_m;
575 info->RegisterParameterName(ss.str());
576 info->RegisterParameter(selectivity->slope_desc[i]);
577 }
578 if (this->slope_desc[i].estimation_type_m.get() == "random_effects") {
579 ss.str("");
580 ss << "Selectivity." << this->id << ".slope_desc."
581 << this->slope_desc[i].id_m;
582 info->RegisterRandomEffectName(ss.str());
583 info->RegisterRandomEffect(selectivity->slope_desc[i]);
584 }
585 }
586
587 info->variable_map[this->slope_desc.id_m] = &(selectivity)->slope_desc;
588
589 // add to Information
590 info->selectivity_models[selectivity->id] = selectivity;
591
592 return true;
593 }
594
599 virtual bool add_to_fims_tmb() {
602
603 return true;
604 }
605
606#endif
607};
608
609#endif
Rcpp interface for logistic selectivity as an S4 object. To instantiate from R: logistic_selectivity ...
Definition rcpp_selectivity.hpp:304
ParameterVector slope_desc
Definition rcpp_selectivity.hpp:313
ParameterVector inflection_point_desc
Definition rcpp_selectivity.hpp:310
DoubleLogisticSelectivityInterface(const DoubleLogisticSelectivityInterface &other)
Construct a new Double Logistic Selectivity Interface object.
Definition rcpp_selectivity.hpp:327
ParameterVector slope_asc
Definition rcpp_selectivity.hpp:309
virtual double evaluate(double x)
evaluate the double logistic selectivity function
Definition rcpp_selectivity.hpp:344
ParameterVector inflection_point_asc
Definition rcpp_selectivity.hpp:306
virtual uint32_t get_id()
returns the id for the double logistic selectivity interface
Definition rcpp_selectivity.hpp:338
virtual std::string to_json()
Convert the data to json representation for the output.
Definition rcpp_selectivity.hpp:434
virtual void finalize()
finalize function. Extracts derived quantities back to the Rcpp interface object from the Information...
Definition rcpp_selectivity.hpp:362
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 logistic selectivity to instantiate the object from R: logistic_selectivity <- met...
Definition rcpp_selectivity.hpp:83
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_selectivity.hpp:143
ParameterVector inflection_point
The index value at which the response reaches 0.5.
Definition rcpp_selectivity.hpp:88
LogisticSelectivityInterface()
The constructor.
Definition rcpp_selectivity.hpp:97
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_selectivity.hpp:194
virtual ~LogisticSelectivityInterface()
The destructor.
Definition rcpp_selectivity.hpp:117
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_selectivity.hpp:123
ParameterVector slope
The width of the curve at the inflection point.
Definition rcpp_selectivity.hpp:92
LogisticSelectivityInterface(const LogisticSelectivityInterface &other)
Construct a new Logistic Selectivity Interface object.
Definition rcpp_selectivity.hpp:109
virtual double evaluate(double x)
Evaluate selectivity using the logistic function.
Definition rcpp_selectivity.hpp:130
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
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:80
std::map< uint32_t, std::shared_ptr< fims_popdy::SelectivityBase< Type > > >::iterator selectivity_models_iterator
Definition information.hpp:84
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:238
#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,...
DoubleLogisticSelectivity class that returns the double logistic function value from fims_math.
Definition double_logistic.hpp:24
fims::Vector< Type > inflection_point_asc
Definition double_logistic.hpp:25
fims::Vector< Type > inflection_point_desc
Definition double_logistic.hpp:31
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:53
LogisticSelectivity class that returns the logistic function value from fims_math.
Definition logistic.hpp:30
fims::Vector< Type > inflection_point
Definition logistic.hpp:32