関連情報

制約なしの2乗和の最適化

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

Keyword: 最適化,最小化,修正ニュートン法

概要

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

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

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

Pythonコード


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

def py_lsqfun(m, n, x, fvec, comm):
    "The (three-dimensional, fifteen-residual) least-squares function to minimize."
    assert m == 15 and n == 3

    # Unpack the user data in comm:
    from ctypes import cast, py_object
    userdata = cast(comm[0].p, py_object).value
    t1, t2, t3, y = userdata[0], userdata[1], userdata[2], userdata[3]

    for i in range(m):
        fvec[i] = x[0] + t1[i]/(x[1]*t2[i] + x[2]*t3[i]) - y[i]

def main():
    """
    Example for e04fcc using nag4py.
    Finds an unconstrained minimum of a sum of squares of m
    nonlinear functions in n variables.
    NAG Copyright 2015.

    >>> main()
    nag_opt_lsq_no_deriv (e04fcc) Python Example Script Results.
    Minimizing a sum of squares.
    Solution is (8.241e-02, 1.133e+00, 2.344e+00).
    Final sum of squares is 8.215e-03.
    Singular values of Jacobian at solution are (4.10e+00, 1.59e+00, 6.13e-02).
    """

    from nag4py.e04 import nag_opt_free, nag_opt_init, nag_opt_lsq_no_deriv
    from nag4py.util import (Nag_Comm, NAG_E04FCC_FUN, Nag_E04_Opt,
                             Nag_FALSE, Nag_NoPrint, NE_NOERROR,
                             noisy_fail, NW_COND_MIN, Pointer)
    from numpy import array, empty

    # The user data (observations):
    y = array([0.14, 0.18, 0.22, 0.25, 0.29, 0.32, 0.35,
               0.39, 0.37, 0.58, 0.73, 0.96, 1.34, 2.10, 4.39])
    t1 = array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,
                8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0])
    t2 = array([15.0, 14.0, 13.0, 12.0, 11.0, 10.0,
                9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0])
    t3 = array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0,
                7.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0])
    # Pack this into a Nag_Comm instance for communication to the least-squares
    # function:
    comm = Nag_Comm()
    from ctypes import cast
    userdata = t1, t2, t3, y
    comm.p = cast(id(userdata), Pointer)

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

    # Create the C callback function:
    c_lsqfun = NAG_E04FCC_FUN(py_lsqfun)

    # Input data:
    n = 3
    m = len(y)
    tdfjac = n
    # Initial estimate of the solution, will be modified on exit:
    x = array([0.5, 1.0, 1.5])
    # Output data, initialized for the NAG call:
    fsumsq = empty(1)
    fvec = empty(m)
    fjac = empty((m, tdfjac))

    fail = noisy_fail()

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

    print("nag_opt_lsq_no_deriv (e04fcc) Python Example Script Results.")
    print("Minimizing a sum of squares.")

    nag_opt_lsq_no_deriv(m, n, c_lsqfun, x, fsumsq, fvec, fjac, tdfjac, options,
                         comm, fail)

    if (fail.code in [NE_NOERROR,
                      NW_COND_MIN]):
        print("Solution is (" + ", ".join("%.3e" % xi for xi in x) + ").")
        print("Final sum of squares is %.3e." % fsumsq)
        # The members of the Nag_E04_Opt structure are C-like objects.
        # To access, for example, options.s, you must reference this field
        # as an explicit array slice (and not as options.s):
        print("Singular values of Jacobian at solution are (" +
              ", ".join("%.2e" % si for si in options.s[:n]) + ").")

    # 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_lsq_no_deriv (e04fcc) Python Example Script Results.
Minimizing a sum of squares.
Solution is (8.241e-02, 1.133e+00, 2.344e+00).
Final sum of squares is 8.215e-03.
Singular values of Jacobian at solution are (4.10e+00, 1.59e+00, 6.13e-02).

ルーチンの仕様


In module nag4py.e04:

nag_opt_lsq_no_deriv(m, n, lsqfun, x, fsumsq, fvec, fjac, tdfjac, options, comm, fail)

    Unconstrained nonlinear least squares (no derivatives required).
    C Libraryの該当マニュアルページ
    
    TypeError will be raised when the following arguments are not instances
    of numpy.ndarray:
        x, fsumsq, fvec, fjac
    TypeError will be raised when the following arguments are not of data type
    numpy.float:
        x, fsumsq, fvec, fjac



Results matter. Trust NAG.

Privacy Policy | Trademarks