Facility Location Example: what do the arguments in select and assign variables creation and if (abs(select[facility].x) > 1e6) mean?
AnsweredHi!
I am new to Gurobi and only have basic Python skills. I was studying the Facility Location example and these are parts of the code that I do not understand.
When the select and assign variables are created,
select = m.addVars(num_facilities, vtype=GRB.BINARY, name='Select')What is the role of num_facilities and cartesian_prod in the arguments?
assign = m.addVars(cartesian_prod, ub=1, vtype=GRB.CONTINUOUS, name='Assign')
if (abs(select[facility].x) > 1e6):

Hi Julie,
What is the role of num_facilities and cartesian_prod in the arguments?
\(\texttt{num_facilities}\) and \(\texttt{cartesian_prod}\) define the indices of the variables \(\texttt{select}\) and \(\texttt{assign}\), cf. documentation of addVars.
In the facility location example, we have
num_facilities = 9
cartesian_prod =[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8)]\(\texttt{num_facilities}\) is just an integer stating that there are 9 facilities and \(\texttt{cartesian_prod}\) is a list of tuples describing the Cartesian product of number of customers and number of facilities.
When we create variables via
select = m.addVars(num_facilities, vtype=GRB.BINARY, name='Select')
assign = m.addVars(cartesian_prod, ub=1, vtype=GRB.CONTINUOUS, name='Assign')you can then access the corresponding variables via their respective indices. For example
print(select[2])
print(assign[1,4])This is possible, because the data structure we get from the addVars method is a tupledict.
I would like to know what > 1e6 means. I also would just like to confirm what .x does, I read the quick start guide and it apparently contains the optimal solution vector, is this correct?
In this particular if clause, we check whether the optimal solution value of variables \(\texttt{select}\) are strictly larger than \(10^{6}\). In other words, we want to check which of the \(\texttt{select}\) variables is \(1\) meaning that a particular facility/warehouse has to be built.
You are correct, we access the optimal solution value of the variables via the X attribute. We do the check against \(10^{6}\) to make sure that the corresponding binary variable is truly bigger than \(0\) and not within the default FeasibilityTol. It is possible that a binary variable is not truly \(0\) or \(1\) but rather, e.g., \(10^{8}\), due to numerical computation. To make the check more clear and to avoid possible numerical instabilities, one could alternatively check
if (abs(select[facility].x) > 0.5):
I hope this helps.
Best regards,
Jaromił0 
Thank you so much for your answer!
Are indices usually the first argument when creating variables?
0 
When you are using the addVars method, then indices have to be the first argument. This can be seen in the corresponding documentation:
addVars ( *indices, lb=0.0, ub=float('inf'), obj=0.0, vtype=GRB.CONTINUOUS, name="" )
0
Please sign in to leave a comment.
Comments
3 comments