xpress.pwl
xpress.pwl |
Purpose
Returns a piecewise linear function over a variable.
Synopsis
xpress.pwl(dict)
Argument
dict
|
Python dictionary containing, as keys, two-elements tuples, and, as values, linear expressions in a variable. If the piecewise linear function has only constant values (i.e. it is a piecewise constant function), the independent variable can be specified with the key-value pair
None: x.
|
Example
The following example shows various usages of
xpress.pwl to model nonlinear functions as piecewise-linear functions :
x = xp.var() # Nonnegative variable y = xp.var(lb=-xp.infinity) # dependent variable, unrestricted t = xp.var() w = xp.var() p = xp.problem(x, y) # Sets a piecewise linear objective: a ramp function p.setObjective (xp.pwl({(-xp.infinity, -1): -2, (-1, 1): 2*x, (1, xp.infinity)]: 2})) p.addConstraint (t == xp.pwl({(1,2): 4*x, (2,4): 2, (4,5): -1})) # Piecewise CONSTANT function: add a key-value pair None: x to specify # independent variable p.addConstraint (t == xp.pwl({(1,2): 4, (2,4): 2, (4,5): -1, None: x})) p.addConstraint (xp.pwl({(-1,0): x, (0,1): 2*x, (1,10): 2}) <= xp.pwl({(0,10): 2*z, (10,20): z+2, (20,xp.infinity): 4}))
Further information
1. A piecewise linear function must use only one variable in all of the dictionary's values;
2. All values in the dictionary must be either constants or linear functions;
3. The intervals, specified as two-element tuples in the dictionary's keys, must be pairwise disjoint, i.e., they must not overlap.
4. Discontinuities in the function are allowed, i.e., one can declare a function as follows:
xp.pwl({(1, 2): 2*x + 4, (2,3): x - 1}), which is obviously discontinuous at 2. The value of the function if the optimal solution has
x=2 will be then either 8 or 1.
Related topics