9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MODELS_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MODELS_HPP
13#include "../../../common/def.hpp"
15#include "../../../models/fisheries_models.hpp"
16#include "../../../utilities/fims_json.hpp"
57 static std::map<uint32_t, std::shared_ptr<FisheryModelInterfaceBase>>
65 this->population_ids = std::make_shared<std::set<uint32_t>>();
98 return "std::string to_json() not yet implemented.";
114 std::shared_ptr<fims_info::Information<double>>
info0 =
117 Rcpp::NumericVector
p;
119 for (
size_t i = 0;
i <
info0->fixed_effects_parameters.size();
i++) {
120 p.push_back(*
info0->fixed_effects_parameters[
i]);
134 std::shared_ptr<fims_info::Information<double>>
d0 =
137 Rcpp::NumericVector
p;
139 for (
size_t i = 0;
i <
d0->random_effects_parameters.size();
i++) {
140 p.push_back(*
d0->random_effects_parameters[
i]);
152 double sum(
const std::valarray<double> &
v) {
154 for (
size_t i = 0;
i <
v.size();
i++) {
166 double sum(
const std::vector<double> &
v) {
168 for (
size_t i = 0;
i <
v.size();
i++) {
181 double min(
const std::valarray<double> &
v) {
183 for (
size_t i = 1;
i <
v.size();
i++) {
199 std::valarray<double>
fabs(
const std::valarray<double> &
v) {
200 std::valarray<double>
result(
v.size());
201 for (
size_t i = 0;
i <
v.size();
i++) {
211std::map<uint32_t, std::shared_ptr<FisheryModelInterfaceBase>>
224 std::shared_ptr<CatchAtAgeInterface>
caa =
225 std::make_shared<CatchAtAgeInterface>(*
this);
244 std::map<uint32_t, std::shared_ptr<PopulationInterfaceBase>>::iterator
pit;
247 std::shared_ptr<PopulationInterfaceBase> &
pop = (*pit).second;
248 pop->initialize_catch_at_age.set(
true);
264 std::shared_ptr<fims_info::Information<double>>
info =
269 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
270 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
286 std::shared_ptr<fims_info::Information<double>>
info =
291 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
292 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
316 std::stringstream
ss;
319 std::shared_ptr<PopulationInterfaceBase>>::iterator
326 " not found in live objects.");
331 std::dynamic_pointer_cast<PopulationInterface>((*pi_it).second);
333 std::shared_ptr<fims_info::Information<double>>
info =
338 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
339 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
346 if (
pit !=
info->populations.end()) {
347 std::shared_ptr<fims_popdy::Population<double>> &
pop = (*pit).second;
350 ss <<
" \"module_name\": \"Population\",\n";
358 ss <<
" \"parameters\": [\n";
359 for (
size_t i = 0;
i <
pop->log_M.size();
i++) {
363 ss <<
"{\n \"name\": \"log_M\",\n";
365 ss <<
" \"type\": \"vector\",\n";
366 ss <<
" \"dimensionality\": {\n";
367 ss <<
" \"header\": [" <<
"\"n_years\", \"n_ages\"" <<
"],\n";
373 for (
size_t i = 0;
i <
pop->log_f_multiplier.size();
i++) {
375 pop->log_f_multiplier[
i];
378 ss <<
"{\n \"name\": \"log_f_multiplier\",\n";
380 ss <<
" \"type\": \"vector\",\n";
381 ss <<
" \"dimensionality\": {\n";
382 ss <<
" \"header\": [" <<
"\"n_years\"" <<
"],\n";
388 for (
size_t i = 0;
i <
pop->spawning_biomass_ratio.size();
i++) {
390 pop->spawning_biomass_ratio[
i];
393 ss <<
"{\n \"name\": \"spawning_biomass_ratio\",\n";
396 ss <<
" \"type\": \"vector\",\n";
397 ss <<
" \"dimensionality\": {\n";
398 ss <<
" \"header\": [" <<
"\"n_years\"" <<
"],\n";
405 for (
size_t i = 0;
i <
pop->log_init_naa.size();
i++) {
407 pop->log_init_naa[
i];
409 ss <<
" {\n\"name\": \"log_init_naa\",\n";
411 ss <<
" \"type\": \"vector\",\n";
412 ss <<
" \"dimensionality\": {\n";
413 ss <<
" \"header\": [" <<
"\"n_ages\"" <<
"],\n";
420 ss <<
" \"derived_quantities\": [\n";
422 std::map<std::string, fims::Vector<double>>
dqs =
423 model_ptr->GetPopulationDerivedQuantities(
426 std::map<std::string, fims_popdy::DimensionInfo>
dim_info =
432 ss <<
" \"name\": \"Population\",\n";
434 ss <<
" \"type\": \"population\",\n";
436 <<
" not found in Information.\",\n";
442 ss <<
" \"derived_quantities\": []}\n";
456 std::stringstream
ss;
465 for (
size_t i = 0;
i <
dim_info.dims.size(); ++
i) {
475 <<
dim_info.dim_names[1] <<
"\"],\n";
477 for (
size_t i = 0;
i <
dim_info.dims.size(); ++
i) {
490 for (
size_t i = 0;
i <
dim_info.dims.size(); ++
i) {
507 ss <<
"\"name\":\"" << (*it).first <<
"\",\n";
510 ss << std::fixed << std::setprecision(10);
512 for (
size_t i = 0;
i <
dq.size() - 1;
i++) {
515 ss <<
"-999" <<
", ";
520 if (
dq[
dq.size() - 1] !=
dq[
dq.size() - 1])
522 ss <<
"-999]" <<
"\n";
524 ss <<
dq[
dq.size() - 1] <<
"]\n";
540 std::map<std::string, fims_popdy::DimensionInfo> &
dim_info) {
541 std::stringstream
ss;
542 std::map<std::string, fims_popdy::DimensionInfo>::iterator
dim_info_it;
543 std::map<std::string, fims::Vector<double>>::iterator
it;
544 std::map<std::string, fims::Vector<double>>::iterator
end_it;
546 typename std::map<std::string, fims::Vector<double>>::iterator
574 std::stringstream
ss;
578 "Fleet pointer is null; cannot get id. Not found in live objects.");
582 std::shared_ptr<fims_info::Information<double>>
info =
587 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
588 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
595 if (
fit !=
info->fleets.end()) {
596 std::shared_ptr<fims_popdy::Fleet<double>> &
fleet = (*fit).second;
599 ss <<
" \"module_name\": \"Fleet\",\n";
605 ss <<
"\"data_ids\" : [\n";
608 ss <<
"{\"lengthcomp\": "
615 ss <<
"\"parameters\": [\n";
621 ss <<
" \"name\": \"log_Fmort\",\n";
623 ss <<
" \"type\": \"vector\",\n";
624 ss <<
" \"dimensionality\": {\n";
625 ss <<
" \"header\": [\"" <<
"n_years" <<
"\"],\n";
631 for (
size_t i = 0;
i <
fleet->log_q.size();
i++) {
634 ss <<
" \"name\": \"log_q\",\n";
636 ss <<
" \"type\": \"vector\",\n";
637 ss <<
" \"dimensionality\": {\n";
638 ss <<
" \"header\": [\"" <<
"na" <<
"\"],\n";
639 ss <<
" \"dimensions\": [" <<
fleet->log_q.size() <<
"]\n},\n";
643 ss <<
"], \"derived_quantities\": [";
645 std::map<std::string, fims::Vector<double>>
dqs =
647 std::map<std::string, fims_popdy::DimensionInfo>
dim_info =
652 ss <<
" \"name\": \"Fleet\",\n";
653 ss <<
" \"type\": \"fleet\",\n";
655 <<
" not found in Information.\",\n";
656 ss <<
" \"derived_quantities\": []}\n";
669 std::set<uint32_t> fleet_ids;
672 typename std::set<uint32_t>::iterator
pit;
673 typename std::set<uint32_t>::iterator
fids;
677 std::dynamic_pointer_cast<PopulationInterface>(
686 fleet_ids.insert(*
fids);
691 for (
fids = fleet_ids.begin();
fids != fleet_ids.end();
fids++) {
693 std::dynamic_pointer_cast<FleetInterface>(
700 std::shared_ptr<fims_info::Information<double>>
info =
703 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model =
704 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
711 model->do_reporting =
false;
716 std::stringstream
ss;
721 ss <<
" \"name\": \"CatchAtAge\",\n";
722 ss <<
" \"type\": \"model\",\n";
723 ss <<
" \"estimation_framework\": ";
725 ss <<
"\"Template_Model_Builder (TMB)\",";
729 ss <<
" \"id\": " << this->
get_id() <<
",\n";
730 ss <<
" \"objective_function_value\": " << value <<
",\n";
731 ss <<
"\"growth\":[\n";
748 ss <<
"\"recruitment\": [\n";
763 ss <<
"\"maturity\": [\n";
778 ss <<
"\"selectivity\": [\n";
793 ss <<
" \"population_ids\": [";
802 ss <<
" \"fleet_ids\": [";
804 for (
fids = fleet_ids.begin();
fids != fleet_ids.end();
fids++) {
806 if (std::next(
fids) != fleet_ids.end()) {
811 ss <<
"\"populations\": [\n";
812 typename std::set<uint32_t>::iterator
pop_it;
813 typename std::set<uint32_t>::iterator
pop_end_it;
817 pop_second_to_last_it = std::prev(
pop_end_it);
824 std::dynamic_pointer_cast<PopulationInterface>(
827 std::set<uint32_t>::iterator
fids;
830 fleet_ids.insert(*
fids);
836 " not found in live objects.");
842 std::dynamic_pointer_cast<PopulationInterface>(
845 std::set<uint32_t>::iterator
fids;
848 fleet_ids.insert(*
fids);
853 " not found in live objects.");
859 ss <<
"\"fleets\": [\n";
861 typename std::set<uint32_t>::iterator
fleet_it;
872 std::dynamic_pointer_cast<FleetInterface>(
879 " not found in live objects.");
884 std::dynamic_pointer_cast<FleetInterface>(
890 " not found in live objects.");
896 ss <<
"\"density_components\": [\n";
899 uint32_t, std::shared_ptr<DistributionsInterfaceBase>>::iterator
dit;
913 ss <<
"\"data\": [\n";
914 typename std::map<uint32_t, std::shared_ptr<DataInterfaceBase>>::iterator
918 std::shared_ptr<DataInterfaceBase>
data_interface = (*d_it).second;
930 model->do_reporting =
true;
937 template <
typename Type>
939 std::shared_ptr<fims_info::Information<Type>>
info =
942 std::shared_ptr<fims_popdy::CatchAtAge<Type>>
model =
943 std::make_shared<fims_popdy::CatchAtAge<Type>>();
949 model->AddPopulation((*
it));
952 std::set<uint32_t> fleet_ids;
953 typedef typename std::set<uint32_t>::iterator fleet_ids_iterator;
962 throw std::runtime_error(
"Population ID " + std::to_string(*
it) +
963 " not found in live_objects");
966 std::dynamic_pointer_cast<PopulationInterface>(
it2->second);
971 std::map<std::string, fims_popdy::DimensionInfo>
975 std::stringstream
ss;
982 "total_landings_weight",
991 "total_landings_numbers",
1031 "unfished_numbers_at_age",
1062 "unfished_spawning_biomass",
1070 "proportion_mature_at_age",
1079 "expected_recruitment",
1094 for (fleet_ids_iterator
fit =
population->fleet_ids->begin();
1096 fleet_ids.insert(*
fit);
1100 for (fleet_ids_iterator
it = fleet_ids.begin();
it != fleet_ids.end();
1103 std::dynamic_pointer_cast<FleetInterface>(
1109 std::map<std::string, fims_popdy::DimensionInfo>
1119 "landings_numbers_at_age",
1128 "landings_weight_at_age",
1137 "landings_numbers_at_length",
1162 "landings_expected",
1170 "log_landings_expected",
1178 "agecomp_proportion",
1187 "lengthcomp_proportion",
1197 "index_numbers_at_age",
1206 "index_weight_at_age",
1215 "index_weight_at_age",
1224 "index_numbers_at_length",
1250 "log_index_expected",
1273 "lengthcomp_expected",
The CatchAtAgeInterface class is used to interface with the CatchAtAge model. It inherits from the Fi...
Definition rcpp_models.hpp:218
virtual std::string to_json()
Serialize the fishery model to a JSON string.
Definition rcpp_models.hpp:664
void AddPopulation(uint32_t id)
Definition rcpp_models.hpp:241
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:453
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_models.hpp:310
CatchAtAgeInterface()
The constructor.
Definition rcpp_models.hpp:223
void DoReporting(bool report)
Enable or disable reporting for the CatchAtAge model.
Definition rcpp_models.hpp:262
virtual uint32_t get_id()
Method to get this id.
Definition rcpp_models.hpp:305
std::string fleet_to_json(FleetInterface *fleet_interface)
Method to convert a fleet to a JSON string.
Definition rcpp_models.hpp:573
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:538
std::string population_to_json(PopulationInterface *population_interface)
Method to convert a population to a JSON string.
Definition rcpp_models.hpp:315
CatchAtAgeInterface(const CatchAtAgeInterface &other)
Construct a new Catch At Age Interface object.
Definition rcpp_models.hpp:235
bool IsReporting()
Check if reporting is enabled for the CatchAtAge model.
Definition rcpp_models.hpp:284
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:575
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:585
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:590
The FisheryModelInterfaceBase class is the base class for all fishery models in the FIMS Rcpp interfa...
Definition rcpp_models.hpp:32
double min(const std::valarray< double > &v)
Minimum method to calculate the minimum of an array or vector of doubles.
Definition rcpp_models.hpp:181
FisheryModelInterfaceBase(const FisheryModelInterfaceBase &other)
Construct a new Data Interface Base object.
Definition rcpp_models.hpp:76
static uint32_t id_g
The static id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:47
virtual uint32_t get_id()=0
Get the ID for the child fleet interface objects to inherit.
FisheryModelInterfaceBase()
The constructor.
Definition rcpp_models.hpp:63
std::set< uint32_t >::iterator population_id_iterator
Iterator for population ids.
Definition rcpp_models.hpp:41
uint32_t id
The local id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:51
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:199
virtual std::string to_json()
Serialize the fishery model to a JSON string.
Definition rcpp_models.hpp:97
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:58
double sum(const std::valarray< double > &v)
Sum method to calculate the sum of an array or vector of doubles.
Definition rcpp_models.hpp:152
Rcpp::NumericVector get_random_parameters_vector()
Get the vector of random effect parameters for the model.
Definition rcpp_models.hpp:133
Rcpp::NumericVector get_fixed_parameters_vector()
Get the vector of fixed effect parameters for the model.
Definition rcpp_models.hpp:113
std::shared_ptr< std::set< uint32_t > > population_ids
The set of population ids that this fishery model operates on.
Definition rcpp_models.hpp:37
virtual ~FisheryModelInterfaceBase()
The destructor.
Definition rcpp_models.hpp:82
double sum(const std::vector< double > &v)
Sum method for a vector of doubles.
Definition rcpp_models.hpp:166
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:35
The Rcpp interface for Fleet to instantiate from R: fleet <- methods::new(Fleet)
Definition rcpp_fleet.hpp:74
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:86
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
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:655
The Rcpp interface to declare fleets. Allows for the use of methods::new() in R.
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,...
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