docplex.mp.pwl module

class docplex.mp.pwl.PwlFunction(model, pwl_def, name=None)[source]

Bases: docplex.mp.basic.ModelingObjectBase

This class models piecewise linear (PWL) functions. This class is not intended to be instantiated: piecewise linear functions are defined by invoking docplex.mp.model.Model.piecewise(), or docplex.mp.model.Model.piecewise_as_slopes().

Piecewise-linear functions are important in many applications. They are often specified either:

  • by giving a set of slopes, a set of breakpoints at which the slopes change, and the value of the functions at a given point, or
  • by giving an ordered list of (x,y) points that are linearly connected, along with the slope before the first point and the slope after the last point.

Note that a piecewise-linear function may be discontinuous.

add(arg)[source]

Adds an expression to self.

Note

This method does not create a new PWL function but modifies the self instance.

Parameters:arg – The expression to be added. Can be a PWL function or a number.
Returns:The modified self.
clone()[source]

Creates a copy of the PWL function on the same model.

Returns:The copy of the PWL function.
divide(arg)[source]

Divides this PWL function by a number.

Note

This method does not create a new function but modifies the self instance.

Parameters:arg – The number that is used to divide self.
Returns:The modified self.
evaluate(x_val)[source]

Evaluates the PWL function at the point whose x-coordinate is x_val.

Parameters:x_val – The x value for which we want to compute the value of the function.
Returns:The value of the PWL function at point x_val. A DOcplexException exception is raised when evaluating at a discontinuity of the PWL function.
multiply(arg)[source]

Multiplies this PWL function by a number.

Note

This method does not create a new function but modifies the self instance.

Parameters:arg – The number that is used to multiply self.
Returns:The modified self.
plot(lx=None, rx=None, k=1, **kwargs)[source]

This method displays the piecewise linear function using the matplotlib package, if found.

Parameters:
  • lx – The value to show the preslope (must be before the first breakpoint x value).
  • rx – The value to show the postslope (must be after the last breakpoint x value).
  • k – Scaling factor to calculate default values for rx and/or lx if these arguments are not provided, based on mean interval length between the x values of breakpoints.
  • kwargs – additional arguments to be passed to matplotlib plot() function
subtract(arg)[source]

Subtracts an expression from this PWL function.

Note

This method does not create a new function but modifies the self instance.

Parameters:arg – The expression to be subtracted. Can be either a PWL function, or a number.
Returns:The modified self.
translate(arg)[source]

Translate this PWL function by a number. This method creates a new PWL function instance for which all breakpoints have been moved along the horizontal axis by the amount specified by arg.

Parameters:arg – The number that is used to translate all breakpoints.
Returns:The translated PWL function.