rhaskett
rhaskett

Reputation: 1962

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)

Upvotes: 1

Views: 2516

Answers (2)

moijn001
moijn001

Reputation: 21

If your curve is simple, like in this picture, and your objective function is to minimize y, then you can do this simply by putting constraints like this:

contraints = [y <= f1, y <= f2, y <= f3, y <= f4 ]
objective = cp.minimize(y)

Picture of simple Piece-wise functions for a non-linear curve

Upvotes: 1

rhaskett
rhaskett

Reputation: 1962

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.

Upvotes: 3

Related Questions