ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
boundfun.cpp
Go to the documentation of this file.
1 
11 #include "fvar.hpp"
12 
18 #define USE_BARD_PEN
19 
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <math.h>
23 
24 // function prototypes duplicated in fvar.hpp
25 // double dmin(double, double);
26 // double dmax(double, double);
27 
37 dvariable dfatan1(dvariable x, double fmin, double fmax,
38  const prevariable& _fpen)
39 {
40  prevariable& fpen=(prevariable&) _fpen;
41  dvariable t;
42 
43  t= (atan(x)/PI);
44  t=( t +.5 );
45  t= t *( fmax-fmin ) + fmin;
46  t=( (atan(x)/PI) +.5 )*( fmax-fmin ) + fmin;
47 
48  if (x < -12.)
49  {
50  fpen+=.1*(x+12.)*(x+12.);
51  }
52 
53  if (x > 12.)
54  {
55  fpen+=.1*(x-12.)*(x-12.);
56  }
57  return(t);
58 }
66 double dftinv(double x, double fmin, double fmax)
67 {
68  if (x <= fmin)
69  {
70  ad_printf("variable out of bounds in dftinv\nvariable = %lg", x);
71  ad_printf("lower bound = %lg", fmin);
72  ad_printf("upper bound = %lg\n", fmax);
73 
74  x=dmin(fmin+.001,fmin+.01*(fmax-fmin));
75  }
76 
77  double tinv = tan( ((x-fmin)/(fmax-fmin) -.5) * PI);
78  return tinv;
79 }
89 dvariable boundp(const prevariable& x, double fmin, double fmax,
90  const prevariable& _fpen,double s)
91 {
92  return boundp(x/s,fmin,fmax,_fpen);
93 }
102 dvariable boundp(const prevariable& x, double fmin, double fmax,
103  const prevariable& _fpen)
104 {
106  {
107  prevariable& fpen=(prevariable&) _fpen;
108  dvariable t,y;
109  double diff=fmax-fmin;
110  const double l4=log(4.0);
111  dvariable ss=0.4999999999999999*sin(x*1.57079632679489661)+0.50;
112  t=fmin + diff*ss;
113 
114  #ifdef USE_BARD_PEN
115  double pen=.000001/diff;
116  fpen-=pen*(log(ss+1.e-40)+log((1.0-ss)+1.e-40)+l4);
117  #else
118  if (x < -.9999)
119  {
120  fpen+=cube(-0.9999-x);
121  if (x < -1.)
122  {
123  fpen+=1.e+6*cube(-1.0-x);
124  if (x < -1.02)
125  {
126  fpen+=1.e+10*cube(-1.02-x);
127  }
128  }
129  }
130  if (x > 0.9999)
131  {
132  fpen+=cube(x-0.9999);
133  if (x > 1.)
134  {
135  fpen+=1.e+6*cube(x-1.);
136  if (x > 1.02)
137  {
138  fpen+=1.e+10*cube(x-1.02);
139  }
140  }
141  }
142  #endif
143  return(t);
144  }
145  else
146  {
147  double diff=fmax-fmin;
148  dvariable t,y;
149  if (x>-20)
150  {
151  y=1.0/(1+exp(-x));
152  }
153  else
154  {
155  dvariable u=exp(x);
156  y=u/(1.0+u);
157  }
158  t=fmin + diff*y;
159  return(t);
160  }
161 }
170 dvariable dfboundp(const prevariable& x, double fmin,double fmax)
171 {
173  {
174  return (fmax-fmin)*0.499999999999999*1.57079632679489661
175  *cos(x*1.57079632679489661);
176  }
177  else
178  {
179  double diff=fmax-fmin;
180  dvariable dfy;
181  if (x>-20)
182  {
183  dvariable u=exp(-x);
184  //y=1.0/(1+u);
185  dfy=u/square(1.0+u);
186  }
187  else
188  {
189  dvariable u=exp(x);
190  //y=u/(1.0+u);
191  dfy=u/square(1.0+u);
192  }
193  if (dfy==0)
194  {
195  // cout << "error in dfboundp" << endl;
196  }
197  return diff*dfy;
198  }
199 }
200 
210 double ndfboundp( double x, double fmin, double fmax,const double& fpen)
211 {
213  {
214  return (fmax-fmin)*0.499999999999999*1.57079632679489661
215  *cos(x*1.57079632679489661);
216  }
217  else
218  {
219  double diff=fmax-fmin;
220  double dfy;
221  if (x>-20)
222  {
223  double u=exp(-x);
224  //y=1.0/(1+u);
225  dfy=u/square(1.0+u);
226  }
227  else
228  {
229  double u=exp(x);
230  //y=u/(1.0+u);
231  dfy=u/square(1.0+u);
232  }
233  return diff*dfy;
234  }
235 }
236 
244 double boundp(double x, double fmin, double fmax)
245 {
247  {
248  double t;
249  double diff=fmax-fmin;
250  double ss=0.49999999999999999*sin(x*1.57079632679489661)+0.50;
251  t=fmin + diff*ss;
252  return(t);
253  }
254  else
255  {
256  double diff=fmax-fmin;
257  double t,y;
258  if (x>-20)
259  {
260  y=1.0/(1+exp(-x));
261  }
262  else
263  {
264  double u=exp(x);
265  y=u/(1.0+u);
266  }
267  t=fmin + diff*y;
268  return(t);
269  }
270 }
271 
282 double nd2fboundp( double x, double fmin, double fmax,const double& fpen)
283 {
284  if (x<-0.99999)
285  {
286  return (boundp(x,fmin,fmax,fpen)-2.*boundp(x+1.e-6,fmin,fmax,fpen)
287  +boundp(x+2.e-6,fmin,fmax,fpen))/1.e-12;
288  }
289  else if (x>0.99999)
290  {
291  return (boundp(x-2.e-6,fmin,fmax,fpen)-2.*boundp(x-1.e-6,fmin,fmax,fpen)
292  +boundp(x,fmin,fmax,fpen))/1.e-12;
293  }
294  else
295  {
296  return (boundp(x+1.e-6,fmin,fmax,fpen)-2.*boundp(x,fmin,fmax,fpen)
297  +boundp(x-1.e-6,fmin,fmax,fpen))/1.e-12;
298  }
299 }
310 double boundp( double x, double fmin, double fmax,const double& _fpen)
311 {
313  {
314  double t;
315  double& fpen=(double&) _fpen;
316  double diff=fmax-fmin;
317  const double l4=log(4.0);
318  double ss=0.499999999999999*sin(x*1.57079632679489661)+0.50;
319  t=fmin + diff*ss;
320  #ifdef USE_BARD_PEN
321  double pen=.001/diff;
322  fpen-=pen*(log(ss+1.e-40)+log((1.0-ss)+1.e-40)+l4);
323  #else
324  if (x < -.9999)
325  {
326  fpen+=(x+0.9999)*(x+0.9999);
327  if (x < -1.)
328  {
329  fpen+=1.e+6*(x+1.)*(x+1.);
330  if (x < -1.02)
331  {
332  fpen+=1.e+10*(x+1.02)*(x+1.02);
333  }
334  }
335  }
336  if (x > 0.9999)
337  {
338  fpen+=(x-0.9999)*(x-0.9999);
339  if (x > 1.)
340  {
341  fpen+=1.e+6*(x-1.)*(x-1.);
342  if (x > 1.02)
343  {
344  fpen+=1.e+10*(x-1.02)*(x-1.02);
345  }
346  }
347  }
348  #endif
349  return(t);
350  }
351  else
352  {
353  double diff=fmax-fmin;
354  double t,y;
355  if (x>-20)
356  {
357  y=1.0/(1+exp(-x));
358  }
359  else
360  {
361  double u=exp(x);
362  y=u/(1.0+u);
363  }
364  t=fmin + diff*y;
365  return(t);
366  }
367 }
368 
378 double boundpin(double x, double fmin, double fmax,double s)
379 {
380  return s*boundpin(x,fmin,fmax);
381 }
382 
391 double boundpin(double x, double fmin, double fmax)
392 {
393  if (x < fmin)
394  {
395  ad_printf("variable out of bounds in boundpin: variable = %lg", x);
396  ad_printf("; min = %lg", fmin);
397  ad_printf("; max = %lg\n", fmax);
398  x=dmin(fmin+.001,fmin+.01*(fmax-fmin));
399  }
400 
401  if (x > fmax)
402  {
403  ad_printf("variable out of bounds in boundpin: variable = %lg", x);
404  ad_printf("; min = %lg", fmin);
405  ad_printf("; max = %lg\n", fmax);
406 
407  x=dmax(fmax-.001,fmax-.01*(fmax-fmin));
408  }
409 
410  double tinv;
412  {
413  tinv=::asin(2.*(x-fmin)/(fmax-fmin)-1.)/1.57079632679489661;
414  }
415  else
416  {
417  //double y=(x-fmin)/(fmax-fmin);
418  //double u=1.e-20+y/(1.e-20+(1.0-y));
419  double y=1.e-20+(fmax-x)/(1.e-20+(x-fmin));
420  tinv=-log(y);
421  }
422  return(tinv);
423 }
434 double boundpin(const prevariable& x, double fmin, double fmax,double s)
435 {
436  return s*boundpin(x,fmin,fmax);
437 }
438 
447 double boundpin(const prevariable& xx, double fmin, double fmax)
448 {
449  double tinv;
450  double x=value(xx);
451 
452  if (x < fmin)
453  {
454  ad_printf("variable out of bounds in boundpin: variable = %lg", x);
455  ad_printf("; min = %lg", fmin);
456  ad_printf("; max = %lg\n", fmax);
457 
458  x=dmin(fmin+.001,fmin+.01*(fmax-fmin));
459  }
460 
461  if (x > fmax)
462  {
463  ad_printf("variable out of bounds in boundpin: variable = %lg", x);
464  ad_printf("; min = %lg", fmin);
465  ad_printf("; max = %lg\n", fmax);
466 
467  x=dmax(fmax-.001,fmax-.01*(fmax-fmin));
468  }
470  {
471  tinv=::asin(2.*(x-fmin)/(fmax-fmin)-1.)/1.57079632679489661;
472  }
473  else
474  {
475  //double y=(x-fmin)/(fmax-fmin);
476  //double u=1.e-20+y/(1.e-20+(1.0-y));
477  //tinv=-log(u);
478  double y=1.e-20+(fmax-x)/(1.e-20+(x-fmin));
479  tinv=-log(y);
480  }
481 
482  return(tinv);
483 }
489 double dmin(double x, double y)
490 {
491  if (x<y)
492  {
493  return (x);
494  }
495  else
496  {
497  return(y);
498  }
499 }
505 double dmax(double x, double y)
506 {
507  if (x>y)
508  {
509  return (x);
510  }
511  else
512  {
513  return(y);
514  }
515 }
Base class for dvariable.
Definition: fvar.hpp:1315
d3_array tan(const d3_array &arr3)
Returns d3_array results with computed tan from elements in arr3.
Definition: d3arr2a.cpp:73
#define x
void fmin(double f, const independent_variables &x, const dvector &g, const int &n, const dvector &w, const dvector &h, const fmm_control &fmc)
df1_one_variable atan(const df1_one_variable &x)
Definition: df11fun.cpp:312
d3_array sin(const d3_array &arr3)
Returns d3_array results with computed sin from elements in arr3.
Definition: d3arr2a.cpp:43
#define dmin(a, b)
Definition: cbivnorm.cpp:190
d3_array cube(const d3_array &m)
Description not yet available.
Definition: d3arr5.cpp:17
#define dmax(a, b)
Definition: cbivnorm.cpp:191
double nd2fboundp(double x, double fmin, double fmax, const double &fpen)
Scale input variable between upper and lower bounds and compute a penalty for exceeding the bounds...
Definition: boundfun.cpp:282
double boundpin(double x, double fmin, double fmax, double s)
Scale model variable over [-1,1]; constant objects.
Definition: boundfun.cpp:378
dvariable dfatan1(dvariable x, double fmin, double fmax, const prevariable &_fpen)
Scale input variable between upper and lower bounds and compute a penalty for exceeding the bounds...
Definition: boundfun.cpp:37
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
double ndfboundp(double x, double fmin, double fmax, const double &fpen)
Derivatative code for double nd2fboundp( double x, double fmin, double fmax,const double&amp; fpen) ...
Definition: boundfun.cpp:210
double dftinv(double x, double fmin, double fmax)
Inverse of dvariable dfatan1(dvariable x, double fmin, double fmax, const prevariable&amp; _fpen) ...
Definition: boundfun.cpp:66
d3_array cos(const d3_array &arr3)
Returns d3_array results with computed cos from elements in arr3.
Definition: d3arr2a.cpp:58
static int Hybrid_bounded_flag
dvariable dfboundp(const prevariable &x, double fmin, double fmax)
Computes the derivative of dvariable boundp(const prevariable&amp; x, double fmin, double fmax...
Definition: boundfun.cpp:170
dvariable boundp(const prevariable &x, double fmin, double fmax, const prevariable &_fpen, double s)
Compute penalty for exceeding bounds on parameter; variable ojbects.
Definition: boundfun.cpp:89
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
#define PI
Definition: fvar.hpp:95
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
int ad_printf(FILE *stream, const char *format, Args...args)
Definition: fvar.hpp:9487
dvector asin(const dvector &vec)
Returns dvector with principal value of the arc sine of vec, expressed in radians.
Definition: dvect6.cpp:229