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

Adds support for MOI.AbstractOptimizer that do not support MOI.NLPBlock #381

Merged
merged 4 commits into from
Apr 17, 2023

Conversation

ValentinKaisermayer
Copy link
Contributor

@ValentinKaisermayer ValentinKaisermayer commented Oct 9, 2022

This adds a caching interface for MOI solvers as well as simple (only linear and quadratic objective and constraints with possible integer variables) support for MOI solvers that do not support the NLBlock interface, e.g. Gurobi or HiGHS.

using ModelingToolkit
import Gurobi
import Optimization
import OptimizationMOI

@parameters c = 0.0
@variables x[1:2] = [0.0, 0.0] [bounds = (c, Inf)]
@variables y = 0.0
@variables z = 0.0
@parameters a = 1.0
@parameters b = 1.0
@parameters d = 1.0
@named sys = OptimizationSystem(
    x[1] + x[2], [x..., y, z], [a, b, c, d]; 
    constraints=[
        1.0  a * x[1]^2 * b + x[2]^2, # non-convex constraint
        x[1]^2 + a*b/d*x[2]^2  2.0, 
        a * y - a*z*y + b^2*z*y ~ 1.0, # some dummy constraint
    ]
)
prob = OptimizationProblem(sys, [x[1] => 2.0, x[2] => 0.0], []; grad=true, hess=true)
cache = Optimization.init(prob, Gurobi.Optimizer(), NonConvex=2)
sol = Optimization.solve(cache)
sol.u # ≈ [1, 0]

Optimal solution found (tolerance 1.00e-04)
Best objective 9.999997337719e-01, best bound 9.999993908614e-01, gap 0.0000%

User-callback calls 192, time in user-callback 0.00 sec
4-element Vector{Float64}:
 0.99999973377192
 0.0
 1.0
 0.0

see also SciML/ModelingToolkit.jl#1786

@codecov
Copy link

codecov bot commented Oct 9, 2022

Codecov Report

Merging #381 (1b199e6) into master (672d7ce) will decrease coverage by 4.45%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##           master     #381      +/-   ##
==========================================
- Coverage   27.89%   23.44%   -4.45%     
==========================================
  Files          34       36       +2     
  Lines        1678     2026     +348     
==========================================
+ Hits          468      475       +7     
- Misses       1210     1551     +341     
Impacted Files Coverage Δ
lib/OptimizationMOI/src/OptimizationMOI.jl 1.14% <0.00%> (+1.14%) ⬆️
lib/OptimizationMOI/src/moi.jl 0.00% <0.00%> (ø)
lib/OptimizationMOI/src/nlp.jl 0.00% <0.00%> (ø)
src/function/function.jl 89.18% <ø> (ø)
src/function/mtk.jl 81.25% <ø> (ø)

... and 4 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@ValentinKaisermayer
Copy link
Contributor Author

builds on #400

@ValentinKaisermayer ValentinKaisermayer marked this pull request as ready for review October 22, 2022 16:58
@ValentinKaisermayer ValentinKaisermayer marked this pull request as draft October 25, 2022 15:04
@ValentinKaisermayer ValentinKaisermayer marked this pull request as ready for review October 26, 2022 17:15
@ChrisRackauckas
Copy link
Member

This is pretty awesome. @Vaibhavdixit02 are you reviewing this?

@Vaibhavdixit02
Copy link
Member

Yes

@ValentinKaisermayer
Copy link
Contributor Author

No idea why the docs fail. There is no such @assert in the code. Maybe the docs do not build on this branch?

@ChrisRackauckas
Copy link
Member

It does run the branch

@ValentinKaisermayer
Copy link
Contributor Author

It does run the branch

I think it is not. I added the exact same code to the tests and they pass but the docs not.

@ValentinKaisermayer
Copy link
Contributor Author

Still, I think the docs are not built with this branch. The only place the error can be from is this: https://github.com/SciML/Optimization.jl/blob/master/lib/OptimizationMOI/src/OptimizationMOI.jl#L219
There is no such @assert in my branch.

