简体   繁体   English

给定点的非线性pyomo约束的梯度

[英]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). 我(反复地)需要给定点的非线性pyomo约束con数值梯度信息(即,相应pyomo模型的变量都设置为特定值)。 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. DeveloperError:内部Pyomo实现错误:“在表达式> exp(x1)的运算符图中找不到对称表达式类型'exp'”请将此问题报告给Pyomo开发人员。

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? 所以,我的问题是:pyomo是否可以一般区分诸如指数函数/平方根之类的表达式,而我的示例只是不幸的巧合,可以很容易地解决吗? I will deal with various models from the MINLPLIB and some tool for differentiating the appearing expressions is crucial. 我将处理来自MINLPLIB的各种模型,并且一些区分出现的表情的工具至关重要。

This error existed through Pyomo 5.2 and was resolved in Pyomo 5.3. 这个错误在Pyomo 5.2中存在,在Pyomo 5.3中已解决。 Upgrading to 5.3 fixes the problem, and your example works fine (after adding from pyomo.core.base.symbolic import differentiate ). 升级到5.3可解决此问题,并且您的示例可以正常工作( from pyomo.core.base.symbolic import differentiate添加from pyomo.core.base.symbolic import differentiate )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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