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

@variable declarations returning empty container should have VariableRef element type #3499

Closed
ExpandingMan opened this issue Sep 11, 2023 · 5 comments · Fixed by #3500
Closed
Labels
Category: Containers Related to the Containers submodule Type: Bug

Comments

@ExpandingMan
Copy link
Contributor

Is your feature request related to a problem? Please describe.
On latest tagged, @variable(m, x[1:0]) returns a Vector{Any}. Because of this, code which uses x as an input cannot infer the containers element type. An example of how this can go wrong is if one calls sum(x) it will throw an error because there is no zero(Any), where as zero(VariableRef) gives a zero AffExpr.

Describe the solution you'd like
The simplest solution would be to check for empty returns and convert them to Array{VariableRef}. However, I realize that this ignores the possibility of other variable types, so, alternatively, it might be nice to add a keyword argument. Preferably, this would still default to something other than Any.

Describe alternatives you've considered
You can convert the result, but it seems undesirable to have to do the check.

Additional context
I think previous versions of JuMP returned a VariableRef container, but I haven't verified this.

@odow
Copy link
Member

odow commented Sep 11, 2023

JuMP@1.0.0 produced:

julia> using JuMP

julia> model = Model();

julia> @variable(model, x[1:0])
VariableRef[]

Latest master produced:

julia> using JuMP

julia> model = Model();

julia> @variable(model, x[1:0])
GenericVariableRef[]

so this is a regression.

Let me take a look.

@odow odow added Type: Bug Category: Containers Related to the Containers submodule labels Sep 11, 2023
@odow
Copy link
Member

odow commented Sep 11, 2023

Ah. This is because model is a non-const global, so inference can't figure out the return type of the variable:

julia> using JuMP

julia> model = Model();

julia> @variable(model, x[1:0])
GenericVariableRef[]

julia> add_var(model) = @variable(model, y[1:0])
add_var (generic function with 1 method)

julia> add_var(model)
VariableRef[]

@ExpandingMan
Copy link
Contributor Author

I'm confused, I was getting Any[] when the type of Model should have been inferable. I'll try to come up with a MWE when I get a chance.

@odow
Copy link
Member

odow commented Sep 11, 2023

I didn't try the latest tag yet. I could see how that might return Any.

I think I have a fix for the underlying issue. Just running the tests.

@odow
Copy link
Member

odow commented Sep 14, 2023

I found your Any:

julia> m = (;m = Model());

julia> @variable(m.m, x[1:0])
Any[]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Containers Related to the Containers submodule Type: Bug
Development

Successfully merging a pull request may close this issue.

2 participants