ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
derch.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 #ifdef __ZTC__
12  #include <conio.h>
13 #endif
14 
15 #include "fvar.hpp"
16 
17 #if defined(_WIN32)
18  #include <conio.h>
19 #endif
20 
21 #include <ctype.h>
22 #include <stdlib.h>
23 
24 #ifndef OPT_LIB
25  #include <cassert>
26  #include <climits>
27 #endif
28 
29 int derchflag=0;
30 double derch_stepsize=0.0;
31 
32 static ofstream* pofs=0;
33 
38 void derch(const double& _f, const independent_variables & _x,
39  const dvector& _gg, int n, const int & _ireturn)
40 {
41  dvector& gg=(dvector&) _gg;
42  double& f=(double&) _f;
43  int& ireturn = (int&) _ireturn;
45  static int i, n1 ,n2,ii;
46  static double fsave;
47  static int order_flag;
48  static double s, f1, f2, g2, xsave;
49  static int j = 1;
50  static int si;
51  si=gg.indexmax();
52  static dvector g(1,si);
53  static dvector index(1,si);
54 
55  if (ireturn == 4 ) goto label4;
56  else if (ireturn == 5) goto label5;
57  g=gg;
58  {
59  int maxind = g.indexmin();
60  double maxg=fabs(g(maxind));
61  dmatrix tmp(1,3,g.indexmin(),g.indexmax());
62  tmp(1).fill_seqadd(1,1);
63  tmp(2)=g;
64  tmp(3)=-fabs(g);
65  ofstream ofs("derivatives");
66  dmatrix dtmp=sort(trans(tmp),3);
67  ofs << dtmp << endl;
68  for (int i=g.indexmin();i<=g.indexmax();i++)
69  {
70  if (fabs(g(i))>maxg)
71  {
72  maxg=fabs(g(i));
73  maxind=i;
74  }
75  }
76  cout << "maxind = " << maxind << " maxg = " << maxg << endl;
77  index=column(dtmp,1);
78  }
79  while (j > 0)
80  {
81  cout << "\nEntering derivative checker.\n";
82  cout << " Enter index (1 ... "<< n <<") of derivative to check.\n";
83  cout << " To check all derivatives, enter 0;\n";
84  cout << " To quit enter -1: ";
85  flush(cout);
86  cin >> j;
87  if (j == -1)
88  {
89  ireturn = -1;
90  return;
91  }
92  else if (j == 0)
93  {
94  cout << "\n Checking all derivatives. Press X to terminate checking.\n";
95  flush(cout);
96  pofs=new ofstream("ders.dat");
97  (*pofs) << " index analytical finite % error " << endl;
98  (*pofs) << " difference " << endl;
99  n1 = 1;
100  n2 = n;
101  }
102  else if (j >= 1 && j <= n)
103  {
104  n1 = j;
105  n2 = j;
106  }
107  else
108  {
109  cout << "Error: Invalid derivative index \"" << j
110  << "\" entered which is not in range (1 ... "<< n <<").\n";
111  ad_exit(1);
112  }
113  cout << "\n Enter step size.\n";
114  cout << " To quit derivative checker enter -1;\n";
115  cout << " to check for uninitialized variables enter 0): ";
116  flush(cout);
117 #if defined(__BORLANDC__)
118  char mystring[1000];
119  cin >> mystring;
120  s=atof(mystring);
121 #else
122  cin >> s;
123 #endif
124  if (s < 0) ad_exit(0);
125 
126  if (j==0)
127  {
128  cout << "\n If you want the derivatives approximated in order\n"
129  << " of decreasing magnitude enter 1;\n"
130  << " else enter 0: ";
131  flush(cout);
132  int tmpint=0;
133  cin >> tmpint;
134  if (tmpint==1)
135  order_flag=1;
136  else
137  order_flag=0;
138  }
139  if (s != 0)
140  {
141  cout <<
142  "\n X Function Analytical Finite Diff ; Index"
143  << endl;
144  }
145 
146  for (ii=n1; ii<=n2; ii++)
147  {
148  i = ii;
149  if (order_flag==1)
150  {
151  int count = 0;
152  for (int _ii = index.indexmin(); _ii <= index.indexmax(); ++_ii)
153  {
154 #ifdef OPT_LIB
155  int idx = (int)index(_ii);
156 #else
157  double _idx = index(_ii);
158  assert(_idx <= (double)INT_MAX);
159  int idx = (int)_idx;
160 #endif
161  if (x.indexmin() <= idx && idx <= x.indexmax())
162  {
163  ++count;
164  }
165  if (count == ii)
166  {
167  i = idx;
168  break;
169  }
170  }
171  }
172  derch_stepsize=s;
173  derchflag=1;
174  xsave=x(i);
175  x(i)=xsave+s;
176  fsave = f;
177  ireturn = 4; // fgcomp(&f1,x,g1,n, params, vars);
178  return;
179 
180  label4:
181  derch_stepsize=s;
182  derchflag=-1;
183  f1 = f;
184  x(i)=xsave-s;
185  ireturn= 5; //fgcomp(&f2,x,g1,n, params, vars);
186  return;
187 
188  label5:
189  f2 = f;
190  f = fsave;
191  x(i)=xsave;
192  if (s==0.0)
193  {
194  if (fabs(f1-f2)>0.0)
195  {
196  cout << "There appear to be uninitialized variables in "
197  << "the objective function " << endl
198  << " f1 = " << setprecision(15) << f1
199  << " f2 = " << setprecision(15) << f2 << endl;
200  }
201  else
202  {
203  cout << "There do not appear to be uninitialized variables in" << endl
204  << "the objective function " << endl;
205  }
206  }
207  else
208  {
209  g2=(f1-f2)/(2.*s);
210  derchflag=0;
211  double perr= fabs(g2-g(i))/(fabs(g(i))+1.e-6);
212 
213  if (pofs)
214  {
215  if (perr > 1.e-3)
216  (*pofs) << " ** ";
217  else
218  (*pofs) << " ";
219  (*pofs) << " " << setw(4) << i
220  << " " << setw(12) << g(i)
221  << " " << setw(12) << g2
222  << " " << setw(12) << perr
223  << endl;
224  }
225  ad_printf(" %12.5e %12.5e %12.5e %12.5e ; %5d \n",
226  x(i), f, g(i), g2, i);
227  fflush(stdout);
228  }
229 #if defined(_MSC_VER)
230  if ( kbhit() )
231  {
232  int c = toupper(getch());
233  if ( c == 'X')
234  {
235  cout << " Exiting derivative checker by user interrupt.\n";
236  ad_exit(0);
237  }
238  }
239 #endif
240  } // for loop
241  } // while (j > 0)
242 // ireturn = 2;
243  ad_exit(0);
244 }
#define getch
Definition: conjprod.cpp:66
double gg
Definition: fvar.hpp:3663
void derch(const double &_f, const dvector &_x, const dvector &_gg, int n, const int &_ireturn)
Description not yet available.
Definition: conjprod.cpp:1008
dmatrix trans(const dmatrix &m1)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat2.cpp:13
int derchflag
Definition: derch.cpp:29
#define x
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
exitptr ad_exit
Definition: gradstrc.cpp:53
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
static ofstream * pofs
Definition: derch.cpp:32
dvector * g
Definition: fvar.hpp:3646
int j
Definition: fvar.hpp:3636
dmatrix sort(const dmatrix &m, int column, int NSTACK)
Description not yet available.
Definition: dmsort.cpp:17
prnstream & endl(prnstream &)
Description not yet available.
Definition: fvar.hpp:1937
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Description not yet available.
Definition: fvar.hpp:2819
double fill_seqadd(double, double)
Description not yet available.
Definition: dmat21.cpp:35
int ireturn
Definition: fvar.hpp:3661
double derch_stepsize
Definition: derch.cpp:30
dvector column(const dmatrix &matrix, int j)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat6.cpp:13
dvector * g2
Definition: fvar.hpp:3651
int ad_printf(FILE *stream, const char *format, Args...args)
Definition: fvar.hpp:9487
int kbhit(void)
Definition: df1b2fun.cpp:15