How do I Add a constraint that a variable have to be multiple of 12?
回答済みIn my problem, y have capacities per hour that are multiple of 12, so the answers have to be multiples of 12 too.
I was planning to use something like this: m.addConstr(F1%12==0,name="c_147")
But I get this Error:
TypeError Traceback (most recent call last) <ipython-input-186-37db67e1659a> in <module> ----> 1 m.addConstr(F1%12==0,name="c_147") 2 m.addConstr(F2%12==0,name="c_148") 3 m.addConstr(F3%12==0,name="c_149") 4 m.addConstr(F4%12==0,name="c_150") 5 m.addConstr(F5%12==0,name="c_151") TypeError: unsupported operand type(s) for %: 'Var' and 'int'
Can you please help me?
0
-
正式なコメント
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?. -
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
投稿コメントは受け付けていません。
コメント
2件のコメント