linearprogrammer
linearprogrammer

Reputation: 53

how to declare a non linear objective function in pyomo? and efficient way of declaring constraints?

I am trying to declare a non linear objective constraint in Pyomo and everytime I try to solve it using Bonmin Solver, I get the following error:

ERROR: Solver (asl) returned non-zero return code (3221225477) ERROR: Solver log: Bonmin 1.8.6 using Cbc 2.9.9 and Ipopt 3.12.8 bonmin: Traceback (most recent call last):

File "", line 2, in results = opt.solve(model)

File "C:\Anaconda\lib\site-packages\pyomo\opt\base\solvers.py", line 626, in solve "Solver (%s) did not exit normally" % self.name)

ApplicationError: Solver (asl) did not exit normally

#

My Objective function definition is:

`def obj_func(model):
   global summer
   summer = 0
   global volumer
   volumer = 0
   for i in range(0,len(data)):
        summer += model.x[i]*data.loc[i,'Predicted.Profit']
   for j in range(0,len(data)):
        volumer += model.x[j]*data.loc[j,'Predicted.Liters.Sold']
   return summer/volumer`

I am invoking the solver through SolverFactory, Lastly I would also like to know the most efficient way of creating constraints as i have constraints of the range 100+ and some of them are non linear. When i try to solve the problem using this model, my memory usage spikes to 100% and my computer hangs.

Upvotes: 0

Views: 2001

Answers (1)

Bethany Nicholson
Bethany Nicholson

Reputation: 2828

You should check the solver output to see why it's failing. You can print this output to the screen by adding the tee=True option when calling the solver:

SolverFactory('bonmin').solve(model, tee=True)

Also, when declaring Pyomo Constraints and Objectives, you should avoid using +=. It can often lead to significant performance degradation when building Pyomo expressions. Here is the recommended way to write your objective function:

def obj_func(model):
   return sum(model.x[i]*data.loc[i,'Predicted.Profit'] for i in model.I)/sum(model.x[j]*data.loc[j,'Predicted.Liters.Sold'] for j in model.J)
model.Objective(rule=_obj_func)

where model.I and model.J are either Pyomo Set components or Python lists.

Upvotes: 2

Related Questions