Adding MVars in indicator function
AnsweredHi,
I would like to write the following formula, where permutations is a known matrix and assignments and unfair_assignments are variables
for i in range(n):
for j in range(m):
for k in range(kk):
m.addConstr((assignments[i, NUM_WEEKS * k + j] * permutations[i][j] >= 2) >> (unfair_assignments[i, NUM_WEEKS * k + j] == 1))
However, I am facing errors whenever I use MVars in Indicator constraints. Here is an example of one of them.
AttributeError: 'MLinExpr' object has no attribute 'size'
Kindly help me write the above constraint
0
-
Official comment
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?. -
Hi,
Could you try using Vars instead of MVars for \(\texttt{assignment}\) and \(\texttt{unfair_assignment}\) variables? The reason is that MVars are meant to be used in matrix operations and not scalar multiplication as in your case.
We're aware of this limitation and hope to make MVar objects compatible with general constraint functions in a future release.
Best regards,
Jaromił0 -
Hi,Gurobi 10.0 was recently released. Included in this release is the extension of Gurobi Matrix API which enables natural model building using matrix based expressions relying on NumPy concepts such as vectorization and broadcasting.The new capabilities of modelling classes MVar, MLinExpr, and MQuadExpr are not still fully integrated with general constraints. The general constraints should be added by indexing the Matrix API classes. See the snippet below:
m = gp.Model()
A = np.random.rand(3, 3)
b = np.random.rand(3, 2)
x = m.addMVar(shape=(3, 2), name="x")
y = m.addVars(3, 2, vtype=gp.GRB.BINARY, name="y")
for i in range(3):
for j in range(2):
m.addConstr((y[i, j] == 1) >> (A[i, :] @ x[:, j] <= b[i, j]))Checkout the Matrix-friendly Modeling with Gurobipy webinar if you would like to learn more about this new functionality.Please continue submitting new community posts for any bugs you might encounter in thefuture or for any comments/questions you might have. Users like you help tomake Gurobi better!Best regards,Maliheh0
Post is closed for comments.
Comments
3 comments