GALAHAD BGO package#

purpose#

The bgo package uses a multi-start trust-region method to find an approximation to the global minimizer of a differentiable objective function \(f(x)\) of n variables \(x\), subject to simple bounds \(x^l <= x <= x^u\) on the variables. Here, any of the components of the vectors of bounds \(x^l\) and \(x^u\) may be infinite. The method offers the choice of direct and iterative solution of the key trust-region subproblems, and is suitable for large problems. First derivatives are required, and if second derivatives can be calculated, they will be exploited - if the product of second derivatives with a vector may be found but not the derivatives themselves, that may also be exploited.

The package offers both random multi-start and local-minimize-and-probe methods to try to locate the global minimizer. There are no theoretical guarantees unless the sampling is huge, and realistically the success of the methods decreases as the dimension and nonconvexity increase.

See Section 4 of $GALAHAD/doc/bgo.pdf for additional details.

method#

A choice of two methods is available. In the first, local-minimization-and-probe, approach, local minimization and univariate global minimization are intermixed. Given a current champion \(x^S_k\), a local minimizer \(x_k\) of \(f(x)\) within the feasible box \(x^l \leq x \leq x^u\) is found using TRB. Thereafter \(m\) random directions \(p\) are generated, and univariate local minimizer of \(f(x_k + \alpha p)\) as a function of the scalar \(\alpha\) along each \(p\) within the interval \([\alpha^L,\alpha^u]\), where \(\alpha^L\) and \(\alpha^u\) are the smallest and largest \(\alpha\) for which \(x^l \leq x_k + \alpha p \leq x^u\), is performed using UGO. The point \(x_k + \alpha p\) that gives the smallest value of \(f\) is then selected as the new champion \(x^S_{k+1}\).

The random directions \(p\) are chosen in one of three ways. The simplest is to select the components as

