forked from amart/MAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAS.hpp
286 lines (223 loc) · 10.2 KB
/
MAS.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
/*
* File: MAS.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_MAS_HPP
#define MAS_MAS_HPP
#include "Information.hpp"
namespace mas {
template<typename REAL_T>
class MAS {
typedef typename VariableTrait<REAL_T>::variable variable;
std::unordered_map<int, mas::Population<REAL_T> > populations;
std::string data_file;
std::string config_file;
//totals
std::vector<variable> N;
std::vector<variable> N_Proportions;
std::vector<variable> C;
std::vector<variable> C_Biomass;
std::vector<variable> C_Proportions;
std::vector<variable> SurveyNumbers;
std::vector<variable> Survey_Biomass;
std::vector<variable> Survey_Proportions;
int calls = 0;
variable catch_biomass_component;
variable survey_biomass_component;
variable fishery_age_comp_component;
variable survey_age_comp_component;
variable recruitment_deviations_component;
public:
mas::Information< REAL_T> info;
MAS() {
}
MAS(const MAS<REAL_T>& orig) {
}
virtual ~MAS() {
}
void Initialize(const std::string& config_file, const std::string& data_file) {
info.ParseConfig(config_file);
info.ParseData(data_file);
info.CreateModel();
info.ShowData();
// exit(0);
}
inline void Run(variable& f) {
// std::cout<<"Calls "<<calls++<<std::endl;
f = 0.0;
this->catch_biomass_component = 0.0;
this->survey_biomass_component = 0.0;
this->fishery_age_comp_component = 0.0;
this->survey_age_comp_component = 0.0;
typename mas::Information<REAL_T>::fleet_iterator fit;
typename std::unordered_map<int, std::shared_ptr<mas::Population<double> > >::iterator it;
std::unordered_map<int, std::shared_ptr<mas::Population<double> > >& pops =
info.GetPopulations();
mas::Information<double>::area_iterator ait;
/**
* Prepare areas for evaluation. Resets runtime information.
*/
for (ait = info.areas.begin(); ait != info.areas.end(); ait++) {
(*ait).second->Prepare();
}
/**
* Prepare Populations for evaluation. Resets runtime
* information.
*/
for (it = pops.begin(); it != pops.end(); ++it) {
(*it).second->Prepare();
}
for (int i = 0; i < info.survey_models.size(); i++) {
info.survey_models[i]->Prepare();
}
for (fit = info.fleets.begin(); fit != info.fleets.end(); ++fit) {
(*fit).second->Prepare();
}
/**
* Evaluate each population and push final numbers to
* Area objects.
*/
for (it = pops.begin(); it != pops.end(); ++it) {
(*it).second->Evaluate();
}
/**
* Loop through each area and compute proportions for catch, surveys,
* and numbers.
*/
// for (ait = info.areas.begin(); ait != info.areas.end(); ait++) {
// (*ait).second->ComputeProportions();
// }
for (fit = info.fleets.begin(); fit != info.fleets.end(); ++fit) {
(*fit).second->ComputeProportions();
}
for (int i = 0; i < info.survey_models.size(); i++) {
info.survey_models[i]->ComputeProportions();
}
// for (int y = 0; y < info.nyears; y++) {
// for (int s = 0; s < info.nseasons; s++) {
// for (fit = info.fleets.begin(); fit != info.fleets.end(); ++fit) {
// (*fit).second->EvaluateBiomassComponent(y, s);
// (*fit).second->EvaluateAgeCompComponent(y, s);
// }
// for (int i = 0; i < info.survey_models.size(); i++) {
// info.survey_models[i]->EvaluateBiomassComponent(y, s);
// info.survey_models[i]->EvaluateAgeCompComponent(y, s);
// }
// }
// }
for (fit = info.fleets.begin(); fit != info.fleets.end(); ++fit) {
(*fit).second->ComputeNLLComponents();
this->catch_biomass_component += (*fit).second->catch_biomass_component;
this->fishery_age_comp_component -= (*fit).second->fishery_age_comp_component;
}
for (int i = 0; i < info.survey_models.size(); i++) {
info.survey_models[i]->ComputeNLLComponents();
this->survey_age_comp_component += info.survey_models[i]->survey_age_comp_component;
this->survey_biomass_component -= info.survey_models[i]->survey_biomass_component;
}
f = this->survey_age_comp_component + this->survey_biomass_component + this->fishery_age_comp_component + this->catch_biomass_component;
// std::cout << "f = " << this->survey_age_comp_component << " + "
// << this->survey_biomass_component << " + " << catch_biomass_component << " + " <<
// fishery_age_comp_component << "= " << f << std::endl;
// exit(0);
}
void Forecast() {
}
void Report() {
std::ofstream out("mas_report.txt");
out << std::fixed;
out<<"Estimated Parameters:\n";
out << std::setw(30) << std::left << "Name" << std::setw(25) << "Value" << " " << std::setw(25) << "Gradient" << "\n";
for (int i = 0; i < this->info.estimated_parameters.size(); i++) {
out << std::setw(30) << std::left << this->info.estimated_parameters[i]->GetName() << std::setw(25) << this->info.estimated_parameters[i]->info->vvalue << " " << std::setw(25) << this->info.estimated_parameters[i]->info->dvalue << "\n";
}
out<<"\n\n";
typename mas::Information<REAL_T>::fleet_iterator fit;
typename mas::Information<REAL_T>::recruitment_model_iterator rit;
typename std::unordered_map<int, std::shared_ptr<mas::Population<double> > >::iterator it;
std::unordered_map<int, std::shared_ptr<mas::Population<double> > >& pops =
info.GetPopulations();
mas::Information<double>::area_iterator ait;
//prepare the recruitment deviations.
for (rit = info.recruitment_models.begin(); rit != info.recruitment_models.end(); ++rit) {
(*rit).second->Prepare();
}
/**
* Prepare areas for evaluation. Resets runtime information.
*/
for (ait = info.areas.begin(); ait != info.areas.end(); ait++) {
(*ait).second->Prepare();
}
/**
* Prepare Populations for evaluation. Resets runtime
* information.
*/
for (it = pops.begin(); it != pops.end(); ++it) {
(*it).second->Prepare();
}
for (int i = 0; i < info.survey_models.size(); i++) {
info.survey_models[i]->Prepare();
}
for (fit = info.fleets.begin(); fit != info.fleets.end(); ++fit) {
(*fit).second->Prepare();
}
/**
* Evaluate each population and push final numbers to
* Area objects.
*/
for (it = pops.begin(); it != pops.end(); ++it) {
(*it).second->Evaluate();
}
/**
* Loop through each area and compute proportions for catch, surveys,
* and numbers.
*/
for (ait = info.areas.begin(); ait != info.areas.end(); ait++) {
(*ait).second->ComputeProportions();
out << *(*ait).second;
}
for (fit = info.fleets.begin(); fit != info.fleets.end(); ++fit) {
(*fit).second->ComputeProportions();
out << *(*fit).second;
}
for (int i = 0; i < info.survey_models.size(); i++) {
info.survey_models[i]->ComputeProportions();
out << *info.survey_models[i];
}
std::ofstream out2("populations.txt");
out2 << std::fixed;
for (it = pops.begin(); it != pops.end(); ++it) {
out2 << *(*it).second << "\n";
}
}
private:
};
}
#endif /* MAS_MAS_HPP */