forked from amart/MAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.hpp
More file actions
285 lines (230 loc) · 7.91 KB
/
Copy pathCommon.hpp
File metadata and controls
285 lines (230 loc) · 7.91 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Common.hpp
* Author: matthewsupernaw
*
* Created on September 16, 2016, 12:42 PM
*/
#ifndef COMMON_HPP
#define COMMON_HPP
#include "third_party/ATL/AutoDiff/AutoDiff.hpp"
#include <vector>
#include <map>
namespace mas {
std::ofstream mas_log("mas.log");
template<typename REAL_T>
struct VariableTrait {
typedef atl::Variable<REAL_T> variable;
static void SetName(variable& var, const std::string& value) {
var.SetName(value);
}
static void SetValue(variable& var, const REAL_T& value) {
var.SetValue(value);
}
static void SetMinBoundary(variable& var, const REAL_T& value) {
var.SetMinBoundary(value);
}
static void SetMaxBoundary(variable& var, const REAL_T& value) {
var.SetMaxBoundary(value);
}
};
template<typename REAL_T>
struct ModelObject {
typedef typename VariableTrait<REAL_T>::variable variable;
int id;
std::map<variable*, int> estimated_parameters_map;
typedef typename std::map<variable*, int>::iterator estimable_parameter_iterator;
std::vector<variable*> estimated_parameters;
std::vector<int> estimated_phase;
void Register(variable& var, int phase = 1) {
estimated_parameters_map[&var] = phase;
this->estimated_parameters.push_back(&var);
this->estimated_phase.push_back(phase);
}
virtual std::string ToString() {
return "ModelBase";
}
};
template<typename T>
std::ostream& operator<<(std::ostream& out, const ModelObject<T>& model) {
out << model.ToString();
return out;
}
enum DataUnits {
MT = 0,
KG,
LBS,
IT,
NUMBERS,
NA
};
enum DataObjectType {
CATCH_BIOMASS = 0,
CATCH_PROPORTION_AT_AGE,
CATCH_PROPORTION_AT_LENGTH,
CATCH_MEAN_SIZE_AT_AGE,
SURVEY_BIOMASS,
SURVEY_PROPORTION_AT_AGE,
SURVEY_PROPORTION_AT_LENGTH,
SURVEY_MEAN_SIZE_AT_AGE,
UNKNOWN
};
enum FishSexType {
MALE = 0,
FEMALE,
UNDIFFERENTIATED
};
template<typename REAL_T>
struct DataObject {
std::vector<REAL_T> data;
DataObjectType type;
FishSexType sex_type;
DataUnits units;
std::string name;
REAL_T missing_value;
uint32_t id;
uint32_t area_id;
uint32_t population_id;
uint32_t dimensions;
size_t imax = 1;
size_t jmax = 1;
size_t kmax = 1;
size_t lmax = 1;
size_t mmax = 1;
size_t nmax = 1;
inline REAL_T& get(int i) {
return data[i];
}
inline REAL_T& get(int i, int j) {
return data[i * jmax + j];
}
inline REAL_T& get(int i, int j, int k) {
return data[i * jmax * kmax + j * kmax + k];
}
inline REAL_T& get(int i, int j, int k, int l) {
return data[i * jmax * kmax * lmax + j * kmax * lmax + k * lmax + l];
}
inline REAL_T& get(int i, int j, int k, int l, int m) {
return data[i * jmax * kmax * lmax * mmax + j * kmax * lmax * mmax + k * lmax * mmax + l * mmax + m];
}
inline REAL_T& get(int i, int j, int k, int l, int m, int n) {
return data[i * jmax * kmax * lmax * mmax * nmax + j * kmax * lmax * mmax * nmax + k * lmax * mmax * nmax + l * mmax * nmax + m * nmax + m];
}
static DataUnits GetUnits(const std::string& str) {
if (str == "MT") {
return mas::MT;
} else if (str == "KG") {
return mas::KG;
} else if (str == "LBS") {
return mas::LBS;
} else if (str == "NUMBERS") {
return mas::NUMBERS;
} else {
return mas::NA;
}
}
static FishSexType GetSex(const std::string& str) {
if (str == "male") {
return mas::MALE;
} else if (str == "female") {
return mas::FEMALE;
} else if (str == "undifferentiated") {
return mas::UNDIFFERENTIATED;
} else {
return mas::UNDIFFERENTIATED;
}
}
static DataObjectType GetType(const std::string& str) {
if (str == "catch_biomass") {
return CATCH_BIOMASS;
} else if (str == "catch_proportion_at_age") {
return CATCH_PROPORTION_AT_AGE;
} else if (str == "catch_proportion_at_length") {
return CATCH_PROPORTION_AT_LENGTH;
} else if (str == "catch_mean_size_at_age") {
return CATCH_MEAN_SIZE_AT_AGE;
} else if (str == "survey_biomass") {
return SURVEY_BIOMASS;
} else if (str == "survey_proportion_at_age") {
return SURVEY_PROPORTION_AT_AGE;
} else if (str == "survey_proportion_at_length") {
return SURVEY_PROPORTION_AT_LENGTH;
} else if (str == "survey_mean_size_at_age") {
return SURVEY_MEAN_SIZE_AT_AGE;
} else {
std::cout << "Data Error: unknown data_object_type \"" << str << "\"";
}
return UNKNOWN;
}
};
template<typename T>
std::ostream& operator<<(std::ostream& out, mas::DataObject<T>& data_object) {
switch (data_object.type) {
case CATCH_BIOMASS:
out << "Catch Biomass:";
break;
case CATCH_PROPORTION_AT_AGE:
out << "Catch Proportion at Age:";
break;
case CATCH_PROPORTION_AT_LENGTH:
out << "Catch Proportion at Length:";
break;
case CATCH_MEAN_SIZE_AT_AGE:
out << "Catch Mean Size at Age:";
break;
case SURVEY_BIOMASS:
out << "Survey Biomass";
break;
case SURVEY_PROPORTION_AT_AGE:
out << "Survey Proportion at Age:";
break;
case SURVEY_PROPORTION_AT_LENGTH:
out << "Survey Proportion at Length:";
break;
case SURVEY_MEAN_SIZE_AT_AGE:
out << "Survey Mean Size at Age:";
break;
}
out << "Area: " << data_object.area_id << "\n";
out << "Population: " << data_object.population_id << "\n";
switch (data_object.sex_type) {
case MALE:
out << "Male\n";
break;
case FEMALE:
out << "Female\n";
break;
case UNDIFFERENTIATED:
out << "Undifferentiated\n";
break;
}
out << "Dimensions: " << data_object.dimensions << "\n";
for (int i = 0; i < data_object.imax; i++) {
for (int j = 0; j < data_object.jmax; j++) {
for (int k = 0; k < data_object.kmax; k++) {
for (int l = 0; l < data_object.lmax; l++) {
for (int m = 0; m < data_object.mmax; m++) {
for (int n = 0; n < data_object.nmax; n++) {
out <<
data_object.get(i,j,k,l,m,n)<<" ";
}
}
}
}
}
out<<"\n";
}
return out;
}
template <typename T>
T StringToNumber(const std::string &Text) {
std::istringstream ss(Text);
T result;
return (ss >> result) ? result : 0;
}
}
#endif /* COMMON_HPP */