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

Add GenericModel{T} and GenericVariableRef{T} #3377

Merged
merged 3 commits into from Jun 28, 2023

Conversation

blegat
Copy link
Member

@blegat blegat commented May 23, 2023

Following #3191 (comment), this PR takes out from #3191 the part of src that should be boilerplate and not prone to generate argument to allow the important parts to stand out from #3191 and also to be able to merge this big part that is prone to create conflicts.

@blegat blegat force-pushed the bl/nonbreaking_generic_number_type branch from f81fe3f to 0ea2871 Compare May 23, 2023 13:59
@codecov
Copy link

codecov bot commented May 23, 2023

Codecov Report

Patch coverage: 99.44% and project coverage change: -0.01 ⚠️

Comparison is base (0d1d90a) 97.97% compared to head (bc9a2b3) 97.97%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3377      +/-   ##
==========================================
- Coverage   97.97%   97.97%   -0.01%     
==========================================
  Files          34       34              
  Lines        4930     4979      +49     
==========================================
+ Hits         4830     4878      +48     
- Misses        100      101       +1     
Impacted Files Coverage Δ
src/copy.jl 96.19% <93.33%> (+0.03%) ⬆️
src/variables.jl 98.76% <99.23%> (-0.17%) ⬇️
src/JuMP.jl 94.87% <100.00%> (+0.20%) ⬆️
src/aff_expr.jl 97.28% <100.00%> (ø)
src/callbacks.jl 100.00% <100.00%> (ø)
src/constraints.jl 96.56% <100.00%> (ø)
src/feasibility_checker.jl 100.00% <100.00%> (ø)
src/file_formats.jl 100.00% <100.00%> (ø)
src/lp_sensitivity2.jl 97.79% <100.00%> (ø)
src/macros.jl 98.63% <100.00%> (ø)
... and 8 more

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

src/constraints.jl Outdated Show resolved Hide resolved
src/JuMP.jl Outdated Show resolved Hide resolved
src/constraints.jl Outdated Show resolved Hide resolved
src/constraints.jl Outdated Show resolved Hide resolved
src/constraints.jl Outdated Show resolved Hide resolved
@odow
Copy link
Member

odow commented May 24, 2023

The last commit, 825459b, fixes a number of cases that still used Model, VariableRef, AffExpr, or QuadExpr. I've gone through and audited every instance of these types remaining in /src, and I think I have them all now (except for src/nlp.jl).

Since this PR is extracted from #3191, which is passing tests, I assume that this means we don't have good test coverage for non-Float64 number types.

@odow
Copy link
Member

odow commented May 24, 2023

Rebasing this now since it was my fault

@odow odow force-pushed the bl/nonbreaking_generic_number_type branch from 825459b to f23f543 Compare May 24, 2023 08:01
src/constraints.jl Outdated Show resolved Hide resolved
src/constraints.jl Outdated Show resolved Hide resolved
src/constraints.jl Outdated Show resolved Hide resolved
src/constraints.jl Outdated Show resolved Hide resolved
Copy link
Member

@odow odow left a comment

Choose a reason for hiding this comment

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

I think we should still hold off merging this for now. But I'm a lot more confident in these changes than when it was in the monolithic PR.

