library.opt
Submodule¶
Module Summary¶
Interfaces for the NAG Mark 26.2 opt Chapter.
opt
 Minimizing or Maximizing a Function
This module provides functions for solving various mathematical optimization problems by solvers based on local stopping criteria. The main classes of problems covered in this module are:
 Linear Programming (LP) – dense and sparse;
 Quadratic Programming (QP) – convex and nonconvex, dense and sparse;
 Nonlinear Programming (NLP) – dense and sparse, based on activeset SQP methods or interior point methods (IPM);
 Semidefinite Programming (SDP) – both linear matrix inequalities (LMI) and bilinear matrix inequalities (BMI);
 Derivativefree Optimization (DFO);
 Least Squares (LSQ), data fitting – linear and nonlinear, constrained and unconstrained.
For a full overview of the functionality offered in this module, see [the functionality index] or the Module Contents (submodule opt
).
See also other modules in the Library relevant to optimization:
 submodule
glopt
contains functions to solve global optimization problems;  submodule
mip
addresses problems arising in operational research and focuses on Mixed Integer Programming (MIP);  submodule
lapacklin
and submodulelapackeig
include functions for linear algebra and in particular unconstrained linear least squares;  submodule
fit
focuses on curve and surface fitting, in which linear data fitting in or norm might be of interest.
See Also¶
naginterfaces.library.examples.opt
: This subpackage contains examples for the
opt
module. See also the Examples subsection.
Functionality Index¶
Linear programming (LP)
dense
activeset method/primal simplex
alternative 1:
lp_solve()
alternative 2:
lsq_lincon_solve()
sparse
interior point method (IPM):
handle_solve_lp_ipm()
activeset method/primal simplex
recommended (see [E04 Introduction]):
qpconvex2_sparse_solve()
alternative:
qpconvex1_sparse_solve()
Quadratic programming (QP)
dense
activeset method for (possibly nonconvex) QP problem:
qp_dense_solve()
activeset method for convex QP problem:
lsq_lincon_solve()
sparse
activeset method sparse convex QP problem
recommended (see [E04 Introduction]):
qpconvex2_sparse_solve()
alternative:
qpconvex1_sparse_solve()
interior point method (IPM) for (possibly nonconvex) QP problems:
handle_solve_ipopt()
Nonlinear programming (NLP)
dense
activeset sequential quadratic programming (SQP)
direct communication
recommended (see [E04 Introduction]):
nlp1_solve()
alternative:
nlp2_solve()
reverse communication:
nlp1_rcomm()
sparse
interior point method (IPM):
handle_solve_ipopt()
activeset sequential quadratic programming (SQP)
recommended (see [E04 Introduction]):
nlp2_sparse_solve()
alternative:
nlp1_sparse_solve()
Nonlinear programming (NLP)  derivativefree optimization (DFO)
modelbased method for boundconstrained optimization:
bounds_bobyqa_func()
NelderMead simplex method for unconstrained optimization:
uncon_simplex()
Nonlinear programming (NLP)  special cases
unidimensional optimization (onedimensional) with bound constraints
method based on quadratic interpolation, no derivatives:
one_var_func()
method based on cubic interpolation:
one_var_deriv()
unconstrained
preconditioned conjugate gradient method:uncon_conjgrd_comp()
boundconstrained
quasiNewton algorithm, no derivatives:
bounds_quasi_func_easy()
quasiNewton algorithm, first derivatives:
bounds_quasi_deriv_easy()
modified Newton algorithm, first derivatives:
bounds_mod_deriv_comp()
modified Newton algorithm, first derivatives, easytouse:
bounds_mod_deriv_easy()
modified Newton algorithm, first and second derivatives:
bounds_mod_deriv2_comp()
modified Newton algorithm, first and second derivatives, easytouse:
bounds_mod_deriv2_easy()
Semidefinite programming (SDP)
generalized augmented Lagrangian method for SDP and SDP with bilinear matrix inequalities (BMISDP):handle_solve_pennon()
Linear least squares, linear regression, data fitting
constrained
boundconstrained least squares problem:
bnd_lin_lsq()
linearlyconstrained activeset method:
lsq_lincon_solve()
Nonlinear least squares, data fitting
unconstrained
combined GaussNewton and modified Newton algorithm
no derivatives:
lsq_uncon_mod_func_comp()
no derivatives, easytouse:
lsq_uncon_mod_func_easy()
first derivatives:
lsq_uncon_mod_deriv_comp()
first derivatives, easytouse:
lsq_uncon_mod_deriv_easy()
first and second derivatives:
lsq_uncon_mod_deriv2_comp()
first and second derivatives, easytouse:
lsq_uncon_mod_deriv2_easy()
combined GaussNewton and quasiNewton algorithm
first derivatives:
lsq_uncon_quasi_deriv_comp()
first derivatives, easytouse:
lsq_uncon_quasi_deriv_easy()
covariance matrix for nonlinear least squares problem (unconstrained):
lsq_uncon_covariance()
bound constrained
nonlinear constraints activeset sequential quadratic programming (SQP):
lsq_gencon_deriv()
modelbased derivativefree algorithm
direct communication:handle_solve_dfls()
NAG optimization modelling suite
initialization of a handle for the NAG optimization modelling suite:
handle_init()
define a linear objective function:
handle_set_linobj()
define a linear or a quadratic objective function:
handle_set_quadobj()
define a nonlinear least square objective function:
handle_set_nlnls()
define a nonlinear objective function:
handle_set_nlnobj()
define bounds of variables:
handle_set_simplebounds()
define a block of linear constraints:
handle_set_linconstr()
define a block of nonlinear constraints:
handle_set_nlnconstr()
define a structure of Hessian of the objective, constraints or the Lagrangian:
handle_set_nlnhess()
add one or more linear matrix inequality constraints:
handle_set_linmatineq()
define bilinear matrix terms:
handle_set_quadmatineq()
print information about a problem handle:
handle_print()
set/get information in a problem handle:
handle_set_get_real()
destroy the problem handle:
handle_free()
interior point method (IPM) for linear programming (LP):
handle_solve_lp_ipm()
interior point method (IPM) for nonlinear programming (NLP):
handle_solve_ipopt()
generalized augmented Lagrangian method for SDP and SDP with bilinear matrix inequalities (BMISDP):
handle_solve_pennon()
supply option values from a character string:
handle_opt_set()
get the setting of option:
handle_opt_get()
supply option values from external file:
handle_opt_set_file()
derivativefree optimisation for nonlinear least squares problems
direct communication:handle_solve_dfls()
Service functions
input and output (I/O)
read MPS data file defining LP, QP, MILP or MIQP problem:
miqp_mps_read()
write MPS data file defining LP, QP, MILP or MIQP problem:
miqp_mps_write()
read sparse SPDA data files for linear SDP problems:
sdp_read_sdpa()
read MPS data file defining LP or QP problem (deprecated):
qpconvex1_sparse_mps()
derivative check and approximation
check user’s function for calculating first derivatives of function:
check_deriv()
check user’s function for calculating second derivatives of function:
check_deriv2()
check user’s function for calculating Jacobian of first derivatives:
lsq_check_deriv()
check user’s function for calculating Hessian of a sum of squares:
lsq_check_hessian()
estimate (using numerical differentiation) gradient and/or Hessian of a function:
estimate_deriv()
determine the pattern of nonzeros in the Jacobian matrix for
nlp2_sparse_solve()
:nlp2_sparse_jacobian()
covariance matrix for nonlinear least squares problem (unconstrained):
lsq_uncon_covariance()
option setting functions
NAG optimization modelling suite
supply option values from a character string:
handle_opt_set()
get the setting of option:
handle_opt_get()
supply option values from external file:
handle_opt_set_file()
supply option values from external file:
uncon_conjgrd_option_file()
supply option values from a character string:
uncon_conjgrd_option_string()
supply option values from external file:
lp_option_file()
supply option values from a character string:
lp_option_string()
supply option values from external file:
lsq_lincon_option_file()
supply option values from a character string:
lsq_lincon_option_string()
supply option values from external file:
qp_dense_option_file()
supply option values from a character string:
qp_dense_option_string()
supply option values from external file:
qpconvex1_sparse_option_file()
supply option values from a character string:
qpconvex1_sparse_option_string()
initialization function:
qpconvex2_sparse_init()
supply option values from external file:
qpconvex2_sparse_option_file()
set a single option from a character string:
qpconvex2_sparse_option_string()
set a single option from an integer argument:
qpconvex2_sparse_option_integer_set()
set a single option from a real argument:
qpconvex2_sparse_option_double_set()
get the setting of an integer valued option:
qpconvex2_sparse_option_integer_get()
get the setting of a real valued option:
qpconvex2_sparse_option_double_get()
initialization function for
nlp1_solve()
andnlp1_rcomm()
:nlp1_init()
supply option values from external file:
nlp1_option_file()
supply option values from a character string:
nlp1_option_string()
supply option values from external file:
nlp1_sparse_option_file()
supply option values from a character string:
nlp1_sparse_option_string()
supply option values from external file:
lsq_gencon_deriv_option_file()
supply option values from a character string:
lsq_gencon_deriv_option_string()
initialization function:
nlp2_sparse_init()
supply option values from external file:
nlp2_sparse_option_file()
set a single option from a character string:
nlp2_sparse_option_string()
set a single option from an integer argument:
nlp2_sparse_option_integer_set()
set a single option from a real argument:
nlp2_sparse_option_double_set()
get the setting of an integer valued option:
nlp2_sparse_option_integer_get()
get the setting of a real valued option:
nlp2_sparse_option_double_get()
initialization function:
nlp2_init()
supply option values from external file:
nlp2_option_file()
set a single option from a character string:
nlp2_option_string()
set a single option from an integer argument:
nlp2_option_integer_set()
set a single option from a real argument:
nlp2_option_double_set()
get the setting of an integer valued option:
nlp2_option_integer_get()
get the setting of a real valued option:
nlp2_option_double_get()
For full information please refer to the NAG Library document
https://www.nag.com/numeric/fl/nagdoc_fl26.2/html/e04/e04intro.html

