How can I decrease number of variables using m.addVars ul and lb

311 views
Skip to first unread message

Taner Cokyasar

unread,
May 17, 2017, 11:45:52 PM5/17/17
to Gurobi Optimization
Hello,

I am defining variables "x" with subscripts i = 1:3 and c = 1: 5  using m.addVars.

After defining i and c, I have the following:

x = m.addVars(i, c, obj = x, name="x")

This will generate x_11,     x_12,   ....,    x_15,  .....................................   x_31, x_22, ........, x_35

However, I know that for i=1,  I need only x_12 and x_13     
                                  for i=2, I need only x_23 and x_24  
                                  for i=3, I need only x_34 and x_35   others are redundant variables.

c_i should be a new list containing consecutive integer numbers:

c_i = P_i -1,    P_i

where P_i is another parameter for each i.

P_1 = 3 
P_2 = 4
P_3 = 5

c_i is giving me the pairs that I need to use in my variable generation. But, I don't know how I will create c_i (in tuples or?) and then create x variables based on it.

Any suggestions are appreciated.

Best regards,

Hervé Audren

unread,
May 18, 2017, 7:22:49 AM5/18/17
to Gurobi Optimization
I think the solution here is to only add the relevant variables. How about using something like:
P = [3, 4, 5]
x = [(m.addVar(name="x_{}{}".format(i+1,j-1)), m.addVar(name="c_{}{}".format(i+1,j))) for i,j  in enumerate(P)]

x is then a list of tuples:
x = [(x_12, x_13), (x_23, x_24), (x_34, x_35)]

Daniel Espinoza

unread,
May 18, 2017, 8:46:47 AM5/18/17
to Gurobi Optimization
You can also define the list of relevant tuples and use that list to define the variables

Taner Cokyasar

unread,
May 18, 2017, 12:24:37 PM5/18/17
to Gurobi Optimization
Thank you very much! It definitely makes sense. However, I am using dictionaries for all my parameters. So, my P is in a dictionary. When I run the code, naturally it says:

Cannot mix scalar and tuple values in a tuplelist

Is there any way to apply this logic when P is a dictionary? Besides, I defined another dictionary for "1" as "d". is it possible code format(i+d, j-d)....?

Thanks again!

Taner Cokyasar

unread,
May 18, 2017, 1:40:24 PM5/18/17
to Gurobi Optimization
Nevermind, I just changed everything to lists and using the code you provided. Works fine! I will have some more questions. If you may have time to assist me, I would highly appreciate Herve Audren!

Thanks,


On Thursday, May 18, 2017 at 7:22:49 AM UTC-4, Hervé Audren wrote:

Taner Cokyasar

unread,
May 18, 2017, 3:39:41 PM5/18/17
to Gurobi Optimization
Hervé ,

Now, I need to add constraints for the binary variables you helped me to create as:

x_12 + x_13 = 1
x_23 + x_23 = 1
x_34 + x_35 = 1

Do you have any suggestion how to add these constraints based on your code? I tried sum.x, but it says lists cannot be added.

On Thursday, May 18, 2017 at 7:22:49 AM UTC-4, Hervé Audren wrote:

Daniel Espinoza

unread,
May 19, 2017, 8:21:29 AM5/19/17
to Gurobi Optimization
Hi Taner,

tuplelist has a select(1,'*') method that is way more efficient than looping in all variables and then select the relevant ones


Best
Daniel

Taner Cokyasar

unread,
May 19, 2017, 8:52:59 AM5/19/17
to gur...@googlegroups.com
Hi Daniel,

Thank you for your advice. I actually read tuplelist function and couldn't get how I am gonna apply it in my case. In this second look, I kind of got how I need to do; basically I will create pairs that I want to produce. Then, how can I addvariables stored in that tuplelist? Also, how can I do summation when i=1  and j is as I specified earlier? If you may tell me the functions, I will definitely change all my bariable constuction to this way due to efficiency.

Thanks,

--

---
You received this message because you are subscribed to a topic in the Google Groups "Gurobi Optimization" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gurobi/sVKW0U1Voms/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gurobi+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Sent from Gmail Mobile

Edgar Alan Avila Gomez

unread,
May 19, 2017, 3:23:03 PM5/19/17
to gur...@googlegroups.com
Hello, you can do this model too easy using python or other lenguaje with Gurobi. 

x = {}
for i in range(3):
....j = i+1
....for n in range(2):
......x[i,j] = m.addvar(vtype=GRB.BINARY, name= "X_" + str(i+1) + "_" + str(j+1))
......j += 1

for i in range(3):
....m.addConstr(x[i,i+1] + x[i,i+2] == 1)

if you want to generalize the model, can use lists,  "for i in list"
For my point of view, it is more intuitive to use python + Gurobi

example = [m.addConstr(x[i,i+1] + x[i,i+2] == 1) for i in range(3)]

--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
ING. Edgar Alan Avila Gomez

Taner Cokyasar

unread,
May 19, 2017, 5:25:31 PM5/19/17
to Gurobi Optimization
Thank you for your respond, Edgar. I also have the following question that I posted on stackoverflow.com, but they couldn't really help me except for questioning my knowledge. If you guys can help me, I would really appreciate it.

