Log In | Get Help   
Home My Page Projects Code Snippets Project Openings Mareframe
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files
[mareframe] Annotation of /trunk/gadget/boundlikelihood.cc
[mareframe] / trunk / gadget / boundlikelihood.cc Repository:
ViewVC logotype

Annotation of /trunk/gadget/boundlikelihood.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "boundlikelihood.h"
2 :     #include "readfunc.h"
3 :     #include "readword.h"
4 :     #include "areatime.h"
5 :     #include "keeper.h"
6 :     #include "errorhandler.h"
7 :     #include "gadget.h"
8 :     #include "global.h"
9 :    
10 :     BoundLikelihood::BoundLikelihood(CommentStream& infile, const AreaClass* const Area,
11 :     const TimeClass* const TimeInfo, const Keeper* const keeper, double weight, const char* name)
12 :     : Likelihood(BOUNDLIKELIHOOD, weight, name) {
13 :    
14 :     int i, j;
15 :     Parameter tempParam;
16 :     double temp;
17 :     int count = 0;
18 :     //set flag to initialise the bounds - called in Reset
19 :     checkInitialised = 0;
20 :    
21 :     infile >> ws;
22 :     if (countColumns(infile) != 4)
23 :     handle.logFileMessage(LOGFAIL, "wrong number of columns in inputfile - should be 4");
24 :     while (!infile.eof()) {
25 :     infile >> tempParam >> ws;
26 :     if (strcasecmp(tempParam.getName(), "default") == 0) {
27 :     infile >> defPower >> defLW >> defUW >> ws;
28 :     count++;
29 :    
30 :     } else {
31 :     switches.resize(tempParam);
32 :     infile >> temp >> ws;
33 :     powers.resize(1, temp);
34 :     infile >> temp >> ws;
35 :     lowerweights.resize(1, temp);
36 :     infile >> temp >> ws;
37 :     upperweights.resize(1, temp);
38 :     switchnr.resize(1, -1);
39 :     count++;
40 :     }
41 :     infile >> ws;
42 :     }
43 :     handle.logMessage(LOGMESSAGE, "Read penalty file - number of entries", count);
44 :     }
45 :    
46 :     void BoundLikelihood::Reset(const Keeper* const keeper) {
47 :    
48 :     Likelihood::Reset(keeper);
49 :     handle.setNaNFlag(0); // reset the NaN count
50 :     if (isZero(weight))
51 :     handle.logMessage(LOGWARN, "Warning in boundlikelihood - zero weight for", this->getName());
52 :    
53 :     if (!checkInitialised) {
54 :     if (!keeper->boundsGiven())
55 :     handle.logMessage(LOGWARN, "Warning in boundlikelihood - no bounds have been set in input file");
56 :    
57 :     int i, j, k, numvar, numset, numfail;
58 :     numvar = keeper->numVariables();
59 :     numset = switches.Size();
60 :    
61 :     if (numset != 0) {
62 :     numfail = 0;
63 :     ParameterVector sw(numvar);
64 :     keeper->getSwitches(sw);
65 :     for (i = 0; i < numset; i++)
66 :     for (j = 0; j < numvar; j++)
67 :     if (switches[i] == sw[j])
68 :     switchnr[i] = j;
69 :    
70 :     for (i = 0; i < numset; i++) {
71 :     if (switchnr[i] == -1) {
72 :     handle.logMessage(LOGWARN, "Warning in boundlikelihood - failed to match switch", switches[i].getName());
73 :     numfail++;
74 :     // delete the entries for the non-existant switch
75 :     switches.Delete(i);
76 :     powers.Delete(i);
77 :     lowerweights.Delete(i);
78 :     upperweights.Delete(i);
79 :     switchnr.Delete(i);
80 :     if (numfail != numset)
81 :     i--;
82 :     }
83 :     }
84 :     numset -= numfail;
85 :     }
86 :    
87 :     IntVector done(numset, 0);
88 :     // resize vectors to store data
89 :     likelihoods.resize(numvar, 0.0);
90 :     lowerbound.resize(numvar, 0.0);
91 :     upperbound.resize(numvar, 0.0);
92 :     values.resize(numvar, 0.0);
93 :    
94 :     powers.resize(numvar - numset, 0.0);
95 :     lowerweights.resize(numvar - numset, 0.0);
96 :     upperweights.resize(numvar - numset, 0.0);
97 :     switchnr.resize(numvar - numset, -1);
98 :    
99 :     DoubleVector lbs(numvar);
100 :     DoubleVector ubs(numvar);
101 :     keeper->getLowerBounds(lbs);
102 :     keeper->getUpperBounds(ubs);
103 :    
104 :     k = 0;
105 :     for (i = 0; i < numvar; i++) {
106 :     if (switchnr[i] != -1) {
107 :     lowerbound[i] = lbs[switchnr[i]];
108 :     upperbound[i] = ubs[switchnr[i]];
109 :     if (i < numset)
110 :     done[i] = switchnr[i];
111 :     else
112 :     handle.logMessage(LOGFAIL, "Error in boundlikelihood - received invalid variable to check bounds");
113 :    
114 :     } else {
115 :     for (j = 0; j < numset; j++)
116 :     if (k == done[j])
117 :     k++;
118 :    
119 :     switchnr[i] = k;
120 :     lowerbound[i] = lbs[k];
121 :     upperbound[i] = ubs[k];
122 :     powers[i] = defPower;
123 :     lowerweights[i] = defLW;
124 :     upperweights[i] = defUW;
125 :     k++;
126 :     }
127 :     }
128 :    
129 :     for (i = 0; i < powers.Size(); i++)
130 :     if (powers[i] < verysmall)
131 :     handle.logMessage(LOGFAIL, "Error in boundlikelihood - invalid value for power", powers[i]);
132 :    
133 :     checkInitialised = 1;
134 :     }
135 :     if (handle.getLogLevel() >= LOGMESSAGE)
136 :     handle.logMessage(LOGMESSAGE, "Reset boundlikelihood component", this->getName());
137 :     }
138 :    
139 :     void BoundLikelihood::addLikelihoodKeeper(const TimeClass* const TimeInfo, Keeper* const keeper) {
140 :    
141 :     int i;
142 :     double temp;
143 :     keeper->getCurrentValues(values);
144 :     for (i = 0; i < switchnr.Size(); i++) {
145 :     if (values[switchnr[i]] < lowerbound[i]) {
146 :     temp = fabs(values[switchnr[i]] - lowerbound[i]);
147 :     if (temp < 1.0)
148 :     likelihoods[i] = lowerweights[i] * pow(temp, (1.0 / powers[i]));
149 :     else
150 :     likelihoods[i] = lowerweights[i] * pow(temp, powers[i]);
151 :     likelihood += likelihoods[i];
152 :     keeper->Update(switchnr[i], lowerbound[i]);
153 :    
154 :     } else if (values[switchnr[i]] > upperbound[i]) {
155 :     temp = fabs(values[switchnr[i]] - upperbound[i]);
156 :     if (temp < 1.0)
157 :     likelihoods[i] = upperweights[i] * pow(temp, (1.0 / powers[i]));
158 :     else
159 :     likelihoods[i] = upperweights[i] * pow(temp, powers[i]);
160 :     likelihood += likelihoods[i];
161 :     keeper->Update(switchnr[i], upperbound[i]);
162 :    
163 :     } else
164 :     likelihoods[i] = 0.0;
165 :     }
166 :    
167 :     if ((handle.getLogLevel() >= LOGMESSAGE) && (isZero(likelihood)))
168 :     handle.logMessage(LOGMESSAGE, "For this model simulation, no parameters are outside the bounds");
169 :    
170 :     if (handle.getNaNFlag()) {
171 :     likelihood += verybig;
172 :     if (handle.getLogLevel() >= LOGMESSAGE)
173 :     handle.logMessage(LOGMESSAGE, "For this model simulation, a NaN was found within the model");
174 :     }
175 :    
176 :     if (handle.getLogLevel() >= LOGMESSAGE)
177 :     handle.logMessage(LOGMESSAGE, "Calculated likelihood score for boundlikelihood component to be", likelihood);
178 :     }
179 :    
180 :     void BoundLikelihood::printSummary(ofstream& outfile) {
181 :     //JMB there is only one likelihood score here ...
182 :     if (!(isZero(likelihood))) {
183 :     outfile << "all all all" << sep << setw(largewidth) << this->getName() << sep
184 :     << setprecision(smallprecision) << setw(smallwidth) << weight << sep
185 :     << setprecision(largeprecision) << setw(largewidth) << likelihood << endl;
186 :     outfile.flush();
187 :     }
188 :     }
189 :    
190 :     void BoundLikelihood::Print(ofstream& outfile) const {
191 :     outfile << "\nBoundlikelihood " << this->getName() << " - likelihood value "
192 :     << likelihood << endl;
193 :     outfile.flush();
194 :     }

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge