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

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

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

Hi there,

Since this is your first post, please read: Please read: make it easier to help you. 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:

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?

3 Likes

Hi, Thank you for your reply!

Here is the equation i was trying to solve:

What I have done so far on Julia-Jump

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


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

Swapping the order of statements

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.

2 Likes

thanks sosososososososo much!!

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?