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/paramin-beta/paraminhooke.cc
[mareframe] / trunk / paramin-beta / paraminhooke.cc Repository:
ViewVC logotype

Annotation of /trunk/paramin-beta/paraminhooke.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "paraminhooke.h"
2 :    
3 :     // ********************************************************
4 :     // functions for class ParaminHooke
5 :     // ********************************************************
6 :     ParaminHooke::ParaminHooke(NetInterface* netInt) : ParaminSearch(netInt) {
7 :     iters = 0;
8 :     returnID = -1;
9 :     lambda = 0;
10 :     rho = 0.5;
11 :     epsilon = 1e-4;
12 :     maxiterations = 1000;
13 :     type = OPTHOOKE;
14 :     // already set in optinfo
15 :     // converge = 0;
16 :     }
17 :    
18 :     ParaminHooke::~ParaminHooke() {
19 :     }
20 :    
21 :     void ParaminHooke::read(CommentStream& infile, char* text) {
22 :     int i = 0;
23 :    
24 :     while (!infile.eof() && strcasecmp(text, "seed") && strcasecmp(text, "[hooke]") && strcasecmp(text, "[bfgs]")) {
25 :     infile >> ws;
26 :     if ((strcasecmp(text, "epsilon") == 0) || (strcasecmp(text, "hookeeps") == 0)) {
27 :     infile >> epsilon;
28 :    
29 :     } else if ((strcasecmp(text, "maxiterations") == 0) || (strcasecmp(text, "hookeiter") == 0)) {
30 :     infile >> maxiterations;
31 :    
32 :     } else if (strcasecmp(text, "rho") == 0) {
33 :     infile >> rho;
34 :    
35 :     } else if (strcasecmp(text, "lambda") == 0) {
36 :     infile >> lambda;
37 :    
38 :     } else if (strcasecmp(text, "bndcheck") == 0) {
39 :     //JMB - read and ignore bndcheck
40 :     infile >> text;
41 :    
42 :     } else {
43 :     cerr << "Error while reading optinfo for Hooke - unknown option " << text << endl;
44 :     exit(EXIT_FAILURE);
45 :     }
46 :     infile >> text;
47 :     i++;
48 :     }
49 :    
50 :     if (i == 0)
51 :     cerr << "Warning - no optinfo give for Hooke in file - using default parameters" << endl;
52 :    
53 :     //check the values specified in the optinfo file ...
54 :     if ((rho < 0) || (rho > 1)) {
55 :     cerr << "\nError in value of rho - setting to default value of 0.5\n";
56 :     rho = 0.5;
57 :     }
58 :     if ((lambda < 0) || (lambda > 1)) {
59 :     cerr << "\nError in value of lambda - setting to default value of " << rho << endl;
60 :     lambda = rho;
61 :     }
62 :     }
63 :    
64 :     void ParaminHooke::OptimiseLikelihood() {
65 :     double steplength;
66 :     int i, keep;
67 :    
68 :     NumberOfHosts = net->getTotalNumProc();
69 :     int maxnumindatagroup = numvar*10;
70 :     // maybe not needed can do resize on the go????
71 :     // Vector tempV(maxnumindatagroup);
72 :     //previousf = tempV;
73 :     previousf.Reset();
74 :     previousf.resize(maxnumindatagroup, 0.0);
75 :     //par = new int[maxnumindatagroup];
76 :     par.resize(maxnumindatagroup, 0);
77 :     xbefore = net->getInitialX();
78 :     fbefore = net->getScore();
79 :     bestx = xbefore;
80 :     bestf = fbefore;
81 :    
82 :     cout << "in opt hooke and Jeeves best x is " << endl;
83 :     for (i = 0; i < bestx.Size(); i++) {
84 :     cout << bestx[i] << " ";
85 :     };
86 :     cout << endl;
87 :     cout << "in opt hooke and jeeves best f is " << bestf << endl;
88 :    
89 :     delta.Reset();
90 :     delta.resize(numvar, 0.0);
91 :     int numFromLineSeek;
92 :     // change = new int[numvar];
93 :     // param = new int[numvar];
94 :     change.Reset();
95 :     change.resize(numvar, 0);
96 :     param.Reset();
97 :     param.resize(numvar,0);
98 :     // The original definition of the delta array has not been changed, even
99 :     // though we're sometimes working with scaled x-values.
100 :     for (i = 0; i < numvar; i++) {
101 :     delta[i] = fabs(bestx[i] * rho);
102 :     if (delta[i] == 0.0)
103 :     delta[i] = rho;
104 :     param[i] = i;
105 :     }
106 :    
107 :     steplength = ((lambda < verysmall) ? rho : lambda);
108 :    
109 :     lineS = new LineSeeker();
110 :    
111 :     while ((iters < maxiterations) && (steplength > epsilon)) {
112 :     /* randomize the order of the parameters once in a while, to avoid */
113 :     /* the order having an influence on which changes are accepted. */
114 :    
115 :     randomOrder(param);
116 :    
117 :     /* find best new point, one coord at a time */
118 :     bestNearby();
119 :    
120 :     //JMB check for too many iterations here
121 :    
122 :     /* if we made some improvements, pursue that direction */
123 :     keep = 1;
124 :     while ((bestf < fbefore) && (keep == 1) && (iters < maxiterations)) {
125 :     // cout << "some improvement from bestNearby" << endl;
126 :     for (i = 0; i < numvar; i++) {
127 :     /* firstly, arrange the sign of delta[] */
128 :     if (bestx[i] <= xbefore[i])
129 :     delta[i] = 0.0 - fabs(delta[i]);
130 :     else
131 :     delta[i] = fabs(delta[i]);
132 :     }
133 :    
134 :     numFromLineSeek = lineS->doLineseek(xbefore, bestx, bestf, net);
135 :     iters += numFromLineSeek;
136 :     xbefore = bestx;
137 :     fbefore = bestf;
138 :     bestf = lineS->getBestF();
139 :     cout << "hooke and jeeves best f " << bestf << endl;
140 :     bestx = lineS->getBestX();
141 :    
142 :     // if the further (optimistic) move was bad
143 :     if (bestf >= fbefore) {
144 :     bestf = fbefore;
145 :     bestx = xbefore;
146 :    
147 :     } else {
148 :     // else, look around from that point
149 :     if (iters < maxiterations) {
150 :     xbefore = bestx;
151 :     fbefore = bestf;
152 :     bestNearby();
153 :     /* if the further (optimistic) move was bad */
154 :     if (bestf < fbefore) {
155 :     // bestf can only be equal or less than fbefore
156 :     /* make sure that the differences between the new and the old */
157 :     /* points are due to actual displacements - beware of roundoff */
158 :     /* errors that might cause newf < fbefore */
159 :     keep = 0;
160 :     for (i = 0; i < numvar; i++) {
161 :     keep = 1;
162 :     // AJ changing to be the same check as in gadget..
163 :     if (fabs(bestx[i] - xbefore[i]) > rathersmall)
164 :     break;
165 :     else
166 :     keep = 0;
167 :     }
168 :     }
169 :     }
170 :     }
171 :     }
172 :    
173 :     if ((steplength >= epsilon) && (bestf >= fbefore)) {
174 :     steplength = steplength * rho;
175 :     for (i = 0; i < numvar; i++)
176 :     delta[i] *= rho;
177 :     }
178 :     }
179 :     // Must look into this cout.. things.. should be handle...
180 :     cout << "\nStopping Hooke and Jeeves\n\nThe optimisation stopped after " << iters
181 :     << " iterations (max " << maxiterations << ")\nThe steplength was reduced to "
182 :     << steplength << " (min " << epsilon << ")\n";
183 :    
184 :     if (iters >= maxiterations)
185 :     cout << "The optimisation stopped because the maximum number of iterations" << "\nwas reached and NOT because an optimum was found for this run\n";
186 :     else {
187 :     cout << "The optimisation stopped because an optimum was found for this run\n";
188 :     converge = 1;
189 :     }
190 :     delete lineS;
191 :     // delete[] change;
192 :     // delete[] param;
193 :     // delete[] par;
194 :     net->setInitialScore(bestx, bestf);
195 :     score = bestf;
196 :     }
197 :    
198 :     void ParaminHooke::bestNearby() {
199 :     double ftmp;
200 :     int i, j;
201 :     int withinbounds;
202 :     int newopt;
203 :     net->startNewDataGroup(10 * numvar);
204 :    
205 :     for (i = 0; i < numvar; i++)
206 :     change[i] = 0;
207 :     numparamset = 0;
208 :     i = 0;
209 :     // sending as many points as there are processors in total.
210 :     // ************
211 :     // AJ if never within bound then will set numvar points!!!!
212 :     //
213 :    
214 :     while ((i < NumberOfHosts) && (numparamset < numvar)) {
215 :     withinbounds = SetPoint(param[numparamset]);
216 :     if (withinbounds == 1) {
217 :     change[param[numparamset]]++;
218 :     i++;
219 :     }
220 :     numparamset++;
221 :     }
222 :     // send all available data to all free hosts and then receive them
223 :     // back one at a time, sending new points when possible. If point I was
224 :     // trying to set was not within bound then nothing has been set and
225 :     // nothing will be sent. This could lead to poor use of available hosts.
226 :     net->sendToAllIdleHosts();
227 :     while (net->getNumNotAns() > 0) {
228 :     ReceiveValue();
229 :     if (iters % 1000 == 0) {
230 :     cout << "\nAfter " << iters << " function evaluations, f(x) = " << bestf << " at\n";
231 :     for (j = 0; j < bestx.Size(); j++)
232 :     cout << bestx[j] << sep;
233 :     cout << endl;
234 :     }
235 :     if (iters < maxiterations) {
236 :     if (MyDataGroup()) {
237 :     // Update the optimum if received a better value
238 :     newopt = UpdateOpt();
239 :     if (!newopt) {
240 :     SetPointNearby();
241 :     };
242 :     }
243 :     if (net->allSent())
244 :     SetPointNextCoord();
245 :     if (!net->allSent())
246 :     net->sendToIdleHosts();
247 :    
248 :     } else {
249 :     net->receiveAll();
250 :     // This takes time, ?? need to do this
251 :     // since I am receiving all, maybe should check if any
252 :     // of the return values are optimum.
253 :     }
254 :     }
255 :     net->stopUsingDataGroup();
256 :     }
257 :    
258 :     int ParaminHooke::SetPoint(int n) {
259 :     // return 0 and do nothing if the changes goes out of bounds.
260 :     DoubleVector z(bestx);
261 :     double next = bestx[n] + delta[n];
262 :     int numset;
263 :    
264 :     if (next < lowerbound[n]) {
265 :    
266 :     return 0;
267 :     }
268 :     else if (next > upperbound[n]) {
269 :    
270 :     return 0;
271 :     }
272 :     else {
273 :    
274 :     numset = net->getNumDataItemsSet();
275 :     z[n] = next;
276 :     net->setX(z);
277 :     par[numset] = n;
278 :     previousf[numset] = bestf;
279 :     return 1;
280 :     }
281 :     }
282 :    
283 :     int ParaminHooke::MyDataGroup() {
284 :     return (returnID >= 0);
285 :     }
286 :    
287 :     void ParaminHooke::ReceiveValue() {
288 :     int receive = net->receiveOne();
289 :     if (receive == net->netError()) {
290 :     cerr << "Error in Hooke - failed to receive data in linesearch\n";
291 :     net->stopUsingDataGroup();
292 :     exit(EXIT_FAILURE);
293 :     }
294 :     returnID = net->getReceiveID();
295 :     if (MyDataGroup()) {
296 :     iters++;
297 :     freceive = net->getY(returnID);
298 :     }
299 :     }
300 :    
301 :     int ParaminHooke::UpdateOpt() {
302 :     int newopt = 0;
303 :     if (freceive < bestf) {
304 :     bestf = freceive;
305 :     bestx = net->getX(returnID);
306 :     newopt = 1;
307 :     }
308 :     return newopt;
309 :     }
310 :    
311 :     void ParaminHooke::SetPointNearby() {
312 :     int withinbounds = 0;
313 :     int returnparam;
314 :     returnparam = par[returnID];
315 :     if (change[returnparam] == 1) {
316 :     if (freceive < previousf[returnID])
317 :     withinbounds = SetPoint(returnparam);
318 :     else {
319 :     delta[returnparam] = 0.0 - delta[returnparam];
320 :     withinbounds = SetPoint(returnparam);
321 :     if (withinbounds)
322 :     change[returnparam]++;
323 :     }
324 :     }
325 :     }
326 :    
327 :     void ParaminHooke::SetPointNextCoord() {
328 :     int withinbounds = 0;
329 :     while ((numparamset < numvar) && withinbounds == 0) {
330 :     withinbounds = SetPoint(param[numparamset]);
331 :     if (withinbounds)
332 :     change[param[numparamset]]++;
333 :     numparamset++;
334 :     }
335 :     }
336 :     void ParaminHooke::Print(ofstream& outfile, int prec) {
337 :     outfile << "; Hooke & Jeeves algorithm ran for " << iters
338 :     << " function evaluations\n; and stopped when the likelihood value was "
339 :     << setprecision(prec) << score;
340 :     if (converge == -1)
341 :     outfile << "\n; because an error occured during the optimisation\n";
342 :     else if (converge == 1)
343 :     outfile << "\n; because the convergence criteria were met\n";
344 :     else
345 :     outfile << "\n; because the maximum number of function evaluations was reached\n";
346 :     }

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

Powered By FusionForge