From c737e5050edfc53a461a7b666995f3f0538ad8c2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 26 Jul 2025 23:17:37 +0530 Subject: [PATCH 1/3] fix: fix `OptimizationFunction` generation of exprs --- src/problems/optimizationproblem.jl | 4 ++-- test/optimizationsystem.jl | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/problems/optimizationproblem.jl b/src/problems/optimizationproblem.jl index 978f973d83..97882092e5 100644 --- a/src/problems/optimizationproblem.jl +++ b/src/problems/optimizationproblem.jl @@ -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) diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index e78ebfe9ee..a0a5168ef3 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -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 @@ -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 From 815468671baf8b63fa79aa0893496b8fbc1b0e79 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 4 Aug 2025 13:46:20 -0400 Subject: [PATCH 2/3] test: mark some tests as no longer broken --- test/optimizationsystem.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index a0a5168ef3..26c7bdbbf2 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -100,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 From a4b96d3114b18688520f8119993d8b9fe0c1177b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 4 Aug 2025 13:58:31 -0400 Subject: [PATCH 3/3] test: fix test to work on ForwardDiff@1 --- test/initializationsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 19209b5e46..c98b60bd57 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -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))