Adding a column to a parameter and rerun it for new column
AnsweredHi,
I have a parameter in a 4*2 matrix. I want to run my optimization problem first for first column,then for second column in Gurobi.
v[i,j] is in my constraint, so I wanna run it first for v[i,'1'] then for v[i,'2']
That means I have two optimization problems, because my parameter changes. How can I write its code? Should I use ''for'' at the first of the code?
Thank you
-
Hi Arash,It would probably make more sense to write a function that gets a column as an input and solves the optimization problem using the Gurobi Optimizer. For example, if you are using Gurobi Python API, you can use the sketch below:
import gurobipy as gp
def solve(col):
model = gp.Model()
# Use col to define the variables, constraints, and objective
# ...
model.optimize()
return model.ObjVal
if __name__ == "__main__":
# Define matrix with n_rows rows and n_cols columns
for j in range(num_cols):
col = matrix[:, j]
solve(col)Best regards,Maliheh0 -
Thank you for your response,
I have another question. I really appreciate you for responding my question.
I have a constraint, and there is relationship between its indices, for example if i=1, then j=2, if i=2,then j=4. I wrote this constraint using "if" for each relationship(i.e. if i==1 and j==2), so for each relationship, I have to write this constraint. How can I indicate this relationship avoiding repetition of the constraint in my code?
Regards,
Arash
0 -
Hi Arash,
You can define two lists \(I\) and \(J\) being the list of \(i\) indices and their corresponding \(j\) indices. In your example, we would have \(I= [1, 2]\) and \(J = [2, 4]\), You can then use a for loop to implement your constraint for every pair of \((i,j)\) indices as below:
for i, j in zip(I, J):
# implement the constraintThis question and the previous one are more Python and programming questions, not Gurobi questions. Real Python website has so many great resources that can help you to find the answer to your Python questions. I would recommend checking it out.
Best regards,
Maliheh
0
Please sign in to leave a comment.
Comments
3 comments