ロゼンブロック関数: 境界制約付き最適化

nAG Library for Python Example集

このページは、nAGライブラリのJupyterノートブックExampleの日本語翻訳版です。オリジナルのノートブックはインタラクティブに操作することができます。

ロゼンブロック関数: 境界制約付き最適化

一次の活性集合法による境界制約付き非線形計画法

2次元ロゼンブロック関数の例: このノートブックでは、FOASを使用して境界制約付き2次元ロゼンブロック関数を解く方法を示します。ソルバーが解点を見つけるまでの過程を示すプロットを生成します。

from naginterfaces.base import utils
from naginterfaces.library import opt
import numpy as np

目的関数、勾配、モニタリングコールバックを追加する

objfun = lambda x, inform: ((1. - x[0])**2 + 100.*(x[1] - x[0]**2)**2, inform)

def objgrd(x, fdx, inform):
    """The objective's gradient."""
    fdx[:] = [
        2.*x[0] - 400.*x[0]*(x[1]-x[0]**2) - 2.,
        200.*(x[1]-x[0]**2),
    ]
    return inform


steps = []
def monit(x, rinfo, _stats, _data=None):
    """The monitor function."""
    steps.append([x[0], x[1], rinfo[0]])

初期推定値を指定する

x = [-1., -1.5]

非線形目的関数を定義する(ハンドルに追加)

nvar = len(x)
handle = opt.handle_init(nvar)
opt.handle_set_nlnobj(handle, idxfd=list(range(1, nvar+1)))

変数xのボックス境界をハンドルに追加する

bl = [-1., -2.]
bu = [0.8, 2.]
opt.handle_set_simplebounds(
    handle,
    bl=bl,
    bu=bu,
)

アルゴリズムのオプションをいくつか設定する

for option in [
    'FOAS Print Frequency = 1',
    'Print Solution = yes',
    'FOAS Monitor Frequency = 1',
    'Print Level = 2',
    'Monitoring Level = 1',
]:
    opt.handle_opt_set(handle, option)

省略された反復出力のために明示的なI/Oマネージャーを使用する

iom = utils.FileObjManager(locus_in_output=False)

問題を解く

ret = opt.handle_solve_bounds_foas(handle, x, objfun=objfun, objgrd=objgrd, monit=monit, io_manager=iom)
steps.append([ret.x[0], ret.x[1], ret.rinfo[0]]) # Add last step
 ----------------------------------------------------------
  E04KF, First order method for bound-constrained problems
 ----------------------------------------------------------

 Begin of Options
     Print File                    =                   9     * d
     Print Level                   =                   2     * U
     Print Options                 =                 Yes     * d
     Print Solution                =                 All     * U
     Monitoring File               =                  -1     * d
     Monitoring Level              =                   1     * U
     Foas Monitor Frequency        =                   1     * U
     Foas Print Frequency          =                   1     * U

     Infinite Bound Size           =         1.00000E+20     * d
     Task                          =            Minimize     * d
     Stats Time                    =                  No     * d
     Time Limit                    =         1.00000E+06     * d
     Verify Derivatives            =                  No     * d

     Foas Estimate Derivatives     =                  No     * d
     Foas Finite Diff Interval     =         1.05367E-08     * d
     Foas Iteration Limit          =            10000000     * d
     Foas Memory                   =                  11     * d
     Foas Progress Tolerance       =         1.08158E-12     * d
     Foas Rel Stop Tolerance       =         1.08158E-12     * d
     Foas Restart Factor           =         6.00000E+00     * d
     Foas Slow Tolerance           =         1.01316E-02     * d
     Foas Stop Tolerance           =         1.00000E-06     * d
     Foas Tolerance Norm           =            Infinity     * d
 End of Options

 Problem Statistics
   No of variables                  2
     free (unconstrained)           0
     bounded                        2
   Objective function       Nonlinear


 -------------------------------------------------------------------------------
   iters |  objective |  optim  |   dir
 -------------------------------------------------------------------------------
        0  6.29000E+02  5.00E+02  3.50E+00
        1  6.29000E+02  5.00E+02  3.50E+00
        2  4.00000E+00  0.00E+00  1.80E+00
        3  4.00000E+00  0.00E+00  1.80E+00
        4  3.99156E+00  2.80E+00  2.80E+00
        5  3.99156E+00  2.80E+00  2.80E+00
        6  3.98433E+00  1.44E+00  1.44E+00
        7  3.97076E+00  5.76E+00  1.79E+00
        8  3.41157E+00  1.66E+01  1.60E+00
        9  3.15876E+00  2.07E+01  1.65E+00
       10  2.34744E+00  2.55E+00  2.29E+00
       11  2.06122E+00  5.09E+00  1.83E+00
       12  1.97065E+00  6.49E+00  1.88E+00
       13  1.77751E+00  9.58E+00  1.99E+00
       14  1.19453E+00  2.20E+00  8.93E-01
       15  1.12429E+00  2.33E+00  2.01E+00
       16  1.01998E+00  5.04E+00  2.02E+00
       17  8.94996E-01  8.97E+00  2.02E+00
       18  7.06235E-01  1.32E+00  1.11E+00
       19  5.06072E-01  5.09E+00  1.91E+00
 -------------------------------------------------------------------------------
   iters |  objective |  optim  |   dir
 -------------------------------------------------------------------------------
       20  3.18869E-01  9.51E-01  3.65E-01
       21  2.98131E-01  1.03E+00  1.03E+00
       22  2.48807E-01  2.90E+00  1.74E+00
       23  2.10033E-01  5.38E+00  1.65E+00
       24  1.19320E-01  1.40E+00  5.40E-01
       25  8.38051E-02  4.97E+00  1.77E+00
       26  6.45222E-02  8.65E-01  8.65E-01
       27  5.31881E-02  6.17E-01  6.17E-01
       28  4.20831E-02  7.71E-01  7.71E-01
       29  4.04842E-02  4.40E-01  4.40E-01
       30  4.04842E-02  4.40E-01  4.40E-01
       31  4.01532E-02  2.48E-01  2.48E-01
       32  4.01532E-02  2.48E-01  2.48E-01
       33  4.00000E-02  0.00E+00  0.00E+00
 -------------------------------------------------------------------------------
 Status: converged, an optimal solution was found
 -------------------------------------------------------------------------------
 Value of the objective             4.00000E-02
 Norm of inactive gradient          0.00000E+00
 Norm of projected direction        0.00000E+00
 Iterations                                  33
 Function evaluations                        80
 FD func. evaluations                         0
 Gradient evaluations                        40
   NPG function calls                        18
   NPG gradient calls                         3
   CG function calls                          9
   CG gradient calls                          5
   LCG function calls                        53
   LCG gradient calls                        32
 -------------------------------------------------------------------------------

 Primal variables:
   idx   Lower bound       Value       Upper bound
     1  -1.00000E+00    8.00000E-01    8.00000E-01
     2  -2.00000E+00    6.40000E-01    2.00000E+00

 Box bounds dual variables:
   idx   Lower bound       Value       Upper bound       Value
     1  -1.00000E+00    0.00000E+00    8.00000E-01    4.00000E-01
     2  -2.00000E+00    0.00000E+00    2.00000E+00    0.00000E+00

