Skip to content

Commit 8f158e2

Browse files
authored
Add tests for using ExprGraphs from NLP evaluators (#1043)
1 parent e3bbd5b commit 8f158e2

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

src/Test/nlp.jl

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
const VI = MOI.VariableIndex
22

33
struct HS071 <: MOI.AbstractNLPEvaluator
44
enable_hessian::Bool
@@ -24,9 +24,23 @@ end
2424

2525
function MOI.features_available(d::HS071)
2626
if d.enable_hessian
27-
return [:Grad, :Jac, :Hess]
27+
return [:Grad, :Jac, :Hess, :ExprGraph]
28+
else
29+
return [:Grad, :Jac, :ExprGraph]
30+
end
31+
end
32+
33+
MOI.objective_expr(d::HS071) = :(x[$(VI(1))] * x[$(VI(4))] * (x[$(VI(1))] +
34+
x[$(VI(2))] + x[$(VI(3))]) + x[$(VI(3))])
35+
36+
function MOI.constraint_expr(d::HS071, i::Int)
37+
if i == 1
38+
return :(x[$(VI(1))] * x[$(VI(2))] * x[$(VI(3))] * x[$(VI(4))] >= 25.0)
39+
elseif i == 2
40+
return :(x[$(VI(1))]^2 + x[$(VI(2))]^2 +
41+
x[$(VI(3))]^2 + x[$(VI(4))]^2 == 40.0)
2842
else
29-
return [:Grad, :Jac]
43+
error("Out of bounds constraint.")
3044
end
3145
end
3246

@@ -178,9 +192,19 @@ end
178192

179193
function MOI.features_available(d::FeasibilitySenseEvaluator)
180194
if d.enable_hessian
181-
return [:Grad, :Jac, :Hess]
195+
return [:Grad, :Jac, :Hess, :ExprGraph]
196+
else
197+
return [:Grad, :Jac, :ExprGraph]
198+
end
199+
end
200+
201+
MOI.objective_expr(d::FeasibilitySenseEvaluator) = :()
202+
203+
function MOI.constraint_expr(d::FeasibilitySenseEvaluator, i::Int)
204+
if i == 1
205+
return :(x[$(VI(1))]^2 == 1)
182206
else
183-
return [:Grad, :Jac]
207+
error("Out of bounds constraint.")
184208
end
185209
end
186210

@@ -253,7 +277,7 @@ function feasibility_sense_test_template(model::MOI.ModelLike,
253277

254278
@test MOI.get(model, MOI.ObjectiveValue()) 0.0 atol=atol rtol=rtol
255279

256-
@test MOI.get(model, MOI.VariablePrimal(), x) 1.0 atol=atol rtol=rtol
280+
@test abs(MOI.get(model, MOI.VariablePrimal(), x)) 1.0 atol=atol rtol=rtol
257281
end
258282
end
259283

test/Test/nlp.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,36 @@ using Test
33
import MathOptInterface
44
const MOI = MathOptInterface
55

6+
@testset "hs071" begin
7+
mock = MOI.Utilities.MockOptimizer(
8+
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
9+
eval_objective_value=false
10+
)
11+
config = MOI.Test.TestConfig(optimal_status = MOI.LOCALLY_SOLVED)
12+
MOI.Utilities.set_mock_optimize!(
13+
mock,
14+
(mock) -> begin
15+
MOI.Utilities.mock_optimize!(
16+
mock, config.optimal_status,
17+
[1.0, 4.7429996418092970, 3.8211499817883077, 1.379408289755698]
18+
)
19+
MOI.set(mock, MOI.ObjectiveValue(), 17.014017145179164)
20+
end
21+
)
22+
MOI.Test.hs071_test(mock, config)
23+
MOI.Test.hs071_no_hessian_test(mock, config)
24+
25+
d = MOI.Test.HS071(false)
26+
VI = MOI.VariableIndex
27+
@test MOI.objective_expr(d) == :(x[$(VI(1))] * x[$(VI(4))] * (x[$(VI(1))] +
28+
x[$(VI(2))] + x[$(VI(3))]) + x[$(VI(3))])
29+
@test MOI.constraint_expr(d, 1) ==
30+
:(x[$(VI(1))] * x[$(VI(2))] * x[$(VI(3))] * x[$(VI(4))] >= 25.0)
31+
@test MOI.constraint_expr(d, 2) ==
32+
:(x[$(VI(1))]^2 + x[$(VI(2))]^2 + x[$(VI(3))]^2 + x[$(VI(4))]^2 == 40.0)
33+
@test_throws ErrorException MOI.constraint_expr(d, 3)
34+
end
35+
636
@testset "mixed_complementarity" begin
737
mock = MOI.Utilities.MockOptimizer(
838
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())

0 commit comments

Comments
 (0)