【问题标题】:Problem trying to get the Gradient and Hessian in Pyomo尝试在 Pyomo 中获取梯度和 Hessian 的问题
【发布时间】:2018-11-07 18:16:53
【问题描述】:

我遇到了一个大规模的问题,我想知道目标函数的梯度和 Hessian 以及一些约束。我看到here 如何获得符号导数。

但是使用这个简单的代码:

> from pyomo.environ import * 
> mc = ConcreteModel()
> mc.X1 = Var() 
> mc.X2 = Var()
> mc.objectiv = Objective(expr = mc.X1**3 + mc.X2**2)
> from pyomo.core.base.symbolic import differentiate 
> from > pyomo.core.base.expr import identify_variables
> varList = list( identify_variables(mc.objectiv.expr) )
> firstDerivs = differentiate(mc.objectiv.expr, wrt_list=varList)
> secondDerivs = [ differentiate(firstDerivs[i], wrt=v) for i,v in enumerate(varList) ]

Pyomo 给我:

> firstDerivs 
[<pyomo.core.kernel.expr_coopr3._ProductExpression at 0x2bf06eada20>,  <pyomo.core.kernel.expr_coopr3._ProductExpression at 0x2bf06eada68>]
> secondDerivs
[<pyomo.core.kernel.expr_coopr3._ProductExpression at 0x2bf070b3af8>, 2.0]

我怎样才能得到符号方程并计算它们?

【问题讨论】:

    标签: python gradient pyomo hessian


    【解决方案1】:

    firstDerivssecondDerivs 是可迭代的,它们包含元素,它们是你的符号方程

    您可以使用以下方法查看方程式:

    [print(item) for item in firstDerivs]
    print(30*'-')
    [print(item) for item in secondDerivs]
    

    这将在其自己的行上打印出每个一阶导数,并以相同的方式打印出二阶导数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      相关资源
      最近更新 更多