Reputation: 51
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?
Upvotes: 4
Views: 2477
Reputation: 51
OK, I think I found the answer! I implement the piecewise linear function using a set of additional variables and constraints, exactly as in below: http://yetanothermathprogrammingconsultant.blogspot.com/2015/10/piecewise-linear-functions-in-mip-models.html
The only issue is the solving time that increased significantly, but the solution is runnable on GLPK (and I guess, any other solver).
Upvotes: 1