ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
scbound.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 #include "fvar.hpp"
12 #ifdef __TURBOC__
13  #pragma hdrstop
14 #endif
15 
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <math.h>
19 
20 double dmin(double,double);
21 double dmax(double, double);
22 
23 #define USE_BARD_PEN
24 
29 dvariable boundp(dvariable xx, double fmin, double fmax,
30  const prevariable& _fpen, const double& s)
31 {
32  prevariable& fpen=(prevariable&) _fpen;
33 
34  dvariable t,y,x;
35  x=xx/s;
36  const double l4=log(4.0);
37  dvariable ss=(sin(x*1.57079632679489661)+1.)/2.;
38  double diff=fmax-fmin;
39  t=fmin + diff*ss;
40 #ifdef USE_BARD_PEN
41  //cout << "xxxxx" << endl;
42  double pen=.00001/diff;
43  fpen=fpen-pen*(log(ss+1.e-40)+log((1.0-ss)+1.e-40)+l4);
44 #else
45 
46  if (x < -.9999)
47  {
48  fpen+=(x+0.9999)*(x+0.9999);
49  }
50 
51  if (x > 0.9999)
52  {
53  fpen+=(x-0.9999)*(x-0.9999);
54  }
55 
56  if (x < -1.)
57  {
58  fpen+=1000.*(x+1.)*(x+1.);
59  }
60 
61  if (x > 1.)
62  {
63  fpen+=1000.*(x-1.)*(x-1.);
64  }
65 #endif
66  return(t);
67 }
68 
73 double boundp( double xx, double fmin, double fmax, const double& _fpen,
74  const double& s)
75 {
76  double& fpen = (double&)_fpen;
77  double t,x;
78  x=xx/s;
79  const double l4=log(4.0);
80  double ss=(sin(x*1.57079632679489661)+1.)/2.;
81  double diff=fmax-fmin;
82  t=fmin + diff*ss;
83 #ifdef USE_BARD_PEN
84  //cout << "xxxxx" << endl;
85  double pen=.00001/diff;
86  fpen-=pen*(log(ss+1.e-40)+log((1.0-ss)+1.e-40)+l4);
87 #else
88  if (x < -.9999)
89  {
90  fpen+=(x+0.9999)*(x+0.9999);
91  }
92 
93  if (x > 0.9999)
94  {
95  fpen+=(x-0.9999)*(x-0.9999);
96  }
97 
98  if (x < -1)
99  {
100  fpen+=1000*(x+1)*(x+1);
101  }
102 
103  if (x > 1)
104  {
105  fpen+=1000*(x-1)*(x-1);
106  }
107 #endif
108  return(t);
109 }
110 
115 double boundpin(double x, double fmin, double fmax,const double& s)
116 {
117  double tinv;
118 
119  if (x < fmin)
120  {
121  ad_printf("variable out of bounds in boundpin: variable = %lg", x);
122  ad_printf("; min = %lg", fmin);
123  ad_printf("; max = %lg\n", fmax);
124 
125  x=dmin(fmin+.001,fmin+.01*(fmax-fmin));
126  }
127 
128  if (x > fmax)
129  {
130  ad_printf("variable out of bounds in boundpin: variable = %lg", x);
131  ad_printf("; min = %lg", fmin);
132  ad_printf("; max = %lg\n", fmax);
133 
134  x=dmax(fmax-.001,fmax-.01*(fmax-fmin));
135  }
136 
137  tinv=::asin(2.*(x-fmin)/(fmax-fmin)-1.)/1.57079632679489661;
138  return(s*tinv);
139 }
140 
141 /*
142 double boundpin(const prevariable& xx, double fmin, double fmax,
143  const double& s)
144 {
145  double tinv;
146  double x=value(xx);
147 
148  if (x < fmin)
149  {
150  ad_printf("variable out of bounds in boundpin: variable = %lg", x);
151  ad_printf("; min = %lg", fmin);
152  ad_printf("; max = %lg\n", fmax);
153 
154  x=dmin(fmin+.001,fmin+.01*(fmax-fmin));
155  }
156 
157  if (x > fmax)
158  {
159  ad_printf("variable out of bounds in boundpin: variable = %lg", x);
160  ad_printf("; min = %lg", fmin);
161  ad_printf("; max = %lg\n", fmax);
162 
163  x=dmax(fmax-.001,fmax-.01*(fmax-fmin));
164  }
165 
166  tinv=::asin(2.*(x-fmin)/(fmax-fmin)-1.)/1.57079632679489661;
167  return(s*tinv);
168 }
169 */
Base class for dvariable.
Definition: fvar.hpp:1315
#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)
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
#define dmax(a, b)
Definition: cbivnorm.cpp:191
double boundpin(double x, double fmin, double fmax, double s)
Scale model variable over [-1,1]; constant objects.
Definition: boundfun.cpp:378
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
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
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