# NAG FL Interfacee04jcf (bounds_​bobyqa_​func)

Note: this routine is deprecated. Replaced by e04jdf and e04jef.
e04jdf and e04jef are a part of the new NAG optimization modelling suite (see Section 3.1 in the E04 Chapter Introduction) which allows you to define and solve various problems in a uniform manner. They also offer various algorithmic additions, such as a performance improvement on noisy problems, a possibility to progress towards the solution earlier than after $n$ initial function evaluations, heuristic stopping criteria, recovery from unavailable function evaluations, and various other algorithmic updates and tuning. In addition, e04jef offers a reverse communication interface (see Section 3.2 in the E04 Chapter Introduction) which might be useful in some environments or if you can parallelize your function evaluations.
Callbacks
The callback functions are almost identical but the new one has added cpuser argument and the order of arguments is slightly different.

#### Old Code

`Subroutine objfun(n,x,f,iuser,ruser,inform)`

#### New Code

`Subroutine objfun(n,x,f,inform,iuser,ruser,cpuser)`
Main Call

#### Old Code

```ifail = -1
Call e04jcf(objfun,n,npt,x,bl,bu,rhobeg,rhoend,e04jcp,maxcal,f,nf,iuser, &
ruser,ifail)```

#### New Code

```!     .. Use Statements ..
Use, Intrinsic                   :: iso_c_binding, Only: c_null_ptr, c_ptr

!     ...

!     Initialize problem with n variables
ifail = 0
Call e04raf(handle,n,ifail)

!     Add nonlinear objective function which depends on all variables
ifail = 0
Call e04rgf(handle,n,(/(j,j=1,n)/),ifail)

!     Add bounds for the variables
Call e04rhf(handle,n,bl,bu,ifail)

!     Pass npt, rhobeg, rhoend, maxcal as options if different from defaults
ifail = 0
Call e04zmf(handle,'DFO Number Interp Points = <your npt>',ifail)
Call e04zmf(handle,'DFO Starting Trust Region = <your rhobeg>',ifail)
Call e04zmf(handle,'DFO Trust Region Tolerance = <your rhoend>',ifail)
Call e04zmf(handle,'DFO Max Objective Calls = <your maxcal>',ifail)

!     Solve the problem
cpuser = c_null_ptr
ifail = -1
Call e04jdf(handle,objfun,e04jdu,n,x,rinfo,stats,iuser,ruser,cpuser,ifail)

!     retrieve the objective value and the total number of calls made to the
!     objective function
f = rinfo(1)
nf = stats(1)

!     Free the handle memory
ifail = 0
Call e04rzf(handle,ifail)```

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

e04jcf is an easy-to-use algorithm that uses methods of quadratic approximation to find a minimum of an objective function $F$ over $\mathbf{x}\in {R}^{n}$, subject to fixed lower and upper bounds on the independent variables ${x}_{1},{x}_{2},\dots ,{x}_{n}$. Derivatives of $F$ are not required.
The routine is intended for functions that are continuous and that have continuous first and second derivatives (although it will usually work even if the derivatives have occasional discontinuities). Efficiency is maintained for large $n$.

## 2Specification

