関連情報

1階の導関数を用いる最適化

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

Keyword: 最適化,最小化

概要

本サンプルは1階の導関数を用いる最適化を行うPythonによるサンプルプログラムです。

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

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

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

Pythonコード


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

def py_obj_func(xc, fc, gc, comm):
    "The function whose zero is to be determined."
    from math import cos, sin
    # To communicate the function value and its derivative back we must treat
    # fc and gc as arrays of size 1:
    fc[0] = sin(xc) / xc
    gc[0] = (cos(xc) - fc[0]) / xc

def main():
    """
    Example for e04bbc using nag4py.
    Finds the minimum of sin(x) / x in the interval [3.5, 5].
    NAG Copyright 2015.

    >>> main()
    nag_opt_one_var_deriv (e04bbc) Python Example Script Results.
    Finding the minimum of sin(x) / x.
    Starting interval is [3.5, 5.0].
    Solution is within the interval [4.493409457909, 4.493409515792].
    Solution is 4.493409457909.
    At the solution the function value is -0.217233628211
      and the gradient is -3.76794086631e-16.
    There were 6 function evaluations required.
    """

    from nag4py.e04 import nag_opt_one_var_deriv
    from nag4py.util import Nag_Comm, NAG_E04BBC_FUN, noisy_fail
    from numpy import array, empty

    # Create the C callback function:
    c_func = NAG_E04BBC_FUN(py_obj_func)

    # Input data:
    e1 = 0.0
    e2 = 0.0
    max_fun = 30
    # Initial bounding interval, will be modified on exit:
    a = array(3.5)
    b = array(5.0)
    # Output data, initialized for the NAG call:
    x = empty(1)
    f = empty(1)
    g = empty(1)

    comm = Nag_Comm()
    fail = noisy_fail()

    print("nag_opt_one_var_deriv (e04bbc) Python Example Script Results.")
    print("Finding the minimum of sin(x) / x.")
    print("Starting interval is [%.1f, %.1f]." % (a, b))

    nag_opt_one_var_deriv(c_func, e1, e2, a, b, max_fun, x, f, g, comm, fail)

    print("Solution is within the interval [%.12f, %.12f]." % (a, b))
    print("Solution is %.12f." % x)
    print("At the solution the function value is %.12f" % f)
    print("  and the gradient is %.11e." % g)
    print("There were %i function evaluations required." % comm.nf)

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

出力結果


この出力例をダウンロード
nag_opt_one_var_deriv (e04bbc) Python Example Script Results.
Finding the minimum of sin(x) / x.
Starting interval is [3.5, 5.0].
Solution is within the interval [4.493409457909, 4.493409515792].
Solution is 4.493409457909.
At the solution the function value is -0.217233628211
  and the gradient is -3.76794086631e-16.
There were 6 function evaluations required.

ルーチンの仕様


In module nag4py.e04:

nag_opt_one_var_deriv(funct, e1, e2, a, b, maxfun, x, f, g, comm, fail)

    Minimizes a function of one variable, requires first derivatives.
    C Libraryの該当マニュアルページ
    
    TypeError will be raised when the following arguments are not instances
    of numpy.ndarray:
        a, b, x, f, g
    TypeError will be raised when the following arguments are not of data type
    numpy.float:
        a, b, x, f, g



Results matter. Trust NAG.

Privacy Policy | Trademarks