为了账号安全,请及时绑定邮箱和手机立即绑定

优化解决时返回函数调用和其他信息

优化解决时返回函数调用和其他信息

翻阅古今 2022-12-20 12:26:29
我正在使用 Gekko 对多个系统的多个问题进行基准测试,我想让我的代码返回函数调用、迭代和解决所需的时间。我知道求解器会自动打印所有这些数据,但是是否有可以返回的对象或属性以允许我的函数返回数值?下面是如何设置代码的示例。def model(plot=False):    t = np.linspace(0, 1, 101)    m = GEKKO(remote=False); m.time=t    fe = m.Param(np.cos(2*np.pi*t)+3)    de = m.Var(fe[0])    e = m.CV(0); e.STATUS=1; e.SPHI=e.SPLO=0; e.WSPHI=1000; e.WSPLO=1    der = m.MV(0, lb=-1, ub=1); der.STATUS=1    m.Equations([de.dt() == der,  e == fe-de])    m.options.IMODE=6; m.solve()    if plot:        import matplotlib.pyplot as plt        plt.plot(t, fe)        plt.plot(t, de)        plt.plot(t, der)        plt.show()    return m.fcallsif __name__ == "__main__":    model(plot=True)
查看完整描述

1 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

目标函数迭代次数、求解时间求解状态在 Gekko 中可用:

  • m.options.OBJFCNVAL

  • m.options.ITERATIONS

  • m.options.SOLVETIME

  • m.options.APPSTATUS

您可以像我对summary.

from gekko import GEKKO

import numpy as np


def model(plot=False):

    t = np.linspace(0, 1, 101)

    m = GEKKO(remote=False); m.time=t


    fe = m.Param(np.cos(2*np.pi*t)+3)

    de = m.Var(fe[0])


    e = m.CV(0); e.STATUS=1; e.SPHI=e.SPLO=0; e.WSPHI=1000; e.WSPLO=1

    der = m.MV(0, lb=-1, ub=1); der.STATUS=1


    m.Equations([de.dt() == der,  e == fe-de])


    m.options.DIAGLEVEL=1

    m.options.SOLVER=1

    m.options.IMODE=6; m.solve()


    if plot:

        import matplotlib.pyplot as plt

        plt.plot(t, fe)

        plt.plot(t, de)

        plt.plot(t, der)

        plt.savefig('result.png')

    return [m.options.OBJFCNVAL,\

            m.options.ITERATIONS,\

            m.options.SOLVETIME,\

            m.options.APPSTATUS]



if __name__ == "__main__":

    summary = model(plot=True)

    print(summary)

如果你想要函数调用,那就稍微复杂一点,因为有不同类型的函数调用。有目标函数和约束的函数调用、一阶导数的函数调用和二阶导数的函数调用。m.options.DIAGLEVEL=1您可以通过设置或更高获得所有子程序调用以及每个子程序调用的个人和累积时间的完整报告。这是此问题的求解器输出:


 Number of state variables:    1900

 Number of total equations: -  1800

 Number of slack variables: -  0

 ---------------------------------------

 Degrees of freedom       :    100


 ----------------------------------------------

 Dynamic Control with APOPT Solver

 ----------------------------------------------


 Iter    Objective  Convergence

    0  9.81590E+01  1.00000E+00

    1  7.62224E+01  4.00000E-10

    2  7.62078E+01  1.10674E-02

    3  7.62078E+01  1.00000E-10

    4  7.62078E+01  8.32667E-17

    5  7.62078E+01  8.32667E-17

 Successful solution


 ---------------------------------------------------

 Solver         :  APOPT (v1.0)

 Solution time  :  0.5382 sec

 Objective      :  76.20778997271815

 Successful solution

 ---------------------------------------------------

某些求解器(如 IPOPT)没有可从 API 轻松获得的迭代,因此它们始终报告为零。对于 APOPT,摘要列表是[76.207789973, 5, 0.5253, 1]. 时序和函数调用报告在求解器摘要之后。


Timer #     1       0.70/       1 =       0.70 Total system time

Timer #     2       0.54/       1 =       0.54 Total solve time

Timer #     3       0.05/       9 =       0.01 Objective Calc: apm_p

Timer #     4       0.00/       5 =       0.00 Objective Grad: apm_g

