Skip to main content

Adding a column to a parameter and rerun it for new column

Answered

Comments

3 comments

  • Maliheh Aramon
    Gurobi Staff Gurobi Staff
    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,
    Maliheh
    0
  • Arash Baharvandi
    Gurobi-versary
    Conversationalist
    First Question

    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
  • Maliheh Aramon
    Gurobi Staff Gurobi Staff

    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 constraint

    This 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.