-
Notifications
You must be signed in to change notification settings - Fork 1
/
Information.hpp
10167 lines (8440 loc) · 558 KB
/
Information.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* File: Information.hpp
*
* Author: Matthew Supernaw
* National Oceanic and Atmospheric Administration
* National Marine Fisheries Service
* Sustainable Fisheries Division
* St. Petersburg, FL, 33701
*
* Created on September 16, 2016, 12:35 PM
*
* This File is part of the NOAA, National Marine Fisheries Service
* Metapopulation Assessment System project.
*
* This software is a "United States Government Work" under the terms of the
* United States Copyright Act. It was written as part of the author's official
* duties as a United States Government employee and thus cannot be copyrighted.
* This software is freely available to the public for use. The National Oceanic
* And Atmospheric Administration and the U.S. Government have not placed any
* restriction on its use or reproduction. Although all reasonable efforts have
* been taken to ensure the accuracy and reliability of the software and data,
* the National Oceanic And Atmospheric Administration and the U.S. Government
* do not and cannot warrant the performance or results that may be obtained by
* using this software or data. The National Oceanic And Atmospheric
* Administration and the U.S. Government disclaim all warranties, express or
* implied, including warranties of performance, merchantability or fitness
* for any particular purpose.
*
* Please cite the author(s) in any work or product based on this material.
*
*/
#ifndef MAS_INFORMATION_HPP
#define MAS_INFORMATION_HPP
#include "Common.hpp"
#include "Area.hpp"
#include "Population.hpp"
#include "Season.hpp"
#include "Movement.hpp"
#include "Selectivity.hpp"
#include "Survey.hpp"
#include "third_party/rapidjson/document.h"
#include "third_party/rapidjson/prettywriter.h"
#include "third_party/rapidjson/stringbuffer.h"
#include "Fleet.hpp"
#include "HarvestControlRule.hpp"
#include <ctime>
//#define MAS_DEBUG
#ifdef MAS_DEBUG
#define INFO_DEBUG std::cout<<"Information.hpp: "<<__func__<<": "<<__LINE__<<std::endl;
#else
#define INFO_DEBUG
#endif
namespace mas {
struct EnsembleUnit {
mas::SubmodelType type;
std::vector<int> units;
rapidjson::Document::MemberIterator val;
};
template<typename REAL_T>
class Information : public mas::ModelObject<REAL_T> {
typedef typename VariableTrait<REAL_T>::variable variable;
typedef typename std::map<int, std::vector<std::shared_ptr<DataObject<REAL_T> > > >::iterator data_iterator;
std::string data_path;
public:
std::map<int, std::vector<std::shared_ptr<DataObject<REAL_T> > > > data_dictionary;
std::vector<std::shared_ptr<DataObject<REAL_T> > > data;
std::string analyst = "NA";
std::string study_name = "NA";
int nyears;
int nseasons = 1;
int first_year;
int last_year;
REAL_T spawning_season_offset = .25;
REAL_T catch_fraction_of_year = .5;
REAL_T survey_fraction_of_year = .5;
std::vector<variable> ages;
std::vector<REAL_T> ages_real;
bool natal_movement = false;
bool valid_configuration = true;
std::unordered_map<int, std::shared_ptr<mas::Area<REAL_T> > > areas;
std::unordered_map<int, std::shared_ptr<mas::Season<REAL_T> > > seasons;
//System sub model objects
std::unordered_map<int, std::shared_ptr<mas::Population<REAL_T> > > populations;
std::unordered_map<int, std::shared_ptr<mas::FishingMortality<REAL_T> > > fishing_mortality_models;
std::unordered_map<int, std::shared_ptr<mas::SelectivityBase<REAL_T> > > selectivity_models;
std::unordered_map<int, std::shared_ptr<mas::Fleet<REAL_T> > > fleets;
std::unordered_map<int, std::shared_ptr<mas::Survey<REAL_T> > > survey_models;
// per population
std::unordered_map<int, std::shared_ptr<mas::GrowthBase<REAL_T> > > growth_models;
std::unordered_map<int, std::shared_ptr<mas::FecundityBase<REAL_T> > > fecundity_models;
std::unordered_map<int, std::shared_ptr<mas::Movement<REAL_T> > > movement_models;
// per population - area
std::unordered_map<int, std::shared_ptr<mas::NaturalMortality<REAL_T> > > natural_mortality_models;
std::unordered_map<int, std::shared_ptr<mas::RecruitmentBase<REAL_T> > > recruitment_models;
std::unordered_map<int, std::shared_ptr<mas::NLLFunctor<REAL_T> > > likelihood_components;
//System sub model iterators
typedef typename std::unordered_map<int, std::shared_ptr<mas::Season<REAL_T> > >::iterator seasons_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::Area<REAL_T> > >::iterator area_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::Population<REAL_T> > >::iterator population_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::GrowthBase<REAL_T> > >::iterator growth_model_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::RecruitmentBase<REAL_T> > >::iterator recruitment_model_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::NaturalMortality<REAL_T> > >::iterator natural_mortality_model_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::FishingMortality<REAL_T> > >::iterator fishing_mortality_model_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::FecundityBase<REAL_T> > >::iterator fecundity_model_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::Movement<REAL_T> > >::iterator movement_model_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::SelectivityBase<REAL_T> > >::iterator selectivity_model_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::Fleet<REAL_T> > >::iterator fleet_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::Survey<REAL_T> > >::iterator survey_model_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::NLLFunctor<REAL_T> > >::iterator likelihood_components_iterator;
rapidjson::Document config_document;
rapidjson::Document data_document;
std::vector<EnsembleUnit> ensemble_units;
Information() {
}
Information(const Information<REAL_T>& orig) {
}
virtual ~Information() {
}
void ParseConfig(const std::string& path) {
std::time_t result = std::time(nullptr);
mas_log << "MAS Run: " << std::asctime(std::localtime(&result)) << "\n";
INFO_DEBUG;
std::stringstream ss;
std::ifstream config;
config.open(path.c_str());
if (!config.good()) {
std::cerr << "MAS Configuration file \"" << path << "\" not found.\n";
mas::mas_log << "MAS Configuration file \"" << path << "\" not found.\n";
this->valid_configuration = false;
}
INFO_DEBUG;
while (config.good()) {
std::string line;
std::getline(config, line);
ss << line << "\n";
}
this->ParseConfig(ss);
}
void ParseConfig(const std::stringstream& ss) {
INFO_DEBUG;
config_document.Parse(ss.str().c_str());
INFO_DEBUG;
this->ParseConfig(config_document);
}
void ParseConfig(rapidjson::Document& document) {
rapidjson::Document::MemberIterator mit;
for (mit = document.MemberBegin(); mit != document.MemberEnd(); ++mit) {
if (std::string((*mit).name.GetString()) == "analyst") {
INFO_DEBUG;
this->analyst = std::string((*mit).value.GetString());
}
if (std::string((*mit).name.GetString()) == "extended_plus_group") {
INFO_DEBUG;
mas::Subpopulation<REAL_T>::length_weight_key_carryout = static_cast<uint32_t> ((*mit).value.GetInt());
}
#warning should these be here or in the population definition
if (std::string((*mit).name.GetString()) == "catch_season_offset") {
INFO_DEBUG;
this->catch_fraction_of_year = static_cast<REAL_T> ((*mit).value.GetDouble());
}
if (std::string((*mit).name.GetString()) == "survey_season_offset") {
INFO_DEBUG;
this->survey_fraction_of_year = static_cast<REAL_T> ((*mit).value.GetDouble());
}
if (std::string((*mit).name.GetString()) == "spawning_season_offset") {
INFO_DEBUG;
this->spawning_season_offset = static_cast<REAL_T> ((*mit).value.GetDouble());
}
if (std::string((*mit).name.GetString()) == "study_name") {
INFO_DEBUG;
this->study_name = std::string((*mit).value.GetString());
}
if (std::string((*mit).name.GetString()) == "movement_type") {
INFO_DEBUG;
if (std::string((*mit).value.GetString()) == std::string("natal_homing")) {
this->natal_movement = true;
}
}
if (std::string((*mit).name.GetString()) == "data") {
INFO_DEBUG;
this->data_path = std::string((*mit).value.GetString());
}
if (std::string((*mit).name.GetString()) == "years") {
INFO_DEBUG;
this->nyears = (*mit).value.GetInt();
}
if (std::string((*mit).name.GetString()) == "first_year") {
INFO_DEBUG;
this->first_year = (*mit).value.GetInt();
}
if (std::string((*mit).name.GetString()) == "last_year") {
INFO_DEBUG;
this->last_year = (*mit).value.GetInt();
}
if (std::string((*mit).name.GetString()) == "ages") {
INFO_DEBUG;
this->ages.clear();
this->ages_real.clear();
rapidjson::Value& ages = (*mit).value;
if (ages.IsArray()) {
for (int i = 0; i < ages.Size(); i++) {
this->ages.push_back(variable(static_cast<REAL_T> (ages[i].GetDouble())));
this->ages_real.push_back(static_cast<REAL_T> (ages[i].GetDouble()));
}
} else {
std::cout << "Configuration Error: \"ages\" should be a vector.\n";
mas::mas_log << "Configuration Error: \"ages\" should be a vector.\n";
this->valid_configuration = false;
}
mas::GrowthBase<REAL_T>::ages.resize(0);
mas::GrowthBase<REAL_T>::ages_to_intrpolate.clear();
for (int i = 0; i < this->ages.size(); i++) {
mas::GrowthBase<REAL_T>::ages.push_back(this->ages[i]);
mas::GrowthBase<REAL_T>::ages_to_intrpolate.insert(this->ages[i].GetValue());
mas::GrowthBase<REAL_T>::ages_to_intrpolate.insert(this->ages[i].GetValue() + this->catch_fraction_of_year);
mas::GrowthBase<REAL_T>::ages_to_intrpolate.insert(this->ages[i].GetValue() + this->survey_fraction_of_year);
mas::GrowthBase<REAL_T>::ages_to_intrpolate.insert(this->ages[i].GetValue() + this->spawning_season_offset);
}
}
if (std::string((*mit).name.GetString()) == "seasons") {
// HandleSeason(mit);
INFO_DEBUG;
this->nseasons = (*mit).value.GetInt();
}
if (std::string((*mit).name.GetString()) == "recruitment") {
INFO_DEBUG;
HandleRecruitmentModel(mit);
}
if (std::string((*mit).name.GetString()) == "growth") {
INFO_DEBUG;
HandleGrowthModel(mit);
}
if (std::string((*mit).name.GetString()) == "selectivity") {
INFO_DEBUG;
HandleSelectivityModel(mit);
}
if (std::string((*mit).name.GetString()) == "area") {
INFO_DEBUG;
HandleAreaModel(mit);
}
if (std::string((*mit).name.GetString()) == "population") {
INFO_DEBUG;
HandlePopulationModel(mit);
}
if (std::string((*mit).name.GetString()) == "fleet") {
INFO_DEBUG;
HandleFleetModel(mit);
}
if (std::string((*mit).name.GetString()) == "movement") {
INFO_DEBUG;
HandleMovementModel(mit);
}
if (std::string((*mit).name.GetString()) == "natural_mortality") {
INFO_DEBUG;
HandleNaturalMortalityModel(mit);
}
if (std::string((*mit).name.GetString()) == "fishing_mortality") {
INFO_DEBUG;
HandleFishingMortalityModel(mit);
}
if (std::string((*mit).name.GetString()) == "survey") {
INFO_DEBUG;
HandleSurveyModel(mit);
}
if (std::string((*mit).name.GetString()) == "likelihood_component") {
INFO_DEBUG;
HandleLikelihoodComponent(mit);
}
INFO_DEBUG;
}
}
void HandleLikelihoodComponent(rapidjson::Document::MemberIterator& likelihood_component) {
INFO_DEBUG
std::shared_ptr < mas::DataObject<REAL_T > > lambda_data(new mas::DataObject<REAL_T>());
rapidjson::Document::MemberIterator rit;
rit = (*likelihood_component).value.FindMember("model");
if (rit == (*likelihood_component).value.MemberEnd()) {
std::cout << "Configuration Error: Likelihood is required to have a model specified.\n";
mas::mas_log << "Configuration Error: Likelihood is required to have a model specified.\n";
this->valid_configuration = false;
}
std::shared_ptr < mas::NLLFunctor<REAL_T > > model(NULL);
std::string smodel = std::string((*rit).value.GetString());
rit = (*likelihood_component).value.FindMember("id");
int model_id = 0;
if (rit == (*likelihood_component).value.MemberEnd()) {
std::cout << "Configuration Error: Likelihood is required to have a unique identifier\n";
mas::mas_log << "Configuration Error: Likelihood is required to have a unique identifier\n";
this->valid_configuration = false;
} else {
model_id = (*rit).value.GetInt();
}
rit = (*likelihood_component).value.FindMember("lambda");
if (rit != (*likelihood_component).value.MemberEnd()) {
// lambda_data = std::make_shared<mas::DataObject<REAL_T> >();
rapidjson::Document::MemberIterator lit;
rapidjson::Document::MemberIterator lvit;
lit = (*rit).value.FindMember("years");
if (lit != (*rit).value.MemberEnd()) {
lambda_data->imax = (*lit).value.GetInt();
}
lit = (*rit).value.FindMember("seasons");
if (lit != (*rit).value.MemberEnd()) {
lambda_data->jmax = (*lit).value.GetInt();
}
lit = (*rit).value.FindMember("ages");
if (lit != (*rit).value.MemberEnd()) {
lambda_data->kmax = (*lit).value.GetInt();
}
lit = (*rit).value.FindMember("value");
if (lit != (*rit).value.MemberEnd()) {
if ((*lit).value.IsArray()) {
rapidjson::Value& v = (*lit).value;
for (int i = 0; i < v.Size(); i++) {
if (v[i].IsArray()) {
for (int j = 0; j < v[i].Size(); j++) {
if (v[i][j].IsArray()) {
for (int k = 0; k < v[i][j].Size(); k++) {
lambda_data->data.push_back(static_cast<REAL_T> (v[i][j][k].GetDouble()));
}
} else {
lambda_data->data.push_back(static_cast<REAL_T> (v[i][j].GetDouble()));
}
}
} else {
lambda_data->data.push_back(static_cast<REAL_T> (v[i].GetDouble()));
}
}
}
}
}
rapidjson::Document::MemberIterator pit = (*likelihood_component).value.FindMember("parameters");
if (smodel == std::string("dirichlet_multinomial")) {
model = std::make_shared<mas::DirichletMultinomial<REAL_T> >();
mas::DirichletMultinomial<REAL_T>* dirichlet = (mas::DirichletMultinomial<REAL_T>*)model.get();
std::stringstream ss;
ss << "dirichlet_multinomial_beta_" << model_id;
VariableTrait<REAL_T>::SetName(dirichlet->beta, ss.str());
ss.str("");
if (pit == (*likelihood_component).value.MemberEnd()) {
std::cout << "Configuration Waring: Likelihood Component \"Dirichlet-Multinomial \" has no parameter definitions. Defaults will be used\n";
mas::mas_log << "Configuration Waring: Likelihood Component \"Dirichlet-Multinomial \" has no parameter definitions. Defaults will be used\n";
} else {
rapidjson::Document::MemberIterator ppit = (*pit).value.FindMember("beta");
if (ppit == (*pit).value.MemberEnd()) {
std::cout << "Configuration Warning: Likelihood Component \"Dirichlet-Multinomial \" " <<
"does not define \"beta\". Model will use the default value of 0.5 and \"beta\" will not be estimated.\n";
mas::mas_log << "Configuration Warning: Likelihood Component \"Dirichlet-Multinomial \" " <<
"does not define \"beta\". Model will use the default value of 0.5 and \"beta\" will not be estimated.\n";
} else {
bool estimated = false;
int phase = 1;
//1. Get initial value if there is one.
rapidjson::Document::MemberIterator pm = (*ppit).value.FindMember("value");
if (pm == (*ppit).value.MemberEnd()) {
std::cout << "Configuration Warning: Selectivity model \"Dirichlet-Multinomial\" does not provide a initial value for \"beta\".\n";
mas::mas_log << "Configuration Warning: Selectivity model \"Dirichlet-Multinomial\" does not provide a initial value for \"beta\".\n";
} else {
VariableTrait<REAL_T>::SetValue(dirichlet->beta, static_cast<REAL_T> ((*pm).value.GetDouble()));
}
//2. Get min boundary if there is one.
pm = (*ppit).value.FindMember("min");
if (pm != (*ppit).value.MemberEnd()) {
VariableTrait<REAL_T>::SetMinBoundary(dirichlet->beta, static_cast<REAL_T> ((*pm).value.GetDouble()));
}
//3. Get max boundary if there is one.
pm = (*ppit).value.FindMember("max");
if (pm != (*ppit).value.MemberEnd()) {
VariableTrait<REAL_T>::SetMaxBoundary(dirichlet->beta, static_cast<REAL_T> ((*pm).value.GetDouble()));
}
pm = (*ppit).value.FindMember("estimated");
if (pm != (*ppit).value.MemberEnd()) {
std::string e = std::string((*pm).value.GetString());
if (e == "true") {
estimated = true;
}
}
INFO_DEBUG
if (estimated) {
phase = 1;
pm = (*ppit).value.FindMember("phase");
if (pm != (*ppit).value.MemberEnd()) {
phase = (*pm).value.GetInt();
}
//register alpha
dirichlet->Register(dirichlet->beta, phase);
}
}
}
} else if (smodel == std::string("dirichlet_multinomial_robust")) {
model = std::make_shared<mas::DirichletMultinomialRobust<REAL_T> >();
mas::DirichletMultinomialRobust<REAL_T>* dirichlet = (mas::DirichletMultinomialRobust<REAL_T>*)model.get();
rapidjson::Document::MemberIterator eit = (*likelihood_component).value.FindMember("epsilon");
if (eit != (*likelihood_component).value.MemberEnd()) {
dirichlet->epsilon = static_cast<REAL_T> ((*eit).value.GetDouble());
}
std::stringstream ss;
ss << "dirichlet_multinomial_robust_beta_" << model_id;
VariableTrait<REAL_T>::SetName(dirichlet->beta, ss.str());
ss.str("");
if (pit == (*likelihood_component).value.MemberEnd()) {
std::cout << "Configuration Waring: Likelihood Component \"Dirichlet-Multinomial Robust\" has no parameter definitions. Defaults will be used\n";
mas::mas_log << "Configuration Waring: Likelihood Component \"Dirichlet-Multinomial Robust\" has no parameter definitions. Defaults will be used\n";
} else {
rapidjson::Document::MemberIterator ppit = (*pit).value.FindMember("beta");
if (ppit == (*pit).value.MemberEnd()) {
std::cout << "Configuration Warning: Likelihood Component \"Dirichlet-Multinomial Robust\" " <<
"does not define \"beta\". Model will use the default value of 0.5 and \"beta\" will not be estimated.\n";
mas::mas_log << "Configuration Warning: Likelihood Component \"Dirichlet-Multinomial Robust\" " <<
"does not define \"beta\". Model will use the default value of 0.5 and \"beta\" will not be estimated.\n";
} else {
bool estimated = false;
int phase = 1;
//1. Get initial value if there is one.
rapidjson::Document::MemberIterator pm = (*ppit).value.FindMember("value");
if (pm == (*ppit).value.MemberEnd()) {
std::cout << "Configuration Warning: Selectivity model \"Dirichlet-Multinomial Robust\" does not provide a initial value for \"beta\".\n";
mas::mas_log << "Configuration Warning: Selectivity model \"Dirichlet-Multinomial Robust\" does not provide a initial value for \"beta\".\n";
} else {
VariableTrait<REAL_T>::SetValue(dirichlet->beta, static_cast<REAL_T> ((*pm).value.GetDouble()));
}
//2. Get min boundary if there is one.
pm = (*ppit).value.FindMember("min");
if (pm != (*ppit).value.MemberEnd()) {
VariableTrait<REAL_T>::SetMinBoundary(dirichlet->beta, static_cast<REAL_T> ((*pm).value.GetDouble()));
}
//3. Get max boundary if there is one.
pm = (*ppit).value.FindMember("max");
if (pm != (*ppit).value.MemberEnd()) {
VariableTrait<REAL_T>::SetMaxBoundary(dirichlet->beta, static_cast<REAL_T> ((*pm).value.GetDouble()));
}
pm = (*ppit).value.FindMember("estimated");
if (pm != (*ppit).value.MemberEnd()) {
std::string e = std::string((*pm).value.GetString());
if (e == "true") {
estimated = true;
}
}
INFO_DEBUG
if (estimated) {
phase = 1;
pm = (*ppit).value.FindMember("phase");
if (pm != (*ppit).value.MemberEnd()) {
phase = (*pm).value.GetInt();
}
//register alpha
dirichlet->Register(dirichlet->beta, phase);
}
}
}
//no estimable parameters for these component types
} else if (smodel == std::string("multinomial")) {
model = std::make_shared<mas::Multinomial<REAL_T> >();
} else if (smodel == std::string("multinomial_robust")) {
model = std::make_shared<mas::MultinomialRobust<REAL_T> >();
} else if (smodel == std::string("lognormal")) {
model = std::make_shared<mas::Lognormal<REAL_T> >();
rapidjson::Document::MemberIterator eit = (*likelihood_component).value.FindMember("standard_error");
if (eit != (*likelihood_component).value.MemberEnd()) {
mas::Lognormal<REAL_T>* models = (mas::Lognormal<REAL_T>*)model.get();
models->sigma = static_cast<REAL_T> ((*eit).value.GetDouble());
}
}
model->id = model_id;
model->lambda = lambda_data;
likelihood_components_iterator it = this->likelihood_components.find(model->id);
if (it != this->likelihood_components.end()) {
std::cout << "Configuration Error: More than one likelihood components with the same identifier defined. Likelihood components require a unique id.\n";
std::cout << "Configuration Error: More than one likelihood components with the same identifier defined. Likelihood components require a unique id.\n";
this->valid_configuration = false;
} else {
this->likelihood_components[model->id] = model;
}
}
void HandleSurveyModel(rapidjson::Document::MemberIterator & survey_model) {
std::shared_ptr<mas::Survey<REAL_T> > model(new mas::Survey<REAL_T>());
std::stringstream ss;
INFO_DEBUG
rapidjson::Document::MemberIterator rit = (*survey_model).value.FindMember("id");
int model_id = 0;
if (rit == (*survey_model).value.MemberEnd()) {
std::cout << "Configuration Error: Survey is required to have a unique identifier\n";
mas::mas_log << "Configuration Error: Survey is required to have a unique identifier\n";
this->valid_configuration = false;
} else {
model_id = (*rit).value.GetInt();
}
model->id = model_id;
rit = (*survey_model).value.FindMember("name");
if (rit != (*survey_model).value.MemberEnd()) {
model->name = std::string((*rit).value.GetString());
}
rit = (*survey_model).value.FindMember("survey_fraction_of_year");
if (rit != (*survey_model).value.MemberEnd()) {
model->survey_fraction_of_year = (*rit).value.GetDouble();
}
rit = (*survey_model).value.FindMember("CV");
if (rit != (*survey_model).value.MemberEnd()) {
model->CV = (*rit).value.GetDouble();
}
rit = (*survey_model).value.FindMember("selectivity");
if (rit != (*survey_model).value.MemberEnd()) {
if (!(*rit).value.IsArray()) {
std::cout << "Configuration Error: Surveys are required to have selectivity definitions in a vector\n";
std::cout << "Configuration Error: Surveys are required to have selectivity definitions in a vector\n";
this->valid_configuration = false;
} else {
for (int i = 0; i < (*rit).value.Size(); i++) {
rapidjson::Value& m = (*rit).value[i];
int sid = 0;
int sarea = 0;
int sseason = 0;
REAL_T fraction = 0.5;
rapidjson::Document::MemberIterator vit = m.FindMember("id");
if (vit == m.MemberEnd()) {
std::cout << "Configuration Error: Survey selectivity has no model identifier\n";
std::cout << "Configuration Error: Survey selectivity has no model identifier\n";
this->valid_configuration = false;
} else {
sid = (*vit).value.GetInt();
}
rapidjson::Document::MemberIterator mit = m.FindMember("ensemble");
if (mit != m.MemberEnd()) {
if (!(*mit).value.IsArray()) {
std::cout << "Expected array for survey selectivity ensemble unit, ignored.\n";
mas_log << "Expected array for survey selectivity ensemble unit, ignored.\n";
} else {
EnsembleUnit eunit;
eunit.type = mas::SELECTIVITY;
rapidjson::Value& e = (*mit).value;
eunit.val = vit;
for (int i = 0; i < e.Size(); i++) {
eunit.units.push_back(e[i].GetInt());
}
this->ensemble_units.push_back(eunit);
}
}
mit = m.FindMember("season");
if (mit == m.MemberEnd()) {
std::cout << "Configuration Error: Survey selectivity season has no identifier\n";
std::cout << "Configuration Error: Survey selectivity season has no identifier\n";
this->valid_configuration = false;
} else {
sseason = (*mit).value.GetInt();
}
mit = m.FindMember("fraction");
if (mit == m.MemberEnd()) {
std::cout << "Configuration Warning: Survey selectivity season has no catch fraction. Default is 0.5\n";
std::cout << "Configuration Error: Survey selectivity season has no catch fraction. Default is 0.5\n";
} else {
fraction = (*mit).value.GetDouble();
}
mit = m.FindMember("area");
if (mit == m.MemberEnd()) {
std::cout << "Configuration Error: Survey selectivity area has no identifier\n";
std::cout << "Configuration Error: Survey selectivity area has no identifier\n";
this->valid_configuration = false;
} else {
sarea = (*mit).value.GetInt();
}
model->season_area_selectivity_ids[sseason][sarea] = sid;
model->area_season_selectivity_ids[sarea][sseason] = sid;
model->area_season_survey_fraction[sarea][sseason] = fraction;
model->season_area_survey_fraction[sseason][sarea] = fraction;
}
}
}
rit = (*survey_model).value.FindMember("parameters");
if (rit == (*survey_model).value.MemberEnd()) {
std::cout << "Configuration Error: Survey model \"" << model->id << "\" has no parameter definitions.\n";
mas::mas_log << "Configuration Error: Survey model \"" << model->id << "\" has no parameter definitions.\n";
this->valid_configuration = false;
} else {
bool estimated = false;
int phase = 1;
rapidjson::Document::MemberIterator qit = (*rit).value.FindMember("q");
//1. Get initial value if there is one.
rapidjson::Document::MemberIterator pm = (*qit).value.FindMember("value");
if (pm == (*qit).value.MemberEnd()) {
std::cout << "Configuration Error: Survey model \"" << model->id << "\" does not provide an initial value for \"q\".\n";
mas::mas_log << "Configuration Error: Survey model \"" << model->id << "\" does not provide an initial value for \"q\".\n";
} else {
ss << "survey_q_" << model_id;
VariableTrait<REAL_T>::SetName(model->q, ss.str());
VariableTrait<REAL_T>::SetValue(model->q, static_cast<REAL_T> ((*pm).value.GetDouble()));
}
//2. Get min boundary if there is one.
pm = (*qit).value.FindMember("min");
if (pm != (*qit).value.MemberEnd()) {
VariableTrait<REAL_T>::SetMinBoundary(model->q, static_cast<REAL_T> ((*pm).value.GetDouble()));
}
//3. Get max boundary if there is one.
pm = (*qit).value.FindMember("max");
if (pm != (*qit).value.MemberEnd()) {
VariableTrait<REAL_T>::SetMaxBoundary(model->q, static_cast<REAL_T> ((*pm).value.GetDouble()));
}
pm = (*qit).value.FindMember("estimated");
if (pm != (*qit).value.MemberEnd()) {
std::string e = std::string((*pm).value.GetString());
if (e == "true") {
estimated = true;
}
}
if (estimated) {
phase = 1;
pm = (*qit).value.FindMember("phase");
if (pm != (*qit).value.MemberEnd()) {
phase = (*pm).value.GetInt();
}
//register alpha
model->Register(model->q, phase);
}
}
rit = (*survey_model).value.FindMember("likelihood_components");
if (rit != (*survey_model).value.MemberEnd()) {
if (!(*rit).value.IsArray()) {
std::cout << "Configuration Warning: No Likelihood components defined for survey \"" << model->id << "\". Default will be used\n";
std::cout << "Configuration Warning: No Likelihood components defined for survey \"" << model->id << "\". Default will be used\n";
} else {
for (int i = 0; i < (*rit).value.Size(); i++) {
rapidjson::Value& m = (*rit).value[i];
rapidjson::Document::MemberIterator mit = m.FindMember("id");
if (mit == m.MemberEnd()) {
std::cout << "Configuration Error: Surveys Likelihood component has no model identifier\n";
std::cout << "Configuration Error: Surveys Likelihood component has no model identifier\n";
this->valid_configuration = false;
} else {
rapidjson::Document::MemberIterator mit2 = m.FindMember("component");
if (mit2 == m.MemberEnd()) {
std::cout << "Configuration Error: Surveys Likelihood component has no \"component\" specified.\n";
std::cout << "Configuration Error: Surveys Likelihood component has no \"component\" specified.\n";
this->valid_configuration = false;
} else {
std::string c = std::string((*mit2).value.GetString());
if (c == "age_comp") {
model->survey_age_comp_likelihood_component_id = (*mit).value.GetInt();
} else if (c == "biomass_comp") {
model->survey_biomass_likelihood_component_id = (*mit).value.GetInt();
} else {
std::cout << "Configuration Error: Unknown \"component\" of \" " << c << " \" specified for Survey \"" << model->id << "\" likelihood component definition.\n";
std::cout << "Configuration Error: Unknown \"component\" of \" " << c << " \" specified for Survey \"" << model->id << "\" likelihood component definition.\n";
this->valid_configuration = false;
}
}
}
//
}
}
}
survey_model_iterator fit;
fit = this->survey_models.find(model->id);
if (fit == this->survey_models.end()) {
this->survey_models[model->id] = model;
} else {
std::cout << "Configuration Error: More than one Survey with the same identifier defined. Surveys require a unique id.\n";
mas::mas_log << "Configuration Error: More than one Survey with the same identifier defined. Surveys require a unique id.\n";
}
}
void HandleFishingMortalityModel(rapidjson::Document::MemberIterator & mortality_model) {
INFO_DEBUG
std::shared_ptr<mas::FishingMortality<REAL_T> > model(new mas::FishingMortality<REAL_T>());
bool estimated = false;
bool delta_method = false;
int phase = 1;
bool bounded = false;
REAL_T min = std::numeric_limits<REAL_T>::min();
REAL_T max = std::numeric_limits<REAL_T>::max();
rapidjson::Document::MemberIterator rit = (*mortality_model).value.FindMember("id");
int model_id = 0;
if (rit == (*mortality_model).value.MemberEnd()) {
std::cout << "Configuration Error: Fishing Mortality is required to have a unique identifier\n";
mas::mas_log << "Configuration Error: Fishing Mortality is required to have a unique identifier\n";
this->valid_configuration = false;
} else {
model_id = (*rit).value.GetInt();
}
model->id = model_id;
rapidjson::Document::MemberIterator pit = (*mortality_model).value.FindMember("parameters");
bool is_random = false;
if (pit == (*mortality_model).value.MemberEnd()) {
std::cout << "Configuration Error: Fishing Mortality model has no parameter definitions.\n";
mas::mas_log << "Configuration Error: Fishing Mortality model has no parameter definitions.\n";
this->valid_configuration = false;
} else {
rapidjson::Document::MemberIterator vit = (*pit).value.FindMember("estimated");
if (vit != (*pit).value.MemberEnd()) {
std::string estring = std::string((*vit).value.GetString());
if (estring == "true") {
estimated = true;
}
}
rapidjson::Document::MemberIterator re_it = (*pit).value.FindMember("random_effect");
if (re_it != (*pit).value.MemberEnd()) {
if (std::string((*re_it).value.GetString()) == "true") {
is_random = true;
}
}
vit = (*pit).value.FindMember("delta_method");
if (vit != (*pit).value.MemberEnd()) {
std::string estring = std::string((*vit).value.GetString());
if (estring == "true") {
delta_method = true;
}
}
vit = (*pit).value.FindMember("min");
if (vit != (*pit).value.MemberEnd()) {
min = static_cast<REAL_T> ((*vit).value.GetDouble());
bounded = true;
}
vit = (*pit).value.FindMember("max");
if (vit != (*pit).value.MemberEnd()) {
max = static_cast<REAL_T> ((*vit).value.GetDouble());
bounded = true;
}
vit = (*pit).value.FindMember("phase");
if (vit != (*pit).value.MemberEnd()) {
phase = (*vit).value.GetInt();
}
vit = (*pit).value.FindMember("value");
if (vit == (*pit).value.MemberEnd()) {
std::cout << "Configuration Error: Fishing Mortality model supplied no values\n";
mas::mas_log << "Configuration Error: Fishing Mortality model supplied no values\n";
this->valid_configuration = false;
} else {
if ((*vit).value.IsArray()) {
rapidjson::Value & v = (*vit).value;
if (v.IsArray()) {
model->fishing_mortality.resize(v.Size());
for (int i = 0; i < v.Size(); i++) {
rapidjson::Value & vv = v[i];
model->fishing_mortality[i].resize(vv.Size());
for (int j = 0; j < vv.Size(); j++) {
mas::VariableTrait<REAL_T>::SetValue(model->fishing_mortality[i][j], static_cast<REAL_T> (vv[j].GetDouble()));
if (estimated && !delta_method) {
if (bounded) {
mas::VariableTrait<REAL_T>::SetMinBoundary(model->fishing_mortality[i][j], min);
mas::VariableTrait<REAL_T>::SetMaxBoundary(model->fishing_mortality[i][j], max);
}
std::stringstream ss;
ss << "fishing_mortality[" << i << "][" << j << "]_" << model_id;
mas::VariableTrait<REAL_T>::SetName(model->fishing_mortality[i][j], ss.str());
if (is_random) {
model->RegisterRandom(model->fishing_mortality[i][j]);
} else {
model->Register(model->fishing_mortality[i][j], phase);
}
} else if (delta_method) {
model->fishing_mortality_type = mas::FMETHOD;
}
}
}
} else {
std::cout << "Configuration Error: Fishing Mortality is requires values in a matrix[year,season] not a vector\n";
std::cout << "Configuration Error: Fishing Mortality is requires values in a matrix[year,season] not a vector\n";
this->valid_configuration = false;
}
} else {
std::cout << "Configuration Error: Fishing Mortality is requires values in a matrix[year,season]\n";
std::cout << "Configuration Error: Fishing Mortality is requires values in a matrix[year,season]\n";
this->valid_configuration = false;
}
}
}
fishing_mortality_model_iterator fit = this->fishing_mortality_models.find(model->id);
if (fit == this->fishing_mortality_models.end()) {
if (delta_method) {
model->fishing_mortality_type = mas::FMETHOD;
}
this->fishing_mortality_models[model->id] = model;
} else {
std::cout << "Configuration Error: More than one fishing mortality model with the same identifier defined. Fishing mortality models require a unique id.\n";
mas_log << "Configuration Error: More than one fishing mortality model with the same identifier defined. Fishing mortality models require a unique id.\n";
this->valid_configuration = false;
}
}
void HandleNaturalMortalityModel(rapidjson::Document::MemberIterator & mortality_model) {
INFO_DEBUG
std::shared_ptr<mas::NaturalMortality<REAL_T> > model(new mas::NaturalMortality<REAL_T>());
bool estimated = false;
int phase = 1;
rapidjson::Document::MemberIterator rit = (*mortality_model).value.FindMember("id");
int model_id = 0;
if (rit == (*mortality_model).value.MemberEnd()) {
std::cout << "Configuration Error: Natural Mortality is required to have a unique identifier\n";
mas::mas_log << "Configuration Error: Natural Mortality is required to have a unique identifier\n";
this->valid_configuration = false;
} else {
model_id = (*rit).value.GetInt();
}
model->id = model_id;
rapidjson::Document::MemberIterator pit = (*mortality_model).value.FindMember("parameters");
rapidjson::Document::MemberIterator mvit = (*pit).value.FindMember("value");
if ((*mvit).value.IsArray()) {
// model->male_mortality.resize((*mvit).value.Size());
rapidjson::Value& v = (*mvit).value;