@odow odow added the Status: Needs developer call This should be discussed on a monthly developer call label May 24, 2023
@odow odow force-pushed the bl/nonbreaking_generic_number_type branch from 19c203f to 28a2bb1 Compare May 24, 2023 22:02
@odow odow changed the title Boilerplate part of generic number types Add GenericModel{T} and GenericVariableRef{T} May 24, 2023
src/variables.jl Outdated Show resolved Hide resolved
src/JuMP.jl Outdated
"""
value_type(::Type{<:AbstractModel}) = Float64

mutable struct GenericModel{T} <: AbstractModel
Copy link
Member

Choose a reason for hiding this comment

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

Can we use a more descriptive name instead of T

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe it's enough if it's in the docstring ? Having a small name might make the user thing there is a simple interpretation and no need to look up the docstring. At least with T it forces the user to read the docstring and get the full explanation and not get a wrong understanding ^^

src/JuMP.jl Outdated Show resolved Hide resolved
@odow
Copy link
Member

odow commented Jun 1, 2023

@blegat take a look at the last commit I pushed. We can always revert, but any reason why we didn't add a value_type keyword argument to Model in the first place?

@blegat
Copy link
Member Author

blegat commented Jun 1, 2023

I guess it's a bit weird to have GenericModel{Float64}(value_type = BigFloat).
Maybe model(value_type = BigFloat) and direct_model(value_type = BigFloat) ?

@odow
Copy link
Member

odow commented Jun 1, 2023

GenericModel{Float64}(value_type = BigFloat)

Yeah this is weird. But people would really do

julia> GenericModel{Float64}(value_type = Int)
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> Model(value_type = Int)
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

model(value_type = BigFloat)

We cannot do this. It would export model and conflict with everyone using model as the name of their Model.

docs/src/reference/models.md Outdated Show resolved Hide resolved
@blegat
Copy link
Member Author

blegat commented Jun 2, 2023

model(value_type = BigFloat)

We cannot do this. It would export model and conflict with everyone using model as the name of their Model.

Then what about generic_model(value_type = BigFloat) and direct_model(value_type = BigFloat) ?

@odow
Copy link
Member

odow commented Jun 4, 2023

The documented API detail is Model(; value_type). It's the most JuMP-like API, and I don't think that's confusing, or that there is any ambiguity.

It's an implementation detail that Model is also a synonym for GenericModel{Float64}. We'd never actually document people using the GenericModel{T} constructor.

docs/src/changelog.md Outdated Show resolved Hide resolved
@odow odow force-pushed the bl/nonbreaking_generic_number_type branch from 34a9b14 to 036d014 Compare June 4, 2023 23:51
@blegat
Copy link
Member Author

blegat commented Jun 5, 2023

We need a keyword argument for direct_model as well. At the moment Model is hardcoded in direct_model. I always found direct_model vs Model a bit inconsistent so I thought it was a good occasion to have direct_model vs generic_model but the generic vs direct is maybe not the best way to describe the difference between direct_model and Model.

I guess we should just check whether having the constructor Model not create a Model violates one of the Julia rules for constructors

src/JuMP.jl Outdated
function direct_model(backend::MOI.ModelLike)
function direct_model(
backend::MOI.ModelLike;
value_type::Type{T} = Float64,
Copy link
Member

Choose a reason for hiding this comment

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

@blegat I already added the kwarg for direct_model

@odow
Copy link
Member

odow commented Jun 5, 2023

I guess we should just check whether having the constructor Model not create a Model violates one of the Julia rules for constructors

I don't think there's any technical issue with it. Stylistically it's weird only if you know that Model is an alias. We just never mention the GenericModel constructor.

@blegat
Copy link
Member Author

blegat commented Jun 6, 2023

@mlubin Any opinion on this ?

@odow odow removed the Status: Needs developer call This should be discussed on a monthly developer call label Jun 9, 2023
@odow odow force-pushed the bl/nonbreaking_generic_number_type branch from 4a55ebc to 585630b Compare June 11, 2023 21:25
docs/src/manual/variables.md Outdated Show resolved Hide resolved
@odow odow force-pushed the bl/nonbreaking_generic_number_type branch from 372f638 to ebe5b47 Compare June 11, 2023 21:54
@mlubin
Copy link
Member

mlubin commented Jun 22, 2023

Model(; value_type) and direct_model(; value_type) seems reasonable to me. It's the least disruptive. Exporting model is a no-go because of so many local variables named model. Will Julia specialize on keyword argument types in this context (i.e., will the return value of Model(; value_type=T) infer as GenericModel{T})?

Could we make GenericModel{Float64}(value_type = BigFloat) an explicit error?

@odow
Copy link
Member

odow commented Jun 22, 2023

  • Check type inference of Model() and Model(; value_type = BigFloat)
  • If inference fails, switch back to GenericModel{T}
  • direct_generic_model(::Type{T}, optimizer) where {T<:Real}
  • direct_model{T}(optimizer)
  • model = @model()

@odow
Copy link
Member

odow commented Jun 22, 2023

So I guess the inference failure is a kicker:

image

Let me revert to what we had before.

@odow odow force-pushed the bl/nonbreaking_generic_number_type branch from ebe5b47 to 70a1ccb Compare June 22, 2023 22:30
docs/make.jl Outdated Show resolved Hide resolved
docs/make.jl Outdated Show resolved Hide resolved
@odow odow force-pushed the bl/nonbreaking_generic_number_type branch 3 times, most recently from af1eda9 to e9bdd50 Compare June 26, 2023 22:20
This is a non-breaking feature addition that prepares the way for generic number
types in JuMP.
This is another step in the process of adding generic number support to
JuMP. These commits are separate to make them easier to review and to
minimize the risk of introducing breaking changes.
@odow odow force-pushed the bl/nonbreaking_generic_number_type branch from 7718087 to 43178a1 Compare June 26, 2023 23:33
@blegat
Copy link
Member Author

blegat commented Jun 27, 2023

Looks good to merge

@odow
Copy link
Member

odow commented Jun 27, 2023

Looks good to merge

Let's wait for @mlubin to take another look.

src/JuMP.jl Outdated Show resolved Hide resolved
Co-authored-by: Miles Lubin <mlubin@google.com>
@odow odow merged commit fcbb69a into master Jun 28, 2023
11 of 12 checks passed
@odow odow deleted the bl/nonbreaking_generic_number_type branch June 28, 2023 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants