Skip to main content

How to define a list of n tuples, each tuple contains 2 variables

Answered

Comments

4 comments

  • Mario Ruthmair
    Gurobi Staff Gurobi Staff

    Hi,

    As you defined the names of your variables, I would suggest to use two variable dicts, e.g.,

    x = model.addVars(n, vtype=GRB.CONTINUOUS, name="x")
    y = model.addVars(n, vtype=GRB.CONTINUOUS, name="y")

    Would this be uncomfortable to use for you?

    Best regards,
    Mario

    1
  • Fei Wang
    Conversationalist
    First Question

    Hi Mario,

    Thanks for the answer, but I want to generalise it to  define a vector which represent a point in r dimensional space, If I use this approach, I would have to modify the code to define z if I want to define a point in R^3, instead of just changing r in the input. 

    The reason I want to extract a row from x is that I want to add some constraints regarding the distances between two points which is a second order cone constraint. 
    For example,  something like d @ d <= t @ t where d =  [x[i] - x[ j], y[i] - y[j], z[i] - y[j],...., ] (there are r component in d), I am not sure how to do this. Instead of x,y,z I should use x_1,x_2,....,x_r?  

    0
  • Mario Ruthmair
    Gurobi Staff Gurobi Staff

    It seems you could use the gurobipy Matrix API to handle your vars and constraints. Here is some material:

    Basically, you can add a matrix variable like this:

    x = model.addMVar((n,r), vtype=GRB.CONTINUOUS, name="x")

    Then, you can slice this matrix variable similar to how you would do it in numpy to build your constraints.

    1
  • Fei Wang
    Conversationalist
    First Question

    Thanks! That's the method I am looking for

    0

Please sign in to leave a comment.