Timer #     5       0.02/       9 =       0.00 Constraint Calc: apm_c

Timer #     6       0.00/       0 =       0.00 Sparsity: apm_s

Timer #     7       0.00/       0 =       0.00 1st Deriv #1: apm_a1

Timer #     8       0.00/       5 =       0.00 1st Deriv #2: apm_a2

Timer #     9       0.02/     200 =       0.00 Custom Init: apm_custom_init

Timer #    10       0.00/     200 =       0.00 Mode: apm_node_res::case 0

Timer #    11       0.00/     600 =       0.00 Mode: apm_node_res::case 1

Timer #    12       0.00/     200 =       0.00 Mode: apm_node_res::case 2

Timer #    13       0.00/     400 =       0.00 Mode: apm_node_res::case 3

Timer #    14       0.00/    4800 =       0.00 Mode: apm_node_res::case 4

Timer #    15       0.00/    2000 =       0.00 Mode: apm_node_res::case 5

Timer #    16       0.00/       0 =       0.00 Mode: apm_node_res::case 6

Timer #    17       0.00/       5 =       0.00 Base 1st Deriv: apm_jacobian

Timer #    18       0.02/       5 =       0.00 Base 1st Deriv: apm_condensed_jacobian

Timer #    19       0.00/       1 =       0.00 Non-zeros: apm_nnz

Timer #    20       0.00/       0 =       0.00 Count: Division by zero

Timer #    21       0.00/       0 =       0.00 Count: Argument of LOG10 negative

Timer #    22       0.00/       0 =       0.00 Count: Argument of LOG negative

Timer #    23       0.00/       0 =       0.00 Count: Argument of SQRT negative

Timer #    24       0.00/       0 =       0.00 Count: Argument of ASIN illegal

Timer #    25       0.00/       0 =       0.00 Count: Argument of ACOS illegal

Timer #    26       0.00/       1 =       0.00 Extract sparsity: apm_sparsity

Timer #    27       0.00/      17 =       0.00 Variable ordering: apm_var_order

Timer #    28       0.00/       1 =       0.00 Condensed sparsity

Timer #    29       0.00/       0 =       0.00 Hessian Non-zeros

Timer #    30       0.00/       3 =       0.00 Differentials

Timer #    31       0.00/       0 =       0.00 Hessian Calculation

Timer #    32       0.00/       0 =       0.00 Extract Hessian

Timer #    33       0.00/       1 =       0.00 Base 1st Deriv: apm_jac_order

Timer #    34       0.06/       1 =       0.06 Solver Setup

Timer #    35       0.40/       1 =       0.40 Solver Solution

Timer #    36       0.00/      23 =       0.00 Number of Variables

Timer #    37       0.00/      12 =       0.00 Number of Equations

Timer #    38       0.05/      17 =       0.00 File Read/Write

Timer #    39       0.00/       1 =       0.00 Dynamic Init A

Timer #    40       0.02/       1 =       0.02 Dynamic Init B

Timer #    41       0.02/       1 =       0.02 Dynamic Init C

Timer #    42       0.00/       1 =       0.00 Init: Read APM File

Timer #    43       0.00/       1 =       0.00 Init: Parse Constants

Timer #    44       0.00/       1 =       0.00 Init: Model Sizing

Timer #    45       0.00/       1 =       0.00 Init: Allocate Memory

Timer #    46       0.00/       1 =       0.00 Init: Parse Model

Timer #    47       0.00/       1 =       0.00 Init: Check for Duplicates

Timer #    48       0.00/       1 =       0.00 Init: Compile Equations

Timer #    49       0.00/       1 =       0.00 Init: Check Uninitialized

Timer #    50       0.00/     205 =       0.00 Evaluate Expression Once

Timer #    51       0.00/       0 =       0.00 Sensitivity Analysis: LU Factorization

Timer #    52       0.00/       0 =       0.00 Sensitivity Analysis: Gauss Elimination

Timer #    53       0.00/       0 =       0.00 Sensitivity Analysis: Total Time

计时器 3、4 和 5 可能与您的问题最相关。它们是目标函数请求、一阶导数请求和约束评估请求。



查看完整回答
反对 回复 2022-12-20
  • 1 回答
  • 0 关注
  • 73 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信