猿问

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

我正在使用 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.fcalls



if __name__ == "__main__":

    model(plot=True)


翻阅古今
浏览 117回答 1
1回答

BIG阳

目标函数、迭代次数、求解时间和求解状态在 Gekko 中可用:m.options.OBJFCNVALm.options.ITERATIONSm.options.SOLVETIMEm.options.APPSTATUS您可以像我对summary.from gekko import GEKKOimport numpy as npdef 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 timeTimer #     2       0.54/       1 =       0.54 Total solve timeTimer #     3       0.05/       9 =       0.01 Objective Calc: apm_pTimer #     4       0.00/       5 =       0.00 Objective Grad: apm_gTimer #     5       0.02/       9 =       0.00 Constraint Calc: apm_cTimer #     6       0.00/       0 =       0.00 Sparsity: apm_sTimer #     7       0.00/       0 =       0.00 1st Deriv #1: apm_a1Timer #     8       0.00/       5 =       0.00 1st Deriv #2: apm_a2Timer #     9       0.02/     200 =       0.00 Custom Init: apm_custom_initTimer #    10       0.00/     200 =       0.00 Mode: apm_node_res::case 0Timer #    11       0.00/     600 =       0.00 Mode: apm_node_res::case 1Timer #    12       0.00/     200 =       0.00 Mode: apm_node_res::case 2Timer #    13       0.00/     400 =       0.00 Mode: apm_node_res::case 3Timer #    14       0.00/    4800 =       0.00 Mode: apm_node_res::case 4Timer #    15       0.00/    2000 =       0.00 Mode: apm_node_res::case 5Timer #    16       0.00/       0 =       0.00 Mode: apm_node_res::case 6Timer #    17       0.00/       5 =       0.00 Base 1st Deriv: apm_jacobianTimer #    18       0.02/       5 =       0.00 Base 1st Deriv: apm_condensed_jacobianTimer #    19       0.00/       1 =       0.00 Non-zeros: apm_nnzTimer #    20       0.00/       0 =       0.00 Count: Division by zeroTimer #    21       0.00/       0 =       0.00 Count: Argument of LOG10 negativeTimer #    22       0.00/       0 =       0.00 Count: Argument of LOG negativeTimer #    23       0.00/       0 =       0.00 Count: Argument of SQRT negativeTimer #    24       0.00/       0 =       0.00 Count: Argument of ASIN illegalTimer #    25       0.00/       0 =       0.00 Count: Argument of ACOS illegalTimer #    26       0.00/       1 =       0.00 Extract sparsity: apm_sparsityTimer #    27       0.00/      17 =       0.00 Variable ordering: apm_var_orderTimer #    28       0.00/       1 =       0.00 Condensed sparsityTimer #    29       0.00/       0 =       0.00 Hessian Non-zerosTimer #    30       0.00/       3 =       0.00 DifferentialsTimer #    31       0.00/       0 =       0.00 Hessian CalculationTimer #    32       0.00/       0 =       0.00 Extract HessianTimer #    33       0.00/       1 =       0.00 Base 1st Deriv: apm_jac_orderTimer #    34       0.06/       1 =       0.06 Solver SetupTimer #    35       0.40/       1 =       0.40 Solver SolutionTimer #    36       0.00/      23 =       0.00 Number of VariablesTimer #    37       0.00/      12 =       0.00 Number of EquationsTimer #    38       0.05/      17 =       0.00 File Read/WriteTimer #    39       0.00/       1 =       0.00 Dynamic Init ATimer #    40       0.02/       1 =       0.02 Dynamic Init BTimer #    41       0.02/       1 =       0.02 Dynamic Init CTimer #    42       0.00/       1 =       0.00 Init: Read APM FileTimer #    43       0.00/       1 =       0.00 Init: Parse ConstantsTimer #    44       0.00/       1 =       0.00 Init: Model SizingTimer #    45       0.00/       1 =       0.00 Init: Allocate MemoryTimer #    46       0.00/       1 =       0.00 Init: Parse ModelTimer #    47       0.00/       1 =       0.00 Init: Check for DuplicatesTimer #    48       0.00/       1 =       0.00 Init: Compile EquationsTimer #    49       0.00/       1 =       0.00 Init: Check UninitializedTimer #    50       0.00/     205 =       0.00 Evaluate Expression OnceTimer #    51       0.00/       0 =       0.00 Sensitivity Analysis: LU FactorizationTimer #    52       0.00/       0 =       0.00 Sensitivity Analysis: Gauss EliminationTimer #    53       0.00/       0 =       0.00 Sensitivity Analysis: Total Time计时器 3、4 和 5 可能与您的问题最相关。它们是目标函数请求、一阶导数请求和约束评估请求。
随时随地看视频慕课网APP

相关分类

Python
我要回答