nAG数値計算ライブラリ
> 最適化アルゴリズムExample集
> 大規模非線形計画問題のFOAS法による省メモリ解法
大規模非線形計画法(FOAS法-省メモリ)
このExampleは、境界条件付きローゼンブロック問題を最小化するために、大規模非線形計画法(FOAS法-省メモリ)を使用しています。FileObjManagerクラスの使用方法を示すことが目的です。
目的関数:
タスク | 式 |
---|---|
minimize | \(f(x) = (1 - x_1)^2 + 100(x_2 - x_1^2)^2\) |
目的関数は、境界条件付きローゼンブロック関数であり、最小化することが目標です。
決定変数:
変数 | 範囲 |
---|---|
\(x_1\) | \(-1 \leq x_1 \leq 0.8\) |
\(x_2\) | \(-2 \leq x_2 \leq 2\) |
制約条件:
制約 | 式 |
---|---|
下限 | \(x_1 \geq -1, \quad x_2 \geq -2\) |
上限 | \(x_1 \leq 0.8, \quad x_2 \leq 2\) |
Exampleの実行コマンド:
python -m naginterfaces.library.examples.opt.handle_solve_bounds_foas_ex
ソースコード表示コマンド:
python -c "import inspect; from naginterfaces.library.examples.opt import handle_solve_bounds_foas_ex; print(''.join(inspect.getsourcelines(handle_solve_bounds_foas_ex)[0]))"
出力結果例:
naginterfaces.library.opt.handle_solve_bounds_foas Python Example Results.
Minimizing a bound-constrained Rosenbrock problem.
E04KF, First order method for bound-constrained problems
Begin of Options
End of Options
Status: converged, an optimal solution was found
Value of the objective 4.00000E-02
マニュアル:
handle_solve_bounds_foasのマニュアル
ソース:
#!/usr/bin/env python3
"""naginterfaces.library.opt.handle_solve_bounds_foas Python Example."""
# nAG Copyright 2018-2020.
# pylint: disable=invalid-name,line-too-long
from naginterfaces.base import utils
from naginterfaces.library import opt
def main():
"""
Example for :func:`naginterfaces.library.opt.handle_solve_bounds_foas`.
Large-scale first order active set bound-constrained nonlinear programming.
Demonstrates using the ``FileObjManager`` class.
>>> main()
naginterfaces.library.opt.handle_solve_bounds_foas Python Example Results.
Minimizing a bound-constrained Rosenbrock problem.
E04KF, First order method for bound-constrained problems
Begin of Options
...
End of Options
<BLANKLINE>
<BLANKLINE>
Status: converged, an optimal solution was found
Value of the objective 4.00000E-02
...
"""
print(
'naginterfaces.library.opt.handle_solve_bounds_foas '
'Python Example Results.'
)print('Minimizing a bound-constrained Rosenbrock problem.')
# The initial guess:
= [-1.5, 1.9]
x
# The Rosenbrock objective:
= lambda x, inform: (
objfun 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
# Create a handle for the problem:
= len(x)
nvar = opt.handle_init(nvar)
handle
# Define the bounds:
opt.handle_set_simplebounds(
handle,=[-1., -2.],
bl=[0.8, 2.],
bu
)
# Define the nonlinear objective:
=list(range(1, nvar+1)))
opt.handle_set_nlnobj(handle, idxfd
# Set some algorithmic options.
for option in [
'FOAS Print Frequency = 5',
'Print Solution = yes',
'Print Level = 1',
'Monitoring Level = 3',
]:
opt.handle_opt_set(handle, option)
# Use an explicit I/O manager for abbreviated iteration output:
= utils.FileObjManager(locus_in_output=False)
iom # Output from handle_solve_bounds_foas is sent
# to the nAG Library Engine advisory unit, which is associated with
# standard output by default in the I/O manager instance.
# To register a different file object to the advisory unit,
# use the I/O manager's register_to_advunit method.
# Solve the problem:
opt.handle_solve_bounds_foas(
handle, x,=objfun, objgrd=objgrd, io_manager=iom,
objfun
)
# Destroy the handle:
opt.handle_free(handle)
if __name__ == '__main__':
import doctest
import sys
sys.exit(
doctest.testmod(None, verbose=True, report=False,
=doctest.ELLIPSIS | doctest.REPORT_NDIFF,
optionflags
).failed )