新しい最適化インターフェース: LP

nAG Library for Python Example集

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

新しい最適化インターフェース: LP

# インポート
from naginterfaces.library import opt
from naginterfaces.base import utils
import numpy as np

問題の段階的定義

以下の線形計画問題を定義したいと思います:
\[ \min c^Tx\\ l_A \leq Ax \leq u_A\\ x \geq 0 \]

すべてのモデル情報は、異なる(しかし単純な)関数呼び出しを通じてハンドルを介して内部のnAG構造に渡されます。配列引数は一般的なPythonデータ構造(タプル、リスト、numpy配列)と互換性があります。

  1. ハンドルの初期化から始めます:問題には5つの変数があります
# 問題のハンドルを作成する:
nvar = 5
handle = opt.handle_init(nvar)
  1. 変数に単純な境界を定義する: \(x \geq 0\)
# 以下のようにnumpy配列を使用して変数の境界を定義します:
bigbnd = 1.0e20
blx = np.empty(nvar)
bux = np.empty(nvar)
blx.fill(0.0)
bux.fill(bigbnd)
opt.handle_set_simplebounds(handle, bl=blx, bu=bux)
  1. 線形目的関数を定義する:\(f(x) = x_1 + x_2 + x_3 + x_4 + x_5\)
# 線形目的関数をPythonのタプルを使用して定義する 
c = (1.0, 1.0, 1.0, 1.0, 1.0)
opt.handle_set_linobj(handle, cvec=c)
  1. 線形制約を定義する: \(-1 \leq Ax \leq 1\)
# 4つのスパース線形制約を定義する
nclin = 4
bla = (-1.0, -1.0, -1.0, -1.0)
bua = (1.0, 1.0, 1.0, 1.0)
nnz = 12
icola = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5]
irowa = [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 2, 4]
a = np.array([1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.])
idlc = 0
k = opt.handle_set_linconstr(handle, bl=bla, bu=bua, irowb=irowa,
                             icolb=icola, b=a, idlc=idlc)

ソルバーを制御するためのオプションパラメータを設定できます

# オプション設定
# 出力の詳細レベルを設定する 
opt.handle_opt_set(handle, 'Print Level = 2')
# プリソルバーを無効にして、IPMに数回の反復を強制的に実行させる
# (問題はプリソルバーによって解決されます)
opt.handle_opt_set(handle, 'LP Presolve = No')
# 収束検出を厳密化する
opt.handle_opt_set(handle, 'LPIPM Stop Tolerance = 1.0e-12')

ハンドルがソルバーに渡される準備が整いました

# LP を解く
x = np.empty(nvar)
iom = utils.FileObjManager(locus_in_output=False)
x = opt.handle_solve_lp_ipm(handle, x=x, u=None, monit=None, io_manager=iom)
 ----------------------------------------------
  E04MT, Interior point method for LP problems
 ----------------------------------------------

 Begin of Options
     Print File                    =                   9     * d
     Print Level                   =                   2     * U
     Print Options                 =                 Yes     * d
     Print Solution                =                  No     * d
     Monitoring File               =                  -1     * d
     Monitoring Level              =                   4     * d
     Lpipm Monitor Frequency       =                   0     * d

     Infinite Bound Size           =         1.00000E+20     * d
     Task                          =            Minimize     * d
     Stats Time                    =                  No     * d

     Lp Presolve                   =                  No     * U
     Lpipm Algorithm               =         Primal-dual     * d
     Lpipm Centrality Correctors   =                   6     * d
     Lpipm Iteration Limit         =                 100     * d
     Lpipm Max Iterative Refinement=                   5     * d
     Lpipm Scaling                 =          Arithmetic     * d
     Lpipm Stop Tolerance          =         1.00000E-12     * U
     Lpipm Stop Tolerance 2        =         2.67452E-10     * d
     Lpipm System Formulation      =                Auto     * d
 End of Options

 Problem Statistics
   No of variables                  5
     free (unconstrained)           0
     bounded                        5
   No of lin. constraints           4
     nonzeroes                     12
   Objective function          Linear

 Presolved Problem Measures
   No of variables                  9
     free (unconstrained)           0
   No of lin. constraints           4
     nonzeroes                     16


 ------------------------------------------------------------------------------
  it|    pobj    |    dobj    |  optim  |  feas   |  compl  |   mu   | mcc | I
 ------------------------------------------------------------------------------
   0  2.95739E-01 -1.53220E-17  2.38E+00  1.58E-01  2.38E-01  2.9E-01
   1  3.82786E-02 -2.79688E-01  3.71E-02  6.98E-17  1.91E-02  2.0E-02   0
   2  1.00015E-02 -5.05058E-03  1.88E-03  1.29E-14  9.30E-04  9.4E-04   0
   3  1.78344E-05 -2.39954E-05  1.83E-16  1.48E-15  2.19E-06  2.2E-06   0
   4  8.91734E-09 -1.19978E-08  9.23E-17  2.78E-16  1.09E-09  1.1E-09   0
   5  4.45867E-12 -5.99890E-12  9.02E-17  5.83E-17  5.46E-13  5.5E-13   0
 ------------------------------------------------------------------------------
 Status: converged, an optimal solution found
 ------------------------------------------------------------------------------
 Final primal objective value         4.458673E-12
 Final dual objective value          -5.998898E-12
 Absolute primal infeasibility        2.543841E-16
 Relative primal infeasibility        5.826594E-17
 Absolute dual infeasibility          1.010318E-16
 Relative dual infeasibility          9.021771E-17
 Absolute complementarity gap         2.464120E-12
 Relative complementarity gap         5.464522E-13
 Iterations                                      5

終了時にハンドルデータをきれいに削除します:

# ハンドルを破壊する
opt.handle_free(handle)
関連情報
MENU
Privacy Policy  /  Trademarks