Skip to content

Conversation

Vaibhavdixit02
Copy link
Member

This will enable the AutoModelingToolkit in Optimization.jl to switch over to using modelingtoolkitize. SciML/Optimization.jl#432

@ChrisRackauckas
Copy link
Member

Needs tests

@ValentinKaisermayer
Copy link
Contributor

closes #1902

end
end

if (isnothing(prob.lcons) || all(isinf.(prob.lcons))) && (isnothing(prob.ucons) || all(isinf.(prob.ucons)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this the behaviour for equality contraints? It seems wrong.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Btw did you see line 33 as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant that -inf <= cons <= inf yields cons ~ 0

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you are saying, it might be better to no assume that if user is not passing equal constraints they don't intend it to be an equality constraint. I was assuming that case to be equivalent to having a equality to 0, it might not always be true

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if a user supplies such a pair of (-inf,inf) it is either a modelling error or the intension is to deactivate the constraint. So in any case no constraint should be added. We might even cry. I wonder how JuMP handles such a case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an error in this case

if !isnothing(prob.lcons) && !isnothing(prob.ucons)
cons = prob.lcons .≲ lhs .≲ prob.ucons
else
if !isnothing(prob.lcons)
Copy link
Contributor

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.

Copy link
Member Author

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 and lb 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.

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])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it the other way around? If ucons and lcons are equal, an equality has to be added.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh shit. Yes it's flipped, thanks for catching that, how did this not get caught by the tests though I'll check that

cons = []
end

de = OptimizationSystem(eqs, vec(vars), vec(params);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toparam?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understood what you meant here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are generating the parameters via variable(:α, i)

Generate `OptimizationSystem`, dependent variables, and parameters from an `OptimizationProblem`.
"""
function modelingtoolkitize(prob::DiffEqBase.OptimizationProblem; num_cons = 0, kwargs...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't num_cons simply isnothing(prob.lcons) ? 0 : length(prob.lcons) ?

@Vaibhavdixit02
Copy link
Member Author

Should be ready now

p = prob.p
end

vars = reshape([variable(:x, i) for i in eachindex(prob.u0)], size(prob.u0))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generate_hessian(combinedsys)
hess_sparsity = ModelingToolkit.hessian_sparsity(sys1)
sparse_prob = OptimizationProblem(sys1, [x, y], [a, b], grad = true, sparse = true)
sparse_prob = OptimizationProblem(sys1, [x, y], [a, b], grad = true, obj_sparse = true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's obj_sparse and how is it different from sparse?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this PR I have removed sparse in favor of obj_sparse (objective's sparse) and cons_sparse (constraints' sparse). This is similar to the AutoModelingToolkit struct. A single kwarg is not sufficient since either could be sparse

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep obj_sparse as sparse. Consistency matters.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can keep it and deprecate and interpret it as obj_sparse, but also keep these two for consistency with Optimization.jl?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not deprecate it. Why would an ODE use obj_sparse instead of sparse?

@ChrisRackauckas ChrisRackauckas merged commit 5659fae into master Jan 13, 2023
@ChrisRackauckas ChrisRackauckas deleted the osmtkizecons branch January 13, 2023 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants