Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when acquiring sparsity pattern of a base2ad function #207

Closed
metab0t opened this issue Jun 16, 2024 · 3 comments
Closed

Error when acquiring sparsity pattern of a base2ad function #207

metab0t opened this issue Jun 16, 2024 · 3 comments

Comments

@metab0t
Copy link

metab0t commented Jun 16, 2024

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
Copy link
Author

metab0t commented Jun 16, 2024

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
Copy link
Contributor

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
Copy link
Author

metab0t commented Jun 16, 2024

Yes, I understand the reasons now. Thanks!

@metab0t metab0t closed this as completed Jun 16, 2024
@coin-or coin-or locked and limited conversation to collaborators Jun 16, 2024
@bradbell bradbell converted this issue into discussion #208 Jun 16, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants