0

A bit new to julia, and JuMP. I wanted to define variables in a loop i.e x_i for some loop index i. For example.

for i=1:10
@variable(m, x_i[1:100], bin)

end

and then reference x_i as needed, just as I would normally (without a loop) use x_1 if I hard coded it in constraints, objective etc. Effectively here, I would like to not have to hardcode.

From the documentation:

@variable(model, expr, args..., kw_args...)

Looks like you pass an expression, but its not clear how I would do this/if it is possible. Also it would be useful to know if I can do this, what the syntax is to reference the x_i created in the loop appropiatley in constraints, objectives, etc.

Any feedback would be greatly appreciated.

Thank you.

1 Answer 1

1

There are a couple of options:

@variable(m, x[i=1:10, 1:100], Bin)

x[1, 99]
x = [@variable(m, [1:100], Bin) for i in 1:10]

x[1][99]

But in no case can you dynamically create a binding like x_i.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey Oscar, thanks for the quick reply. I guess the user case I was interested in further was x_i would be either real, integer, or binary vectors. I.e lets say if i=1 those variables are real, if i=2 those are integer, etc. Is there the ability to build in this logic?
Use the keyword arguments: @variable(m, [1:100], integer = i > 1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.