ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fmmtr1.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: Unknown
5  * Copyright (c) 2009-2012 ADMB Foundation
6  *
7  * This file was originally written in FORTRAN II by and unknown author.
8  * In the 1980s, it was ported to C and C++ and extensively modified by
9  * David Fournier.
10  *
11  */
16 #ifdef __ZTC__
17  #include <conio.h>
18 #endif
19 #include <fvar.hpp>
20 extern int ctlc_flag;
21 #ifdef __TURBOC__
22  #pragma hdrstop
23  #include <iostream.h>
24  #include <conio.h>
25 #endif
26 #if defined (__WAT32__) || defined(_MSC_VER)
27  #include <conio.h>
28 #endif
29 #ifdef __ZTC__
30  #include <iostream.hpp>
31  #include <disp.h>
32  #define endl "\n"
33 #endif
34 #ifdef __SUN__
35  #include <iostream.h>
36  #include <signal.h>
37  #define getch getchar
38  #ifdef __HP__
39  extern "C" void onintr(int k);
40  #endif
41 #endif
42 #ifdef __NDPX__
43  #include <iostream.hxx>
44 #endif
45 #if defined (_MSC_VER)
46  void __cdecl clrscr();
47 #else
48  #include <iostream>
49  #include <signal.h>
50  #define getch getchar
51  extern "C" void onintr(int k);
52  extern "C" void clrscr();
53 #endif
54 #include <math.h>
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <ctype.h>
58 dvector update(int nvar, int iter, int m, const dvector& g,
59  const dmatrix& xalpha, dmatrix& y, const dvector& x, const dvector& xold,
60  const dvector& gold, const dvector& xrho);
61 double dafsqrt( double x );
62 
67 void fmmt1::fmin(const double& _f, const dvector & _x, const dvector& _g)
68 {
69  double& f=(double&) _f;
71  dvector& g=(dvector&) _g;
72  #ifdef DIAG
73  cout << "On entry to fmin: " << *this << endl;
74  #endif
75  if (use_control_c)
76  {
77 #if !defined (_MSC_VER)
78  #if defined( __SUN__) && !(defined __GNU__)
79  #if defined( __HP__)
80  if (ireturn <= 0 )
81  {
82  signal(SIGINT, &onintr);
83  }
84  #else
85  if (ireturn <= 0 )
86  {
87  signal(SIGINT, (SIG_PF)&onintr);
88  }
89  #endif
90  #endif
91  #endif
92  }
93  if (use_control_c)
94  {
95  #if defined( __GNU__)
96  if (ireturn <= 0 )
97  {
98  signal(SIGINT, &onintr);
99  }
100  #endif
101  }
102  #ifdef __ZTC__
103  if (ireturn <= 0 )
104  {
105  if (disp_inited == 0)
106  {
107  disp_open();
108  disp_usebios();
109  }
110  }
111  #endif
112  if (ireturn >= 3)
113  {
114  derch(f, x, g, n, ireturn);
115  return;
116  }
117  if (ireturn == 1) goto call1;
118  if (ireturn == 2) goto call2;
119  ihflag=0;
120  if (n <= 0)
121  {
122  cerr << "Error -- the number of active parameters"
123  " fmin must be > 0\n";
124  ad_exit(1);
125  }
126  if (x.indexmin() !=1)
127  {
128  cerr << "Error -- minimum valid index"
129  " for independent_variables in fmin must be 1\n"
130  << " it is " << x.indexmin() << "\n";
131  ad_exit(1);
132  }
133  if (x.size() < static_cast<unsigned int>(n))
134  {
135  cerr << "Error -- the size of the independent_variables"
136  " which is " << x.size() << " must be >= " << n << "\n"
137  << " the number of independent variables in fmin\n";
138  ad_exit(1);
139  }
140  if (g.indexmin() !=1)
141  {
142  cerr << "Error -- minimum valid index"
143  " for the gradient vector in fmin must be 1\n"
144  << " it is " << g.indexmin() << "\n";
145  ad_exit(1);
146  }
147  if (g.size() < static_cast<unsigned int>(n))
148  {
149  cerr << "Error -- the size of the gradient vector"
150  " which is " << g.size() << " must be >=\n"
151  << " the number of independent variables in fmin\n";
152  ad_exit(1);
153  }
154  for (i=1; i<=n; i++)
155  xx.elem(i)=x.elem(i);
156  itn=0;
157  icc=0;
158  for (i=1; i< 11; i++)
159  funval.elem(i)=0.;
160  ihang = 0;
161  llog=1;
162  np=n+1;
163  n1=n-1;
164  nn=n*np/2;
165  is=n;
166  iu=n;
167  iv=n+n;
168  ib=iv+n;
169  iexit=0;
170  dmin=1;
171  if (dmin <= 0.)
172  goto label7020;
173  if(dfn == 0.)
174  z = 0.0;
175  for (i=1; i<=n; i++)
176  {
177  xsave.elem(i)=x.elem(i);
178  x.elem(i)=xx.elem(i);
179  }
180  ireturn=1;
181  return;
182  call1:
183  for (i=1; i<=n; i++)
184  {
185  x.elem(i)=xsave.elem(i);
186  }
187  ireturn=3;
188  fbest = f;
189  for ( i=1; i<=n; i++)
190  gbest.elem(i)=g.elem(i);
191  funval.elem(10) = f;
192  df=dfn;
193  if(dfn == 0.0)
194  df = f - z;
195  if(dfn < 0.0)
196  df=fabs(df * f);
197  if(df <= 0.0)
198  df=1.;
199 label20:
200  ic=0;
201  iconv = 1;
202  for ( i=1; i<=9; i++)
203  funval.elem(i)= funval.elem(i+1);
204  funval.elem(10) = f;
205  if ( itn>15 && fabs( funval.elem(1)-funval.elem(10))< min_improve )
206  ihang = 1;
207  gmax = 0;
208  for ( i=1; i<=n; i++)
209  {
210  if(fabs(g.elem(i)) > crit) iconv = 2;
211  if(fabs(g.elem(i)) > fabs(gmax) ) gmax = g.elem(i);
212  }
213  if( iconv == 1 || ihang == 1)
214  {
215  ireturn=-1;
216  goto label92;
217  }
218  if(iprint == 0)
219  goto label21;
220  if(itn == 0)
221  goto label7000;
222  if( (itn%iprint) != 0)
223  goto label21;
224  if (llog) goto label7010;
225 #if !defined (_MSC_VER) && !defined (__GNUC__)
226  if (!scroll_flag) clrscr();
227 #endif
228 label7003:
229  if (iprint!=0)
230  {
231  ad_printf("%d variables; iteration %ld; function evaluation %ld\n",
232  n, itn, ifn);
233  ad_printf("Function value %12.4le; maximum gradient component mag %12.4le\n",
234  f, gmax);
235  }
236 /*label7002:*/
237  /* Printing Statistics table */
238  if(iprint>0)
239  {
240  fmmdisp(x, g, n, this->scroll_flag,noprintx);
241  }
242 label21 :
243  itn++;
244  rrr=update(n,itn-1,xm,g,xstep,xy,x,xold,gold,xrho);
245  for (i=1; i<=n; i++)
246  x.elem(i)=xx.elem(i);
247  for (i=1; i<=n; i++)
248  {
249  w.elem(i)=-g.elem(i);
250  }
251  for (i=1; i<=n; i++)
252  {
253  w(n+i)=rrr(i);
254  }
255  gs=0.0;
256  for (i=1; i<=n; i++)
257  gs+=w.elem(is+i)*g.elem(i);
258  iexit=2;
259  if(gs >= 0.0)
260  {
261  double a=1.1*gs/norm2(g);
262  for (i=1; i<=n; i++)
263  w.elem(is+i)-=a*g.elem(i);
264  gs=0.0;
265  for (i=1; i<=n; i++)
266  gs+=w.elem(is+i)*g.elem(i);
267  if(gs >= 0.0)
268  {
269  cout << "Kludge didn't work" << endl;
270  goto label92;
271  }
272  }
273  gso=gs;
274  alpha=-2.0*df/gs;
275  if(alpha > 1.0)
276  alpha=1.0;
277  df=f;
278  tot=0.0;
279 label30:
280  iexit=3;
281  if( ifn >= maxfn)
282  {
283  maxfn_flag=1;
284  goto label92;
285  }
286  else
287  {
288  maxfn_flag=0;
289  iexit=1;
290  }
291  if(quit_flag && use_control_c) goto label92;
292  for (i=1; i<=n; i++)
293  {
294  z=alpha*w.elem(is+i);
295  xx.elem(i)+=z;
296  }
297  for (i=1; i<=n; i++)
298  {
299  xsave.elem(i)=x.elem(i);
300  gsave.elem(i)=g.elem(i);
301  x.elem(i)=xx.elem(i);
302  fsave = f;
303  }
304  fsave = f;
305  ireturn=2;
306  return;
307  call2:
308  for (i=1; i<=n; i++)
309  {
310  x.elem(i)=xsave.elem(i);
311  w.elem(i)=g.elem(i);
312  g.elem(i)=gsave.elem(i);
313  }
314  fy = f;
315  f = fsave;
316  ireturn=-1;
317  if (fy < fbest)
318  {
319  fbest=fy;
320  for (i=1; i<=n; i++)
321  {
322  x.elem(i)=xx.elem(i);
323  gbest.elem(i)=w.elem(i);
324  }
325  }
326 #if defined(_MSC_VER)
327  if (kbhit())
328 #else
329  if(ctlc_flag && use_control_c)
330 #endif
331  {
332  #if defined(__DJGPP__)
333  int c = toupper(getxkey());
334  #else
335  int c = toupper(getch());
336  #endif
337  if ( c == 'C')
338  {
339  for (i=1; i<=n; i++)
340  {
341  x.elem(i)=xx.elem(i);
342  }
343  ireturn = 3;
344  derch(f, x , w, n, ireturn);
345  return;
346  }
347  else
348  {
349  if ( c == 'Q'|| c == 'N')
350  {
351  quit_flag=c;
352  goto label92;
353  }
354  else
355  {
356  quit_flag=0;
357  }
358  }
359  }
360  icc+=1;
361  if( icc >= 5)
362  icc=0;
363  ic++;
364  if( ic >imax)
365  {
366  if (iprint>0)
367  {
368  ad_printf(" ic > imax in fminim is answer attained ?\n" );
369  fmmdisp(x, g, n, this->scroll_flag,noprintx);
370  }
371  ihflag=1;
372  ihang=1;
373  goto label92;
374  }
375  ifn++;
376  gys=0.0;
377  for (i=1; i<= n; i++)
378  gys+=w.elem(i)*w.elem(is+i);
379  if(fy>=f)
380  goto label40;
381  if(fabs(gys/gso)<=.9)
382  goto label50;
383  if(fabs(gys/gso)<=.95 && ic > 4)
384  goto label50;
385  if(gys>0.0)
386  goto label40;
387  tot+=alpha;
388  z=10.0;
389  if(gs<gys)
390  z=gys/(gs-gys);
391  if(z>10.0)
392  z=10.0;
393  alpha=alpha*z;
394  if (alpha == 0.)
395  {
396  ialph=1;
397  #ifdef __ZTC__
398  if (ireturn <= 0)
399  {
400  disp_close();
401  }
402  #endif
403  return;
404  }
405  f=fy;
406  gs=gys;
407  goto label30;
408 label40:
409  for (i=1;i<=n;i++)
410  xx.elem(i)-=alpha*w.elem(is+i);
411  z=3.0*(f-fy)/alpha+gys+gs;
412  zz=dafsqrt(z*z-gs*gys);
413  z=1.0-(gys+zz-z)/(2.0*zz+gys-gs);
414  if (fabs(fy-1.e+95) < 1.e-66)
415  {
416  alpha*=.001;
417  }
418  else
419  {
420  alpha=alpha*z;
421  }
422  if (alpha == 0.)
423  {
424  ialph=1;
425  #ifdef __ZTC__
426  if (ireturn <= 0)
427  {
428  disp_close();
429  }
430  #endif
431  return;
432  }
433  goto label30;
434 label50:
435  alpha+=tot;
436  f=fy;
437  df-=f;
438  dgs=gys-gso;
439  link=1;
440  if(dgs+alpha*gso>0.0)
441  goto label52;
442  for (i=1;i<=n;i++)
443  w.elem(iu+i)=w.elem(i)-g.elem(i);
444  sig=1.0/(alpha*dgs);
445  goto label70;
446 label52:
447  zz=alpha/(dgs-alpha*gso);
448  z=dgs*zz-1.0;
449  for (i=1;i<=n;i++)
450  w.elem(iu+i)=z*g.elem(i)+w.elem(i);
451  sig=1.0/(zz*dgs*dgs);
452  goto label70;
453 label60:
454  link=2;
455  for (i=1;i<=n;i++)
456  w.elem(iu+i)=g.elem(i);
457  if(dgs+alpha*gso>0.0)
458  goto label62;
459  sig=1.0/gso;
460  goto label70;
461 label62:
462  sig=-zz;
463  goto label70;
464 label65:
465  for (i=1;i<=n;i++)
466  g.elem(i)=w.elem(i);
467  goto label20;
468 label70:
469  if (link == 1) goto label60;
470  if (link == 2) goto label65;
471 /*label90:*/
472  for (i=1;i<=n;i++)
473  g.elem(i)=w.elem(i);
474 label92:
475  if (iprint>0)
476  {
477  if (ihang == 1)
478  ad_printf("Function minimizer not making progress ... is minimum attained?\n");
479  }
480  if(iexit == 2)
481  {
482  if (iprint>0)
483  {
484  ad_printf("*** grad transpose times delta x greater >= 0\n"
485  " --- convergence critera may be too strict\n");
486  ireturn=-1;
487  }
488  }
489 # if defined (_MSC_VER) && !defined (__WAT32__)
490  if (scroll_flag == 0) clrscr();
491 # endif
492  if (maxfn_flag == 1)
493  {
494  if (iprint>0)
495  {
496  ad_printf("Maximum number of function evaluations exceeded");
497  }
498  }
499  if (iprint>0)
500  {
501  if (quit_flag == 'Q' && use_control_c)
502  ad_printf("User initiated interrupt");
503  }
504  if(iprint == 0) goto label777;
505  ad_printf(" - final statistics:\n");
506  ad_printf("%d variables; iteration %ld; function evaluation %ld\n",
507  n, itn, ifn);
508  ad_printf("Function value %12.4le; maximum gradient component mag %12.4le\n",
509  f, gmax);
510  ad_printf("Exit code = %ld; converg criter %12.4le\n",iexit,crit);
511 
512  fmmdisp(x, g, n, this->scroll_flag,noprintx);
513 label777:
514  if (ireturn <= 0)
515  #ifdef DIAG
516  if (ad_printf) (*ad_printf)("Final values of h in fmin:\n");
517  //cout << h << "\n";
518  #endif
519  #ifdef __ZTC__
520  {
521  disp_close();
522  }
523  #endif
524  return;
525 label7000:
526  if (iprint>0)
527  {
528 # if defined (_MSC_VER) && !defined (__WAT32__)
529  if (!scroll_flag) clrscr();
530 #endif
531  ad_printf("Initial statistics: ");
532  }
533  goto label7003;
534 label7010:
535  if (iprint>0)
536  {
537 # if defined (_MSC_VER) && !defined (__WAT32__)
538  if (!scroll_flag) clrscr();
539 #endif
540  ad_printf("Intermediate statistics: ");
541  }
542  llog=0;
543  goto label7003;
544 label7020:
545  if (iprint>0)
546  {
547  ad_printf("*** hessian not positive definite\n");
548  }
549  #ifdef __ZTC__
550  if (ireturn <= 0)
551  {
552  disp_close();
553  }
554  #endif
555  return;
556  }
557 
562 dvector update(int nvar, int iter, int m, const dvector& g, const dmatrix& _s,
563  dmatrix& y, const dvector& x, const dvector& _xold, const dvector& _gold,
564  const dvector& _xrho)
565  {
566  dvector& xold= (dvector&) _xold;
567  dmatrix& s= (dmatrix&) _s;
568  dvector& gold= (dvector&) _gold;
569  dvector& xrho=(dvector&)_xrho;
570  dvector beta(1,nvar);
571  dvector alpha(0,m);
572  dvector r(1,nvar);
573  dvector t(1,nvar);
574  int m1=m+1;
575  if (iter<1)
576  {
577  xold=x;
578  gold=g;
579  r=g;
580  }
581  else
582  {
583  int k=iter-1;
584  int k1=k%(m1);
585  y(k1)=g-gold;
586  s(k1)=x-xold;
587  xrho(k1)=1./(y(k1)*s(k1));
588  xold=x;
589  gold=g;
590  int i;
591  int lb=k-m+1;
592  if (lb <0) lb=0;
593  t=g;
594  for (i=k;i>=lb;i--)
595  {
596  int i1=i%(m1);
597  //int i2=(i+1)%(m1);
598  {
599  alpha(i-lb)=xrho(i1)*(s(i1)*t);
600  }
601  t-=alpha(i-lb)*y(i1);
602  }
603  r=t;
604  for (i=lb;i<=k;i++)
605  {
606  int i1=i%(m1);
607  r+= (alpha(i-lb)-xrho(i1)*(y(i1)*r)) * s(i1);
608  }
609  }
610  return -1.0*r;
611  }
dvector gbest
Definition: fvar.hpp:3357
dvector xsave
Definition: fvar.hpp:3358
double z
Definition: fvar.hpp:3351
long ialph
Definition: fvar.hpp:3190
long int iv
Definition: fvar.hpp:3352
long int link
Definition: fvar.hpp:3350
double dfn
Definition: fvar.hpp:3187
void fmmdisp(const dvector &x, const dvector &g, const int &nvar, int scroll_flag, int noprintx)
Description not yet available.
Definition: fmm_disp.cpp:102
double crit
Definition: fvar.hpp:3184
void derch(const double &_f, const dvector &_x, const dvector &_gg, int n, const int &_ireturn)
Description not yet available.
Definition: conjprod.cpp:1008
double tot
Definition: fvar.hpp:3351
int i
Definition: fvar.hpp:3353
double & elem(int i)
Definition: dvector.h:152
dvector gsave
Definition: fvar.hpp:3359
dvector rrr
Definition: fvar.hpp:3343
long int nn
Definition: fvar.hpp:3352
void fmin(const double &f, const dvector &x, const dvector &g)
Description not yet available.
Definition: fmmtr1.cpp:67
double gmax
Definition: fvar.hpp:3354
#define x
double gso
Definition: fvar.hpp:3351
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
int maxfn_flag
Definition: fvar.hpp:3194
long int icc
Definition: fvar.hpp:3352
long maxfn
Definition: fvar.hpp:3182
int noprintx
Definition: fvar.hpp:3181
double fsave
Definition: fvar.hpp:3355
long int n1
Definition: fvar.hpp:3350
#define getch
Definition: fmmtr1.cpp:50
void clrscr()
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: clrscr.cpp:8
dmatrix xy
Definition: fvar.hpp:3344
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
exitptr ad_exit
Definition: gradstrc.cpp:53
long int iconv
Definition: fvar.hpp:3350
dvector w
Definition: fvar.hpp:3338
long imax
Definition: fvar.hpp:3186
long int ic
Definition: fvar.hpp:3350
double zz
Definition: fvar.hpp:3351
dvector update(int nvar, int iter, int m, const dvector &g, const dmatrix &xalpha, dmatrix &y, const dvector &x, const dvector &xold, const dvector &gold, const dvector &xrho)
Description not yet available.
Definition: fmmtr1.cpp:562
prnstream & endl(prnstream &)
Description not yet available.
Definition: fvar.hpp:1937
double dmin
Definition: fvar.hpp:3348
long ihang
Definition: fvar.hpp:3192
double fbest
Definition: fvar.hpp:3348
int scroll_flag
Definition: fvar.hpp:3193
double df
Definition: fvar.hpp:3348
int xm
Definition: fvar.hpp:3340
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
dvector funval
Definition: fvar.hpp:3339
long ifn
Definition: fvar.hpp:3188
int itn
Definition: fvar.hpp:3353
dvariable beta(const prevariable &a, const prevariable &b)
Beta density function.
Definition: vbeta.cpp:18
long int np
Definition: fvar.hpp:3352
Description not yet available.
Definition: fvar.hpp:2819
double dgs
Definition: fvar.hpp:3351
int use_control_c
Definition: fvar.hpp:3199
int quit_flag
Definition: fvar.hpp:3195
double norm2(const d3_array &a)
Return sum of squared elements in a.
Definition: d3arr2a.cpp:167
dmatrix xstep
Definition: fvar.hpp:3341
dvector gold
Definition: fvar.hpp:3346
unsigned int size() const
Get number of elements in array.
Definition: dvector.h:209
int iu
Definition: fvar.hpp:3353
double sig
Definition: fvar.hpp:3351
double gs
Definition: fvar.hpp:3351
dvector xrho
Definition: fvar.hpp:3342
double dafsqrt(double x)
Robust square root.
Definition: newfmin.cpp:1295
long ihflag
Definition: fvar.hpp:3191
dvector xx
Definition: fvar.hpp:3356
int is
Definition: fvar.hpp:3353
long int ib
Definition: fvar.hpp:3352
double min_improve
Definition: fvar.hpp:3196
double fy
Definition: fvar.hpp:3351
double alpha
Definition: fvar.hpp:3351
dvector xold
Definition: fvar.hpp:3345
long iprint
Definition: fvar.hpp:3183
int n
Definition: fvar.hpp:3361
long int llog
Definition: fvar.hpp:3350
int onintr(int *k)
double gys
Definition: fvar.hpp:3351
int ireturn
Definition: fvar.hpp:3197
long iexit
Definition: fvar.hpp:3189
int ad_printf(FILE *stream, const char *format, Args...args)
Definition: fvar.hpp:9487
int kbhit(void)
Definition: df1b2fun.cpp:15
int ctlc_flag
Definition: gradstrc.cpp:68