Skip to content

Commit

Permalink
Objective expr and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaibhavdixit02 committed Dec 14, 2023
1 parent 0f3113f commit a0d1511
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/systems/optimization/optimizationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Check warning on line 271 in src/systems/optimization/optimizationsystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/optimization/optimizationsystem.jl#L271

Added line #L271 was not covered by tests

if grad
grad_oop, grad_iip = generate_gradient(sys, checkbounds = checkbounds,
Expand Down
13 changes: 13 additions & 0 deletions test/optimizationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a0d1511

Please sign in to comment.