coin-or / CppAD

A C++ Algorithmic Differentiation Package: Home Page
https://cppad.readthedocs.io
Other
446 stars 94 forks source link

Error when acquiring sparsity pattern of a base2ad function #207

Closed metab0t closed 3 weeks ago

metab0t commented 3 weeks ago

The following code errors:

#include <iostream>
#include <unordered_map>
#include <string>

#include "cppad/cppad.hpp"

using CppAD::AD;
using CppAD::ADFun;
using CppAD::Independent;
using CppAD::sparse_rc;
using CppAD::sparse_rcv;

typedef CPPAD_TESTVECTOR(double) d_vector;
typedef CPPAD_TESTVECTOR(AD<double>) ad_vector;
typedef CPPAD_TESTVECTOR(size_t) s_vector;

void debug_mvp()
{
    size_t n = 3;
    size_t m = 1;
    CPPAD_TESTVECTOR(AD<double>) ax(n), ay(m);
    Independent(ax);
    ay[0] = ax[0] / ax[1] - 1.0 / (ax[2] * ax[2]);
    ADFun<double> f;
    f.Dependent(ax, ay);

    // n by n identity matrix sparsity
    sparse_rc<s_vector> pattern_in;
    pattern_in.resize(n, n, n);
    for (size_t k = 0; k < n; k++)
        pattern_in.set(k, k, k);

    // sparsity for J(x)
    bool transpose = false;
    bool dependency = false;
    bool internal_bool = true;
    sparse_rc<s_vector> pattern_jac;
    f.for_jac_sparsity(pattern_in, transpose, dependency, internal_bool, pattern_jac);

    ADFun<AD<double>, double> af = f.base2ad();

    CPPAD_TESTVECTOR(AD<double>) axp(n);
    {
        Independent(axp);
        for (size_t j = 0; j < n; ++j)
            ax[j] = axp[j];
        // compute entire forward mode Jacobian
        sparse_rcv<s_vector, ad_vector> subset(pattern_jac);
        CppAD::sparse_jac_work work;
        std::string coloring = "cppad";
        size_t n_color = af.sparse_jac_rev(ax, subset, pattern_jac, coloring, work);
        ADFun<double> g;
        g.Dependent(axp, subset.val());
    }
}

int main(void)
{
    debug_mvp();
}

The error happens at the size_t n_color = af.sparse_jac_rev(ax, subset, pattern_jac, coloring, work); line as follows:

cppad-20240602 error from a known source:
yq = f.Forward(q, xq): a zero order Taylor coefficient is nan.
Corresponding independent variables vector was written to binary a file.
vector_size = 3
file_name = C:\Users\yangyue\AppData\Local\Temp\uv5o.0
index = 0

Error detected by false result for
    if( CppAD::isnan( yq[ (q+1) * index + 0 ] )
at line 265 in the file
    E:\codeworkspace\cpp_try\cppad_try\thirdparty\include\cppad/core/forward/forward.hpp
Assertion failed: false, file E:\codeworkspace\cpp_try\cppad_try\thirdparty\include\cppad/utility/error_handler.hpp, line 205
metab0t commented 3 weeks ago

The problem seems that AD<double> defaults to 0.0, and causes the evaluation function problematic. Just assign correct values to axp like

CPPAD_TESTVECTOR(AD<double>) axp(n);
axp[0] = 1.0;
axp[1] = 2.0;
axp[2] = 3.0;
bradbell commented 3 weeks ago

During the recording of a function, the function is evaluated at the independent variable values. These values are unspecified if you do not set them before the call to Independent.

If you are satisfied with this explanation, please close this issue.

metab0t commented 3 weeks ago

Yes, I understand the reasons now. Thanks!