1 : |
agomez |
1 |
#include "understocking.h" |
2 : |
|
|
#include "readword.h" |
3 : |
|
|
#include "readaggregation.h" |
4 : |
|
|
#include "errorhandler.h" |
5 : |
|
|
#include "areatime.h" |
6 : |
|
|
#include "predator.h" |
7 : |
|
|
#include "gadget.h" |
8 : |
|
|
#include "global.h" |
9 : |
|
|
|
10 : |
|
|
UnderStocking::UnderStocking(CommentStream& infile, const AreaClass* const Area,
|
11 : |
|
|
const TimeClass* const TimeInfo, double weight, const char* name)
|
12 : |
|
|
: Likelihood(UNDERSTOCKINGLIKELIHOOD, weight, name) {
|
13 : |
|
|
|
14 : |
|
|
//set the default values
|
15 : |
|
|
powercoeff = 2.0;
|
16 : |
|
|
allpredators = 1;
|
17 : |
|
|
|
18 : |
|
|
infile >> ws;
|
19 : |
|
|
if (infile.eof())
|
20 : |
|
|
return; //JMB - OK, we're done here
|
21 : |
|
|
|
22 : |
|
|
char text[MaxStrLength];
|
23 : |
|
|
strncpy(text, "", MaxStrLength);
|
24 : |
|
|
infile >> text >> ws;
|
25 : |
|
|
//JMB - removed the need to read in the area aggregation file
|
26 : |
|
|
if (strcasecmp(text, "areaaggfile") == 0) {
|
27 : |
|
|
infile >> text >> ws;
|
28 : |
|
|
handle.logMessage(LOGWARN, "Warning in understocking - area aggregation file ignored");
|
29 : |
|
|
infile >> text >> ws;
|
30 : |
|
|
}
|
31 : |
|
|
|
32 : |
|
|
if (strcasecmp(text, "powercoeff") == 0)
|
33 : |
|
|
infile >> powercoeff >> ws >> text >> ws;
|
34 : |
|
|
if (isZero(powercoeff))
|
35 : |
|
|
handle.logMessage(LOGWARN, "Warning in understocking - power coefficient set to zero");
|
36 : |
|
|
|
37 : |
|
|
//reading the predator names is optional - default to all predators
|
38 : |
|
|
if ((strcasecmp(text, "predatornames") == 0) || (strcasecmp(text, "fleetnames") == 0)) {
|
39 : |
|
|
allpredators = 0;
|
40 : |
|
|
int i = 0;
|
41 : |
|
|
infile >> text >> ws;
|
42 : |
|
|
while (!infile.eof() && (strcasecmp(text, "[component]") != 0) && (strcasecmp(text, "yearsandsteps") != 0)) {
|
43 : |
|
|
prednames.resize(new char[strlen(text) + 1]);
|
44 : |
|
|
strcpy(prednames[i++], text);
|
45 : |
|
|
infile >> text >> ws;
|
46 : |
|
|
}
|
47 : |
|
|
|
48 : |
|
|
if (prednames.Size() == 0)
|
49 : |
|
|
handle.logFileMessage(LOGFAIL, "\nError in understocking - failed to read predators");
|
50 : |
|
|
handle.logMessage(LOGMESSAGE, "Read predator data - number of predators", prednames.Size());
|
51 : |
|
|
}
|
52 : |
|
|
|
53 : |
|
|
//JMB - removed the need to read in the yearsandsteps
|
54 : |
|
|
if (strcasecmp(text, "yearsandsteps") == 0) {
|
55 : |
|
|
handle.logMessage(LOGWARN, "Warning in understocking - yearsandsteps data ignored");
|
56 : |
|
|
infile >> text >> ws;
|
57 : |
|
|
while (!infile.eof() && (strcasecmp(text, "[component]") != 0))
|
58 : |
|
|
infile >> text >> ws;
|
59 : |
|
|
}
|
60 : |
|
|
|
61 : |
|
|
//prepare for next likelihood component
|
62 : |
|
|
if (!infile.eof() && (strcasecmp(text, "[component]") != 0))
|
63 : |
|
|
handle.logFileUnexpected(LOGFAIL, "[component]", text);
|
64 : |
|
|
}
|
65 : |
|
|
|
66 : |
|
|
UnderStocking::~UnderStocking() {
|
67 : |
|
|
int i;
|
68 : |
|
|
for (i = 0; i < prednames.Size(); i++)
|
69 : |
|
|
delete[] prednames[i];
|
70 : |
|
|
}
|
71 : |
|
|
|
72 : |
|
|
void UnderStocking::setPredatorsAndPreys(PredatorPtrVector& predvec,
|
73 : |
|
|
PreyPtrVector& preyvec, const AreaClass* const Area) {
|
74 : |
|
|
|
75 : |
|
|
int i, j, found;
|
76 : |
|
|
|
77 : |
|
|
if (allpredators) {
|
78 : |
|
|
//store all the preys
|
79 : |
|
|
for (i = 0; i < preyvec.Size(); i++)
|
80 : |
|
|
preys.resize(preyvec[i]);
|
81 : |
|
|
|
82 : |
|
|
} else {
|
83 : |
|
|
//store the predators that have been specified
|
84 : |
|
|
for (i = 0; i < prednames.Size(); i++) {
|
85 : |
|
|
found = 0;
|
86 : |
|
|
for (j = 0; j < predvec.Size(); j++) {
|
87 : |
|
|
if (strcasecmp(prednames[i], predvec[j]->getName()) == 0) {
|
88 : |
|
|
found ++;
|
89 : |
|
|
predators.resize(predvec[j]);
|
90 : |
|
|
}
|
91 : |
|
|
}
|
92 : |
|
|
if (found == 0)
|
93 : |
|
|
handle.logMessage(LOGFAIL, "Error in understocking - unrecognised predator", prednames[i]);
|
94 : |
|
|
}
|
95 : |
|
|
|
96 : |
|
|
for (i = 0; i < predators.Size(); i++)
|
97 : |
|
|
for (j = 0; j < predators.Size(); j++)
|
98 : |
|
|
if ((strcasecmp(predators[i]->getName(), predators[j]->getName()) == 0) && (i != j))
|
99 : |
|
|
handle.logMessage(LOGFAIL, "Error in understocking - repeated predator", predators[i]->getName());
|
100 : |
|
|
}
|
101 : |
|
|
|
102 : |
|
|
//store a list of the areas in the model
|
103 : |
|
|
areas = Area->getAllModelAreas();
|
104 : |
|
|
for (i = 0; i < areas.Size(); i++)
|
105 : |
|
|
areas[i] = Area->getInnerArea(areas[i]);
|
106 : |
|
|
}
|
107 : |
|
|
|
108 : |
|
|
void UnderStocking::Reset(const Keeper* const keeper) {
|
109 : |
|
|
Likelihood::Reset(keeper);
|
110 : |
|
|
if (isZero(weight))
|
111 : |
|
|
handle.logMessage(LOGWARN, "Warning in understocking - zero weight for", this->getName());
|
112 : |
|
|
Years.Reset();
|
113 : |
|
|
Steps.Reset();
|
114 : |
|
|
likelihoodValues.Reset();
|
115 : |
|
|
if (handle.getLogLevel() >= LOGMESSAGE)
|
116 : |
|
|
handle.logMessage(LOGMESSAGE, "Reset understocking component", this->getName());
|
117 : |
|
|
}
|
118 : |
|
|
|
119 : |
|
|
void UnderStocking::addLikelihood(const TimeClass* const TimeInfo) {
|
120 : |
|
|
|
121 : |
|
|
if (isZero(weight))
|
122 : |
|
|
return;
|
123 : |
|
|
|
124 : |
|
|
int i, j;
|
125 : |
|
|
double err, l;
|
126 : |
|
|
|
127 : |
|
|
if (handle.getLogLevel() >= LOGMESSAGE)
|
128 : |
|
|
handle.logMessage(LOGMESSAGE, "Checking understocking likelihood component", this->getName());
|
129 : |
|
|
|
130 : |
|
|
l = 0.0;
|
131 : |
|
|
for (j = 0; j < areas.Size(); j++) {
|
132 : |
|
|
err = 0.0;
|
133 : |
|
|
|
134 : |
|
|
if (allpredators) {
|
135 : |
|
|
for (i = 0; i < preys.Size(); i++)
|
136 : |
|
|
if (preys[i]->isOverConsumption(areas[j]))
|
137 : |
|
|
err += preys[i]->getTotalOverConsumption(areas[j]);
|
138 : |
|
|
} else {
|
139 : |
|
|
for (i = 0; i < predators.Size(); i++)
|
140 : |
|
|
if (predators[i]->hasOverConsumption(areas[j]))
|
141 : |
|
|
err += predators[i]->getTotalOverConsumption(areas[j]);
|
142 : |
|
|
}
|
143 : |
|
|
|
144 : |
|
|
if (!(isZero(err)))
|
145 : |
|
|
l += pow(err, powercoeff);
|
146 : |
|
|
}
|
147 : |
|
|
|
148 : |
|
|
if (!(isZero(l))) {
|
149 : |
|
|
likelihoodValues.resize(1, l);
|
150 : |
|
|
Years.resize(1, TimeInfo->getYear());
|
151 : |
|
|
Steps.resize(1, TimeInfo->getStep());
|
152 : |
|
|
if (handle.getLogLevel() >= LOGMESSAGE)
|
153 : |
|
|
handle.logMessage(LOGMESSAGE, "The likelihood score for this component on this timestep is", l);
|
154 : |
|
|
likelihood += l;
|
155 : |
|
|
}
|
156 : |
|
|
}
|
157 : |
|
|
|
158 : |
|
|
void UnderStocking::Print(ofstream& outfile) const {
|
159 : |
|
|
int i;
|
160 : |
|
|
outfile << "\nUnderstocking " << this->getName() << " - likelihood value "
|
161 : |
|
|
<< likelihood << endl;
|
162 : |
|
|
|
163 : |
|
|
if (!allpredators) {
|
164 : |
|
|
outfile << "Checking understocking caused by the following predators consumption of preys" << endl;
|
165 : |
|
|
for (i = 0; i < prednames.Size(); i++)
|
166 : |
|
|
outfile << TAB << prednames[i];
|
167 : |
|
|
}
|
168 : |
|
|
|
169 : |
|
|
for (i = 0; i < likelihoodValues.Size(); i++)
|
170 : |
|
|
outfile << "\n\tYear " << Years[i] << " and step " << Steps[i] << " likelihood score "
|
171 : |
|
|
<< setw(smallwidth) << setprecision(smallprecision) << likelihoodValues[i] << endl;
|
172 : |
|
|
outfile.flush();
|
173 : |
|
|
}
|
174 : |
|
|
|
175 : |
|
|
void UnderStocking::printSummary(ofstream& outfile) {
|
176 : |
|
|
int i;
|
177 : |
|
|
for (i = 0; i < likelihoodValues.Size(); i++)
|
178 : |
|
|
outfile << setw(lowwidth) << Years[i] << sep << setw(lowwidth) << Steps[i]
|
179 : |
|
|
<< " all " << this->getName() << sep << setprecision(smallprecision)
|
180 : |
|
|
<< setw(smallwidth) << weight << sep << setprecision(largeprecision)
|
181 : |
|
|
<< setw(largewidth) << likelihoodValues[i] << endl;
|
182 : |
|
|
outfile.flush();
|
183 : |
|
|
}
|