The Python syntax could be like this:
model.addConstr(sum(x[i,j] for i in A for j in A if i != j) <= 10, "firstConstraint")
model.addConstrs(sum(x[i,j] for i in A if i != j) <= 10 for j in B, "secondConstraintClass")
model.addConstr(x[i,j] <= 10 for i in A for j in B if i != j, "thirdConstraintClass")
where model
is the gurobi API object in your code.
Note that addConstrs
is for the case when you add many similar constraints indexed by a set, while addConstr
is for when you add only one constraint at the time (note the for j in B
after the inequality and before the comma in the second constraint where addConstrs
is used).
You can check more about the addConstrs
method in its documentation, including some examples and use cases.
For sums over large arrays or linear products, or when adding a linear product where the coefficients could be sparse, I'd recommend reading about the quicksum and LinExpr methods which can yield further efficiency.