In pyomo how can one extract the second derivative from the objective function

Deadly 提交于 2019-11-28 14:28:44

There are two ways to get derivative information in Pyomo.

If you need numeric derivatives at a single point, you can use a tool like the "gjh_asl_json" tool (https://github.com/ghackebeil/gjh_asl_json) that can take an NL file generated by Pyomo and produces a JSON file with the Jacobian and Hessian information.

If you want symbolic derivatives, Pyomo can provide those directly, provided you also have sympy installed:

from pyomo.core.base.symbolic import differentiate
from pyomo.core.base.expr import identify_variables
# assuming model.objective is your Objective component
varList = 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) ]

Of course, given that your expression is quadratic, symbolic and numeric differentiation will both give you the same answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!