Module docplex.cp.function¶
This module contains the objects representing CP Optimizer function expressions, Stepwise and Piecewise Linear functions:
In particular, it defines the following classes:
CpoFunction
: the root class of all function expressions,CpoSegmentedFunction
: for functions represented as a list of segments,CpoStepFunction
: for functions represented as a list of steps.
Detailed description¶

class
docplex.cp.function.
CpoFunction
(typ, s0=None, v0=None, x=None, v=None, s=None, name=None)[source]¶ Bases:
docplex.cp.expression.CpoExpr
Parent class for step and segmented functions.
Constructor
Parameters:  typ – Function type.
 s0 – Initial slope of the function.
 v0 – Initial value of the function.
 x – Abscissas (assumed to be sorted and all different).
 v – Values.
 s – Slopes.

add_slope
(x1, x2, v1, s)[source]¶ Adds a piecewise linear step on an interval.
Adds a piecewise linear step f(x) = v1 + s * (xx1) to the invoking function on interval [x1,x2).
Parameters:  x1 (int) – Start of the interval.
 x2 (int) – End of the interval.
 v1 (int or float) – Added value at x1.
 s (int or float) – Added function slope on interval [x1,x2);

add_value
(x1, x2, v)[source]¶ Adds a constant to the value of the function over an interval.
Adds constant value v to the value of the invoking function on the interval [x1,x2).
Parameters:  x1 (int) – Start of the interval.
 x2 (int) – End of the interval.
 v (int or float) – Added value.

get_value
(t)[source]¶ Gets the value of the function.
Gets the value of the function for an abscissa t. Complexity is in O(log n) for a function with n segments.
Parameters: t (int or float) – Abscissa value. Returns: Value of the function for abscissa t.

set_slope
(x1, x2, v1, s)[source]¶ Sets the value of the invoking function over an interval.
Sets the value of the invoking function on interval [x1,x2) to equal to f(x) = v1 + s * (xx1).
Parameters:  x1 (int) – Start of the interval.
 x2 (int) – End of the interval.
 v1 (int or float) – Function value at x1.
 s (int or float) – Function slope on interval [x1,x2).

set_value
(x1, x2, v)[source]¶ Sets the value of the function over an interval.
Sets the value of the invoking function to be constant and equal to v on the interval [x1,x2).
Parameters:  x1 (int) – Start of the interval (included).
 x2 (int) – End of the interval (excluded)
 v (int or float) – Function value.

class
docplex.cp.function.
CpoSegmentedFunction
(segment0=None, segments=None, name=None)[source]¶ Bases:
docplex.cp.function.CpoFunction
Class representing a segmented function.
In CP Optimizer, piecewise linear functions are typically used in modeling a known function of time, for instance the cost that is incurred for completing an activity after a known date.
A segmented function is a piecewise linear function defined on an interval [xmin, xmax) which is partitioned into segments such that over each segment, the function is linear.
When two consecutive segments of the function are colinear, these segments are merged so that the function is always represented with the minimal number of segments.
Constructor
Parameters:  segment0 (tuple) – Initial segment of the function (slope, vright).
 segments (list) – Segments of the function represented as a list of tuples (xleft, vleft, slope).

class
docplex.cp.function.
CpoStepFunction
(steps=None, name=None)[source]¶ Bases:
docplex.cp.function.CpoFunction
Class representing a step function.
In CP Optimizer, stepwise functions are typically used to model the efficiency of a resource over time.
A stepwise function is a special case of piecewise linear function where all slopes are equal to 0 and the domain and image of the function are integer.
When two consecutive steps of the function have the same value, these steps are merged so that the function is always represented with the minimal number of steps.
The function steps are expressed as a list of couples (x, val) specifying that the value of the function is val after x, up to the next step. By default, the value of the function is zero up to the first step. To change this default value, the first step should be (INTERVAL_MIN, value).
Constructor
Parameters:  steps – (Optional) Function steps, expressed as a list of couples (x, val).
 name – (Optional) Function name