Skip to content

Commit e68f1b1

Browse files
Add test
1 parent 2481023 commit e68f1b1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

test/optimizationsystem.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,30 @@ end
312312
@test all(prob.f.cons_expr[i].lhs isa Symbolics.Symbolic
313313
for i in 1:length(prob.f.cons_expr))
314314
end
315+
316+
@testset "Derivatives, iip and oop" begin
317+
@variables x y
318+
@parameters a b
319+
loss = (a - x)^2 + b * (y - x^2)^2
320+
cons2 = [x^2 + y^2 ~ 0, y * sin(x) - x ~ 0]
321+
sys = complete(OptimizationSystem(loss, [x, y], [a, b], name = :sys2, constraints = cons2))
322+
prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0],
323+
grad = true, hess = true, cons_j = true, cons_h = true)
324+
325+
G1 = Array{Float64}(undef, 2)
326+
H1 = Array{Float64}(undef, 2, 2)
327+
J = Array{Float64}(undef, 2, 2)
328+
H3 = [Array{Float64}(undef, 2, 2), Array{Float64}(undef, 2, 2)]
329+
330+
prob.f.grad(G1, [1.0, 1.0], [1.0, 100.0])
331+
@test prob.f.grad([1.0, 1.0], [1.0, 100.0]) == G1
332+
333+
prob.f.hess(H1, [1.0, 1.0], [1.0, 100.0])
334+
@test prob.f.hess([1.0, 1.0], [1.0, 100.0]) == H1
335+
336+
prob.f.cons_j(J, [1.0, 1.0], [1.0, 100.0])
337+
@test prob.f.cons_j([1.0, 1.0], [1.0, 100.0]) == J
338+
339+
prob.f.cons_h(H3, [1.0, 1.0], [1.0, 100.0])
340+
@test prob.f.cons_h([1.0, 1.0], [1.0, 100.0]) == H3
341+
end

0 commit comments

Comments
 (0)