関連情報

関数値のみを用いた最適化

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

Keyword: 最適化,最小化

概要

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

本サンプルプログラムでは以下の関数の最適化を行います。

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

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

Pythonコード


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

def py_obj_func(xc, fc, comm):
    "The function to minimize."
    from math import sin
    # To communicate the function value back we must treat
    # fc as an array of size 1:
    fc[0] = sin(xc) / xc

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

    >>> main()
    nag_opt_one_var_no_deriv (e04abc) Python Example Script Results.
    Finding the minimum of sin(x) / x.
    Starting interval is [3.5, 5.0].
    Solution is within the interval [4.493409395932, 4.493409511697].
    Solution is 4.493409453814, where the function value is -0.217233628211.
    There were 10 function evaluations required.
    """

    from nag4py.e04 import nag_opt_one_var_no_deriv
    from nag4py.util import Nag_Comm, NAG_E04ABC_FUN, noisy_fail
    from numpy import array, empty

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

    # Input data:
    e1 = 0.0
    e2 = 0.0
    max_fun = 100
    # 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)

    comm = Nag_Comm()
    fail = noisy_fail()

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

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

    print("Solution is within the interval [%.12f, %.12f]." % (a, b))
    print("Solution is %.12f, where the function value is %.12f." % (x, f))
    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_no_deriv (e04abc) Python Example Script Results.
Finding the minimum of sin(x) / x.
Starting interval is [3.5, 5.0].
Solution is within the interval [4.493409395932, 4.493409511697].
Solution is 4.493409453814, where the function value is -0.217233628211.
There were 10 function evaluations required.

ルーチンの仕様


In module nag4py.e04:

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

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



Results matter. Trust NAG.

Privacy Policy | Trademarks