naginterfaces.library.opt.
bnd_lin_lsq
(a, b, bl, bu, itype=1, tol=0.0)[source]¶ Computes the least squares solution to a set of linear equations subject to fixed upper and lower bounds on the variables. An option is provided to return a minimal length solution if a solution is not unique.
For full information please refer to the NAG Library document for e04pc
https://www.nag.com/numeric/fl/nagdoc_fl26.2/html/e04/e04pcf.html
Parameters: a : float, arraylike, shape
The by matrix .
b : float, arraylike, shape
The righthand side vector .
bl : float, arraylike, shape
and must specify the lower and upper bounds, and respectively, to be imposed on the solution vector .
bu : float, arraylike, shape
and must specify the lower and upper bounds, and respectively, to be imposed on the solution vector .
itype : int, optional
Provides the choice of returning a regularized solution if the matrix is not of full rank.
Specifies that a regularized solution is to be computed.
Specifies that no regularization is to take place.
tol : float, optional
specifies a parameter used to determine the relative linear dependence of a column vector for a variable moved from its initial value. It determines the computational rank of the matrix. Increasing its value from will increase the likelihood of additional elements of being set to zero. It may be worth experimenting with increasing values of to determine whether the nature of the solution, , changes significantly. In practice a value of is recommended (see
machine.precision
).If on entry , is used.
Returns: a : float, ndarray, shape
If , this argument contains the product matrix , where is an by orthogonal matrix generated by
bnd_lin_lsq
; otherwise this argument is unchanged.b : float, ndarray, shape
If , the product of times the original vector , where is as described in argument ; otherwise this argument is unchanged.
x : float, ndarray, shape
The solution vector .
rnorm : float
The Euclidean norm of the residual vector .
nfree : int
Indicates the number of components of the solution vector that are not at one of the constraints.
w : float, ndarray, shape
Contains the dual solution vector. The magnitude of gives a measure of the improvement in the objective value if the corresponding bound were to be relaxed so that could take different values.
A value of equal to the special value is indicative of the matrix not having full rank.
It is only likely to occur when .
However a matrix may have less than full rank without being set to .
If , then the values contained in this argument (other than those set to ) may be unreliable; the corresponding values in may likewise be unreliable.
If you have any doubts set .
Otherwise the values of have the following meaning:
if is unconstrained.
if is constrained by its lower bound.
if is constrained by its upper bound.
may be any value if .
indx : int, ndarray, shape
The contents of this array describe the components of the solution vector as follows:
, for
These elements of the solution have not hit a constraint; i.e., .
, for
These elements of the solution have been constrained by either the lower or upper bound.
, for
These elements of the solution are fixed by the bounds; i.e., .
Here is determined from and the number of fixed components. (Often the latter will be , so will be .)
Raises: NagValueError
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, when , and .
Constraint: .
 (errno )
