source: trunk/test_more/general/checkpoint.cpp @ 3941

Last change on this file since 3941 was 3941, checked in by bradbell, 2 years ago

merge to branch: trunk
from repository: https://github.com/coin-or/CppAD
start hash code: b7056a15a1028d7be587b70a3ecc44b1f42dc05e
end hash code: c8c4cc081accff3628e7e66370ec01e4c99afe8d

commit c8c4cc081accff3628e7e66370ec01e4c99afe8d
Author: Brad Bell <bradbell@…>
Date: Thu Jun 1 23:16:39 2017 -0600

Changes automatically generated by the autotools.

commit f4392bc3eee8f6d0ccd45a0bb3be51181e211680
Author: Brad Bell <bradbell@…>
Date: Thu Jun 1 23:11:56 2017 -0600

  1. Add colpack_jac.cpp example (rename colpack_jac.cpp->colpack_jacobian.cpp).
  2. Add colpack_hescpp example (rename colpack_hes.cpp->colpack_hessian.cpp).


test_one.sh.in: adapt to using test_boolofvoid for testing.
sparse_hes.hpp: fix bug in cppad.symmetric case.

commit 086b8a8709b0c9cb01ce2cf8bc7910e903105ff7
Author: Brad Bell <bradbell@…>
Date: Thu Jun 1 08:54:59 2017 -0600

  1. Fix bug in use of colpack (see kludge in comments).
  2. Fix colpack.symmetric (not general) and add colpack.general.
  3. Deprecate colpack.star.
  4. More autotools from install to deprecated.
  5. Advance to cppad-20170601.

commit 23f26c060648f5c6fc62a1598c659aeccc5ca46f
Author: Brad Bell <bradbell@…>
Date: Tue May 30 08:14:04 2017 -0700

Advance to cppad-20170530.

commit 97f8c08509865d1bfb7ec2e5cd557ddc979f8412
Author: Brad Bell <bradbell@…>
Date: Tue May 30 07:38:47 2017 -0700

debug_rel branch:
There is a problem with speed sparse_hessian debug that goes back to master.
Supresss debug in cppad speed tests until it is fixed.

commit 39ea0d7d9c041784ccd26ce80d19a7ab02752818
Author: Brad Bell <bradbell@…>
Date: Mon May 29 22:34:22 2017 -0700

debug_rel branch:
run_cmake.sh: fix debug_none case.
CMakeLists.txt: use cppad_debug_which to determine debug or release.
CMakeLists.txt: let set_compile_flags determkine build type.

commit 191553e54dca407207789cf0d7c6c27fe6188775
Author: Brad Bell <bradbell@…>
Date: Mon May 29 19:53:08 2017 -0700

debug_rel branch:
Use set_compile_flags in introduction.

commit fba276a84e58d9a0d0944168d5706b7446beb32c
Author: Brad Bell <bradbell@…>
Date: Mon May 29 19:46:30 2017 -0700

debug_rel branch:
Use set_compile_flags in eample/multi_thread subdirectories.

commit 66c8cdb266fa3af29b211b8c870a3aed7a13b021
Author: Brad Bell <bradbell@…>
Date: Mon May 29 18:56:48 2017 -0700

debug_rel branch:
Use set_compile_flags in speed directory.

commit c431b15ee7714d3106234bc527ba2f9a836750e1
Author: Brad Bell <bradbell@…>
Date: Mon May 29 18:36:51 2017 -0700

debug_rel branch:
Convert cppad_ipopt to use set_compile_flags and cppad_debug_which.


CMakeLists.txt: alwasy compile for release to reduce testing time.

commit 2c95b0019f1b665fb14b9f00b049e8b5fb11f89d
Author: Brad Bell <bradbell@…>
Date: Mon May 29 16:55:07 2017 -0700

debug_rel branch:
Add cppad_debug_which to the cmake command line.

commit fd8d1498cf6dc092deca41f764cbb2a001a4cc88
Author: Brad Bell <bradbell@…>
Date: Mon May 29 08:14:23 2017 -0700

debug_rel branch:
Change random_debug_release -> set_compile_flags.

