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"
49 static std::map<uint32_t, std::shared_ptr<FisheryModelInterfaceBase>>
93 return "std::string to_json() not yet implemented.";
115std::map<uint32_t, std::shared_ptr<FisheryModelInterfaceBase>>
126 std::shared_ptr<std::set<uint32_t>> population_ids;
130 typedef typename std::set<uint32_t>::iterator population_id_iterator;
137 std::map<std::string, std::vector<double>> se_values;
144 this->population_ids = std::make_shared<std::set<uint32_t>>();
145 std::shared_ptr<CatchAtAgeInterface>
caa =
146 std::make_shared<CatchAtAgeInterface>(*
this);
158 population_ids(
other.population_ids) {}
164 this->population_ids->insert(
id);
166 std::map<uint32_t, std::shared_ptr<PopulationInterfaceBase>>::iterator
pit;
169 std::shared_ptr<PopulationInterfaceBase> &
pop = (*pit).second;
170 pop->initialize_catch_at_age.set(
true);
186 std::shared_ptr<fims_info::Information<double>>
info =
191 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
192 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
208 std::shared_ptr<fims_info::Information<double>>
info =
213 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
214 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
238 std::stringstream
ss;
241 std::shared_ptr<PopulationInterfaceBase>>::iterator
248 " not found in live objects.");
253 std::dynamic_pointer_cast<PopulationInterface>((*pi_it).second);
255 std::shared_ptr<fims_info::Information<double>>
info =
260 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
261 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
268 if (
pit !=
info->populations.end()) {
269 std::shared_ptr<fims_popdy::Population<double>> &
pop = (*pit).second;
272 ss <<
" \"module_name\": \"Population\",\n";
280 ss <<
" \"parameters\": [\n";
283 for (
size_t i = 0;
i <
pop->log_M.size();
i++) {
288 ss <<
"{\n \"name\": \"log_M\",\n";
290 ss <<
" \"type\": \"vector\",\n";
291 ss <<
" \"dimensionality\": {\n";
292 ss <<
" \"header\": [" <<
"\"n_years\", \"n_ages\"" <<
"],\n";
299 pop->log_f_multiplier.size(), -999);
302 for (
size_t i = 0;
i <
pop->log_f_multiplier.size();
i++) {
304 pop->log_f_multiplier[
i];
309 ss <<
"{\n \"name\": \"log_f_multiplier\",\n";
311 ss <<
" \"type\": \"vector\",\n";
312 ss <<
" \"dimensionality\": {\n";
313 ss <<
" \"header\": [" <<
"\"n_years\"" <<
"],\n";
320 pop->spawning_biomass_ratio.size(), -999);
321 this->
get_se_values(
"spawning_biomass_ratio", this->se_values,
323 for (
size_t i = 0;
i <
pop->spawning_biomass_ratio.size();
i++) {
325 pop->spawning_biomass_ratio[
i];
330 ss <<
"{\n \"name\": \"spawning_biomass_ratio\",\n";
333 ss <<
" \"type\": \"vector\",\n";
334 ss <<
" \"dimensionality\": {\n";
335 ss <<
" \"header\": [" <<
"\"n_years\"" <<
"],\n";
346 for (
size_t i = 0;
i <
pop->log_init_naa.size();
i++) {
348 pop->log_init_naa[
i];
352 ss <<
" {\n\"name\": \"log_init_naa\",\n";
354 ss <<
" \"type\": \"vector\",\n";
355 ss <<
" \"dimensionality\": {\n";
356 ss <<
" \"header\": [" <<
"\"n_ages\"" <<
"],\n";
363 ss <<
" \"derived_quantities\": [\n";
365 std::map<std::string, fims::Vector<double>>
dqs =
366 model_ptr->GetPopulationDerivedQuantities(
369 std::map<std::string, fims_popdy::DimensionInfo>
dim_info =
375 ss <<
" \"name\": \"Population\",\n";
377 ss <<
" \"type\": \"population\",\n";
379 <<
" not found in Information.\",\n";
385 ss <<
" \"derived_quantities\": []}\n";
399 std::stringstream
ss;
400 std::string name = (*it).first;
404 typename std::map<std::string, std::vector<double>>::iterator
se_vals =
405 this->se_values.find(name);
407 if (
se_vals != this->se_values.end()) {
416 for (
size_t i = 0;
i <
dim_info.dims.size(); ++
i) {
426 <<
dim_info.dim_names[1] <<
"\"],\n";
428 for (
size_t i = 0;
i <
dim_info.dims.size(); ++
i) {
441 for (
size_t i = 0;
i <
dim_info.dims.size(); ++
i) {
458 ss <<
"\"name\":\"" << (*it).first <<
"\",\n";
461 ss << std::fixed << std::setprecision(10);
463 for (
size_t i = 0;
i <
dq.size() - 1;
i++) {
466 ss <<
"-999" <<
", ";
471 if (
dq[
dq.size() - 1] !=
dq[
dq.size() - 1])
473 ss <<
"-999]" <<
",\n";
475 ss <<
dq[
dq.size() - 1] <<
"],\n";
484 throw std::runtime_error(
485 "Standard error vector size is smaller than derived quantity "
486 "size for derived quantity " +
495 ss <<
"\"uncertainty\": " << uncertainty <<
"\n";
496 }
catch (
const std::exception &
e) {
497 throw std::runtime_error(
498 "Error processing uncertainty for derived quantity " + name +
": " +
502 ss <<
"\"uncertainty\": [";
503 for (
size_t i = 0;
i <
dq.size(); ++
i) {
505 if (
i <
dq.size() - 1) {
522 std::map<std::string, fims_popdy::DimensionInfo> &
dim_info) {
523 std::stringstream
ss;
524 std::map<std::string, fims_popdy::DimensionInfo>::iterator
dim_info_it;
525 std::map<std::string, fims::Vector<double>>::iterator
it;
526 std::map<std::string, fims::Vector<double>>::iterator
end_it;
528 typename std::map<std::string, fims::Vector<double>>::iterator
556 std::stringstream
ss;
561 " not found in live objects.");
565 std::shared_ptr<fims_info::Information<double>>
info =
570 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model_ptr =
571 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
578 if (
fit !=
info->fleets.end()) {
579 std::shared_ptr<fims_popdy::Fleet<double>> &
fleet = (*fit).second;
582 ss <<
" \"module_name\": \"Fleet\",\n";
588 ss <<
"\"data_ids\" : [\n";
591 ss <<
"{\"lengthcomp\": "
598 ss <<
"\"parameters\": [\n";
607 ss <<
" \"name\": \"log_Fmort\",\n";
609 ss <<
" \"type\": \"vector\",\n";
610 ss <<
" \"dimensionality\": {\n";
611 ss <<
" \"header\": [\"" <<
"n_years" <<
"\"],\n";
619 for (
size_t i = 0;
i <
fleet->log_q.size();
i++) {
623 ss <<
" \"name\": \"log_q\",\n";
625 ss <<
" \"type\": \"vector\",\n";
626 ss <<
" \"dimensionality\": {\n";
627 ss <<
" \"header\": [\"" <<
"na" <<
"\"],\n";
628 ss <<
" \"dimensions\": [" <<
fleet->log_q.size() <<
"]\n},\n";
632 ss <<
"], \"derived_quantities\": [";
634 std::map<std::string, fims::Vector<double>>
dqs =
636 std::map<std::string, fims_popdy::DimensionInfo>
dim_info =
641 ss <<
" \"name\": \"Fleet\",\n";
642 ss <<
" \"type\": \"fleet\",\n";
644 <<
" not found in Information.\",\n";
645 ss <<
" \"derived_quantities\": []}\n";
659 std::shared_ptr<fims_info::Information<double>>
info0 =
662 Rcpp::NumericVector
p;
664 for (
size_t i = 0;
i <
info0->fixed_effects_parameters.size();
i++) {
665 p.push_back(*
info0->fixed_effects_parameters[
i]);
680 std::shared_ptr<fims_info::Information<double>>
d0 =
683 Rcpp::NumericVector
p;
685 for (
size_t i = 0;
i <
d0->random_effects_parameters.size();
i++) {
686 p.push_back(*
d0->random_effects_parameters[
i]);
703 Rcpp::Environment
base = Rcpp::Environment::base_env();
707 Rcpp::Environment
TMB = Rcpp::Environment::namespace_env(
"TMB");
711 Rcpp::Environment
global = Rcpp::Environment::global_env();
713 Rcpp::List parameters = Rcpp::List::create(
718 Rcpp::Named(
"data") = Rcpp::List::create(),
719 Rcpp::Named(
"parameters") = parameters, Rcpp::Named(
"DLL") =
"FIMS",
720 Rcpp::Named(
"silent") =
true, Rcpp::Named(
"map") =
R_NilValue,
721 Rcpp::Named(
"random") =
"re");
724 Rcpp::Function
func =
obj[
"fn"];
739 Rcpp::NumericMatrix
mat;
756 std::map<std::string, std::vector<double>>
grouped;
758 for (
int i = 0;
i <
nrow;
i++) {
759 std::string key = Rcpp::as<std::string>(
rownames[
i]);
776 mat = Rcpp::NumericMatrix(0, 0);
777 rownames = Rcpp::CharacterVector(0);
778 colnames = Rcpp::CharacterVector(0);
783 return Rcpp::List::create(
784 Rcpp::Named(
"objective_function_value") =
of_value,
785 Rcpp::Named(
"gradient") =
grad,
786 Rcpp::Named(
"max_gradient_component") =
maxgc,
787 Rcpp::Named(
"report") =
rep, Rcpp::Named(
"sdr_summary") =
sdr_summary,
788 Rcpp::Named(
"sdr_summary_matrix") =
mat,
799 double max_gc = Rcpp::as<double>(
report[
"max_gradient_component"]);
800 Rcpp::NumericVector
grad =
report[
"gradient"];
801 double of_value = Rcpp::as<double>(
report[
"objective_function_value"]);
808 std::map<std::string, std::vector<double>>
grouped_cpp;
812 std::string key = Rcpp::as<std::string>(
names[
i]);
813 Rcpp::NumericVector
vec =
828 std::set<uint32_t> fleet_ids;
831 typename std::set<uint32_t>::iterator
pit;
832 typename std::set<uint32_t>::iterator
fids;
833 for (
pit = this->population_ids->begin();
834 pit != this->population_ids->end();
pit++) {
836 std::dynamic_pointer_cast<PopulationInterface>(
845 fleet_ids.insert(*
fids);
850 for (
fids = fleet_ids.begin();
fids != fleet_ids.end();
fids++) {
852 std::dynamic_pointer_cast<FleetInterface>(
859 std::shared_ptr<fims_info::Information<double>>
info =
862 std::shared_ptr<fims_popdy::CatchAtAge<double>>
model =
863 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
870 model->do_reporting =
false;
875 std::stringstream
ss;
880 ss <<
" \"name\": \"CatchAtAge\",\n";
881 ss <<
" \"type\": \"model\",\n";
882 ss <<
" \"estimation_framework\": ";
884 ss <<
"\"Template_Model_Builder (TMB)\",";
888 ss <<
" \"id\": " << this->
get_id() <<
",\n";
889 ss <<
" \"objective_function_value\": " << value <<
",\n";
890 ss <<
"\"growth\":[\n";
908 ss <<
"\"recruitment\": [\n";
924 ss <<
"\"maturity\": [\n";
940 ss <<
"\"selectivity\": [\n";
956 ss <<
" \"population_ids\": [";
957 for (
pit = this->population_ids->begin();
958 pit != this->population_ids->end();
pit++) {
960 if (std::next(
pit) != this->population_ids->end()) {
965 ss <<
" \"fleet_ids\": [";
967 for (
fids = fleet_ids.begin();
fids != fleet_ids.end();
fids++) {
969 if (std::next(
fids) != fleet_ids.end()) {
974 ss <<
"\"populations\": [\n";
975 typename std::set<uint32_t>::iterator
pop_it;
976 typename std::set<uint32_t>::iterator
pop_end_it;
979 if (
pop_end_it != this->population_ids->begin()) {
984 for (
pop_it = this->population_ids->begin();
987 std::dynamic_pointer_cast<PopulationInterface>(
990 std::set<uint32_t>::iterator
fids;
993 fleet_ids.insert(*
fids);
999 " not found in live objects.");
1005 std::dynamic_pointer_cast<PopulationInterface>(
1008 std::set<uint32_t>::iterator
fids;
1011 fleet_ids.insert(*
fids);
1016 " not found in live objects.");
1022 ss <<
"\"fleets\": [\n";
1024 typename std::set<uint32_t>::iterator
fleet_it;
1035 std::dynamic_pointer_cast<FleetInterface>(
1042 " not found in live objects.");
1047 std::dynamic_pointer_cast<FleetInterface>(
1053 " not found in live objects.");
1059 ss <<
"\"density_components\": [\n";
1062 uint32_t, std::shared_ptr<DistributionsInterfaceBase>>::iterator
dit;
1076 ss <<
"\"data\": [\n";
1077 typename std::map<uint32_t, std::shared_ptr<DataInterfaceBase>>::iterator
1081 std::shared_ptr<DataInterfaceBase>
data_interface = (*d_it).second;
1092 ss <<
" \"log\": {\n";
1097 model->do_reporting =
true;
1108 double sum(
const std::valarray<double> &
v) {
1110 for (
size_t i = 0;
i <
v.size();
i++) {
1122 double sum(
const std::vector<double> &
v) {
1124 for (
size_t i = 0;
i <
v.size();
i++) {
1137 double min(
const std::valarray<double> &
v) {
1139 for (
size_t i = 1;
i <
v.size();
i++) {
1154 std::valarray<double>
fabs(
const std::valarray<double> &
v) {
1155 std::valarray<double>
result(
v.size());
1156 for (
size_t i = 0;
i <
v.size();
i++) {
1164 template <
typename Type>
1166 std::shared_ptr<fims_info::Information<Type>>
info =
1169 std::shared_ptr<fims_popdy::CatchAtAge<Type>>
model =
1170 std::make_shared<fims_popdy::CatchAtAge<Type>>();
1172 population_id_iterator
it;
1174 for (
it = this->population_ids->begin();
it != this->population_ids->end();
1179 std::set<uint32_t> fleet_ids;
1180 typedef typename std::set<uint32_t>::iterator fleet_ids_iterator;
1185 for (it = this->population_ids->begin();
it != this->population_ids->end();
1189 throw std::runtime_error(
"Population ID " + std::to_string(*
it) +
1190 " not found in live_objects");
1193 std::dynamic_pointer_cast<PopulationInterface>(
it2->second);
1198 std::map<std::string, fims_popdy::DimensionInfo>
1202 std::stringstream
ss;
1209 "total_landings_weight",
1218 "total_landings_numbers",
1258 "unfished_numbers_at_age",
1289 "unfished_spawning_biomass",
1297 "proportion_mature_at_age",
1306 "expected_recruitment",
1321 for (fleet_ids_iterator
fit =
population->fleet_ids->begin();
1323 fleet_ids.insert(*
fit);
1327 for (fleet_ids_iterator
it = fleet_ids.begin();
it != fleet_ids.end();
1330 std::dynamic_pointer_cast<FleetInterface>(
1336 std::map<std::string, fims_popdy::DimensionInfo>
1346 "landings_numbers_at_age",
1355 "landings_weight_at_age",
1364 "landings_numbers_at_length",
1389 "landings_expected",
1397 "log_landings_expected",
1405 "agecomp_proportion",
1414 "lengthcomp_proportion",
1424 "index_numbers_at_age",
1433 "index_weight_at_age",
1442 "index_weight_at_age",
1451 "index_numbers_at_length",
1477 "log_index_expected",
1500 "lengthcomp_expected",
1525#ifdef TMBAD_FRAMEWORK
The CatchAtAgeInterface class is used to interface with the CatchAtAge model. It inherits from the Fi...
Definition rcpp_models.hpp:122
double min(const std::valarray< double > &v)
Minimum method to calculate the minimum of an array or vector of doubles.
Definition rcpp_models.hpp:1137
Rcpp::NumericVector get_fixed_parameters_vector()
Get the vector of fixed effect parameters for the CatchAtAge model.
Definition rcpp_models.hpp:657
void AddPopulation(uint32_t id)
Definition rcpp_models.hpp:163
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:396
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_models.hpp:232
CatchAtAgeInterface()
The constructor.
Definition rcpp_models.hpp:143
Rcpp::NumericVector get_random_parameters_vector()
Get the vector of random effect parameters for the CatchAtAge model.
Definition rcpp_models.hpp:678
void DoReporting(bool report)
Enable or disable reporting for the CatchAtAge model.
Definition rcpp_models.hpp:184
Rcpp::List get_report(bool do_sd_report=true)
Get the report output for the CatchAtAge model.
Definition rcpp_models.hpp:702
virtual uint32_t get_id()
Method to get this id.
Definition rcpp_models.hpp:227
double sum(const std::vector< double > &v)
Sum method for a vector of doubles.
Definition rcpp_models.hpp:1122
std::string fleet_to_json(FleetInterface *fleet_interface)
Method to convert a fleet to a JSON string.
Definition rcpp_models.hpp:555
double sum(const std::valarray< double > &v)
Sum method to calculate the sum of an array or vector of doubles.
Definition rcpp_models.hpp:1108
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:520
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:1154
virtual std::string to_json(bool do_sd_report=true)
Serialize the fishery model to a JSON string.
Definition rcpp_models.hpp:796
std::string population_to_json(PopulationInterface *population_interface)
Method to convert a population to a JSON string.
Definition rcpp_models.hpp:237
CatchAtAgeInterface(const CatchAtAgeInterface &other)
Construct a new Catch At Age Interface object.
Definition rcpp_models.hpp:156
bool IsReporting()
Check if reporting is enabled for the CatchAtAge model.
Definition rcpp_models.hpp:206
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:634
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:644
void get_se_values(std::string name, std::map< std::string, std::vector< double > > &se_values, fims::Vector< double > &values)
Method to extract standard error values from the se_values working map.
Definition rcpp_interface_base.hpp:672
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:649
The FisheryModelInterfaceBase class is the base class for all fishery models in the FIMS Rcpp interfa...
Definition rcpp_models.hpp:34
FisheryModelInterfaceBase(const FisheryModelInterfaceBase &other)
Construct a new Data Interface Base object.
Definition rcpp_models.hpp:67
static uint32_t id_g
The static id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:39
virtual uint32_t get_id()=0
Get the ID for the child fleet interface objects to inherit.
virtual std::string to_json(bool do_sd_report=true)
Serialize the fishery model to a JSON string.
Definition rcpp_models.hpp:92
FisheryModelInterfaceBase()
The constructor.
Definition rcpp_models.hpp:55
virtual Rcpp::List calculate_reference_points()
A function to calculate reference points for the fishery model.
Definition rcpp_models.hpp:101
uint32_t id
The local id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:43
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:50
virtual ~FisheryModelInterfaceBase()
The destructor.
Definition rcpp_models.hpp:73
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:224
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:606
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:279
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