Pyomo+GLPK: Resolving Quadratic Objective and Nonlinear Constraint erros

4,115 views
Skip to first unread message

BA

unread,
Nov 2, 2014, 1:03:54 PM11/2/14
to pyomo...@googlegroups.com
Hello

I am new to Pyomo+GLPK and have some questions regarding a price optimization model I’m trying to get off the ground. I purchased and reviewed the Pyomo Optimization modeling book but am still stumped.

The objective is to maximize revenue as defined as:

Pricew*Demandw + Pricesalvage*Inventoryfinal.

I want to optimize the former term on weekly basis and iteratively such that the optimal price for week,w, factors in the subsequent price/demand dynamics for subsequent weeks.  The summary the mathematical objective is:
Max  ΣPwλw + ΣPsalvgIfinal
Where: 
Pw = price in week, w;  
λw = demand in week, w;
Psalv = Salvage price
Ifinal = Final salvage inventory

Here is my model thus far:

model = AbstractModel()

*******Params and Vars *******************
# Number of weeks
model.Wks = RangeSet(1,12)  

# Salvage price
model.Psalv = Param(initialize=15)  

# Inventory at end of week, w
model.Inv_end = Var(model.Wks, within=NonNegativeIntegers, initialize=inv)

# Demand in week, w  
model.Wk_dmd = Var(model.Wks, within=NonNegativeIntegers) 

#Price in week , w
model.Pwk = Var(model.Wks, within=NonNegativeReals, initialize=5)

*******Objective*******************
def objective_rule(model):
    return sum((model.Wk_dmd[w]*model.Pwk[w] for w in model.Wks) + model.Psalv * model.Inv_end[12])
model.objective = Objective(rule=objective_rule, sense=maximize)

************Constraints**********************
def Dmd_dynamics_rule(model, w):
   ###Calcs demand for given week###
   if w == 1:
      return model.Wk_dmd[w] == inv*.15          
   else:
       # Main demand equation
      return model.Wk_dmd[w] == model.Wk_dmd[w-1]*.3+og_ct*.001+30*(200/age)+.01*inv+2000/model.Pwk[w]   #vars og_ct, age, and inv are global vats
model.Dmd_dynamics = Constraint(model.Wks,rule=Dmd_dynamics_rule)


def Inv_dmd_dynam_rule(model, w):
    ### Prev w End inv is ceiling to current w demand###
    if w > 1:
        return (model.Wk_dmd[w] <= model.Inv_end[w-1])
    return Constraint.Skip
model.Inv_dmd_dynam =Constraint(model.Wks, rule=Inv_dmd_dynam_rule)                                

def Price_dynam_rule(model, w):
     ###Price can not go up, only down###
     if w > 1:
          return model.Pcrrnt[w] <= model.Pcrrnt[w-1]
     return Constraint.Skip
model.Price_dynam = Constraint(model.Wks,rule=Price_dynam_rule)


def Calc_inv(model, w):
    ### Calculates Ending inventory in week, w ###
    if w >> 1:
          return model.Inv_end[w] == model.Inv_end[w-1] - model.Wk_dmd[w]
    return Constraint.Skip
model.Inv_end_dynamics = Constraint(model.Wks, rule=Calc_inv)


When I run the model I get the following 2 errors:
  1. ’ ValueError: Cannot write legal LP file.  Constraint 'Dmd_dynamics[2]' has a body with nonlinear terms.’
    • It appears this refers specifically to the 2000/model.Pcrrnt[w] term in the dmd_dynamics_rule fct
  2. 'Selected solver is unable to handle objective functions with quadratic terms. Objective at issue: objective.'
    • Self explanatory.

I believe from looking at some other resource iterative LP could be different implementation that resolve error 2 but unclear how to execute that in this environment.

Any guidance or recommendations on how to resolve either errors (or recs on another open source solver that can handle this model) would be much appreciated!

Watson, Jean-Paul

unread,
Nov 2, 2014, 3:14:59 PM11/2/14
to pyomo...@googlegroups.com
The problem with the dmd_dynamics[2] constraint is that it contains a non-linear term (1/x), which no linear solver is going to be able to handle. For that, you would need something like ipopt. 

Temporarily ignoring the issue above, commercial solvers such as cplex and gurobi can handle linear programs with quadratic objectives. This is not the case for glpk. 

jpw

--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Antonello Lobianco

unread,
Nov 2, 2014, 3:48:00 PM11/2/14
to pyomo...@googlegroups.com

Hello,
   as Watson said you need to use Pyomo with a solver that can handle quadratic functions. Ipopt would be a great free choise.
If you have some gams experience, this is a mini-tutorial to couple them:

https://lobianco.org/antonello/personal:blog:2014:0904_pyomo_for_gams_users

BA

unread,
Nov 2, 2014, 7:20:41 PM11/2/14
to pyomo...@googlegroups.com
Ah OK thanks very much for the reply.
Reply all
Reply to author
Forward
0 new messages