@zsz00
Copy link

zsz00 commented Jan 13, 2023

what's status now?
@ValentinKaisermayer @ChrisRackauckas

@ValentinKaisermayer
Copy link
Contributor Author

It needs review and the conflicts need to be resolved.

@@ -5,9 +5,11 @@ version = "0.1.7"

[deps]
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this dependecy?

@Vaibhavdixit02
Copy link
Member

@ValentinKaisermayer this seems to based off on the caching PR but that's marked as draft still, can you clean that up and we will merge that and then this?

Comment on lines +21 to +31
expr_map = get_expr_map(prob.f.sys)
expr = repl_getindex!(convert_to_expr(MTK.subs_constants(MTK.objective(prob.f.sys)),
prob.f.sys; expand_expr = false, expr_map))

cons = MTK.constraints(prob.f.sys)
cons_expr = Vector{Expr}(undef, length(cons))
Threads.@sync for i in eachindex(cons)
Threads.@spawn cons_expr[i] = repl_getindex!(convert_to_expr(Symbolics.canonical_form(MTK.subs_constants(cons[i])),
prob.f.sys;
expand_expr = false,
expr_map))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

MTK is needed here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although both are in the prob.expr and prob.cons_expr, the parameters are replaced in MTK:
https://github.com/SciML/ModelingToolkit.jl/blob/master/src/systems/optimization/optimizationsystem.jl#L497-L498

@zsz00
Copy link

zsz00 commented Mar 17, 2023

what's status now? can merge this ?
@ValentinKaisermayer @ChrisRackauckas

@ChrisRackauckas
Copy link
Member

It doesn't pass tests if I'm not mistaken.

src/function/mtk.jl Outdated Show resolved Hide resolved
@Vaibhavdixit02
Copy link
Member

@ValentinKaisermayer can you take a look at the Codecov warnings, it seems to be a lot so I am worried (also kind of making reviewing the MOI file painful)

robustifies MOI term collection

fix

fix

fix

format

do the expression expansion ourselfs

adds returncode handling

format

make sure that simplify returns eventually

expand quad expressions

dont call on operator

changes @asserts to meaningfull exceptions

error early if NaN or Inf is in an expression

adds test

adds opt

fix test

use process_p_u0_symbolic

formatting
@ValentinKaisermayer
Copy link
Contributor Author

I'm pretty certain that the codevov is broken. It marks essentially every line as not hit, but the tests pass, that is not possible.

@ChrisRackauckas
Copy link
Member

Yes, Codecov has issues that won't be fixed here. That shouldn't get in the way of the review.

lib/OptimizationMOI/src/moi.jl Show resolved Hide resolved
lib/OptimizationMOI/src/moi.jl Show resolved Hide resolved
lib/OptimizationMOI/src/moi.jl Show resolved Hide resolved
lib/OptimizationMOI/src/moi.jl Show resolved Hide resolved
@ValentinKaisermayer
Copy link
Contributor Author

@ChrisRackauckas
Copy link
Member

That should probably also using nothings.

@ChrisRackauckas
Copy link
Member

Oh wait, there the reason is that nothing is an allowed answer for a lot of those values.

@ChrisRackauckas
Copy link
Member

A concretized cache can have a nothing initial condition if u0 is not used by the optimizer (some global optimizers), so maybe that is appropriate here.

@ChrisRackauckas
Copy link
Member

Also p = nothing is an allowed value. So yes keep it missing.

@Vaibhavdixit02
Copy link
Member

Okay that makes sense, I'll dismiss my comments above.

@Vaibhavdixit02 Vaibhavdixit02 merged commit 2639ecb into SciML:master Apr 17, 2023
@ChrisRackauckas
Copy link
Member

🎉

Very happy this is in.

Though what's missing is tutorials on these new features. This and the integer support is not documented anywhere. The docs really need to get up to speed.

@Vaibhavdixit02
Copy link
Member

Really great work @ValentinKaisermayer!

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.

Support for MathOptInterface.AbstractOptimizer that do not support MathOptInterface.NLPBlock
4 participants