Skip to main content

Creating binary variables

Answered

Comments

6 comments

  • Eli Towle
    Gurobi Staff Gurobi Staff

    Hi Jose,

    Yes, setting the vtype argument of Model.addVar() to GRB.BINARY will make sure the newly created variable is binary. In Python, the full construction of a single constraint of this form would look like:

    import gurobipy as gp

    m = gp.Model()
    y = m.addVar(vtype=gp.GRB.BINARY, name="y")
    v = m.addVar(name="v")
    LB = 5

    m.addConstr(v >= LB*(1 - y))

    I hope this helps. Thanks!

    Eli

    0
  • Jose Vindel
    Gurobi-versary
    Thought Leader
    Investigator
    Hello, thanks for your help, I tried with the following syntax but I get this error, just for clarification reactions is a list and lb_reaction is a directory of reaction and its lower bound

    ---------------------------------------------------------------------------
    KeyError Traceback (most recent call last) <ipython-input-58-a7b59fc1a1ac> in <module> 12 13 ---> 14 m.addConstrs(v[j]>=LB[i]*(1-y[j]) for j in reactions for i in lb_reaction) 15 16 m.addConstrs(v[j]<=UB[i]*(1-y[j]) for j in reactions for i in ub_reaction) model.pxi in gurobipy.Model.addConstrs() <ipython-input-58-a7b59fc1a1ac> in <genexpr>(.0) 12 13 ---> 14 m.addConstrs(v[j]>=LB[i]*(1-y[j]) for j in reactions for i in lb_reaction) 15 16 m.addConstrs(v[j]<=UB[i]*(1-y[j]) for j in reactions for i in ub_reaction) KeyError: 'ACALD'
    0
  • Eli Towle
    Gurobi Staff Gurobi Staff

    Hi Jose,

    This error means that the key 'ACALD' was not defined for one of the variables or parameters used in that constraint family. Specifically, v['ACALD'], LB['ACALD'], or y['ACALD'] does not exist. Could you include the code that shows how you define the "reactions" and "lb_reaction" structures, as well as where you add the \( v \) and \( y \) variables to the model?

    Also, I'm not entirely sure this constraint is correct. It seems like the keys for the lb_reaction dictionary should precisely correspond to the list of reactions, and only one of these constraints should be added for each reaction. I.e., the constraint should instead be:

    $$\begin{align*} v_j &\geq LB_j (1 - y_j) \qquad \forall j \in N,\end{align*}$$

    where \( N \) is the set of reactions. Right now, you are adding \( |N|^2 \) of these constraints rather than \( |N| \), and most of them are redundant.

    Thanks!

    Eli

    0
  • Jose Vindel
    Gurobi-versary
    Thought Leader
    Investigator

    Hello Elli, 

    Thanks so much for your help, this is the code to get all the list and dictionaries for the model, 

    metabolites = df_metabolites[' abbreviation   '].tolist() #the list of metabolites for the variables

    reactions = df_reactions['abbreviation'].tolist() #The list of reactions for the variables

    UB = df_UB[0].tolist() #The list of the Upper Bounds

    LB = df_LB[0].tolist() #The list of the Lower Bounds

    matrixlist = []
    for n in range (95): # The Smatrix converted to a list
    reaction = df_smatrix[n].tolist()
    matrixlist.extend(reaction)

    reac_met = list(itertools.product(reactions,metabolites))
    sreact_met = dict(zip(reac_met,matrixlist))
    a = list()
    for ele in sreact_met.keys():
    a.append((ele[1], ele[0]))
    smatr = dict(zip(a,matrixlist)) # dictionary (metabolite,reaction:coefficient)

    lb_reaction = dict(zip(reactions,LB)) #This is the pair (reaction:bound)
    ub_reaction = dict(zip(reactions,UB))

    y = m.addVars(reactions, vtype=GRB.BINARY, name='y')
    v = m.addVars(metabolites, name='v')
    s = m.addVars(smatr, name='s')


    0
  • Eli Towle
    Gurobi Staff Gurobi Staff

    Hi Jose,

    I wonder if this error is caused by these two lines:

    v = m.addVars(metabolites, name='v')
    m.addConstrs(v[j]>=LB[i]*(1-y[j]) for j in reactions for i in lb_reaction)

    The \( v \) variables are defined over the set of metabolites. However, In the constraints, \( v \) is indexed by the set of reactions. So I think this is a matter of making sure the indices on the variables match the indices in the constraints, though I am not familiar enough with your application to know what needs to be changed.

    I hope this helps!

    Eli

    0
  • Jose Vindel
    Gurobi-versary
    Thought Leader
    Investigator

    Thank you so much for taking the time to answer, it sure helped me a lot, it turned out to be a matter of indexing

     

     

    0

Please sign in to leave a comment.