Gurobi 12.0 onward
In Gurobi 12.0, one can add a nonlinear constraint to set one variable equal to a nonlinear expression. This nonlinear expression can contain a variety of nonlinear operations, including division. For example, to model \( z = x / y \) for a strictly positive \( y \) in Python:
x = model.addVar(name="x") y = model.addVar(lb=1, name="y") z = model.addVar(name="z") model.addGenConstrNL(z, x / y)
It's good practice to provide the tightest possible bounds on all variables participating in nonlinear constraints. For more programmatic examples of how to directly model nonlinear constraints in Gurobi, see the genconstrnl examples in the documentation.
Older Gurobi versions
Gurobi versions 11.0.3 and earlier do not support dividing by variables. However, one can model the expression \(\frac{1}{x}\) by introducing a continuous variable \( z \) and adding the constraint
$$x \cdot z = 1.$$
By the above constraint, \(z = \frac{1}{x}\). Wherever the expression \( \frac{1}{x} \) would appear in the model, the variable \( z \) can be used instead.
In order for the above formulation to work properly and avoid division by \( 0 \), one must provide appropriate bounds for both \(x\) and \(z\). For \(x \in [x^L, x^U]\) with \(x^L>0\), the lower and upper bounds for \(z\) should be \(\frac{1}{x^U}\) and \(\frac{1}{x^L}\), respectively.
Further information:
- Documentation: Nonlinear Constraints
- Documentation: genconstrl Examples
- What types of models can Gurobi solve?
- How do I model piecewise-linear functions?
- How do I model a piecewise-linear function of more than one variable?
- How do I model logical and general expressions?
- How do I model multilinear terms in Gurobi?
Comments
0 comments
Article is closed for comments.