関連情報

制約なしの最適化

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

Keyword: 最適化,最小化,前処理付共役勾配アルゴリズム,CG法

概要

本サンプルは制約なしの最適化を行うPythonによるサンプルプログラムです。

本プログラムは以下の関数の最小化を行います。

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

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

Pythonコード


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

def py_obj_func(n, x, objf, g, comm):
    "The (two-dimensional) function to minimize."
    assert n == 2
    from math import exp
    ex1 = exp(x[0])
    # To communicate the function value back we must treat
    # objf as an array of size 1:
    objf[0] = ex1*(4*x[0]**2 + 2*x[1]**2 + 4*x[0]*x[1] + 2*x[1] + 1)
    # The gradient array must be assigned element-by-element:
    g[0] = 4*ex1*(2*x[0] + x[1]) + objf[0]
    g[1] = 2*ex1*(2*x[1] + 2*x[0] + 1)

def main():
    """
    Example for e04dgc using nag4py.
    A minimization problem solved using a quasi-Newton conjugate gradient method.
    NAG Copyright 2015.

    >>> main()
    nag_opt_conj_grad (e04dgc) Python Example Script Results.
    Minimizing using a quasi-Newton conjugate gradient method.
    Solution is (5.000e-01, -1.000e+00).
    Gradient at solution is (6.444e-07, 5.951e-07).
    Final function value is 2.67245456121e-14.
    """

    from nag4py.e04 import nag_opt_conj_grad, nag_opt_free, nag_opt_init
    from nag4py.util import (Nag_Comm, NAG_E04DGC_FUN, Nag_E04_Opt, Nag_FALSE,
                             Nag_NoCheck, Nag_NoPrint, NE_NOERROR, noisy_fail)
    from numpy import array, empty

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

    # Create the C callback function:
    c_obj_func = NAG_E04DGC_FUN(py_obj_func)

    # Input data:
    n = 2
    # Initial estimate of the solution, will be modified on exit:
    x = array([-1.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.verify_grad = Nag_NoCheck

    print("nag_opt_conj_grad (e04dgc) Python Example Script Results.")
    print("Minimizing using a quasi-Newton conjugate gradient method.")

    nag_opt_conj_grad(n, c_obj_func, x, objf, g, options, comm, fail)

    if fail.code == NE_NOERROR:
        print("Solution is (" + ", ".join("%.3e" % xi for xi in x) + ").")
        print("Gradient at solution is (" +
              ", ".join("%.3e" % gi for gi in g) + ").")
        print("Final function value is %.11e." % 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_conj_grad (e04dgc) Python Example Script Results.
Minimizing using a quasi-Newton conjugate gradient method.
Solution is (5.000e-01, -1.000e+00).
Gradient at solution is (6.444e-07, 5.951e-07).
Final function value is 2.67245456121e-14.

ルーチンの仕様


In module nag4py.e04:

nag_opt_conj_grad(n, objfun, x, objf, g, options, comm, fail)

    Unconstrained minimization using conjugate gradients.
    C Libraryの該当マニュアルページ
    
    TypeError will be raised when the following arguments are not instances
    of numpy.ndarray:
        x, objf, g
    TypeError will be raised when the following arguments are not of data type
    numpy.float:
        x, objf, g



Results matter. Trust NAG.

Privacy Policy | Trademarks