Question about UndefVarError in for loop

@variable(model,Y[1:n], lower_bound=0)
T = Float64.(model.TP)
D = Float64.(model.TD)

Yy = transpose(Y)
a = Int64
b = Int64
a = 1
b = n

for j in 1:m
@constraint(model,Yy[1:n].*T[a:b] .<= D[j])
a = a+n
b = b+n
end - UndefVarError: a not defined

Please quote your code: PSA: how to quote code with backticks

You need global a = a + n if you want to modify the global variable a from a for loop.

2 Likes

Also just as an aside

a = Int64
b = Int64
a = 1
b = n

this doesn’t seem to make sense - the first two lines just make a and b aliases of Int64, i.e. after doing this you can do:

julia> a = Int64
Int64

julia> a(2.0)
2

You might be thinking of languages in which variables have to be declared with type annotations, this is not necessary in Julia. Consider:

julia> a = 1
1

julia> typeof(a)
Int64

although there might be instances where you want to use the type declaration syntax a::Int64, see the section on type declarations in the manual.

2 Likes