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

Scope of index variables #858

Closed
allbits opened this issue Oct 2, 2016 · 6 comments · Fixed by #2486
Closed

Scope of index variables #858

allbits opened this issue Oct 2, 2016 · 6 comments · Fixed by #2486
Labels
Type: Error Messages Can be fixed with better error message

Comments

@allbits
Copy link

allbits commented Oct 2, 2016

m = Model()
@variable(m,x[1:5])
@constraint(m,[m=1:4],x[m]==x[m+1])

complains that there's no addconstraint method with an Int64 as the first argument. Should this work?

@constraint(m,[i=1:4],x[i]==x[i+1])

is fine.

@mlubin
Copy link
Member

mlubin commented Oct 3, 2016

Should this work?

I'm struggling to see the benefit of allowing this to work.

@allbits
Copy link
Author

allbits commented Oct 3, 2016

I'm struggling to see the benefit of allowing this to work.

There's probably little benefit, since you always can change the variable. I see this more as a question of clear scoping rules. In
@constraint(m,[m=1:4],x[m]==x[m+1])
it seems natural to read "[m=1:4]" as a universal quantifier and expect m to be bound to 1:4 in the scope of the restrictions. Or consider:
@constraint(m,sum{x[m],m=1:5}==1)
where it seems natural to expect m to be bound to 1:5 in the scope of the sum.

@mlubin
Copy link
Member

mlubin commented Oct 4, 2016

I'm not opposed to making things more consistent but I don't expect to spend any time fixing this soon. PRs will be considered.

@mlubin
Copy link
Member

mlubin commented Dec 2, 2017

Possibly addressed in Julia 0.7 by JuliaLang/julia#22314

@mlubin
Copy link
Member

mlubin commented Feb 24, 2019

This is not addressed by JuliaLang/julia#22314.

@constraint(m,[i=1:4],x[i]==x[i+1])

is translated essentially to:

for i in 1:4
    @constraint(m, x[i] == x[i + 1])
end

We would need to mangle the iteration variables to avoid the name conflict. This is feasible to do but still not high priority. The only cases I've seen where users run into this issue is when the model is named m. We've stopped recommending this name for the model.

@odow odow added the Type: Error Messages Can be fixed with better error message label Feb 19, 2021
@odow
Copy link
Member

odow commented Feb 19, 2021

Rather than make this work, we should just throw an error if the index set has the same name as the model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Error Messages Can be fixed with better error message
Development

Successfully merging a pull request may close this issue.

3 participants