ラグランジュ乗数を取得する

from naginterfaces.base.opt import handle_set_get_real
mult = np.empty(2*nvar)
mult.fill(0.)
ret = handle_set_get_real(handle, "Dual Variables", 1, 2*nvar, mult)
print("Lagrange multipliers: ", mult[0:-1:2]-mult[1:ret+1:2])

ラグランジュ乗数: [-0.4 0. ]

ハンドルを破棄する

opt.handle_free(handle)

領域全体で関数を評価する

x_m = np.linspace(bl[0]-0.5, bu[0]+0.5, 101)
y_m = np.linspace(bl[1]-0.5, bu[1]+0.5, 101)
z_m = np.empty((101, 101))
j = y_m[0]
for i in range(0, 101):
    for j in range(0, 101):
        z_m[i, j], _inform = objfun([x_m[i], y_m[j]], 1)
nb = 25
x_box = np.linspace(bl[0], bu[0], nb)
y_box = np.linspace(bl[1], bu[1], nb)
box = np.array([np.concatenate([x_box, bu[0]*np.ones(nb), x_box[::-1], bl[0]*np.ones(nb)]),
      np.concatenate([bl[1]*np.ones(nb), y_box, bu[1]*np.ones(nb), y_box[::-1]])])
z_box = np.empty(box[0].shape)
for i in range(0, (box[0].size)):
    z_box[i], _inform = objfun([box[0][i], box[1][i]], 1)

X, Y = np.meshgrid(x_m, y_m, indexing='ij')

関数とその手順をプロット

# Jupyter の表示バックエンドを選択:
%matplotlib nbagg
steps = np.column_stack(steps)
import matplotlib.pyplot as plt
from matplotlib import cm
ax = plt.figure().add_subplot(projection='3d')
ax.grid(False)
ax.plot(box[0], box[1], z_box, 'k-', linewidth=1.5)
ax.plot([bl[0], bu[0], bu[0], bl[0], bl[0]], [bl[1], bl[1], bu[1], bu[1], bl[1]], -1.2*np.ones(5), 'k-')
ax.contour(X, Y, z_m, 15, offset=-1.2, cmap=cm.jet)
ax.plot_surface(X, Y, z_m, cmap=cm.jet, alpha=0.5)
ax.set_title('Rosenbrock Function')
ax.set_xlabel(r'$\mathit{x}$')
ax.set_ylabel(r'$\mathit{y}$')
ax.plot(steps[0], steps[1], steps[2], 'o-', color='red', markersize=3, linewidth=2)
ax.azim = 160
ax.elev = 35
plt.show()

nAG Library for Pythonの入手方法

nAG Library for Pythonは商用ライセンスのソフトウェアですが、このノートブックはBSD 3-Clause Licenseの下でライセンスされています * nAG Library for Pythonのインストール詳細についてはここをクリックしてください * nAG Library for Pythonのライセンス取得方法の詳細についてはここをクリックしてください

関連情報
MENU
© 譌・譛ャ繝九Η繝シ繝。繝ェ繧ォ繝ォ繧「繝ォ繧エ繝ェ繧コ繝�繧コ繧ー繝ォ繝シ繝玲�ェ蠑丈シ夂、セ 2025
Privacy Policy  /  Trademarks