FIMS  v0.8.0
Loading...
Searching...
No Matches
rcpp_models.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MODELS_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MODELS_HPP
11
12#include <set>
13#include "../../../common/def.hpp"
14#include "../../../models/fisheries_models.hpp"
15#include "../../../utilities/fims_json.hpp"
16#include "rcpp_population.hpp"
17
19#include "rcpp_population.hpp"
20#include "rcpp_fleet.hpp"
21#include "rcpp_maturity.hpp"
22#include "rcpp_recruitment.hpp"
23#include "rcpp_selectivity.hpp"
24#include <valarray>
25#include <cmath>
26#include <mutex>
27
35 public:
39 static uint32_t id_g;
49 static std::map<uint32_t, std::shared_ptr<FisheryModelInterfaceBase>>
51
57 /* Create instance of map: key is id and value is pointer to
58 FleetInterfaceBase */
59 // FisheryModelInterfaceBase::live_objects[this->id] = this;
60 }
61
69
74
92 virtual std::string to_json(bool do_sd_report = true) {
93 return "std::string to_json() not yet implemented.";
94 }
95
101 virtual Rcpp::List calculate_reference_points() {
102 Rcpp::List result;
103 return result;
104 }
105
109 virtual uint32_t get_id() = 0;
110};
111// static id of the FleetInterfaceBase object
113
114// FleetInterfaceBase to the FleetInterfaceBase objects
115std::map<uint32_t, std::shared_ptr<FisheryModelInterfaceBase>>
117
126 std::shared_ptr<std::set<uint32_t>> population_ids;
130 typedef typename std::set<uint32_t>::iterator population_id_iterator;
131
137 std::map<std::string, std::vector<double>> se_values;
138
139 public:
144 this->population_ids = std::make_shared<std::set<uint32_t>>();
145 std::shared_ptr<CatchAtAgeInterface> caa =
146 std::make_shared<CatchAtAgeInterface>(*this);
149 }
150
158 population_ids(other.population_ids) {}
159
164 this->population_ids->insert(id);
165
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);
171 } else {
172 FIMS_ERROR_LOG("Population with id " + fims::to_string(id) +
173 " not found.");
174 }
175 }
176
184 void DoReporting(bool report) {
185#ifdef TMB_MODEL
186 std::shared_ptr<fims_info::Information<double>> info =
189 model_it = info->models_map.find(this->get_id());
190 if (model_it != info->models_map.end()) {
191 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
192 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
193 (*model_it).second);
194 model_ptr->do_reporting = report;
195 }
196#endif
197 }
198
206 bool IsReporting() {
207#ifdef TMB_MODEL
208 std::shared_ptr<fims_info::Information<double>> info =
211 model_it = info->models_map.find(this->get_id());
212 if (model_it != info->models_map.end()) {
213 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
214 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
215 (*model_it).second);
216 return model_ptr->do_reporting;
217 }
218 return false;
219#else
220 return false;
221#endif
222 }
223
227 virtual uint32_t get_id() { return this->id; }
228
232 virtual void finalize() {}
233
238 std::stringstream ss;
239
240 typename std::map<uint32_t,
241 std::shared_ptr<PopulationInterfaceBase>>::iterator
242 pi_it; // population interface iterator
244 population_interface->get_id());
246 FIMS_ERROR_LOG("Population with id " +
247 fims::to_string(population_interface->get_id()) +
248 " not found in live objects.");
249 return "{}"; // Return empty JSON
250 }
251
252 std::shared_ptr<PopulationInterface> population_interface_ptr =
253 std::dynamic_pointer_cast<PopulationInterface>((*pi_it).second);
254
255 std::shared_ptr<fims_info::Information<double>> info =
257
259 model_it = info->models_map.find(this->get_id());
260 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
261 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
262 (*model_it).second);
263
265
266 pit = info->populations.find(population_interface->get_id());
267
268 if (pit != info->populations.end()) {
269 std::shared_ptr<fims_popdy::Population<double>> &pop = (*pit).second;
270 ss << "{\n";
271
272 ss << " \"module_name\": \"Population\",\n";
273 ss << " \"population\": \"" << population_interface->name << "\",\n";
274 ss << " \"module_id\": " << population_interface->id << ",\n";
275 ss << " \"recruitment_id\": " << population_interface->recruitment_id
276 << ",\n";
277 ss << " \"growth_id\": " << population_interface->growth_id << ",\n";
278 ss << " \"maturity_id\": " << population_interface->maturity_id << ",\n";
279
280 ss << " \"parameters\": [\n";
281 fims::Vector<double> log_M_uncertainty(pop->log_M.size(), -999);
282 this->get_se_values("log_M", this->se_values, log_M_uncertainty);
283 for (size_t i = 0; i < pop->log_M.size(); i++) {
284 population_interface_ptr->log_M[i].final_value_m = pop->log_M[i];
285 population_interface_ptr->log_M[i].uncertainty_m = log_M_uncertainty[i];
286 }
287
288 ss << "{\n \"name\": \"log_M\",\n";
289 ss << " \"id\":" << population_interface->log_M.id_m << ",\n";
290 ss << " \"type\": \"vector\",\n";
291 ss << " \"dimensionality\": {\n";
292 ss << " \"header\": [" << "\"n_years\", \"n_ages\"" << "],\n";
293 ss << " \"dimensions\": [" << population_interface->n_years.get() << ", "
294 << population_interface->n_ages.get() << "]\n},\n";
295 ss << " \"values\": " << population_interface->log_M << "\n\n";
296 ss << "},\n";
297
299 pop->log_f_multiplier.size(), -999);
300 this->get_se_values("log_f_multiplier", this->se_values,
302 for (size_t i = 0; i < pop->log_f_multiplier.size(); i++) {
303 population_interface_ptr->log_f_multiplier[i].final_value_m =
304 pop->log_f_multiplier[i];
305 population_interface_ptr->log_f_multiplier[i].uncertainty_m =
307 }
308
309 ss << "{\n \"name\": \"log_f_multiplier\",\n";
310 ss << " \"id\":" << population_interface->log_f_multiplier.id_m << ",\n";
311 ss << " \"type\": \"vector\",\n";
312 ss << " \"dimensionality\": {\n";
313 ss << " \"header\": [" << "\"n_years\"" << "],\n";
314 ss << " \"dimensions\": [" << population_interface->n_years.get()
315 << "]\n},\n";
316 ss << " \"values\": " << population_interface->log_f_multiplier << "\n\n";
317 ss << "},\n";
318
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++) {
324 population_interface_ptr->spawning_biomass_ratio[i].final_value_m =
325 pop->spawning_biomass_ratio[i];
326 population_interface_ptr->spawning_biomass_ratio[i].uncertainty_m =
328 }
329
330 ss << "{\n \"name\": \"spawning_biomass_ratio\",\n";
331 ss << " \"id\":" << population_interface->spawning_biomass_ratio.id_m
332 << ",\n";
333 ss << " \"type\": \"vector\",\n";
334 ss << " \"dimensionality\": {\n";
335 ss << " \"header\": [" << "\"n_years\"" << "],\n";
336 ss << " \"dimensions\": [" << (population_interface->n_years.get() + 1)
337 << "]\n},\n";
338 ss << " \"values\": " << population_interface->spawning_biomass_ratio
339 << "\n\n";
340 ss << "},\n";
341
343 -999);
344 this->get_se_values("log_init_naa", this->se_values,
346 for (size_t i = 0; i < pop->log_init_naa.size(); i++) {
347 population_interface_ptr->log_init_naa[i].final_value_m =
348 pop->log_init_naa[i];
349 population_interface_ptr->log_init_naa[i].uncertainty_m =
351 }
352 ss << " {\n\"name\": \"log_init_naa\",\n";
353 ss << " \"id\":" << population_interface->log_init_naa.id_m << ",\n";
354 ss << " \"type\": \"vector\",\n";
355 ss << " \"dimensionality\": {\n";
356 ss << " \"header\": [" << "\"n_ages\"" << "],\n";
357 ss << " \"dimensions\": [" << population_interface->n_ages.get()
358 << "]\n},\n";
359
360 ss << " \"values\":" << population_interface->log_init_naa << "\n";
361 ss << "}],\n";
362
363 ss << " \"derived_quantities\": [\n";
364
365 std::map<std::string, fims::Vector<double>> dqs =
366 model_ptr->GetPopulationDerivedQuantities(
367 population_interface->get_id());
368
369 std::map<std::string, fims_popdy::DimensionInfo> dim_info =
370 model_ptr->GetPopulationDimensionInfo(population_interface->get_id());
372 << " ]}\n";
373 } else {
374 ss << "{\n";
375 ss << " \"name\": \"Population\",\n";
376
377 ss << " \"type\": \"population\",\n";
378 ss << " \"tag\": \"" << population_interface->get_id()
379 << " not found in Information.\",\n";
380 ss << " \"id\": " << population_interface->get_id() << ",\n";
381 ss << " \"recruitment_id\": " << population_interface->recruitment_id
382 << ",\n";
383 ss << " \"growth_id\": " << population_interface->growth_id << ",\n";
384 ss << " \"maturity_id\": " << population_interface->maturity_id << ",\n";
385 ss << " \"derived_quantities\": []}\n";
386 }
387
388 return ss.str();
389 }
390
397 std::map<std::string, fims::Vector<double>>::iterator it,
399 std::stringstream ss;
400 std::string name = (*it).first;
401 fims::Vector<double> &dq = (*it).second;
402 std::stringstream dim_entry;
403 bool has_se = false;
404 typename std::map<std::string, std::vector<double>>::iterator se_vals =
405 this->se_values.find(name);
406
407 if (se_vals != this->se_values.end()) {
408 has_se = true;
409 }
410 // gather dimension information
411 switch (dim_info.ndims) {
412 case 1:
413 dim_entry << "\"dimensionality\": {\n";
414 dim_entry << " \"header\": [\"" << dim_info.dim_names[0] << "\"],\n";
415 dim_entry << " \"dimensions\": [";
416 for (size_t i = 0; i < dim_info.dims.size(); ++i) {
417 if (i > 0) dim_entry << ", ";
418 dim_entry << dim_info.dims[i];
419 }
420 dim_entry << "]\n";
421 dim_entry << "}";
422 break;
423 case 2:
424 dim_entry << "\"dimensionality\": {\n";
425 dim_entry << " \"header\": [\"" << dim_info.dim_names[0] << "\", \""
426 << dim_info.dim_names[1] << "\"],\n";
427 dim_entry << " \"dimensions\": [";
428 for (size_t i = 0; i < dim_info.dims.size(); ++i) {
429 if (i > 0) dim_entry << ", ";
430 dim_entry << dim_info.dims[i];
431 }
432 dim_entry << "]\n";
433 dim_entry << "}";
434 break;
435 case 3:
436 dim_entry << "\"dimensionality\": {\n";
437 dim_entry << " \"header\": [\"" << dim_info.dim_names[0] << "\", \""
438 << dim_info.dim_names[1] << "\", \"" << dim_info.dim_names[2]
439 << "\"],\n";
440 dim_entry << " \"dimensions\": [";
441 for (size_t i = 0; i < dim_info.dims.size(); ++i) {
442 if (i > 0) dim_entry << ", ";
443 dim_entry << dim_info.dims[i];
444 }
445 dim_entry << "]\n";
446 dim_entry << "}";
447 break;
448 default:
449 dim_entry << "\"dimensionality\": {\n";
450 dim_entry << " \"header\": [],\n";
451 dim_entry << " \"dimensions\": []\n";
452 dim_entry << "}";
453 break;
454 }
455
456 // build JSON string
457 ss << "{\n";
458 ss << "\"name\":\"" << (*it).first << "\",\n";
459 ss << dim_entry.str() << ",\n";
460 ss << "\"value\":[";
461 ss << std::fixed << std::setprecision(10);
462 if (dq.size() > 0) {
463 for (size_t i = 0; i < dq.size() - 1; i++) {
464 if (dq[i] != dq[i]) // check for NaN
465 {
466 ss << "-999" << ", ";
467 } else {
468 ss << dq[i] << ", ";
469 }
470 }
471 if (dq[dq.size() - 1] != dq[dq.size() - 1]) // check for NaN
472 {
473 ss << "-999]" << ",\n";
474 } else {
475 ss << dq[dq.size() - 1] << "],\n";
476 }
477 } else {
478 ss << "],\n";
479 }
480 if (has_se) {
481 try {
482 std::vector<double> &se_vals_vector = (*se_vals).second;
483 if (se_vals_vector.size() < dq.size()) {
484 throw std::runtime_error(
485 "Standard error vector size is smaller than derived quantity "
486 "size for derived quantity " +
487 name);
488 }
489 std::vector<double> uncertainty_std(se_vals_vector.begin(),
490 se_vals_vector.begin() + dq.size());
491 std::vector<double> temp(se_vals_vector.begin() + dq.size(),
492 se_vals_vector.end());
495 ss << "\"uncertainty\": " << uncertainty << "\n";
496 } catch (const std::exception &e) {
497 throw std::runtime_error(
498 "Error processing uncertainty for derived quantity " + name + ": " +
499 e.what());
500 }
501 } else {
502 ss << "\"uncertainty\": [";
503 for (size_t i = 0; i < dq.size(); ++i) {
504 ss << "-999.0"; // Placeholder for uncertainty values
505 if (i < dq.size() - 1) {
506 ss << ", ";
507 }
508 }
509 ss << "]\n";
510 }
511 ss << "}";
512
513 return ss.str();
514 }
515
521 std::map<std::string, fims::Vector<double>> &dqs,
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;
527 end_it = dqs.end();
528 typename std::map<std::string, fims::Vector<double>>::iterator
530 second_to_last = dqs.end();
531 if (it != end_it) {
533 }
534
535 it = dqs.begin();
536 for (; it != second_to_last; ++it) {
537 dim_info_it = dim_info.find(it->first);
538 ss << this->derived_quantity_to_json(it, dim_info_it->second) << ",\n";
539 }
540
541 dim_info_it = dim_info.find(second_to_last->first);
542 if (dim_info_it != dim_info.end()) {
543 ss << this->derived_quantity_to_json(second_to_last, dim_info_it->second)
544 << "\n";
545 } else {
546 ss << "{}";
547 // Handle case where dimension info is not found
548 }
549 return ss.str();
550 }
551
556 std::stringstream ss;
557
558 if (!fleet_interface) {
559 FIMS_ERROR_LOG("Fleet with id " +
560 fims::to_string(fleet_interface->get_id()) +
561 " not found in live objects.");
562 return "{}"; // Return empty JSON
563 }
564
565 std::shared_ptr<fims_info::Information<double>> info =
567
569 model_it = info->models_map.find(this->get_id());
570 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
571 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
572 (*model_it).second);
573
575
576 fit = info->fleets.find(fleet_interface->get_id());
577
578 if (fit != info->fleets.end()) {
579 std::shared_ptr<fims_popdy::Fleet<double>> &fleet = (*fit).second;
580
581 ss << "{\n";
582 ss << " \"module_name\": \"Fleet\",\n";
583 ss << " \"fleet\": \"" << fleet_interface->name << "\",\n";
584 ss << " \"module_id\": " << fleet_interface->id << ",\n";
585 ss << " \"n_ages\": " << fleet_interface->n_ages.get() << ",\n";
586 ss << " \"n_years\": " << fleet_interface->n_years.get() << ",\n";
587 ss << " \"n_lengths\": " << fleet_interface->n_lengths.get() << ",\n";
588 ss << "\"data_ids\" : [\n";
589 ss << "{\"agecomp\": " << fleet_interface->GetObservedAgeCompDataID()
590 << "},\n";
591 ss << "{\"lengthcomp\": "
592 << fleet_interface->GetObservedLengthCompDataID() << "},\n";
593 ss << "{\"index\": " << fleet_interface->GetObservedIndexDataID()
594 << "},\n";
595 ss << "{\"landings\": " << fleet_interface->GetObservedLandingsDataID()
596 << "}\n";
597 ss << "],\n";
598 ss << "\"parameters\": [\n";
599 ss << "{\n";
600 fims::Vector<double> log_Fmort_uncertainty(fleet->log_Fmort.size(), -999);
601 this->get_se_values("log_Fmort", this->se_values, log_Fmort_uncertainty);
602 for (size_t i = 0; i < fleet_interface->log_Fmort.size(); i++) {
603 fleet_interface->log_Fmort[i].final_value_m = fleet->log_Fmort[i];
604 fleet_interface->log_Fmort[i].uncertainty_m = log_Fmort_uncertainty[i];
605 }
606
607 ss << " \"name\": \"log_Fmort\",\n";
608 ss << " \"id\":" << fleet_interface->log_Fmort.id_m << ",\n";
609 ss << " \"type\": \"vector\",\n";
610 ss << " \"dimensionality\": {\n";
611 ss << " \"header\": [\"" << "n_years" << "\"],\n";
612 ss << " \"dimensions\": [" << fleet_interface->n_years.get()
613 << "]\n},\n";
614 ss << " \"values\": " << fleet_interface->log_Fmort << "},\n";
615
616 ss << " {\n";
617 fims::Vector<double> log_q_uncertainty(fleet->log_q.size(), -999);
618 this->get_se_values("log_q", this->se_values, log_q_uncertainty);
619 for (size_t i = 0; i < fleet->log_q.size(); i++) {
620 fleet_interface->log_q[i].final_value_m = fleet->log_q[i];
621 fleet_interface->log_q[i].uncertainty_m = log_q_uncertainty[i];
622 }
623 ss << " \"name\": \"log_q\",\n";
624 ss << " \"id\":" << fleet_interface->log_q.id_m << ",\n";
625 ss << " \"type\": \"vector\",\n";
626 ss << " \"dimensionality\": {\n";
627 ss << " \"header\": [\"" << "na" << "\"],\n";
628 ss << " \"dimensions\": [" << fleet->log_q.size() << "]\n},\n";
629
630 ss << " \"values\": " << fleet_interface->log_q << "}\n";
631
632 ss << "], \"derived_quantities\": [";
633
634 std::map<std::string, fims::Vector<double>> dqs =
635 model_ptr->GetFleetDerivedQuantities(fleet_interface->get_id());
636 std::map<std::string, fims_popdy::DimensionInfo> dim_info =
637 model_ptr->GetFleetDimensionInfo(fleet_interface->get_id());
638 ss << this->derived_quantities_component_to_json(dqs, dim_info) << "]}\n";
639 } else {
640 ss << "{\n";
641 ss << " \"name\": \"Fleet\",\n";
642 ss << " \"type\": \"fleet\",\n";
643 ss << " \"tag\": \"" << fleet_interface->get_id()
644 << " not found in Information.\",\n";
645 ss << " \"derived_quantities\": []}\n";
646 }
647 return ss.str();
648 }
649
657 Rcpp::NumericVector get_fixed_parameters_vector() {
658 // base model
659 std::shared_ptr<fims_info::Information<double>> info0 =
661
662 Rcpp::NumericVector p;
663
664 for (size_t i = 0; i < info0->fixed_effects_parameters.size(); i++) {
665 p.push_back(*info0->fixed_effects_parameters[i]);
666 }
667
668 return p;
669 }
670
678 Rcpp::NumericVector get_random_parameters_vector() {
679 // base model
680 std::shared_ptr<fims_info::Information<double>> d0 =
682
683 Rcpp::NumericVector p;
684
685 for (size_t i = 0; i < d0->random_effects_parameters.size(); i++) {
686 p.push_back(*d0->random_effects_parameters[i]);
687 }
688
689 return p;
690 }
691
702 Rcpp::List get_report(bool do_sd_report = true) {
703 Rcpp::Environment base = Rcpp::Environment::base_env();
704 Rcpp::Function summary = base["summary"];
705
706 // Grab needed R functions
707 Rcpp::Environment TMB = Rcpp::Environment::namespace_env("TMB");
708 Rcpp::Function MakeADFun = TMB["MakeADFun"];
709 Rcpp::Function sdreport = TMB["sdreport"];
710 // Grab your helpers from R global environment
711 Rcpp::Environment global = Rcpp::Environment::global_env();
712 // Build parameters list
713 Rcpp::List parameters = Rcpp::List::create(
714 Rcpp::Named("p") = this->get_fixed_parameters_vector(),
715 Rcpp::Named("re") = this->get_random_parameters_vector());
716 // Call MakeADFun with map = NULL
717 Rcpp::List obj = MakeADFun(
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");
722 // Call obj$report()
723 Rcpp::Function report = obj["report"];
724 Rcpp::Function func = obj["fn"];
725 Rcpp::Function gradient = obj["gr"];
726 Rcpp::NumericVector grad = gradient(this->get_fixed_parameters_vector());
727 double maxgc = -999;
728 for (R_xlen_t i = 0; i < grad.size(); i++) {
729 if (std::fabs(grad[i]) > maxgc) {
730 maxgc = std::fabs(grad[i]);
731 }
732 }
733 double of_value =
734 Rcpp::as<double>(func(this->get_fixed_parameters_vector()));
735 Rcpp::List rep = report();
736
737 // Initialize variables for sdreport results
738 Rcpp::RObject sdr_summary;
739 Rcpp::NumericMatrix mat;
740 Rcpp::CharacterVector rownames;
741 Rcpp::CharacterVector colnames;
742 Rcpp::List grouped_out = Rcpp::List::create();
743 double first_est = 0.0;
744
745 // Only run sdreport if do_sd_report is true
746 if (do_sd_report) {
747 SEXP sdr = sdreport(obj);
748 sdr_summary = summary(sdr, "report");
749
750 mat = Rcpp::NumericMatrix(sdr_summary);
751 Rcpp::List dimnames = mat.attr("dimnames");
752 rownames = dimnames[0];
753 colnames = dimnames[1];
754
755 // ---- Group into map ----
756 std::map<std::string, std::vector<double>> grouped;
757 int nrow = mat.nrow();
758 for (int i = 0; i < nrow; i++) {
759 std::string key = Rcpp::as<std::string>(rownames[i]);
760 double val = mat(i, 1); // col 1 = "Std. Error"
761 grouped[key].push_back(val);
762 }
763
764 // ---- Convert map -> R list ----
765 for (auto const &kv : grouped) {
766 grouped_out[kv.first] = Rcpp::wrap(kv.second);
767 }
768
769 // Example: grab "Estimate" for first row
770 if (nrow > 0) {
771 first_est = mat(0, 0);
772 }
773 } else {
774 // Return empty objects when do_sd_report is false
776 mat = Rcpp::NumericMatrix(0, 0);
777 rownames = Rcpp::CharacterVector(0);
778 colnames = Rcpp::CharacterVector(0);
779 grouped_out = Rcpp::List::create();
780 // first_est is already initialized to 0.0
781 }
782
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,
789 Rcpp::Named("first_est") = first_est,
790 Rcpp::Named("rownames") = rownames, Rcpp::Named("colnames") = colnames,
791 Rcpp::Named("grouped_se") = grouped_out);
792 }
796 virtual std::string to_json(bool do_sd_report = true) {
797 Rcpp::List report = get_report(do_sd_report);
798 Rcpp::List grouped_out = report["grouped_se"];
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"]);
802
804 for (R_xlen_t i = 0; i < grad.size(); i++) {
805 gradient[i] = grad[i];
806 }
807 // Assume grouped_out is an Rcpp::List
808 std::map<std::string, std::vector<double>> grouped_cpp;
809 if (grouped_out.size() > 0) {
810 Rcpp::CharacterVector names = grouped_out.names();
811 for (R_xlen_t i = 0; i < grouped_out.size(); i++) {
812 std::string key = Rcpp::as<std::string>(names[i]);
813 Rcpp::NumericVector vec =
814 grouped_out[i]; // each element is a numeric vector
815 std::vector<double> vec_std(vec.size());
816 for (R_xlen_t j = 0; j < vec.size(); j++) {
817 vec_std[j] = vec[j];
818 }
819 grouped_cpp[key] = vec_std;
820 }
821 }
822 this->se_values = grouped_cpp;
823
824 std::set<uint32_t> recruitment_ids;
825 std::set<uint32_t> growth_ids;
826 std::set<uint32_t> maturity_ids;
827 std::set<uint32_t> selectivity_ids;
828 std::set<uint32_t> fleet_ids;
829 // gather sub-module info from population and fleets
830 typename std::set<uint32_t>::iterator module_id_it; // generic
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++) {
835 std::shared_ptr<PopulationInterface> population_interface =
836 std::dynamic_pointer_cast<PopulationInterface>(
839 recruitment_ids.insert(population_interface->recruitment_id.get());
840 growth_ids.insert(population_interface->growth_id.get());
841 maturity_ids.insert(population_interface->maturity_id.get());
842
843 for (fids = population_interface->fleet_ids->begin();
844 fids != population_interface->fleet_ids->end(); fids++) {
845 fleet_ids.insert(*fids);
846 }
847 }
848 }
849
850 for (fids = fleet_ids.begin(); fids != fleet_ids.end(); fids++) {
851 std::shared_ptr<FleetInterface> fleet_interface =
852 std::dynamic_pointer_cast<FleetInterface>(
854 if (fleet_interface) {
855 selectivity_ids.insert(fleet_interface->GetSelectivityID());
856 }
857 }
858
859 std::shared_ptr<fims_info::Information<double>> info =
861
862 std::shared_ptr<fims_popdy::CatchAtAge<double>> model =
863 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
864 info->models_map[this->get_id()]);
865
866 std::shared_ptr<fims_model::Model<double>> model_internal =
868
869#ifdef TMB_MODEL
870 model->do_reporting = false;
871#endif
872
873 double value = model_internal->Evaluate();
874
875 std::stringstream ss;
876
877 ss.str("");
878
879 ss << "{\n";
880 ss << " \"name\": \"CatchAtAge\",\n";
881 ss << " \"type\": \"model\",\n";
882 ss << " \"estimation_framework\": ";
883#ifdef TMB_MODEL
884 ss << "\"Template_Model_Builder (TMB)\",";
885#else
886 ss << "\"FIMS\",";
887#endif
888 ss << " \"id\": " << this->get_id() << ",\n";
889 ss << " \"objective_function_value\": " << value << ",\n";
890 ss << "\"growth\":[\n";
891 for (module_id_it = growth_ids.begin(); module_id_it != growth_ids.end();
892 module_id_it++) {
893 std::shared_ptr<GrowthInterfaceBase> growth_interface =
895
896 if (growth_interface != NULL) {
897 growth_interface->set_uncertainty(this->se_values);
898 growth_interface->finalize();
899 ss << growth_interface->to_json();
900 if (std::next(module_id_it) != growth_ids.end()) {
901 ss << ", ";
902 }
903 }
904 }
905
906 ss << "],\n";
907
908 ss << "\"recruitment\": [\n";
909 for (module_id_it = recruitment_ids.begin();
911 std::shared_ptr<RecruitmentInterfaceBase> recruitment_interface =
914 recruitment_interface->set_uncertainty(this->se_values);
915 recruitment_interface->finalize();
916 ss << recruitment_interface->to_json();
917 if (std::next(module_id_it) != recruitment_ids.end()) {
918 ss << ", ";
919 }
920 }
921 }
922 ss << "],\n";
923
924 ss << "\"maturity\": [\n";
925 for (module_id_it = maturity_ids.begin();
927 std::shared_ptr<MaturityInterfaceBase> maturity_interface =
929 if (maturity_interface) {
930 maturity_interface->set_uncertainty(this->se_values);
931 maturity_interface->finalize();
932 ss << maturity_interface->to_json();
933 if (std::next(module_id_it) != maturity_ids.end()) {
934 ss << ", ";
935 }
936 }
937 }
938 ss << "],\n";
939
940 ss << "\"selectivity\": [\n";
941 for (module_id_it = selectivity_ids.begin();
943 std::shared_ptr<SelectivityInterfaceBase> selectivity_interface =
946 selectivity_interface->set_uncertainty(this->se_values);
947 selectivity_interface->finalize();
948 ss << selectivity_interface->to_json();
949 if (std::next(module_id_it) != selectivity_ids.end()) {
950 ss << ", ";
951 }
952 }
953 }
954 ss << "],\n";
955
956 ss << " \"population_ids\": [";
957 for (pit = this->population_ids->begin();
958 pit != this->population_ids->end(); pit++) {
959 ss << *pit;
960 if (std::next(pit) != this->population_ids->end()) {
961 ss << ", ";
962 }
963 }
964 ss << "],\n";
965 ss << " \"fleet_ids\": [";
966
967 for (fids = fleet_ids.begin(); fids != fleet_ids.end(); fids++) {
968 ss << *fids;
969 if (std::next(fids) != fleet_ids.end()) {
970 ss << ", ";
971 }
972 }
973 ss << "],\n";
974 ss << "\"populations\": [\n";
975 typename std::set<uint32_t>::iterator pop_it;
976 typename std::set<uint32_t>::iterator pop_end_it;
977 pop_end_it = this->population_ids->end();
978 typename std::set<uint32_t>::iterator pop_second_to_last_it;
979 if (pop_end_it != this->population_ids->begin()) {
981 } else {
983 }
984 for (pop_it = this->population_ids->begin();
986 std::shared_ptr<PopulationInterface> population_interface =
987 std::dynamic_pointer_cast<PopulationInterface>(
990 std::set<uint32_t>::iterator fids;
991 for (fids = population_interface->fleet_ids->begin();
992 fids != population_interface->fleet_ids->end(); fids++) {
993 fleet_ids.insert(*fids);
994 }
995 population_interface->finalize();
996 ss << this->population_to_json(population_interface.get()) << ",";
997 } else {
998 FIMS_ERROR_LOG("Population with id " + fims::to_string(*pop_it) +
999 " not found in live objects.");
1000 ss << "{}"; // Return empty JSON for this population
1001 }
1002 }
1003
1004 std::shared_ptr<PopulationInterface> population_interface =
1005 std::dynamic_pointer_cast<PopulationInterface>(
1008 std::set<uint32_t>::iterator fids;
1009 for (fids = population_interface->fleet_ids->begin();
1010 fids != population_interface->fleet_ids->end(); fids++) {
1011 fleet_ids.insert(*fids);
1012 }
1013 ss << this->population_to_json(population_interface.get());
1014 } else {
1015 FIMS_ERROR_LOG("Population with id " + fims::to_string(*pop_it) +
1016 " not found in live objects.");
1017 ss << "{}"; // Return empty JSON for this population
1018 }
1019
1020 ss << "]";
1021 ss << ",\n";
1022 ss << "\"fleets\": [\n";
1023
1024 typename std::set<uint32_t>::iterator fleet_it;
1025 typename std::set<uint32_t>::iterator fleet_end_it;
1026 fleet_end_it = fleet_ids.end();
1027 typename std::set<uint32_t>::iterator fleet_second_to_last_it;
1028
1029 if (fleet_end_it != fleet_ids.begin()) {
1031 }
1032 for (fleet_it = fleet_ids.begin(); fleet_it != fleet_second_to_last_it;
1033 fleet_it++) {
1034 std::shared_ptr<FleetInterface> fleet_interface =
1035 std::dynamic_pointer_cast<FleetInterface>(
1037 if (fleet_interface) {
1038 fleet_interface->finalize();
1039 ss << this->fleet_to_json(fleet_interface.get()) << ",";
1040 } else {
1041 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*fleet_it) +
1042 " not found in live objects.");
1043 ss << "{}"; // Return empty JSON for this fleet
1044 }
1045 }
1046 std::shared_ptr<FleetInterface> fleet_interface =
1047 std::dynamic_pointer_cast<FleetInterface>(
1049 if (fleet_interface) {
1050 ss << this->fleet_to_json(fleet_interface.get());
1051 } else {
1052 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*fleet_it) +
1053 " not found in live objects.");
1054 ss << "{}"; // Return empty JSON for this fleet
1055 }
1056
1057 ss << "],\n";
1058
1059 ss << "\"density_components\": [\n";
1060
1061 typename std::map<
1062 uint32_t, std::shared_ptr<DistributionsInterfaceBase>>::iterator dit;
1065 std::shared_ptr<DistributionsInterfaceBase> dist_interface =
1066 (*dit).second;
1067 if (dist_interface) {
1068 dist_interface->finalize();
1069 ss << dist_interface->to_json();
1070 if (std::next(dit) != DistributionsInterfaceBase::live_objects.end()) {
1071 ss << ",\n";
1072 }
1073 }
1074 }
1075 ss << "\n],\n";
1076 ss << "\"data\": [\n";
1077 typename std::map<uint32_t, std::shared_ptr<DataInterfaceBase>>::iterator
1078 d_it;
1081 std::shared_ptr<DataInterfaceBase> data_interface = (*d_it).second;
1082 if (data_interface) {
1083 data_interface->finalize();
1084 ss << data_interface->to_json();
1085 if (std::next(d_it) != DataInterfaceBase::live_objects.end()) {
1086 ss << ",\n";
1087 }
1088 }
1089 }
1090 ss << "\n],\n";
1091 // add log
1092 ss << " \"log\": {\n";
1093 ss << "\"info\": " << fims::FIMSLog::fims_log->get_info() << ","
1094 << "\"warnings\": " << fims::FIMSLog::fims_log->get_warnings() << ","
1095 << "\"errors\": " << fims::FIMSLog::fims_log->get_errors() << "}}";
1096#ifdef TMB_MODEL
1097 model->do_reporting = true;
1098#endif
1100 }
1101
1108 double sum(const std::valarray<double> &v) {
1109 double sum = 0.0;
1110 for (size_t i = 0; i < v.size(); i++) {
1111 sum += v[i];
1112 }
1113 return sum;
1114 }
1115
1122 double sum(const std::vector<double> &v) {
1123 double sum = 0.0;
1124 for (size_t i = 0; i < v.size(); i++) {
1125 sum += v[i];
1126 }
1127 return sum;
1128 }
1129
1137 double min(const std::valarray<double> &v) {
1138 double min = v[0];
1139 for (size_t i = 1; i < v.size(); i++) {
1140 if (v[i] < min) {
1141 min = v[i];
1142 }
1143 }
1144 return min;
1145 }
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++) {
1157 result[i] = std::fabs(v[i]);
1158 }
1159 return result;
1160 }
1161
1162#ifdef TMB_MODEL
1163
1164 template <typename Type>
1166 std::shared_ptr<fims_info::Information<Type>> info =
1168
1169 std::shared_ptr<fims_popdy::CatchAtAge<Type>> model =
1170 std::make_shared<fims_popdy::CatchAtAge<Type>>();
1171
1172 population_id_iterator it;
1173
1174 for (it = this->population_ids->begin(); it != this->population_ids->end();
1175 ++it) {
1176 model->AddPopulation((*it));
1177 }
1178
1179 std::set<uint32_t> fleet_ids; // all fleets in the model
1180 typedef typename std::set<uint32_t>::iterator fleet_ids_iterator;
1181
1182 // add to Information
1183 info->models_map[this->get_id()] = model;
1184
1185 for (it = this->population_ids->begin(); it != this->population_ids->end();
1186 ++it) {
1189 throw std::runtime_error("Population ID " + std::to_string(*it) +
1190 " not found in live_objects");
1191 }
1192 auto population =
1193 std::dynamic_pointer_cast<PopulationInterface>(it2->second);
1194 model->InitializePopulationDerivedQuantities(population->id);
1195 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1196 model->GetPopulationDerivedQuantities(population->id);
1197
1198 std::map<std::string, fims_popdy::DimensionInfo>
1200 model->GetPopulationDimensionInfo(population->id);
1201
1202 std::stringstream ss;
1203
1204 derived_quantities["total_landings_weight"] =
1205 fims::Vector<Type>(population->n_years.get());
1206
1207 derived_quantities_dim_info["total_landings_weight"] =
1209 "total_landings_weight",
1210 fims::Vector<int>{(int)population->n_years.get()},
1211 fims::Vector<std::string>{"n_years"});
1212
1213 derived_quantities["total_landings_numbers"] =
1214 fims::Vector<Type>(population->n_years.get());
1215
1216 derived_quantities_dim_info["total_landings_numbers"] =
1218 "total_landings_numbers",
1219 fims::Vector<int>{population->n_years.get()},
1220 fims::Vector<std::string>{"n_years"});
1221
1222 derived_quantities["mortality_F"] = fims::Vector<Type>(
1223 population->n_years.get() * population->n_ages.get());
1225 "mortality_F",
1226 fims::Vector<int>{population->n_years.get(),
1227 population->n_ages.get()},
1228 fims::Vector<std::string>{"n_years", "n_ages"});
1229
1230 derived_quantities["mortality_M"] = fims::Vector<Type>(
1231 population->n_years.get() * population->n_ages.get());
1233 "mortality_M",
1234 fims::Vector<int>{population->n_years.get(),
1235 population->n_ages.get()},
1236 fims::Vector<std::string>{"n_years", "n_ages"});
1237
1238 derived_quantities["mortality_Z"] = fims::Vector<Type>(
1239 population->n_years.get() * population->n_ages.get());
1241 "mortality_Z",
1242 fims::Vector<int>{population->n_years.get(),
1243 population->n_ages.get()},
1244 fims::Vector<std::string>{"n_years", "n_ages"});
1245
1246 derived_quantities["numbers_at_age"] = fims::Vector<Type>(
1247 (population->n_years.get() + 1) * population->n_ages.get());
1249 "numbers_at_age",
1250 fims::Vector<int>{(population->n_years.get() + 1),
1251 population->n_ages.get()},
1252 fims::Vector<std::string>{"n_years+1", "n_ages"});
1253
1254 derived_quantities["unfished_numbers_at_age"] = fims::Vector<Type>(
1255 (population->n_years.get() + 1) * population->n_ages.get());
1256 derived_quantities_dim_info["unfished_numbers_at_age"] =
1258 "unfished_numbers_at_age",
1259 fims::Vector<int>{(population->n_years.get() + 1),
1260 population->n_ages.get()},
1261 fims::Vector<std::string>{"n_years+1", "n_ages"});
1262
1263 derived_quantities["biomass"] =
1264 fims::Vector<Type>((population->n_years.get() + 1));
1266 "biomass", fims::Vector<int>{(population->n_years.get() + 1)},
1267 fims::Vector<std::string>{"n_years+1"});
1268
1269 derived_quantities["spawning_biomass"] =
1270 fims::Vector<Type>((population->n_years.get() + 1));
1271 derived_quantities_dim_info["spawning_biomass"] =
1273 "spawning_biomass",
1274 fims::Vector<int>{(population->n_years.get() + 1)},
1275 fims::Vector<std::string>{"n_years+1"});
1276
1277 derived_quantities["unfished_biomass"] =
1278 fims::Vector<Type>((population->n_years.get() + 1));
1279 derived_quantities_dim_info["unfished_biomass"] =
1281 "unfished_biomass",
1282 fims::Vector<int>{(population->n_years.get() + 1)},
1283 fims::Vector<std::string>{"n_years+1"});
1284
1285 derived_quantities["unfished_spawning_biomass"] =
1286 fims::Vector<Type>((population->n_years.get() + 1));
1287 derived_quantities_dim_info["unfished_spawning_biomass"] =
1289 "unfished_spawning_biomass",
1290 fims::Vector<int>{(population->n_years.get() + 1)},
1291 fims::Vector<std::string>{"n_years+1"});
1292
1293 derived_quantities["proportion_mature_at_age"] = fims::Vector<Type>(
1294 (population->n_years.get() + 1) * population->n_ages.get());
1295 derived_quantities_dim_info["proportion_mature_at_age"] =
1297 "proportion_mature_at_age",
1298 fims::Vector<int>{(population->n_years.get() + 1),
1299 population->n_ages.get()},
1300 fims::Vector<std::string>{"n_years+1", "n_ages"});
1301
1302 derived_quantities["expected_recruitment"] =
1303 fims::Vector<Type>((population->n_years.get() + 1));
1304 derived_quantities_dim_info["expected_recruitment"] =
1306 "expected_recruitment",
1307 fims::Vector<int>{(population->n_years.get() + 1)},
1308 fims::Vector<std::string>{"n_years+1"});
1309
1310 derived_quantities["sum_selectivity"] = fims::Vector<Type>(
1311 population->n_years.get() * population->n_ages.get());
1312 derived_quantities_dim_info["sum_selectivity"] =
1314 "sum_selectivity",
1315 fims::Vector<int>{population->n_years.get(),
1316 population->n_ages.get()},
1317 fims::Vector<std::string>{"n_years", "n_ages"});
1318
1319 // replace elements in the variable map
1320
1321 for (fleet_ids_iterator fit = population->fleet_ids->begin();
1322 fit != population->fleet_ids->end(); ++fit) {
1323 fleet_ids.insert(*fit);
1324 }
1325 }
1326
1327 for (fleet_ids_iterator it = fleet_ids.begin(); it != fleet_ids.end();
1328 ++it) {
1329 std::shared_ptr<FleetInterface> fleet_interface =
1330 std::dynamic_pointer_cast<FleetInterface>(
1332 model->InitializeFleetDerivedQuantities(fleet_interface->id);
1333 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1334 model->GetFleetDerivedQuantities(fleet_interface->id);
1335
1336 std::map<std::string, fims_popdy::DimensionInfo>
1338 model->GetFleetDimensionInfo(fleet_interface->id);
1339
1340 // initialize derive quantities
1341 // landings
1342 derived_quantities["landings_numbers_at_age"] = fims::Vector<Type>(
1343 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1344 derived_quantities_dim_info["landings_numbers_at_age"] =
1346 "landings_numbers_at_age",
1347 fims::Vector<int>{(fleet_interface->n_years.get()),
1348 fleet_interface->n_ages.get()},
1349 fims::Vector<std::string>{"n_years", "n_ages"});
1350
1351 derived_quantities["landings_weight_at_age"] = fims::Vector<Type>(
1352 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1353 derived_quantities_dim_info["landings_weight_at_age"] =
1355 "landings_weight_at_age",
1356 fims::Vector<int>{(fleet_interface->n_years.get()),
1357 fleet_interface->n_ages.get()},
1358 fims::Vector<std::string>{"n_years", "n_ages"});
1359
1360 derived_quantities["landings_numbers_at_length"] = fims::Vector<Type>(
1361 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1362 derived_quantities_dim_info["landings_numbers_at_length"] =
1364 "landings_numbers_at_length",
1365 fims::Vector<int>{(fleet_interface->n_years.get()),
1366 fleet_interface->n_lengths.get()},
1367 fims::Vector<std::string>{"n_years", "n_lengths"});
1368
1369 derived_quantities["landings_weight"] =
1370 fims::Vector<Type>(fleet_interface->n_years.get());
1371 derived_quantities_dim_info["landings_weight"] =
1373 "landings_weight",
1374 fims::Vector<int>{(fleet_interface->n_years.get())},
1375 fims::Vector<std::string>{"n_years"});
1376
1377 derived_quantities["landings_numbers"] =
1378 fims::Vector<Type>(fleet_interface->n_years.get());
1379 derived_quantities_dim_info["landings_numbers"] =
1381 "landings_numbers",
1382 fims::Vector<int>{(fleet_interface->n_years.get())},
1383 fims::Vector<std::string>{"n_years"});
1384
1385 derived_quantities["landings_expected"] =
1386 fims::Vector<Type>(fleet_interface->n_years.get());
1387 derived_quantities_dim_info["landings_expected"] =
1389 "landings_expected",
1390 fims::Vector<int>{(fleet_interface->n_years.get())},
1391 fims::Vector<std::string>{"n_years"});
1392
1393 derived_quantities["log_landings_expected"] =
1394 fims::Vector<Type>(fleet_interface->n_years.get());
1395 derived_quantities_dim_info["log_landings_expected"] =
1397 "log_landings_expected",
1398 fims::Vector<int>{(fleet_interface->n_years.get())},
1399 fims::Vector<std::string>{"n_years"});
1400
1401 derived_quantities["agecomp_proportion"] = fims::Vector<Type>(
1402 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1403 derived_quantities_dim_info["agecomp_proportion"] =
1405 "agecomp_proportion",
1406 fims::Vector<int>{(fleet_interface->n_years.get()),
1407 fleet_interface->n_ages.get()},
1408 fims::Vector<std::string>{"n_years", "n_ages"});
1409
1410 derived_quantities["lengthcomp_proportion"] = fims::Vector<Type>(
1411 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1412 derived_quantities_dim_info["lengthcomp_proportion"] =
1414 "lengthcomp_proportion",
1415 fims::Vector<int>{(fleet_interface->n_years.get()),
1416 fleet_interface->n_lengths.get()},
1417 fims::Vector<std::string>{"n_years", "n_lengths"});
1418
1419 // index
1420 derived_quantities["index_numbers_at_age"] = fims::Vector<Type>(
1421 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1422 derived_quantities_dim_info["index_numbers_at_age"] =
1424 "index_numbers_at_age",
1425 fims::Vector<int>{(fleet_interface->n_years.get()),
1426 fleet_interface->n_ages.get()},
1427 fims::Vector<std::string>{"n_years", "n_ages"});
1428
1429 derived_quantities["index_weight_at_age"] = fims::Vector<Type>(
1430 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1431 derived_quantities_dim_info["index_weight_at_age"] =
1433 "index_weight_at_age",
1434 fims::Vector<int>{(fleet_interface->n_years.get()),
1435 fleet_interface->n_ages.get()},
1436 fims::Vector<std::string>{"n_years", "n_ages"});
1437
1438 derived_quantities["index_weight_at_age"] = fims::Vector<Type>(
1439 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1440 derived_quantities_dim_info["index_weight_at_age"] =
1442 "index_weight_at_age",
1443 fims::Vector<int>{(fleet_interface->n_years.get()),
1444 fleet_interface->n_ages.get()},
1445 fims::Vector<std::string>{"n_years", "n_ages"});
1446
1447 derived_quantities["index_numbers_at_length"] = fims::Vector<Type>(
1448 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1449 derived_quantities_dim_info["index_numbers_at_length"] =
1451 "index_numbers_at_length",
1452 fims::Vector<int>{(fleet_interface->n_years.get()),
1453 fleet_interface->n_lengths.get()},
1454 fims::Vector<std::string>{"n_years", "n_lengths"});
1455 derived_quantities["index_weight"] =
1456 fims::Vector<Type>(fleet_interface->n_years.get());
1458 "index_weight", fims::Vector<int>{(fleet_interface->n_years.get())},
1459 fims::Vector<std::string>{"n_years"});
1460
1461 derived_quantities["index_numbers"] =
1462 fims::Vector<Type>(fleet_interface->n_years.get());
1464 "index_numbers", fims::Vector<int>{(fleet_interface->n_years.get())},
1465 fims::Vector<std::string>{"n_years"});
1466
1467 derived_quantities["index_expected"] =
1468 fims::Vector<Type>(fleet_interface->n_years.get());
1470 "index_expected", fims::Vector<int>{(fleet_interface->n_years.get())},
1471 fims::Vector<std::string>{"n_years"});
1472
1473 derived_quantities["log_index_expected"] =
1474 fims::Vector<Type>(fleet_interface->n_years.get());
1475 derived_quantities_dim_info["log_index_expected"] =
1477 "log_index_expected",
1478 fims::Vector<int>{(fleet_interface->n_years.get())},
1479 fims::Vector<std::string>{"n_years"});
1480
1481 derived_quantities["catch_index"] =
1482 fims::Vector<Type>(fleet_interface->n_years.get());
1484 "catch_index", fims::Vector<int>{(fleet_interface->n_years.get())},
1485 fims::Vector<std::string>{"n_years"});
1486
1487 derived_quantities["agecomp_expected"] = fims::Vector<Type>(
1488 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1489 derived_quantities_dim_info["agecomp_expected"] =
1491 "agecomp_expected",
1492 fims::Vector<int>{(fleet_interface->n_years.get()),
1493 (fleet_interface->n_ages.get())},
1494 fims::Vector<std::string>{"n_years", "n_ages"});
1495
1496 derived_quantities["lengthcomp_expected"] = fims::Vector<Type>(
1497 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1498 derived_quantities_dim_info["lengthcomp_expected"] =
1500 "lengthcomp_expected",
1501 fims::Vector<int>{(fleet_interface->n_years.get()),
1502 (fleet_interface->n_lengths.get())},
1503 fims::Vector<std::string>{"n_years", "n_lengths"});
1504
1505 // replace elements in the variable map
1506 info->variable_map[fleet_interface->log_landings_expected.id_m] =
1507 &(derived_quantities["log_landings_expected"]);
1508 info->variable_map[fleet_interface->log_index_expected.id_m] =
1509 &(derived_quantities["log_index_expected"]);
1510 info->variable_map[fleet_interface->agecomp_expected.id_m] =
1511 &(derived_quantities["agecomp_expected"]);
1512 info->variable_map[fleet_interface->agecomp_proportion.id_m] =
1513 &(derived_quantities["agecomp_proportion"]);
1514 info->variable_map[fleet_interface->lengthcomp_expected.id_m] =
1515 &(derived_quantities["lengthcomp_expected"]);
1516 info->variable_map[fleet_interface->lengthcomp_proportion.id_m] =
1517 &(derived_quantities["lengthcomp_proportion"]);
1518 }
1519
1520 return true;
1521 }
1522
1523 virtual bool add_to_fims_tmb() {
1525#ifdef TMBAD_FRAMEWORK
1527#else
1532#endif
1533 return true;
1534 }
1535
1536#endif
1537};
1538
1539#endif
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
std::unordered_map< uint32_t, std::shared_ptr< fims_popdy::FisheryModelBase< Type > > > models_map
Definition information.hpp:136
std::map< uint32_t, std::shared_ptr< fims_popdy::Fleet< Type > > >::iterator fleet_iterator
Definition information.hpp:110
std::map< uint32_t, std::shared_ptr< fims_popdy::Population< Type > > >::iterator population_iterator
Definition information.hpp:120
std::unordered_map< uint32_t, std::shared_ptr< fims_popdy::FisheryModelBase< Type > > >::iterator model_map_iterator
Definition information.hpp:140
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:238
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