How to register the name of anonymous variable to certain model?

Hi all,

I cannot view the anonymous variable in the model, when I use the following method:

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

julia> x = @variable(model, [1:2, 1:3])
2×3 Matrix{VariableRef}:
 noname  noname  noname
 noname  noname  noname

julia> model
A JuMP Model
Feasibility problem with:
Variables: 6
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

How can I set a name for the anonymous variable so that I can create it in the same way as the following variable creation method.

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

julia> @variable(model, x[1:2, 1:3])
2×3 Matrix{VariableRef}:
 x[1,1]  x[1,2]  x[1,3]
 x[2,1]  x[2,2]  x[2,3]

julia> model
A JuMP Model
Feasibility problem with:
Variables: 6
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: x

I don’t want to add 6 base_name to each of the 6 noname, but I want to register these 6 anonymous variables with the only one name x in the model, as if I didn’t create an anonymous variable in the beginning.

My question does seem strange, but I still want to know if there is such a way to achieve the idea.

Thanks a lot!

It seems there is a bit of confusion because “name” is an overloaded concept:

  • Each variable has a string name, accessible via name(x). If name(x) == "", then it get’s printed as noname
  • You can use base_name = to set a string name for anonymous variables
  • JuMP variables (and collections of) can be assigned to Julia variables. We often say that this is a binding between a name and an object. In the assignment y = 1, the object 1 is bound to the name y.
  • Named JuMP variables get bound to the Julia variable of the same name
  • Named JuMP variables are registered in the model using the symbol of the same name
  • Anonymous variables don’t get registered in the model, and you choose which name it gets bound to
  • You can manually register things in the model via model[:key] = value

Perhaps this example makes things clearer

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

julia> x_binding = @variable(model, [1:2, 1:3], base_name = "x")
2×3 Matrix{VariableRef}:
 x[1,1]  x[1,2]  x[1,3]
 x[2,1]  x[2,2]  x[2,3]

julia> model
A JuMP Model
Feasibility problem with:
Variables: 6
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> x
ERROR: UndefVarError: x not defined

julia> x_binding
2×3 Matrix{VariableRef}:
 x[1,1]  x[1,2]  x[1,3]
 x[2,1]  x[2,2]  x[2,3]

julia> model[:x_register] = x_binding
2×3 Matrix{VariableRef}:
 x[1,1]  x[1,2]  x[1,3]
 x[2,1]  x[2,2]  x[2,3]

julia> model
A JuMP Model
Feasibility problem with:
Variables: 6
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: x_register

julia> model[:x_register]
2×3 Matrix{VariableRef}:
 x[1,1]  x[1,2]  x[1,3]
 x[2,1]  x[2,2]  x[2,3]

julia> model[:x_register] === x_binding
true

julia> x
ERROR: UndefVarError: x not defined
1 Like