在Pyomo中有两种获取派生信息的方法。
如果您需要单点数值导数,则可以使用“
gjh_asl_json”工具(https://github.com/ghackebeil/gjh_asl_json)之类的工具,该工具可以获取由Pyomo生成的NL文件,并使用雅各布和黑森州的信息。
如果您需要符号派生,Pyomo可以直接提供它们,前提是您还
sympy安装了:
from pyomo.core.base.symbolic import differentiatefrom pyomo.core.base.expr import identify_variables# assuming model.objective is your Objective componentvarList = list( identify_variables(model.objective.expr) )firstDerivs = differentiate(model.objective.expr, wrt_list=varList)# Note this calculates d^2/dx_i^2; if you want the full Hessian matrix# ( delta^2/{delta x_i delta x_j} ) replace "wrt=v" with "wrt_list=varList"secondDerivs = [ differentiate(firstDerivs[i], wrt=v) for i,v in enumerate(varList) ]当然,假设您的表达式是二次方的,则符号和数字微分都将为您提供相同的答案。



