# How do I write @variable(z=x\[1\]+x\[2\])?

**URL:** https://discourse.julialang.org/t/how-do-i-write-variable-z-x-1-x-2/79149
**Category:** Optimization (Mathematical)
**Tags:** question, jump
**Created:** [April 7, 2022, 8:30am UTC](https://discourse.julialang.org/t/how-do-i-write-variable-z-x-1-x-2/79149 "2022-04-07T08:30:45Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![castlemas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/castlemas/32/35276_2.png) [@castlemas](https://discourse.julialang.org/u/castlemas)
#### Post date: [April 7, 2022, 8:30am UTC](https://discourse.julialang.org/t/how-do-i-write-variable-z-x-1-x-2/79149/1 "2022-04-07T08:30:45Z")

</div>

Hi,  
I am trying to solve a min-max optimisation problem that requires me to add in a constraint

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/2/121d5f8e9d45cee03dd5cfedd1dfae3ea171712a.png)

How do I assign a constraint such that a variable equals to another variable?

---

<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: [April 7, 2022, 8:53am UTC](https://discourse.julialang.org/t/how-do-i-write-variable-z-x-1-x-2/79149/2 "2022-04-07T08:53:04Z")

</div>

Hi there,

Since this is your first post, please read: [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757). It has some advice on how to write a good question.

I’m not entirely sure what you mean by an equality in the objective function, but here’s how I would write your model:

```julia
model = Model()
@variable(model, x[1:2] >= 0, Int)
@variable(model, z)
@constraint(model, z == x[1]) # Note sure which one of these you meant 
@constraint(model, z == 8x[1] + 5x[2]) # 
@constraint(model, 8x[1] + 1.5x[2] <= z)
@constraint(model, x[1] + x[2] <= 6)
@objective(model, Max, z)

```

If that isn’t helpful, perhaps you could explain in a bit more detail what you’re trying to achieve?

---

<div class="post-metadata">

### Author: ![castlemas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/castlemas/32/35276_2.png) [@castlemas](https://discourse.julialang.org/u/castlemas)
#### Post date: [April 7, 2022, 9:47am UTC](https://discourse.julialang.org/t/how-do-i-write-variable-z-x-1-x-2/79149/3 "2022-04-07T09:47:19Z")

</div>

Hi, Thank you for your reply!

Here is the equation i was trying to solve:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/3/c3fcaedbeea563ce5972bdda72c321da5f927947.png)

What I have done so far on Julia-Jump

```julia
m = Model(Mosek.Optimizer)
@objective(m, Min, Z);
@variable(m, Z);
@variable(m, F[1:14]>=0);
#I have only included a portion of the constriant 
@constraint(m,8*F[1]+1.5*F[2]+10*F[3] <= Z);
print(m)

```

**However, I face the error**

```julia

UndefVarError: Z not defined

Stacktrace:
 [1] macro expansion
   @ C:\Users\Admin\.julia\packages\MutableArithmetics\UtY4H\src\rewrite.jl:281 [inlined]
 [2] macro expansion
   @ C:\Users\Admin\.julia\packages\JuMP\lnUbA\src\macros.jl:1403 [inlined]
 [3] top-level scope
   @ In[34]:2
 [4] eval
   @ .\boot.jl:373 [inlined]
 [5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base .\loading.jl:1196

```

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [April 7, 2022, 9:52am UTC](https://discourse.julialang.org/t/how-do-i-write-variable-z-x-1-x-2/79149/4 "2022-04-07T09:52:21Z")

</div>

Swapping the order of statements

```julia
using JuMP

m = Model()
@variable(m, Z);
@variable(m, F[1:14]>=0);
#I have only included a portion of the constriant 
@constraint(m,8*F[1]+1.5*F[2]+10*F[3] <= Z);
@objective(m, Min, Z);
print(m)

```

seems to help.One tip: if you want to know what the macros (`@variable` etc.) are doing you can use `@macroexpand`.

---

<div class="post-metadata">

### Author: ![castlemas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/castlemas/32/35276_2.png) [@castlemas](https://discourse.julialang.org/u/castlemas)
#### Post date: [April 7, 2022, 9:27pm UTC](https://discourse.julialang.org/t/how-do-i-write-variable-z-x-1-x-2/79149/5 "2022-04-07T21:27:07Z")

</div>

thanks sosososososososo much!!

---

<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: [April 7, 2022, 9:32pm UTC](https://discourse.julialang.org/t/how-do-i-write-variable-z-x-1-x-2/79149/6 "2022-04-07T21:32:38Z")

</div>

I don’t really understand the problem. Are these coupled as a bilevel program? Or do you want to solve two separate problems? First to find an upper bound on `Z`, and then to find a lower bound?
