Skip to content

Commit c737e50

Browse files
fix: fix OptimizationFunction generation of exprs
1 parent 647c9f9 commit c737e50

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/problems/optimizationproblem.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ function SciMLBase.OptimizationFunction{iip}(sys::System;
5858
else
5959
_cons_h = cons_hess_prototype = nothing
6060
end
61-
cons_expr = cstr
61+
cons_expr = Code.toexpr.(expand.([eq.lhs for eq in Symbolics.canonical_form.(cstr)]))
6262
end
6363

64-
obj_expr = cost(sys)
64+
obj_expr = Code.toexpr(expand(cost(sys)))
6565

6666
observedfun = ObservedFunctionCache(
6767
sys; expression, eval_expression, eval_module, checkbounds, cse)

test/optimizationsystem.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ end
6464
sys = complete(sys)
6565
prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, a => 1.0, b => 1.0],
6666
grad = true, hess = true, cons_j = true, cons_h = true)
67+
@test prob.f.cons_expr isa Vector{Expr}
68+
@test prob.f.expr isa Expr
6769
@test prob.f.sys === sys
6870
sol = solve(prob, IPNewton())
6971
@test sol.objective < 1.0
@@ -289,9 +291,8 @@ end
289291
sys = complete(sys)
290292

291293
prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, a => 1.0, b => 100.0])
292-
@test prob.f.expr isa Symbolics.Symbolic
293-
@test all(prob.f.cons_expr[i].lhs isa Symbolics.Symbolic
294-
for i in 1:length(prob.f.cons_expr))
294+
@test prob.f.expr isa Expr
295+
@test all(x -> x isa Expr, prob.f.cons_expr)
295296
end
296297

297298
@testset "Derivatives, iip and oop" begin

0 commit comments

Comments
 (0)