関連情報

混合整数非線形計画問題を解く

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

Keyword: 混合整数非線形計画問題を解く

概要

本サンプルは混合整数非線形計画問題を解くを行うPythonによるサンプルプログラムです。

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

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

Pythonコード


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

def py_confun(mode, ncnln, n, varcon, x, c, cjac, nstate, comm):
    "The constraint function for the optimization."

    if mode[0] == 0:

        # Unpack the user data in comm:
        from ctypes import cast, py_object
        userdata = cast(comm[0].p, py_object).value

        c[0] = 8.0*x[0] + 9.0*x[1] + 12.0*x[2] + 7.0*x[3] - userdata[1]
        c[1] = userdata[0] - x[4] - x[5] - x[6] - x[7]
    else:
        cjac[0] = 8.0
        cjac[1] = 0.0
        cjac[2] = 9.0
        cjac[3] = 0.0
        cjac[4] = 12.0
        cjac[5] = 0.0
        cjac[6] = 7.0
        cjac[7] = 0.0

def py_objfun(mode, n, varcon, x, objmip, objgrd, nstate, comm):
    "The objective function for the optimization."
    if mode[0] == 0:
        objmip[0] = (x[0]*(4.0*x[0]+3.0*x[1]-x[2]) +
                     x[1]*(3.0*x[0]+6.0*x[1]+x[2]) +
                     x[2]*(x[1]-x[0]+10.0*x[2]))
    else:
        objgrd[0] = 8.0*x[0] + 6.0*x[1] - 2.0*x[2]
        objgrd[1] = 6.0*x[0] + 12.0*x[1] + 2.0*x[2]
        objgrd[2] = 2.0*(x[1]-x[0]) + 20.0*x[2]
        objgrd[3] = 0.0