commit 159f5a5aa09012213a52f4ed1c9f0607129a5fe7
Author: Brad Bell <bradbell@…>
Date: Mon May 29 06:50:43 2017 -0700

debug_rel branch:
Update the autotools automatically generated build files.


batch_edit.sh: Start comments about a plan for editing all the source files.
get_sacado.sh: advance to trilions-12.10.11.
makefile.am: advance to trilinos-12.10.1

commit 302153317cd296ec6f927c3202cf96bf38594bbb
Author: Brad Bell <bradbell@…>
Date: Mon May 29 05:20:00 2017 -0700

debug_rel branch:
Add error message if sacado configuration file does not exist.

commit 3f01a631ae808c3a1359e53e1cd55e9a0ea88711
Author: Brad Bell <bradbell@…>
Date: Mon May 29 04:24:00 2017 -0700

debug_rel branch:
CMakeLists.txt: automate naming of libraries Sacado needs.
checkpoint.cpp: fix warnings.

commit dd240928c0c8b6972a8197c985ccc01f08b8886b
Author: Brad Bell <bradbell@…>
Date: Sun May 28 08:25:20 2017 -0700

debug_rel branch:
sparse_sub_hes.cpp: add missing cases found by clang compiler.

commit 30a0c35f1ac50ec425be9a2b7b026284026eccd7
Author: Brad Bell <bradbell@…>
Date: Sun May 28 07:57:36 2017 -0700

debug_rel branch:
eigen_cholesky.hpp: fix compiler warning.
harmonic_time.cpp: remove include that is not used.
forward_active.cpp: fix compiler warning.

commit 4876d14e49dc235865b1574fb38a55cf5ea7a422
Author: Brad Bell <bradbell@…>
Date: Sun May 28 06:19:48 2017 -0700

debug_rel branch:
random_debug_release.cmake: fix comment, remove message replaced by random_choice_0123 in output.
multiple_solution.cpp: fix warnings with clang compiler.
eigen_cholesky.hpp: fix warnings with clang compiler.
compare_change.cpp: fix CPPAD_DEBUG_AND_RELEASE case.

commit 2c51a18f35188d04d2f94069382439580e23f4ac
Author: Brad Bell <bradbell@…>
Date: Sat May 27 21:04:37 2017 -0700

debug_rel branch:
Advance version to cppad-20170527.

commit 4500887b362537774b05e954ad2a95b65a7b8ba0
Author: Brad Bell <bradbell@…>
Date: Sat May 27 09:04:56 2017 -0700

debug_rel branch:
Ramdomly select debug or release flags in example directory.


CMakeLists.txt: always debug for multi_threed examples.

commit 140b5269a0b1a30643894e5a7a8c9a5eb1310301
Author: Brad Bell <bradbell@…>
Date: Sat May 27 08:10:51 2017 -0700

debug_rel branch:
Changing how we set all debug and release flags.

commit e6fb2639db1288fb75de4030b5906df1e41756f9
Author: Brad Bell <bradbell@…>
Date: Sat May 27 07:30:24 2017 -0700

debug_rel branch:
Replace use of cppad_extra_debug by CPPAD_DEBUG_AND_RELEASE.

commit fbbfd0f6e94862174a8a7a17308489ffddb28084
Author: Brad Bell <bradbell@…>
Date: Sat May 27 05:55:58 2017 -0700

debug_rel branch:
Improve random selection of which files are build for release or debug.


forward.cpp: use new -DCPPAD_DEBUG_AND_RELEASE flag.

commit 284be366fb5e2f685a0c71ea6a0e3f74584bf187
Author: Brad Bell <bradbell@…>
Date: Thu May 25 07:39:32 2017 -0700

debug_rel branch:
Add test that failed before change to player.


player.hpp: Fix so it has the same size in debug and release more.
checkpoint.cpp: fix warning when compiling for release.
run_cmake.sh: prepare to use random number to switch debug and release set.
CMakeLists.txt: switch to only test debug (for now).

commit f32375b77e3825628fee6cb160f691a32c48b796
Author: Brad Bell <bradbell@…>
Date: Wed May 24 12:04:27 2017 -0700

