9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MODELS_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MODELS_HPP
15#include "../../../models/fisheries_models.hpp"
17#include "../../../utilities/fims_json.hpp"
61 static std::map<uint32_t, std::shared_ptr<FisheryModelInterfaceBase>>
69 this->population_ids = std::make_shared<std::set<uint32_t>>();
102 return "std::string to_json() not yet implemented.";
118 std::shared_ptr<fims_info::Information<double>>
info0 =
121 Rcpp::NumericVector
p;
123 for (
size_t i = 0;
i <
info0->fixed_effects_parameters.size();
i++) {
124 p.push_back(*
info0->fixed_effects_parameters[
i]);
138 std::shared_ptr<fims_info::Information<double>>
d0 =
141 Rcpp::NumericVector
p;
143 for (
size_t i = 0;
i <
d0->random_effects_parameters.size();
i++) {
144 p.push_back(*
d0->random_effects_parameters[
i]);
156 double sum(
const std::valarray<double> &
v) {
158 for (
size_t i = 0;
i <
v.size();
i++) {
170 double sum(
const std::vector<double> &
v) {
172 for (
size_t i = 0;
i <
v.size();
i++) {
185 double min(
const std::valarray<double> &
v) {
187 for (
size_t i = 1;
i <
v.size();
i++) {
203 std::valarray<double>
fabs(
const std::valarray<double> &
v) {
204 std::valarray<double>
result(
v.size());
205 for (
size_t i = 0;
i <
v.size();
i++) {
222 std::shared_ptr<CatchAtAgeInterface>
caa =
223 std::make_shared<CatchAtAgeInterface>(*
this);
242 std::map<uint32_t, std::shared_ptr<PopulationInterfaceBase>>::iterator
pit;
245 std::shared_ptr<PopulationInterfaceBase> &
pop = (*pit).second;
246 pop->initialize_catch_at_age.set(
true);
262 std::shared_ptr<fims_info::Information<double>>
info =
267 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
268 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
284 std::shared_ptr<fims_info::Information<double>>
info =
289 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
290 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
314 std::stringstream
ss;
317 std::shared_ptr<PopulationInterfaceBase>>::iterator
324 " not found in live objects.");
329 std::dynamic_pointer_cast<PopulationInterface>((*pi_it).second);
331 std::shared_ptr<fims_info::Information<double>>
info =
336 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
337 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
344 if (
pit !=
info->populations.end()) {
345 std::shared_ptr<fims_popdy::Population<double>> &
pop = (*pit).second;
348 ss <<
" \"module_name\": \"Population\",\n";
356 ss <<
" \"parameters\": [\n";
357 for (
size_t i = 0;
i <
pop->log_M.size();
i++) {
361 ss <<
"{\n \"name\": \"log_M\",\n";
363 ss <<
" \"type\": \"vector\",\n";
364 ss <<
" \"dimensionality\": {\n";
365 ss <<
" \"header\": [" <<
"\"n_years\", \"n_ages\"" <<
"],\n";
371 for (
size_t i = 0;
i <
pop->log_f_multiplier.size();
i++) {
373 pop->log_f_multiplier[
i];
376 ss <<
"{\n \"name\": \"log_f_multiplier\",\n";
378 ss <<
" \"type\": \"vector\",\n";
379 ss <<
" \"dimensionality\": {\n";
380 ss <<
" \"header\": [" <<
"\"n_years\"" <<
"],\n";
386 for (
size_t i = 0;
i <
pop->spawning_biomass_ratio.size();
i++) {
388 pop->spawning_biomass_ratio[
i];
391 ss <<
"{\n \"name\": \"spawning_biomass_ratio\",\n";
394 ss <<
" \"type\": \"vector\",\n";
395 ss <<
" \"dimensionality\": {\n";
396 ss <<
" \"header\": [" <<
"\"n_years\"" <<
"],\n";
403 for (
size_t i = 0;
i <
pop->log_init_naa.size();
i++) {
405 pop->log_init_naa[
i];
407 ss <<
" {\n\"name\": \"log_init_naa\",\n";
409 ss <<
" \"type\": \"vector\",\n";
410 ss <<
" \"dimensionality\": {\n";
411 ss <<
" \"header\": [" <<
"\"n_ages\"" <<
"],\n";
421 pop->proportion_female.get_force_scalar(
i);
423 ss <<
" {\n\"name\": \"proportion_female\",\n";
426 ss <<
" \"type\": \"vector\",\n";
427 ss <<
" \"dimensionality\": {\n";
428 ss <<
" \"header\": [" <<
"\"n_ages\"" <<
"],\n";
429 ss <<
" \"dimensions\": ["
435 ss <<
" \"derived_quantities\": [\n";
437 std::map<std::string, fims::Vector<double>>
dqs =
438 model_ptr->GetPopulationDerivedQuantities(
441 std::map<std::string, fims_popdy::DimensionInfo>
dim_info =
447 ss <<
" \"name\": \"Population\",\n";
449 ss <<
" \"type\": \"population\",\n";
451 <<
" not found in Information.\",\n";
457 ss <<
" \"derived_quantities\": []}\n";
471 std::stringstream
ss;
480 for (
size_t i = 0;
i <
dim_info.dims.size(); ++
i) {
490 <<
dim_info.dim_names[1] <<
"\"],\n";
492 for (
size_t i = 0;
i <
dim_info.dims.size(); ++
i) {
505 for (
size_t i = 0;
i <
dim_info.dims.size(); ++
i) {
522 ss <<
"\"name\":\"" << (*it).first <<
"\",\n";
525 ss << std::fixed << std::setprecision(10);
527 for (
size_t i = 0;
i <
dq.size() - 1;
i++) {
530 ss <<
"-999" <<
", ";
535 if (
dq[
dq.size() - 1] !=
dq[
dq.size() - 1])
537 ss <<
"-999]" <<
"\n";
539 ss <<
dq[
dq.size() - 1] <<
"]\n";
555 std::map<std::string, fims_popdy::DimensionInfo> &
dim_info) {
556 std::stringstream
ss;
557 std::map<std::string, fims_popdy::DimensionInfo>::iterator
dim_info_it;
558 std::map<std::string, fims::Vector<double>>::iterator
it;
559 std::map<std::string, fims::Vector<double>>::iterator
end_it;
561 typename std::map<std::string, fims::Vector<double>>::iterator
589 std::stringstream
ss;
593 "Fleet pointer is null; cannot get id. Not found in live objects.");
597 std::shared_ptr<fims_info::Information<double>>
info =
602 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
603 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
610 if (
fit !=
info->fleets.end()) {
611 std::shared_ptr<fims_popdy::Fleet<double>> &
fleet = (*fit).second;
614 ss <<
" \"module_name\": \"Fleet\",\n";
620 ss <<
"\"data_ids\" : [\n";
623 ss <<
"{\"lengthcomp\": "
630 ss <<
"\"parameters\": [\n";
636 ss <<
" \"name\": \"log_Fmort\",\n";
638 ss <<
" \"type\": \"vector\",\n";
639 ss <<
" \"dimensionality\": {\n";
640 ss <<
" \"header\": [\"" <<
"n_years" <<
"\"],\n";
646 for (
size_t i = 0;
i <
fleet->log_q.size();
i++) {
649 ss <<
" \"name\": \"log_q\",\n";
651 ss <<
" \"type\": \"vector\",\n";
652 ss <<
" \"dimensionality\": {\n";
653 ss <<
" \"header\": [\"" <<
"na" <<
"\"],\n";
654 ss <<
" \"dimensions\": [" <<
fleet->log_q.size() <<
"]\n},\n";
658 ss <<
"], \"derived_quantities\": [";
660 std::map<std::string, fims::Vector<double>>
dqs =
662 std::map<std::string, fims_popdy::DimensionInfo>
dim_info =
667 ss <<
" \"name\": \"Fleet\",\n";
668 ss <<
" \"type\": \"fleet\",\n";
670 <<
" not found in Information.\",\n";
671 ss <<
" \"derived_quantities\": []}\n";
684 std::set<uint32_t> fleet_ids;
687 typename std::set<uint32_t>::iterator
pit;
688 typename std::set<uint32_t>::iterator
fids;
692 std::dynamic_pointer_cast<PopulationInterface>(
701 fleet_ids.insert(*
fids);
706 for (
fids = fleet_ids.begin();
fids != fleet_ids.end();
fids++) {
708 std::dynamic_pointer_cast<FleetInterface>(
715 std::shared_ptr<fims_info::Information<double>>
info =
718 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model =
719 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
726 model->do_reporting =
false;
731 std::stringstream
ss;
736 ss <<
" \"name\": \"CatchAtAge\",\n";
737 ss <<
" \"type\": \"model\",\n";
738 ss <<
" \"estimation_framework\": ";
740 ss <<
"\"Template_Model_Builder (TMB)\",";
744 ss <<
" \"id\": " << this->
get_id() <<
",\n";
745 ss <<
" \"objective_function_value\": " <<
sanitize_val(value) <<
",\n";
746 ss <<
"\"growth\":[\n";
763 ss <<
"\"recruitment\": [\n";
778 ss <<
"\"maturity\": [\n";
793 ss <<
"\"selectivity\": [\n";
808 ss <<
" \"population_ids\": [";
817 ss <<
" \"fleet_ids\": [";
819 for (
fids = fleet_ids.begin();
fids != fleet_ids.end();
fids++) {
821 if (std::next(
fids) != fleet_ids.end()) {
826 ss <<
"\"populations\": [\n";
827 typename std::set<uint32_t>::iterator
pop_it;
828 typename std::set<uint32_t>::iterator
pop_end_it;
832 pop_second_to_last_it = std::prev(
pop_end_it);
839 std::dynamic_pointer_cast<PopulationInterface>(
842 std::set<uint32_t>::iterator
fids;
845 fleet_ids.insert(*
fids);
851 " not found in live objects.");
857 std::dynamic_pointer_cast<PopulationInterface>(
860 std::set<uint32_t>::iterator
fids;
863 fleet_ids.insert(*
fids);
868 " not found in live objects.");
874 ss <<
"\"fleets\": [\n";
876 typename std::set<uint32_t>::iterator
fleet_it;
887 std::dynamic_pointer_cast<FleetInterface>(
894 " not found in live objects.");
899 std::dynamic_pointer_cast<FleetInterface>(
905 " not found in live objects.");
911 ss <<
"\"density_components\": [\n";
914 uint32_t, std::shared_ptr<DistributionsInterfaceBase>>::iterator
dit;
928 ss <<
"\"data\": [\n";
929 typename std::map<uint32_t, std::shared_ptr<DataInterfaceBase>>::iterator
933 std::shared_ptr<DataInterfaceBase>
data_interface = (*d_it).second;
945 model->do_reporting =
true;
952 template <
typename Type>
954 std::shared_ptr<fims_info::Information<Type>>
info =
957 std::shared_ptr<fims_popdy::CatchAtAge<Type>>
model =
958 std::make_shared<fims_popdy::CatchAtAge<Type>>();
964 model->AddPopulation((*
it));
967 std::set<uint32_t> fleet_ids;
968 typedef typename std::set<uint32_t>::iterator fleet_ids_iterator;
977 throw std::runtime_error(
"Population ID " + std::to_string(*
it) +
978 " not found in live_objects");
981 std::dynamic_pointer_cast<PopulationInterface>(
it2->second);
986 std::map<std::string, fims_popdy::DimensionInfo>
990 std::stringstream
ss;
997 "total_landings_weight",
1006 "total_landings_numbers",
1046 "unfished_numbers_at_age",
1077 "unfished_spawning_biomass",
1085 "proportion_mature_at_age",
1094 "expected_recruitment",
1109 for (fleet_ids_iterator
fit =
population->fleet_ids->begin();
1111 fleet_ids.insert(*
fit);
1115 for (fleet_ids_iterator
it = fleet_ids.begin();
it != fleet_ids.end();
1118 std::dynamic_pointer_cast<FleetInterface>(
1124 std::map<std::string, fims_popdy::DimensionInfo>
1134 "landings_numbers_at_age",
1143 "landings_weight_at_age",
1152 "landings_numbers_at_length",
1177 "landings_expected",
1185 "log_landings_expected",
1193 "agecomp_proportion",
1202 "lengthcomp_proportion",
1212 "index_numbers_at_age",
1221 "index_weight_at_age",
1230 "index_weight_at_age",
1239 "index_numbers_at_length",
1265 "log_index_expected",
1288 "lengthcomp_expected",
The CatchAtAgeInterface class is used to interface with the CatchAtAge model. It inherits from the Fi...
Definition rcpp_models.hpp:216
virtual std::string to_json()
Serialize the fishery model to a JSON string.
Definition rcpp_models.hpp:679
void AddPopulation(uint32_t id)
Definition rcpp_models.hpp:239
std::string derived_quantity_to_json(std::map< std::string, fims::Vector< double > >::iterator it, const fims_popdy::DimensionInfo &dim_info)
Definition rcpp_models.hpp:468
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_models.hpp:308
CatchAtAgeInterface()
The constructor.
Definition rcpp_models.hpp:221
void DoReporting(bool report)
Enable or disable reporting for the CatchAtAge model.
Definition rcpp_models.hpp:260
virtual uint32_t get_id()
Method to get this id.
Definition rcpp_models.hpp:303
std::string fleet_to_json(FleetInterface *fleet_interface)
Method to convert a fleet to a JSON string.
Definition rcpp_models.hpp:588
std::string derived_quantities_component_to_json(std::map< std::string, fims::Vector< double > > &dqs, std::map< std::string, fims_popdy::DimensionInfo > &dim_info)
Send the fleet-based derived quantities to the json file.
Definition rcpp_models.hpp:553
std::string population_to_json(PopulationInterface *population_interface)
Method to convert a population to a JSON string.
Definition rcpp_models.hpp:313
CatchAtAgeInterface(const CatchAtAgeInterface &other)
Construct a new Catch At Age Interface object.
Definition rcpp_models.hpp:233
bool IsReporting()
Check if reporting is enabled for the CatchAtAge model.
Definition rcpp_models.hpp:282
static std::map< uint32_t, std::shared_ptr< DataInterfaceBase > > live_objects
The map associating the IDs of DataInterfaceBase to the objects. This is a live object,...
Definition rcpp_data.hpp:43
static std::map< uint32_t, std::shared_ptr< DistributionsInterfaceBase > > live_objects
The map associating the ID of the DistributionsInterfaceBase to the DistributionsInterfaceBase object...
Definition rcpp_distribution.hpp:67
Base class for all interface objects.
Definition rcpp_interface_base.hpp:628
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
The FisheryModelInterfaceBase class is the base class for all fishery models in the FIMS Rcpp interfa...
Definition rcpp_models.hpp:36
double min(const std::valarray< double > &v)
Minimum method to calculate the minimum of an array or vector of doubles.
Definition rcpp_models.hpp:185
FisheryModelInterfaceBase(const FisheryModelInterfaceBase &other)
Construct a new Data Interface Base object.
Definition rcpp_models.hpp:80
static uint32_t id_g
The static id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:51
virtual uint32_t get_id()=0
Get the ID for the child fleet interface objects to inherit.
FisheryModelInterfaceBase()
The constructor.
Definition rcpp_models.hpp:67
std::set< uint32_t >::iterator population_id_iterator
Iterator for population ids.
Definition rcpp_models.hpp:45
uint32_t id
The local id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:55
std::valarray< double > fabs(const std::valarray< double > &v)
A function to compute the absolute value of a value array of floating-point values....
Definition rcpp_models.hpp:203
virtual std::string to_json()
Serialize the fishery model to a JSON string.
Definition rcpp_models.hpp:101
static std::map< uint32_t, std::shared_ptr< FisheryModelInterfaceBase > > live_objects
The map associating the IDs of FleetInterfaceBase to the objects. This is a live object,...
Definition rcpp_models.hpp:62
double sum(const std::valarray< double > &v)
Sum method to calculate the sum of an array or vector of doubles.
Definition rcpp_models.hpp:156
Rcpp::NumericVector get_random_parameters_vector()
Get the vector of random effect parameters for the model.
Definition rcpp_models.hpp:137
Rcpp::NumericVector get_fixed_parameters_vector()
Get the vector of fixed effect parameters for the model.
Definition rcpp_models.hpp:117
std::shared_ptr< std::set< uint32_t > > population_ids
The set of population ids that this fishery model operates on.
Definition rcpp_models.hpp:41
virtual ~FisheryModelInterfaceBase()
The destructor.
Definition rcpp_models.hpp:86
double sum(const std::vector< double > &v)
Sum method for a vector of doubles.
Definition rcpp_models.hpp:170
static std::map< uint32_t, std::shared_ptr< FleetInterfaceBase > > live_objects
The map associating the IDs of FleetInterfaceBase to the objects. This is a live object,...
Definition rcpp_fleet.hpp:34
The Rcpp interface for Fleet to instantiate from R: fleet <- methods::new(Fleet)
Definition rcpp_fleet.hpp:68
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
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
static std::map< uint32_t, std::shared_ptr< PopulationInterfaceBase > > live_objects
The map associating the IDs of PopulationInterfaceBase to the objects. This is a live object,...
Definition rcpp_population.hpp:35
Rcpp interface for a new Population to instantiate from R: population <- methods::new(population)
Definition rcpp_population.hpp:80
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
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
static std::string PrettyFormatJSON(const std::string &json)
Formats a JSON string.
Definition fims_json.hpp:128
Definition fims_vector.hpp:27
static std::shared_ptr< Model< Type > > GetInstance()
Evaluate. Calculates the joint negative log-likelihood function.
Definition model.hpp:54
Platform macros and the core FIMS logging system.
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:664
: Loops over model components and returns the negative log-likelihood function.
The Rcpp interface to declare different types of data, e.g., age-composition and index data....
The Rcpp interface to declare different distributions, e.g., normal and log normal....
The Rcpp interface to declare fleets. Allows for the use of methods::new() in R.
The Rcpp interface to declare different types of growth, e.g., empirical weight-at-age data....
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,...
double sanitize_val(double x)
Sanitize a double value by replacing NaN or Inf with -999.0.
Definition rcpp_interface_base.hpp:113
The Rcpp interface to declare different maturity options, e.g., logistic. Allows for the use of metho...
The Rcpp interface to declare different types of populations. Allows for the use of methods::new() in...
The Rcpp interface to declare different types of recruitment, e.g., Beverton–Holt stock–recruitment r...
The Rcpp interface to declare different types of selectivity, e.g., logistic and double logistic....
Structure to hold dimension information for derived quantities.
Definition fishery_model_base.hpp:25