On entry, and .
Constraint: .
 (errno )
The function failed to converge in iterations. This is not expected. Please contact NAG.

naginterfaces.library.opt.
bounds_bobyqa_func
(objfun, npt, x, bl, bu, rhobeg, rhoend, maxcal, monfun=None, data=None)[source]¶ Bound constrained minimum, modelbased algorithm, using function values only.
For full information please refer to the NAG Library document for e04jc
https://www.nag.com/numeric/fl/nagdoc_fl26.2/html/e04/e04jcf.html
Parameters: objfun : callable f = objfun(x, data=None)
must evaluate the objective function at a specified vector .
 Parameters
 x : float, ndarray, shape
, the vector at which the objective function is to be evaluated.
 data : arbitrary, optional, modifiable in place
Usercommunication data for callback functions.
 Returns
 f : float
Must be set to the value of the objective function at .
npt : int
, the number of interpolation conditions imposed on the quadratic approximation at each iteration.
x : float, arraylike, shape
An estimate of the position of the minimum. If any component is outofbounds it is replaced internally by the bound it violates.
bl : float, arraylike, shape
The fixed vectors of lower bounds. To signify that a variable is unbounded you should choose a large scalar appropriate to your problem, then set the lower bound on that variable to . For wellscaled problems may be suitable, where denotes the largest positive model number (see
machine.real_largest
).bu : float, arraylike, shape
The fixed vectors of upper bounds. To signify that a variable is unbounded you should choose a large scalar appropriate to your problem, then set the upper bound on that variable to . For wellscaled problems may be suitable, where denotes the largest positive model number (see
machine.real_largest
).rhobeg : float
An initial lower bound on the value of the trust region radius.
rhoend : float
A final lower bound on the value of the trust region radius.
maxcal : int
The maximum permitted number of calls to .
monfun : None or callable monfun(nf, x, f, rho, data=None), optional
Note: if this argument is
None
then a NAGsupplied facility will be used.may be used to monitor the optimization process.
It is invoked every time a new trust region radius is chosen.
 Parameters
 nf : int
The cumulative number of calls made to .
 x : float, ndarray, shape
The current best point.
 f : float
The value of at .
 rho : float
A lower bound on the current trust region radius.
 data : arbitrary, optional, modifiable in place
Usercommunication data for callback functions.
data : arbitrary, optional
Usercommunication data for callback functions.
Returns: x : float, ndarray, shape
The lowest point found during the calculations. Thus, if no exception or warning is raised on exit, this argument is the position of the minimum.
f : float
The function value at the lowest point found ().
nf : int
Unless = 1 or
NagMemoryError
on exit, the total number of calls made to .Raises: NagValueError
 (errno )
There were unequal bounds and on entry.
Constraint: .
 (errno )
There were unequal bounds.
Constraint: .
 (errno )
On entry, , , and .
Constraint: if in coordinate , .
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, .
Constraint: , where , the machine precision.
 (errno )
On entry, and .
Constraint: .
 (errno )