def main():
    """
    Example for h02dac using nag4py.
    Mixed integer nonlinear programming.
    NAG Copyright 2015.

    >>> main()
    nag_mip_sqp (h02dac) Python Example Program Results.
    Mixed integer nonlinear programming.
    Final estimate:
    x[0] =       0.3750
    x[1] =       0.0000
    x[2] =       0.5250
    x[3] =       0.1000
    x[4] =       1.0000
    x[5] =       0.0000
    x[6] =       1.0000
    x[7] =       1.0000
    Optimized value = 2.925.
    Node Selection = DEPTH FIRST.
    Requested accuracy of QP subproblems = 1e-10.
    """

    from ctypes import create_string_buffer
    from nag4py.h02 import (NAG_H02DAC_CONFUN, NAG_H02DAC_OBJFUN,
                            nag_mip_opt_get, nag_mip_opt_set, nag_mip_sqp)
    from nag4py.util import (Nag_Comm, nag_int_type, NE_NOERROR, noisy_fail,
                             Pointer)
    from numpy import array, empty, int32, zeros

    # Initialize the option-setting arrays.
    # Input data for h02zkc:
    liopts = 200
    lopts = 100
    # Output data, initialized for the NAG call:
    iopts = empty(liopts, dtype=nag_int_type)
    opts = empty(lopts)

    fail = noisy_fail()

    nag_mip_opt_set(b"Initialize = nag_mip_sqp", iopts, liopts, opts, lopts,
                    fail)

    if fail.code != NE_NOERROR:
        print("Error from nag_mip_opt_set (h02zkc).\n%s\n"% fail.message)
        return

    # Input data for h02dac:
    n = 8
    nclin = 5
    ncnln = 2
    pda = nclin
    a = zeros(pda*n, dtype=float)
    a[0] = 1.0
    a[5] = 1.0
    a[10] = 1.0
    a[15] = 1.0
    a[1] = -1.0
    a[21] = 1.0
    a[7] = -1.0
    a[27] = 1.0
    a[13] = -1.0
    a[33] = 1.0
    a[19] = -1.0
    a[39] = 1.0
    d = array([1, 0, 0, 0, 0], dtype=float)
    bl = zeros(8, dtype=float)
    bu = array([1.0e3, 1.0e3, 1.0e3, 1.0e3, 1, 1, 1, 1], dtype=float)
    varcon = array([0, 0, 0, 0, 1, 1, 1, 1, 3, 4, 4, 4, 4, 3, 4],
                   dtype=nag_int_type)
    maxit = 500
    acc = 1.0e-6
    # Starting point, will be modified on exit:
    x = array([1, 1, 1, 1, 0, 0, 0, 0], dtype=float)
    # Output data, initialized for the NAG call:
    ax = empty(nclin)
    c = empty(ncnln)
    cjac = empty(ncnln*n)
    objgrd = empty(n)
    objmip = empty(1)

    # Pack two values into a Nag_Comm instance for communication to the
    # constraint function:
    p = 3
    rho = 10.0
    comm = Nag_Comm()
    from ctypes import cast
    userdata = p, rho
    comm.p = cast(id(userdata), Pointer)

    fail = noisy_fail()

    print("nag_mip_sqp (h02dac) Python Example Program Results.")
    print("Mixed integer nonlinear programming.")

    nag_mip_sqp(n, nclin, ncnln, a, pda, d, ax, bl, bu, varcon, x,
                NAG_H02DAC_CONFUN(py_confun), c, cjac,
                NAG_H02DAC_OBJFUN(py_objfun), objgrd, maxit, acc, objmip,
                iopts, opts, comm, fail)

    if fail.code != NE_NOERROR:
        print("Error from nag_mip_sqp (h02dac).\n%s\n" % fail.message)
        return

    print("Final estimate:")
    for i in range(n):
        print("x[%1ld] = %12.4f" % (i, x[i]))

    print("Optimized value = %.3f." % objmip)

    # Query the selection strategy used by the solver, using h02zlc.
    # Input data for h02zlc:
    lcvalue = 40
    # Output data, initialized for the NAG call:
    ivalue = empty(1, dtype=nag_int_type)
    rvalue = empty(1, dtype=float)
    cvalue = create_string_buffer(lcvalue)
    optype = empty(1, dtype=int32)

    nag_mip_opt_get(b"Node Selection", ivalue, rvalue, cvalue, lcvalue, optype,
                    iopts, opts, fail)

    if fail.code != NE_NOERROR:
        print("Error from nag_mip_opt_get (h02zlc).\n%s\n", fail.message)
        return

    print("Node Selection = %s." % bytes.decode(cvalue.value))

    # Query the termination tolerance used by the solver.
    nag_mip_opt_get(b"QP Accuracy", ivalue, rvalue, cvalue, lcvalue, optype,
                    iopts, opts, fail)

    if fail.code != NE_NOERROR:
        print("Error from nag_mip_opt_get (h02zlc).\n%s\n", fail.message)
        return

    print("Requested accuracy of QP subproblems = %.4g." % rvalue)

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

出力結果


この出力例をダウンロード
nag_mip_sqp (h02dac) Python Example Program Results.
Mixed integer nonlinear programming.
Final estimate:
x[0] =       0.3750
x[1] =       0.0000
x[2] =       0.5250
x[3] =       0.1000
x[4] =       1.0000
x[5] =       0.0000
x[6] =       1.0000
x[7] =       1.0000
Optimized value = 2.925.
Node Selection = DEPTH FIRST.
Requested accuracy of QP subproblems = 1e-10.

ルーチンの仕様


In module nag4py.h02:

nag_mip_sqp(n, nclin, ncnln, a, pda, d, ax, bl, bu, varcon, x, confun, c, cjac, objfun, objgrd, maxit, acc, objmip, iopts, opts, comm, fail)

    Mixed integer nonlinear programming.
    C Libraryの該当マニュアルページ
    
    TypeError will be raised when the following arguments are not instances
    of numpy.ndarray:
        a, d, ax, bl, bu, varcon, x, c, cjac, objgrd, objmip, iopts, opts
    TypeError will be raised when the following arguments are not of data type
    numpy.float:
        a, d, ax, bl, bu, x, c, cjac, objgrd, objmip, opts
    TypeError will be raised when the following arguments are not of data type
    numpy.int32 or numpy.int64:
        varcon, iopts



Results matter. Trust NAG.

Privacy Policy | Trademarks