﻿ e04jc Method
e04jc 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 method 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$.

# Syntax

C#
```public static void e04jc(
E04..::..E04JC_OBJFUN objfun,
int n,
int npt,
double[] x,
double[] bl,
double[] bu,
double rhobeg,
double rhoend,
E04..::..E04JC_MONFUN monfun,
int maxcal,
out double f,
out int nf,
out int ifail
)```
Visual Basic
```Public Shared Sub e04jc ( _
objfun As E04..::..E04JC_OBJFUN, _
n As Integer, _
npt As Integer, _
x As Double(), _
bl As Double(), _
bu As Double(), _
rhobeg As Double, _
rhoend As Double, _
monfun As E04..::..E04JC_MONFUN, _
maxcal As Integer, _
<OutAttribute> ByRef f As Double, _
<OutAttribute> ByRef nf As Integer, _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void e04jc(
E04..::..E04JC_OBJFUN^ objfun,
int n,
int npt,
array<double>^ x,
array<double>^ bl,
array<double>^ bu,
double rhobeg,
double rhoend,
E04..::..E04JC_MONFUN^ monfun,
int maxcal,
[OutAttribute] double% f,
[OutAttribute] int% nf,
[OutAttribute] int% ifail
)```
F#
```static member e04jc :
objfun : E04..::..E04JC_OBJFUN *
n : int *
npt : int *
x : float[] *
bl : float[] *
bu : float[] *
rhobeg : float *
rhoend : float *
monfun : E04..::..E04JC_MONFUN *
maxcal : int *
f : float byref *
nf : int byref *
ifail : int byref -> unit
```

#### Parameters

objfun
Type: NagLibrary..::..E04..::..E04JC_OBJFUN
objfun must evaluate the objective function $F$ at a specified vector $\mathbf{x}$.

A delegate of type E04JC_OBJFUN.

n
Type: System..::..Int32
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.
npt
Type: System..::..Int32
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.
x
Type: array<System..::..Double>[]()[][]
An array of size [n]
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}}={0}$ on exit, x is the position of the minimum.
bl
Type: array<System..::..Double>[]()[][]
An array of size [n]
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 x02al).
Constraints:
• if ${\mathbf{x}}\left[\mathit{i}-1\right]$ is to be fixed at ${\mathbf{bl}}\left[\mathit{i}-1\right]$, then ${\mathbf{bl}}\left[\mathit{i}-1\right]={\mathbf{bu}}\left[\mathit{i}-1\right]$;
• otherwise ${\mathbf{bu}}\left[\mathit{i}-1\right]-{\mathbf{bl}}\left[\mathit{i}-1\right]\ge 2.0×{\mathbf{rhobeg}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
bu
Type: array<System..::..Double>[]()[][]
An array of size [n]
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 x02al).
Constraints:
• if ${\mathbf{x}}\left[\mathit{i}-1\right]$ is to be fixed at ${\mathbf{bl}}\left[\mathit{i}-1\right]$, then ${\mathbf{bl}}\left[\mathit{i}-1\right]={\mathbf{bu}}\left[\mathit{i}-1\right]$;
• otherwise ${\mathbf{bu}}\left[\mathit{i}-1\right]-{\mathbf{bl}}\left[\mathit{i}-1\right]\ge 2.0×{\mathbf{rhobeg}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
rhobeg
Type: System..::..Double
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}}$.
rhoend
Type: System..::..Double
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}}>0.0$.
monfun
Type: NagLibrary..::..E04..::..E04JC_MONFUN
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 method E04JCP supplied by the NAG Library.

A delegate of type E04JC_MONFUN.

maxcal
Type: System..::..Int32
On entry: the maximum permitted number of calls to objfun.
Constraint: ${\mathbf{maxcal}}\ge 1$.
f
Type: System..::..Double%
On exit: the function value at the lowest point found (x).
nf
Type: System..::..Int32%
On exit: unless ${\mathbf{ifail}}={1}$ or ${-}{999}$ on exit, the total number of calls made to objfun.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

e04jc is applicable to problems of the form:
 $minimizex∈RnFx 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 e04jc.
You must supply a method to calculate the value of $F$ at any given point $\mathbf{x}$.
The method used by e04jc is based on BOBYQA, the method of Bound Optimization BY Quadratic Approximation described in Powell (2009). In particular, each iteration of e04jc 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.

# References

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

# Error Indicators and Warnings

Errors or warnings detected by the method:
${\mathbf{ifail}}=1$
An input parameter is invalid.
${\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$
You terminated the solver.
You indicated that you wished to halt solution of the current problem by setting inform to a negative value in either objfun or monfun.
${\mathbf{ifail}}=-999$
Internal memory allocation failed.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

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.

# Parallelism and Performance

None.

For each invocation of e04jc, 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.

# Example

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

Example program (C#): e04jce.cs

Example program results: e04jce.r