Skip to content
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

Document use of conditionals for sparsity detection #283

Open
gdalle opened this issue Jul 31, 2024 · 2 comments
Open

Document use of conditionals for sparsity detection #283

gdalle opened this issue Jul 31, 2024 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@gdalle
Copy link
Collaborator

gdalle commented Jul 31, 2024

With the new TracerSparsityDetector from SparseConnectivityTracer, if blocks are not supported if they involve an input variable. They must be replaced with ifelse statements, which are compatible with operator overloading.

In your objective functions or constraints, do not write

y = if x[1] > 0
    x[2]
else
    x[3]
end

but instead write

y = ifelse(x[1] > 0, x[2], x[3])

Related:

@timholy
Copy link

timholy commented Sep 18, 2024

Can one turn off sparsity detection? (Or better, turn off sparsity altogether.) I have a problem where the Hessian is not very sparse and the objective function uses PDMats.jl, which involves representing matrices via their Cholesky factorization. I get errors like

julia> solveconstr(<my args>)
ERROR: TypeError: non-boolean (SparseConnectivityTracer.GradientTracer{SparseConnectivityTracer.IndexSetGradientPattern{Int64, BitSet}}) used in boolean context
Stacktrace:
  [1] ishermitian(A::SMatrix{2, 2, SparseConnectivityTracer.GradientTracer{…}, 4})
    @ LinearAlgebra ~/.julia/juliaup/julia-1.10.5+0.x64.linux.gnu/share/julia/stdlib/v1.10/LinearAlgebra/src/generic.jl:1257
  [2] #cholesky#549
    @ ~/.julia/packages/StaticArrays/MSJcA/src/cholesky.jl:4 [inlined]
  [3] cholesky
    @ ~/.julia/packages/StaticArrays/MSJcA/src/cholesky.jl:3 [inlined]
  [4] PDMat(mat::SMatrix{2, 2, SparseConnectivityTracer.GradientTracer{…}, 4})
    @ PDMats ~/.julia/dev/PDMats/src/pdmat.jl:19
 ...

@tmigot
Copy link
Member

tmigot commented Sep 18, 2024

Hi @timholy !
Yes, you can pass another backend for the Hessian like this:

f(x) = 100 * (x[2] - x[1]^2)^2 + (x[1] - 1)^2
x0 = [-1.2; 1.0]
nlp = ADNLPModel(f, x0, hessian_backend = ADNLPModels.ForwardDiffADHessian)

I think the documentation for this is here https://jso.dev/ADNLPModels.jl/dev/backend/ but can be improved.

If you don't use the Hessian, then this works as well:

nlp = ADNLPModel(f, x0, hessian_backend = ADNLPModels.EmptyADbackend)
nlp = ADNLPModel(f, x0, matrix_free = true) # is an alternative that skip Hessian and Jacobian computation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants