简体   繁体   English

在 Pyomo 中获取目标的梯度和 Hessian

[英]Get Gradient and Hessian of objective in Pyomo

I have a Pyomo model and I'd like to get the gradient and Hessian of the objective.我有一个 Pyomo 模型,我想获得目标的梯度和 Hessian。 A related SO question asks the same question.一个相关的SO 问题提出了同样的问题。 When I try the solution proposed there当我尝试那里提出的解决方案时

from pyomo.core.base.symbolic import differentiate
from pyomo.core.base.expr import identify_variables

varList = list(identify_variables(zipfe.loglikelihood.expr))
firstDerivs = differentiate(zipfe.loglikelihood.expr, wrt_list=varList)

I get the following error:我收到以下错误:

Traceback (most recent call last):
  File "/home/pauperei/.conda/envs/py36/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-9-6f2637b1fe13>", line 1, in <module>
    firstDerivs = differentiate(zipfe.loglikelihood.expr, wrt_list=varList)
  File "/home/pauperei/.conda/envs/py36/lib/python3.6/site-packages/pyomo/core/base/symbolic.py", line 122, in differentiate
    tmp_expr, locals=dict((str(x), x) for x in sympy_vars) )
  File "/home/pauperei/.conda/envs/py36/lib/python3.6/site-packages/sympy/core/sympify.py", line 354, in sympify
    expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
  File "/home/pauperei/.conda/envs/py36/lib/python3.6/site-packages/sympy/parsing/sympy_parser.py", line 894, in parse_expr
    return eval_expr(code, local_dict, global_dict)
  File "/home/pauperei/.conda/envs/py36/lib/python3.6/site-packages/sympy/parsing/sympy_parser.py", line 807, in eval_expr
    code, global_dict, local_dict)  # take local objects in preference
  File "<string>", line 1, in <module>
TypeError: 'Symbol' object does not support indexing

Here is how my objective looks like (first few lines):这是我的目标的样子(前几行):

zipfe.loglikelihood.pprint()
loglikelihood : Size=1, Index=None, Active=True
Key  : Active : Sense    : Expression
None :   True : minimize : log( 1 + exp( alpha1[0] + 2.0*alpha1[1] + alpha1[4] + 2.8986705607108596*( delta[0] + 2.0*delta[1] ) ) ) - ( 2.0*beta1[0] + beta1[3] + 2.8986705607108596*( gamma[0] + 2.0*gamma[1] ) ) + log( exp(  - log( 1 + exp( alpha1[0] + 2.0*alpha1[1] + alpha1[4] + 2.8986705607108596*( delta[0] + 2.0*delta[1] ) ) ) + 2.0*beta1[0] + beta1[3] + 2.8986705607108596*( gamma[0] + 2.0*gamma[1] ) ) + exp(  - log( 1 + exp( alpha1[0] + 5.0*alpha1[1] + 2.8986705607108596*( delta[0] + 2.0*delta[1] ) ) ) + 5.0*beta1[0] + 2.8986705607108596*( gamma[0] + 2.0*gamma[1] ) ) + exp(  - log( 1 + exp( alpha1[0] + 2.0*alpha1[1] + alpha1[7] + 2.8986705607108596*( delta[0] + 2.0*delta[1] ) ) ) + 2.0*beta1[0] + beta1[6] + 2.8986705607108596*( gamma[0] + 2.0*gamma[1] ) ) + exp(  - log( 1 + exp( alpha1[0] + alpha1[1] + alpha1[6] 

It seems that the problem is that Sympy doesn't like indexed variables like alpha1[0] .似乎问题在于 Sympy 不喜欢像alpha1[0]这样的索引变量。 Is there any workaround to this problem?这个问题有什么解决方法吗?

Edit:编辑:

I'm using Pyomo 5.2 and Python 3.6.我正在使用 Pyomo 5.2 和 Python 3.6。 I'll try to add a minimal working example soon.我会尽快尝试添加一个最小的工作示例。

In the last couple of days this has been aded as To Do in the Pyomo GitHub repository so hopefully there will be a solution soon.在过去的几天里,这已在 Pyomo GitHub 存储库中添加为待办事项,因此希望很快就会有解决方案。

To use indexed variables, use Indexed .要使用索引变量,请使用Indexed

>>> alpha1 = IndexedBase('alpha1')
>>> alpha1[0]
alpha1[0]

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

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