-
-
Notifications
You must be signed in to change notification settings - Fork 232
Handle constraints inside modelingtoolkitize
of OptimizationSystem
#1983
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1342a50
Handle constraints inside `modelingtoolkitize` of `OptimizationSystem`
Vaibhavdixit02 4b10b72
Handle constraint bounds and add test
Vaibhavdixit02 4f5349d
Address review comments and substitute in inequalities while simplifi…
Vaibhavdixit02 2f2e9c4
Use obj_sparse kwarg and remove num_cons in tests
Vaibhavdixit02 0d1ce4f
Update src/systems/optimization/modelingtoolkitize.jl
Vaibhavdixit02 3c4d820
Use restructure instead of reshape
Vaibhavdixit02 a501bcf
Keep sparse
Vaibhavdixit02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
$(TYPEDSIGNATURES) | ||
|
||
Generate `OptimizationSystem`, dependent variables, and parameters from an `OptimizationProblem`. | ||
""" | ||
function modelingtoolkitize(prob::DiffEqBase.OptimizationProblem; kwargs...) | ||
num_cons = isnothing(prob.lcons) ? 0 : length(prob.lcons) | ||
if prob.p isa Tuple || prob.p isa NamedTuple | ||
p = [x for x in prob.p] | ||
else | ||
p = prob.p | ||
end | ||
|
||
vars = ArrayInterfaceCore.restructure(prob.u0, | ||
[variable(:x, i) for i in eachindex(prob.u0)]) | ||
params = p isa DiffEqBase.NullParameters ? [] : | ||
ArrayInterfaceCore.restructure(p, [variable(:α, i) for i in eachindex(p)]) | ||
|
||
eqs = prob.f(vars, params) | ||
|
||
if DiffEqBase.isinplace(prob) && !isnothing(prob.f.cons) | ||
lhs = Array{Num}(undef, num_cons) | ||
prob.f.cons(lhs, vars, params) | ||
cons = Union{Equation, Inequality}[] | ||
|
||
if !isnothing(prob.lcons) | ||
for i in 1:num_cons | ||
!isinf(prob.lcons[i]) && prob.lcons[i] !== prob.ucons[i] && | ||
push!(cons, prob.lcons[i] ≲ lhs[i]) | ||
if !isinf(prob.ucons[i]) | ||
prob.lcons[i] == prob.ucons[i] ? push!(cons, lhs[i] ~ prob.ucons[i]) : | ||
push!(cons, lhs[i] ≲ prob.ucons[i]) | ||
end | ||
end | ||
end | ||
|
||
if !isnothing(prob.ucons) | ||
for i in 1:num_cons | ||
if !isinf(prob.ucons[i]) | ||
prob.lcons[i] == prob.ucons[i] ? push!(cons, lhs[i] ~ prob.ucons[i]) : | ||
push!(cons, lhs[i] ≲ prob.ucons[i]) | ||
end | ||
end | ||
end | ||
|
||
if (isnothing(prob.lcons) || all(isinf.(prob.lcons))) && | ||
(isnothing(prob.ucons) || all(isinf.(prob.ucons))) | ||
throw(ArgumentError("Constraints passed have no proper bounds defined. | ||
Ensure you pass equal bounds (the scalar that the constraint should evaluate to) for equality constraints | ||
or pass the lower and upper bounds for inequality constraints.")) | ||
end | ||
elseif !isnothing(prob.f.cons) | ||
cons = prob.f.cons(vars, params) | ||
else | ||
cons = [] | ||
end | ||
|
||
de = OptimizationSystem(eqs, vec(vars), vec(toparam.(params)); | ||
name = gensym(:MTKizedOpt), | ||
constraints = cons, | ||
kwargs...) | ||
de | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be refactored, since ucons and lcons are either both nothing or both not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That holds true for
ub
andlb
but not for these. We could change it in SciMLBase, but I have left it as it is for correct behavior now and will make the change there subsequently.