Skip to content

Commit 5cb17b6

Browse files
Merge pull request #845 from aml5600/fix/vjp-return
MOI Constraint jacobian-vector fixes
2 parents d483b16 + ab79a1b commit 5cb17b6

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

lib/OptimizationMOI/src/nlp.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,14 @@ function MOI.eval_constraint_jacobian_product(
302302
evaluator::MOIOptimizationNLPEvaluator, y, x, w)
303303
if evaluator.f.cons_jvp !== nothing
304304
evaluator.f.cons_jvp(y, x, w)
305-
306305
elseif evaluator.f.cons_j !== nothing
307306
J = evaluator.J
308307
evaluator.f.cons_j(J, x)
309308
mul!(y, J, w)
310-
return
309+
else
310+
error("Thou shalt provide the v'J of the constraint jacobian, not doing so is associated with great misfortune and also no ice cream for you.")
311311
end
312-
error("Thou shalt provide the v'J of the constraint jacobian, not doing so is associated with great misfortune and also no ice cream for you.")
312+
return nothing
313313
end
314314

315315
function MOI.eval_constraint_jacobian_transpose_product(
@@ -320,14 +320,14 @@ function MOI.eval_constraint_jacobian_transpose_product(
320320
)
321321
if evaluator.f.cons_vjp !== nothing
322322
evaluator.f.cons_vjp(y, x, w)
323-
324323
elseif evaluator.f.cons_j !== nothing
325324
J = evaluator.J
326325
evaluator.f.cons_j(J, x)
327326
mul!(y, J', w)
328-
return
327+
else
328+
error("Thou shalt provide the v'J of the constraint jacobian, not doing so is associated with great misfortune and also no ice cream for you.")
329329
end
330-
error("Thou shalt provide the v'J of the constraint jacobian, not doing so is associated with great misfortune and also no ice cream for you.")
330+
return nothing
331331
end
332332

333333
function MOI.hessian_lagrangian_structure(evaluator::MOIOptimizationNLPEvaluator)

lib/OptimizationMOI/test/runtests.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ using OptimizationMOI, Optimization, Ipopt, NLopt, Zygote, ModelingToolkit, Reve
22
using AmplNLWriter, Ipopt_jll, Juniper, HiGHS
33
using Test, SparseArrays
44

5+
import MathOptInterface
6+
57
function _test_sparse_derivatives_hs071(backend, optimizer)
68
function objective(x, ::Any)
79
return x[1] * x[4] * (x[1] + x[2] + x[3]) + x[3]
@@ -29,6 +31,32 @@ function _test_sparse_derivatives_hs071(backend, optimizer)
2931
return
3032
end
3133

34+
@testset "Evaluator" begin
35+
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
36+
x0 = zeros(2)
37+
_p = [1.0, 100.0]
38+
cons_circ = (res, x, p) -> res .= [x[1]^2 + x[2]^2]
39+
optprob = OptimizationFunction(
40+
rosenbrock, Optimization.AutoZygote();
41+
cons = cons_circ)
42+
prob = OptimizationProblem(optprob, x0, _p, ucons = [Inf], lcons = [0.0])
43+
evaluator = init(prob, Ipopt.Optimizer()).evaluator
44+
45+
x = prob.u0
46+
# vector-constraint jacobian product
47+
@test (evaluator.f.cons_j !== nothing) || (evaluator.f.cons_jvp !== nothing)
48+
y = zeros(1)
49+
w = ones(2)
50+
@test MathOptInterface.eval_constraint_jacobian_product(evaluator, y, x, w) === nothing
51+
52+
# constraint jacobian-vector product
53+
@test (evaluator.f.cons_j !== nothing) || (evaluator.f.cons_vjp !== nothing)
54+
y = zeros(2)
55+
w = ones(1)
56+
@test MathOptInterface.eval_constraint_jacobian_transpose_product(
57+
evaluator, y, x, w) === nothing
58+
end
59+
3260
@testset "NLP" begin
3361
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
3462
x0 = zeros(2)

0 commit comments

Comments
 (0)