On entry, .
Constraint: .
Warns: NagAlgorithmicMajorWarning
 (errno )
The function evaluations limit was reached: has been called times.
 (errno )
The predicted reduction in a trust region step was nonpositive. Check your specification of and whether the function needs rescaling. Try a different initial .
 (errno )
A rescue procedure has been called in order to correct damage from rounding errors when computing an update to a quadratic approximation of , but no further progess could be made. Check your specification of and whether the function needs rescaling. Try a different initial .
NagCallbackTerminateWarning
 (errno )
Usersupplied objective function requested termination.
 (errno )
Usersupplied monitoring function requested termination.

naginterfaces.library.opt.
bounds_mod_deriv2_comp
(funct, h, monit, ibound, bl, bu, x, lh, iprint=1, maxcal=None, eta=None, xtol=0.0, stepmx=100000.0, data=None, io_manager=None)[source]¶ Bound constrained minimum, modified Newton algorithm, using first and second derivatives (comprehensive).
For full information please refer to the NAG Library document for e04lb
https://www.nag.com/numeric/fl/nagdoc_fl26.2/html/e04/e04lbf.html
Parameters: funct : callable (fc, gc) = funct(xc, data=None)
must evaluate the function and its first derivatives at any point .
 Parameters
 xc : float, ndarray, shape
The point at which and the are required.
 data : arbitrary, optional, modifiable in place
Usercommunication data for callback functions.
 Returns
 fc : float
must set to the value of the objective function at the current point .
 gc : float, arraylike, shape
must set to the value of the first derivative at the point , for .
h : callable (fhesl, fhesd) = h(xc, lh, fhesd, data=None)
must calculate the second derivatives of at any point . (As with , there is the option of causing
bounds_mod_deriv2_comp
to terminate immediately.) Parameters
 xc : float, ndarray, shape
The point at which the second derivatives of are required.
 lh : int
The length of the array .
 fhesd : float, ndarray, shape
The value of at the point , for .
These values may be useful in the evaluation of the second derivatives.
 data : arbitrary, optional, modifiable in place
Usercommunication data for callback functions.
 Returns
 fhesl : float, arraylike, shape
must place the strict lower triangle of the second derivative matrix of (evaluated at the point ) in , stored by rows, i.e., set , for , for . (The upper triangle is not required because the matrix is symmetric.)
 fhesd : float, arraylike, shape
must place the diagonal elements of the second derivative matrix of (evaluated at the point ) in , i.e., set , .
monit : callable monit(xc, fc, gc, istate, gpjnrm, cond, posdef, niter, nf, data=None)
If , you must supply which is suitable for monitoring the minimization process. must not change the values of any of its arguments.
 Parameters
 xc : float, ndarray, shape
The coordinates of the current point .
 fc : float
The value of at the current point .
 gc : float, ndarray, shape
The value of at the current point , for .
 istate : int, ndarray, shape
Information about which variables are currently fixed on their bounds and which are free.
If is negative, is currently:
–
fixed on its upper bound if ;
–
fixed on its lower bound if ;
–
effectively a constant (i.e., ) if .
If is positive, its value gives the position of in the sequence of free variables.
 gpjnrm : float
The Euclidean norm of the projected gradient vector .
 cond : float
The ratio of the largest to the smallest elements of the diagonal factor of the projected Hessian matrix (see specification of ). This quantity is usually a good estimate of the condition number of the projected Hessian matrix. (If no variables are currently free, is set to zero.)
 posdef : bool
Is set or according to whether the second derivative matrix for the current subspace, , is positive definite or not.
 niter : int
The number of iterations (as outlined in [Description]) which have been performed by
bounds_mod_deriv2_comp
so far. nf : int
The number of times that has been called so far. Thus is the number of function and gradient evaluations made so far.
 data : arbitrary, optional, modifiable in place
