FIMS  v0.8.0
Loading...
Searching...
No Matches
rcpp_growth.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_GROWTH_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_GROWTH_HPP
11
12#include "../../../population_dynamics/growth/growth.hpp"
14
20 public:
24 static uint32_t id_g;
34 static std::map<uint32_t, std::shared_ptr<GrowthInterfaceBase>> live_objects;
35
40 this->id = GrowthInterfaceBase::id_g++;
41 /* Create instance of map: key is id and value is pointer to
42 GrowthInterfaceBase */
43 // GrowthInterfaceBase::live_objects[this->id] =
44 // std::make_shared<GrowthInterfaceBase>(*this);
45 }
46
53
58
62 virtual uint32_t get_id() = 0;
63
68 virtual double evaluate(double age) = 0;
69};
70// static id of the GrowthInterfaceBase object
72// local id of the GrowthInterfaceBase object map relating the ID of the
73// GrowthInterfaceBase to the GrowthInterfaceBase objects
74std::map<uint32_t, std::shared_ptr<GrowthInterfaceBase>>
76
83 public:
96 std::shared_ptr<std::map<double, double>> ewaa;
100 bool initialized = false;
101
106 this->ewaa = std::make_shared<std::map<double, double>>();
108 std::make_shared<EWAAGrowthInterface>(*this);
110 std::make_shared<EWAAGrowthInterface>(*this));
111 }
112
124
129
134 virtual uint32_t get_id() { return this->id; }
135
142 inline std::map<double, double> make_map(RealVector ages,
144 std::map<double, double> mymap;
145 for (size_t i = 0; i < ages.size(); i++) {
146 mymap.insert(std::pair<double, double>(ages[i], weights[i]));
147 }
148 return mymap;
149 }
150
156 virtual double evaluate(double age) {
158
159 if (initialized == false) {
160 // Check that ages and weights vector are the same length
161 if (this->ages.size() != this->weights.size()) {
162 Rcpp::stop("ages and weights must be the same length");
163 }
164 EWAAGrowth.ewaa = make_map(this->ages, this->weights);
165 initialized = true;
166 } else {
167 Rcpp::stop("this empirical weight at age object is already initialized");
168 }
169 return EWAAGrowth.evaluate(age);
170 }
171
179 virtual std::string to_json() {
180 std::stringstream ss;
181 ss << "{\n";
182 ss << " \"module_name\": \"Growth\",\n";
183 ss << " \"module_type\": \"EWAA\",\n";
184 ss << " \"module_id\":" << this->id << ",\n";
185 ss << " \"parameters\": [\n{\n";
186 ss << " \"name\": null,\n";
187 ss << " \"id\": null,\n";
188 ss << " \"type\": \"vector\",\n";
189 ss << " \"dimensionality\": {\n";
190 ss << " \"header\": [\"n_ages\"],\n";
191 ss << " \"dimensions\": [" << this->ages.size() << "]\n},\n";
192
193 ss << " \"values\": [\n";
194 for (size_t i = 0; i < weights.size() - 1; i++) {
195 ss << "{\n";
196 ss << "\"id\": null,\n";
197 ss << "\"value\": " << weights[i] << ",\n";
198 ss << "\"estimated_value\": " << weights[i] << ",\n";
199 ss << "\"uncertainty\": " << 0 << ",\n";
200 ss << "\"min\": \"-Infinity\",\n";
201 ss << "\"max\": \"Infinity\",\n";
202 ss << "\"estimation_type\": \"constant\"\n";
203 ss << "},\n";
204 }
205 ss << "{\n";
206 ss << "\"id\": null,\n";
207 ss << "\"value\": " << weights[weights.size() - 1] << ",\n";
208 ss << "\"estimated_value\": " << weights[weights.size() - 1] << ",\n";
209 ss << "\"uncertainty\": " << 0 << ",\n";
210 ss << "\"min\": \"-Infinity\",\n";
211 ss << "\"max\": \"Infinity\",\n";
212 ss << "\"estimation_type\": \"constant\"\n";
213 ss << "}\n]\n";
214 ss << "}\n]\n}\n";
215 return ss.str();
216 }
217
218#ifdef TMB_MODEL
219
220 template <typename Type>
222 std::shared_ptr<fims_info::Information<Type>> info =
224
225 std::shared_ptr<fims_popdy::EWAAGrowth<Type>> ewaa_growth =
226 std::make_shared<fims_popdy::EWAAGrowth<Type>>();
227
228 // set relative info
229 ewaa_growth->id = this->id;
230 ewaa_growth->ewaa = make_map(this->ages, this->weights); // this->ewaa;
231 // add to Information
232 info->growth_models[ewaa_growth->id] = ewaa_growth;
233
234 return true;
235 }
236
241 virtual bool add_to_fims_tmb() {
243#ifdef TMBAD_FRAMEWORK
245#else
250#endif
251
252 return true;
253 }
254
255#endif
256};
257
258#endif
Rcpp interface for EWAAGrowth to instantiate the object from R: ewaa <- methods::new(EWAAGrowth)....
Definition rcpp_growth.hpp:82
virtual ~EWAAGrowthInterface()
The destructor.
Definition rcpp_growth.hpp:128
std::shared_ptr< std::map< double, double > > ewaa
A map of empirical weight-at-age values allowing multiple modules to access and modify the weights wi...
Definition rcpp_growth.hpp:96
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_growth.hpp:134
RealVector ages
Ages (years) for each age class.
Definition rcpp_growth.hpp:91
virtual double evaluate(double age)
Evaluate the growth using empirical weight at age.
Definition rcpp_growth.hpp:156
EWAAGrowthInterface(const EWAAGrowthInterface &other)
Construct a new EWAAGrowthInterface object.
Definition rcpp_growth.hpp:118
bool initialized
Have weight and age vectors been set? The default is false.
Definition rcpp_growth.hpp:100
RealVector weights
Weights (mt) for each age class.
Definition rcpp_growth.hpp:87
EWAAGrowthInterface()
The constructor.
Definition rcpp_growth.hpp:105
std::map< double, double > make_map(RealVector ages, RealVector weights)
Create a map of input numeric vectors.
Definition rcpp_growth.hpp:142
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_growth.hpp:179
Base class for all interface objects.
Definition rcpp_interface_base.hpp:634
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:644
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:649
Rcpp interface that serves as the parent class for Rcpp growth interfaces. This type should be inheri...
Definition rcpp_growth.hpp:19
static std::map< uint32_t, std::shared_ptr< GrowthInterfaceBase > > live_objects
The map associating the IDs of GrowthInterfaceBase to the objects. This is a live object,...
Definition rcpp_growth.hpp:34
virtual double evaluate(double age)=0
A method for each child growth interface object to inherit so each growth option can have an evaluate...
GrowthInterfaceBase(const GrowthInterfaceBase &other)
Construct a new Growth Interface Base object.
Definition rcpp_growth.hpp:52
static uint32_t id_g
The static id of the GrowthInterfaceBase object.
Definition rcpp_growth.hpp:24
virtual ~GrowthInterfaceBase()
The destructor.
Definition rcpp_growth.hpp:57
GrowthInterfaceBase()
The constructor.
Definition rcpp_growth.hpp:39
uint32_t id
The local id of the GrowthInterfaceBase object.
Definition rcpp_growth.hpp:28
virtual uint32_t get_id()=0
Get the ID for the child growth interface objects to inherit.
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:434
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:595
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:238
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:279
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...
EWAAGrowth class that returns the EWAA function value.
Definition ewaa.hpp:23
virtual const Type evaluate(const double &a)
Returns the weight at age a (in kg) from the input vector.
Definition ewaa.hpp:43
std::map< double, double > ewaa
Definition ewaa.hpp:29