このページは、nAGライブラリのJupyterノートブックExampleの日本語翻訳版です。オリジナルのノートブックはインタラクティブに操作することができます。
新しい最適化インターフェース: LP
# インポート
from naginterfaces.library import opt
from naginterfaces.base import utils
import numpy as np
問題の段階的定義
以下の線形計画問題を定義したいと思います:すべてのモデル情報は、異なる(しかし単純な)関数呼び出しを通じてハンドルを介して内部のnAG構造に渡されます。配列引数は一般的なPythonデータ構造(タプル、リスト、numpy配列)と互換性があります。
- ハンドルの初期化から始めます:問題には5つの変数があります
# 問題のハンドルを作成する:
= 5
nvar = opt.handle_init(nvar) handle
- 変数に単純な境界を定義する: \(x \geq 0\)
# 以下のようにnumpy配列を使用して変数の境界を定義します:
= 1.0e20
bigbnd = np.empty(nvar)
blx = np.empty(nvar)
bux 0.0)
blx.fill(
bux.fill(bigbnd)=blx, bu=bux) opt.handle_set_simplebounds(handle, bl
- 線形目的関数を定義する:\(f(x) = x_1 + x_2 + x_3 + x_4 + x_5\)
# 線形目的関数をPythonのタプルを使用して定義する
= (1.0, 1.0, 1.0, 1.0, 1.0)
c =c) opt.handle_set_linobj(handle, cvec
- 線形制約を定義する: \(-1 \leq Ax \leq 1\)
# 4つのスパース線形制約を定義する
= 4
nclin = (-1.0, -1.0, -1.0, -1.0)
bla = (1.0, 1.0, 1.0, 1.0)
bua = 12
nnz = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5]
icola = [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 2, 4]
irowa = np.array([1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.])
a = 0
idlc = opt.handle_set_linconstr(handle, bl=bla, bu=bua, irowb=irowa,
k =icola, b=a, idlc=idlc) icolb
ソルバーを制御するためのオプションパラメータを設定できます
# オプション設定
# 出力の詳細レベルを設定する
'Print Level = 2')
opt.handle_opt_set(handle, # プリソルバーを無効にして、IPMに数回の反復を強制的に実行させる
# (問題はプリソルバーによって解決されます)
'LP Presolve = No')
opt.handle_opt_set(handle, # 収束検出を厳密化する
'LPIPM Stop Tolerance = 1.0e-12') opt.handle_opt_set(handle,
ハンドルがソルバーに渡される準備が整いました
# LP を解く
= np.empty(nvar)
x = utils.FileObjManager(locus_in_output=False)
iom = opt.handle_solve_lp_ipm(handle, x=x, u=None, monit=None, io_manager=iom) x
----------------------------------------------
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)