Usercommunication data for callback functions.
ibound : int
Specifies whether the problem is unconstrained or bounded. If there are bounds on the variables, can be used to indicate whether the facility for dealing with bounds of special forms is to be used. It must be set to one of the following values:
If the variables are bounded and you are supplying all the and individually.
If the problem is unconstrained.
If the variables are bounded, but all the bounds are of the form .
If all the variables are bounded, and and .
If the problem is unconstrained. (The option is provided purely for consistency with other functions. In
bounds_mod_deriv2_comp
it produces the same effect as .)bl : float, arraylike, shape
The fixed lower bounds .
If is set to , you must set to , for . (If a lower bound is not specified for any , the corresponding should be set to a large negative number, e.g., .)
If is set to , you must set to ;
bounds_mod_deriv2_comp
will then set the remaining elements of equal to .If is set to , or , will be initialized by
bounds_mod_deriv2_comp
.bu : float, arraylike, shape
The fixed upper bounds .
If is set to , you must set to , for . (If an upper bound is not specified for any variable, the corresponding should be set to a large positive number, e.g., .)
If is set to , you must set to ;
bounds_mod_deriv2_comp
will then set the remaining elements of equal to .If is set to , or , will then be initialized by
bounds_mod_deriv2_comp
.x : float, arraylike, shape
must be set to a guess at the th component of the position of the minimum, for .
lh : int
The dimension of the array .
iprint : int, optional
The frequency with which is to be called.
is called once every iterations and just before exit from
bounds_mod_deriv2_comp
.is just called at the final point.
is not called at all.
should normally be set to a small positive number.
maxcal : int, optional
Note: if this argument is
None
then a default value will be used, determined as follows: .The maximum permitted number of evaluations of , i.e., the maximum permitted number of calls of .
eta : float, optional
Note: if this argument is
None
then a default value will be used, determined as follows: if : ; otherwise: .Every iteration of
bounds_mod_deriv2_comp
involves a linear minimization (i.e., minimization of with respect to ). specifies how accurately these linear minimizations are to be performed. The minimum with respect to will be located more accurately for small values of (say, ) than for large values (say, ).Although accurate linear minimizations will generally reduce the number of iterations of
bounds_mod_deriv2_comp
, this usually results in an increase in the number of function and gradient evaluations required for each iteration.On balance, it is usually more efficient to perform a low accuracy linear minimization.
xtol : float, optional
The accuracy in to which the solution is required.
If is the true value of at the minimum, then , the estimated position before a normal exit, is such that , where .
For example, if the elements of are not much larger than in modulus, and if is set to , then is usually accurate to about five decimal places. (For further details see [Accuracy].)
If the problem is scaled roughly as described in [Further Comments] and is the machine precision, then is probably the smallest reasonable choice for . (This is because, normally, to machine accuracy, where is any column of the identity matrix.)
If you set to (or any positive value less than ),
bounds_mod_deriv2_comp
will use instead of .stepmx : float, optional
An estimate of the Euclidean distance between the solution and the starting point supplied by you. (For maximum efficiency a slight overestimate is preferable.)
bounds_mod_deriv2_comp
will ensure that, for each iteration,where is the iteration number.
Thus, if the problem has more than one solution,
bounds_mod_deriv2_comp
is most likely to find the one nearest to the starting point.On difficult problems, a realistic choice can prevent the sequence of entering a region where the problem is illbehaved and can also help to avoid possible overflow in the evaluation of .
However, an underestimate of can lead to inefficiency.
data : arbitrary, optional
Usercommunication data for callback functions.
io_manager : FileObjManager, optional
Manager for I/O in this routine.
Returns: bl : float, ndarray, shape
The lower bounds actually used by
bounds_mod_deriv2_comp
, e.g., if , .bu : float, ndarray, shape
The upper bounds actually used by
bounds_mod_deriv2_comp
, e.g., if , .x : float, ndarray, shape
The final point . Thus, if no exception or warning is raised on exit, is the th component of the estimated position of the minimum.
hesl : float, ndarray, shape
During the determination of a direction (see [Description]), is decomposed into the product , where is a unit lower triangular matrix and is a diagonal matrix. (The matrices , , and are all of dimension , where is the number of variables free from their bounds. consists of those rows and columns of the full estimated second derivative matrix which relate to free variables. is chosen so that is positive definite.)
and are used to store the factors and .
The elements of the strict lower triangle of are stored row by row in the first positions of .
The diagonal elements of are stored in the first positions of .
In the last factorization before a normal exit, the matrix will be zero, so that and will contain, on exit, the factors of the final estimated second derivative matrix .
The elements of are useful for deciding whether to accept the results produced by
bounds_mod_deriv2_comp
(see [Accuracy]).hesd : float, ndarray, shape
During the determination of a direction (see [Description]), is decomposed into the product , where is a unit lower triangular matrix and is a diagonal matrix. (The matrices , , and are all of dimension , where is the number of variables free from their bounds. consists of those rows and columns of the full second derivative matrix which relate to free variables. is chosen so that is positive definite.)
and are used to store the factors and .
The elements of the strict lower triangle of are stored row by row in the first positions of .
The diagonal elements of are stored in the first positions of .
In the last factorization before a normal exit, the matrix will be zero, so that and will contain, on exit, the factors of the final second derivative matrix .
The elements of are useful for deciding whether to accept the result produced by
bounds_mod_deriv2_comp
(see [Accuracy]).istate : int, ndarray, shape
Information about which variables are currently on their bounds and which are free. If is:
 – equal to , is fixed on its upper bound;
 – equal to , is fixed on its lower bound;
 – equal to , is effectively a constant (i.e., );
 – positive, gives the position of in the sequence of free variables.
f : float
The function value at the final point given in .
g : float, ndarray, shape
The first derivative vector corresponding to the final point given in . The components of corresponding to free variables should normally be close to zero.
Raises: NagValueError
 (errno )
On entry, and for some .
 (errno )
On entry, and .
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, and .
Constraint: .
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, .
Constraint: .
Warns: NagAlgorithmicWarning
 (errno )
There have been function evaluations.
 (errno )
The conditions for a minimum have not all been satisfied, but a lower point could not be found.
 (errno )
No further progress can be made.
NagCallbackTerminateWarning
 (errno )
User requested termination by setting negative in or .
Notes
In the NAG C Library similar functionality can be accessed via
nag_opt_bounds_2nd_deriv
. However, this uses a different algorithmic base. Please contact NAG if you have any questions about compatibility.

