関連情報

大域的最適化

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

Keyword: 大域的最適化,グローバル最適化

概要

本サンプルは大域的最適化を行うPythonによるサンプルプログラムです。

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

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

Pythonコード


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

def py_monit(n, ncall, xbest, icount, sdlist, inlist, numpts, initpt, nbaskt,
             xbaskt, boxl, boxu, nstate, comm, inform):
    "Dummy monitoring function."
    # To communicate a value of inform back we must treat
    # inform as an array of size 1:
    inform[0] = 0

def py_objfun(n, x, f, nstate, comm, inform):
    "'Peaks' (two-dimensional) objective function."
    assert n == 2
    from math import exp
    # To communicate values of inform and f back we must treat
    # them as arrays of size 1, as in the monitoring function:
    inform[0] = 0
    f[0] = (
        3*(1 - x[0])**2*exp(-x[0]**2 - (x[1] + 1)**2)
        - (10*(x[0]/5.0 - x[0]**3 - x[1]**5)*exp(-x[0]**2 - x[1]**2))
        - 1/3.0*exp(-(x[0] + 1)**2 - x[1]**2)
    )

def init_bounds(n):
    "Set the bounds mode and arrays."
    from nag4py.util import Nag_Bounds
    from numpy import ones
    bound = Nag_Bounds
    bl = -3.0*ones(n)
    bu = 3.0*ones(n)
    return bound, bl, bu

def init_list(n):
    "Set the initialization list and associated data."
    assert n == 2
    from nag4py.util import nag_int_type, Nag_UserSet
    from numpy import array, matrix
    initmethod = Nag_UserSet
    initlist = matrix([[-3.0, -1.0, 3.0],
                       [-3.0, 0.0, 3.0]])
    sdlist = initlist.shape[1]
    numpts = array([3, 3], dtype=nag_int_type)
    initpt = array([2, 2], dtype=nag_int_type)
    return initmethod, initlist, sdlist, numpts, initpt

def handle_options(state):
    "Set some optional algorithmic parameters."
    from nag4py.e05 import (nag_glopt_bnd_mcs_optset_char,
                            nag_glopt_bnd_mcs_optset_int,
                            nag_glopt_bnd_mcs_optset_real)
    from nag4py.util import noisy_fail
    fail = noisy_fail()
    # For compatibility with Python 3, string arguments must be byte strings:
    nag_glopt_bnd_mcs_optset_int(b"Function Evaluations Limit", 100000,
                                 state, fail)
    nag_glopt_bnd_mcs_optset_real(b"Local Searches Tolerance", 1.0e-10,
                                  state, fail)
    nag_glopt_bnd_mcs_optset_int(b"Splits Limit", 20, state, fail)
    nag_glopt_bnd_mcs_optset_int(b"Static Limit", 8, state, fail)
    nag_glopt_bnd_mcs_optset_int(b"Local Searches Limit", 40, state, fail)
    nag_glopt_bnd_mcs_optset_char(b"Local Searches", b"On", state, fail)
    return state

def main():
    """
    Example for e05jbc using nag4py.
    Minimizes the 'peaks' objective function.
    NAG Copyright 2015.

    >>> main()
    nag_glopt_bnd_mcs_solve (e05jbc) Python Example Script Results.
    Minimizing the 'peaks' function.
    Final objective value is -6.55113.
    Global optimum is (0.22828, -1.62553).
    """

    from nag4py.e05 import nag_glopt_bnd_mcs_init, nag_glopt_bnd_mcs_solve
    from nag4py.util import (Nag_Comm, NAG_E05JBC_MON, NAG_E05JBC_OBJFUN,
                             Nag_E05State, NE_NOERROR, noisy_fail)
    from numpy import empty

    # Set up the state structure:
    state = Nag_E05State()
    fail = noisy_fail()
    nag_glopt_bnd_mcs_init(0, state, fail)

    # Create the C callback functions:
    c_objfun = NAG_E05JBC_OBJFUN(py_objfun)
    c_monfun = NAG_E05JBC_MON(py_monit)

    # Input data:
    n = 2
    bound, bl, bu = init_bounds(n)
    initmethod, initlist, sdlist, numpts, initpt = init_list(n)
    handle_options(state)
    # Output data, initialized for the NAG call:
    obj = empty(1)
    x = empty(n)

    comm = Nag_Comm()

    print("nag_glopt_bnd_mcs_solve (e05jbc) Python Example Script Results.")
    print("Minimizing the 'peaks' function.")

    nag_glopt_bnd_mcs_solve(n, c_objfun, bound, initmethod,
                            bl, bu, sdlist, initlist, numpts, initpt,
                            c_monfun, x, obj, state, comm, fail)

    if fail.code == NE_NOERROR:
        print("Final objective value is %.5f." % obj)
        print("Global optimum is (" + ', '.join(['%.5f' % xi for xi in x]) + ").")

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

出力結果


この出力例をダウンロード
nag_glopt_bnd_mcs_solve (e05jbc) Python Example Script Results.
Minimizing the 'peaks' function.
Final objective value is -6.55113.
Global optimum is (0.22828, -1.62553).

ルーチンの仕様


In module nag4py.e05:

nag_glopt_bnd_mcs_solve(n, objfun, bound, initmethod, bl, bu, sdlist, _list, numpts, initpt, monit, x, obj, state, comm, fail)

    Global optimization by multi-level coordinate search, simple bounds,
    using function values only.
    C Libraryの該当マニュアルページ
    
    TypeError will be raised when the following arguments are not instances
    of numpy.ndarray:
        bl, bu, _list, numpts, initpt, x, obj
    TypeError will be raised when the following arguments are not of data type
    numpy.float:
        bl, bu, _list, x, obj
    TypeError will be raised when the following arguments are not of data type
    numpy.int32 or numpy.int64:
        numpts, initpt



Results matter. Trust NAG.

Privacy Policy | Trademarks