関連情報

逐次2次計画最適化

Pythonによるサンプルソースコード
使用ルーチン名:nag_opt_nlp

Keyword: 逐次2次計画最適化

概要

本サンプルは逐次2次計画最適化を行うPythonによるサンプルプログラムです。

本サンプルは以下に示される非線形関数を最小化する解を求めて出力します。

※本サンプルはNAG Library for Pythonに含まれるルーチン nag_opt_nlp() のExampleコードです。

ご相談やお問い合わせはこちらまで

Pythonコード


このPythonコードをダウンロード
#!/usr/bin/env python
"nag_opt_nlp (e04ucc) Python Example Script."

def py_objfun(n, x, objf, g, comm):
    "The (four-dimensional) function to minimize."
    assert n == 4
    # To access the 'flag' member of the communication structure we must
    # 'dereference' comm by treating it as an array of size 1.
    # To communicate the function value back we must treat
    # objf in the same manner:
    if comm[0].flag in [0, 2]:
        objf[0] = x[0]*x[3]*(x[0] + x[1] + x[2]) + x[2]
    # The gradient array must be assigned element-by-element:
    if comm[0].flag == 2:
        g[0] = x[3]*(2*x[0] + x[1] + x[2])
        g[1] = x[0]*x[3]
        g[2] = x[0]*x[3] + 1.0
        g[3] = x[0]*(x[0] + x[1] + x[2])

def py_confun(n, ncnlin, needc, x, conf, conjac, comm):
    "The nonlinear constraints."
    # To access the 'flag' member of the communication structure we must
    # 'dereference' comm by treating it as an array of size 1, as in the
    # objective function.
    if needc[0] > 0:
        if comm[0].flag in [0, 2]:
            conf[0] = (x[0]**2 + x[1]**2 + x[2]**2 + x[3]**2)
        if comm[0].flag == 2:
            conjac[0] = x[0]*2
            conjac[1] = x[1]*2
            conjac[2] = x[2]*2
            conjac[3] = x[3]*2
    if needc[1] > 0:
        if comm[0].flag in [0, 2]:
            conf[1] = x[0]*x[1]*x[2]*x[3]
        if comm[0].flag == 2:
            conjac[4] = x[1]*x[2]*x[3]
            conjac[5] = x[0]*x[2]*x[3]
            conjac[6] = x[0]*x[1]*x[3]
            conjac[7] = x[0]*x[1]*x[2]

def main():
    """
    Example for e04ucc using nag4py.
    Solves minimization Problem 71 from Hock and Schittkowski's
    'Test Examples for Nonlinear Scriptming Codes'.
    NAG Copyright 2015.

    >>> main()
    nag_opt_nlp (e04ucc) Python Example Script Results.
    Minimizing Hock and Schittkowski Problem 71.
    Final objective value is 1.7014017e+01.
    """

    from nag4py.e04 import nag_opt_free, nag_opt_init, nag_opt_nlp
    from nag4py.util import (Nag_Comm, NAG_E04UCC_CONFUN, NAG_E04UCC_FUN,
                             Nag_E04_Opt, Nag_FALSE, Nag_NoCheck, Nag_NoPrint,
                             NE_NOERROR, noisy_fail)
    from numpy import array, empty, ones

    # Set up the options structure:
    options = Nag_E04_Opt()
    nag_opt_init(options)

    # Create the C callback functions:
    c_objfun = NAG_E04UCC_FUN(py_objfun)
    c_confun = NAG_E04UCC_CONFUN(py_confun)

    # Input data:
    n = 4
    nclin = 1
    ncnlin = 2
    tda = n
    a = ones((nclin, tda))
    bl = array([1.0, 1.0, 1.0, 1.0, -1.0E+25, -1.0E+25, 25.0])
    bu = array([5.0, 5.0, 5.0, 5.0, 20.0, 40.0, 1.0E+25])
    # Initial estimate of the solution, will be modified on exit:
    x = array([1.0, 5.0, 5.0, 1.0])
    # Output data, initialized for the NAG call:
    objf = empty(1)
    g = empty(n)

    comm = Nag_Comm()
    fail = noisy_fail()

    # Configure printing (off) in the Library routine:
    options.list = Nag_FALSE
    options.print_level = Nag_NoPrint
    options.minor_print_level = Nag_NoPrint
    options.verify_grad = Nag_NoCheck

    print("nag_opt_nlp (e04ucc) Python Example Script Results.")
    print("Minimizing Hock and Schittkowski Problem 71.")

    nag_opt_nlp(n, nclin, ncnlin, a, tda, bl, bu, c_objfun,
                c_confun, x, objf, g, options, comm, fail)

    if fail.code == NE_NOERROR:
        print("Final objective value is %.7e." % objf)

    # For compatibility with Python 3, string arguments must be byte strings:
    nag_opt_free(options, b"", fail)

if __name__ == '__main__':
    import sys
    from doctest import testmod
    main()
    sys.exit(testmod().failed)

出力結果


この出力例をダウンロード
nag_opt_nlp (e04ucc) Python Example Script Results.
Minimizing Hock and Schittkowski Problem 71.
Final objective value is 1.7014017e+01.

ルーチンの仕様


In module nag4py.e04:

nag_opt_nlp(n, nclin, ncnlin, a, tda, bl, bu, objfun, confun, x, objf, g, options, comm, fail)

    Minimization with nonlinear constraints using a sequential QP method.
    C Libraryの該当マニュアルページ
    
    TypeError will be raised when the following arguments are not instances
    of numpy.ndarray:
        a, bl, bu, x, objf, g
    TypeError will be raised when the following arguments are not of data type
    numpy.float:
        a, bl, bu, x, objf, g



Results matter. Trust NAG.

Privacy Policy | Trademarks