naginterfaces.library.opt.
bounds_mod_deriv2_easy
(ibound, funct2, hess2, bl, bu, x, data=None)[source]¶ Bound constrained minimum, modified Newton algorithm, using first and second derivatives (easytouse).
For full information please refer to the NAG Library document for e04ly
https://www.nag.com/numeric/fl/nagdoc_fl26.2/html/e04/e04lyf.html
Parameters: ibound : int
Indicates whether the facility for dealing with bounds of special forms is to be used. It must be set to one of the following values:
If you are supplying all the and individually.
If there are no bounds on any .
If all the bounds are of the form .
If and .
funct2 : callable (fc, gc) = funct2(xc, data=None)
You must supply this function to calculate the values of the function and its first derivatives at any point .
It should be tested separately before being used in conjunction with
bounds_mod_deriv2_easy
. Parameters
 xc : float, ndarray, shape
The point at which the function and its derivatives are required.
 data : arbitrary, optional, modifiable in place
Usercommunication data for callback functions.
 Returns
 fc : float
The value of the function at the current point .
 gc : float, arraylike, shape
must be set to the value of the first derivative at the point , for .
hess2 : callable (heslc, hesdc) = hess2(xc, lh, data=None)
You must supply this function to evaluate the elements of the matrix of second derivatives of at any point .
It should be tested separately before being used in conjunction with
bounds_mod_deriv2_easy
. Parameters
 xc : float, ndarray, shape
The point at which the derivatives are required.
 lh : int
The length of the array .
 data : arbitrary, optional, modifiable in place
Usercommunication data for callback functions.
 Returns
 heslc : float, arraylike, shape
must place the strict lower triangle of the second derivative matrix in , stored by rows, i.e., set , for , for . (The upper triangle is not required because the matrix is symmetric.)
 hesdc : float, arraylike, shape
Must contain the diagonal elements of the second derivative matrix, i.e., set , for .
bl : float, arraylike, shape
The lower bounds .
If is set to , must be set to , for . (If a lower bound is not specified for any , the corresponding should be set to .)
If is set to , you must set to ;
bounds_mod_deriv2_easy
will then set the remaining elements of equal to .bu : float, arraylike, shape
The upper bounds .
If is set to , must be set to , for . (If an upper bound is not specified for any the corresponding should be set to .)
If is set to , you must set to ;
bounds_mod_deriv2_easy
will then set the remaining elements of equal to .x : float, arraylike, shape
must be set to a guess at the th component of the position of the minimum, for . The function checks the gradient and the Hessian matrix at the starting point, and is more likely to detect any error in your programming if the initial are nonzero and mutually distinct.
data : arbitrary, optional
Usercommunication data for callback functions.
Returns: bl : float, ndarray, shape
The lower bounds actually used by
bounds_mod_deriv2_easy
.bu : float, ndarray, shape
The upper bounds actually used by
bounds_mod_deriv2_easy
.x : float, ndarray, shape
The lowest point found during the calculations. Thus, if no exception or warning is raised on exit, is the th component of the position of the minimum.
f : float
The value of corresponding to the final point stored in .
g : float, ndarray, shape
The value of corresponding to the final point stored in , for ; the value of for variables not on a bound should normally be close to zero.
Raises: NagValueError
 (errno )
On entry, and for some .
 (errno )
On entry, and .
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, .
Constraint: .
 (errno )
There have been function evaluations.
 (errno )
The modulus of a variable has become very large. There may be a mistake in your supplied functions, your problem has no finite solution, or the problem needs rescaling.
Warns: NagAlgorithmicWarning
 (errno )
The conditions for a minimum have not all been satisfied, but a lower point could not be found.
 (errno )
It is probable that a local minimum has been found, but it cannot be guaranteed.
 (errno )
It is possible that a local minimum has been found, but it cannot be guaranteed.
 (errno )
It is unlikely that a local minimum has been found.
 (errno )
It is very unlikely that a local minimum has been found.
 (errno )
It is very likely that you have made an error forming the gradient.
 (errno )
It is very likely that you have made an error forming the 2nd derivatives.
Notes
No equivalent for this routine exists in the NAG C Library.

naginterfaces.library.opt.
bounds_mod_deriv_comp
(funct, monit, eta, ibound, bl, bu, x, lh, iprint=1, maxcal=None, xtol=0.0, delta=0.0, stepmx=100000.0, data=None, io_manager=None)[source]¶ Bound constrained minimum, modified Newton algorithm, using first derivatives (comprehensive).
For full information please refer to the NAG Library document for e04kd
https://www.nag.com/numeric/fl/nagdoc_fl26.2/html/e04/e04kdf.html
Parameters: funct : callable (fc, gc) = funct(iflag, xc, data=None)
must evaluate the function and its first derivatives at a specified point. (However, if you do not wish to calculate or its first derivatives at a particular , there is the option of setting an argument to cause
bounds_mod_deriv_comp
to terminate immediately.) Parameters
 iflag : int
Will have been set to or . The value indicates that only the first derivatives of need be supplied, and the value indicates that both itself and its first derivatives must be calculated.
 xc : float, ndarray, shape
The point at which the , or and the , are required.
 data : arbitrary, optional, modifiable in place
Usercommunication data for callback functions.
 Returns
 fc : float
Unless on entry, must set to the value of the objective function at the current point .
 gc : float, arraylike, shape
must set to the value of the first derivative at the point , for .
monit : callable monit(xc, fc, gc, istate, gpjnrm, cond, posdef, niter, nf, data=None)
If , you must supply which is suitable for monitoring the minimization process. must not change the values of any of its arguments.
 Parameters
 xc : float, ndarray, shape
The coordinates of the current point .
 fc : float
The value of at the current point .
 gc : float, ndarray, shape