\[\begin{split}p_i = \mbox{pseudo random $\in$} \left\{ \begin{array}{rl} \mbox{[-1,1]} & \mbox{if} \;\; x^l_i < x_{k,i} < x^u_i \\ \mbox{[0,1]} & \mbox{if} \;\; x_{k,i} = x^l_i \\ \mbox{[-1,0]} & \mbox{if} \;\; x_{k,i} = x^u_i \end{array} \right.\end{split}\]
for each \(1 \leq i \leq n\). An alternative is to pick \(p\) by partitioning each dimension of the feasible “hypercube” box into \(m\) equal segments, and then selecting sub-boxes randomly within this hypercube using Latin hypercube sampling via LHS. Each components of \(p\) is then selected in its sub-box, either uniformly or pseudo randomly.

The other, random-multi-start, method provided selects \(m\) starting points at random, either componentwise pseudo randomly in the feasible box, or by partitioning each component into \(m\) equal segments, assigning each to a sub-box using Latin hypercube sampling, and finally choosing the values either uniformly or pseudo randomly. Local minimizers within the feasible box are then computed by TRB, and the best is assigned as the current champion. This process is then repeated until evaluation limits are achieved.

If \(n=1\), UGO is called directly.

We reiterate that there are no theoretical guarantees unless the sampling is huge, and realistically the success of the methods decreases as the dimension and nonconvexity increase. Thus the methods used should best be viewed as heuristics.

references#

The generic bound-constrained trust-region method is described in detail in

A. R. Conn, N. I. M. Gould and Ph. L. Toint, Trust-region methods. SIAM/MPS Series on Optimization (2000),

the univariate global minimization method employed is an extension of that due to

D. Lera and Ya. D. Sergeyev, ``Acceleration of univariate global optimization algorithms working with Lipschitz functions and Lipschitz first derivatives’’ SIAM J. Optimization 23(1) (2013) 508–529,

while the Latin-hypercube sampling method employed is that of

B. Beachkofski and R. Grandhi, ``Improved Distributed Hypercube Sampling’’, 43rd AIAA structures, structural dynamics, and materials conference, (2002) 2002-1274.

matrix storage#

The symmetric \(n\) by \(n\) matrix \(H = \nabla^2_{xx}f\) may be presented and stored in a variety of formats. But crucially symmetry is exploited by only storing values from the lower triangular part (i.e, those entries that lie on or below the leading diagonal).

Dense storage format: The matrix \(H\) is stored as a compact dense matrix by rows, that is, the values of the entries of each row in turn are stored in order within an appropriate real one-dimensional array. Since \(H\) is symmetric, only the lower triangular part (that is the part \(H_{ij}\) for \(1 \leq j \leq i \leq n\)) need be held. In this case the lower triangle should be stored by rows, that is component \((i-1) * i / 2 + j\) of the storage array H_val will hold the value \(H_{ij}\) (and, by symmetry, \(H_{ji}\)) for \(1 \leq j \leq i \leq n\). The string H_type = ‘dense’ should be specified.

Sparse co-ordinate storage format: Only the nonzero entries of the matrices are stored. For the \(l\)-th entry, \(1 \leq l \leq ne\), of \(H\), its row index i, column index j and value \(H_{ij}\), \(1 \leq j \leq i \leq n\), are stored as the \(l\)-th components of the integer arrays H_row and H_col and real array H_val, respectively, while the number of nonzeros is recorded as H_ne = \(ne\). Note that only the entries in the lower triangle should be stored. The string H_type = ‘coordinate’ should be specified.

Sparse row-wise storage format: Again only the nonzero entries are stored, but this time they are ordered so that those in row i appear directly before those in row i+1. For the i-th row of \(H\) the i-th component of the integer array H_ptr holds the position of the first entry in this row, while H_ptr(n+1) holds the total number of entries plus one. The column indices j, \(1 \leq j \leq i\), and values \(H_{ij}\) of the entries in the i-th row are stored in components l = H_ptr(i), …, H_ptr(i+1)-1 of the integer array H_col, and real array H_val, respectively. Note that as before only the entries in the lower triangle should be stored. For sparse matrices, this scheme almost always requires less storage than its predecessor. The string H_type = ‘sparse_by_rows’ should be specified.

Diagonal storage format: If \(H\) is diagonal (i.e., \(H_{ij} = 0\) for all \(1 \leq i \neq j \leq n\)) only the diagonals entries \(H_{ii}\), \(1 \leq i \leq n\) need be stored, and the first n components of the array H_val may be used for the purpose. The string H_type = ‘diagonal’ should be specified.

introduction to function calls#

To solve a given problem, functions from the bgo package must be called in the following order:

  • bgo_initialize - provide default control parameters and set up initial data structures

  • bgo_read_specfile (optional) - override control values by reading replacement values from a file

  • bgo_import - set up problem data structures and fixed values

  • bgo_reset_control (optional) - possibly change control parameters if a sequence of problems are being solved

  • solve the problem by calling one of

    • bgo_solve_with_mat - solve using function calls to evaluate function, gradient and Hessian values

    • bgo_solve_without_mat - solve using function calls to evaluate function and gradient values and Hessian-vector products

    • bgo_solve_reverse_with_mat - solve returning to the calling program to obtain function, gradient and Hessian values, or

    • bgo_solve_reverse_without_mat - solve returning to the calling prorgram to obtain function and gradient values and Hessian-vector products

  • bgo_information (optional) - recover information about the solution and solution process

  • bgo_terminate - deallocate data structures

See the examples section for illustrations of use.

parametric real type T#

Below, the symbol T refers to a parametric real type that may be Float32 (single precision) or Float64 (double precision). Calable functions as described are with T as Float64, but variants (with the additional suffix _s, e.g., bgo_initialize_s) are available with T as Float32.

callable functions#

    function bgo_initialize(data, control, status)

Set default control values and initialize private data

Parameters:

data

holds private internal data

control

is a structure containing control information (see bgo_control_type)

status

is a scalar variable of type Int32 that gives the exit status from the package. Possible values are (currently):

  • 0

    The initialization was successful.

    function bgo_read_specfile(control, specfile)

Read the content of a specification file, and assign values associated with given keywords to the corresponding control parameters. An in-depth discussion of specification files is available, and a detailed list of keywords with associated default values is provided in $GALAHAD/src/bgo/BGO.template. See also Table 2.1 in the Fortran documentation provided in $GALAHAD/doc/bgo.pdf for a list of how these keywords relate to the components of the control structure. .. rubric:: Parameters:

control

is a structure containing control information (see bgo_control_type)

specfile

is a one-dimensional array of type Vararg{Cchar} that must give the name of the specification file

    function bgo_import(control, data, status, n, x_l, x_u,
                        H_type, ne, H_row, H_col, H_ptr)

Import problem data into internal storage prior to solution.

Parameters:

control

is a structure whose members provide control parameters for the remaining procedures (see bgo_control_type)

data

holds private internal data

status

is a scalar variable of type Int32 that gives the exit status from the package. Possible values are:

  • 1

    The import was successful, and the package is ready for the solve phase

  • -1

    An allocation error occurred. A message indicating the offending array is written on unit control.error, and the returned allocation status and a string containing the name of the offending array are held in inform.alloc_status and inform.bad_alloc respectively.

  • -2

    A deallocation error occurred. A message indicating the offending array is written on unit control.error and the returned allocation status and a string containing the name of the offending array are held in inform.alloc_status and inform.bad_alloc respectively.

  • -3

    The restriction n > 0 or requirement that type contains its relevant string ‘dense’, ‘coordinate’, ‘sparse_by_rows’, ‘diagonal’ or ‘absent’ has been violated.

n

is a scalar variable of type Int32 that holds the number of variables.

x_l

is a one-dimensional array of size n and type T that holds the values \(x^l\) of the lower bounds on the optimization variables \(x\). The j-th component of x_l, \(j = 1, \ldots, n\), contains \(x^l_j\).

x_u

is a one-dimensional array of size n and type T that holds the values \(x^u\) of the upper bounds on the optimization variables \(x\). The j-th component of x_u, \(j = 1, \ldots, n\), contains \(x^u_j\).

H_type

is a one-dimensional array of type Vararg{Cchar} that specifies the symmetric storage scheme used for the Hessian. It should be one of ‘coordinate’, ‘sparse_by_rows’, ‘dense’, ‘diagonal’ or ‘absent’, the latter if access to the Hessian is via matrix-vector products; lower or upper case variants are allowed.

ne

is a scalar variable of type Int32 that holds the number of entries in the lower triangular part of H in the sparse co-ordinate storage scheme. It need not be set for any of the other three schemes.

H_row

is a one-dimensional array of size ne and type Int32 that holds the row indices of the lower triangular part of H in the sparse co-ordinate storage scheme. It need not be set for any of the other three schemes, and in this case can be C_NULL

H_col

is a one-dimensional array of size ne and type Int32 that holds the column indices of the lower triangular part of H in either the sparse co-ordinate, or the sparse row-wise storage scheme. It need not be set when the dense or diagonal storage schemes are used, and in this case can be C_NULL

H_ptr

is a one-dimensional array of size n+1 and type Int32 that holds the starting position of each row of the lower triangular part of H, as well as the total number of entries, in the sparse row-wise storage scheme. It need not be set when the other schemes are used, and in this case can be C_NULL

    function bgo_reset_control(control, data, status)

Reset control parameters after import if required.

Parameters:

control

is a structure whose members provide control parameters for the remaining procedures (see bgo_control_type)

data

holds private internal data

status

is a scalar variable of type Int32 that gives the exit status from the package. Possible values are:

  • 1

    The import was successful, and the package is ready for the solve phase

    function bgo_solve_with_mat(data, userdata, status, n, x, g, ne,
                                eval_f, eval_g, eval_h, eval_hprod, eval_prec)

Find an approximation to the global minimizer of a given function subject to simple bounds on the variables using a multistart trust-region method.

This call is for the case where \(H = \nabla_{xx}f(x)\) is provided specifically, and all function/derivative information is available by function calls.

Parameters:

data

holds private internal data

userdata

is a structure that allows data to be passed into the function and derivative evaluation programs.

status

is a scalar variable of type Int32 that gives the entry and exit status from the package.

On initial entry, status must be set to 1.

Possible exit values are:

  • 0

    The run was successful

  • -1

    An allocation error occurred. A message indicating the offending array is written on unit control.error, and the returned allocation status and a string containing the name of the offending array are held in inform.alloc_status and inform.bad_alloc respectively.

  • -2

    A deallocation error occurred. A message indicating the offending array is written on unit control.error and the returned allocation status and a string containing the name of the offending array are held in inform.alloc_status and inform.bad_alloc respectively.

  • -3

    The restriction n > 0 or requirement that type contains its relevant string ‘dense’, ‘coordinate’, ‘sparse_by_rows’, ‘diagonal’ or ‘absent’ has been violated.

  • -7

    The objective function appears to be unbounded from below

  • -9

    The analysis phase of the factorization failed; the return status from the factorization package is given in the component inform.factor_status

  • -10

    The factorization failed; the return status from the factorization package is given in the component inform.factor_status.

  • -11

    The solution of a set of linear equations using factors from the factorization package failed; the return status from the factorization package is given in the component inform.factor_status.

  • -16

    The problem is so ill-conditioned that further progress is impossible.

  • -18

    Too many iterations have been performed. This may happen if control.maxit is too small, but may also be symptomatic of a badly scaled problem.

  • -19

    The CPU time limit has been reached. This may happen if control.cpu_time_limit is too small, but may also be symptomatic of a badly scaled problem.

  • -82

    The user has forced termination of solver by removing the file named control.alive_file from unit unit control.alive_unit.

n

is a scalar variable of type Int32 that holds the number of variables

x

is a one-dimensional array of size n and type T that holds the values \(x\) of the optimization variables. The j-th component of x, j = 1, … , n, contains \(x_j\).

g

is a one-dimensional array of size n and type T that holds the gradient \(g = \nabla_xf(x)\) of the objective function. The j-th component of g, j = 1, … , n, contains \(g_j\).

ne

is a scalar variable of type Int32 that holds the number of entries in the lower triangular part of the Hessian matrix \(H\).

eval_f

is a user-supplied function that must have the following signature:

function eval_f(n, x, f, userdata)

The value of the objective function \(f(x)\) evaluated at x=\(x\) must be assigned to f, and the function return value set to 0. If the evaluation is impossible at x, return should be set to a nonzero value. Data may be passed into eval_f via the structure userdata.

eval_g

is a user-supplied function that must have the following signature:

function eval_g(n, x, g, userdata)

The components of the gradient \(g = \nabla_x f(x\)) of the objective function evaluated at x=\(x\) must be assigned to g, and the function return value set to 0. If the evaluation is impossible at x, return should be set to a nonzero value. Data may be passed into eval_g via the structure userdata.

eval_h

is a user-supplied function that must have the following signature:

function eval_h(n, ne, x, h, userdata)

The nonzeros of the Hessian \(H = \nabla_{xx}f(x)\) of the objective function evaluated at x=\(x\) must be assigned to h in the same order as presented to bgo_import, and the function return value set to 0. If the evaluation is impossible at x, return should be set to a nonzero value. Data may be passed into eval_h via the structure userdata.

eval_prec

is an optional user-supplied function that may be C_NULL. If non-NULL, it must have the following signature:

function eval_prec(n, x, u, v, userdata)

The product \(u = P(x) v\) of the user’s preconditioner \(P(x)\) evaluated at \(x\) with the vector v=\(v\), the result \(u\) must be retured in u, and the function return value set to 0. If the evaluation is impossible at x, return should be set to a nonzero value. Data may be passed into eval_prec via the structure userdata.

    function bgo_solve_without_mat(data, userdata, status, n, x, g,
                                   eval_f, eval_g, eval_hprod,
                                   eval_shprod, eval_prec)

Find an approximation to the global minimizer of a given function subject to simple bounds on the variables using a multistart trust-region method.

This call is for the case where access to \(H = \nabla_{xx}f(x)\) is provided by Hessian-vector products, and all function/derivative information is available by function calls.

Parameters:

data

holds private internal data

userdata

is a structure that allows data to be passed into the function and derivative evaluation programs.

status

is a scalar variable of type Int32 that gives the entry and exit status from the package.

On initial entry, status must be set to 1.

Possible exit values are:

  • 0

    The run was successful

  • -1

    An allocation error occurred. A message indicating the offending array is written on unit control.error, and the returned allocation status and a string containing the name of the offending array are held in inform.alloc_status and inform.bad_alloc respectively.

  • -2

    A deallocation error occurred. A message indicating the offending array is written on unit control.error and the returned allocation status and a string containing the name of the offending array are held in inform.alloc_status and inform.bad_alloc respectively.

  • -3

    The restriction n > 0 or requirement that type contains its relevant string ‘dense’, ‘coordinate’, ‘sparse_by_rows’, ‘diagonal’ or ‘absent’ has been violated.

  • -7

    The objective function appears to be unbounded from below

  • -9

    The analysis phase of the factorization failed; the return status from the factorization package is given in the component inform.factor_status

  • -10

    The factorization failed; the return status from the factorization package is given in the component inform.factor_status.

  • -11

    The solution of a set of linear equations using factors from the factorization package failed; the return status from the factorization package is given in the component inform.factor_status.

  • -16

    The problem is so ill-conditioned that further progress is impossible.

  • -18

    Too many iterations have been performed. This may happen if control.maxit is too small, but may also be symptomatic of a badly scaled problem.

  • -19

    The CPU time limit has been reached. This may happen if control.cpu_time_limit is too small, but may also be symptomatic of a badly scaled problem.

  • -82

    The user has forced termination of solver by removing the file named control.alive_file from unit unit control.alive_unit.

n

is a scalar variable of type Int32 that holds the number of variables

x

is a one-dimensional array of size n and type T that holds the values \(x\) of the optimization variables. The j-th component of x, j = 1, … , n, contains \(x_j\).

g

is a one-dimensional array of size n and type T that holds the gradient \(g = \nabla_xf(x)\) of the objective function. The j-th component of g, j = 1, … , n, contains \(g_j\).

eval_f

is a user-supplied function that must have the following signature:

function eval_f(n, x, f, userdata)

The value of the objective function \(f(x)\) evaluated at x=\(x\) must be assigned to f, and the function return value set to 0. If the evaluation is impossible at x, return should be set to a nonzero value. Data may be passed into eval_f via the structure userdata.

eval_g

is a user-supplied function that must have the following signature:

function eval_g(n, x, g, userdata)

The components of the gradient \(g = \nabla_x f(x\)) of the objective function evaluated at x=\(x\) must be assigned to g, and the function return value set to 0. If the evaluation is impossible at x, return should be set to a nonzero value. Data may be passed into eval_g via the structure userdata.

eval_hprod

is a user-supplied function that must have the following signature:

function eval_hprod(n, x, u, v, got_h, userdata)

The sum \(u + \nabla_{xx}f(x) v\) of the product of the Hessian \(\nabla_{xx}f(x)\) of the objective function evaluated at x=\(x\) with the vector v=\(v\) and the vector $ \(u\) must be returned in u, and the function return value set to 0. If the evaluation is impossible at x, return should be set to a nonzero value. The Hessian has already been evaluated or used at x if the Bool got_h is true. Data may be passed into eval_hprod via the structure userdata.

eval_shprod

is a user-supplied function that must have the following signature:

function eval_shprod(n, x, nnz_v, index_nz_v, v, nnz_u,
                     index_nz_u, u, got_h, userdata)

The product \(u = \nabla_{xx}f(x) v\) of the Hessian \(\nabla_{xx}f(x)\) of the objective function evaluated at \(x\) with the sparse vector v=\(v\) must be returned in u, and the function return value set to 0. Only the components index_nz_v[0:nnz_v-1] of v are nonzero, and the remaining components may not have been be set. On exit, the user must indicate the nnz_u indices of u that are nonzero in index_nz_u[0:nnz_u-1], and only these components of u need be set. If the evaluation is impossible at x, return should be set to a nonzero value. The Hessian has already been evaluated or used at x if the Bool got_h is true. Data may be passed into eval_prec via the structure userdata.

eval_prec

is an optional user-supplied function that may be C_NULL. If non-NULL, it must have the following signature:

function eval_prec(n, x, u, v, userdata)

The product \(u = P(x) v\) of the user’s preconditioner \(P(x)\) evaluated at \(x\) with the vector v=\(v\), the result \(u\) must be retured in u, and the function return value set to 0. If the evaluation is impossible at x, return should be set to a nonzero value. Data may be passed into eval_prec via the structure userdata.

    function bgo_solve_reverse_with_mat(data, status, eval_status,
                                        n, x, f, g, ne, H_val, u, v)

Find an approximation to the global minimizer of a given function subject to simple bounds on the variables using a multistart trust-region method.

This call is for the case where \(H = \nabla_{xx}f(x)\) is provided specifically, but function/derivative information is only available by returning to the calling procedure

Parameters:

data

holds private internal data

status

is a scalar variable of type Int32 that gives the entry and exit status from the package.

On initial entry, status must be set to 1.

Possible exit values are:

  • 0

    The run was successful

  • -1

    An allocation error occurred. A message indicating the offending array is written on unit control.error, and the returned allocation status and a string containing the name of the offending array are held in inform.alloc_status and inform.bad_alloc respectively.

  • -2

    A deallocation error occurred. A message indicating the offending array is written on unit control.error and the returned allocation status and a string containing the name of the offending array are held in inform.alloc_status and inform.bad_alloc respectively.

  • -3

    The restriction n > 0 or requirement that type contains its relevant string ‘dense’, ‘coordinate’, ‘sparse_by_rows’, ‘diagonal’ or ‘absent’ has been violated.

  • -7

    The objective function appears to be unbounded from below

  • -9

    The analysis phase of the factorization failed; the return status from the factorization package is given in the component inform.factor_status

  • -10

    The factorization failed; the return status from the factorization package is given in the component inform.factor_status.

  • -11

    The solution of a set of linear equations using factors from the factorization package failed; the return status from the factorization package is given in the component inform.factor_status.

  • -16

    The problem is so ill-conditioned that further progress is impossible.

  • -18

    Too many iterations have been performed. This may happen if control.maxit is too small, but may also be symptomatic of a badly scaled problem.

  • -19

    The CPU time limit has been reached. This may happen if control.cpu_time_limit is too small, but may also be symptomatic of a badly scaled problem.

  • -82

    The user has forced termination of solver by removing the file named control.alive_file from unit unit control.alive_unit.

  • 2

    The user should compute the objective function value \(f(x)\) at the point \(x\) indicated in x and then re-enter the function. The required value should be set in f, and eval_status should be set to 0. If the user is unable to evaluate \(f(x)\) for instance, if the function is undefined at \(x\) the user need not set f, but should then set eval_status to a non-zero value.

  • 3

    The user should compute the gradient of the objective function \(\nabla_x f(x)\) at the point \(x\) indicated in x and then re-enter the function. The value of the i-th component of the gradient should be set in g[i], for i = 1, …, n and eval_status should be set to 0. If the user is unable to evaluate a component of \(\nabla_x f(x)\) for instance if a component of the gradient is undefined at \(x\) -the user need not set g, but should then set eval_status to a non-zero value.

  • 4

    The user should compute the Hessian of the objective function \(\nabla_{xx}f(x)\) at the point x indicated in \(x\) and then re-enter the function. The value l-th component of the Hessian stored according to the scheme input in the remainder of \(H\) should be set in H_val[l], for l = 0, …, ne-1 and eval_status should be set to 0. If the user is unable to evaluate a component of \(\nabla_{xx}f(x)\) for instance, if a component of the Hessian is undefined at \(x\) the user need not set H_val, but should then set eval_status to a non-zero value.

  • 5

    The user should compute the product \(\nabla_{xx}f(x)v\) of the Hessian of the objective function \(\nabla_{xx}f(x)\) at the point \(x\) indicated in x with the vector \(v\), add the result to the vector \(u\) and then re-enter the function. The vectors \(u\) and \(v\) are given in u and v respectively, the resulting vector \(u + \nabla_{xx}f(x)v\) should be set in u and eval_status should be set to 0. If the user is unable to evaluate the product for instance, if a component of the Hessian is undefined at \(x\) the user need not alter u, but should then set eval_status to a non-zero value.

  • 6

    The user should compute the product \(u = P(x)v\) of their preconditioner \(P(x)\) at the point x indicated in \(x\) with the vector \(v\) and then re-enter the function. The vector \(v\) is given in v, the resulting vector \(u = P(x)v\) should be set in u and eval_status should be set to 0. If the user is unable to evaluate the product for instance, if a component of the preconditioner is undefined at \(x\) the user need not set u, but should then set eval_status to a non-zero value.

  • 23

    The user should follow the instructions for 2 and 3 above before returning.

  • 25

    The user should follow the instructions for 2 and 5 above before returning.

  • 35

    The user should follow the instructions for 3 and 5 above before returning.

  • 235

    The user should follow the instructions for 2, 3 and 5 above before returning.

eval_status

is a scalar variable of type Int32 that is used to indicate if objective function/gradient/Hessian values can be provided (see above)

n

is a scalar variable of type Int32 that holds the number of variables

x

is a one-dimensional array of size n and type T that holds the values \(x\) of the optimization variables. The j-th component of x, j = 1, … , n, contains \(x_j\).

f

is a scalar variable pointer of type T that holds the value of the objective function.

g

is a one-dimensional array of size n and type T that holds the gradient \(g = \nabla_xf(x)\) of the objective function. The j-th component of g, j = 1, … , n, contains \(g_j\).

ne

is a scalar variable of type Int32 that holds the number of entries in the lower triangular part of the Hessian matrix \(H\).

H_val

is a one-dimensional array of size ne and type T that holds the values of the entries of the lower triangular part of the Hessian matrix \(H\) in any of the available storage schemes.

u

is a one-dimensional array of size n and type T that is used for reverse communication (see above for details)

v

is a one-dimensional array of size n and type T that is used for reverse communication (see above for details)

    function bgo_solve_reverse_without_mat(data, status, eval_status,
                                            n, x, f, g, u, v, index_nz_v,
                                            nnz_v, index_nz_u, nnz_u)

Find an approximation to the global minimizer of a given function subject to simple bounds on the variables using a multistart trust-region method.

This call is for the case where access to \(H = \nabla_{xx}f(x)\) is provided by Hessian-vector products, but function/derivative information is only available by returning to the calling procedure.

Parameters:

data

holds private internal data

status

is a scalar variable of type Int32 that gives the entry and exit status from the package.

On initial entry, status must be set to 1.

Possible exit values are:

  • 0

    The run was successful

  • -1

    An allocation error occurred. A message indicating the offending array is written on unit control.error, and the returned allocation status and a string containing the name of the offending array are held in inform.alloc_status and inform.bad_alloc respectively.

  • -2

    A deallocation error occurred. A message indicating the offending array is written on unit control.error and the returned allocation status and a string containing the name of the offending array are held in inform.alloc_status and inform.bad_alloc respectively.

  • -3

    The restriction n > 0 or requirement that type contains its relevant string ‘dense’, ‘coordinate’, ‘sparse_by_rows’, ‘diagonal’ or ‘absent’ has been violated.

  • -7

    The objective function appears to be unbounded from below

  • -9

    The analysis phase of the factorization failed; the return status from the factorization package is given in the component inform.factor_status

  • -10

    The factorization failed; the return status from the factorization package is given in the component inform.factor_status.

  • -11

    The solution of a set of linear equations using factors from the factorization package failed; the return status from the factorization package is given in the component inform.factor_status.

  • -16

    The problem is so ill-conditioned that further progress is impossible.

  • -18

    Too many iterations have been performed. This may happen if control.maxit is too small, but may also be symptomatic of a badly scaled problem.

  • -19

    The CPU time limit has been reached. This may happen if control.cpu_time_limit is too small, but may also be symptomatic of a badly scaled problem.

  • -82

    The user has forced termination of solver by removing the file named control.alive_file from unit unit control.alive_unit.

  • 2

    The user should compute the objective function value \(f(x)\) at the point \(x\) indicated in x and then re-enter the function. The required value should be set in f, and eval_status should be set to 0. If the user is unable to evaluate \(f(x)\) for instance, if the function is undefined at \(x\) the user need not set f, but should then set eval_status to a non-zero value.

  • 3

    The user should compute the gradient of the objective function \(\nabla_x f(x)\) at the point \(x\) indicated in x and then re-enter the function. The value of the i-th component of the gradient should be set in g[i], for i = 1, …, n and eval_status should be set to 0. If the user is unable to evaluate a component of \(\nabla_x f(x)\) for instance if a component of the gradient is undefined at \(x\) -the user need not set g, but should then set eval_status to a non-zero value.

  • 5

    The user should compute the product \(\nabla_{xx}f(x)v\) of the Hessian of the objective function \(\nabla_{xx}f(x)\) at the point \(x\) indicated in x with the vector \(v\), add the result to the vector \(u\) and then re-enter the function. The vectors \(u\) and \(v\) are given in u and v respectively, the resulting vector \(u + \nabla_{xx}f(x)v\) should be set in u and eval_status should be set to 0. If the user is unable to evaluate the product for instance, if a component of the Hessian is undefined at \(x\) the user need not alter u, but should then set eval_status to a non-zero value.

  • 6

    The user should compute the product \(u = P(x)v\) of their preconditioner \(P(x)\) at the point x indicated in \(x\) with the vector \(v\) and then re-enter the function. The vector \(v\) is given in v, the resulting vector \(u = P(x)v\) should be set in u and eval_status should be set to 0. If the user is unable to evaluate the product for instance, if a component of the preconditioner is undefined at \(x\) the user need not set u, but should then set eval_status to a non-zero value.

  • 7

    The user should compute the product \(u = \nabla_{xx}f(x)v\) of the Hessian of the objective function \(\nabla_{xx}f(x)\) at the point \(x\) indicated in x with the sparse vector v=\(v\) and then re-enter the function. The nonzeros of \(v\) are stored in v[index_nz_v[0:nnz_v-1]] while the nonzeros of \(u\) should be returned in u[index_nz_u[0:nnz_u-1]]; the user must set nnz_u and index_nz_u accordingly, and set eval_status to 0. If the user is unable to evaluate the product for instance, if a component of the Hessian is undefined at \(x\) the user need not alter u, but should then set eval_status to a non-zero value.

  • 23

    The user should follow the instructions for 2 and 3 above before returning.

  • 25

    The user should follow the instructions for 2 and 5 above before returning.

  • 35

    The user should follow the instructions for 3 and 5 above before returning.

  • 235

    The user should follow the instructions for 2, 3 and 5 above before returning.

eval_status

is a scalar variable of type Int32 that is used to indicate if objective function/gradient/Hessian values can be provided (see above)

n

is a scalar variable of type Int32 that holds the number of variables

x

is a one-dimensional array of size n and type T that holds the values \(x\) of the optimization variables. The j-th component of x, j = 1, … , n, contains \(x_j\).

f

is a scalar variable pointer of type T that holds the value of the objective function.

g

is a one-dimensional array of size n and type T that holds the gradient \(g = \nabla_xf(x)\) of the objective function. The j-th component of g, j = 1, … , n, contains \(g_j\).

u

is a one-dimensional array of size n and type T that is used for reverse communication (see status=5,6,7 above for details)

v

is a one-dimensional array of size n and type T that is used for reverse communication (see status=5,6,7 above for details)

index_nz_v

is a one-dimensional array of size n and type Int32 that is used for reverse communication (see status=7 above for details)

nnz_v

is a scalar variable of type Int32 that is used for reverse communication (see status=7 above for details)

index_nz_u

is a one-dimensional array of size n and type Int32 that is used for reverse communication (see status=7 above for details)

nnz_u

is a scalar variable of type Int32 that is used for reverse communication (see status=7 above for details). On initial (status=1) entry, nnz_u should be set to an (arbitrary) nonzero value, and nnz_u=0 is recommended

    function bgo_information(data, inform, status)

Provides output information

Parameters:

data

holds private internal data

inform

is a structure containing output information (see bgo_inform_type)

status

is a scalar variable of type Int32 that gives the exit status from the package. Possible values are (currently):

  • 0

    The values were recorded successfully

    function bgo_terminate(data, control, inform)

Deallocate all internal private storage

Parameters:

data

holds private internal data

control

is a structure containing control information (see bgo_control_type)

inform

is a structure containing output information (see bgo_inform_type)

available structures#

bgo_control_type structure#

    struct bgo_control_type{T}
      f_indexing::Bool
      error::Int32
      out::Int32
      print_level::Int32
      attempts_max::Int32
      max_evals::Int32
      sampling_strategy::Int32
      hypercube_discretization::Int32
      alive_unit::Int32
      alive_file::NTuple{31,Cchar}
      infinity::T
      obj_unbounded::T
      cpu_time_limit::T
      clock_time_limit::T
      random_multistart::Bool
      hessian_available::Bool
      space_critical::Bool
      deallocate_error_fatal::Bool
      prefix::NTuple{31,Cchar}
      ugo_control::ugo_control_type{T}
      lhs_control::lhs_control_type
      trb_control::trb_control_type{T}

detailed documentation#

control derived type as a Julia structure

components#

Bool f_indexing

use C or Fortran sparse matrix indexing

Int32 error

error and warning diagnostics occur on stream error

Int32 out

general output occurs on stream out

Int32 print_level

the level of output required. Possible values are:

  • \(\leq\) 0 no output,

  • 1 a one-line summary for every improvement

  • 2 a summary of each iteration

  • \(\geq\) 3 increasingly verbose (debugging) output

Int32 attempts_max

the maximum number of random searches from the best point found so far

Int32 max_evals

the maximum number of function evaluations made

Int32 sampling_strategy

sampling strategy used. Possible values are

  • 1 uniformly spread

  • 2 Latin hypercube sampling

  • 3 niformly spread within a Latin hypercube

Int32 hypercube_discretization

hyper-cube discretization (for sampling stategies 2 and 3)

Int32 alive_unit

removal of the file alive_file from unit alive_unit terminates execution

char alive_file[31]

see alive_unit

T infinity

any bound larger than infinity in modulus will be regarded as infinite

T obj_unbounded

the smallest value the objective function may take before the problem is marked as unbounded

T cpu_time_limit

the maximum CPU time allowed (-ve means infinite)

T clock_time_limit

the maximum elapsed clock time allowed (-ve means infinite)

Bool random_multistart

perform random-multistart as opposed to local minimize and probe

Bool hessian_available

is the Hessian matrix of second derivatives available or is access only via matrix-vector products?

Bool space_critical

if .space_critical true, every effort will be made to use as little space as possible. This may result in longer computation time

Bool deallocate_error_fatal

if .deallocate_error_fatal is true, any array/pointer deallocation error will terminate execution. Otherwise, computation will continue

NTuple{31,Cchar} prefix

all output lines will be prefixed by .prefix(2:LEN(TRIM(.prefix))-1) where .prefix contains the required string enclosed in quotes, e.g. “string” or ‘string’

struct ugo_control_type ugo_control

control parameters for UGO

struct lhs_control_type lhs_control

control parameters for LHS

struct trb_control_type trb_control

control parameters for TRB

bgo_time_type structure#

    struct bgo_time_type{T}
      total::Float32
      univariate_global::Float32
      multivariate_local::Float32
      clock_total::T
      clock_univariate_global::T
      clock_multivariate_local::T

detailed documentation#

time derived type as a Julia structure

components#

Float32 total

the total CPU time spent in the package

Float32 univariate_global

the CPU time spent performing univariate global optimization

Float32 multivariate_local

the CPU time spent performing multivariate local optimization

T clock_total

the total clock time spent in the package

T clock_univariate_global

the clock time spent performing univariate global optimization

T clock_multivariate_local

the clock time spent performing multivariate local optimization

bgo_inform_type structure#

#include <galahad_bgo.h>

    struct bgo_inform_type{T}
      status::Int32
      alloc_status::Int32
      bad_alloc::NTuple{81,Cchar}
      f_eval::Int32
      g_eval::Int32
      h_eval::Int32
      obj::T
      norm_pg::T
      time::bgo_time_type{T}
      ugo_inform::ugo_inform_type{T}
      lhs_inform::lhs_inform_type
      trb_inform::trb_inform_type{T}

detailed documentation#

inform derived type as a Julia structure

components#

Int32 status

return status. See BGO_solve for details

Int32 alloc_status

the status of the last attempted allocation/deallocation

NTuple{81,Cchar} bad_alloc

the name of the array for which an allocation/deallocation error occurred

Int32 f_eval

the total number of evaluations of the objective function

Int32 g_eval

the total number of evaluations of the gradient of the objective function

Int32 h_eval

the total number of evaluations of the Hessian of the objective function

T obj

the value of the objective function at the best estimate of the solution determined by BGO_solve

T norm_pg

the norm of the projected gradient of the objective function at the best estimate of the solution determined by BGO_solve

struct bgo_time_type time

timings (see above)

struct ugo_inform_type ugo_inform

inform parameters for UGO

struct lhs_inform_type lhs_inform

inform parameters for LHS

struct trb_inform_type trb_inform

inform parameters for TRB

example calls#

This is an example of how to use the package to minimize a multi-dimensional objective within a box; the code is available in $GALAHAD/src/bgo/Julia/test_bgo.jl . A variety of supported Hessian and constraint matrix storage formats are shown.

# test_bgo.jl
# Simple code to test the Julia interface to BGO

using GALAHAD
using Test
using Printf
using Accessors

# Custom userdata struct
struct userdata_bgo
  p::Float64
  freq::Float64
  mag::Float64
end

function test_bgo()
  # Objective function
  function fun(n::Int, x::Vector{Float64}, f::Ref{Float64}, userdata::userdata_bgo)
    p = userdata.p
    freq = userdata.freq
    mag = userdata.mag
    f[] = (x[1] + x[3] + p)^2 + (x[2] + x[3])^2 + mag * cos(freq * x[1]) + sum(x)
    return 0
  end

  # Gradient of the objective
  function grad(n::Int, x::Vector{Float64}, g::Vector{Float64}, userdata::userdata_bgo)
    p = userdata.p
    freq = userdata.freq
    mag = userdata.mag
    g[1] = 2.0 * (x[1] + x[3] + p) - mag * freq * sin(freq * x[1]) + 1.0
    g[2] = 2.0 * (x[2] + x[3]) + 1.0
    g[3] = 2.0 * (x[1] + x[3] + p) + 2.0 * (x[2] + x[3]) + 1.0
    return 0
  end

  # Hessian of the objective
  function hess(n::Int, ne::Int, x::Vector{Float64}, hval::Vector{Float64},
                userdata::userdata_bgo)
    p = userdata.p
    freq = userdata.freq
    mag = userdata.mag
    hval[1] = 2.0 - mag * freq^2 * cos(freq * x[1])
    hval[2] = 2.0
    hval[3] = 2.0
    hval[4] = 4.0
    return 0
  end

  # Dense Hessian
  function hess_dense(n::Int, ne::Int, x::Vector{Float64}, hval::Vector{Float64},
                      userdata::userdata_bgo)
    p = userdata.p
    freq = userdata.freq
    mag = userdata.mag
    hval[1] = 2.0 - mag * freq^2 * cos(freq * x[1])
    hval[2] = 0.0
    hval[3] = 2.0
    hval[4] = 2.0
    hval[5] = 4.0
    return 0
  end

  # Hessian-vector product
  function hessprod(n::Int, x::Vector{Float64}, u::Vector{Float64}, v::Vector{Float64},
                    got_h::Bool, userdata::userdata_bgo)
    p = userdata.p
    freq = userdata.freq
    mag = userdata.mag
    u[1] = u[1] + 2.0 * (v[1] + v[3]) - mag * freq^2 * cos(freq * x[1]) * v[1]
    u[2] = u[2] + 2.0 * (v[2] + v[3])
    u[3] = u[3] + 2.0 * (v[1] + v[2] + 2.0 * v[3])
    return 0
  end

  # Sparse Hessian-vector product
  function shessprod(n::Int, x::Vector{Float64}, nnz_v::Cint, index_nz_v::Vector{Cint},
                     v::Vector{Float64}, nnz_u::Ref{Cint}, index_nz_u::Vector{Cint},
                     u::Vector{Float64}, got_h::Bool, userdata::userdata_bgo)
    p = userdata.p
    freq = userdata.freq
    mag = userdata.mag
    p = zeros(Float64, 3)
    used = falses(3)
    for i in 1:nnz_v
      j = index_nz_v[i]
      if j == 1
        p[1] = p[1] + 2.0 * v[1] - mag * freq^2 * cos(freq * x[1]) * v[1]
        used[1] = true
        p[3] = p[3] + 2.0 * v[1]
        used[3] = true
      elseif j == 2
        p[2] = p[2] + 2.0 * v[2]
        used[2] = true
        p[3] = p[3] + 2.0 * v[2]
        used[3] = true
      elseif j == 3
        p[1] = p[1] + 2.0 * v[3]
        used[1] = true
        p[2] = p[2] + 2.0 * v[3]
        used[2] = true
        p[3] = p[3] + 4.0 * v[3]
        used[3] = true
      end
    end

    nnz_u[] = 0
    for j in 1:3
      if used[j]
        u[j] = p[j]
        nnz_u[] += 1
        index_nz_u[nnz_u[]] = j
      end
    end
    return 0
  end

  # Apply preconditioner
  function prec(n::Int, x::Vector{Float64}, u::Vector{Float64}, v::Vector{Float64},
                userdata::userdata_bgo)
    u[1] = 0.5 * v[1]
    u[2] = 0.5 * v[2]
    u[3] = 0.25 * v[3]
    return 0
  end

  # Objective function
  function fun_diag(n::Int, x::Vector{Float64}, f::Ref{Float64}, userdata::userdata_bgo)
    p = userdata.p
    freq = userdata.freq
    mag = userdata.mag

    f[] = (x[3] + p)^2 + x[2]^2 + mag * cos(freq * x[1]) + x[1] + x[2] + x[3]
    return 0
  end

  # Gradient of the objective
  function grad_diag(n::Int, x::Vector{Float64}, g::Vector{Float64}, userdata::userdata_bgo)
    p = userdata.p
    freq = userdata.freq
    mag = userdata.mag

    g[1] = -mag * freq * sin(freq * x[1]) + 1
    g[2] = 2.0 * x[2] + 1
    g[3] = 2.0 * (x[3] + p) + 1
    return 0
  end

  # Hessian of the objective
  function hess_diag(n::Int, ne::Int, x::Vector{Float64}, hval::Vector{Float64},
                     userdata::userdata_bgo)
    freq = userdata.freq
    mag = userdata.mag

    hval[1] = -mag * freq^2 * cos(freq * x[1])
    hval[2] = 2.0
    hval[3] = 2.0
    return 0
  end

  # Hessian-vector product
  function hessprod_diag(n::Int, x::Vector{Float64}, u::Vector{Float64}, v::Vector{Float64},
                         got_h::Bool, userdata::userdata_bgo)
    freq = userdata.freq
    mag = userdata.mag

    u[1] += -mag * freq^2 * cos(freq * x[1]) * v[1]
    u[2] += 2.0 * v[2]
    u[3] += 2.0 * v[3]
    return 0
  end

  # Sparse Hessian-vector product
  function shessprod_diag(n::Int, x::Vector{Float64}, nnz_v::Cint, index_nz_v::Vector{Cint},
                          v::Vector{Float64}, nnz_u::Ref{Cint}, index_nz_u::Vector{Cint},
                          u::Vector{Float64}, got_h::Bool, userdata::userdata_bgo)
    freq = userdata.freq
    mag = userdata.mag

    p = zeros(3)
    used = falses(3)
    for i in 1:nnz_v
      j = index_nz_v[i]
      if j == 1
        p[1] -= mag * freq^2 * cos(freq * x[1]) * v[1]
        used[1] = true
      elseif j == 2
        p[2] += 2.0 * v[2]
        used[2] = true
      elseif j == 3
        p[3] += 2.0 * v[3]
        used[3] = true
      end
    end
    nnz_u[] = 0
    for j in 1:3
      if used[j]
        u[j] = p[j]
        nnz_u[] += 1
        index_nz_u[nnz_u[]] = j
      end
    end
    return 0
  end

  # Derived types
  data = Ref{Ptr{Cvoid}}()
  control = Ref{bgo_control_type{Float64}}()
  inform = Ref{bgo_inform_type{Float64}}()

  # Set user data
  userdata = userdata_bgo(4.0, 10, 1000)

  # Set problem data
  n = 3 # dimension
  ne = 5 # Hesssian elements
  x_l = Float64[-10, -10, -10]
  x_u = Float64[0.5, 0.5, 0.5]
  H_row = Cint[1, 2, 3, 3, 3]  # Hessian H
  H_col = Cint[1, 2, 1, 2, 3]  # NB lower triangle
  H_ptr = Cint[1, 2, 3, 6]  # row pointers

  # Set storage
  g = zeros(Float64, n) # gradient
  st = ' '
  status = Ref{Cint}()

  @printf(" Fortran sparse matrix indexing\n\n")
  @printf(" tests reverse-communication options\n\n")

  # reverse-communication input/output
  eval_status = Ref{Cint}()
  nnz_u = Ref{Cint}()
  nnz_v = Ref{Cint}()
  f = Ref{Float64}(0.0)
  u = zeros(Float64, n)
  v = zeros(Float64, n)
  index_nz_u = zeros(Cint, n)
  index_nz_v = zeros(Cint, n)
  H_val = zeros(Float64, ne)
  H_dense = zeros(Float64, div(n * (n + 1), 2))
  H_diag = zeros(Float64, n)

  for d in 1:5

    # Initialize BGO
    bgo_initialize(data, control, status)

    # Set user-defined control options
    @reset control[].f_indexing = true # Fortran sparse matrix indexing
    @reset control[].attempts_max = Cint(10000)
    @reset control[].max_evals = Cint(20000)
    @reset control[].sampling_strategy = Cint(3)
    @reset control[].trb_control.maxit = Cint(100)
    # @reset control[].print_level = CInt(1)

    # Start from 0
    x = Float64[0.0, 0.0, 0.0]

    # sparse co-ordinate storage
    if d == 1
      st = 'C'
      bgo_import(control, data, status, n, x_l, x_u,
                 "coordinate", ne, H_row, H_col, C_NULL)

      terminated = false
      while !terminated # reverse-communication loop
        bgo_solve_reverse_with_mat(data, status, eval_status,
                                   n, x, f[], g, ne, H_val, u, v)
        if status[] == 0 # successful termination
          terminated = true
        elseif status[] < 0 # error exit
          terminated = true
        elseif status[] == 2 # evaluate f
          eval_status[] = fun(n, x, f, userdata)
        elseif status[] == 3 # evaluate g
          eval_status[] = grad(n, x, g, userdata)
        elseif status[] == 4 # evaluate H
          eval_status[] = hess(n, ne, x, H_val, userdata)
        elseif status[] == 5 # evaluate Hv product
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        elseif status[] == 6 # evaluate the product with P
          eval_status[] = prec(n, x, u, v, userdata)
        elseif status[] == 23 # evaluate f and g
          eval_status[] = fun(n, x, f, userdata)
          eval_status[] = grad(n, x, g, userdata)
        elseif status[] == 25 # evaluate f and Hv product
          eval_status[] = fun(n, x, f, userdata)
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        elseif status[] == 35 # evaluate g and Hv product
          eval_status[] = grad(n, x, g, userdata)
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        elseif status[] == 235 # evaluate f, g and Hv product
          eval_status[] = fun(n, x, f, userdata)
          eval_status[] = grad(n, x, g, userdata)
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        else
          @printf(" the value %1i of status should not occur\n",
                  status)
        end
      end
    end

    # sparse by rows
    if d == 2
      st = 'R'
      bgo_import(control, data, status, n, x_l, x_u,
                 "sparse_by_rows", ne, C_NULL, H_col, H_ptr)

      terminated = false
      while !terminated # reverse-communication loop
        bgo_solve_reverse_with_mat(data, status, eval_status,
                                   n, x, f[], g, ne, H_val, u, v)
        if status[] == 0 # successful termination
          terminated = true
        elseif status[] < 0 # error exit
          terminated = true
        elseif status[] == 2 # evaluate f
          eval_status[] = fun(n, x, f, userdata)
        elseif status[] == 3 # evaluate g
          eval_status[] = grad(n, x, g, userdata)
        elseif status[] == 4 # evaluate H
          eval_status[] = hess(n, ne, x, H_val, userdata)
        elseif status[] == 5 # evaluate Hv product
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        elseif status[] == 6 # evaluate the product with P
          eval_status[] = prec(n, x, u, v, userdata)
        elseif status[] == 23 # evaluate f and g
          eval_status[] = fun(n, x, f, userdata)
          eval_status[] = grad(n, x, g, userdata)
        elseif status[] == 25 # evaluate f and Hv product
          eval_status[] = fun(n, x, f, userdata)
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        elseif status[] == 35 # evaluate g and Hv product
          eval_status[] = grad(n, x, g, userdata)
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        elseif status[] == 235 # evaluate f, g and Hv product
          eval_status[] = fun(n, x, f, userdata)
          eval_status[] = grad(n, x, g, userdata)
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        else
          @printf(" the value %1i of status should not occur\n", status)
        end
      end
    end

    # dense
    if d == 3
      st = 'D'
      bgo_import(control, data, status, n, x_l, x_u,
                 "dense", ne, C_NULL, C_NULL, C_NULL)

      terminated = false
      while !terminated # reverse-communication loop
        bgo_solve_reverse_with_mat(data, status, eval_status,
                                   n, x, f[], g, div(n * (n + 1), 2),
                                   H_dense, u, v)
        if status[] == 0 # successful termination
          terminated = true
        elseif status[] < 0 # error exit
          terminated = true
        elseif status[] == 2 # evaluate f
          eval_status[] = fun(n, x, f, userdata)
        elseif status[] == 3 # evaluate g
          eval_status[] = grad(n, x, g, userdata)
        elseif status[] == 4 # evaluate H
          eval_status[] = hess_dense(n, div(n * (n + 1), 2), x, H_dense, userdata)
        elseif status[] == 5 # evaluate Hv product
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        elseif status[] == 6 # evaluate the product with P
          eval_status[] = prec(n, x, u, v, userdata)
        elseif status[] == 23 # evaluate f and g
          eval_status[] = fun(n, x, f, userdata)
          eval_status[] = grad(n, x, g, userdata)
        elseif status[] == 25 # evaluate f and Hv product
          eval_status[] = fun(n, x, f, userdata)
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        elseif status[] == 35 # evaluate g and Hv product
          eval_status[] = grad(n, x, g, userdata)
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        elseif status[] == 235 # evaluate f, g and Hv product
          eval_status[] = fun(n, x, f, userdata)
          eval_status[] = grad(n, x, g, userdata)
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        else
          @printf(" the value %1i of status should not occur\n", status)
        end
      end
    end

    # diagonal
    if d == 4
      st = 'I'
      bgo_import(control, data, status, n, x_l, x_u,
                 "diagonal", ne, C_NULL, C_NULL, C_NULL)

      terminated = false
      while !terminated # reverse-communication loop
        bgo_solve_reverse_with_mat(data, status, eval_status,
                                   n, x, f[], g, n, H_diag, u, v)
        if status[] == 0 # successful termination
          terminated = true
        elseif status[] < 0 # error exit
          terminated = true
        elseif status[] == 2 # evaluate f
          eval_status[] = fun_diag(n, x, f, userdata)
        elseif status[] == 3 # evaluate g
          eval_status[] = grad_diag(n, x, g, userdata)
        elseif status[] == 4 # evaluate H
          eval_status[] = hess_diag(n, n, x, H_diag, userdata)
        elseif status[] == 5 # evaluate Hv product
          eval_status[] = hessprod_diag(n, x, u, v, false, userdata)
        elseif status[] == 6 # evaluate the product with P
          eval_status[] = prec(n, x, u, v, userdata)
        elseif status[] == 23 # evaluate f and g
          eval_status[] = fun_diag(n, x, f, userdata)
          eval_status[] = grad_diag(n, x, g, userdata)
        elseif status[] == 25 # evaluate f and Hv product
          eval_status[] = fun_diag(n, x, f, userdata)
          eval_status[] = hessprod_diag(n, x, u, v, false,
                                        userdata)
        elseif status[] == 35 # evaluate g and Hv product
          eval_status[] = grad_diag(n, x, g, userdata)
          eval_status[] = hessprod_diag(n, x, u, v, false,
                                        userdata)
        elseif status[] == 235 # evaluate f, g and Hv product
          eval_status[] = fun_diag(n, x, f, userdata)
          eval_status[] = grad_diag(n, x, g, userdata)
          eval_status[] = hessprod_diag(n, x, u, v, false,
                                        userdata)
        else
          @printf(" the value %1i of status should not occur\n", status)
        end
      end
    end

    # access by products
    if d == 5
      st = 'P'
      bgo_import(control, data, status, n, x_l, x_u,
                 "absent", ne, C_NULL, C_NULL, C_NULL)

      nnz_u = Ref{Cint}(0)
      terminated = false
      while !terminated # reverse-communication loop
        bgo_solve_reverse_without_mat(data, status, eval_status,
                                      n, x, f[], g, u, v, index_nz_v,
                                      nnz_v, index_nz_u, nnz_u[])
        if status[] == 0 # successful termination
          terminated = true
        elseif status[] < 0 # error exit
          terminated = true
        elseif status[] == 2 # evaluate f
          eval_status[] = fun(n, x, f, userdata)
        elseif status[] == 3 # evaluate g
          eval_status[] = grad(n, x, g, userdata)
        elseif status[] == 5 # evaluate Hv product
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        elseif status[] == 6 # evaluate the product with P
          eval_status[] = prec(n, x, u, v, userdata)
        elseif status[] == 7 # evaluate sparse Hess-vect product
          eval_status[] = shessprod(n, x, nnz_v[], index_nz_v, v,
                                    nnz_u, index_nz_u, u,
                                    false, userdata)
        elseif status[] == 23 # evaluate f and g
          eval_status[] = fun(n, x, f, userdata)
          eval_status[] = grad(n, x, g, userdata)
        elseif status[] == 25 # evaluate f and Hv product
          eval_status[] = fun(n, x, f, userdata)
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        elseif status[] == 35 # evaluate g and Hv product
          eval_status[] = grad(n, x, g, userdata)
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        elseif status[] == 235 # evaluate f, g and Hv product
          eval_status[] = fun(n, x, f, userdata)
          eval_status[] = grad(n, x, g, userdata)
          eval_status[] = hessprod(n, x, u, v, false, userdata)
        else
          @printf(" the value %1i of status should not occur\n",
                  status)
        end
      end
    end

    # Record solution information
    bgo_information(data, inform, status)

    if inform[].status == 0
      @printf("%c:%6i evaluations. Optimal objective value = %5.2f status = %1i\n", st,
              inform[].f_eval, inform[].obj, inform[].status)
    else
      @printf("%c: BGO_solve exit status = %1i\n", st, inform[].status)
    end

    # @printf("x: ")
    # for i in 1:n
    #   @printf("%f ", x[i])
    # end
    # @printf("\n")
    # @printf("gradient: ")
    # for i in 1:n
    #  @printf("%f ", g[i])
    # end
    # @printf("\n")

    # Delete internal workspace
    bgo_terminate(data, control, inform)
  end
  return 0
end

@testset "BGO" begin
  @test test_bgo() == 0
end