There is a known issue that prevents users from using NumPy scalars (such as numpy.int32, numpy.int64, numpy.float32, or numpy.float64) on the left-hand side of a constraint. For example, the following code results in the error GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?):
import numpy as np
import gurobipy as gp
model = gp.Model()
x = model.addVar()
a = np.float64(5)
model.addConstr(a >= x) # error!
This error is a result of Python using the __ge__ (or __le__) method from the appropriate NumPy data class, which is not meant for constructing Gurobi constraint expressions.
Two possible workarounds are:
- Recast the NumPy data structure into a standard Python numeric type like int or float. In this case, the last line becomes model.addConstr(int(a) >= x).
- Rewrite the constraint so the left-hand side expression begins with a Var or LinExpr object. With this workaround, the last line becomes instead model.addConstr(x <= a).