Skip to content

Commit 1ad84a7

Browse files
Objective expr and add tests
1 parent c4fc597 commit 1ad84a7

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/systems/optimization/optimizationsystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map,
268268
f = generate_function(sys, checkbounds = checkbounds, linenumbers = linenumbers,
269269
expression = Val{false})
270270

271-
obj_expr = toexpr(subs_constants(objective(sys)))
271+
obj_expr = subs_constants(objective(sys))
272272

273273
if grad
274274
grad_oop, grad_iip = generate_gradient(sys, checkbounds = checkbounds,

test/optimizationsystem.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,16 @@ end
283283

284284
@test sol1.u sol2.u
285285
end
286+
287+
@testset "#2323 keep symbolic exressions and xor condition on constraint bounds" begin
288+
@variables x y
289+
@parameters a b
290+
loss = (a - x)^2 + b * (y - x^2)^2
291+
@named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = [x^2 + y^2 0.0])
292+
@test_throws ArgumentError OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0], lcons = [0.0])
293+
@test_throws ArgumentError OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0], ucons = [0.0])
294+
295+
prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0])
296+
@test prob.f.expr isa Symbolics.Symbolic
297+
@test all(prob.f.cons_expr[i].lhs isa Symbolics.Symbolic for i in 1:length(prob.f.cons_expr))
298+
end

0 commit comments

Comments
 (0)