FIMS  v0.8.1
Loading...
Searching...
No Matches
rcpp_maturity.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MATURITY_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MATURITY_HPP
11
12#include "../../../population_dynamics/maturity/maturity.hpp"
14
20 public:
24 static uint32_t id_g;
34 static std::map<uint32_t, std::shared_ptr<MaturityInterfaceBase>>
36
41 this->id = MaturityInterfaceBase::id_g++;
42 /* Create instance of map: key is id and value is pointer to
43 MaturityInterfaceBase */
44 // MaturityInterfaceBase::live_objects[this->id] = this;
45 }
46
53
58
62 virtual uint32_t get_id() = 0;
63
68 virtual double evaluate(double x) = 0;
69};
70// static id of the MaturityInterfaceBase object
72// local id of the MaturityInterfaceBase object map relating the ID of the
73// MaturityInterfaceBase to the MaturityInterfaceBase objects
74std::map<uint32_t, std::shared_ptr<MaturityInterfaceBase>>
76
82 public:
91
97 std::make_shared<LogisticMaturityInterface>(*this);
100 }
101
111
116
121 virtual uint32_t get_id() { return this->id; }
122
128 virtual double evaluate(double x) {
131 LogisticMat.inflection_point[0] = this->inflection_point[0].initial_value_m;
132 LogisticMat.slope.resize(1);
133 LogisticMat.slope[0] = this->slope[0].initial_value_m;
134 return LogisticMat.evaluate(x);
135 }
136
141 virtual void finalize() {
142 if (this->finalized) {
143 // log warning that finalize has been called more than once.
144 FIMS_WARNING_LOG("Logistic Maturity " + fims::to_string(this->id) +
145 " has been finalized already.");
146 }
147
148 this->finalized = true; // indicate this has been called already
149
150 std::shared_ptr<fims_info::Information<double>> info =
152
154
155 // search for maturity in Information
156 it = info->maturity_models.find(this->id);
157 // if not found, just return
158 if (it == info->maturity_models.end()) {
159 FIMS_WARNING_LOG("Logistic Maturity " + fims::to_string(this->id) +
160 " not found in Information.");
161 return;
162 } else {
163 std::shared_ptr<fims_popdy::LogisticMaturity<double>> mat =
164 std::dynamic_pointer_cast<fims_popdy::LogisticMaturity<double>>(
165 it->second);
166
167 for (size_t i = 0; i < inflection_point.size(); i++) {
168 if (this->inflection_point[i].estimation_type_m.get() == "constant") {
169 this->inflection_point[i].final_value_m =
170 this->inflection_point[i].initial_value_m;
171 } else {
172 this->inflection_point[i].final_value_m = mat->inflection_point[i];
173 }
174 }
175
176 for (size_t i = 0; i < slope.size(); i++) {
177 if (this->slope[i].estimation_type_m.get() == "constant") {
178 this->slope[i].final_value_m = this->slope[i].initial_value_m;
179 } else {
180 this->slope[i].final_value_m = mat->slope[i];
181 }
182 }
183 }
184 }
185
192 virtual std::string to_json() {
193 std::stringstream ss;
194 ss << "{\n";
195 ss << " \"module_name\": \"Maturity\",\n";
196 ss << " \"module_type\": \"Logistic\",\n";
197 ss << " \"module_id\": " << this->id << ",\n";
198
199 ss << " \"parameters\": [\n{\n";
200 ss << " \"name\": \"inflection_point\",\n";
201 ss << " \"id\":" << this->inflection_point.id_m << ",\n";
202 ss << " \"type\": \"vector\",\n";
203 ss << " \"dimensionality\": {\n";
204 ss << " \"header\": [null],\n";
205 ss << " \"dimensions\": [1]\n},\n";
206 ss << " \"values\":" << this->inflection_point << "},\n ";
207
208 ss << "{\n";
209 ss << " \"name\": \"slope\",\n";
210 ss << " \"id\":" << this->slope.id_m << ",\n";
211 ss << " \"type\": \"vector\",\n";
212 ss << " \"dimensionality\": {\n";
213 ss << " \"header\": [null],\n";
214 ss << " \"dimensions\": [1]\n},\n";
215 ss << " \"values\":" << this->slope << "}]\n";
216
217 ss << "}";
218
219 return ss.str();
220 }
221
222#ifdef TMB_MODEL
223
224 template <typename Type>
226 std::shared_ptr<fims_info::Information<Type>> info =
228
229 std::shared_ptr<fims_popdy::LogisticMaturity<Type>> maturity =
230 std::make_shared<fims_popdy::LogisticMaturity<Type>>();
231
232 // set relative info
233 maturity->id = this->id;
234 std::stringstream ss;
235 maturity->inflection_point.resize(this->inflection_point.size());
236 for (size_t i = 0; i < this->inflection_point.size(); i++) {
237 maturity->inflection_point[i] = this->inflection_point[i].initial_value_m;
238 if (this->inflection_point[i].estimation_type_m.get() ==
239 "fixed_effects") {
240 ss.str("");
241 ss << "Maturity." << this->id << ".inflection_point."
242 << this->inflection_point[i].id_m;
243 info->RegisterParameterName(ss.str());
244 info->RegisterParameter(maturity->inflection_point[i]);
245 }
246 if (this->inflection_point[i].estimation_type_m.get() ==
247 "random_effects") {
248 ss.str("");
249 ss << "Maturity." << this->id << ".inflection_point."
250 << this->inflection_point[i].id_m;
251 info->RegisterRandomEffectName(ss.str());
252 info->RegisterRandomEffect(maturity->inflection_point[i]);
253 }
254 }
255
256 maturity->slope.resize(this->slope.size());
257 for (size_t i = 0; i < this->slope.size(); i++) {
258 maturity->slope[i] = this->slope[i].initial_value_m;
259 if (this->slope[i].estimation_type_m.get() == "fixed_effects") {
260 ss.str("");
261 ss << "Maturity." << this->id << ".slope." << this->slope[i].id_m;
262 info->RegisterParameterName(ss.str());
263 info->RegisterParameter(maturity->slope[i]);
264 }
265 if (this->slope[i].estimation_type_m.get() == "random_effects") {
266 ss.str("");
267 ss << "Maturity." << this->id << ".slope." << this->slope[i].id_m;
268 info->RegisterRandomEffect(maturity->slope[i]);
269 info->RegisterRandomEffectName(ss.str());
270 }
271 }
272
273 // add to Information
274 info->maturity_models[maturity->id] = maturity;
275
276 return true;
277 }
278
283 virtual bool add_to_fims_tmb() {
286
287 return true;
288 }
289
290#endif
291};
292
293#endif
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 maturity to instantiate the object from R: logistic_maturity <- methods::...
Definition rcpp_maturity.hpp:81
virtual ~LogisticMaturityInterface()
The destructor.
Definition rcpp_maturity.hpp:115
ParameterVector slope
The width of the curve at the inflection point.
Definition rcpp_maturity.hpp:90
ParameterVector inflection_point
The index value at which the response reaches 0.5.
Definition rcpp_maturity.hpp:86
LogisticMaturityInterface()
The constructor.
Definition rcpp_maturity.hpp:95
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_maturity.hpp:121
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_maturity.hpp:141
LogisticMaturityInterface(const LogisticMaturityInterface &other)
Construct a new Logistic Maturity Interface object.
Definition rcpp_maturity.hpp:107
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_maturity.hpp:192
virtual double evaluate(double x)
Evaluate maturity using the logistic function.
Definition rcpp_maturity.hpp:128
Rcpp interface that serves as the parent class for Rcpp maturity interfaces. This type should be inhe...
Definition rcpp_maturity.hpp:19
virtual double evaluate(double x)=0
A method for each child maturity interface object to inherit so each maturity option can have an eval...
virtual ~MaturityInterfaceBase()
The destructor.
Definition rcpp_maturity.hpp:57
virtual uint32_t get_id()=0
Get the ID for the child maturity interface objects to inherit.
static std::map< uint32_t, std::shared_ptr< MaturityInterfaceBase > > live_objects
The map associating the IDs of MaturityInterfaceBase to the objects. This is a live object,...
Definition rcpp_maturity.hpp:35
uint32_t id
The local id of the MaturityInterfaceBase object.
Definition rcpp_maturity.hpp:28
MaturityInterfaceBase(const MaturityInterfaceBase &other)
Construct a new Maturity Interface Base object.
Definition rcpp_maturity.hpp:52
MaturityInterfaceBase()
The constructor.
Definition rcpp_maturity.hpp:40
static uint32_t id_g
The static id of the MaturityInterfaceBase object.
Definition rcpp_maturity.hpp:24
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
std::map< uint32_t, std::shared_ptr< fims_popdy::MaturityBase< Type > > > maturity_models
Definition information.hpp:97
std::map< uint32_t, std::shared_ptr< fims_popdy::MaturityBase< Type > > >::iterator maturity_models_iterator
Definition information.hpp:101
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,...
LogisticMaturity class that returns the logistic function value from fims_math.
Definition logistic.hpp:24
fims::Vector< Type > inflection_point
Definition logistic.hpp:26