debug_rel branch:
forward.cpp: fix a warning during release build.

commit 5fcc7eb78ae8de9f1dbc6c4f0c76fe38e8aeba95
Author: Brad Bell <bradbell@…>
Date: Wed May 24 10:11:12 2017 -0700

debug_rel branch:
CMakeLists.txt: make easy to mix debug and release builds.
eigen_mat_inv.hpp: fix release version warning.

commit 696266f3d62079f5e3bfb1a0f60a7e4f8134e068
Author: Brad Bell <bradbell@…>
Date: Wed May 24 05:43:29 2017 -0700

push_git2svn.py: user ./build in place of ./build/work.
testvector.hpp: improve comments about replacing CPPAD_TESTVECTOR.

  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1/* --------------------------------------------------------------------------
2CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
3
4CppAD is distributed under multiple licenses. This distribution is under
5the terms of the
6                    Eclipse Public License Version 1.0.
7
8A copy of this license is included in the COPYING file of this distribution.
9Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
10-------------------------------------------------------------------------- */
11
12# include <cppad/cppad.hpp>
13
14namespace {
15        using CppAD::AD;
16        typedef CPPAD_TESTVECTOR(AD<double>) ADVector;
17
18        bool f_algo(const ADVector& x, ADVector& y)
19        {       size_t m = y.size();
20                assert( size_t(x.size()) == m + 1);
21                for(size_t i = 0; i < m; i++)
22                        y[i] = x[i] * x[i+1];
23                return true;
24        }
25        bool g_algo(const ADVector& y, ADVector& z)
26        {       size_t m = z.size();
27                assert( size_t(y.size()) + 1 == m );
28                z[0] = 0.0;
29                for(size_t i = 1; i < m; i++)
30                {       z[0] += y[i-1];
31                        z[i]  = y[i-1];
32                }
33                return true;
34        }
35
36        bool test_one(void)
37        {       bool ok = true;
38                using CppAD::checkpoint;
39                using CppAD::ADFun;
40                using CppAD::NearEqual;
41                size_t i, j, k, n = 4, ell = n-1 , m = ell + 1;
42                double eps = 10. * std::numeric_limits<double>::epsilon();
43
44                // checkpoint version of the function F(x)
45                ADVector ax(n), ay(ell), az(m);
46                for(j = 0; j < n; j++)
47                        ax[j] = double(j);
48                checkpoint<double> f_check("f_check", f_algo, ax, ay);
49                checkpoint<double> g_check("g_check", g_algo, ay, az);
50
51                // Record a version of z = g[f(x)] without checkpointing
52                Independent(ax);
53                f_algo(ax, ay);
54                g_algo(ay, az);
55                ADFun<double> check_not(ax, az);
56
57                // Record a version of z = g[f(x)] with checkpointing
58                Independent(ax);
59                f_check(ax, ay);
60                g_check(ay, az);
61                ADFun<double> check_yes(ax, az);
62
63                // compare forward mode results for orders 0, 1, 2
64                size_t p = 2;
65                CPPAD_TESTVECTOR(double) x_p(n*(p+1)), z_not(m*(p+1)), z_yes(m*(p+1));
66                for(j = 0; j < n; j++)
67                {       for(k = 0; k <= p; k++)
68                                x_p[ j * (p+1) + k ] = 1.0 / (p + 1 - k);
69                }
70                z_not = check_not.Forward(p, x_p);
71                z_yes = check_yes.Forward(p, x_p);
72                for(i = 0; i < m; i++)
73                {       for(k = 0; k <= p; k++)
74                        {       double zik_not = z_not[ i * (p+1) + k];
75                                double zik_yes = z_yes[ i * (p+1) + k];
76                                ok &= NearEqual(zik_not, zik_yes, eps, eps);
77                        }
78                }
79
80                // compare reverse mode results
81                CPPAD_TESTVECTOR(double) w(m*(p+1)), dw_not(n*(p+1)), dw_yes(n*(p+1));
82                for(i = 0; i < m; i++)
83                {       for(k = 0; k <= p; k++)
84                                w[ i * (p+1) + k ] = 2.0 / (p + 1 - k );
85                }
86                dw_not = check_not.Reverse(p+1, w);
87                dw_yes = check_yes.Reverse(p+1, w);
88                for(j = 0; j < n; j++)
89                {       for(k = 0; k <= p; k++)
90                        {       double dwjk_not = dw_not[ j * (p+1) + k];
91                                double dwjk_yes = dw_yes[ j * (p+1) + k];
92                                ok &= NearEqual(dwjk_not, dwjk_yes, eps, eps);
93                        }
94                }
95
96                // mix sparsity so test both cases
97                f_check.option( CppAD::atomic_base<double>::bool_sparsity_enum );
98                g_check.option( CppAD::atomic_base<double>::set_sparsity_enum );
99
100                // compare forward mode Jacobian sparsity patterns
101                size_t q = n - 1;
102                CppAD::vector< std::set<size_t> > r(n), s_not(m), s_yes(m);
103                for(j = 0; j < n; j++)
104                {       if( j < q )
105                                r[j].insert(j);
106                        else
107                        {       r[j].insert(0);
108                                r[j].insert(1);
109                        }
110                }
111                s_not = check_not.ForSparseJac(q, r);
112                s_yes = check_yes.ForSparseJac(q, r);
113                for(i = 0; i < m; i++)
114                        ok &= s_not[i] == s_yes[i];
115
116                // compare reverse mode Jacobian sparsity patterns
117                CppAD::vector< std::set<size_t> > s(m), r_not(m), r_yes(m);
118                for(i = 0; i < m; i++)
119                        s[i].insert(i);
120                r_not = check_not.RevSparseJac(m, s);
121                r_yes = check_yes.RevSparseJac(m, s);
122                for(i = 0; i < m; i++)
123                        ok &= s_not[i] == s_yes[i];
124
125
126                // compare reverse mode Hessian sparsity patterns
127                CppAD::vector< std::set<size_t> > s_one(1), h_not(q), h_yes(q);
128                for(i = 0; i < m; i++)
129                        s_one[0].insert(i);
130                h_not = check_not.RevSparseHes(q, s_one);
131                h_yes = check_yes.RevSparseHes(q, s_one);
132                for(i = 0; i < q; i++)
133                        ok &= h_not[i] == h_yes[i];
134
135                checkpoint<double>::clear();
136                return ok;
137        }
138
139        bool h_algo(const ADVector& ax, ADVector& ay)
140        {       ay[0] = ax[0];
141                ay[1] = ax[1] + ax[2];
142                return true;
143        }
144        bool test_two(void)
145        {       bool ok = true;
146                using CppAD::checkpoint;
147                using CppAD::ADFun;
148                using CppAD::NearEqual;
149
150                // checkpoint version of H(x)
151                size_t m = 2;
152                size_t n = 3;
153                ADVector ax(n), ay(m);
154                for(size_t j = 0; j < n; j++)
155                        ax[j] = double(j);
156                checkpoint<double> h_check("h_check", h_algo, ax, ay);
157
158                // record function using h_check
159                Independent(ax);
160                h_check(ax, ay);
161                ADFun<double> h(ax, ay);
162
163                for(size_t k = 0; k < 3; k++)
164                {       if( k == 0 )
165                                h_check.option(CppAD::atomic_base<double>::pack_sparsity_enum);
166                        if( k == 1 )
167                                h_check.option(CppAD::atomic_base<double>::bool_sparsity_enum);
168                        if( k == 2 )
169                                h_check.option(CppAD::atomic_base<double>::set_sparsity_enum);
170
171                        // compute sparsity pattern h_1(x) = x[1] + x[2]
172                        CppAD::vector< std::set<size_t> > r(1), s(1);
173                        r[0].insert(1);
174                        s = h.RevSparseJac(1, r);
175
176                        // check result
177                        std::set<size_t> check;
178                        check.insert(1);
179                        check.insert(2);
180                        ok &= s[0] == check;
181                }
182
183                return ok;
184        }
185}
186
187bool checkpoint(void)
188{       bool ok = true;
189        ok  &= test_one();
190        ok  &= test_two();
191        return ok;
192}
193// END C++
Note: See TracBrowser for help on using the repository browser.