From 60f3acd25c36f4957fc766630744bce8ffa6cc70 Mon Sep 17 00:00:00 2001 From: Vaibhav Dixit Date: Thu, 19 Oct 2023 23:38:40 -0400 Subject: [PATCH 1/4] minor fixes --- src/systems/optimization/modelingtoolkitize.jl | 2 +- src/systems/optimization/optimizationsystem.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/optimization/modelingtoolkitize.jl b/src/systems/optimization/modelingtoolkitize.jl index 710f3f392b..254f2d1a3a 100644 --- a/src/systems/optimization/modelingtoolkitize.jl +++ b/src/systems/optimization/modelingtoolkitize.jl @@ -26,7 +26,7 @@ function modelingtoolkitize(prob::DiffEqBase.OptimizationProblem; kwargs...) if !isnothing(prob.lcons) for i in 1:num_cons if !isinf(prob.lcons[i]) - if prob.lcons[i] != prob.ucons[i] && + if prob.lcons[i] != prob.ucons[i] push!(cons, prob.lcons[i] ≲ lhs[i]) else push!(cons, lhs[i] ~ prob.ucons[i]) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index e1ab36c9ae..2bd0cefe9a 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -349,7 +349,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, lcons = lcons_ ucons = ucons_ else # use the user supplied constraints bounds - haskey(kwargs, :lcons) && haskey(kwargs, :ucons) && + (haskey(kwargs, :lcons) ⊻ haskey(kwargs, :ucons)) && throw(ArgumentError("Expected both `ucons` and `lcons` to be supplied")) haskey(kwargs, :lcons) && length(kwargs[:lcons]) != length(cstr) && throw(ArgumentError("Expected `lcons` to be of the same length as the vector of constraints")) @@ -527,7 +527,7 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0map, lcons = lcons_ ucons = ucons_ else # use the user supplied constraints bounds - !haskey(kwargs, :lcons) && !haskey(kwargs, :ucons) && + (haskey(kwargs, :lcons) ⊻ haskey(kwargs, :ucons)) && throw(ArgumentError("Expected both `ucons` and `lcons` to be supplied")) haskey(kwargs, :lcons) && length(kwargs[:lcons]) != length(cstr) && throw(ArgumentError("Expected `lcons` to be of the same length as the vector of constraints")) From c4fc59747c8319b267497a3539a8bbfe61129f3e Mon Sep 17 00:00:00 2001 From: Vaibhav Dixit Date: Thu, 23 Nov 2023 00:34:36 -0500 Subject: [PATCH 2/4] Don't convert to julia expression here --- src/systems/optimization/optimizationsystem.jl | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 2bd0cefe9a..af608128f8 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -196,13 +196,6 @@ end hessian_sparsity(sys::OptimizationSystem) = hessian_sparsity(get_op(sys), states(sys)) -function rep_pars_vals!(e::Expr, p) - rep_pars_vals!.(e.args, Ref(p)) - replace!(e.args, p...) -end - -function rep_pars_vals!(e, p) end - """ ```julia DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, @@ -276,13 +269,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, expression = Val{false}) obj_expr = toexpr(subs_constants(objective(sys))) - pairs_arr = if p isa SciMLBase.NullParameters - [Symbol(_s) => Expr(:ref, :x, i) for (i, _s) in enumerate(dvs)] - else - vcat([Symbol(_s) => Expr(:ref, :x, i) for (i, _s) in enumerate(dvs)], - [Symbol(_p) => p[i] for (i, _p) in enumerate(ps)]) - end - rep_pars_vals!(obj_expr, pairs_arr) + if grad grad_oop, grad_iip = generate_gradient(sys, checkbounds = checkbounds, linenumbers = linenumbers, @@ -342,8 +329,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, else _cons_h = nothing end - cons_expr = toexpr.(subs_constants(constraints(cons_sys))) - rep_pars_vals!.(cons_expr, Ref(pairs_arr)) + cons_expr = subs_constants(constraints(cons_sys)) if !haskey(kwargs, :lcons) && !haskey(kwargs, :ucons) # use the symbolically specified bounds lcons = lcons_ From 1ad84a71cc6cb0f6b7d8e8f4e8439122440da248 Mon Sep 17 00:00:00 2001 From: Vaibhav Dixit Date: Thu, 14 Dec 2023 17:32:38 -0500 Subject: [PATCH 3/4] 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 af608128f8..a21db0d6bc 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 From 96990289ba5882fb3ca67ee04eeedbce32b1c88a Mon Sep 17 00:00:00 2001 From: Vaibhav Kumar Dixit Date: Thu, 14 Dec 2023 17:51:25 -0500 Subject: [PATCH 4/4] Update test/optimizationsystem.jl Co-authored-by: Christopher Rackauckas --- test/optimizationsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index 282f7112a2..09b780ef74 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -284,7 +284,7 @@ end @test sol1.u ≈ sol2.u end -@testset "#2323 keep symbolic exressions and xor condition on constraint bounds" begin +@testset "#2323 keep symbolic expressions and xor condition on constraint bounds" begin @variables x y @parameters a b loss = (a - x)^2 + b * (y - x^2)^2