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"
14#include "../../../models/fisheries_models.hpp"
15#include "../../../utilities/fims_json.hpp"
48 static std::map<uint32_t, std::shared_ptr<FisheryModelInterfaceBase>>
88 return "std::string to_json() not yet implemented.";
100std::map<uint32_t, std::shared_ptr<FisheryModelInterfaceBase>>
111 std::shared_ptr<std::set<uint32_t>> population_ids;
115 typedef typename std::set<uint32_t>::iterator population_id_iterator;
122 std::map<std::string, std::vector<double>> se_values;
129 this->population_ids = std::make_shared<std::set<uint32_t>>();
130 std::shared_ptr<CatchAtAgeInterface>
caa =
131 std::make_shared<CatchAtAgeInterface>(*
this);
143 population_ids(
other.population_ids) {}
149 this->population_ids->insert(
id);
151 std::map<uint32_t, std::shared_ptr<PopulationInterfaceBase>>::iterator
pit;
154 std::shared_ptr<PopulationInterfaceBase> &
pop = (*pit).second;
155 pop->initialize_catch_at_age.set(
true);
171 std::shared_ptr<fims_info::Information<double>>
info =
176 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
177 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
193 std::shared_ptr<fims_info::Information<double>>
info =
198 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
199 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
223 std::stringstream
ss;
226 std::shared_ptr<PopulationInterfaceBase>>::iterator
233 " not found in live objects.");
238 std::dynamic_pointer_cast<PopulationInterface>((*pi_it).second);
240 std::shared_ptr<fims_info::Information<double>>
info =
245 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
246 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
253 if (
pit !=
info->populations.end()) {
254 std::shared_ptr<fims_popdy::Population<double>> &
pop = (*pit).second;
257 ss <<
" \"module_name\": \"Population\",\n";
265 ss <<
" \"parameters\": [\n";
266 for (
size_t i = 0;
i <
pop->log_M.size();
i++) {
270 ss <<
"{\n \"name\": \"log_M\",\n";
272 ss <<
" \"type\": \"vector\",\n";
273 ss <<
" \"dimensionality\": {\n";
274 ss <<
" \"header\": [" <<
"\"n_years\", \"n_ages\"" <<
"],\n";
280 for (
size_t i = 0;
i <
pop->log_f_multiplier.size();
i++) {
282 pop->log_f_multiplier[
i];
285 ss <<
"{\n \"name\": \"log_f_multiplier\",\n";
287 ss <<
" \"type\": \"vector\",\n";
288 ss <<
" \"dimensionality\": {\n";
289 ss <<
" \"header\": [" <<
"\"n_years\"" <<
"],\n";
295 for (
size_t i = 0;
i <
pop->spawning_biomass_ratio.size();
i++) {
297 pop->spawning_biomass_ratio[
i];
300 ss <<
"{\n \"name\": \"spawning_biomass_ratio\",\n";
303 ss <<
" \"type\": \"vector\",\n";
304 ss <<
" \"dimensionality\": {\n";
305 ss <<
" \"header\": [" <<
"\"n_years\"" <<
"],\n";
312 for (
size_t i = 0;
i <
pop->log_init_naa.size();
i++) {
314 pop->log_init_naa[
i];
316 ss <<
" {\n\"name\": \"log_init_naa\",\n";
318 ss <<
" \"type\": \"vector\",\n";
319 ss <<
" \"dimensionality\": {\n";
320 ss <<
" \"header\": [" <<
"\"n_ages\"" <<
"],\n";
327 ss <<
" \"derived_quantities\": [\n";
329 std::map<std::string, fims::Vector<double>>
dqs =
330 model_ptr->GetPopulationDerivedQuantities(
333 std::map<std::string, fims_popdy::DimensionInfo>
dim_info =
339 ss <<
" \"name\": \"Population\",\n";
341 ss <<
" \"type\": \"population\",\n";
343 <<
" not found in Information.\",\n";
349 ss <<
" \"derived_quantities\": []}\n";
363 std::stringstream
ss;
372 for (
size_t i = 0;
i <
dim_info.dims.size(); ++
i) {
382 <<
dim_info.dim_names[1] <<
"\"],\n";
384 for (
size_t i = 0;
i <
dim_info.dims.size(); ++
i) {
397 for (
size_t i = 0;
i <
dim_info.dims.size(); ++
i) {
414 ss <<
"\"name\":\"" << (*it).first <<
"\",\n";
417 ss << std::fixed << std::setprecision(10);
419 for (
size_t i = 0;
i <
dq.size() - 1;
i++) {
422 ss <<
"-999" <<
", ";
427 if (
dq[
dq.size() - 1] !=
dq[
dq.size() - 1])
429 ss <<
"-999]" <<
"\n";
431 ss <<
dq[
dq.size() - 1] <<
"]\n";
447 std::map<std::string, fims_popdy::DimensionInfo> &
dim_info) {
448 std::stringstream
ss;
449 std::map<std::string, fims_popdy::DimensionInfo>::iterator
dim_info_it;
450 std::map<std::string, fims::Vector<double>>::iterator
it;
451 std::map<std::string, fims::Vector<double>>::iterator
end_it;
453 typename std::map<std::string, fims::Vector<double>>::iterator
481 std::stringstream
ss;
485 "Fleet pointer is null; cannot get id. Not found in live objects.");
489 std::shared_ptr<fims_info::Information<double>>
info =
494 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
495 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
502 if (
fit !=
info->fleets.end()) {
503 std::shared_ptr<fims_popdy::Fleet<double>> &
fleet = (*fit).second;
506 ss <<
" \"module_name\": \"Fleet\",\n";
512 ss <<
"\"data_ids\" : [\n";
515 ss <<
"{\"lengthcomp\": "
522 ss <<
"\"parameters\": [\n";
528 ss <<
" \"name\": \"log_Fmort\",\n";
530 ss <<
" \"type\": \"vector\",\n";
531 ss <<
" \"dimensionality\": {\n";
532 ss <<
" \"header\": [\"" <<
"n_years" <<
"\"],\n";
538 for (
size_t i = 0;
i <
fleet->log_q.size();
i++) {
541 ss <<
" \"name\": \"log_q\",\n";
543 ss <<
" \"type\": \"vector\",\n";
544 ss <<
" \"dimensionality\": {\n";
545 ss <<
" \"header\": [\"" <<
"na" <<
"\"],\n";
546 ss <<
" \"dimensions\": [" <<
fleet->log_q.size() <<
"]\n},\n";
550 ss <<
"], \"derived_quantities\": [";
552 std::map<std::string, fims::Vector<double>>
dqs =
554 std::map<std::string, fims_popdy::DimensionInfo>
dim_info =
559 ss <<
" \"name\": \"Fleet\",\n";
560 ss <<
" \"type\": \"fleet\",\n";
562 <<
" not found in Information.\",\n";
563 ss <<
" \"derived_quantities\": []}\n";
577 std::shared_ptr<fims_info::Information<double>>
info0 =
580 Rcpp::NumericVector
p;
582 for (
size_t i = 0;
i <
info0->fixed_effects_parameters.size();
i++) {
583 p.push_back(*
info0->fixed_effects_parameters[
i]);
598 std::shared_ptr<fims_info::Information<double>>
d0 =
601 Rcpp::NumericVector
p;
603 for (
size_t i = 0;
i <
d0->random_effects_parameters.size();
i++) {
604 p.push_back(*
d0->random_effects_parameters[
i]);
618 std::set<uint32_t> fleet_ids;
621 typename std::set<uint32_t>::iterator
pit;
622 typename std::set<uint32_t>::iterator
fids;
623 for (
pit = this->population_ids->begin();
624 pit != this->population_ids->end();
pit++) {
626 std::dynamic_pointer_cast<PopulationInterface>(
635 fleet_ids.insert(*
fids);
640 for (
fids = fleet_ids.begin();
fids != fleet_ids.end();
fids++) {
642 std::dynamic_pointer_cast<FleetInterface>(
649 std::shared_ptr<fims_info::Information<double>>
info =
652 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model =
653 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
660 model->do_reporting =
false;
665 std::stringstream
ss;
670 ss <<
" \"name\": \"CatchAtAge\",\n";
671 ss <<
" \"type\": \"model\",\n";
672 ss <<
" \"estimation_framework\": ";
674 ss <<
"\"Template_Model_Builder (TMB)\",";
678 ss <<
" \"id\": " << this->
get_id() <<
",\n";
679 ss <<
" \"objective_function_value\": " << value <<
",\n";
680 ss <<
"\"growth\":[\n";
697 ss <<
"\"recruitment\": [\n";
712 ss <<
"\"maturity\": [\n";
727 ss <<
"\"selectivity\": [\n";
742 ss <<
" \"population_ids\": [";
743 for (
pit = this->population_ids->begin();
744 pit != this->population_ids->end();
pit++) {
746 if (std::next(
pit) != this->population_ids->end()) {
751 ss <<
" \"fleet_ids\": [";
753 for (
fids = fleet_ids.begin();
fids != fleet_ids.end();
fids++) {
755 if (std::next(
fids) != fleet_ids.end()) {
760 ss <<
"\"populations\": [\n";
761 typename std::set<uint32_t>::iterator
pop_it;
762 typename std::set<uint32_t>::iterator
pop_end_it;
765 if (
pop_end_it != this->population_ids->begin()) {
770 for (
pop_it = this->population_ids->begin();
773 std::dynamic_pointer_cast<PopulationInterface>(
776 std::set<uint32_t>::iterator
fids;
779 fleet_ids.insert(*
fids);
785 " not found in live objects.");
791 std::dynamic_pointer_cast<PopulationInterface>(
794 std::set<uint32_t>::iterator
fids;
797 fleet_ids.insert(*
fids);
802 " not found in live objects.");
808 ss <<
"\"fleets\": [\n";
810 typename std::set<uint32_t>::iterator
fleet_it;
821 std::dynamic_pointer_cast<FleetInterface>(
828 " not found in live objects.");
833 std::dynamic_pointer_cast<FleetInterface>(
839 " not found in live objects.");
845 ss <<
"\"density_components\": [\n";
848 uint32_t, std::shared_ptr<DistributionsInterfaceBase>>::iterator
dit;
862 ss <<
"\"data\": [\n";
863 typename std::map<uint32_t, std::shared_ptr<DataInterfaceBase>>::iterator
867 std::shared_ptr<DataInterfaceBase>
data_interface = (*d_it).second;
878 ss <<
" \"log\": {\n";
883 model->do_reporting =
true;
894 double sum(
const std::valarray<double> &
v) {
896 for (
size_t i = 0;
i <
v.size();
i++) {
908 double sum(
const std::vector<double> &
v) {
910 for (
size_t i = 0;
i <
v.size();
i++) {
923 double min(
const std::valarray<double> &
v) {
925 for (
size_t i = 1;
i <
v.size();
i++) {
940 std::valarray<double>
fabs(
const std::valarray<double> &
v) {
941 std::valarray<double>
result(
v.size());
942 for (
size_t i = 0;
i <
v.size();
i++) {
950 template <
typename Type>
952 std::shared_ptr<fims_info::Information<Type>>
info =
955 std::shared_ptr<fims_popdy::CatchAtAge<Type>>
model =
956 std::make_shared<fims_popdy::CatchAtAge<Type>>();
958 population_id_iterator
it;
960 for (
it = this->population_ids->begin();
it != this->population_ids->end();
965 std::set<uint32_t> fleet_ids;
966 typedef typename std::set<uint32_t>::iterator fleet_ids_iterator;
971 for (it = this->population_ids->begin();
it != this->population_ids->end();
975 throw std::runtime_error(
"Population ID " + std::to_string(*
it) +
976 " not found in live_objects");
979 std::dynamic_pointer_cast<PopulationInterface>(
it2->second);
984 std::map<std::string, fims_popdy::DimensionInfo>
988 std::stringstream
ss;
995 "total_landings_weight",
1004 "total_landings_numbers",
1044 "unfished_numbers_at_age",
1075 "unfished_spawning_biomass",
1083 "proportion_mature_at_age",
1092 "expected_recruitment",
1107 for (fleet_ids_iterator
fit =
population->fleet_ids->begin();
1109 fleet_ids.insert(*
fit);
1113 for (fleet_ids_iterator
it = fleet_ids.begin();
it != fleet_ids.end();
1116 std::dynamic_pointer_cast<FleetInterface>(
1122 std::map<std::string, fims_popdy::DimensionInfo>
1132 "landings_numbers_at_age",
1141 "landings_weight_at_age",
1150 "landings_numbers_at_length",
1175 "landings_expected",
1183 "log_landings_expected",
1191 "agecomp_proportion",
1200 "lengthcomp_proportion",
1210 "index_numbers_at_age",
1219 "index_weight_at_age",
1228 "index_weight_at_age",
1237 "index_numbers_at_length",
1263 "log_index_expected",
1286 "lengthcomp_expected",
The CatchAtAgeInterface class is used to interface with the CatchAtAge model. It inherits from the Fi...
Definition rcpp_models.hpp:107
double min(const std::valarray< double > &v)
Minimum method to calculate the minimum of an array or vector of doubles.
Definition rcpp_models.hpp:923
virtual std::string to_json()
Serialize the fishery model to a JSON string.
Definition rcpp_models.hpp:613
Rcpp::NumericVector get_fixed_parameters_vector()
Get the vector of fixed effect parameters for the CatchAtAge model.
Definition rcpp_models.hpp:575
void AddPopulation(uint32_t id)
Definition rcpp_models.hpp:148
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:360
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_models.hpp:217
CatchAtAgeInterface()
The constructor.
Definition rcpp_models.hpp:128
Rcpp::NumericVector get_random_parameters_vector()
Get the vector of random effect parameters for the CatchAtAge model.
Definition rcpp_models.hpp:596
void DoReporting(bool report)
Enable or disable reporting for the CatchAtAge model.
Definition rcpp_models.hpp:169
virtual uint32_t get_id()
Method to get this id.
Definition rcpp_models.hpp:212
double sum(const std::vector< double > &v)
Sum method for a vector of doubles.
Definition rcpp_models.hpp:908
std::string fleet_to_json(FleetInterface *fleet_interface)
Method to convert a fleet to a JSON string.
Definition rcpp_models.hpp:480
double sum(const std::valarray< double > &v)
Sum method to calculate the sum of an array or vector of doubles.
Definition rcpp_models.hpp:894
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:445
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:940
std::string population_to_json(PopulationInterface *population_interface)
Method to convert a population to a JSON string.
Definition rcpp_models.hpp:222
CatchAtAgeInterface(const CatchAtAgeInterface &other)
Construct a new Catch At Age Interface object.
Definition rcpp_models.hpp:141
bool IsReporting()
Check if reporting is enabled for the CatchAtAge model.
Definition rcpp_models.hpp:191
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:68
Base class for all interface objects.
Definition rcpp_interface_base.hpp:574
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
The FisheryModelInterfaceBase class is the base class for all fishery models in the FIMS Rcpp interfa...
Definition rcpp_models.hpp:33
FisheryModelInterfaceBase(const FisheryModelInterfaceBase &other)
Construct a new Data Interface Base object.
Definition rcpp_models.hpp:66
static uint32_t id_g
The static id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:38
virtual uint32_t get_id()=0
Get the ID for the child fleet interface objects to inherit.
FisheryModelInterfaceBase()
The constructor.
Definition rcpp_models.hpp:54
uint32_t id
The local id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:42
virtual std::string to_json()
Serialize the fishery model to a JSON string.
Definition rcpp_models.hpp:87
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:49
virtual ~FisheryModelInterfaceBase()
The destructor.
Definition rcpp_models.hpp:72
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::shared_ptr< FIMSLog > fims_log
A singleton instance of the log, i.e., where there is only one log. The object is created when the ....
Definition def.hpp:215
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()
Definition model.hpp:45
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:553
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:239
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