1 : |
agomez |
1 |
#include "migrationproportion.h" |
2 : |
|
|
#include "readfunc.h" |
3 : |
|
|
#include "readword.h" |
4 : |
|
|
#include "readaggregation.h" |
5 : |
|
|
#include "errorhandler.h" |
6 : |
|
|
#include "areatime.h" |
7 : |
|
|
#include "fleet.h" |
8 : |
|
|
#include "stock.h" |
9 : |
|
|
#include "mathfunc.h" |
10 : |
|
|
#include "stockprey.h" |
11 : |
|
|
#include "gadget.h" |
12 : |
|
|
#include "global.h" |
13 : |
|
|
|
14 : |
|
|
MigrationProportion::MigrationProportion(CommentStream& infile, const AreaClass* const Area,
|
15 : |
|
|
const TimeClass* const TimeInfo, double weight, const char* name)
|
16 : |
|
|
: Likelihood(MIGRATIONPROPORTIONLIKELIHOOD, weight, name) {
|
17 : |
|
|
|
18 : |
|
|
int i, j;
|
19 : |
|
|
char text[MaxStrLength];
|
20 : |
|
|
strncpy(text, "", MaxStrLength);
|
21 : |
|
|
int numarea = 0, numage = 0, numlen = 0;
|
22 : |
|
|
|
23 : |
|
|
char datafilename[MaxStrLength];
|
24 : |
|
|
char aggfilename[MaxStrLength];
|
25 : |
|
|
strncpy(datafilename, "", MaxStrLength);
|
26 : |
|
|
strncpy(aggfilename, "", MaxStrLength);
|
27 : |
|
|
ifstream datafile;
|
28 : |
|
|
CommentStream subdata(datafile);
|
29 : |
|
|
|
30 : |
|
|
timeindex = 0;
|
31 : |
|
|
biomass = 1; // default is to use the biomass to calculate the likelihood score
|
32 : |
|
|
functionname = new char[MaxStrLength];
|
33 : |
|
|
strncpy(functionname, "", MaxStrLength);
|
34 : |
|
|
|
35 : |
|
|
readWordAndValue(infile, "datafile", datafilename);
|
36 : |
|
|
readWordAndValue(infile, "function", functionname);
|
37 : |
|
|
|
38 : |
|
|
functionnumber = 0;
|
39 : |
|
|
if (strcasecmp(functionname, "sumofsquares") == 0) {
|
40 : |
|
|
functionnumber = 1;
|
41 : |
|
|
} else
|
42 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in migrationproportion - unrecognised function", functionname);
|
43 : |
|
|
|
44 : |
|
|
char c = infile.peek();
|
45 : |
|
|
if ((c == 'b') || (c == 'B'))
|
46 : |
|
|
readWordAndVariable(infile, "biomass", biomass);
|
47 : |
|
|
if (biomass != 0 && biomass != 1)
|
48 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in migrationproportion - biomass must be 0 or 1");
|
49 : |
|
|
|
50 : |
|
|
//read in area aggregation from file
|
51 : |
|
|
readWordAndValue(infile, "areaaggfile", aggfilename);
|
52 : |
|
|
datafile.open(aggfilename, ios::in);
|
53 : |
|
|
handle.checkIfFailure(datafile, aggfilename);
|
54 : |
|
|
handle.Open(aggfilename);
|
55 : |
|
|
numarea = readAggregation(subdata, areas, areaindex);
|
56 : |
|
|
handle.Close();
|
57 : |
|
|
datafile.close();
|
58 : |
|
|
datafile.clear();
|
59 : |
|
|
|
60 : |
|
|
//Must change from outer areas to inner areas.
|
61 : |
|
|
for (i = 0; i < areas.Nrow(); i++)
|
62 : |
|
|
for (j = 0; j < areas.Ncol(i); j++)
|
63 : |
|
|
areas[i][j] = Area->getInnerArea(areas[i][j]);
|
64 : |
|
|
if (areaindex.Size() == 0)
|
65 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in migrationproportion - failed to read areas");
|
66 : |
|
|
if (areaindex.Size() == 1)
|
67 : |
|
|
handle.logFileMessage(LOGWARN, "\nWarning in migrationproportion - only read one area");
|
68 : |
|
|
handle.logMessage(LOGMESSAGE, "Read area data - number of areas", areaindex.Size());
|
69 : |
|
|
|
70 : |
|
|
//read in the stocknames
|
71 : |
|
|
i = 0;
|
72 : |
|
|
infile >> text >> ws;
|
73 : |
|
|
if (strcasecmp(text, "stocknames") != 0)
|
74 : |
|
|
handle.logFileUnexpected(LOGFAIL, "stocknames", text);
|
75 : |
|
|
infile >> text;
|
76 : |
|
|
while (!infile.eof() && (strcasecmp(text, "[component]") != 0)) {
|
77 : |
|
|
infile >> ws;
|
78 : |
|
|
stocknames.resize(new char[strlen(text) + 1]);
|
79 : |
|
|
strcpy(stocknames[i++], text);
|
80 : |
|
|
infile >> text;
|
81 : |
|
|
}
|
82 : |
|
|
if (stocknames.Size() == 0)
|
83 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in migrationproportion - failed to read stocks");
|
84 : |
|
|
handle.logMessage(LOGMESSAGE, "Read stock data - number of stocks", stocknames.Size());
|
85 : |
|
|
|
86 : |
|
|
//We have now read in all the data from the main likelihood file
|
87 : |
|
|
//But we have to read in the migration proportion data from datafilename
|
88 : |
|
|
datafile.open(datafilename, ios::in);
|
89 : |
|
|
handle.checkIfFailure(datafile, datafilename);
|
90 : |
|
|
handle.Open(datafilename);
|
91 : |
|
|
readProportionData(subdata, TimeInfo, numarea);
|
92 : |
|
|
handle.Close();
|
93 : |
|
|
datafile.close();
|
94 : |
|
|
datafile.clear();
|
95 : |
|
|
}
|
96 : |
|
|
|
97 : |
|
|
void MigrationProportion::readProportionData(CommentStream& infile,
|
98 : |
|
|
const TimeClass* TimeInfo, int numarea) {
|
99 : |
|
|
|
100 : |
|
|
int i, year, step;
|
101 : |
|
|
double tmpnumber;
|
102 : |
|
|
char tmparea[MaxStrLength];
|
103 : |
|
|
strncpy(tmparea, "", MaxStrLength);
|
104 : |
|
|
int keepdata, timeid, areaid, count, reject;
|
105 : |
|
|
|
106 : |
|
|
//Check the number of columns in the inputfile
|
107 : |
|
|
infile >> ws;
|
108 : |
|
|
if (countColumns(infile) != 4)
|
109 : |
|
|
handle.logFileMessage(LOGFAIL, "wrong number of columns in inputfile - should be 4");
|
110 : |
|
|
|
111 : |
|
|
year = step = count = reject = 0;
|
112 : |
|
|
while (!infile.eof()) {
|
113 : |
|
|
keepdata = 1;
|
114 : |
|
|
infile >> year >> step >> tmparea >> tmpnumber >> ws;
|
115 : |
|
|
|
116 : |
|
|
//crude check to see if something has gone wrong and avoid infinite loops
|
117 : |
|
|
if (strlen(tmparea) == 0)
|
118 : |
|
|
handle.logFileMessage(LOGFAIL, "failed to read data from file");
|
119 : |
|
|
|
120 : |
|
|
//if tmparea is in areaindex find areaid, else dont keep the data
|
121 : |
|
|
areaid = -1;
|
122 : |
|
|
for (i = 0; i < areaindex.Size(); i++)
|
123 : |
|
|
if (strcasecmp(areaindex[i], tmparea) == 0)
|
124 : |
|
|
areaid = i;
|
125 : |
|
|
|
126 : |
|
|
if (areaid == -1)
|
127 : |
|
|
keepdata = 0;
|
128 : |
|
|
|
129 : |
|
|
//check if the year and step are in the simulation
|
130 : |
|
|
timeid = -1;
|
131 : |
|
|
if ((TimeInfo->isWithinPeriod(year, step)) && (keepdata == 1)) {
|
132 : |
|
|
//if this is a new timestep, resize to store the data
|
133 : |
|
|
for (i = 0; i < Years.Size(); i++)
|
134 : |
|
|
if ((Years[i] == year) && (Steps[i] == step))
|
135 : |
|
|
timeid = i;
|
136 : |
|
|
|
137 : |
|
|
if (timeid == -1) {
|
138 : |
|
|
Years.resize(1, year);
|
139 : |
|
|
Steps.resize(1, step);
|
140 : |
|
|
timeid = (Years.Size() - 1);
|
141 : |
|
|
|
142 : |
|
|
obsDistribution.AddRows(1, numarea, 0.0);
|
143 : |
|
|
modelDistribution.AddRows(1, numarea, 0.0);
|
144 : |
|
|
likelihoodValues.resize(1, 0.0);
|
145 : |
|
|
}
|
146 : |
|
|
|
147 : |
|
|
} else
|
148 : |
|
|
keepdata = 0;
|
149 : |
|
|
|
150 : |
|
|
if (keepdata == 1) {
|
151 : |
|
|
//distribution data is required, so store it
|
152 : |
|
|
count++;
|
153 : |
|
|
obsDistribution[timeid][areaid] = tmpnumber;
|
154 : |
|
|
} else
|
155 : |
|
|
reject++; //count number of rejected data points read from file
|
156 : |
|
|
}
|
157 : |
|
|
|
158 : |
|
|
AAT.addActions(Years, Steps, TimeInfo);
|
159 : |
|
|
if (count == 0)
|
160 : |
|
|
handle.logMessage(LOGWARN, "Warning in migrationproportion - found no data in the data file for", this->getName());
|
161 : |
|
|
if (reject != 0)
|
162 : |
|
|
handle.logMessage(LOGMESSAGE, "Discarded invalid migrationproportion data - number of invalid entries", reject);
|
163 : |
|
|
handle.logMessage(LOGMESSAGE, "Read migrationproportion data file - number of entries", count);
|
164 : |
|
|
}
|
165 : |
|
|
|
166 : |
|
|
MigrationProportion::~MigrationProportion() {
|
167 : |
|
|
int i, j;
|
168 : |
|
|
for (i = 0; i < stocknames.Size(); i++)
|
169 : |
|
|
delete[] stocknames[i];
|
170 : |
|
|
for (i = 0; i < areaindex.Size(); i++)
|
171 : |
|
|
delete[] areaindex[i];
|
172 : |
|
|
delete[] functionname;
|
173 : |
|
|
}
|
174 : |
|
|
|
175 : |
|
|
void MigrationProportion::Reset(const Keeper* const keeper) {
|
176 : |
|
|
Likelihood::Reset(keeper);
|
177 : |
|
|
if (isZero(weight))
|
178 : |
|
|
handle.logMessage(LOGWARN, "Warning in migrationproportion - zero weight for", this->getName());
|
179 : |
|
|
|
180 : |
|
|
modelDistribution.setToZero();
|
181 : |
|
|
if (handle.getLogLevel() >= LOGMESSAGE)
|
182 : |
|
|
handle.logMessage(LOGMESSAGE, "Reset migrationproportion component", this->getName());
|
183 : |
|
|
}
|
184 : |
|
|
|
185 : |
|
|
void MigrationProportion::Print(ofstream& outfile) const {
|
186 : |
|
|
int i;
|
187 : |
|
|
outfile << "\nMigration Proportion " << this->getName() << " - likelihood value " << likelihood
|
188 : |
|
|
<< "\n\tFunction " << functionname << "\n\tStock names:";
|
189 : |
|
|
for (i = 0; i < stocknames.Size(); i++)
|
190 : |
|
|
outfile << sep << stocknames[i];
|
191 : |
|
|
outfile << endl;
|
192 : |
|
|
outfile.flush();
|
193 : |
|
|
}
|
194 : |
|
|
|
195 : |
|
|
void MigrationProportion::printLikelihood(ofstream& outfile, const TimeClass* const TimeInfo) {
|
196 : |
|
|
|
197 : |
|
|
if (!AAT.atCurrentTime(TimeInfo))
|
198 : |
|
|
return;
|
199 : |
|
|
|
200 : |
|
|
int i, area;
|
201 : |
|
|
timeindex = -1;
|
202 : |
|
|
for (i = 0; i < Years.Size(); i++)
|
203 : |
|
|
if ((Years[i] == TimeInfo->getYear()) && (Steps[i] == TimeInfo->getStep()))
|
204 : |
|
|
timeindex = i;
|
205 : |
|
|
if (timeindex == -1)
|
206 : |
|
|
handle.logMessage(LOGFAIL, "Error in migrationproportion - invalid timestep");
|
207 : |
|
|
|
208 : |
|
|
for (area = 0; area < modelDistribution.Ncol(timeindex); area++) {
|
209 : |
|
|
outfile << setw(lowwidth) << Years[timeindex] << sep << setw(lowwidth)
|
210 : |
|
|
<< Steps[timeindex] << sep << setw(printwidth) << areaindex[area] << sep;
|
211 : |
|
|
|
212 : |
|
|
//JMB crude filter to remove the 'silly' values from the output
|
213 : |
|
|
if (modelDistribution[timeindex][area] < rathersmall)
|
214 : |
|
|
outfile << setw(largewidth) << 0 << endl;
|
215 : |
|
|
else
|
216 : |
|
|
outfile << setprecision(largeprecision) << setw(largewidth)
|
217 : |
|
|
<< modelDistribution[timeindex][area] << endl;
|
218 : |
|
|
}
|
219 : |
|
|
}
|
220 : |
|
|
|
221 : |
|
|
void MigrationProportion::setFleetsAndStocks(FleetPtrVector& Fleets, StockPtrVector& Stocks) {
|
222 : |
|
|
int i, j, k, found;
|
223 : |
|
|
|
224 : |
|
|
for (i = 0; i < stocknames.Size(); i++) {
|
225 : |
|
|
found = 0;
|
226 : |
|
|
for (j = 0; j < Stocks.Size(); j++) {
|
227 : |
|
|
if (strcasecmp(stocknames[i], Stocks[j]->getName()) == 0) {
|
228 : |
|
|
found++;
|
229 : |
|
|
stocks.resize(Stocks[j]);
|
230 : |
|
|
}
|
231 : |
|
|
}
|
232 : |
|
|
if (found == 0)
|
233 : |
|
|
handle.logMessage(LOGFAIL, "Error in migrationproportion - unrecognised stock", stocknames[i]);
|
234 : |
|
|
}
|
235 : |
|
|
|
236 : |
|
|
for (i = 0; i < stocks.Size(); i++)
|
237 : |
|
|
for (j = 0; j < stocks.Size(); j++)
|
238 : |
|
|
if ((strcasecmp(stocks[i]->getName(), stocks[j]->getName()) == 0) && (i != j))
|
239 : |
|
|
handle.logMessage(LOGFAIL, "Error in migrationproportion - repeated stock", stocks[i]->getName());
|
240 : |
|
|
|
241 : |
|
|
if (handle.getLogLevel() >= LOGWARN) {
|
242 : |
|
|
for (j = 0; j < areas.Nrow(); j++) {
|
243 : |
|
|
found = 0;
|
244 : |
|
|
for (i = 0; i < stocks.Size(); i++)
|
245 : |
|
|
for (k = 0; k < areas.Ncol(j); k++)
|
246 : |
|
|
if (stocks[i]->isInArea(areas[j][k]))
|
247 : |
|
|
found++;
|
248 : |
|
|
if (found == 0)
|
249 : |
|
|
handle.logMessage(LOGWARN, "Warning in migrationproportion - stock not defined on all areas");
|
250 : |
|
|
}
|
251 : |
|
|
}
|
252 : |
|
|
}
|
253 : |
|
|
|
254 : |
|
|
void MigrationProportion::addLikelihood(const TimeClass* const TimeInfo) {
|
255 : |
|
|
|
256 : |
|
|
if ((!(AAT.atCurrentTime(TimeInfo))) || (isZero(weight)))
|
257 : |
|
|
return;
|
258 : |
|
|
|
259 : |
|
|
if (handle.getLogLevel() >= LOGMESSAGE)
|
260 : |
|
|
handle.logMessage(LOGMESSAGE, "Calculating likelihood score for migrationproportion component", this->getName());
|
261 : |
|
|
|
262 : |
|
|
int i;
|
263 : |
|
|
timeindex = -1;
|
264 : |
|
|
for (i = 0; i < Years.Size(); i++)
|
265 : |
|
|
if ((Years[i] == TimeInfo->getYear()) && (Steps[i] == TimeInfo->getStep()))
|
266 : |
|
|
timeindex = i;
|
267 : |
|
|
if (timeindex == -1)
|
268 : |
|
|
handle.logMessage(LOGFAIL, "Error in migrationproportion - invalid timestep");
|
269 : |
|
|
|
270 : |
|
|
double l = 0.0;
|
271 : |
|
|
switch (functionnumber) {
|
272 : |
|
|
case 1:
|
273 : |
|
|
l = calcLikSumSquares(TimeInfo);
|
274 : |
|
|
break;
|
275 : |
|
|
default:
|
276 : |
|
|
handle.logMessage(LOGWARN, "Warning in migrationproportion - unrecognised function", functionname);
|
277 : |
|
|
break;
|
278 : |
|
|
}
|
279 : |
|
|
|
280 : |
|
|
likelihood += l;
|
281 : |
|
|
if (handle.getLogLevel() >= LOGMESSAGE)
|
282 : |
|
|
handle.logMessage(LOGMESSAGE, "The likelihood score for this component on this timestep is", l);
|
283 : |
|
|
}
|
284 : |
|
|
|
285 : |
|
|
double MigrationProportion::calcLikSumSquares(const TimeClass* const TimeInfo) {
|
286 : |
|
|
|
287 : |
|
|
double temp, totalmodel, totaldata;
|
288 : |
|
|
int a, r, s;
|
289 : |
|
|
|
290 : |
|
|
if (biomass) {
|
291 : |
|
|
for (a = 0; a < areas.Nrow(); a++)
|
292 : |
|
|
for (r = 0; r < areas.Ncol(a); r++)
|
293 : |
|
|
for (s = 0; s < stocks.Size(); s++)
|
294 : |
|
|
modelDistribution[timeindex][a] += stocks[s]->getTotalStockBiomass(areas[a][r]);
|
295 : |
|
|
} else {
|
296 : |
|
|
for (a = 0; a < areas.Nrow(); a++)
|
297 : |
|
|
for (r = 0; r < areas.Ncol(a); r++)
|
298 : |
|
|
for (s = 0; s < stocks.Size(); s++)
|
299 : |
|
|
modelDistribution[timeindex][a] += stocks[s]->getTotalStockNumber(areas[a][r]);
|
300 : |
|
|
}
|
301 : |
|
|
|
302 : |
|
|
totalmodel = 0.0;
|
303 : |
|
|
totaldata = 0.0;
|
304 : |
|
|
for (a = 0; a < areas.Nrow(); a++) {
|
305 : |
|
|
totalmodel += modelDistribution[timeindex][a];
|
306 : |
|
|
totaldata += obsDistribution[timeindex][a];
|
307 : |
|
|
}
|
308 : |
|
|
if (!(isZero(totalmodel)))
|
309 : |
|
|
totalmodel = 1.0 / totalmodel;
|
310 : |
|
|
if (!(isZero(totaldata)))
|
311 : |
|
|
totaldata = 1.0 / totaldata;
|
312 : |
|
|
|
313 : |
|
|
likelihoodValues[timeindex] = 0.0;
|
314 : |
|
|
for (a = 0; a < areas.Nrow(); a++) {
|
315 : |
|
|
temp = ((obsDistribution[timeindex][a] * totaldata)
|
316 : |
|
|
- (modelDistribution[timeindex][a] * totalmodel));
|
317 : |
|
|
likelihoodValues[timeindex] += (temp * temp);
|
318 : |
|
|
}
|
319 : |
|
|
|
320 : |
|
|
return likelihoodValues[timeindex];
|
321 : |
|
|
}
|
322 : |
|
|
|
323 : |
|
|
void MigrationProportion::printSummary(ofstream& outfile) {
|
324 : |
|
|
int year;
|
325 : |
|
|
|
326 : |
|
|
for (year = 0; year < likelihoodValues.Size(); year++) {
|
327 : |
|
|
outfile << setw(lowwidth) << Years[year] << sep << setw(lowwidth) << Steps[year] << " all "
|
328 : |
|
|
<< setw(largewidth) << this->getName() << sep << setprecision(smallprecision)
|
329 : |
|
|
<< setw(smallwidth) << weight << sep << setprecision(largeprecision)
|
330 : |
|
|
<< setw(largewidth) << likelihoodValues[year] << endl;
|
331 : |
|
|
}
|
332 : |
|
|
outfile.flush();
|
333 : |
|
|
}
|