Skip to content

fix: fix OptimizationFunction generation of exprs #3844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/problems/optimizationproblem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ function SciMLBase.OptimizationFunction{iip}(sys::System;
else
_cons_h = cons_hess_prototype = nothing
end
cons_expr = cstr
cons_expr = Code.toexpr.(expand.([eq.lhs for eq in Symbolics.canonical_form.(cstr)]))
end

obj_expr = cost(sys)
obj_expr = Code.toexpr(expand(cost(sys)))

observedfun = ObservedFunctionCache(
sys; expression, eval_expression, eval_module, checkbounds, cse)
Expand Down
2 changes: 1 addition & 1 deletion test/initializationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ end
@test SciMLBase.successful_retcode(solve(prob))

seta = setsym_oop(prob, [a])
(newu0, newp) = seta(prob, ForwardDiff.Dual{ForwardDiff.Tag{:tag, Float64}}.([1.0], 1))
(newu0, newp) = seta(prob, ForwardDiff.Dual{ForwardDiff.Tag{:tag, Float64}}.([1.0], 0))
newprob = remake(prob, u0 = newu0, p = newp)

@test SciMLBase.successful_retcode(solve(newprob))
Expand Down
15 changes: 8 additions & 7 deletions test/optimizationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ end
sys = complete(sys)
prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, a => 1.0, b => 1.0],
grad = true, hess = true, cons_j = true, cons_h = true)
@test prob.f.cons_expr isa Vector{Expr}
@test prob.f.expr isa Expr
@test prob.f.sys === sys
sol = solve(prob, IPNewton())
@test sol.objective < 1.0
Expand Down Expand Up @@ -98,10 +100,10 @@ end

prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, z => 0.0, a => 1.0, b => 1.0],
grad = false, hess = false, cons_j = false, cons_h = false)
@test_broken sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe))
@test_skip sol.objective < 1.0
@test_skip sol.u≈[0.808, -0.064] atol=1e-3
@test_skip sol[x]^2 + sol[y]^2 ≈ 1.0
sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe))
@test sol.objective < 1.0
@test_broken sol.u≈[0.808, -0.064] atol=1e-3
@test_broken sol[x]^2 + sol[y]^2 ≈ 1.0
end

@testset "rosenbrock" begin
Expand Down Expand Up @@ -289,9 +291,8 @@ end
sys = complete(sys)

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

@testset "Derivatives, iip and oop" begin
Expand Down
Loading