From 95698a58bc15fc00b88527fd77ea1f7300fe2e8d Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 27 Jun 2024 14:19:25 +1200 Subject: [PATCH 1/2] [docs] add section of querying the Jacobian --- .../tutorials/nonlinear/querying_hessians.jl | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/docs/src/tutorials/nonlinear/querying_hessians.jl b/docs/src/tutorials/nonlinear/querying_hessians.jl index e740a33e829..37c97da2d01 100644 --- a/docs/src/tutorials/nonlinear/querying_hessians.jl +++ b/docs/src/tutorials/nonlinear/querying_hessians.jl @@ -256,3 +256,42 @@ LinearAlgebra.eigvals(H_star) # we see that they are all positive. Therefore, the Hessian is positive # definite, and so the solution found by Ipopt is a local minimizer. + +# ## Jacobians + +# In addition to the Hessian, it is also possible to query other parts of the +# nonlinear model. For example, the Jacobian of the constraints can be queried +# using [`MOI.jacobian_structure`](@ref) and [`MOI.eval_constraint_jacobian`](@ref). + +function compute_optimal_jacobian(model::Model) + rows = Any[] + nlp = MOI.Nonlinear.Model() + for (F, S) in list_of_constraint_types(model) + for ci in all_constraints(model, F, S) + if !(F <: VariableRef) + push!(rows, ci) + object = constraint_object(ci) + MOI.Nonlinear.add_constraint(nlp, object.func, object.set) + end + end + end + MOI.Nonlinear.set_objective(nlp, objective_function(model)) + x = all_variables(model) + backend = MOI.Nonlinear.SparseReverseMode() + evaluator = MOI.Nonlinear.Evaluator(nlp, backend, index.(x)) + ## Initialize the Jacobian + MOI.initialize(evaluator, [:Jac]) + ## Query the Jacobian structure + sparsity = MOI.jacobian_structure(evaluator) + I, J, V = first.(sparsity), last.(sparsity), zeros(length(sparsity)) + ## Query the Jacobian values + MOI.eval_constraint_jacobian(evaluator, V, value.(x)) + return SparseArrays.sparse(I, J, V, length(rows), length(x)) +end + +compute_optimal_jacobian(model) + +# Compare that to the analytic solution: + +y = value.(x) +[2y[1] 0; 2y[1]+2y[2] 2y[1]+2y[2]] From 07b14c505fe242febd0d3937aa3e337f78fdb63e Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 27 Jun 2024 15:04:59 +1200 Subject: [PATCH 2/2] Update accept.txt --- docs/styles/config/vocabularies/JuMP/accept.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/styles/config/vocabularies/JuMP/accept.txt b/docs/styles/config/vocabularies/JuMP/accept.txt index 900f3eb8deb..3f9ed1c8fa3 100644 --- a/docs/styles/config/vocabularies/JuMP/accept.txt +++ b/docs/styles/config/vocabularies/JuMP/accept.txt @@ -70,6 +70,7 @@ functionize geomean hypergraph infeasibilities +Jacobian(?s) (lin|mixint|quad)prog (log|root)det (L|Q|MI|MIC)P(?s)