Fortran Interface
 Subroutine e04jcf ( n, npt, x, bl, bu, f, nf,
 Integer, Intent (In) :: n, npt, maxcal Integer, Intent (Inout) :: iuser(*), ifail Integer, Intent (Out) :: nf Real (Kind=nag_wp), Intent (In) :: bl(n), bu(n), rhobeg, rhoend Real (Kind=nag_wp), Intent (Inout) :: x(n), ruser(*) Real (Kind=nag_wp), Intent (Out) :: f External :: objfun, monfun
#include <nag.h>
 void e04jcf_ (void (NAG_CALL *objfun)(const Integer *n, const double x[], double *f, Integer iuser[], double ruser[], Integer *inform),const Integer *n, const Integer *npt, double x[], const double bl[], const double bu[], const double *rhobeg, const double *rhoend, void (NAG_CALL *monfun)(const Integer *n, const Integer *nf, const double x[], const double *f, const double *rho, Integer iuser[], double ruser[], Integer *inform),const Integer *maxcal, double *f, Integer *nf, Integer iuser[], double ruser[], Integer *ifail)
The routine may be called by the names e04jcf or nagf_opt_bounds_bobyqa_func.

## 3Description

e04jcf is applicable to problems of the form:
 $minimize x∈Rn F(x) subject to ℓ ≤ x ≤ u and ℓ≤u ,$
where $F$ is a nonlinear scalar function whose derivatives may be unavailable, and where the bound vectors are elements of ${R}^{n}$. Relational operators between vectors are interpreted elementwise.
Fixing variables (that is, setting ${\ell }_{i}={u}_{i}$ for some $i$) is allowed in e04jcf.
You must supply a subroutine to calculate the value of $F$ at any given point $\mathbf{x}$.
The method used by e04jcf is based on BOBYQA, the method of Bound Optimization BY Quadratic Approximation described in Powell (2009). In particular, each iteration of e04jcf generates a quadratic approximation $Q$ to $F$ that agrees with $F$ at $m$ automatically chosen interpolation points. The value of $m$ is a constant prescribed by you. Updates to the independent variables mostly occur from approximate solutions to trust region subproblems, using the current quadratic model.

## 4References

Powell M J D (2009) The BOBYQA algorithm for bound constrained optimization without derivatives Report DAMTP 2009/NA06 University of Cambridge https://www.damtp.cam.ac.uk/user/na/NA_papers/NA2009_06.pdf

## 5Arguments

1: $\mathbf{objfun}$Subroutine, supplied by the user. External Procedure
objfun must evaluate the objective function $F$ at a specified vector $\mathbf{x}$.
The specification of objfun is:
Fortran Interface
 Subroutine objfun ( n, x, f,
 Integer, Intent (In) :: n Integer, Intent (Inout) :: iuser(*) Integer, Intent (Out) :: inform Real (Kind=nag_wp), Intent (In) :: x(n) Real (Kind=nag_wp), Intent (Inout) :: ruser(*) Real (Kind=nag_wp), Intent (Out) :: f
 void objfun (const Integer *n, const double x[], double *f, Integer iuser[], double ruser[], Integer *inform)
1: $\mathbf{n}$Integer Input
On entry: $n$, the number of independent variables.
2: $\mathbf{x}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: $\mathbf{x}$, the vector at which the objective function is to be evaluated.
3: $\mathbf{f}$Real (Kind=nag_wp) Output
On exit: must be set to the value of the objective function at $\mathbf{x}$.
4: $\mathbf{iuser}\left(*\right)$Integer array User Workspace
5: $\mathbf{ruser}\left(*\right)$Real (Kind=nag_wp) array User Workspace
objfun is called with the arguments iuser and ruser as supplied to e04jcf. You should use the arrays iuser and ruser to supply information to objfun.
6: $\mathbf{inform}$Integer Output
On exit: must be set to a value describing the action to be taken by the solver on return from objfun. Specifically, if the value is negative the solution of the current problem will terminate immediately; otherwise, computations will continue.
objfun must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which e04jcf is called. Arguments denoted as Input must not be changed by this procedure.
Note: objfun should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by e04jcf. If your code inadvertently does return any NaNs or infinities, e04jcf is likely to produce unexpected results.
2: $\mathbf{n}$Integer Input
On entry: $n$, the number of independent variables.
Constraint: ${\mathbf{n}}\ge 2$ and ${n}_{r}\ge 2$, where ${n}_{r}$ denotes the number of non-fixed variables.
3: $\mathbf{npt}$Integer Input
On entry: $m$, the number of interpolation conditions imposed on the quadratic approximation at each iteration.
Suggested value: ${\mathbf{npt}}=2×{n}_{r}+1$, where ${n}_{r}$ denotes the number of non-fixed variables.
Constraint: ${n}_{r}+2\le {\mathbf{npt}}\le \frac{\left({n}_{r}+1\right)×\left({n}_{r}+2\right)}{2}$, where ${n}_{r}$ denotes the number of non-fixed variables.
4: $\mathbf{x}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input/Output
On entry: an estimate of the position of the minimum. If any component is out-of-bounds it is replaced internally by the bound it violates.
On exit: the lowest point found during the calculations. Thus, if ${\mathbf{ifail}}={\mathbf{0}}$ on exit, x is the position of the minimum.
5: $\mathbf{bl}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
6: $\mathbf{bu}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: the fixed vectors of bounds: the lower bounds $\mathbf{\ell }$ and the upper bounds $\mathbf{u}$, respectively. To signify that a variable is unbounded you should choose a large scalar $r$ appropriate to your problem, then set the lower bound on that variable to $-r$ and the upper bound to $r$. For well-scaled problems $r={r}_{\mathrm{max}}^{\frac{1}{4}}$ may be suitable, where ${r}_{\mathrm{max}}$ denotes the largest positive model number (see x02alf).
Constraints:
• if ${\mathbf{x}}\left(\mathit{i}\right)$ is to be fixed at ${\mathbf{bl}}\left(\mathit{i}\right)$, then${\mathbf{bl}}\left(\mathit{i}\right)={\mathbf{bu}}\left(\mathit{i}\right)$;
• otherwise ${\mathbf{bu}}\left(\mathit{i}\right)-{\mathbf{bl}}\left(\mathit{i}\right)\ge 2.0×{\mathbf{rhobeg}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
7: $\mathbf{rhobeg}$Real (Kind=nag_wp) Input
On entry: an initial lower bound on the value of the trust region radius.
Suggested value: rhobeg should be about one tenth of the greatest expected overall change to a variable: the initial quadratic model will be constructed by taking steps from the initial x of length rhobeg along each coordinate direction.
Constraints:
• ${\mathbf{rhobeg}}>0.0$;
• ${\mathbf{rhobeg}}\ge {\mathbf{rhoend}}$.
8: $\mathbf{rhoend}$Real (Kind=nag_wp) Input
On entry: a final lower bound on the value of the trust region radius.
Suggested value: rhoend should indicate the absolute accuracy that is required in the final values of the variables.
Constraint: ${\mathbf{rhoend}}\ge \mathit{macheps}$, where $\mathit{macheps}={\mathbf{x02ajf}}\left(\right)$, the machine precision.
9: $\mathbf{monfun}$Subroutine, supplied by the NAG Library or the user. External Procedure
monfun may be used to monitor the optimization process. It is invoked every time a new trust region radius is chosen.
If no monitoring is required, monfun may be the dummy monitoring routine e04jcp supplied by the NAG Library.
The specification of monfun is:
Fortran Interface
 Subroutine monfun ( n, nf, x, f, rho,
 Integer, Intent (In) :: n, nf Integer, Intent (Inout) :: iuser(*) Integer, Intent (Out) :: inform Real (Kind=nag_wp), Intent (In) :: x(n), f, rho Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
 void monfun (const Integer *n, const Integer *nf, const double x[], const double *f, const double *rho, Integer iuser[], double ruser[], Integer *inform)
1: $\mathbf{n}$Integer Input
On entry: $n$, the number of independent variables.
2: $\mathbf{nf}$Integer Input
On entry: the cumulative number of calls made to objfun.
3: $\mathbf{x}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: the current best point.
4: $\mathbf{f}$Real (Kind=nag_wp) Input
On entry: the value of objfun at x.
5: $\mathbf{rho}$Real (Kind=nag_wp) Input
On entry: a lower bound on the current trust region radius.
6: $\mathbf{iuser}\left(*\right)$Integer array User Workspace
7: $\mathbf{ruser}\left(*\right)$Real (Kind=nag_wp) array User Workspace
monfun is called with the arguments iuser and ruser as supplied to e04jcf. You should use the arrays iuser and ruser to supply information to monfun.
8: $\mathbf{inform}$Integer Output
On exit: must be set to a value describing the action to be taken by the solver on return from monfun. Specifically, if the value is negative the solution of the current problem will terminate immediately; otherwise, computations will continue.
monfun must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which e04jcf is called. Arguments denoted as Input must not be changed by this procedure.
10: $\mathbf{maxcal}$Integer Input
On entry: the maximum permitted number of calls to objfun.
Constraint: ${\mathbf{maxcal}}\ge 1$.
11: $\mathbf{f}$Real (Kind=nag_wp) Output
On exit: the function value at the lowest point found (x).
12: $\mathbf{nf}$Integer Output
On exit: unless ${\mathbf{ifail}}={\mathbf{1}}$ or $-{\mathbf{999}}$ on exit, the total number of calls made to objfun.
13: $\mathbf{iuser}\left(*\right)$Integer array User Workspace
14: $\mathbf{ruser}\left(*\right)$Real (Kind=nag_wp) array User Workspace
iuser and ruser are not used by e04jcf, but are passed directly to objfun and monfun and may be used to pass information to these routines.
15: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).
e04jcf returns with ${\mathbf{ifail}}={\mathbf{0}}$ if the final trust region radius has reached its lower bound rhoend.

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{maxcal}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{maxcal}}\ge 1$.
On entry, ${\mathbf{rhobeg}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{bl}}\left(i\right)=⟨\mathit{\text{value}}⟩$, ${\mathbf{bu}}\left(i\right)=⟨\mathit{\text{value}}⟩$ and $i=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{bl}}\left(i\right)\ne {\mathbf{bu}}\left(i\right)$ in coordinate $i$, ${\mathbf{bu}}\left(i\right)-{\mathbf{bl}}\left(i\right)\ge 2×{\mathbf{rhobeg}}$.
On entry, ${\mathbf{rhobeg}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{rhobeg}}>0.0$.
On entry, ${\mathbf{rhobeg}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{rhoend}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{rhoend}}\le {\mathbf{rhobeg}}$.
On entry, ${\mathbf{rhoend}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{rhoend}}\ge \mathit{macheps}$, where $\mathit{macheps}={\mathbf{x02ajf}}\left(\right)$, the machine precision.
There were ${n}_{r}=⟨\mathit{\text{value}}⟩$ unequal bounds.
Constraint: ${n}_{r}\ge 2$.
There were ${n}_{r}=⟨\mathit{\text{value}}⟩$ unequal bounds and ${\mathbf{npt}}=⟨\mathit{\text{value}}⟩$ on entry.
Constraint: ${n}_{r}+2\le {\mathbf{npt}}\le \frac{\left({n}_{r}+1\right)×\left({n}_{r}+2\right)}{2}$.
${\mathbf{ifail}}=2$
The function evaluations limit was reached: objfun has been called maxcal times.
${\mathbf{ifail}}=3$
The predicted reduction in a trust region step was non-positive. Check your specification of objfun and whether the function needs rescaling. Try a different initial x.
${\mathbf{ifail}}=4$
A rescue procedure has been called in order to correct damage from rounding errors when computing an update to a quadratic approximation of $F$, but no further progess could be made. Check your specification of objfun and whether the function needs rescaling. Try a different initial x.
${\mathbf{ifail}}=5$
User-supplied monitoring routine requested termination.
User-supplied objective function requested termination.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

Experience shows that, in many cases, on successful termination the $\infty$-norm distance from the best point $\mathbf{x}$ to a local minimum of $F$ is less than $10×{\mathbf{rhoend}}$, unless rhoend is so small that such accuracy is unattainable.

## 8Parallelism and Performance

e04jcf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

For each invocation of e04jcf, local workspace arrays of fixed length are allocated internally. The total size of these arrays amounts to $\left({\mathbf{npt}}+6\right)×\left({\mathbf{npt}}+{n}_{r}\right)+\frac{{n}_{r}×\left(3{n}_{r}+21\right)}{2}$ real elements and ${n}_{r}$ integer elements, where ${n}_{r}$ denotes the number of non-fixed variables; that is, the total size is $\mathcal{O}\left({n}_{r}^{4}\right)$. If you follow the recommendation for the choice of npt on entry, this total size reduces to $\mathcal{O}\left({n}_{r}^{2}\right)$.
Usually the total number of function evaluations (nf) is substantially less than $\mathcal{O}\left({n}_{r}^{2}\right)$, and often, if ${\mathbf{npt}}=2×{n}_{r}+1$ on entry, nf is only of magnitude ${n}_{r}$ or less.

## 10Example

This example involves the minimization of
 $F = (x1+10x2) 2 +5⁢ (x3-x4) 2 + (x2-2x3) 4 +10⁢ (x1-x4) 4$
subject to
 $-1≤x1≤ 3, −2≤x2≤ 0, -1≤x4≤ 3,$
starting from the initial guess $\left(3,-1,0,1\right)$.

### 10.1Program Text

Program Text (e04jcfe.f90)

None.

### 10.3Program Results

Program Results (e04jcfe.r)