1 : |
agomez |
1 |
#include "migrationpenalty.h" |
2 : |
|
|
#include "errorhandler.h" |
3 : |
|
|
#include "readword.h" |
4 : |
|
|
#include "gadget.h" |
5 : |
|
|
#include "global.h" |
6 : |
|
|
|
7 : |
|
|
MigrationPenalty::MigrationPenalty(CommentStream& infile, double weight, const char* name)
|
8 : |
|
|
: Likelihood(MIGRATIONPENALTYLIKELIHOOD, weight, name), powercoeffs(2) {
|
9 : |
|
|
|
10 : |
|
|
char text[MaxStrLength];
|
11 : |
|
|
stockname = new char[MaxStrLength];
|
12 : |
|
|
strncpy(text, "", MaxStrLength);
|
13 : |
|
|
strncpy(stockname, "", MaxStrLength);
|
14 : |
|
|
|
15 : |
|
|
readWordAndValue(infile, "stockname", stockname);
|
16 : |
|
|
readWordAndTwoVariables(infile, "powercoeffs", powercoeffs[0], powercoeffs[1]);
|
17 : |
|
|
|
18 : |
|
|
//prepare for next likelihood component
|
19 : |
|
|
infile >> ws;
|
20 : |
|
|
if (!infile.eof()) {
|
21 : |
|
|
infile >> text >> ws;
|
22 : |
|
|
if (strcasecmp(text, "[component]") != 0)
|
23 : |
|
|
handle.logFileUnexpected(LOGFAIL, "[component]", text);
|
24 : |
|
|
}
|
25 : |
|
|
}
|
26 : |
|
|
|
27 : |
|
|
void MigrationPenalty::addLikelihood(const TimeClass* const TimeInfo) {
|
28 : |
|
|
if (TimeInfo->getTime() != TimeInfo->numTotalSteps())
|
29 : |
|
|
return;
|
30 : |
|
|
|
31 : |
|
|
if (handle.getLogLevel() >= LOGMESSAGE)
|
32 : |
|
|
handle.logMessage(LOGMESSAGE, "Calculating likelihood score for migrationpenalty component", this->getName());
|
33 : |
|
|
|
34 : |
|
|
int i;
|
35 : |
|
|
double l = 0.0;
|
36 : |
|
|
if (stock->doesMigrate()) {
|
37 : |
|
|
DoubleVector penalty(stock->getMigration()->getPenalty());
|
38 : |
|
|
for (i = 0; i < penalty.Size(); i++)
|
39 : |
|
|
l += pow(penalty[i], powercoeffs[0]);
|
40 : |
|
|
l = pow(l, powercoeffs[1]);
|
41 : |
|
|
likelihood += l;
|
42 : |
|
|
}
|
43 : |
|
|
if (handle.getLogLevel() >= LOGMESSAGE)
|
44 : |
|
|
handle.logMessage(LOGMESSAGE, "The likelihood score for this component on this timestep is", l);
|
45 : |
|
|
}
|
46 : |
|
|
|
47 : |
|
|
void MigrationPenalty::setFleetsAndStocks(FleetPtrVector& Fleets, StockPtrVector& Stocks) {
|
48 : |
|
|
int i;
|
49 : |
|
|
int found = 0;
|
50 : |
|
|
for (i = 0; i < Stocks.Size(); i++)
|
51 : |
|
|
if (strcasecmp(stockname, Stocks[i]->getName()) == 0) {
|
52 : |
|
|
found++;
|
53 : |
|
|
stock = Stocks[i];
|
54 : |
|
|
}
|
55 : |
|
|
|
56 : |
|
|
if (found != 1)
|
57 : |
|
|
handle.logMessage(LOGFAIL, "Error in migrationpenalty - failed to match stock", stockname);
|
58 : |
|
|
if (!(stock->doesMigrate()))
|
59 : |
|
|
handle.logMessage(LOGWARN, "Warning in migrationpenalty - stock doesnt migrate");
|
60 : |
|
|
}
|
61 : |
|
|
|
62 : |
|
|
MigrationPenalty::~MigrationPenalty() {
|
63 : |
|
|
delete[] stockname;
|
64 : |
|
|
}
|
65 : |
|
|
|
66 : |
|
|
void MigrationPenalty::Reset(const Keeper* const keeper) {
|
67 : |
|
|
Likelihood::Reset(keeper);
|
68 : |
|
|
if (handle.getLogLevel() >= LOGMESSAGE)
|
69 : |
|
|
handle.logMessage(LOGMESSAGE, "Reset migrationpenalty component");
|
70 : |
|
|
}
|
71 : |
|
|
|
72 : |
|
|
void MigrationPenalty::printSummary(ofstream& outfile) {
|
73 : |
|
|
//JMB there is only one likelihood score here ...
|
74 : |
|
|
if (!(isZero(likelihood))) {
|
75 : |
|
|
outfile << "all all all" << sep << setw(largewidth) << this->getName() << sep
|
76 : |
|
|
<< setprecision(smallprecision) << setw(smallwidth) << weight << sep
|
77 : |
|
|
<< setprecision(largeprecision) << setw(largewidth) << likelihood << endl;
|
78 : |
|
|
outfile.flush();
|
79 : |
|
|
}
|
80 : |
|
|
}
|
81 : |
|
|
|
82 : |
|
|
void MigrationPenalty::Print(ofstream& outfile) const {
|
83 : |
|
|
outfile << "\nMigration Penalty " << this->getName() << " - likelihood value "
|
84 : |
|
|
<< likelihood << endl;
|
85 : |
|
|
outfile.flush();
|
86 : |
|
|
}
|