问题
I'm working with pyomo and already have a model defined, with an objective function to go with it. After the model is solved, the objective function has certain parameters attached to it. So if i had a multi index variable [x1, x2, x3]
, my quadratic objective function would suppose look something like this: (x1^2 + 13*x2^2 + 10*x3^2) + (2*x1 +......)
.
My question is: given that i can actually access this expression in string format from the objective, is there any way to obtain the second derivative of this function with respect to all the variables?
回答1:
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.
来源:https://stackoverflow.com/questions/46570022/in-pyomo-how-can-one-extract-the-second-derivative-from-the-objective-function