# Modular implementation of optimization problems with JuMP

**URL:** <https://discourse.julialang.org/t/modular-implementation-of-optimization-problems-with-jump/116261>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, energy\
**Created:** [June 26, 2024, 2:06pm UTC](https://discourse.julialang.org/t/modular-implementation-of-optimization-problems-with-jump/116261 "2024-06-26T14:06:27Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [July 17, 2024, 2:59pm UTC](https://discourse.julialang.org/t/modular-implementation-of-optimization-problems-with-jump/116261/10 "2024-07-17T14:59:31Z")

</div>

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](https://discourse.julialang.org/t/modular-implementation-of-optimization-problems-with-jump/116261/4) 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:

```julia
data["x"] = @variable(model, 0 <= x <= 1)
unregister(model, :x)

```

versus

```julia
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)`](https://jump.dev/JuMP.jl/stable/api/JuMP/#object_dictionary) mentioned in [How to get all the registered variable names of a `JuMP` model?](https://discourse.julialang.org/t/how-to-get-all-the-registered-variable-names-of-a-jump-model/113837). The model definition would then look like this:

```julia
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

```

---

_[View the full topic](https://discourse.julialang.org/t/modular-implementation-of-optimization-problems-with-jump/116261)._
