FIMS  v0.9.3
Loading...
Searching...
No Matches
information.hpp
Go to the documentation of this file.
1
11#ifndef FIMS_COMMON_INFORMATION_HPP
12#define FIMS_COMMON_INFORMATION_HPP
13
14#include <algorithm>
15#include <map>
16#include <memory>
17#include <vector>
18
19#include "../distributions/distributions.hpp"
20#include "../models/functors/fishery_model_base.hpp"
21#include "../population_dynamics/fleet/fleet.hpp"
22#include "../population_dynamics/growth/growth.hpp"
23#include "../population_dynamics/population/population.hpp"
24#include "../population_dynamics/recruitment/recruitment.hpp"
25#include "../population_dynamics/selectivity/selectivity.hpp"
26#include "fims_vector.hpp"
27#include "model_object.hpp"
28
29namespace fims_info {
30
35template <typename Type>
37 public:
38 size_t n_years = 0;
39 size_t n_ages = 0;
41 static std::shared_ptr<Information<Type>>
43 std::vector<Type*> parameters;
44 std::vector<Type*>
46 std::vector<Type*>
48 std::vector<std::string> parameter_names;
50 std::vector<std::string>
54 // data objects
55 std::map<uint32_t, std::shared_ptr<fims_data_object::DataObject<Type>>>
57 typedef typename std::map<
58 uint32_t, std::shared_ptr<fims_data_object::DataObject<Type>>>::iterator
61 // life history modules
62 std::map<uint32_t, std::shared_ptr<fims_popdy::RecruitmentBase<Type>>>
65 typedef typename std::map<
66 uint32_t, std::shared_ptr<fims_popdy::RecruitmentBase<Type>>>::iterator
70 std::map<uint32_t, std::shared_ptr<fims_popdy::RecruitmentBase<Type>>>
73 typedef typename std::map<
74 uint32_t, std::shared_ptr<fims_popdy::RecruitmentBase<Type>>>::iterator
78 std::map<uint32_t, std::shared_ptr<fims_popdy::SelectivityBase<Type>>>
81 typedef typename std::map<
82 uint32_t, std::shared_ptr<fims_popdy::SelectivityBase<Type>>>::iterator
86 std::map<uint32_t, std::shared_ptr<fims_popdy::GrowthBase<Type>>>
89 typedef
90 typename std::map<uint32_t,
91 std::shared_ptr<fims_popdy::GrowthBase<Type>>>::iterator
95 std::map<uint32_t, std::shared_ptr<fims_popdy::MaturityBase<Type>>>
98 typedef typename std::map<
99 uint32_t, std::shared_ptr<fims_popdy::MaturityBase<Type>>>::iterator
103 // fleet modules
104 std::map<uint32_t, std::shared_ptr<fims_popdy::Fleet<Type>>>
107 typedef typename std::map<uint32_t,
108 std::shared_ptr<fims_popdy::Fleet<Type>>>::iterator
112 // populations
113 std::map<uint32_t, std::shared_ptr<fims_popdy::Population<Type>>>
116 typedef
117 typename std::map<uint32_t,
118 std::shared_ptr<fims_popdy::Population<Type>>>::iterator
122 // distributions
123 std::map<uint32_t,
124 std::shared_ptr<fims_distributions::DensityComponentBase<Type>>>
127 typedef typename std::map<
128 uint32_t,
129 std::shared_ptr<fims_distributions::DensityComponentBase<Type>>>::iterator
133 std::unordered_map<uint32_t,
134 std::shared_ptr<fims_popdy::FisheryModelBase<Type>>>
137 typedef typename std::unordered_map<
138 uint32_t, std::shared_ptr<fims_popdy::FisheryModelBase<Type>>>::iterator
141 std::unordered_map<uint32_t, fims::Vector<Type>*>
144 typedef typename std::unordered_map<uint32_t, fims::Vector<Type>*>::iterator
147 Information() {}
148
149 virtual ~Information() {}
150
155 void Clear() {
156 this->data_objects.clear();
157 this->populations.clear();
158 this->fixed_effects_parameters.clear();
159 this->fleets.clear();
160 this->growth_models.clear();
161 this->maturity_models.clear();
162 this->parameter_names.clear();
163 this->parameters.clear();
164 this->random_effects_names.clear();
165 this->random_effects_parameters.clear();
166 this->selectivity_models.clear();
167 this->models_map.clear();
168 this->n_years = 0;
169 this->n_ages = 0;
170
172 it != recruitment_models.end(); ++it) {
173 std::shared_ptr<fims_popdy::RecruitmentBase<Type>> recruitment =
174 (*it).second;
175 if (recruitment->process) {
176 recruitment->process.reset();
177 }
178 if (recruitment->recruitment) {
179 recruitment->recruitment.reset();
180 }
181 }
183 it != recruitment_process_models.end(); ++it) {
184 std::shared_ptr<fims_popdy::RecruitmentBase<Type>> recruitment =
185 (*it).second;
186 if (recruitment->process) {
187 recruitment->process.reset();
188 }
189 if (recruitment->recruitment) {
190 recruitment->recruitment.reset();
191 }
192 }
193 this->recruitment_models.clear();
194 this->recruitment_process_models.clear();
195
197 it != density_components.end(); ++it) {
198 std::shared_ptr<fims_distributions::DensityComponentBase<Type>> d =
199 (*it).second;
200 if ((d->priors)[0]) {
201 d->priors.clear();
202 }
203 if (d->data_observed_values) {
204 d->data_observed_values.reset();
205 }
206 if (d->data_expected_values) {
207 d->data_expected_values->clear();
208 }
209 if (d->re) {
210 d->re->clear();
211 }
212 if (d->re_expected_values) {
213 d->re_expected_values->clear();
214 }
215 }
216 this->density_components.clear();
217 }
218
228 std::string State() {
229 std::stringstream ss;
230 ss << "Information object State:\n";
231 ss << "data_objects: " << this->data_objects.clear();
232 ss << "populations: " << this->populations.size() << std::endl;
233 ss << "fixed_effects_parameters: " << this->fixed_effects_parameters.size()
234 << std::endl;
235 ss << "fleets: " << this->fleets.size() << std::endl;
236 ss << "growth_models: " << this->growth_models.size() << std::endl;
237 ss << "maturity_models: " << this->maturity_models.size() << std::endl;
238 ss << "parameter_names: " << this->parameter_names.size() << std::endl;
239 ss << "parameters: " << this->parameters.size() << std::endl;
240 ss << "random_effects_names: " << this->random_effects_names.size()
241 << std::endl;
242 ss << "random_effects_parameters: "
243 << this->random_effects_parameters.size() << std::endl;
244 ss << "recruitment_models: " << this->recruitment_models.size()
245 << std::endl;
246 ss << "recruitment_process_models: "
247 << this->recruitment_process_models.size() << std::endl;
248 ss << "selectivity_models: " << this->selectivity_models.size()
249 << std::endl;
250 ss << "models_map: " << this->models_map.size() << std::endl;
251 ss << "n_years: " << this->n_years << std::endl;
252 ss << "n_ages: " << this->n_ages << std::endl;
253 ss << "density_components: " << this->density_components.size()
254 << std::endl;
255 return ss.str();
256 }
257
263 static std::shared_ptr<Information<Type>> GetInstance() {
266 std::make_shared<fims_info::Information<Type>>();
267 }
269 }
270
276 void RegisterParameter(Type& p) {
277 this->fixed_effects_parameters.push_back(&p);
278 }
279
285 void RegisterRandomEffect(Type& re) {
286 this->random_effects_parameters.push_back(&re);
287 }
288
294 void RegisterParameterName(std::string p_name) {
295 this->parameter_names.push_back(p_name);
296 }
297
303 void RegisterRandomEffectName(std::string re_name) {
304 this->random_effects_names.push_back(re_name);
305 }
306
311 void SetupPriors() {
313 it != density_components.end(); ++it) {
314 std::shared_ptr<fims_distributions::DensityComponentBase<Type>> d =
315 (*it).second;
316 if (d->input_type == "prior") {
317 FIMS_INFO_LOG("Setup prior for distribution " + fims::to_string(d->id));
319 FIMS_INFO_LOG("Link prior from distribution " + fims::to_string(d->id) +
320 " to parameter " + fims::to_string(d->key[0]));
321 d->priors.resize(d->key.size());
322 for (size_t i = 0; i < d->key.size(); i++) {
323 FIMS_INFO_LOG("Link prior from distribution " +
324 fims::to_string(d->id) + " to parameter " +
325 fims::to_string(d->key[0]));
326 vmit = this->variable_map.find(d->key[i]);
327 d->priors[i] = (*vmit).second;
328 }
329 FIMS_INFO_LOG("Prior size for distribution " + fims::to_string(d->id) +
330 "is: " + fims::to_string(d->observed_values.size()));
331 }
332 }
333 }
334
340 for (density_components_iterator it = this->density_components.begin();
341 it != this->density_components.end(); ++it) {
342 std::shared_ptr<fims_distributions::DensityComponentBase<Type>> d =
343 (*it).second;
344 if (d->input_type == "random_effects") {
345 FIMS_INFO_LOG("Setup random effects for distribution " +
346 fims::to_string(d->id));
348 FIMS_INFO_LOG("Link random effects from distribution " +
349 fims::to_string(d->id) + " to derived value " +
350 fims::to_string(d->key[0]));
351 vmit = this->variable_map.find(d->key[0]);
352 d->re = (*vmit).second;
353 if (d->key.size() == 2) {
354 vmit = this->variable_map.find(d->key[1]);
355 d->re_expected_values = (*vmit).second;
356 } else {
357 d->re_expected_values = &d->expected_values;
358 }
359 FIMS_INFO_LOG("Random effect size for distribution " +
360 fims::to_string(d->id) +
361 " is: " + fims::to_string(d->observed_values.size()));
362 }
363 }
364 }
365
370 void SetupData() {
371 for (density_components_iterator it = this->density_components.begin();
372 it != this->density_components.end(); ++it) {
373 std::shared_ptr<fims_distributions::DensityComponentBase<Type>> d =
374 (*it).second;
375 if (d->input_type == "data") {
376 FIMS_INFO_LOG("Setup expected value for data distribution " +
377 fims::to_string(d->id));
379 FIMS_INFO_LOG("Link expected value from distribution " +
380 fims::to_string(d->id) + " to derived value " +
381 fims::to_string(d->key[0]));
382 vmit = this->variable_map.find(d->key[0]);
383 d->data_expected_values = (*vmit).second;
385 "Expected value size for distribution " + fims::to_string(d->id) +
386 " is: " + fims::to_string((*d->data_expected_values).size()));
387 }
388 }
389 }
390
398 void SetFleetLandingsData(bool& valid_model,
399 std::shared_ptr<fims_popdy::Fleet<Type>> f) {
400 if (f->fleet_observed_landings_data_id_m != static_cast<Type>(-999)) {
401 uint32_t observed_landings_id =
402 static_cast<uint32_t>(f->fleet_observed_landings_data_id_m);
403 data_iterator it = this->data_objects.find(observed_landings_id);
404 if (it != this->data_objects.end()) {
405 f->observed_landings_data = (*it).second;
406 FIMS_INFO_LOG("Landings data for fleet " + fims::to_string(f->id) +
407 " successfully set to " +
408 fims::to_string(f->observed_landings_data->at(1)));
409 } else {
410 valid_model = false;
411 FIMS_ERROR_LOG("Expected landings data not defined for fleet " +
412 fims::to_string(f->id) + ", index " +
413 fims::to_string(observed_landings_id));
414 }
415 }
416 }
417
425 void SetFleetIndexData(bool& valid_model,
426 std::shared_ptr<fims_popdy::Fleet<Type>> f) {
427 if (f->fleet_observed_index_data_id_m != static_cast<Type>(-999)) {
428 uint32_t observed_index_id =
429 static_cast<uint32_t>(f->fleet_observed_index_data_id_m);
430 data_iterator it = this->data_objects.find(observed_index_id);
431 if (it != this->data_objects.end()) {
432 f->observed_index_data = (*it).second;
433 FIMS_INFO_LOG("Index data for fleet " + fims::to_string(f->id) +
434 " successfully set to " +
435 fims::to_string(f->observed_index_data->at(1)));
436 } else {
437 valid_model = false;
438 FIMS_ERROR_LOG("Expected index data not defined for fleet " +
439 fims::to_string(f->id) + ", index " +
440 fims::to_string(observed_index_id));
441 }
442 }
443 }
444
452 void SetAgeCompositionData(bool& valid_model,
453 std::shared_ptr<fims_popdy::Fleet<Type>> f) {
454 if (f->fleet_observed_agecomp_data_id_m != static_cast<Type>(-999)) {
455 uint32_t observed_agecomp_id =
456 static_cast<uint32_t>(f->fleet_observed_agecomp_data_id_m);
457 data_iterator it = this->data_objects.find(observed_agecomp_id);
458 if (it != this->data_objects.end()) {
459 f->observed_agecomp_data = (*it).second;
460 FIMS_INFO_LOG("Observed input age-composition data for fleet " +
461 fims::to_string(f->id) + " successfully set to " +
462 fims::to_string(f->observed_agecomp_data->at(1)));
463 } else {
464 valid_model = false;
466 "Expected age-composition observations not defined for fleet " +
467 fims::to_string(f->id));
468 }
469 }
470 }
471
479 void SetLengthCompositionData(bool& valid_model,
480 std::shared_ptr<fims_popdy::Fleet<Type>> f) {
481 if (f->fleet_observed_lengthcomp_data_id_m != static_cast<Type>(-999)) {
482 uint32_t observed_lengthcomp_id =
483 static_cast<uint32_t>(f->fleet_observed_lengthcomp_data_id_m);
484 data_iterator it = this->data_objects.find(observed_lengthcomp_id);
485 if (it != this->data_objects.end()) {
486 f->observed_lengthcomp_data = (*it).second;
487 FIMS_INFO_LOG("Observed input length-composition data for fleet " +
488 fims::to_string(f->id) + " successfully set to " +
489 fims::to_string(f->observed_lengthcomp_data->at(1)));
490 } else {
491 valid_model = false;
493 "Expected length-composition observations not defined for fleet " +
494 fims::to_string(f->id));
495 }
496 }
497 }
498
507 void SetFleetSelectivityModel(bool& valid_model,
508 std::shared_ptr<fims_popdy::Fleet<Type>> f) {
509 if (f->fleet_selectivity_id_m != static_cast<Type>(-999)) {
510 uint32_t sel_id = static_cast<uint32_t>(
511 f->fleet_selectivity_id_m); // cast as unsigned integer
513 sel_id); // if find, set it, otherwise invalid
514
515 if (it != this->selectivity_models.end()) {
516 f->selectivity = (*it).second; // elements in container held in pair
517 FIMS_INFO_LOG("Selectivity model " +
518 fims::to_string(f->fleet_selectivity_id_m) +
519 " successfully set to fleet " + fims::to_string(f->id));
520 } else {
521 valid_model = false;
522 FIMS_ERROR_LOG("Expected selectivity pattern not defined for fleet " +
523 fims::to_string(f->id) + ", selectivity pattern " +
524 fims::to_string(sel_id));
525 }
526 } else {
527 FIMS_WARNING_LOG("Warning: No selectivity pattern defined for fleet " +
528 fims::to_string(f->id) +
529 ". FIMS requires selectivity be defined for all fleets "
530 "when running a catch at age model.");
531 }
532 }
533
542 void SetRecruitment(bool& valid_model,
543 std::shared_ptr<fims_popdy::Population<Type>> p) {
544 if (p->recruitment_id != static_cast<Type>(-999)) {
545 uint32_t recruitment_uint = static_cast<uint32_t>(p->recruitment_id);
546
548 this->recruitment_models.find(recruitment_uint);
549
550 if (it != this->recruitment_models.end()) {
551 p->recruitment = (*it).second; // recruitment defined in population.hpp
552 FIMS_INFO_LOG("Recruitment model " + fims::to_string(recruitment_uint) +
553 " successfully set to population " +
554 fims::to_string(p->id));
555 } else {
556 valid_model = false;
558 "Expected recruitment function not defined for "
559 "population " +
560 fims::to_string(p->id) + ", recruitment function " +
561 fims::to_string(recruitment_uint));
562 }
563 } else {
565 "No recruitment function defined for population " +
566 fims::to_string(p->id) +
567 ". FIMS requires recruitment functions be defined for all "
568 "populations when running a catch at age model.");
569 }
570 }
571
580 void SetRecruitmentProcess(bool& valid_model,
581 std::shared_ptr<fims_popdy::Population<Type>> p) {
582 std::shared_ptr<fims_popdy::RecruitmentBase<Type>> r = p->recruitment;
583 // if recruitment is defined
584 if (r) {
585 if (r->process_id != static_cast<Type>(-999)) {
586 uint32_t process_uint = static_cast<uint32_t>(r->process_id);
588 this->recruitment_process_models.find(process_uint);
589
590 if (it != this->recruitment_process_models.end()) {
591 r->process = (*it).second; // recruitment process
593 "Recruitment Process model " + fims::to_string(process_uint) +
594 " successfully set to population " + fims::to_string(p->id));
595 (*it).second->recruitment = r;
596 } else {
597 valid_model = false;
599 "Expected recruitment process function not defined for "
600 "population " +
601 fims::to_string(p->id) + ", recruitment process function " +
602 fims::to_string(process_uint));
603 }
604 } else {
606 "No recruitment process function defined for population " +
607 fims::to_string(p->id) +
608 ". FIMS requires recruitment process functions be defined for all "
609 "recruitments when running a catch at age model.");
610 }
611 }
612 }
613
622 void SetGrowth(bool& valid_model,
623 std::shared_ptr<fims_popdy::Population<Type>> p) {
624 if (p->growth_id != static_cast<Type>(-999)) {
625 uint32_t growth_uint = static_cast<uint32_t>(p->growth_id);
627 growth_uint); // growth_models is specified in information.hpp
628 // and used in rcpp
629 // at the head of information.hpp; are the
630 // dimensions of ages defined in rcpp or where?
631 if (it != this->growth_models.end()) {
632 p->growth =
633 (*it).second; // growth defined in population.hpp (the object
634 // is called p, growth is within p)
635 FIMS_INFO_LOG("Growth model " + fims::to_string(growth_uint) +
636 " successfully set to population " +
637 fims::to_string(p->id));
638 } else {
639 valid_model = false;
640 FIMS_ERROR_LOG("Expected growth function not defined for population " +
641 fims::to_string(p->id) + ", growth function " +
642 fims::to_string(growth_uint));
643 }
644 } else {
645 FIMS_WARNING_LOG("Growth function undefined for population " +
646 fims::to_string(p->id) +
647 ". FIMS requires growth functions be defined for all "
648 "populations when running a catch at age model.");
649 }
650 }
651
660 void SetMaturity(bool& valid_model,
661 std::shared_ptr<fims_popdy::Population<Type>> p) {
662 if (p->maturity_id != static_cast<Type>(-999)) {
663 uint32_t maturity_uint = static_cast<uint32_t>(p->maturity_id);
665 maturity_uint); // >maturity_models is specified in
666 // information.hpp and used in rcpp
667 if (it != this->maturity_models.end()) {
668 p->maturity = (*it).second; // >maturity defined in population.hpp
669 FIMS_INFO_LOG("Maturity model " + fims::to_string(maturity_uint) +
670 " successfully set to population " +
671 fims::to_string(p->id));
672 } else {
673 valid_model = false;
675 "Expected maturity function not defined for population " +
676 fims::to_string(p->id) + ", maturity function " +
677 fims::to_string(maturity_uint));
678 }
679 } else {
680 FIMS_WARNING_LOG("Maturity function undefined for population " +
681 fims::to_string(p->id) +
682 ". FIMS requires maturity functions be defined for all "
683 "populations when running a catch at age model.");
684 }
685 }
686
693 void CreateFleetObjects(bool& valid_model) {
694 for (fleet_iterator it = this->fleets.begin(); it != this->fleets.end();
695 ++it) {
696 std::shared_ptr<fims_popdy::Fleet<Type>> f = (*it).second;
697 FIMS_INFO_LOG("Initializing fleet " + fims::to_string(f->id));
698
699 SetFleetLandingsData(valid_model, f);
700
701 SetFleetIndexData(valid_model, f);
702
703 SetAgeCompositionData(valid_model, f);
704
705 SetLengthCompositionData(valid_model, f);
706
707 SetFleetSelectivityModel(valid_model, f);
708 }
709 }
710
717 void SetDataObjects(bool& valid_model) {
718 for (density_components_iterator it = this->density_components.begin();
719 it != this->density_components.end(); ++it) {
720 std::shared_ptr<fims_distributions::DensityComponentBase<Type>> d =
721 (*it).second;
722
723 // set data objects if distribution is a data type
724 if (d->input_type == "data") {
725 if (d->observed_data_id_m != static_cast<Type>(-999)) {
726 uint32_t observed_data_id =
727 static_cast<uint32_t>(d->observed_data_id_m);
728 data_iterator it = this->data_objects.find(observed_data_id);
729
730 if (it != this->data_objects.end()) {
731 d->data_observed_values = (*it).second;
732 FIMS_INFO_LOG("Observed data " + fims::to_string(observed_data_id) +
733 " successfully set to density component " +
734 fims::to_string(d->id));
735 } else {
736 valid_model = false;
738 "Expected data observations not defined for density "
739 "component " +
740 fims::to_string(d->id) + ", observed data " +
741 fims::to_string(observed_data_id));
742 }
743 } else {
744 valid_model = false;
745 FIMS_ERROR_LOG("No data input for density component" +
746 fims::to_string(d->id));
747 }
748 }
749 }
750 }
751
758 void CreatePopulationObjects(bool& valid_model) {
759 for (population_iterator it = this->populations.begin();
760 it != this->populations.end(); ++it) {
761 std::shared_ptr<fims_popdy::Population<Type>> p = (*it).second;
762
763 FIMS_INFO_LOG("Initializing population " + fims::to_string(p->id));
764 // check if population has fleets
765 typename std::set<uint32_t>::iterator fleet_ids_it;
766
767 for (fleet_ids_it = p->fleet_ids.begin();
768 fleet_ids_it != p->fleet_ids.end(); ++fleet_ids_it) {
769 // error check and set population elements
770 // check me - add another fleet iterator to push information from
771 // for (fleet_iterator it = this->fleets.begin(); it !=
772 // this->fleets.end();
773 // ++it) {
774
775 fleet_iterator it = this->fleets.find(*fleet_ids_it);
776
777 if (it != this->fleets.end()) {
778 // Initialize fleet object
779 std::shared_ptr<fims_popdy::Fleet<Type>> f = (*it).second;
780 // population to the individual fleets This is to pass catch at age
781 // from population to fleets?
782 // any shared member in p (population is pushed into fleets)
783 p->fleets.push_back(f);
784 } else {
785 valid_model = false;
786 FIMS_ERROR_LOG("Fleet \"" + fims::to_string(*fleet_ids_it) +
787 "\" undefined, not found for Population \"" +
788 fims::to_string(p->id) + "\". ");
789 }
790 }
791
792 // set information dimensions
793 this->n_years = std::max(this->n_years, p->n_years);
794 this->n_ages = std::max(this->n_ages, p->n_ages);
795
796 SetRecruitment(valid_model, p);
797
798 SetRecruitmentProcess(valid_model, p);
799
800 SetGrowth(valid_model, p);
801
802 SetMaturity(valid_model, p);
803 }
804 }
805
809 void CreateModelingObjects(bool& valid_model) {
810 for (model_map_iterator it = this->models_map.begin();
811 it != this->models_map.end(); ++it) {
812 std::shared_ptr<fims_popdy::FisheryModelBase<Type>>& model = (*it).second;
813 std::set<uint32_t>::iterator jt;
814
815 for (jt = model->population_ids.begin();
816 jt != model->population_ids.end(); ++jt) {
817 population_iterator pt = this->populations.find((*jt));
818
819 if (pt != this->populations.end()) {
820 std::shared_ptr<fims_popdy::Population<Type>> p = (*pt).second;
821 model->populations.push_back(p);
822 for (size_t i = 0; i < p->fleets.size(); i++) {
823 uint32_t local_fleet_id = p->fleets[i]->GetId();
824 model->fleets[local_fleet_id] = p->fleets[i];
825 FIMS_INFO_LOG(std::string("Linked fleet id ") +
826 fims::to_string(local_fleet_id) +
827 std::string(" into model id ") +
828 fims::to_string(model->GetId()));
829 }
830 } else {
831 valid_model = false;
832 FIMS_ERROR_LOG("No population object defined for model " +
833 fims::to_string(model->GetId()));
834 }
835 }
836 model->Initialize();
837 }
838 }
839
850 bool CreateModel() {
851 FIMS_INFO_LOG("Creating model and checking for required components...");
852 bool valid_model = true;
853
854 CreateFleetObjects(valid_model);
855
856 SetDataObjects(valid_model);
857
858 CreatePopulationObjects(valid_model);
859
860 CreateModelingObjects(valid_model);
861
862 // setup priors, random effect, and data density components
863 SetupPriors();
865 SetupData();
866
867 if (valid_model) {
868 FIMS_INFO_LOG("Model successfully created.");
869 } else {
870 FIMS_ERROR_LOG("Model creation failed.");
871 }
872
873 return valid_model;
874 }
875
881 size_t GetNages() const { return n_ages; }
882
888 void SetNages(size_t n_ages) { this->n_ages = n_ages; }
889
895 size_t GetNyears() const { return n_years; }
896
902 void SetNyears(size_t n_years) { this->n_years = n_years; }
903
909 std::vector<Type*>& GetParameters() { return parameters; }
910
916 std::vector<Type*>& GetFixedEffectsParameters() {
918 }
919
925 std::vector<Type*>& GetRandomEffectsParameters() {
927 }
928
936 bool CheckModel() {
937 bool valid_model = true;
938 for (model_map_iterator it = this->models_map.begin();
939 it != this->models_map.end(); ++it) {
940 std::shared_ptr<fims_popdy::FisheryModelBase<Type>>& model = (*it).second;
941 std::set<uint32_t>::iterator jt;
942
943 for (jt = model->population_ids.begin();
944 jt != model->population_ids.end(); ++jt) {
945 population_iterator pt = this->populations.find((*jt));
946
947 if (pt != this->populations.end()) {
948 std::shared_ptr<fims_popdy::Population<Type>> p = (*pt).second;
949
950 if (model->model_type_m == "caa") {
951 typename std::set<uint32_t>::iterator fleet_ids_it;
952 for (fleet_ids_it = p->fleet_ids.begin();
953 fleet_ids_it != p->fleet_ids.end(); ++fleet_ids_it) {
954 fleet_iterator it = this->fleets.find(*fleet_ids_it);
955
956 if (it != this->fleets.end()) {
957 // Initialize fleet object
958 std::shared_ptr<fims_popdy::Fleet<Type>> f = (*it).second;
959
960 if (f->fleet_selectivity_id_m == static_cast<Type>(-999)) {
961 valid_model = false;
963 "No selectivity pattern defined for fleet " +
964 fims::to_string(f->id) +
965 ". FIMS requires selectivity be defined for all fleets "
966 "when running a catch at age model.");
967 }
968 }
969 }
970
971 if (p->recruitment_id == static_cast<Type>(-999)) {
972 valid_model = false;
974 "No recruitment function defined for population " +
975 fims::to_string(p->id) +
976 ". FIMS requires recruitment functions be defined for all "
977 "populations when running a catch at age model.");
978 }
979
980 std::shared_ptr<fims_popdy::RecruitmentBase<Type>> r =
981 p->recruitment;
982 r = p->recruitment;
983 if (r->process_id == static_cast<Type>(-999)) {
984 valid_model = false;
986 "No recruitment process function defined for population " +
987 fims::to_string(p->id) +
988 ". FIMS requires recruitment process functions be defined "
989 "for all "
990 "recruitments when running a catch at age model.");
991 }
992
993 if (p->growth_id == static_cast<Type>(-999)) {
994 valid_model = false;
996 "No growth function defined for population " +
997 fims::to_string(p->id) +
998 ". FIMS requires growth functions be defined for all "
999 "populations when running a catch at age model.");
1000 }
1001
1002 if (p->maturity_id == static_cast<Type>(-999)) {
1003 valid_model = false;
1004
1006 "No maturity function defined for population " +
1007 fims::to_string(p->id) +
1008 ". FIMS requires maturity functions be defined for all "
1009 "populations when running a catch at age model.");
1010 }
1011 }
1012 }
1013 }
1014 }
1015 return valid_model;
1016 }
1017};
1018
1019template <typename Type>
1020std::shared_ptr<Information<Type>> Information<Type>::fims_information =
1021 nullptr;
1023} // namespace fims_info
1024
1025#endif /* FIMS_COMMON_INFORMATION_HPP */
Stores FIMS model information and creates model. Contains all objects and data pre-model construction...
Definition information.hpp:36
std::map< uint32_t, std::shared_ptr< fims_popdy::GrowthBase< Type > > >::iterator growth_models_iterator
Definition information.hpp:92
void SetupRandomEffects()
Loop over distributions and set links to distribution x value if distribution is a random effects typ...
Definition information.hpp:339
std::map< uint32_t, std::shared_ptr< fims_popdy::Population< Type > > > populations
Definition information.hpp:114
std::unordered_map< uint32_t, std::shared_ptr< fims_popdy::FisheryModelBase< Type > > > models_map
Definition information.hpp:135
void RegisterRandomEffectName(std::string re_name)
Register a random effects name.
Definition information.hpp:303
std::map< uint32_t, std::shared_ptr< fims_data_object::DataObject< Type > > >::iterator data_iterator
Definition information.hpp:59
std::vector< Type * > & GetFixedEffectsParameters()
Get the Fixed Effects Parameters object.
Definition information.hpp:916
std::vector< Type * > random_effects_parameters
Definition information.hpp:45
void SetFleetLandingsData(bool &valid_model, std::shared_ptr< fims_popdy::Fleet< Type > > f)
Set pointers to landings data in the fleet module.
Definition information.hpp:398
void SetFleetIndexData(bool &valid_model, std::shared_ptr< fims_popdy::Fleet< Type > > f)
Set pointers to index data in the fleet module.
Definition information.hpp:425
void SetGrowth(bool &valid_model, std::shared_ptr< fims_popdy::Population< Type > > p)
Set pointers to the growth module referenced in the population module.
Definition information.hpp:622
std::map< uint32_t, std::shared_ptr< fims_popdy::RecruitmentBase< Type > > > recruitment_process_models
Definition information.hpp:71
void SetMaturity(bool &valid_model, std::shared_ptr< fims_popdy::Population< Type > > p)
Set pointers to the maturity module referenced in the population module.
Definition information.hpp:660
std::map< uint32_t, std::shared_ptr< fims_popdy::Fleet< Type > > >::iterator fleet_iterator
Definition information.hpp:109
void SetDataObjects(bool &valid_model)
Loop over all density components and set pointers to data objects.
Definition information.hpp:717
size_t n_years
Definition information.hpp:38
std::map< uint32_t, std::shared_ptr< fims_popdy::Fleet< Type > > > fleets
Definition information.hpp:105
void CreatePopulationObjects(bool &valid_model)
Loop over all populations and set pointers to population objects.
Definition information.hpp:758
std::map< uint32_t, std::shared_ptr< fims_popdy::GrowthBase< Type > > > growth_models
Definition information.hpp:87
void SetLengthCompositionData(bool &valid_model, std::shared_ptr< fims_popdy::Fleet< Type > > f)
Set pointers to length composition data in the fleet module.
Definition information.hpp:479
void SetFleetSelectivityModel(bool &valid_model, std::shared_ptr< fims_popdy::Fleet< Type > > f)
Set pointers to the selectivity module referenced in the fleet module.
Definition information.hpp:507
std::map< uint32_t, std::shared_ptr< fims_popdy::Population< Type > > >::iterator population_iterator
Definition information.hpp:119
void RegisterParameter(Type &p)
Register a parameter as estimable.
Definition information.hpp:276
std::map< uint32_t, std::shared_ptr< fims_distributions::DensityComponentBase< Type > > > density_components
Definition information.hpp:125
void RegisterRandomEffect(Type &re)
Register a random effect as estimable.
Definition information.hpp:285
void SetRecruitment(bool &valid_model, std::shared_ptr< fims_popdy::Population< Type > > p)
Set pointers to the recruitment module referenced in the population module.
Definition information.hpp:542
void SetNages(size_t n_ages)
Set the Nages object.
Definition information.hpp:888
size_t GetNages() const
Get the Nages object.
Definition information.hpp:881
std::unordered_map< uint32_t, fims::Vector< Type > * > variable_map
Definition information.hpp:142
void SetNyears(size_t n_years)
Set the n_years object.
Definition information.hpp:902
std::map< uint32_t, std::shared_ptr< fims_popdy::RecruitmentBase< Type > > >::iterator recruitment_models_iterator
Definition information.hpp:67
void SetRecruitmentProcess(bool &valid_model, std::shared_ptr< fims_popdy::Population< Type > > p)
Set pointers to the recruitment process module referenced in the population module.
Definition information.hpp:580
bool CheckModel()
Checks to make sure all required modules are present for specified model.
Definition information.hpp:936
std::map< uint32_t, std::shared_ptr< fims_popdy::SelectivityBase< Type > > > selectivity_models
Definition information.hpp:79
bool CreateModel()
Create the generalized stock assessment model that will evaluate the objective function....
Definition information.hpp:850
void Clear()
Clears all containers.
Definition information.hpp:155
size_t n_ages
Definition information.hpp:39
std::unordered_map< uint32_t, std::shared_ptr< fims_popdy::FisheryModelBase< Type > > >::iterator model_map_iterator
Definition information.hpp:139
std::map< uint32_t, std::shared_ptr< fims_distributions::DensityComponentBase< Type > > >::iterator density_components_iterator
Definition information.hpp:130
void SetAgeCompositionData(bool &valid_model, std::shared_ptr< fims_popdy::Fleet< Type > > f)
Set pointers to age composition data in the fleet module.
Definition information.hpp:452
std::vector< Type * > parameters
Definition information.hpp:43
void CreateFleetObjects(bool &valid_model)
Loop over all fleets and set pointers to fleet objects.
Definition information.hpp:693
std::vector< std::string > parameter_names
Definition information.hpp:48
std::map< uint32_t, std::shared_ptr< fims_popdy::MaturityBase< Type > > > maturity_models
Definition information.hpp:96
std::vector< Type * > & GetRandomEffectsParameters()
Get the Random Effects Parameters object.
Definition information.hpp:925
std::vector< std::string > random_effects_names
Definition information.hpp:51
std::vector< Type * > & GetParameters()
Get the Parameters object.
Definition information.hpp:909
std::map< uint32_t, std::shared_ptr< fims_popdy::RecruitmentBase< Type > > >::iterator recruitment_process_iterator
Definition information.hpp:75
void SetupPriors()
Loop over distributions and set links to distribution x value if distribution is a prior type.
Definition information.hpp:311
size_t GetNyears() const
Get the n_years object.
Definition information.hpp:895
std::unordered_map< uint32_t, fims::Vector< Type > * >::iterator variable_map_iterator
Definition information.hpp:145
std::map< uint32_t, std::shared_ptr< fims_popdy::SelectivityBase< Type > > >::iterator selectivity_models_iterator
Definition information.hpp:83
void RegisterParameterName(std::string p_name)
Register a parameter name.
Definition information.hpp:294
void SetupData()
Loop over distributions and set links to distribution expected value if distribution is a data type.
Definition information.hpp:370
std::map< uint32_t, std::shared_ptr< fims_popdy::MaturityBase< Type > > >::iterator maturity_models_iterator
Definition information.hpp:100
std::string State()
Get a summary string of the Information object state.
Definition information.hpp:228
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:263
void CreateModelingObjects(bool &valid_model)
Loop over all models and set pointers to population objects.
Definition information.hpp:809
static std::shared_ptr< Information< Type > > fims_information
Definition information.hpp:42
std::map< uint32_t, std::shared_ptr< fims_popdy::RecruitmentBase< Type > > > recruitment_models
Definition information.hpp:63
std::vector< Type * > fixed_effects_parameters
Definition information.hpp:47
std::map< uint32_t, std::shared_ptr< fims_data_object::DataObject< Type > > > data_objects
Definition information.hpp:56
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:648
#define FIMS_INFO_LOG(MESSAGE)
Record an info-log entry with metadata.
Definition def.hpp:636
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:664
Establishes the FIMS Vector class.
Definition of the FIMSObject structure.
Base class for all fleets.
Definition fleet.hpp:25
Population class. Contains subpopulations that are divided into generic partitions (e....
Definition population.hpp:25