Constraints
Linear, quadratic, and nonlinear constraints can be specified as follows:
constraint (constraint, body, lb, ub, sense, rhs, name)
The parameters are:
- constraint is the full-form constraint, such as x1 + 2 * x2 <= 4;
- body is the body of the constraint, such as 3 * x1 + x2 (it may contain constants);
- lb is the lower bound on the body of the constraint;
- ub is the upper bound on the body of the constraint;
- sense is the sense of the constraint, one among xpress.leq, xpress.geq, xpress.eq, and xpress.range; in the first three cases, the parameter rhs must be specified; only in the fourth case must lb and ub be specified;
- rhs is the right-hand side of the constraint;
- name is the name of the constraint. Parameters lb, ub, and rhs must be constant.
A constraint can be specified more naturally as a condition on an expression:
myconstr = x1 + x2 * (x2 + 1) <= 4 myconstr2 = xp.exp (xp.sin (x1)) + x2 * (x2**5 + 1) <= 4
One or more constraints (or vectors of constraints) can be added to a problem via the addConstraint method:
m.addConstraint (myconstr) m.addConstraint (v1 + xp.tan (v2) <= 3) m.addConstraint (x[i] + y[i] <= 2 for i in range (10))
In order to help formulate compact problems, the Sum operator of the xpress module can be used to express sums of expressions. Its argument is a list of expressions:
m.addConstraint (xp.Sum ([y[i] for i in range (10)]) <= 1) m.addConstraint (xp.Sum ([x[i]**5 for i in range (9)]) <= x[9])
When handling variables or expressions, it is advised to use the Sum operator in the Xpress module rather than the native Python operator, for reasons of efficiency.
As for variables, an object of type constraint allows for read/write access of its features via its members name, body, lb, and ub. The same caveat for variables holds here: any change to an object's members will only have an effect in the problems to which a constraint is added after the change.