Skip to main content

How do I Add a constraint that a variable have to be multiple of 12?

Answered

Comments

2 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?.
  • Eli Towle
    • Gurobi Staff

    The modulo operation isn't directly supported. However, you can model this constraint by introducing an auxiliary integer variable \( \texttt{u1} \) and setting \( \texttt{F1} \) equal to 12 times \( \texttt{u1} \):

    u1 = m.addVar(vtype='I', name='u1')
    m.addConstr(F1 == 12*u1)

    Because \( \texttt{u1} \) is an integer variable, \( \texttt{F1} \) must be equal to some multiple of 12.

    By the way, you might find Model.addVars() and Model.addConstrs() useful for building the model more concisely. E.g.:

    F = m.addVars(5, name='F')
    u = m.addVars(5, name='u')
    m.addConstrs((F[i] == 12*u[i] for i in range(5)), name='modulo')
    0

Post is closed for comments.