# Registering Expressions as JuMP variables in a loop

**URL:** <https://discourse.julialang.org/t/registering-expressions-as-jump-variables-in-a-loop/119223>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [September 9, 2024, 6:18pm UTC](https://discourse.julialang.org/t/registering-expressions-as-jump-variables-in-a-loop/119223 "2024-09-09T18:18:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![theplatters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theplatters/32/52199_2.png) [@theplatters](https://discourse.julialang.org/u/theplatters)\
**Post date:** [September 9, 2024, 6:18pm UTC](https://discourse.julialang.org/t/registering-expressions-as-jump-variables-in-a-loop/119223/1 "2024-09-09T18:18:07Z")

</div>

Hey guys, I have a project, where I create a model like this

```julia
m = @model begin
	@exogenous data
	@exogenous consumption
	production[1:71] := inv(I - data.Ω) * consumtion
	prices[1:71] := @maxc c(production) constraints(c)
end

```

This should then be converted into a JuMP Model, so `production` and `prices` should be a variable in this model. I store the expression on the LHS and RHS of the `:=` operator in a `Dict{Expr,Expr}` and when building the JuMP Model I loop over them. Now here is the problem:

```julia
function build_jump_model(em::EconomicModel)
	m = Model(HiGHS.Optimizer)
	for (k,v) in em.maximisation_variables
		@info k
		@variable(m, eval(k))
	end
	return m
end

```

This errors on compilation and

```julia
function build_jump_model(em::EconomicModel)
   m = Model(HiGHS.Optimizer)
   for (k,v) in em.maximisation_variables
   	@info k
   	@variable(m, k)
   end
   return m
end

```

this just registers `:k` as a variable. (why is it :k and not just k tho?)  
Is it even possible to correctly register the value of `k`? Thanks

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 10, 2024, 3:09am UTC](https://discourse.julialang.org/t/registering-expressions-as-jump-variables-in-a-loop/119223/2 "2024-09-10T03:09:43Z")

</div>

Hi @theplatters 😄

I don’t know what `@model` and `@exogenous` are, so I don’t know how much help I will be, but:

You probably need

```Julia
function build_jump_model(em::EconomicModel)
	model = Model(HiGHS.Optimizer)
	for (k, v) in em.maximisation_variables
		model[k] = @variable(model)
	end
	return model
end

```

or

```Julia
function build_jump_model(em::EconomicModel)
	model = Model(HiGHS.Optimizer)
	@variable(model, x[keys(em.maximisation_variables)])
	return model
end

```

You should almost _never_ use `eval`.

I’ll point you to these parts of the JuMP documentation:

- [Variables · JuMP](https://jump.dev/JuMP.jl/stable/manual/variables/#anonymous_variables)
- [Variables · JuMP](https://jump.dev/JuMP.jl/stable/manual/variables/#variable_names_and_bindings)
- [Variables · JuMP](https://jump.dev/JuMP.jl/stable/manual/variables/#User-defined-containers)

Happy to answer further questions after you’ve read.

I’ll caution you that if you’re writing the `@model` macro, then getting this to work can be _very_ tricky. I’d encourage you to think of alternative approaches before you try to be too clever with the macros.

---

<div class="post-metadata">

**Author:** ![theplatters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theplatters/32/52199_2.png) [@theplatters](https://discourse.julialang.org/u/theplatters)\
**Post date:** [September 14, 2024, 2:52pm UTC](https://discourse.julialang.org/t/registering-expressions-as-jump-variables-in-a-loop/119223/4 "2024-09-14T14:52:13Z")

</div>

Thank you for the detailed reply.

The first code you provided solves the problem. Although I don’t like how JuMP changes the global state of variables.

What alternative approaches would you suggest? I want it to be as concise as possible, so that the user has to not worry about implementation details, that’s why I started writing it like this.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 15, 2024, 4:41am UTC](https://discourse.julialang.org/t/registering-expressions-as-jump-variables-in-a-loop/119223/5 "2024-09-15T04:41:30Z")

</div>

> Although I don’t like how JuMP changes the global state of variables

Unfortunately for you, then, this is a fundamental design decision in JuMP which will never change.

> What alternative approaches would you suggest

I didn’t suggest anything because I don’t know what you are trying to achieve. One downside to making a DSL with macros is that simplifies the process for people who are already familiar with the domain, but it makes it harder for people unfamiliar to reason about what is happening. (As one example you yourself have already run into: `@variable(model, x)` does something very specific. And you cannot interpolate symbols or other variables into the `x` name.)
