How to define a list of n tuples, each tuple contains 2 variables
AnsweredI want to define a list of n tuples, each tuple represents a point in a plane, so each tuple contains two variables, what gurobi API should I use? I am using Python
For example, if n = 5, then my variables are
(x1, y1), (x2, y2),(x3,y3),(x4,y4), (x5, y5)
I tried x = model.addVars(n, 2, vtype=GRB.CONTINUOUS, name="x")
But then I don't know how to extract a row from x
-
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,
Mario1 -
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 -
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 -
Thanks! That's the method I am looking for
0
Please sign in to leave a comment.
Comments
4 comments