ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
conjprod.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 // this is to get UNIX systems to use getchar
12 // #define UNIXKLUDGE
13 
14 //#define DIAG
15 #ifdef _MSC_VER
16  #include <conio.h>
17 #else
18  #include <unistd.h>
19 #endif
20 
21 #include <fvar.hpp>
22 extern int ctlc_flag;
23 
24 #ifdef __TURBOC__
25 #pragma hdrstop
26 #include <conio.h>
27 #include <iomanip.h>
28 #endif
29 
30 #include <ctype.h>
31 #include <string.h>
32 
33 //#define CUBIC_INTERPOLATION
34 
35 #ifdef __ZTC__
36 #include <conio.h>
37 #include <iomanip.hpp>
38 #include <disp.h>
39 
40  // preprocessor defines and function definitions that emulate Borland
41  // text screen managment functions using the Zortech disp_ package
42  // note the order of includes is important - this stuff should come
43  // after stdio.h, disp.h and anything that might also include these
44 
45  void gotoxy(int x, int y);
46  void clrscr();
47 
48  struct text_info
49  {
50  unsigned char winleft, wintop;
51  unsigned char winright, winbottom;
52  //unsigned char attribute, normattr;
53  unsigned char currmode;
54  unsigned char screenheight;
55  unsigned char screenwidth;
56  unsigned char curx, cury;
57  };
58 
59  void gettextinfo(struct text_info *r);
60 #endif
61 
62 #if defined(UNIXKLUDGE) || \
63  ((defined(__SUN__) || defined(__GNU__)) && !defined(__GNUDOS__))
64 #include <iostream>
65 #include <signal.h>
66 #define getch getchar
67  #if defined(__HP__) || defined(linux)
68  extern "C" void onintr(int k);
69  #else
70  extern "C" int onintr(int* k);
71  #endif
72 #endif
73 
74 #if defined(_MSC_VER)
75  void __cdecl clrscr();
76 #else
77  extern "C" void clrscr();
78 #endif
79 
80 #ifdef __NDPX__
81 
82 #include <iostream.hxx>
83  extern "C" {
84 #include <grex.h>
85  void clrscr();
86  };
87 
88  void gotoxy(int x, int y);
89 
90  struct text_info
91  {
92  unsigned char winleft, wintop;
93  unsigned char winright, winbottom;
94  //unsigned char attribute, normattr;
95  //unsigned char currmode;
96  unsigned char screenheight;
97  unsigned char screenwidth;
98  unsigned char curx, cury;
99  };
100 
101  void gettextinfo(struct text_info *r);
102 #endif
103 
104 class fmmc;
105 
106 double do_interpolate(const double& fret, const double& left_bracket,
108  double& right_bracket, const double& right_bracket_value,
110  const int& J, long int& ifn, const double& crit1,
111  int& int_flag, const double& rho_1, const double& Psi_2, const dvector& g1);
112 
113 void do_extrapolate(const double& left_bracket,
115  const double& right_bracket, double& right_bracket_value,
117  dvector& d, const int& J, const double& rho_0, long int& ifn,
118  const int& ifnex, int& ext_flag, const double& rho_1, const double& rf,
119  const dvector& g1);
120 
121 double mylinmin(const double& fret, const double& Phi_i, const dvector& theta1,
122  const dvector& q_i, fmmc& cs);
123 
124 void bracket_report(const dvector& theta, const double& left_bracket,
125  double& right_bracket, const dvector& d);
126 
127 double cubic_interpolation(const double& u, const double& v, const double& a,
128  const double& b, double& ap, const double& bp);
129 
130 double Phi(const dvector&);
131 double min(const double&, const double&);
132 double max(const double&, const double&);
133 
134 #include <math.h>
135 
136 #define ITMAX 5000
137 #define EPS 1.0e-10
138 #define FREEALL free_vector(xi,1,n);free_vector(h,1,n);free_vector(g,1,n);
139 
144 void fmmc::fmin(const double& fret, const dvector& _p, const dvector& _gg)
145 {
146  dfn=0.0;;
147  maxfn_flag=0;
148  iexit=0;
149  ihflag=0;
150 #ifdef __ZTC__
151  if (disp_inited == 0)
152  {
153  disp_open();
154  disp_usebios();
155  }
156 #endif
157  dvector& p = (dvector&)_p;
158  dvector& gg = (dvector&)_gg;
159  int n=p.size();
160  dvector& xi=*(this->xi);
161  dvector& h=*(this->h);
162  dvector& g=*(this->g);
163  double& fp=this->fp;
164  //int& its=this->its;
165  //int& J=this->J;
166  if (this->frp_flag > 0) this->ifn++;
167 
168  if (ireturn >= 3)
169  {
170  {
171  derch(fret, p , gg, n, ireturn);
172  }
173  return;
174  }
175 
176  if (this->frp_flag>1)
177  {
178  if (fret < fbest)
179  {
180 #ifdef DIAG
181  cerr << " Improving fbest old fbest = " << fbest << "\n" <<
182  " new fbest = " << fret << "\n";
183 #endif
184 
185  int con_flag = 1;
186  for (int jj=gg.indexmin();jj<=gg.indexmax();jj++)
187  {
188  if (fabs(gg(jj)) > crit)
189  {
190  con_flag=0;
191  break;
192  }
193  }
194 
195  if ( con_flag==1)
196  {
197  this->ireturn=-1;
198  {
199  if (iprint>0)
200  {
201  ad_printf("Gradient magnitude criterion satisfied\n");
202  ad_printf("%d variables; iteration %ld; function evaluation %ld\n",
203  n, iter, ifn);
204  ad_printf("Function value %le; maximum gradient component mag %le\n",
205  fret, max(fabs(gg)) );
206  fmmdisp(p, gg, n, this->scroll_flag); //fmc);
207  }
208  }
209  return;
210  }
211  fbest=fret;
212  *xbest=p;
213  *gbest=gg;
214  }
215  }
216 
217  if (this->frp_flag==1) goto label800;
218  if (this->frp_flag==2) goto label2000;
219  if (this->frp_flag==3) goto label3000;
220 
221 #if defined(__SUN__) && !defined(__GNUDOS__) && !defined(_MSC_VER)
222  #ifdef __HP__
223  signal(SIGINT, &onintr);
224  #else
225  signal(SIGINT, (SIG_PF)&onintr);
226  #endif
227 #endif
228 #if (defined(__GNU_) && !defined(__GNUDOS__)) || defined(UNIXKLUDGE)
229  signal(SIGINT, &onintr);
230 #endif
231  this->J=1;
232  this->rho_min=1.e-10;
233  this->converge_flag=0;
234 
235  this->frp_flag=1;
236  this->ireturn=1;
237  return;
238 label800:
239 
240  fbest=fret;
241  *xbest=p;
242  *gbest=gg;
243 
244  this->frp_flag=0;
245  this->ireturn=0;
246  xi=gg;
247  this->fp=fret;
248  //(*dfunc)(p,xi);
249 
250  if (iprint>0)
251  {
252  if (!(iter%iprint)&& (iprint > 0) )
253  {
254 #if !defined (__WAT32__) && !defined (_MSC_VER)
255  if (!scroll_flag) clrscr();
256 #endif
257  ad_printf("Initial statistics: ");
258  ad_printf("%d variables; iteration %ld; function evaluation %ld\n",
259  n, iter, ifn);
260  ad_printf("Function value %le; maximum gradient component mag %le\n",
261  fbest, max(fabs(*gbest)) );
262  fmmdisp(*xbest, *gbest, n, this->scroll_flag); //fmc);
263  }
264  }
265 
266  {
267  for (int j=1;j<=n;j++)
268  {
269  g[j] = -xi[j];
270  xi[j]=h[j]=g[j];
271  }
272  }
273 
274  this->ifnex=0;
275 
276  //this->its=1;
277 
278 label1000:
279  iter++;
280  label5:
281 #if ((defined(__SUN__) || defined(__GNU__)) && !defined(__GNUDOS__)) \
282  || defined(UNIXKLUDGE)
283  if (ctlc_flag)
284 #else
285  if ( kbhit() )
286 #endif
287  {
288  int c = toupper(getch());
289  if ( c == 'C')
290  {
291  ireturn = 3;
292  {
293  derch(fret, p , gg, n, ireturn);
294  }
295  return;
296  }
297  else
298  {
299  if ( c == 'Q' || c == 'N' )
300  {
301  quit_flag=c;
302  this->ireturn=-1;
303  {
304  if (iprint>0)
305  {
306  ad_printf("User initiated interrupt\n");
307  ad_printf(" - final statistics:\n");
308  ad_printf("%d variables; iteration %ld; function evaluation %ld\n",
309  n, iter, ifn);
310  ad_printf("Function value %le; maximum gradient component mag %le\n",
311  fbest, max(fabs(*gbest)) );
312  fmmdisp(*xbest, *gbest, n, this->scroll_flag); //fmc);
313  }
314  }
315  p=*xbest;
316  gg=*gbest;
317  return;
318  }
319  else
320  {
321  quit_flag=0;
322  }
323  }
324  }
325  {
326  mylinmin(fret,fp,p,-1.*(g),*this);
327  }
328  if (this->lin_flag ==0) goto label10;
329 
330  p=*(this->extx);
331  this->frp_flag=2;
332  this->ireturn=1;
333  return;
334 
335 
336  label2000:
337  this->ireturn=0;
338  this->frp_flag=0;
339  //this->extf=fcomp( *(this->extx),*(this->extg) );
340  this->extf=fret;
341  *(this->extx)=p;
342  *(this->extg)=gg;
343  goto label5;
344  label10:
345 
346  {
347  for ( int i=1; i<=9; i++)
348  {
349  (*funval)[i]= (*funval)[i+1];
350  }
351  }
352 
353  (*funval)[10] = fret;
354 
355  if ( iter>15 && fabs( (*funval)[1]-(*funval)[10])< min_improve )
356  {
357  ihang = 1;
358  this->ireturn=-1;
359  {
360  if (iprint>0)
361  {
362  ad_printf("%d variables; iteration %ld; function evaluation %ld\n",
363  n, iter, ifn);
364  ad_printf("Function value %le; maximum gradient component mag %le\n",
365  fbest, max(fabs(*gbest)) );
366  fmmdisp(*xbest, *gbest, n, this->scroll_flag); //fmc);
367  }
368  }
369  p=*xbest;
370  gg=*gbest;
371  return;
372  }
373 
374 
375  /* Moved this up to entry point
376  {
377  int con_flag;
378  for (int jj=gg.indexmin();jj<=gg.indexmax();jj++)
379  {
380  con_flag=1;
381  if (fabs(g(jj)) > crit)
382  {
383  con_flag=0;
384  break;
385  }
386  }
387 
388  if ( con_flag==1)
389  {
390  this->ireturn=-1;
391  {
392  if (iprint>0)
393  {
394  ad_printf("Gradient magnitude criterion satisfied\n");
395  ad_printf("%d variables; iteration %ld; function evaluation %ld\n",
396  n, iter, ifn);
397  ad_printf("Function value %le; maximum gradient component mag %le\n",
398  fbest, max(fabs(*gbest)) );
399  fmmdisp(*xbest, *gbest, n, this->scroll_flag); //fmc);
400  }
401  }
402  p= *xbest;
403  gg= *gbest;
404  return;
405  }
406  }
407  */
408 
409 
410  if ( ifn > maxfn )
411  {
412  if (iprint>0)
413  {
414  ad_printf("Maximum number of function evaluations exceeded\n");
415  ad_printf("%d variables; iteration %ld; function evaluation %ld\n",
416  n, iter, ifn);
417  ad_printf("Function value %le; maximum gradient component mag %le\n",
418  fbest, max(fabs(*gbest)) );
419  fmmdisp(*xbest, *gbest, n, this->scroll_flag); //fmc);
420  }
421  p=*xbest;
422  gg=*gbest;
423  this->ireturn=-1;
424  return;
425  }
426 
427  if (iprint>0)
428  {
429  if (!(iter%iprint)&&(iprint>0))
430  {
431 #if !defined (__WAT32__) && !defined (_MSC_VER)
432  if (!scroll_flag) clrscr();
433 #endif
434  ad_printf("Intermediate statistics: ");
435  ad_printf("%d variables; iteration %ld; function evaluation %ld\n",
436  n, iter, ifn);
437  ad_printf("Function value %le; maximum gradient component mag %le\n",
438  fbest, max(fabs(*gbest)) );
439  fmmdisp(*xbest, *gbest, n, this->scroll_flag); //fmc);
440  }
441  }
442 
443  this->frp_flag=3;
444  this->ireturn=1;
445  // return;
446  label3000:
447  this->frp_flag=0;
448  this->ireturn=0;
449  xi=gg;
450  this->fp=fret;
451 
452  //fp=fcomp(p,xi);
453 
454  this->dgg=this->gg=0.0;
455  {
456  for (int j=1;j<=n;j++)
457  {
458  this->gg += g[j]*g[j];
459 /* dgg += xi[j]*xi[j]; */
460  this->dgg += (xi[j]+g[j])*xi[j];
461  }
462  }
463  if (this->gg == 0.0)
464  {
465  this->ireturn=-1;
466  return;
467  }
468  this->gam=this->dgg/this->gg;
469  {
470  for (int j=1;j<=n;j++)
471  {
472  g[j] = -xi[j]; // g seems to hold the negative gradient
473  xi[j]=h[j]=g[j]+this->gam*h[j];
474  }
475  }
476 // if (this->iter <= ITMAX) goto label1000;
477  goto label1000;
478 
479  cerr << "Too many iterations in FRPRMN\n";
480 }
481 
482 #undef ITMAX
483 #undef EPS
484 #undef FREEALL
485 
491 double mylinmin(const double& fret, const double& Phi_i, const dvector& theta1,
492  const dvector& q_i,
493  fmmc& cs)
494 {
495  // d is the current direction for the 1 dimensional search
496  // q_i is the gradient at the current best point
497 
498  dvector& theta= *(cs.theta);
499  if (cs.lin_flag==1)goto label1;
500  if (cs.lin_flag==2)goto label2;
501  if (cs.lin_flag==3)goto label3;
502  {
503  *(cs.theta) = theta1;
504  *(cs.d)=*(cs.xi);
505  cs.rho_0=pow(2.,-cs.J)/norm(q_i);
506  cs.gamma=q_i* *(cs.d);
507  cs.lin_flag=1;
508  *(cs.extx)=theta+cs.rho_0* *(cs.d);
509  }
510  return 0;
511 label1:
512  {
513  cs.lin_flag=0;
514  cs.Psi_0=cs.extf;
515  *(cs.g2)=*(cs.extg);
516 
517  //double rho_star=gamma*rho_0*rho_0/(2*(gamma*rho_0+Phi_i-Psi_0));
518 
519  cs.ext_flag=0;
520  cs.int_flag=0;
521  cs.dir_deriv=*(cs.d)* *(cs.g2);
522  cs.left_bracket_value=Phi_i;
523  *(cs.left_bracket_gradient)=q_i;
524  cs.left_bracket=0;
525  }
526 
527 
528 //cout << "Check " << cs.Psi_0 << " " << Phi_i << " " << cs.dir_deriv << "\n";
529 
530  if (!(cs.Psi_0 < Phi_i && cs.dir_deriv < 0)) goto label555;
531  // Extrapolate to get a right bracket
532 
533  label100:
534  {
535 #ifdef DIAG
536  cerr << "doing extrapolation\n";
537 #endif
541  *(cs.d),cs.J,cs.rho_0,cs.ifn,cs.ifnex,cs.ext_flag,cs.rho_1,
542  cs.Psi_1,*(cs.grad) );
543  }
544  if (cs.ext_flag==0)
545  {
546  goto label120;
547  }
548  else
549  {
550  {
551  cs.lin_flag=2;
552  *(cs.extx)=theta+cs.rho_1* *(cs.d);
553  //extx=theta+rho_1*d;
554  }
555  return 0;
556  label2:
557  cs.lin_flag=0;
558  //Psi_1=fcomp(theta+rho_1*d,grad);
559  cs.Psi_1=cs.extf;
560  *(cs.grad)= *(cs.extg);
561  }
562  goto label100;
563 
564  //label120:
565  // int itemp=1;
566 
567 label555:
568  //else
569  // We have a bracket
570  {
571 #ifdef DIAG
572  cerr << "We have a right bracket\n";
573 #endif
574  cs.right_bracket=cs.rho_0;
576  *(cs.right_bracket_gradient)=*(cs.g2);
577  }
578  label120:
579  // cout << "After extrapolation\n";
580  bracket_report(theta,cs.left_bracket,cs.right_bracket,*(cs.d));
581 
582  // We have a bracket [theta,theta+rho_0*d] so interpolate
583  {
584  label1100:
585  {
586  //cout << "Intrap\n";
589  *(cs.right_bracket_gradient),theta,*(cs.d),cs.J,cs.ifn,cs.crit1,
590  cs.int_flag,cs.rho_1,cs.Psi_1,*(cs.grad) );
591  }
592  if (cs.int_flag==0)
593  {
594  goto label1120;
595  }
596  else
597  {
598  {
599  cs.lin_flag=3;
600  *(cs.extx)=theta+cs.rho_1* *(cs.d) ;
601  }
602  return 0;
603  label3:
604  cs.lin_flag=0;
605  //Psi_1=fcomp(theta+rho_1*d,grad);
606  cs.Psi_1=cs.extf;
607  *(cs.grad)=*(cs.extg);
608  }
609  goto label1100;
610  label1120:
611  // int itemp=1;;
612  ;;
613  }
614  theta=theta+cs.rho_i* *(cs.d) ;
615  return cs.converge_flag;
616 }
617 
622 double do_interpolate(const double& _fret, const double& _left_bracket,
623  double& left_bracket_value, const dvector& _left_bracket_gradient,
624  double& right_bracket, const double& _right_bracket_value,
626  const int& _J, long int& ifn, const double& crit1,
627  int& int_flag, const double& _rho_1, const double& Psi_2, const dvector& g1)
628 {
629  double& fret = (double&)_fret;
630  double& rho_1 = (double&)_rho_1;
631  double& left_bracket = (double&)_left_bracket;
632  dvector& left_bracket_gradient = (dvector&)_left_bracket_gradient;
633  double& right_bracket_value = (double&)_right_bracket_value;
634 
635  double rho_min=1.e-10;
636  int& J = (int&) _J;
637  static double rho_star;
638  //static double dir_deriv;
639  //double Psi_2;
640  //dvector g1(1,d.size());
641  static double gamma;
642 #ifdef CUBIC_INTERPOLATION
643  static double gamma1;
644 #endif
645  static double rho_0;
646  if (int_flag ==1) goto label200;
647  J+=1;
648 label120:
649  // do
650  //{
651  gamma=left_bracket_gradient*d;
652 #ifdef CUBIC_INTERPOLATION
653  gamma1=right_bracket_gradient*d;
654 #endif
655  rho_0=right_bracket-left_bracket;
656 
657 #ifdef DIAG
658  #ifdef CUBIC_INTERPOLATION
659  cout << "f'(x) " << gamma << " "
660  << "f'(y) " << gamma1 << endl;
661  #endif
662 
663  cout << "f(x) " << left_bracket_value << " "
664  << "f(y) " << right_bracket_value << endl;
665 
666  cout << "x " << left_bracket << " "
667  << "y " << right_bracket << endl;
668 #endif
669 
670  if (gamma==0.0)
671  {
672  rho_star=0.;
673  }
674  else
675  {
676 #ifdef CUBIC_INTERPOLATION
677  rho_star=cubic_interpolation(left_bracket,right_bracket,
678  left_bracket_value,right_bracket_value,gamma,gamma1);
679 #else
680  double step=gamma*rho_0*rho_0/
681  (2*(gamma*rho_0+left_bracket_value-right_bracket_value));
682  // rho_star=left_bracket+gamma*rho_0*rho_0/
683  //(2*(gamma*rho_0+left_bracket_value-right_bracket_value));
684  double width=right_bracket-left_bracket;
685  if (step<.25*width)step=.25*width;
686  if (step>.75*width)step=.75*width;
687  rho_star=left_bracket+step;
688 
689 #endif
690  }
691 
692  if (rho_star < left_bracket )
693  {
694  cout << " rho_star out of range ..value changed\n";
695  }
696 
697  if (rho_star > right_bracket)
698  {
699  cout << " rho_star out of range ..value changed\n";
700  rho_star=.001*left_bracket+.999*right_bracket;
701  }
702 
703  if (rho_star <= rho_min)
704  {
705  return rho_min;
706  }
707  int_flag=1;
708  rho_1=rho_star;
709  return rho_min;
710 label200:
711  int_flag=0;
712  rho_1=rho_star;
713  //Psi_2=fcomp(theta+rho_star*d,g1);
714  //J+=1;
715 
716  //dir_deriv=d*g1;
717 
718  //cout << "Check2 " << Psi_2 << " " << left_bracket_value << " " <<
719  // d*g1 << "\n";
720 
721  if (Psi_2 < left_bracket_value && (d*g1) < 0)
722  {
723 #ifdef DIAG
724  cout << " Before interpolation -- new left side\n";
725  bracket_report( theta,left_bracket,right_bracket,d);
726 #endif
727 
728  left_bracket =rho_star;
729  left_bracket_value=Psi_2;
730  left_bracket_gradient=g1;
731 
732 #ifdef DIAG
733  cout << " After interpolation -- new left side\n";
734  bracket_report( theta,left_bracket,right_bracket,d);
735 #endif
736  }
737  else
738  {
739 #ifdef DIAG
740  cout << " Before interpolation -- new right side\n";
741  bracket_report( theta,left_bracket,right_bracket,d);
742 #endif
743  right_bracket =rho_star;
744  right_bracket_value=Psi_2;
745  right_bracket_gradient=g1;
746 
747 
748 #ifdef DIAG
749  cout << " After interpolation -- new right side\n";
750  bracket_report( theta,left_bracket,right_bracket,d);
751 #endif
752  }
753 
754  //while (dir_deriv > crit1
755  // && (right_bracket-left_bracket)> 1.e-10);
756 
757  double cos_theta=d*g1/(norm(d)*norm(g1));
758 #ifdef DIAG
759  cerr << " The cosine of angle between the search direction and \n"
760  " the gradient is " << cos_theta << "\n";
761 #endif
762 
763  if (cos_theta > crit1
764  && (right_bracket-left_bracket)> 1.e-10)
765  {
766  if (cos_theta > .05)
767  {
768  J+=1;
769  }
770  goto label120;
771  }
772 
773  if (cos_theta < -crit1 && (right_bracket-left_bracket)> 1.e-10)
774  {
775  goto label120;
776  }
777 
778 #ifdef DIAG
779  if (cos_theta < crit1)
780  {
781  cerr << " Leaving interpolation routine because"
782  " the cosine of angle between the \n search direction and "
783  " the gradient is < crit1 = " << crit1 << "\n";
784  }
785 #endif
786 
787 
788  fret=Psi_2;
789  return rho_star;
790 }
791 
796 void do_extrapolate(const double& _left_bracket,
797  const double& _left_bracket_value, dvector& left_bracket_gradient,
798  const double& _right_bracket, double& right_bracket_value,
799  const dvector& _right_bracket_gradient, const dvector& theta,
800  dvector& d, const int& _J, const double& rho_0, long int& ifn,
801  const int& _ifnex, int& ext_flag, const double& _rho_1, const double& rf,
802  const dvector& g1)
803 {
804  int& J = (int&)_J;
805  int& ifnex = (int&)_ifnex;
806  double& rho_1 = (double&)_rho_1;
807  double& left_bracket_value = (double&)_left_bracket_value;
808  double& left_bracket = (double&)_left_bracket;
809  double& right_bracket = (double&)_right_bracket;
810  dvector& right_bracket_gradient = (dvector&)_right_bracket_gradient;
811 
812  if (ext_flag==1) goto label1500;
813  J=J/2;
814  rho_1=4.*rho_0;
815  //dvector g1(1,d.size());
816 #ifndef __NDPX__
817  double Psi_1;
818 #endif
819 label1000:
820  ext_flag=1;
821  return;
822 label1500:
823 #ifdef __NDPX__
824  double Psi_1;
825 #endif
826  ext_flag=0;
827  //Psi_1=fcomp(theta+rho_1*d,g1);
828  Psi_1=rf;
829  ifnex++;
830  if (Psi_1 >= left_bracket_value || d*g1 >0) goto label2000;
831  left_bracket_value=Psi_1;
832  left_bracket_gradient=g1;
833  left_bracket=rho_1;
834  rho_1=4*rho_1;
835  goto label1000;
836 label2000:
837 
838  right_bracket=rho_1;
839  right_bracket_value=Psi_1;
840  right_bracket_gradient=g1;
841 }
842 
847 void bracket_report(const dvector& theta, const double& left_bracket,
848  double& right_bracket, const dvector& d)
849 {
850  dvector g(1,d.size());
851  dvector u(1,d.size());
852  ivector ii(1,3);
853  int one=1;
854  ii.fill_seqadd(one,one);
855 
856 #ifdef DIAG
857  cout << "lb " << setprecision(4) << setw(12) << left_bracket
858  << "rb " << setprecision(4) << setw(12) << right_bracket
859  << setprecision(4) << setw(12) << theta <<"\n";
860 #endif
861 }
862 
867 double cubic_interpolation(const double& u, const double& v, const double& aa,
868  const double& bb, double& ap, const double& bp)
869 {
870  //cout <<"Begin cube\n";
871  dmatrix M(1,4,1,4);
872 
873  M(1,1)=1;M(2,1)=0;M(3,1)=1;M(4,1)=0; // First column
874 
875  M(1,2)=u;M(2,2)=1;M(3,2)=v;M(4,2)=1; // Second column
876 
877  for (int i=3;i<=4;i++)
878  {
879  M(1,i)=u*M(1,i-1);
880  M(2,i)=(i-1)*M(1,i-1);
881  M(3,i)=v*M(3,i-1);
882  M(4,i)=(i-1)*M(3,i-1);
883  }
884 
885  dvector d(1,4);
886  d(1)=aa;
887  d(2)=ap;
888  d(3)=bb;
889  d(4)=bp;
890 
891  dvector e = inv(M)*d;
892  double a,b,c;
893 
894  a=3*e(4);
895  b=2*e(3);
896  c=e(2);
897 
898  double q;
899  if (a !=0)
900  {
901  if (b>0)
902  {
903  double y=b*b-4*a*c;
904  if (y<0) return (u+v)/2.;
905  q=-.5*(b+sqrt(y));
906  }
907  else
908  {
909  double y=b*b-4*a*c;
910  if (y<0) return (u+v)/2.;
911  q=-.5*(b-sqrt(b*b-4*a*c));
912  }
913  double x1,x2;
914  x1=q/a; // x1 and x2 are the two roots of the quadratic
915  x2=c/q; // equation that is the max andmin of the cubic
916  // polynomial
917  double sgn1=b+2*a*x1;
918  //double sgn2=b+2*a*x2;
919  if (sgn1>0)
920  {
921  return x1;
922  }
923  else
924  {
925  return x2;
926  }
927  }
928  else
929  {
930  // coeffcient of the quadratic term = 0
931  return -c/b;
932  }
933  //cout <<"end cube\n";
934 }
935 
936 #undef CUBIC_INTERPOLATION
937 
942 fmmc::fmmc(const int& n)
943 {
944  ctlc_flag = 0;
945  maxfn=500;
946  lin_flag=0;
947  ext_flag=0;
948  int_flag=0;
949  ifnex=0;
950  frp_flag=0;
951  quit_flag=0;
952  #ifdef __ZTC__
953  scroll_flag=0;
954  #else
955  scroll_flag=1;
956  #endif
957  crit=1.e-4;
958  crit1=1.e-2;
959  min_improve=1.e-6;
960  ireturn=0;
961  iprint=1;
962  imax=30;
963  ihang=0;
964  iter=0;
965  ifn=0;
968  funval=new dvector(1,10);
969  g=new dvector(1,n);
970  h=new dvector(1,n);
971  xi=new dvector(1,n);
972  d=new dvector(1,n);
973  extx=new dvector(1,n);
974  g2=new dvector(1,n);
975  grad=new dvector(1,n);
976  extg=new dvector(1,n);
977  theta=new dvector(1,n);
978  xbest=new dvector(1,n);
979  gbest=new dvector(1,n);
980 }
981 
987 {
988  delete left_bracket_gradient;
989  delete right_bracket_gradient;
990  delete funval;
991  delete g;
992  delete h;
993  delete xi;
994  delete d;
995  delete extx;
996  delete g2;
997  delete grad;
998  delete extg;
999  delete theta;
1000  delete xbest;
1001  delete gbest;
1002 }
1003 
1008 void derch(const double& _f, const dvector& _x, const dvector& _gg, int n,
1009  const int & _ireturn)
1010 {
1011  double& f = (double&)_f;
1012  int& ireturn=(int&) _ireturn;
1013  dvector& x = (dvector&) _x;
1014  dvector& gg = (dvector&) _gg;
1015  static long int n2;
1016  static int i, n1;
1017  static double fsave;
1018  static double s, f1, f2, g2, xsave;
1019  static int j = 1;
1020  static int si;
1021  si=gg.indexmax();
1022  static dvector g(1,si);
1023 
1024  if (ireturn == 4 ) goto label4;
1025  else if (ireturn == 5) goto label5;
1026  g=gg;
1027  while (j > 0)
1028  {
1029  //cout << "\nEntering derivative checker.\n";
1030  cout << "\n Enter index (1 ... "<< n <<") of derivative to check.";
1031  cout << " To check all derivatives, enter 0: ";
1032  cin >> j;
1033  if (j <= 0)
1034  {
1035  cout << "\n Checking all derivatives. Press X to terminate checking.\n";
1036  n1 = 1;
1037  n2 = n;
1038  }
1039  else
1040  {
1041  n1 = j;
1042  n2 = j;
1043  }
1044  cout << " Enter step size (to quit derivative checker, enter 0): ";
1045  cin >> s;
1046  cout <<
1047  "\n X Function Analytical Finite Diff; Index\n";
1048 
1049  if (s <= 0) ad_exit(0);
1050 
1051  for (i=n1; i<=n2; i++)
1052  {
1053  xsave=x(i);
1054  x(i)=xsave+s;
1055  fsave = f;
1056  ireturn = 4; // fgcomp(&f1,x,g1,n, params, vars);
1057  return;
1058 
1059  label4:
1060  f1 = f;
1061  x(i)=xsave-s;
1062  ireturn= 5; //fgcomp(&f2,x,g1,n, params, vars);
1063  return;
1064 
1065  label5:
1066  f2 = f;
1067  f = fsave;
1068  x(i)=xsave;
1069  g2=(f1-f2)/(2.*s);
1070  ad_printf(" %12.5e %12.5e %12.5e %12.5e ; %5d \n",
1071  x(i), f, g(i), g2, i);
1072  } // for loop
1073  } // while (j > 0)
1074 // ireturn = 2;
1075  ad_exit(0);
1076 }
1077 // #undef DIAG
int lin_flag
Definition: fvar.hpp:3657
unsigned char curx
Definition: fmm_disp.cpp:90
#define getch
Definition: conjprod.cpp:66
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
int maxfn
Definition: fvar.hpp:3631
double right_bracket_value
Definition: fvar.hpp:3680
double gg
Definition: fvar.hpp:3663
double dfn
Definition: fvar.hpp:3686
unsigned char wintop
Definition: fmm_disp.cpp:84
void derch(const double &_f, const dvector &_x, const dvector &_gg, int n, const int &_ireturn)
Description not yet available.
Definition: conjprod.cpp:1008
Description not yet available.
Definition: fvar.hpp:3628
double Psi_1
Definition: fvar.hpp:3674
dvector * grad
Definition: fvar.hpp:3652
double extf
Definition: fvar.hpp:3671
~fmmc()
Description not yet available.
Definition: conjprod.cpp:986
int iprint
Definition: fvar.hpp:3634
double dir_deriv
Definition: fvar.hpp:3675
Description not yet available.
Definition: fmm_disp.cpp:82
dvector * xi
Definition: fvar.hpp:3648
double dgg
Definition: fvar.hpp:3666
double crit
Definition: fvar.hpp:3632
int ext_flag
Definition: fvar.hpp:3658
double left_bracket
Definition: fvar.hpp:3677
#define x
Vector of double precision numbers.
Definition: dvector.h:50
unsigned char screenwidth
Definition: fmm_disp.cpp:89
long iexit
Definition: fvar.hpp:3688
int int_flag
Definition: fvar.hpp:3659
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
double left_bracket_value
Definition: fvar.hpp:3678
void fmin(const double &f, const dvector &p, const dvector &gg)
Description not yet available.
Definition: conjprod.cpp:144
double gamma
Definition: fvar.hpp:3669
void clrscr()
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: clrscr.cpp:8
fmmc(const int &n)
Description not yet available.
Definition: conjprod.cpp:942
double do_interpolate(const double &fret, const double &left_bracket, double &left_bracket_value, const dvector &left_bracket_gradient, double &right_bracket, const double &right_bracket_value, dvector &right_bracket_gradient, const dvector &theta, const dvector &d, const int &J, long int &ifn, const double &crit1, int &int_flag, const double &rho_1, const double &Psi_2, const dvector &g1)
Description not yet available.
Definition: conjprod.cpp:622
exitptr ad_exit
Definition: gradstrc.cpp:53
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
unsigned char screenheight
Definition: fmm_disp.cpp:88
long ihflag
Definition: fvar.hpp:3689
double norm(const d3_array &a)
Return computed norm value of a.
Definition: d3arr2a.cpp:190
int imax
Definition: fvar.hpp:3640
dvector * extg
Definition: fvar.hpp:3653
dvector * extx
Definition: fvar.hpp:3650
dvector * g
Definition: fvar.hpp:3646
double Psi_0
Definition: fvar.hpp:3670
int j
Definition: fvar.hpp:3636
int ifnex
Definition: fvar.hpp:3660
double rho_0
Definition: fvar.hpp:3681
dvector * xbest
Definition: fvar.hpp:3655
double Phi(const dvector &)
prnstream & endl(prnstream &)
void gotoxy(int x, int y)
Definition: fmm_disp.cpp:76
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
double gam
Definition: fvar.hpp:3664
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
dvector * d
Definition: fvar.hpp:3649
double right_bracket
Definition: fvar.hpp:3679
#define min(a, b)
Definition: cbivnorm.cpp:188
double rho_min
Definition: fvar.hpp:3667
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
long int iter
Definition: fvar.hpp:3639
double mylinmin(const double &fret, const double &Phi_i, const dvector &theta1, const dvector &q_i, fmmc &cs)
version of mylinmin which uses the deviative to help bracket the minimum
Definition: conjprod.cpp:491
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
unsigned char currmode
Definition: fmm_disp.cpp:87
dvector * gbest
Definition: fvar.hpp:3656
#define M
Definition: rngen.cpp:57
double rho_1
Definition: fvar.hpp:3673
unsigned char winbottom
Definition: fmm_disp.cpp:85
int frp_flag
Definition: fvar.hpp:3662
Description not yet available.
Definition: fvar.hpp:2819
dvector * h
Definition: fvar.hpp:3647
unsigned char winleft
Definition: fmm_disp.cpp:84
long ihang
Definition: fvar.hpp:3641
int maxfn_flag
Definition: fvar.hpp:3687
unsigned int size() const
Get number of elements in array.
Definition: dvector.h:209
int ireturn
Definition: fvar.hpp:3661
double cubic_interpolation(const double &u, const double &v, const double &a, const double &b, double &ap, const double &bp)
Description not yet available.
Definition: conjprod.cpp:867
int scroll_flag
Definition: fvar.hpp:3635
double fbest
Definition: fvar.hpp:3682
unsigned char cury
Definition: fmm_disp.cpp:90
int J
Definition: fvar.hpp:3637
void gettextinfo(struct text_info *r)
Definition: fmm_disp.cpp:93
dvector * funval
Definition: fvar.hpp:3643
void do_extrapolate(const double &left_bracket, const double &left_bracket_value, dvector &left_bracket_gradient, const double &right_bracket, double &right_bracket_value, const dvector &right_bracket_gradient, const dvector &theta, dvector &d, const int &J, const double &rho_0, long int &ifn, const int &ifnex, int &ext_flag, const double &rho_1, const double &rf, const dvector &g1)
Description not yet available.
Definition: conjprod.cpp:796
double crit1
Definition: fvar.hpp:3672
long int ifn
Definition: fvar.hpp:3638
dvector * theta
Definition: fvar.hpp:3654
double converge_flag
Definition: fvar.hpp:3668
int quit_flag
Definition: fvar.hpp:3642
double rho_i
Definition: fvar.hpp:3676
dvector * g2
Definition: fvar.hpp:3651
dvector * left_bracket_gradient
Definition: fvar.hpp:3644
#define max(a, b)
Definition: cbivnorm.cpp:189
int onintr(int *k)
dvector * right_bracket_gradient
Definition: fvar.hpp:3645
void bracket_report(const dvector &theta, const double &left_bracket, double &right_bracket, const dvector &d)
Description not yet available.
Definition: conjprod.cpp:847
unsigned char winright
Definition: fmm_disp.cpp:85
double fp
Definition: fvar.hpp:3665
double min_improve
Definition: fvar.hpp:3633
df1_one_variable inv(const df1_one_variable &x)
Definition: df11fun.cpp:384
int ad_printf(FILE *stream, const char *format, Args...args)
Definition: fvar.hpp:9487
int kbhit(void)
Definition: df1b2fun.cpp:15
d3_array pow(const d3_array &m, int e)
Description not yet available.
Definition: d3arr6.cpp:17
int ctlc_flag
Definition: gradstrc.cpp:68