Skip to content

Commit 6e3cece

Browse files
committed
Cover the quadratic parameter substitution
Add nonlinear constraints with quadratic subfunctions, with and without a parameter, and query ListOfSupportedNonlinearOperators through the layer. These methods were never compiled by the tests, which local coverage reports as non-executable lines but codecov reports as uncovered.
1 parent 08b2e60 commit 6e3cece

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

test/Nonlinear/test_model_with_quad.jl

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,28 @@ function test_quad_parameters()
304304
),
305305
MOI.LessThan(10.0),
306306
)
307+
# Nonlinear constraints with quadratic subfunctions: with a parameter
308+
# (converted to `ScalarNonlinearFunction`) and without (left as is).
309+
q_p = MOI.ScalarQuadraticFunction(
310+
[MOI.ScalarQuadraticTerm(2.0, p, x)],
311+
MOI.ScalarAffineTerm{Float64}[],
312+
0.0,
313+
)
314+
Nonlinear.add_constraint(
315+
model,
316+
MOI.ScalarNonlinearFunction(:sqrt, Any[q_p]),
317+
MOI.LessThan(30.0),
318+
)
319+
q_x = MOI.ScalarQuadraticFunction(
320+
[MOI.ScalarQuadraticTerm(2.0, x, x)],
321+
MOI.ScalarAffineTerm{Float64}[],
322+
0.0,
323+
)
324+
Nonlinear.add_constraint(
325+
model,
326+
MOI.ScalarNonlinearFunction(:sqrt, Any[q_x]),
327+
MOI.LessThan(30.0),
328+
)
307329
# A nonlinear constraint and objective mentioning the parameter and the
308330
# variable directly.
309331
Nonlinear.add_constraint(
@@ -314,9 +336,17 @@ function test_quad_parameters()
314336
Nonlinear.set_objective(model, MOI.ScalarNonlinearFunction(:*, Any[p, x]))
315337
d = Nonlinear.Evaluator(model, Nonlinear.SparseReverseMode())
316338
MOI.initialize(d, [:Grad, :Jac])
317-
g = fill(NaN, 5)
339+
g = fill(NaN, 7)
318340
MOI.eval_constraint(d, g, [1.0])
319-
@test g [2.0 + 3.0 * 7.0, 7.0, sqrt(3.0 * 7.0 + 1.0), 1.0, 1.0 + 7.0]
341+
@test g [
342+
2.0 + 3.0 * 7.0,
343+
7.0,
344+
sqrt(3.0 * 7.0 + 1.0),
345+
1.0,
346+
sqrt(2.0 * 7.0),
347+
1.0,
348+
1.0 + 7.0,
349+
]
320350
@test MOI.eval_objective(d, [1.5]) == 7.0 * 1.5
321351
return
322352
end
@@ -345,6 +375,8 @@ function test_attribute_forwarding()
345375
ex = Nonlinear.add_expression(model, :($p * $x))
346376
@test model[ex] isa Nonlinear.Expression
347377
Nonlinear.register_operator(model, :my_square, 1, z -> z^2)
378+
ops = MOI.get(model, MOI.ListOfSupportedNonlinearOperators())
379+
@test :my_square in ops
348380
c = Nonlinear.add_constraint(model, :(my_square($ex)), MOI.LessThan(1.0))
349381
@test MOI.is_valid(model, c)
350382
d = Nonlinear.Evaluator(model, Nonlinear.SparseReverseMode())

0 commit comments

Comments
 (0)