pythonoptimizationcvxpypiecewiseconvex-optimization

Piecewise Linear Functions in CVXPY


I have a convex optimization problem with separable, convex, piecewise linear functions f_i(var_i) each defined by a list of points [(values, costs)] and a couple other terms that are also convex. I'm trying to figure out how two build those piecewise functions in CVXPY.

How do I take the below two lists of points and add them to a CVXPY objective as piecewise functions?

import cvxpy as cp
w = cp.Variable(n)

f1_points = [(-5, 10), (-2, -1), (0, 0)]  # -5 <= var1 <= 0  (Convex)
f2_points = [(-4, 5), (0, 0)]  # -4 <= var2 <= 0  (Linear)

f1_cost_function = ...
f2_cost_function = ...
constraints = [cp.sum(w) = 0] + ...
problem = cp.Problem(cp.Minimize(cp.sum([f1_cost_function, f2_cost_function] + ...)), constraints)

Solution

  • So this does not appear directly possible in CVXPY from the list of points. However if the piecewise functions are rewritten as point-slope functions instead of a collection of points, the cvxpy maximum function can be used for to make the piecewise linear function.

    f1_functions = [f1_line1, f1_line2, ...]
    f1 = cp.maximum(f1_functions)
    

    This is described with an example in the user guide.