Yes, with anonymous variables come the need to use the lower_bound
/upper_bound
keywords. Then this starts to look like my custom add_var!
wrapper mentioned above that I’d like to avoid.
But the main argument against anonymous variables is the need to add the variable to the current namespace (I say “need” in the sense that it’s handy for writing subsequent model lines). Plus, using anonymous variables needs setting the base_name
attribute (here “need” means only that is useful when printing expressions or constraints)
In the end, it’s about trade-offs:
data["x"] = @variable(model, 0 <= x <= 1)
unregister(model, :x)
versus
x = @variable(model, lower_bound=0, upper_bound=1, base_name="x") # base_name is optional, only for prints
data["x"] = x # and this may be compressed into data["x"] = x = @var...
Now, thinking a bit more, if I’m sure I want to unregister all registered symbols, it’s possible to do it in one loop at the end using object_dictionary(model)
mentioned in How to get all the registered variable names of a `JuMP` model?. The model definition would then look like this:
data["x"] = @variable(model, 0 <= x <= 1)
data["y"] = @variable(model, 0 <= y <= 1)
@constraint(model, x+y <= 1) # here comes the interest of having x and y in the namespace
# ...
for var in keys(object_dictionary(model))
unregister(model, var)
end