Beginner to Julia and want to quickly recapitulate an ILP model I wrote in Python to evaluate speed in JuMP.
I’m wondering how list comprehensions work for JuMP objects?
In Python, I use list comprehension to make the following constraints:
# PYTHON
for k in range(0,math.ceil(K_window/34)):
model.addConstr(y_i_k.sum('*',[l for l in range(k*34,min(k*34+34,K_window))]) <=1,name='Seen_'+str(k))
After reading about Julia syntax on list comprehension, I tried the following but it does not work:
# JULIA
for k in 1:floor(Int, K_window/34)
@constraint(model, sum(y_i_k[:, l] for l in (k*34):min(k*34, min(k*34+34, K_window)), dims=1) <=1)
end
I realize this is question may alternatively be categorized as “First Steps”, I have a hunch it’s just me not understanding Julia syntax? Thank you for your help.
So y_i_k[:, l] is the lth column of y_i_k. So the sum is a vector. Then you constraint this sum to be smaller than 1. What does it means for a vector to be smaller than a number ?
If you want each entry to be smaller than 1, do .<= instead of <=.
Here, you are constraining, for each row, the sum of all entries of that row that are in the column range (k*34):min(k*34, min(k*34+34, K_window)) to be smaller than or equal to 1.
I doesn’t work for me because [k*34:min(k*34, min(k*34+34, K_window))] is a vector containing one range so the for loop has one iteration with l = k*34:min(k*34, min(k*34+34, K_window)) and the sum is therefore the some of one term which is the term itself and you are left with a vector that cannot be compared to 1.
So either do sum(y_i_k[i, l] for l in k*34:min(k*34, min(k*34+34, K_window)))
or sum(y_i_k[i, k*34:min(k*34, min(k*34+34, K_window))])