The value of at the current point , for .
 istate : int, ndarray, shape
Information about which variables are currently fixed on their bounds and which are free.
If is negative, is currently:
–
fixed on its upper bound if
–
fixed on its lower bound if
–
effectively a constant (i.e., ) if
If is positive, its value gives the position of in the sequence of free variables.
 gpjnrm : float
The Euclidean norm of the current projected gradient vector .
 cond : float
The ratio of the largest to the smallest elements of the diagonal factor of the approximated projected Hessian matrix. This quantity is usually a good estimate of the condition number of the projected Hessian matrix. (If no variables are currently free, is set to zero.)
 posdef : bool
Specifies or according to whether or not the approximation to the second derivative matrix for the current subspace, , is positive definite.
 niter : int
The number of iterations (as outlined in [Description]) which have been performed by
bounds_mod_deriv_comp
so far. nf : int
The number of evaluations of so far, i.e., the number of calls of with set to . Each such call of also calculates the first derivatives of . (In addition to these calls monitored by , is called with set to not more than times per iteration.)
 data : arbitrary, optional, modifiable in place
Usercommunication data for callback functions.
eta : float
Every iteration of
bounds_mod_deriv_comp
involves a linear minimization (i.e., minimization of with respect to ). specifies how accurately these linear minimizations are to be performed. The minimum with respect to will be located more accurately for small values of (say, ) than large values (say, ).Although accurate linear minimizations will generally reduce the number of iterations (and hence the number of calls of to estimate the second derivatives), they will tend to increase the number of calls of needed for each linear minimization.
On balance, it is usually more efficient to perform a low accuracy linear minimization when is small and a high accuracy minimization when is large.
ibound : int
Indicates whether the problem is unconstrained or bounded. If there are bounds on the variables, can be used to indicate whether the facility for dealing with bounds of special forms is to be used. It must be set to one of the following values:
If the variables are bounded and you are supplying all the and individually.
If the problem is unconstrained.
If the variables are bounded, but all the bounds are of the form .
If all the variables are bounded, and and .
If the problem is unconstrained. (The option is provided for consistency with other functions. In
bounds_mod_deriv_comp
it produces the same effect as )bl : float, arraylike, shape
The fixed lower bounds .
If is set to , you must set to , for . (If a lower bound is not specified for any , the corresponding should be set to a large negative number, e.g., .)
If is set to , you must set to ;
bounds_mod_deriv_comp
will then set the remaining elements of equal to .If is set to , or , will be initialized by
bounds_mod_deriv_comp
.bu : float, arraylike, shape
The fixed upper bounds .
If is set to , you must set to , for . (If an upper bound is not specified for any variable, the corresponding should be set to a large positive number, e.g., .)
If is set to , you must set to ;
bounds_mod_deriv_comp
will then set the remaining elements of equal to .If is set to , or , will be initialized by
bounds_mod_deriv_comp
.x : float, arraylike, shape
must be set to a guess at the th component of the position of the minimum, for .
lh : int
The dimension of the array .
iprint : int, optional
The frequency with which is to be called.
is called once every iterations and just before exit from
bounds_mod_deriv_comp
.is just called at the final point.
is not called at all.
should normally be set to a small positive number.
maxcal : int, optional
Note: if this argument is
None
then a default value will be used, determined as follows: .The maximum permitted number of evaluations of , i.e., the maximum permitted number of calls of with set to . It should be borne in mind that, in addition to the calls of which are limited directly by , there will be calls of (with set to ) to evaluate only first derivatives.
xtol : float, optional
The accuracy in to which the solution is required.
If is the true value of at the minimum, then , the estimated position before a normal exit, is such that where .
For example, if the elements of are not much larger than in modulus, and if is set to , then is usually accurate to about five decimal places. (For further details see [Accuracy].)
If the problem is scaled as described in [Further Comments] and is the machine precision, then is probably the smallest reasonable choice for .
This is because, normally, to machine accuracy, , for any where is the th column of the identity matrix.
If you set to (or any positive value less than ),
bounds_mod_deriv_comp
will use instead of .delta : float, optional
The differencing interval to be used for approximating the second derivatives of . Thus, for the finite difference approximations, the first derivatives of are evaluated at points which are apart. If is the machine precision, will usually be a suitable setting for . If you set to (or to any positive value less than ),
bounds_mod_deriv_comp
will automatically use as the differencing interval.stepmx : float, optional
An estimate of the Euclidean distance between the solution and the starting point supplied by you. (For maximum efficiency a slight overestimate is preferable.)
bounds_mod_deriv_comp
will ensure that, for each iteration,where is the iteration number.
Thus, if the problem has more than one solution,
bounds_mod_deriv_comp
is most likely to find the one nearest to the starting point.On difficult problems, a realistic choice can prevent the sequence of entering a region where the problem is illbehaved and can also help to avoid possible overflow in the evaluation of .
However, an underestimate of can lead to inefficiency.
data : arbitrary, optional
Usercommunication data for callback functions.
io_manager : FileObjManager, optional
Manager for I/O in this routine.
Returns: bl : float, ndarray, shape
The lower bounds actually used by
bounds_mod_deriv_comp
, e.g., if , .bu : float, ndarray, shape
The upper bounds actually used by
bounds_mod_deriv_comp
, e.g., if , .x : float, ndarray, shape
The final point . Thus, if no exception or warning is raised on exit, is the th component of the estimated position of the minimum.
hesl : float, ndarray, shape
During the determination of a direction (see [Description]), is decomposed into the product , where is a unit lower triangular matrix and is a diagonal matrix. (The matrices , , and are all of dimension , where is the number of variables free from their bounds. consists of those rows and columns of the full estimated second derivative matrix which relate to free variables. is chosen so that is positive definite.)
and are used to store the factors and .
The elements of the strict lower triangle of are stored row by row in the first positions of .
The diagonal elements of are stored in the first positions of .
In the last factorization before a normal exit, the matrix will be zero, so that and will contain, on exit, the factors of the final estimated second derivative matrix .
The elements of are useful for deciding whether to accept the results produced by
bounds_mod_deriv_comp
(see [Accuracy]).hesd : float, ndarray, shape
During the determination of a direction (see [Description]), is decomposed into the product , where is a unit lower triangular matrix and is a diagonal matrix. (The matrices , , and are all of dimension , where is the number of variables free from their bounds. consists of those rows and columns of the full estimated second derivative matrix which relate to free variables. is chosen so that is positive definite.)
and are used to store the factors and .
The elements of the strict lower triangle of are stored row by row in the first positions of .
The diagonal elements of are stored in the first positions of .
In the last factorization before a normal exit, the matrix will be zero, so that and will contain, on exit, the factors of the final estimated second derivative matrix .
The elements of are useful for deciding whether to accept the results produced by
bounds_mod_deriv_comp
(see [Accuracy]).istate : int, ndarray, shape
Information about which variables are currently on their bounds and which are free. If is:
 – equal to , is fixed on its upper bound;
 – equal to , is fixed on its lower bound;
 – equal to , is effectively a constant (i.e., );
 – positive, gives the position of in the sequence of free variables.
