简体   繁体   中英

Gradient of a nonlinear pyomo constraint at a given point

I (repeatedly) need numeric gradient information of a nonlinear pyomo constraint con at a given point (ie the variables of the corresponding pyomo model are all set to a specific value). I have read this post and decided that the (slightly modified) lines

from pyomo.core.base.symbolic import differentiate

var_list = list(model.component_objects(Var, active=True))
grad_num = [value(partial) for partial in differentiate(g_nu.body, wrt_list=vars)] 

should serve my purpose.

However, the example below already fails, presumably due to the appearance of the exponential function:

from pyomo.environ import *

model = ConcreteModel()
model.x_1 = Var()
model.x_2 = Var()
model.constr = Constraint(expr = 2*(model.x_1)**4+exp(model.x_2)<=3)
model.x_1.set_value(1)
model.x_2.set_value(1)
varList = list(model.component_objects(Var, active=True))
grad = [value(partial) for partial in differentiate(model.constr.body, wrt_list=varList)]

DeveloperError: Internal Pyomo implementation error: "sympy expression type 'exp' not found in the operator map for expression >exp(x1)" Please report this to the Pyomo Developers.

So, my question is: Can pyomo generally differentiate expressions like the exponential function/ square root etc. and is my example just an unfortunate coincidence which can be easily fixed? I will deal with various models from the MINLPLIB and some tool for differentiating the appearing expressions is crucial.

This error existed through Pyomo 5.2 and was resolved in Pyomo 5.3. Upgrading to 5.3 fixes the problem, and your example works fine (after adding from pyomo.core.base.symbolic import differentiate ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM