1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-24 Bradley M. Bell
// ----------------------------------------------------------------------------
/*
@begin atomic_two_rev_sparse_hes.cpp@@
$section Atomic Reverse Hessian Sparsity: Example and Test$$
$head Purpose$$
This example demonstrates calculation of the reverse Hessian sparsity pattern
for an atomic operation.
$head function$$
For this example, the atomic function
$latex f : \B{R}^3 \rightarrow \B{R}^2$$ is defined by
$latex \[
f( x ) = \left( \begin{array}{c}
x_2 * x_2 \\
x_0 * x_1
\end{array} \right)
\] $$
The Hessians of the component functions are
$latex \[
f_0^{(2)} ( x ) = \left( \begin{array}{ccc}
0 & 0 & 0 \\
0 & 0 & 0 \\
0 & 0 & 2
\end{array} \right)
\W{,}
f_1^{(2)} ( x ) = \left( \begin{array}{ccc}
0 & 1 & 0 \\
1 & 0 & 0 \\
0 & 0 & 0
\end{array} \right)
\] $$
$head Start Class Definition$$
$srccode%cpp% */
# include <cppad/cppad.hpp>
namespace { // isolate items below to this file
using CppAD::vector; // abbreviate as vector
//
class atomic_rev_sparse_hes : public CppAD::atomic_base<double> {
/* %$$
$head Constructor $$
$srccode%cpp% */
public:
// constructor (could use const char* for name)
atomic_rev_sparse_hes(const std::string& name) :
// this example only uses pack sparsity patterns
CppAD::atomic_base<double>(name, pack_sparsity_enum)
{ }
private:
/* %$$
$head forward$$
$srccode%cpp% */
// forward mode routine called by CppAD
virtual bool forward(
size_t p ,
size_t q ,
const vector<bool>& vx ,
vector<bool>& vy ,
const vector<double>& tx ,
vector<double>& ty
)
{
# ifndef NDEBUG
size_t n = tx.size() / (q + 1);
size_t m = ty.size() / (q + 1);
# endif
assert( n == 3 );
assert( m == 2 );
// return flag
bool ok = q == 0;
if( ! ok )
return ok;
// check for defining variable information
// This case must always be implemented
if( vx.size() > 0 )
{ vy[0] = vx[0];
vy[1] = vx[0] || vy[0];
}
// Order zero forward mode.
// This case must always be implemented
// f(x) = [ x_0 * x_0 ]
// [ x_0 * x_1 ]
assert( p <= 0 );
if( p <= 0 )
{ ty[0] = tx[2] * tx[2];
ty[1] = tx[0] * tx[1];
}
return ok;
}
/* %$$
$head for_sparse_jac$$
$srccode%cpp% */
// forward Jacobian sparsity routine called by CppAD
virtual bool for_sparse_jac(
size_t q ,
const CppAD::vectorBool& r ,
CppAD::vectorBool& s ,
const vector<double>& x )
{ // This function needed because we are using ForSparseHes
// with afun.option( CppAD::atomic_base<double>::pack_sparsity_enum )
# ifndef NDEBUG
size_t n = r.size() / q;
size_t m = s.size() / q;
# endif
assert( n == x.size() );
assert( n == 3 );
assert( m == 2 );
// f'(x) = [ 0, 0, 2 x_2 ]
// [ x_1, x_0, 0 ]
// sparsity for first row of S(x) = f'(x) * R
size_t i = 0;
for(size_t j = 0; j < q; j++)
s[ i * q + j ] = r[ 2 * q + j ];
// sparsity for second row of S(x) = f'(x) * R
i = 1;
for(size_t j = 0; j < q; j++)
s[ i * q + j ] = r[ 0 * q + j ] || r[ 1 * q + j];
return true;
}
/* %$$
$head rev_sparse_jac$$
$srccode%cpp% */
// reverse Jacobian sparsity routine called by CppAD
virtual bool rev_sparse_jac(
size_t q ,
const CppAD::vectorBool& rt ,
CppAD::vectorBool& st ,
const vector<double>& x )
{ // This function needed because we are using ForSparseHes
// with afun.option( CppAD::atomic_base<double>::pack_sparsity_enum )
# ifndef NDEBUG
size_t m = rt.size() / q;
size_t n = st.size() / q;
# endif
assert( n == x.size() );
assert( n == 3 );
assert( m == 2 );
// [ 0, x_1 ]
// f'(x)^T = [ 0, x_0 ]
// [ 2 x_2, 0 ]
// sparsity for first row of S(x)^T = f'(x)^T * R^T
size_t i = 0;
for(size_t j = 0; j < q; j++)
st[ i * q + j ] = rt[ 1 * q + j ];
// sparsity for second row of S(x)^T = f'(x)^T * R^T
i = 1;
for(size_t j = 0; j < q; j++)
st[ i * q + j ] = rt[ 1 * q + j ];
// sparsity for third row of S(x)^T = f'(x)^T * R^T
i = 2;
for(size_t j = 0; j < q; j++)
st[ i * q + j ] = rt[ 0 * q + j ];
return true;
}
/* %$$
$head rev_sparse_hes$$
$srccode%cpp% */
// reverse Hessian sparsity routine called by CppAD
virtual bool rev_sparse_hes(
const vector<bool>& vx,
const vector<bool>& s ,
vector<bool>& t ,
size_t q ,
const CppAD::vectorBool& r ,
const CppAD::vectorBool& u ,
CppAD::vectorBool& v ,
const vector<double>& x )
{ // This function needed because we are using RevSparseHes
// with afun.option( CppAD::atomic_base<double>::pack_sparsity_enum )
# ifndef NDEBUG
size_t m = s.size();
size_t n = t.size();
# endif
assert( x.size() == n );
assert( r.size() == n * q );
assert( u.size() == m * q );
assert( v.size() == n * q );
assert( n == 3 );
assert( m == 2 );
//
// f'(x) = [ 0, 0, 2 x_2 ]
// [ x_1, x_0, 0 ]
//
// [ 0 , 0 , 0 ] [ 0 , 1 , 0 ]
// f_0''(x) = [ 0 , 0 , 0 ] f_1^{(2)} (x) = [ 1 , 0 , 0 ]
// [ 0 , 0 , 2 ] [ 0 , 0 , 0 ]
// ------------------------------------------------------------------
// sparsity pattern for row vector T(x) = S(x) * f'(x)
t[0] = s[1];
t[1] = s[1];
t[2] = s[0];
// ------------------------------------------------------------------
// sparsity pattern for W(x) = f'(x)^T * U(x)
for(size_t j = 0; j < q; j++)
{ v[ 0 * q + j ] = u[ 1 * q + j ];
v[ 1 * q + j ] = u[ 1 * q + j ];
v[ 2 * q + j ] = u[ 0 * q + j ];
}
// ------------------------------------------------------------------
// sparsity pattern for Q(x) = W(x) + S_0 (x) * f_0^{(2)} (x) * R
if( s[0] )
{ for(size_t j = 0; j < q; j++)
{ // cannot use |= with vectorBool
v[ 2 * q + j ] = bool(v[ 2 * q + j ]) || bool(r[ 2 * q + j ]);
}
}
// ------------------------------------------------------------------
// sparsity pattern for V(x) = Q(x) + S_1 (x) * f_1^{(2)} (x) * R
if( s[1] )
{ for(size_t j = 0; j < q; j++)
{ // cannot use |= with vectorBool
v[ 0 * q + j ] = bool(v[ 0 * q + j ]) || bool(r[ 1 * q + j ]);
v[ 1 * q + j ] = bool(v[ 1 * q + j ]) || bool(r[ 0 * q + j ]);
}
}
return true;
}
}; // End of atomic_rev_sparse_hes class
/* %$$
$head Use Atomic Function$$
$srccode%cpp% */
bool use_atomic_rev_sparse_hes(bool x_1_variable)
{ bool ok = true;
using CppAD::AD;
using CppAD::NearEqual;
double eps = 10. * CppAD::numeric_limits<double>::epsilon();
//
// Create the atomic rev_sparse_hes object
atomic_rev_sparse_hes afun("atomic_rev_sparse_hes");
//
// Create the function f(u)
//
// domain space vector
size_t n = 3;
double x_0 = 1.00;
double x_1 = 2.00;
double x_2 = 3.00;
vector< AD<double> > au(n);
au[0] = x_0;
au[1] = x_1;
au[2] = x_2;
// declare independent variables and start tape recording
CppAD::Independent(au);
// range space vector
size_t m = 2;
vector< AD<double> > ay(m);
// call atomic function
vector< AD<double> > ax(n);
ax[0] = au[0];
ax[2] = au[2];
if( x_1_variable )
ax[1] = au[1];
else
ax[1] = x_1;
afun(ax, ay); // y = [ x_2 * x_2 , x_0 * x_1 ]^T
// create f: u -> y and stop tape recording
CppAD::ADFun<double> f;
f.Dependent (au, ay); // f(u) = y
//
// check function value
double check = x_2 * x_2;
ok &= NearEqual( Value(ay[0]) , check, eps, eps);
check = x_0 * x_1;
ok &= NearEqual( Value(ay[1]) , check, eps, eps);
// check zero order forward mode
size_t q;
vector<double> xq(n), yq(m);
q = 0;
xq[0] = x_0;
xq[1] = x_1;
xq[2] = x_2;
yq = f.Forward(q, xq);
check = x_2 * x_2;
ok &= NearEqual(yq[0] , check, eps, eps);
check = x_0 * x_1;
ok &= NearEqual(yq[1] , check, eps, eps);
// reverse sparse Hessian
CppAD::vectorBool r(n * n), s(m), h(n * n);
for(size_t i = 0; i < n; i++)
{ for(size_t j = 0; j < n; j++)
r[i * n + j] = i == j;
}
for(size_t i = 0; i < m; i++)
s[i] = true;
f.ForSparseJac(n, r);
h = f.RevSparseHes(n, s);
// check result
CppAD::vectorBool check_h(n * n);
for(size_t i = 0; i < n * n; i++)
check_h[i] = false;
check_h[ 2 * n + 2 ] = true;
if( x_1_variable )
{ check_h[0 * n + 1] = true;
check_h[1 * n + 0] = true;
}
for(size_t i = 0; i < n * n; i++)
ok &= h[ i ] == check_h[ i ];
//
return ok;
}
} // End empty namespace
/* %$$
$head Test with x_1 Both a Variable and a Parameter$$
$srccode%cpp% */
bool rev_sparse_hes(void)
{ bool ok = true;
// test with x_1 a variable
ok &= use_atomic_rev_sparse_hes(true);
// test with x_1 a parameter
ok &= use_atomic_rev_sparse_hes(false);
return ok;
}
/* %$$
$end
*/
|