Different upper bound and lower bound for a list of variables
AnsweredHi,
I'm using m.addVars(Candidate, name="load") where Candidate is a tuple list, looks like:
( 1 , 1 )
( 1 , 2 )
( 1 , 3 )
( 2 , 1 )
( 2 , 2 )
( 2 , 3 )
( 3 , 1 )
...
However, I want to add different lb and ub to the variable based on what's in the tuple.
Such as:
if first_number_tuple > 2:
if second_number_tuple <2:
add.Var(lb=x,ub=y,"load")
However, if I add a single variable each time like this, my variable will loose it structure.
I want to keep the structure in the way of: m.addVars(Candidate, name="load").
How should I do that?
Thanks a lot!
-
I guess a more easier way is to change the upper bound and lower bound for the existing variables. How should I do that?
0 -
Hi Ruoyun,
You can use variable attributes to set the lower and upper bounds after you added the variables to your model.
An example would be
import gurobipy
from gurobipy import *
m = Model('test')
Candidate = [(1,1), (1,2), (1,3), (2,1), (2,2), (2,3)]
load = m.addVars(Candidate, name="load")
for c in Candidate:
if c[0] > 1: # if first number of the tuple is > 1
load[c].lb = -2 # some lower bound
if c[1] < 2: # if second number of the tuple is <2
load[c].ub = 3 # some upper bound
m.write("myLP.lp") # write LP file to check whether it workedBest regards,
Jaromił2
Please sign in to leave a comment.
Comments
2 comments