From a0d151123b9bfb6ac7d796e60f200d2b9141478d Mon Sep 17 00:00:00 2001 From: Vaibhav Dixit Date: Thu, 14 Dec 2023 17:32:38 -0500 Subject: [PATCH] Objective expr and add tests --- src/systems/optimization/optimizationsystem.jl | 2 +- test/optimizationsystem.jl | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 7c85e3697e..bf20cc4ffd 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -268,7 +268,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, f = generate_function(sys, checkbounds = checkbounds, linenumbers = linenumbers, expression = Val{false}) - obj_expr = toexpr(subs_constants(objective(sys))) + obj_expr = subs_constants(objective(sys)) if grad grad_oop, grad_iip = generate_gradient(sys, checkbounds = checkbounds, diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index d190003339..282f7112a2 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -283,3 +283,16 @@ end @test sol1.u ≈ sol2.u end + +@testset "#2323 keep symbolic exressions and xor condition on constraint bounds" begin + @variables x y + @parameters a b + loss = (a - x)^2 + b * (y - x^2)^2 + @named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = [x^2 + y^2 ≲ 0.0]) + @test_throws ArgumentError OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0], lcons = [0.0]) + @test_throws ArgumentError OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0], ucons = [0.0]) + + 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)) +end \ No newline at end of file