首页 > 解决方案 > 用 pyomo 计算数值导数

问题描述

我需要在非线性 pyomo 约束的给定点重复计算导数。根据这篇文章,基本上有两种选择:一种符号方法(使用 sympy)和一种通过 Pyomo 生成的 NL 文件的数字方法。

我尝试了如下所示的符号方法:

def gradient(constr, variables):
    grad_num = np.array([value(partial) for partial in 
    differentiate(constr.body, wrt_list=variables)])
    if (not (is_leq_constr(constr))):
    grad_num = -grad_num
    return grad_num

原则上,这种方法是有效的。但是,在处理更大的问题时,函数调用需要很长时间,我希望数值方法更快。

这是真的?如果是这样,任何人都可以帮助我,将上述函数“翻译”为gjh_asl_json方法吗?我很感激任何提示。

标签: python-3.xmathematical-optimizationpyomo

解决方案


推荐阅读