f : float
The function value at the final point given in .
g : float, ndarray, shape
The first derivative vector corresponding to the final point given in . The components of corresponding to free variables should normally be close to zero.
Raises: NagValueError
 (errno )
On entry, and for some .
 (errno )
On entry, and .
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, and .
Constraint: .
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, .
Constraint: .
Warns: NagAlgorithmicWarning
 (errno )
There have been function evaluations.
 (errno )
The conditions for a minimum have not all been satisfied, but a lower point could not be found.
 (errno )
No further progress can be made.
NagCallbackTerminateWarning
 (errno )
User requested termination by setting negative in .
Notes
No equivalent for this routine exists in the NAG C Library.

naginterfaces.library.opt.
bounds_mod_deriv_easy
(ibound, funct2, bl, bu, x, data=None)[source]¶ Bound constrained minimum, modified Newton algorithm, using first derivatives (easytouse).
For full information please refer to the NAG Library document for e04kz
https://www.nag.com/numeric/fl/nagdoc_fl26.2/html/e04/e04kzf.html
Parameters: ibound : int
Indicates whether the facility for dealing with bounds of special forms is to be used. It must be set to one of the following values:
If you are supplying all the and individually.
If there are no bounds on any .
If all the bounds are of the form .
If and .
funct2 : callable (fc, gc) = funct2(xc, data=None)
You must supply this function to calculate the values of the function and its first derivatives at any point .
It should be tested separately before being used in conjunction with
bounds_mod_deriv_easy
(see submoduleopt
). Parameters
 xc : float, ndarray, shape
The point at which the function and derivatives are required.
 data : arbitrary, optional, modifiable in place
Usercommunication data for callback functions.
 Returns
 fc : float
The value of the function at the current point ,
 gc : float, arraylike, shape
must be set to the value of the first derivative at the point , for .
bl : float, arraylike, shape
The lower bounds .
If is set to , you must set to , for . (If a lower bound is not specified for a particular , the corresponding should be set to .)
If is set to , you must set to ;
bounds_mod_deriv_easy
will then set the remaining elements of equal to .bu : float, arraylike, shape
The upper bounds .
If is set to , you must set to , for . (If an upper bound is not specified for a particular , the corresponding should be set to .)
If is set to , you must set to ;
bounds_mod_deriv_easy
will then set the remaining elements of equal to .x : float, arraylike, shape
must be set to a guess at the th component of the position of the minimum, for . The function checks the gradient at the starting point, and is more likely to detect any error in your programming if the initial are nonzero and mutually distinct.
data : arbitrary, optional
Usercommunication data for callback functions.
Returns: bl : float, ndarray, shape
The lower bounds actually used by
bounds_mod_deriv_easy
.bu : float, ndarray, shape
The upper bounds actually used by
bounds_mod_deriv_easy
.x : float, ndarray, shape
The lowest point found during the calculations of the position of the minimum.
f : float
The value of corresponding to the final point stored in .
g : float, ndarray, shape
The value of corresponding to the final point stored in , for ; the value of for variables not on a bound should normally be close to zero.
Raises: NagValueError
 (errno )
On entry, and for some .
 (errno )
On entry, and .
 (errno )
On entry, .
Constraint: .
 (errno )
On entry, .
Constraint: .
 (errno )
There have been function evaluations.
 (errno )
The modulus of a variable has become very large. There may be a mistake in , your problem has no finite solution, or the problem needs rescaling.
 (errno )
It is very likely that you have made an error forming the gradient.
Warns: NagAlgorithmicWarning
 (errno