Usually, variables are created with two complete indexes in Gurobi such as i=3 and j=2 yielding: x_11, x_12, x_21, x_22, x_31, x_32. However, I am trying to eliminate some redundant variables so as to decrease computation time. I have created the following variables with the following code:

from gurobipy import *
rangevalue = list(range(8,13))
I = 10
E = [6, 3, 4, 2, 4, 2, 5, 1, 4, 2]
m = Model("a")

for i in range(I):
    for k in range(len(rangevalue)):
        t = E[i]
        y[i+1, rangevalue[k] - t] = m.addVar(vtype=GRB.BINARY,
           name="y%s" % str([i+1, rangevalue[k] - t]))
m.update()
y

This yields following variables:

{(1, 2): <gurobi.Var y[1, 2]>,
 (1, 3): <gurobi.Var y[1, 3]>,
 (1, 4): <gurobi.Var y[1, 4]>,
 (1, 5): <gurobi.Var y[1, 5]>,
 (1, 6): <gurobi.Var y[1, 6]>,
 (2, 5): <gurobi.Var y[2, 5]>,
 (2, 6): <gurobi.Var y[2, 6]>,
 (2, 7): <gurobi.Var y[2, 7]>,
 (2, 8): <gurobi.Var y[2, 8]>,
 (2, 9): <gurobi.Var y[2, 9]>,
 (3, 4): <gurobi.Var y[3, 4]>,
 (3, 5): <gurobi.Var y[3, 5]>,
 (3, 6): <gurobi.Var y[3, 6]>,
 (3, 7): <gurobi.Var y[3, 7]>,
 (3, 8): <gurobi.Var y[3, 8]>,
 (4, 6): <gurobi.Var y[4, 6]>,
 (4, 7): <gurobi.Var y[4, 7]>,
 (4, 8): <gurobi.Var y[4, 8]>,
 (4, 9): <gurobi.Var y[4, 9]>,
 (4, 10): <gurobi.Var y[4, 10]>,
 (5, 4): <gurobi.Var y[5, 4]>,
 (5, 5): <gurobi.Var y[5, 5]>,
 (5, 6): <gurobi.Var y[5, 6]>,
 (5, 7): <gurobi.Var y[5, 7]>,
 (5, 8): <gurobi.Var y[5, 8]>,
 (6, 6): <gurobi.Var y[6, 6]>,
 (6, 7): <gurobi.Var y[6, 7]>,
 (6, 8): <gurobi.Var y[6, 8]>,
 (6, 9): <gurobi.Var y[6, 9]>,
 (6, 10): <gurobi.Var y[6, 10]>,
 (7, 3): <gurobi.Var y[7, 3]>,
 (7, 4): <gurobi.Var y[7, 4]>,
 (7, 5): <gurobi.Var y[7, 5]>,
 (7, 6): <gurobi.Var y[7, 6]>,
 (7, 7): <gurobi.Var y[7, 7]>,
 (8, 7): <gurobi.Var y[8, 7]>,
 (8, 8): <gurobi.Var y[8, 8]>,
 (8, 9): <gurobi.Var y[8, 9]>,
 (8, 10): <gurobi.Var y[8, 10]>,
 (8, 11): <gurobi.Var y[8, 11]>,
 (9, 4): <gurobi.Var y[9, 4]>,
 (9, 5): <gurobi.Var y[9, 5]>,
 (9, 6): <gurobi.Var y[9, 6]>,
 (9, 7): <gurobi.Var y[9, 7]>,
 (9, 8): <gurobi.Var y[9, 8]>,
 (10, 6): <gurobi.Var y[10, 6]>,
 (10, 7): <gurobi.Var y[10, 7]>,
 (10, 8): <gurobi.Var y[10, 8]>,
 (10, 9): <gurobi.Var y[10, 9]>,
 (10, 10): <gurobi.Var y[10, 10]>}

Now, I need to create the following constraint set:

y[1, 2] + y[1, 3] + y[1, 4] + y[1, 5] + y[1, 6] == 1
y[2, 5] + y[2, 6] + y[2, 7] + y[2, 8] + y[2, 9] == 1

For all values of variable i. My code directly works and shows what I am doing. If you may find a way to create these constraints either with addConstr and addConstrs functions, I would really appreciate it.


Edgar Alan Avila Gomez

unread,
May 19, 2017, 7:13:24 PM5/19/17
to gur...@googlegroups.com

for i in range(I):
m.addConstr(quicksum([y[i+1, rangevalue[k] - E[i]] for k in range(len(rangevalue))]) == 1)

or.

example = [[m.addConstr(quicksum([y[i+1, rangevalue[k] - E[i]] for k in range(len(rangevalue))]) == 1)
]for i in range(I)]

--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Taner Cokyasar

unread,
May 20, 2017, 6:29:18 AM5/20/17
to Gurobi Optimization
I really appreciate it, Edgar. Now, I got the logic of using for loops inside the addConstr function.

Best,
Taner
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages