Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep symbolic expressions as is and minor bugfixes optimization system #2323

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/systems/optimization/modelingtoolkitize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
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]

Check warning on line 29 in src/systems/optimization/modelingtoolkitize.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/optimization/modelingtoolkitize.jl#L29

Added line #L29 was not covered by tests
push!(cons, prob.lcons[i] ≲ lhs[i])
else
push!(cons, lhs[i] ~ prob.ucons[i])
Expand Down
24 changes: 5 additions & 19 deletions src/systems/optimization/optimizationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,6 @@

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,
Expand Down Expand Up @@ -275,14 +268,8 @@
f = generate_function(sys, checkbounds = checkbounds, linenumbers = linenumbers,
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)
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,
linenumbers = linenumbers,
Expand Down Expand Up @@ -342,14 +329,13 @@
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))

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

View check run for this annotation

Codecov / codecov/patch

src/systems/optimization/optimizationsystem.jl#L332

Added line #L332 was not covered by tests

if !haskey(kwargs, :lcons) && !haskey(kwargs, :ucons) # use the symbolically specified bounds
lcons = lcons_
ucons = ucons_
else # use the user supplied constraints bounds
haskey(kwargs, :lcons) && haskey(kwargs, :ucons) &&
(haskey(kwargs, :lcons) haskey(kwargs, :ucons)) &&

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

View check run for this annotation

Codecov / codecov/patch

src/systems/optimization/optimizationsystem.jl#L338

Added line #L338 was not covered by tests
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"))
Expand Down Expand Up @@ -527,7 +513,7 @@
lcons = lcons_
ucons = ucons_
else # use the user supplied constraints bounds
!haskey(kwargs, :lcons) && !haskey(kwargs, :ucons) &&
(haskey(kwargs, :lcons) haskey(kwargs, :ucons)) &&

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

View check run for this annotation

Codecov / codecov/patch

src/systems/optimization/optimizationsystem.jl#L516

Added line #L516 was not covered by tests
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"))
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
Vaibhavdixit02 marked this conversation as resolved.
Show resolved Hide resolved
@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
Loading