how to write piecewise linear objective function in Pyomo

Viewed 1501

I want to create a linear model in Pyomo that has piecewise linear function in its objective function. I managed to create the following code:

model = AbstractModel()
breakpoints = [-5,0,5]
values = [10,0, 10]
model.X = Var(bounds=(-5,5))
model.Y= Var(bounds=(0,10))

def pw(x):
    return x**2

model.Z = Piecewise(model.Y, model.X, pw_constr_type='EQ', pw_pts=[-5, 0, 5], f_rule=lambda model,x: pw(x))

model.obj = Objective(rule = lambda model: model.Y, sense=minimize)
instance = model.create_instance()
opt.solve(instance)

but it throws me an error: Solver does not support SOS level 2 constraints (I am using GLPK).

What I understood from Pyomo documentation so far, is that the piecewise functions are kind of constraints on related variables - while I am looking for linear approximation of quadratic cost function with explicitly given breakpoints in domain and slopes of function pieces (something like AMPL provides, for example). Therefore I don't actually need SOS2 constraints, but I didn't find any other solution except modelling it by binary variables (which I wouldn't like to utilize): http://winglpk.sourceforge.net/media/glpk-sos2_02.pdf

Any tips on that?

1 Answers
Related