Skip to content

Feature request: reuse a variable name in @variable on non-overlapping indices #2200

@remi-garcia

Description

@remi-garcia

It would be nice if the following code works

using JuMP
model = JuMP.Model()
@variable(model, -5 <= x[0:5] <= 5, Int)
@variable(model, -5 <= x[6:12] <= 5, Int)

or

for i in -7:7
    @variable(model, x[i], lower_bound=foo(i))
end

We could reuse a variable name if the indices on which it is defined don't overlap.

This has been discussed on discourse. Thanks to @odow I use this solution:

using JuMP
model = Model()
n = 10
x = Dict{Int, VariableRef}(
    i => @variable(model, base_name = "x[$(i)]")
    for i = 0:n
)

n += 1
x[n] = @variable(model, base_name = "x[$n]")
@constraint(model, x[n] <= foo(n))

and when x is needed in another function it can be assigned like this:

n = 0
while variable_by_name(model, "x[$n]") !== nothing
    n += 1
end
n -= 1
x = Dict{Int, VariableRef}(
    i => variable_by_name(model, "x[$i]")
    for i in 0:n
)

This works for me, yet a better way (first block of code) to achieve this would be great.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions