Source code for naginterfaces.library.examples.opt.handle_solve_bounds_foas_ex
#!/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
[docs]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:
x = [-1.5, 1.9]
# The Rosenbrock objective:
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
# Create a handle for the problem:
nvar = len(x)
handle = opt.handle_init(nvar)
# Define the bounds:
opt.handle_set_simplebounds(
handle,
bl=[-1., -2.],
bu=[0.8, 2.],
)
# Define the nonlinear objective:
opt.handle_set_nlnobj(handle, idxfd=list(range(1, nvar+1)))
# 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:
iom = utils.FileObjManager(locus_in_output=False)
# 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=objfun, objgrd=objgrd, io_manager=iom,
)
# Destroy the handle:
opt.handle_free(handle)
if __name__ == '__main__':
import doctest
import sys
sys.exit(
doctest.testmod(
None, verbose=True, report=False,
optionflags=doctest.ELLIPSIS | doctest.REPORT_NDIFF,
).failed
)