File: hes_sparsity.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (470 lines) | stat: -rw-r--r-- 12,094 bytes parent folder | download
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// 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
// ----------------------------------------------------------------------------


# include <cppad/cppad.hpp>

namespace { // Begin empty namespace

void forward_sparse_jacobian_bool(CppAD::ADFun<double>& f)
{  size_t n = f.Domain();
   CPPAD_TESTVECTOR(bool) eye(n * n);
   for(size_t i = 0; i < n; i++)
   {  for(size_t j = 0; j < n; j++)
         eye[ i * n + j] = (i == j);
   }
   f.ForSparseJac(n, eye);
   return;
}
void forward_sparse_jacobian_set(CppAD::ADFun<double>& f)
{  size_t n = f.Domain();
   CPPAD_TESTVECTOR(std::set<size_t>) eye(n);
   for(size_t i = 0; i < n; i++)
      eye[i].insert(i);
   f.ForSparseJac(n, eye);
   return;
}

bool sparse_hessian_test(
   CppAD::ADFun<double>&    f     ,
   size_t                   index ,
   CPPAD_TESTVECTOR(bool)&  check )
{  bool   ok = true;
   size_t n  = f.Domain();
   size_t m  = f.Range();

   // boolean sparsity patterns
   CPPAD_TESTVECTOR(bool) r_bool(n), s_bool(m), h_bool(n * n);
   for(size_t j = 0; j < n; j++)
      r_bool[j] = true;
   for(size_t i = 0; i < m; i++)
      s_bool[i] = i == index;
   //
   // bool ForSparseHes
   h_bool = f.ForSparseHes(r_bool, s_bool);
   for(size_t i = 0; i < n * n; i++)
      ok &= h_bool[i] == check[i];
   //
   // bool RevSparseHes
   forward_sparse_jacobian_bool(f);
   h_bool = f.RevSparseHes(n, s_bool);
   for(size_t i = 0; i < n * n; i++)
      ok &= h_bool[i] == check[i];
   //
   // set sparsity patterns
   CPPAD_TESTVECTOR( std::set<size_t> ) r_set(1), s_set(1), h_set(n);
   for(size_t j = 0; j < n; j++)
      r_set[0].insert(j);
   s_set[0].insert(index);
   //
   // set ForSparseHes
   h_set = f.ForSparseHes(r_set, s_set);
   for(size_t i = 0; i < n; i++)
   {  for(size_t j = 0; j < n; j++)
      {  bool found = h_set[i].find(j) != h_set[i].end();
         ok        &= found == check[i * n + j];
      }
   }
   //
   // set RevSparseHes
   forward_sparse_jacobian_set(f);
   h_set = f.RevSparseHes(n, s_set);
   for(size_t i = 0; i < n; i++)
   {  for(size_t j = 0; j < n; j++)
      {  bool found = h_set[i].find(j) != h_set[i].end();
         ok        &= found == check[i * n + j];
      }
   }
   //
   return ok;
}



bool case_one()
{  bool ok = true;
   using namespace CppAD;

   // dimension of the domain space
   size_t n = 10;

   // dimension of the range space
   size_t m = 2;

   // temporary indices

   // initialize check values to false
   CPPAD_TESTVECTOR(bool) Check(n * n);
   for(size_t j = 0; j < n * n; j++)
      Check[j] = false;

   // independent variable vector
   CPPAD_TESTVECTOR(AD<double>) X(n);
   for(size_t j = 0; j < n; j++)
      X[j] = AD<double>(j);
   Independent(X);

   // accumulate sum here
   AD<double> sum(0.);

   // variable * variable
   sum += X[2] * X[3];
   Check[2 * n + 3] = Check[3 * n + 2] = true;

   // variable / variable
   sum += X[4] / X[5];
   Check[4 * n + 5] = Check[5 * n + 4] = Check[5 * n + 5] = true;

   // CondExpLt(variable, variable, variable, variable)
   sum += CondExpLt(X[1], X[2], sin(X[6]), cos(X[7]) );
   Check[6 * n + 6] = true;
   Check[7 * n + 7] = true;

   // pow(variable, variable)
   sum += pow(X[8], X[9]);
   Check[8 * n + 8] = Check[8 * n + 9] = true;
   Check[9 * n + 8] = Check[9 * n + 9] = true;

   // dependent variable vector
   CPPAD_TESTVECTOR(AD<double>) Y(m);
   Y[0] = sum;

   // variable - variable
   Y[1]  = X[0] - X[1];

   // create function object F : X -> Y
   ADFun<double> F(X, Y);

   // check Hessian of F_0
   ok &= sparse_hessian_test(F, 0, Check);

   // check Hessian of F_1
   for(size_t j = 0; j < n * n; j++)
      Check[j] = false;
   ok &= sparse_hessian_test(F, 1, Check);

   // -----------------------------------------------------------------------
   return ok;
}

bool case_two()
{  bool ok = true;
   using namespace CppAD;

   // dimension of the domain space
   size_t n = 4;

   // dimension of the range space
   size_t m = 1;

   // initialize check values to false
   CPPAD_TESTVECTOR(bool) Check(n * n);
   for(size_t j = 0; j < n * n; j++)
      Check[j] = false;

   // independent variable vector
   CPPAD_TESTVECTOR(AD<double>) X(n);
   for(size_t j = 0; j < n; j++)
      X[j] = AD<double>(j);
   Independent(X);

   // Test the case where dependent variable is a non-linear function
   // of the result of a conditional expression.
   CPPAD_TESTVECTOR(AD<double>) Y(m);
   Y[0] = CondExpLt(X[0], X[1], X[2], X[3]);
   Y[0] = cos(Y[0]) + X[0] + X[1];

   // Hessian with respect to x[0] and x[1] is zero.
   // Hessian with respect to x[2] and x[3] is full
   // (although we know that there are no cross terms, this is an
   // inefficiency of the conditional expression operator).
   Check[2 * n + 2] = Check[ 2 * n + 3 ] = true;
   Check[3 * n + 2] = Check[ 3 * n + 3 ] = true;

   // create function object F : X -> Y
   ADFun<double> F(X, Y);

   // -----------------------------------------------------------------
   sparse_hessian_test(F, 0, Check);
   //
   return ok;
}

bool case_three()
{  bool ok = true;
   using CppAD::AD;

   // domain space vector
   size_t n = 1;
   CPPAD_TESTVECTOR(AD<double>) X(n);
   X[0] = 0.;

   // declare independent variables and start recording
   CppAD::Independent(X);

   // range space vector
   size_t m = 1;
   CPPAD_TESTVECTOR(AD<double>) Y(m);

   // make sure reverse jacobian is propagating dependency to
   // intermediate values (not just final ones).
   Y[0] = X[0] * X[0] + 2;

   // create f: X -> Y and stop tape recording
   CppAD::ADFun<double> f(X, Y);

   // ------------------------------------------------------------------
   CPPAD_TESTVECTOR(bool) check(n * n);
   check[0] = true;
   sparse_hessian_test(f, 0, check);
   //
   return ok;
}

bool case_four()
{  bool ok = true;
   using namespace CppAD;

   // dimension of the domain space
   size_t n = 3;

   // dimension of the range space
   size_t m = 1;

   // initialize the vector as zero
   CppAD::VecAD<double> Z(n - 1);
   size_t k;
   for(k = 0; k < n-1; k++)
      Z[k] = 0.;

   // independent variable vector
   CPPAD_TESTVECTOR(AD<double>) X(n);
   X[0] = 0.;
   X[1] = 1.;
   X[2] = 2.;
   Independent(X);

   // VecAD vector z depends on both x[1] and x[2]
   // (component indices do not matter because they can change).
   Z[ X[0] ] = X[1] * X[2];
   Z[ X[1] ] = 0.;

   // dependent variable vector
   CPPAD_TESTVECTOR(AD<double>) Y(m);

   // check results vector
   CPPAD_TESTVECTOR( bool )       Check(n * n);

   // y = z[j] where j might be zero or one.
   Y[0]  =  Z[ X[1] ];

   Check[0 * n + 0] = false; // partial w.r.t x[0], x[0]
   Check[0 * n + 1] = false; // partial w.r.t x[0], x[1]
   Check[0 * n + 2] = false; // partial w.r.t x[0], x[2]

   Check[1 * n + 0] = false; // partial w.r.t x[1], x[0]
   Check[1 * n + 1] = false; // partial w.r.t x[1], x[1]
   Check[1 * n + 2] = true;  // partial w.r.t x[1], x[2]

   Check[2 * n + 0] = false; // partial w.r.t x[2], x[0]
   Check[2 * n + 1] = true;  // partial w.r.t x[2], x[1]
   Check[2 * n + 2] = false; // partial w.r.t x[2], x[2]

   // create function object F : X -> Y
   ADFun<double> F(X, Y);

   // -----------------------------------------------------
   sparse_hessian_test(F, 0, Check);
   //
   return ok;
}

bool case_five(void)
{  bool ok = true;
   using CppAD::AD;

   size_t n = 2;
   CPPAD_TESTVECTOR(AD<double>) X(n);
   X[0] = 1.;
   X[1] = 2.;
   CppAD::Independent(X);

   size_t m = 2;
   CPPAD_TESTVECTOR(AD<double>) Y(m);
   Y[0] = pow(X[0], 2.);
   Y[1] = pow(2., X[1]);

   // create function object F : X -> Y
   CppAD::ADFun<double> F(X, Y);

   // Test F_0 and F_1
   for(size_t index = 0; index < n; index++)
   {  CPPAD_TESTVECTOR(bool) check(n * n);
      for(size_t i = 0; i < n; i++)
         for(size_t j = 0; j < n; j++)
            check[i * n + j] = (i == index) && (j == index);
      sparse_hessian_test(F, index, check);
   }
   //
   return ok;
}

// Note ForSparseHes does not work for this case because R not diagonal.
bool case_six()
{  bool ok = true;
   using namespace CppAD;

   // dimension of the domain space
   size_t n = 3;

   // dimension of the range space
   size_t m = 1;

   // independent variable vector
   CPPAD_TESTVECTOR(AD<double>) X(n);
   X[0] = 0.;
   X[1] = 1.;
   X[2] = 2.;
   Independent(X);
   // y = z[j] where j might be zero or one.
   CPPAD_TESTVECTOR(AD<double>) Y(m);
   Y[0]  =  X[1] * X[2];
   // create function object F : X -> Y
   ADFun<double> F(X, Y);

   // sparsity pattern for hessian of F^2
   CPPAD_TESTVECTOR(bool) F2(n * n);
   F2[0 * n + 0] = false; // partial w.r.t x[0], x[0]
   F2[0 * n + 1] = false; // partial w.r.t x[0], x[1]
   F2[0 * n + 2] = false; // partial w.r.t x[0], x[2]

   F2[1 * n + 0] = false; // partial w.r.t x[1], x[0]
   F2[1 * n + 1] = false; // partial w.r.t x[1], x[1]
   F2[1 * n + 2] = true;  // partial w.r.t x[1], x[2]

   F2[2 * n + 0] = false; // partial w.r.t x[2], x[0]
   F2[2 * n + 1] = true;  // partial w.r.t x[2], x[1]
   F2[2 * n + 2] = false; // partial w.r.t x[2], x[2]

   // choose a non-symmetric sparsity pattern for R
   CPPAD_TESTVECTOR( bool ) r(n * n);
   size_t i, j, k;
   for(i = 0; i < n; i++)
   {  for(j = 0; j < n; j++)
         r[ i * n + j ] = false;
      j = n - i - 1;
      r[ j * n + j ] = true;
   }

   // sparsity pattern for H^T
   CPPAD_TESTVECTOR(bool) Check(n * n);
   for(i = 0; i < n; i++)
   {  for(j = 0; j < n; j++)
      {  Check[ i * n + j] = false;
         for(k = 0; k < n; k++)
         {  // some gcc versions std::vector<bool> do not support |=
            // on elements (because they pack the bits).
            bool tmp         = Check[i * n + j];
            Check[i * n + j] = tmp | (F2[i * n + k] && r[ k * n + j]);
         }
      }
   }

   // compute the reverse Hessian sparsity pattern for F^2 * R
   F.ForSparseJac(n, r);
   CPPAD_TESTVECTOR( bool ) s(m), h(n * n);
   s[0] = 1.;
   bool transpose = true;
   h = F.RevSparseHes(n, s, transpose);

   // check values
   for(i = 0; i < n; i++)
   {  for(j = 0; j < n; j++)
         ok &= (h[i * n + j] == Check[i * n + j]);
   }

   // compute the reverse Hessian sparsity pattern for R^T * F^2
   transpose = false;
   h = F.RevSparseHes(n, s, transpose);

   // check values
   for(i = 0; i < n; i++)
   {  for(j = 0; j < n; j++)
         ok &= (h[j * n + i] == Check[i * n + j]);
   }

   return ok;
}

// bug in cppad/local/sweep/for_hes.hpp fixed on 2022-05-15
bool case_seven()
{  bool ok = true;
   using namespace CppAD;
   typedef CPPAD_TESTVECTOR(size_t) size_vector;

   // dimension of the domain space
   size_t n = 3;

   // dimension of the range space
   size_t m = 2;

   // independent variable vector
   CPPAD_TESTVECTOR(AD<double>) ax(n);
   ax[0] = 0.;
   ax[1] = 1.;
   ax[2] = 2.;
   Independent(ax);

   // dependent variable vector
   CPPAD_TESTVECTOR(AD<double>) ay(m);
   ay[0] = ax[0] * ax[1];
   ay[1] = ax[1] * ax[2];

   // create function object f : x -> y
   ADFun<double> f(ax, ay);

   // sparsity pattern for Hessian of y[1]
   CppAD::sparse_rc<size_vector> pattern_out;
   bool internal_bool = false;
   CPPAD_TESTVECTOR(bool) select_domain(n), select_range(m);
   select_domain[0] = false;
   select_domain[1] = true;
   select_domain[2] = true;
   select_range[0]  = false;
   select_range[1]  = true;
   f.for_hes_sparsity(
      select_domain, select_range, internal_bool, pattern_out
   );
   //
   ok &= pattern_out.nnz() == 2;
   for(size_t k = 0; k < pattern_out.nnz(); ++k)
   {  size_t i = pattern_out.row()[k];
      size_t j = pattern_out.col()[k];
      if( i == 1 )
         ok &= j == 2;
      else if( i == 2 )
         ok &= j == 1;
      else
         ok = false;
   }
   //
   return ok;
}

} // End of empty namespace

bool hes_sparsity(void)
{  bool ok = true;

   ok &= case_one();
   ok &= case_two();
   ok &= case_three();
   ok &= case_four();
   ok &= case_five();
   ok &= case_six();
   ok &= case_seven();

   return ok;
}