GALAHAD DGO package#

purpose#

The dgo package uses a deterministic partition-and-bound 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.

Although there are theoretical guarantees, these may require a large number of evaluations as the dimension and nonconvexity increase. The alternative package bgo may sometimes be preferred.

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

method#

Starting with the initial box \(x^l \leq x \leq x^u\), a sequence of boxes is generated by considering the current set, and partitioning a promising candidate into three equally-sized sub-boxes by splitting along one of the box dimensions. Each partition requires only a pair of new function and derivative evaluations, and these values, together with estimates of Lipschitz constants, makes it possible to remove other boxes from further consideration as soon as they cannot contain a global minimizer. Efficient control of the dictionary of vertices of the sub-boxes is handled using a suitable hashing procedure provided by HASH; each sub-box is indexed by the concatenated coordinates of a pair of opposite vertices. At various stages, local minimization in a promising sub-box, using TRB, may be used to improve the best-known upper bound on the global minimizer. If \(n=1\), the specialised univariate global minimization package UGO is called directly.

We reiterate that although there are theoretical guarantees, these may require a large number of evaluations as the dimension and nonconvexity increase. Thus the method should best be viewed as a heuristic to try to find a reasonable approximation of the global minimum.

references#

The global minimization method employed is an extension of that due to

Ya. D. Sergeyev and D. E. Kasov, ``A deterministic global optimization using smooth diagonal auxiliary functions’’, Communications in Nonlinear Science and Numerical Simulation, 21(1-3) (2015) 99-111.

but adapted to use 2nd derivatives, while in the special case when \(n=1\), a simplification based on the ideas in

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

is used instead. The generic bound-constrained trust-region method used for local minimization 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).

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 \(0 <= j <= i <= n-1\)) need be held. In this case the lower triangle should be stored by rows, that is component \(i * i / 2 + j\) of the storage array H_val will hold the value \(H_{ij}\) (and, by symmetry, \(H_{ji}\)) for \(0 <= j <= i <= n-1\). 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, \(0 <= l <= ne-1\), of \(H\), its row index i, column index j and value \(H_{ij}\), \(0 <= j <= i <= n-1\), 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) holds the total number of entries. The column indices j, \(0 <= j <= 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.

introduction to function calls#

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

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

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

  • dgo_import - set up problem data structures and fixed values

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

  • solve the problem by calling one of

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

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

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

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

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

  • dgo_terminate - deallocate data structures

See the examples section for illustrations of use.

callable functions#

overview of functions provided#

// typedefs

typedef float spc_;
typedef double rpc_;
typedef int ipc_;

// structs

struct dgo_control_type;
struct dgo_inform_type;
struct dgo_time_type;

// function calls

void dgo_initialize(void **data, struct dgo_control_type* control, ipc_ *status);
void dgo_read_specfile(struct dgo_control_type* control, const char specfile[]);

void dgo_import(
    struct dgo_control_type* control,
    void **data,
    ipc_ *status,
    ipc_ n,
    const rpc_ x_l[],
    const rpc_ x_u[],
    const char H_type[],
    ipc_ ne,
    const ipc_ H_row[],
    const ipc_ H_col[],
    const ipc_ H_ptr[]
);

void dgo_reset_control(
    struct dgo_control_type* control,
    void **data,
    ipc_ *status
);

void dgo_solve_with_mat(
    void **data,
    void *userdata,
    ipc_ *status,
    ipc_ n,
    rpc_ x[],
    rpc_ g[],
    ipc_ ne,
    ipc_(*)(ipc_, const rpc_[], rpc_*, const void*) eval_f,
    ipc_(*)(ipc_, const rpc_[], rpc_[], const void*) eval_g,
    ipc_(*)(ipc_, ipc_, const rpc_[], rpc_[], const void*) eval_h,
    ipc_(*)(ipc_, const rpc_[], rpc_[], const rpc_[], bool, const void*) eval_hprod,
    ipc_(*)(ipc_, const rpc_[], rpc_[], const rpc_[], const void*) eval_prec
);

void dgo_solve_without_mat(
    void **data,
    void *userdata,
    ipc_ *status,
    ipc_ n,
    rpc_ x[],
    rpc_ g[],
    ipc_(*)(ipc_, const rpc_[], rpc_*, const void*) eval_f,
    ipc_(*)(ipc_, const rpc_[], rpc_[], const void*) eval_g,
    ipc_(*)(ipc_, const rpc_[], rpc_[], const rpc_[], bool, const void*) eval_hprod,
    ipc_(*)(ipc_, const rpc_[], ipc_, const int[], const rpc_[], int*, int[], rpc_[], bool, const void*) eval_shprod,
    ipc_(*)(ipc_, const rpc_[], rpc_[], const rpc_[], const void*) eval_prec
);

void dgo_solve_reverse_with_mat(
    void **data,
    ipc_ *status,
    ipc_ *eval_status,
    ipc_ n,
    rpc_ x[],
    rpc_ f,
    rpc_ g[],
    ipc_ ne,
    rpc_ H_val[],
    const rpc_ u[],
    rpc_ v[]
);

void dgo_solve_reverse_without_mat(
    void **data,
    ipc_ *status,
    ipc_ *eval_status,
    ipc_ n,
    rpc_ x[],
    rpc_ f,
    rpc_ g[],
    rpc_ u[],
    rpc_ v[],
    ipc_ index_nz_v[],
    ipc_ *nnz_v,
    const ipc_ index_nz_u[],
    ipc_ nnz_u
);

void dgo_information(void **data, struct dgo_inform_type* inform, ipc_ *status);

void dgo_terminate(
    void **data,
    struct dgo_control_type* control,
    struct dgo_inform_type* inform
);

typedefs#

typedef float spc_

spc_ is real single precision

typedef double rpc_

rpc_ is the real working precision used, but may be changed to float by defining the preprocessor variable SINGLE.

typedef int ipc_

ipc_ is the default integer word length used, but may be changed to int64_t by defining the preprocessor variable INTEGER_64.

function calls#

void dgo_initialize(void **data, struct dgo_control_type* control, ipc_ *status)

Set default control values and initialize private data

Parameters:

data

holds private internal data

control

is a struct containing control information (see dgo_control_type)

status

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

  • 0

    The initialization was successful.

void dgo_read_specfile(struct dgo_control_type* control, const char 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/dgo/DGO.template. See also Table 2.1 in the Fortran documentation provided in $GALAHAD/doc/dgo.pdf for a list of how these keywords relate to the components of the control structure.

Parameters:

control

is a struct containing control information (see dgo_control_type)

specfile

is a character string containing the name of the specification file

void dgo_import(
    struct dgo_control_type* control,
    void **data,
    ipc_ *status,
    ipc_ n,
    const rpc_ x_l[],
    const rpc_ x_u[],
    const char H_type[],
    ipc_ ne,
    const ipc_ H_row[],
    const ipc_ H_col[],
    const ipc_ H_ptr[]
)

Import problem data into internal storage prior to solution.

Parameters:

control

is a struct whose members provide control paramters for the remaining prcedures (see dgo_control_type)

data

holds private internal data

status

is a scalar variable of type ipc_, 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 ipc_, that holds the number of variables.

x_l

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

x_u

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

H_type

is a one-dimensional array of type char 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 ipc_, 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 ipc_, 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 NULL

H_col

is a one-dimensional array of size ne and type ipc_, 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 NULL

H_ptr

is a one-dimensional array of size n+1 and type ipc_, 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 NULL

void dgo_reset_control(
    struct dgo_control_type* control,
    void **data,
    ipc_ *status
)

Reset control parameters after import if required.

Parameters:

control

is a struct whose members provide control paramters for the remaining prcedures (see dgo_control_type)

data

holds private internal data

status

is a scalar variable of type ipc_, 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

void dgo_solve_with_mat(
    void **data,
    void *userdata,
    ipc_ *status,
    ipc_ n,
    rpc_ x[],
    rpc_ g[],
    ipc_ ne,
    ipc_(*)(ipc_, const rpc_[], rpc_*, const void*) eval_f,
    ipc_(*)(ipc_, const rpc_[], rpc_[], const void*) eval_g,
    ipc_(*)(ipc_, ipc_, const rpc_[], rpc_[], const void*) eval_h,
    ipc_(*)(ipc_, const rpc_[], rpc_[], const rpc_[], bool, const void*) eval_hprod,
    ipc_(*)(ipc_, const rpc_[], rpc_[], const rpc_[], const void*) eval_prec
)

Find an approximation to the global minimizer of a given function subject to simple bounds on the variables using a partition-and-bound 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 ipc_, 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.

  • -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.

  • -91

    The hash table used to store the dictionary of vertices of the sub-boxes is full, and there is no room to increase it further.

  • -99

    The budget limit on function evaluations has been reached. This will happen if the limit control.max_evals is exceeded, and is quite normal for stochastic global-optimization methods. The user may explore increasing control.max_evals to see if that produces a lower value of the objective function, but there are unfortunately no guarantees.

n

is a scalar variable of type ipc_, that holds the number of variables

x

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

g

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

ne

is a scalar variable of type ipc_, 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:

ipc_ eval_f( ipc_ n, const rpc_ x[], rpc_ *f, const void *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:

ipc_ eval_g( ipc_ n, const rpc_ x[], rpc_ g[], const void *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:

ipc_ eval_h( ipc_ n, ipc_ ne, const rpc_ x[], rpc_ h[],
            const void *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 dgo_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 NULL. If non-NULL, it must have the following signature:

ipc_ eval_prec( ipc_ n, const rpc_ x[], rpc_ u[], const rpc_ v[],
               const void *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.

void dgo_solve_without_mat(
    void **data,
    void *userdata,
    ipc_ *status,
    ipc_ n,
    rpc_ x[],
    rpc_ g[],
    ipc_(*)(ipc_, const rpc_[], rpc_*, const void*) eval_f,
    ipc_(*)(ipc_, const rpc_[], rpc_[], const void*) eval_g,
    ipc_(*)(ipc_, const rpc_[], rpc_[], const rpc_[], bool, const void*) eval_hprod,
    ipc_(*)(ipc_, const rpc_[], ipc_, const int[], const rpc_[], int*, int[], rpc_[], bool, const void*) eval_shprod,
    ipc_(*)(ipc_, const rpc_[], rpc_[], const rpc_[], const void*) eval_prec
)

Find an approximation to the global minimizer of a given function subject to simple bounds on the variables using a partition-and-bound 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 ipc_, 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.

  • -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.

  • -99

    The budget limit on function evaluations has been reached. This will happen if the limit control.max_evals is exceeded, and is quite normal for stochastic global-optimization methods. The user may explore increasing control.max_evals to see if that produces a lower value of the objective function, but there are unfortunately no guarantees.

n

is a scalar variable of type ipc_, that holds the number of variables

x

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

g

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

eval_f

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

ipc_ eval_f( ipc_ n, const rpc_ x[], rpc_ *f, const void *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:

ipc_ eval_g( ipc_ n, const rpc_ x[], rpc_ g[], const void *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:

ipc_ eval_hprod( ipc_ n, const rpc_ x[], rpc_ u[], const rpc_ v[],
                bool got_h, const void *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 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:

ipc_ eval_shprod( ipc_ n, const rpc_ x[], ipc_ nnz_v,
                 const ipc_ index_nz_v[], const rpc_ v[],
                 ipc_ *nnz_u, ipc_ index_nz_u[], rpc_ u[],
                 bool got_h, const void *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 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 NULL. If non-NULL, it must have the following signature:

ipc_ eval_prec( ipc_ n, const rpc_ x[], rpc_ u[], const rpc_ v[],
               const void *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.

void dgo_solve_reverse_with_mat(
    void **data,
    ipc_ *status,
    ipc_ *eval_status,
    ipc_ n,
    rpc_ x[],
    rpc_ f,
    rpc_ g[],
    ipc_ ne,
    rpc_ H_val[],
    const rpc_ u[],
    rpc_ v[]
)

Find an approximation to the global minimizer of a given function subject to simple bounds on the variables using a partition-and-bound 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 ipc_, 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.

  • -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.

  • -99

    The budget limit on function evaluations has been reached. This will happen if the limit control.max_evals is exceeded, and is quite normal for stochastic global-optimization methods. The user may explore increasing control.max_evals to see if that produces a lower value of the objective function, but there are unfortunately no guarantees.

  • 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 g radient should be set in g[i], for i = 0, …, n-1 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 ipc_, that is used to indicate if objective function/gradient/Hessian values can be provided (see above)

n

is a scalar variable of type ipc_, that holds the number of variables

x

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

f

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

g

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

ne

is a scalar variable of type ipc_, 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 rpc_, 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 rpc_, that is used for reverse communication (see above for details)

v

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

void dgo_solve_reverse_without_mat(
    void **data,
    ipc_ *status,
    ipc_ *eval_status,
    ipc_ n,
    rpc_ x[],
    rpc_ f,
    rpc_ g[],
    rpc_ u[],
    rpc_ v[],
    ipc_ index_nz_v[],
    ipc_ *nnz_v,
    const ipc_ index_nz_u[],
    ipc_ nnz_u
)

Find an approximation to the global minimizer of a given function subject to simple bounds on the variables using a partition-and-bound 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 ipc_, 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.

  • -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.

  • -99

    The budget limit on function evaluations has been reached. This will happen if the limit control.max_evals is exceeded, and is quite normal for stochastic global-optimization methods. The user may explore increasing control.max_evals to see if that produces a lower value of the objective function, but there are unfortunately no guarantees.

  • 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 g radient should be set in g[i], for i = 0, …, n-1 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 ipc_, that is used to indicate if objective function/gradient/Hessian values can be provided (see above)

n

is a scalar variable of type ipc_, that holds the number of variables

x

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

f

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

g

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

u

is a one-dimensional array of size n and type rpc_, 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 rpc_, 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 ipc_, that is used for reverse communication (see status=7 above for details)

nnz_v

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

index_nz_u

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

nnz_u

is a scalar variable of type ipc_, 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

void dgo_information(void **data, struct dgo_inform_type* inform, ipc_ *status)

Provides output information

Parameters:

data

holds private internal data

inform

is a struct containing output information (see dgo_inform_type)

status

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

  • 0

    The values were recorded successfully

void dgo_terminate(
    void **data,
    struct dgo_control_type* control,
    struct dgo_inform_type* inform
)

Deallocate all internal private storage

Parameters:

data

holds private internal data

control

is a struct containing control information (see dgo_control_type)

inform

is a struct containing output information (see dgo_inform_type)

available structures#

dgo_control_type structure#

#include <galahad_dgo.h>

struct dgo_control_type {
    // components

    bool f_indexing;
    ipc_ error;
    ipc_ out;
    ipc_ print_level;
    ipc_ start_print;
    ipc_ stop_print;
    ipc_ print_gap;
    ipc_ maxit;
    ipc_ max_evals;
    ipc_ dictionary_size;
    ipc_ alive_unit;
    char alive_file[31];
    rpc_ infinity;
    rpc_ lipschitz_lower_bound;
    rpc_ lipschitz_reliability;
    rpc_ lipschitz_control;
    rpc_ stop_length;
    rpc_ stop_f;
    rpc_ obj_unbounded;
    rpc_ cpu_time_limit;
    rpc_ clock_time_limit;
    bool hessian_available;
    bool prune;
    bool perform_local_optimization;
    bool space_critical;
    bool deallocate_error_fatal;
    char prefix[31];
    struct hash_control_type hash_control;
    struct ugo_control_type ugo_control;
    struct trb_control_type trb_control;
};

detailed documentation#

control derived type as a C struct

components#

bool f_indexing

use C or Fortran sparse matrix indexing

ipc_ error

error and warning diagnostics occur on stream error

ipc_ out

general output occurs on stream out

ipc_ 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

ipc_ start_print

any printing will start on this iteration

ipc_ stop_print

any printing will stop on this iteration

ipc_ print_gap

the number of iterations between printing

ipc_ maxit

the maximum number of iterations performed

ipc_ max_evals

the maximum number of function evaluations made

ipc_ dictionary_size

the size of the initial hash dictionary

ipc_ alive_unit

removal of the file alive_file from unit alive_unit terminates execution

char alive_file[31]

see alive_unit

rpc_ infinity

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

rpc_ lipschitz_lower_bound

a small positive constant (<= 1e-6) that ensure that the estimted gradient Lipschitz constant is not too small

rpc_ lipschitz_reliability

the Lipschitz reliability parameter, the Lipschiz constant used will be a factor lipschitz_reliability times the largest value observed

rpc_ lipschitz_control

the reliablity control parameter, the actual reliability parameter used will be .lipschitz_reliability

  • MAX( 1, n - 1 ) * .lipschitz_control / iteration

rpc_ stop_length

the iteration will stop if the length, delta, of the diagonal in the box with the smallest-found objective function is smaller than .stop_length times that of the original bound box, delta_0

rpc_ stop_f

the iteration will stop if the gap between the best objective value found and the smallest lower bound is smaller than .stop_f

rpc_ obj_unbounded

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

rpc_ cpu_time_limit

the maximum CPU time allowed (-ve means infinite)

rpc_ clock_time_limit

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

bool hessian_available

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

bool prune

should boxes that cannot contain the global minimizer be pruned (i.e., removed from further consideration)?

bool perform_local_optimization

should approximate minimizers be impoved by judicious local minimization?

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

char prefix[31]

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 hash_control_type hash_control

control parameters for HASH

struct ugo_control_type ugo_control

control parameters for UGO

struct trb_control_type trb_control

control parameters for TRB

dgo_time_type structure#

#include <galahad_dgo.h>

struct dgo_time_type {
    // components

    spc_ total;
    spc_ univariate_global;
    spc_ multivariate_local;
    rpc_ clock_total;
    rpc_ clock_univariate_global;
    rpc_ clock_multivariate_local;
};

detailed documentation#

time derived type as a C struct

components#

spc_ total

the total CPU time spent in the package

spc_ univariate_global

the CPU time spent performing univariate global optimization

spc_ multivariate_local

the CPU time spent performing multivariate local optimization

rpc_ clock_total

the total clock time spent in the package

rpc_ clock_univariate_global

the clock time spent performing univariate global optimization

rpc_ clock_multivariate_local

the clock time spent performing multivariate local optimization

dgo_inform_type structure#

#include <galahad_dgo.h>

struct dgo_inform_type {
    // components

    ipc_ status;
    ipc_ alloc_status;
    char bad_alloc[81];
    ipc_ iter;
    ipc_ f_eval;
    ipc_ g_eval;
    ipc_ h_eval;
    rpc_ obj;
    rpc_ norm_pg;
    rpc_ length_ratio;
    rpc_ f_gap;
    char why_stop[2];
    struct dgo_time_type time;
    struct hash_inform_type hash_inform;
    struct ugo_inform_type ugo_inform;
    struct trb_inform_type trb_inform;
};

detailed documentation#

inform derived type as a C struct

components#

ipc_ status

return status. See DGO_solve for details

ipc_ alloc_status

the status of the last attempted allocation/deallocation

char bad_alloc[81]

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

ipc_ iter

the total number of iterations performed

ipc_ f_eval

the total number of evaluations of the objective function

ipc_ g_eval

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

ipc_ h_eval

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

rpc_ obj

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

rpc_ norm_pg

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

rpc_ length_ratio

the ratio of the final to the initial box lengths

rpc_ f_gap

the gap between the best objective value found and the lowest bound

char why_stop[2]

why did the iteration stop? This wil be ‘D’ if the box length is small enough, ‘F’ if the objective gap is small enough, and ‘ ‘ otherwise

struct dgo_time_type time

timings (see above)

struct hash_inform_type hash_inform

inform parameters for HASH

struct ugo_inform_type ugo_inform

inform parameters for UGO

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; the code is available in $GALAHAD/src/dgo/C/dgot.c . A variety of supported Hessian and constraint matrix storage formats are shown.

Notice that C-style indexing is used, and that this is flagged by setting control.f_indexing to false. The floating-point type rpc_ is set in galahad_precision.h to double by default, but to float if the preprocessor variable SINGLE is defined. Similarly, the integer type ipc_ from galahad_precision.h is set to int by default, but to int64_t if the preprocessor variable INTEGER_64 is defined.

/* dgot.c */
/* Full test for the DGO C interface using C sparse matrix indexing */

#include <stdio.h>
#include <math.h>
#include <string.h>
#include "galahad_precision.h"
#include "galahad_cfunctions.h"
#include "galahad_dgo.h"

// Custom userdata struct
struct userdata_type {
   rpc_ p;
   rpc_ freq;
   rpc_ mag;
};

// Function prototypes
ipc_ fun( ipc_ n, const rpc_ x[], rpc_ *f, const void * );
ipc_ grad( ipc_ n, const rpc_ x[], rpc_ g[], const void * );
ipc_ hess( ipc_ n, ipc_ ne, const rpc_ x[], rpc_ hval[], const void * );
ipc_ hess_dense( ipc_ n, ipc_ ne, const rpc_ x[], rpc_ hval[],
                 const void * );
ipc_ hessprod( ipc_ n, const rpc_ x[], rpc_ u[], const rpc_ v[],
              bool got_h, const void * );
ipc_ shessprod( ipc_ n, const rpc_ x[], ipc_ nnz_v, const ipc_ index_nz_v[],
               const rpc_ v[], ipc_ *nnz_u, ipc_ index_nz_u[], rpc_ u[],
               bool got_h, const void * );
ipc_ prec(ipc_ n, const rpc_ x[], rpc_ u[], const rpc_ v[],
          const void * );
ipc_ fun_diag(ipc_ n, const rpc_ x[], rpc_ *f, const void * );
ipc_ grad_diag(ipc_ n, const rpc_ x[], rpc_ g[], const void * );
ipc_ hess_diag(ipc_ n, ipc_ ne, const rpc_ x[], rpc_ hval[], const void * );
ipc_ hessprod_diag( ipc_ n, const rpc_ x[], rpc_ u[],
                    const rpc_ v[], bool got_h, const void * );
ipc_ shessprod_diag( ipc_ n, const rpc_ x[], ipc_ nnz_v, const ipc_ index_nz_v[],
                    const rpc_ v[], ipc_ *nnz_u, ipc_ index_nz_u[],
                    rpc_ u[], bool got_h, const void * );

int main(void) {

    // Derived types
    void *data;
    struct dgo_control_type control;
    struct dgo_inform_type inform;

    // Set user data
    struct userdata_type userdata;
    userdata.p = 4.0;
    userdata.freq = 10;
    userdata.mag = 1000;

    // Set problem data
    ipc_ n = 3; // dimension
    ipc_ ne = 5; // Hesssian elements
    rpc_ x_l[] = {-10,-10,-10};
    rpc_ x_u[] = {0.5,0.5,0.5};
    ipc_ H_row[] = {0, 1, 2, 2, 2}; // Hessian H
    ipc_ H_col[] = {0, 1, 0, 1, 2}; // NB lower triangle
    ipc_ H_ptr[] = {0, 1, 2, 5};    // row pointers

    // Set storage
    rpc_ g[n]; // gradient
    char st = ' ';
    ipc_ status;

    printf(" C sparse matrix indexing\n\n");

    printf(" tests options for all-in-one storage format\n\n");

    for(ipc_ d=1; d <= 5; d++){

        // Initialize DGO
        dgo_initialize( &data, &control, &status );
        strcpy(control.trb_control.trs_control.symmetric_linear_solver,"sytr ");
        strcpy(control.trb_control.trs_control.definite_linear_solver,"potr ");
        strcpy(control.trb_control.psls_control.definite_linear_solver,"potr ");

        // Set user-defined control options
        control.f_indexing = false; // C sparse matrix indexing
        control.maxit = 2500;
        // control.trb_control.maxit = 100;
        //control.print_level = 1;

        // Start from 0
        rpc_ x[] = {0,0,0};

        switch(d){
            case 1: // sparse co-ordinate storage
                st = 'C';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "coordinate", ne, H_row, H_col, NULL );
                dgo_solve_with_mat( &data, &userdata, &status, n, x, g,
                                    ne, fun, grad, hess, hessprod, prec );
                break;
            case 2: // sparse by rows
                st = 'R';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "sparse_by_rows", ne, NULL, H_col, H_ptr );
                dgo_solve_with_mat( &data, &userdata, &status, n, x, g,
                                    ne, fun, grad, hess, hessprod, prec );
                break;
            case 3: // dense
                st = 'D';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "dense", ne, NULL, NULL, NULL );
                dgo_solve_with_mat( &data, &userdata, &status, n, x, g,
                                    ne, fun, grad, hess_dense, hessprod, prec );
                break;
            case 4: // diagonal
                st = 'I';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "diagonal", ne, NULL, NULL, NULL );
                dgo_solve_with_mat( &data, &userdata, &status, n, x, g,
                                    ne, fun_diag, grad_diag, hess_diag,
                                    hessprod_diag, prec );
                break;
            case 5: // access by products
                st = 'P';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "absent", ne, NULL, NULL, NULL );
                dgo_solve_without_mat( &data, &userdata, &status, n, x, g,
                                       fun, grad, hessprod, shessprod, prec );
                break;
        }

        // Record solution information
        dgo_information( &data, &inform, &status );

        if(inform.status == 0){
            printf("%c:%6" i_ipc_ " evaluations. Optimal objective value = %5.2f"
              " status = %1" i_ipc_ "\n", st, inform.f_eval, inform.obj, inform.status);
        }else if(inform.status == -18){
            printf("%c:%6" i_ipc_ " evaluations. Best objective value = %5.2f"
              " status = %1" i_ipc_ "\n", st, inform.f_eval, inform.obj, inform.status);
        }else{
            printf("%c: DGO_solve exit status = %1" i_ipc_ "\n", st, inform.status);
        }
        //printf("x: ");
        //for(ipc_ i = 0; i < n; i++) printf("%f ", x[i]);
        //printf("\n");
        //printf("gradient: ");
        //for(ipc_ i = 0; i < n; i++) printf("%f ", g[i]);
        //printf("\n");

        // Delete internal workspace
        dgo_terminate( &data, &control, &inform );
    }

    printf("\n tests reverse-communication options\n\n");

    // reverse-communication input/output
    ipc_ eval_status, nnz_u, nnz_v;
    rpc_ f = 0.0;
    rpc_ u[n], v[n];
    ipc_ index_nz_u[n], index_nz_v[n];
    rpc_ H_val[ne], H_dense[n*(n+1)/2], H_diag[n];

    for(ipc_ d=1; d <= 5; d++){

        // Initialize DGO
        dgo_initialize( &data, &control, &status );
        strcpy(control.trb_control.trs_control.symmetric_linear_solver,"sytr ");
        strcpy(control.trb_control.trs_control.definite_linear_solver,"potr ");
        strcpy(control.trb_control.psls_control.definite_linear_solver,"potr ");

        // Set user-defined control options
        control.f_indexing = false; // C sparse matrix indexing
        control.maxit = 2500;
        // control.trb_control.maxit = 100;
        //control.print_level = 1;

        // Start from 0
        rpc_ x[] = {0,0,0};
        switch(d){
            case 1: // sparse co-ordinate storage
                st = 'C';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "coordinate", ne, H_row, H_col, NULL );
                while(true){ // reverse-communication loop
                    dgo_solve_reverse_with_mat( &data, &status, &eval_status,
                                                n, x, f, g, ne, H_val, u, v );
                    if(status == 0){ // successful termination
                        break;
                    }else if(status < 0){ // error exit
                        break;
                    }else if(status == 2){ // evaluate f
                        eval_status = fun( n, x, &f, &userdata );
                    }else if(status == 3){ // evaluate g
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 4){ // evaluate H
                        eval_status = hess( n, ne, x, H_val, &userdata );
                    }else if(status == 5){ // evaluate Hv product
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 6){ // evaluate the product with P
                        eval_status = prec( n, x, u, v, &userdata );
                    }else if(status == 23){ // evaluate f and g
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 25){ // evaluate f and Hv product
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 35){ // evaluate g and Hv product
                        eval_status = grad( n, x, g, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(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 %1" i_ipc_ " of status should not occur\n",
                               status );
                        break;
                    }
                }
                break;
            case 2: // sparse by rows
                st = 'R';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "sparse_by_rows", ne, NULL, H_col, H_ptr );
                while(true){ // reverse-communication loop
                    dgo_solve_reverse_with_mat( &data, &status, &eval_status,
                                                n, x, f, g, ne, H_val, u, v );
                    if(status == 0){ // successful termination
                        break;
                    }else if(status < 0){ // error exit
                        break;
                    }else if(status == 2){ // evaluate f
                        eval_status = fun( n, x, &f, &userdata );
                    }else if(status == 3){ // evaluate g
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 4){ // evaluate H
                        eval_status = hess( n, ne, x, H_val, &userdata );
                    }else if(status == 5){ // evaluate Hv product
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 6){ // evaluate the product with P
                        eval_status = prec( n, x, u, v, &userdata );
                    }else if(status == 23){ // evaluate f and g
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 25){ // evaluate f and Hv product
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 35){ // evaluate g and Hv product
                        eval_status = grad( n, x, g, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(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 %1" i_ipc_ " of status should not occur\n",
                               status);
                        break;
                    }
                }
                break;
            case 3: // dense
                st = 'D';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "dense", ne, NULL, NULL, NULL );
                while(true){ // reverse-communication loop
                    dgo_solve_reverse_with_mat( &data, &status, &eval_status,
                                                n, x, f, g, n*(n+1)/2,
                                                H_dense, u, v );
                    if(status == 0){ // successful termination
                        break;
                    }else if(status < 0){ // error exit
                        break;
                    }else if(status == 2){ // evaluate f
                        eval_status = fun( n, x, &f, &userdata );
                    }else if(status == 3){ // evaluate g
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 4){ // evaluate H
                        eval_status = hess_dense( n, n*(n+1)/2, x, H_dense,
                                                  &userdata );
                    }else if(status == 5){ // evaluate Hv product
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 6){ // evaluate the product with P
                        eval_status = prec( n, x, u, v, &userdata );
                    }else if(status == 23){ // evaluate f and g
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 25){ // evaluate f and Hv product
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 35){ // evaluate g and Hv product
                        eval_status = grad( n, x, g, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(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 %1" i_ipc_ " of status should not occur\n",
                               status);
                        break;
                    }
                }
                break;
            case 4: // diagonal
                st = 'I';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "diagonal", ne, NULL, NULL, NULL );
                while(true){ // reverse-communication loop
                    dgo_solve_reverse_with_mat( &data, &status, &eval_status,
                                                n, x, f, g, n, H_diag, u, v );
                    if(status == 0){ // successful termination
                        break;
                    }else if(status < 0){ // error exit
                        break;
                    }else if(status == 2){ // evaluate f
                        eval_status = fun_diag( n, x, &f, &userdata );
                    }else if(status == 3){ // evaluate g
                        eval_status = grad_diag( n, x, g, &userdata );
                    }else if(status == 4){ // evaluate H
                        eval_status = hess_diag( n, n, x, H_diag, &userdata );
                    }else if(status == 5){ // evaluate Hv product
                        eval_status = hessprod_diag( n, x, u, v, false,
                                                     &userdata );
                    }else if(status == 6){ // evaluate the product with P
                        eval_status = prec( n, x, u, v, &userdata );
                    }else if(status == 23){ // evaluate f and g
                        eval_status = fun_diag( n, x, &f, &userdata );
                        eval_status = grad_diag( n, x, g, &userdata );
                    }else if(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 );
                    }else if(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 );
                    }else if(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 %1" i_ipc_ " of status should not occur\n",
                               status);
                        break;
                    }
                }
                break;
            case 5: // access by products
                st = 'P';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "absent", ne, NULL, NULL, NULL );
                nnz_u = 0;
                while(true){ // reverse-communication loop
                    dgo_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
                        break;
                    }else if(status < 0){ // error exit
                        break;
                    }else if(status == 2){ // evaluate f
                        eval_status = fun( n, x, &f, &userdata );
                    }else if(status == 3){ // evaluate g
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 5){ // evaluate Hv product
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 6){ // evaluate the product with P
                        eval_status = prec( n, x, u, v, &userdata );
                    }else if(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 );
                    }else if(status == 23){ // evaluate f and g
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 25){ // evaluate f and Hv product
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 35){ // evaluate g and Hv product
                        eval_status = grad( n, x, g, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(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 %1" i_ipc_ " of status should not occur\n",
                               status);
                        break;
                    }
                }
                break;
        }

        // Record solution information
        dgo_information( &data, &inform, &status );

        if(inform.status == 0){
            printf("%c:%6" i_ipc_ " evaluations. Optimal objective value = %5.2f"
              " status = %1" i_ipc_ "\n", st, inform.f_eval, inform.obj, inform.status);
        }else if(inform.status == -18){
            printf("%c:%6" i_ipc_ " evaluations. Best objective value = %5.2f"
              " status = %1" i_ipc_ "\n", st, inform.f_eval, inform.obj, inform.status);
        }else{
            printf("%c: DGO_solve exit status = %1" i_ipc_ "\n", st, inform.status);
        }
        //printf("x: ");
        //for(ipc_ i = 0; i < n; i++) printf("%f ", x[i]);
        //printf("\n");
        //printf("gradient: ");
        //for(ipc_ i = 0; i < n; i++) printf("%f ", g[i]);
        //printf("\n");

        // Delete internal workspace
        dgo_terminate( &data, &control, &inform );
    }

}

// Objective function
ipc_ fun( ipc_ n,
         const rpc_ x[],
         rpc_ *f,
         const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ p = myuserdata->p;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    *f = pow(x[0] + x[2] + p, 2) + pow(x[1] + x[2], 2) + mag * cos(freq*x[0])
         + x[0] + x[1] + x[2];
    return 0;
}

// Gradient of the objective
ipc_ grad( ipc_ n,
          const rpc_ x[],
          rpc_ g[],
          const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ p = myuserdata->p;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

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

// Hessian of the objective
ipc_ hess( ipc_ n,
          ipc_ ne,
          const rpc_ x[],
          rpc_ hval[],
          const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    hval[0] = 2.0 - mag * freq * freq * cos(freq*x[0]);
    hval[1] = 2.0;
    hval[2] = 2.0;
    hval[3] = 2.0;
    hval[4] = 4.0;
    return 0;
}

// Dense Hessian
ipc_ hess_dense( ipc_ n,
                ipc_ ne,
                const rpc_ x[],
                rpc_ hval[],
                const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    hval[0] = 2.0 - mag * freq * freq * cos(freq*x[0]);
    hval[1] = 0.0;
    hval[2] = 2.0;
    hval[3] = 2.0;
    hval[4] = 2.0;
    hval[5] = 4.0;
    return 0;
}

// Hessian-vector product
ipc_ hessprod( ipc_ n,
              const rpc_ x[],
              rpc_ u[],
              const rpc_ v[],
              bool got_h,
              const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    u[0] = u[0] + 2.0 * ( v[0] + v[2] )
           - mag * freq * freq * cos(freq*x[0]) * v[0];
    u[1] = u[1] + 2.0 * ( v[1] + v[2] );
    u[2] = u[2] + 2.0 * ( v[0] + v[1] + 2.0 * v[2] );
    return 0;
}

// Sparse Hessian-vector product
ipc_ shessprod( ipc_ n,
               const rpc_ x[],
               ipc_ nnz_v,
               const ipc_ index_nz_v[],
               const rpc_ v[],
               ipc_ *nnz_u,
               ipc_ index_nz_u[],
               rpc_ u[],
               bool got_h,
               const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    rpc_ p[] = {0., 0., 0.};
    bool used[] = {false, false, false};
    for(ipc_ i = 0; i < nnz_v; i++){
        ipc_ j = index_nz_v[i];
        switch(j){
            case 0:
                p[0] = p[0] + 2.0 * v[0]
                     - mag * freq * freq * cos(freq*x[0]) * v[0];
                used[0] = true;
                p[2] = p[2] + 2.0 * v[0];
                used[2] = true;
                break;
            case 1:
                p[1] = p[1] + 2.0 * v[1];
                used[1] = true;
                p[2] = p[2] + 2.0 * v[1];
                used[2] = true;
                break;
            case 2:
                p[0] = p[0] + 2.0 * v[2];
                used[0] = true;
                p[1] = p[1] + 2.0 * v[2];
                used[1] = true;
                p[2] = p[2] + 4.0 * v[2];
                used[2] = true;
                break;
        }
    }
    *nnz_u = 0;
    for(ipc_ j = 0; j < 3; j++){
        if(used[j]){
        u[j] = p[j];
        *nnz_u = *nnz_u + 1;
        index_nz_u[*nnz_u-1] = j;
        }
    }
    return 0;
}

// Apply preconditioner
ipc_ prec( ipc_ n,
          const rpc_ x[],
          rpc_ u[],
          const rpc_ v[],
          const void *userdata ){
   u[0] = 0.5 * v[0];
   u[1] = 0.5 * v[1];
   u[2] = 0.25 * v[2];
   return 0;
}

// Objective function
ipc_ fun_diag( ipc_ n,
              const rpc_ x[],
              rpc_ *f,
              const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ p = myuserdata->p;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    *f = pow(x[2] + p, 2) + pow(x[1], 2) + mag * cos(freq*x[0])
         + x[0] + x[1] + x[2];
    return 0;
}

// Gradient of the objective
ipc_ grad_diag( ipc_ n,
               const rpc_ x[],
               rpc_ g[],
               const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ p = myuserdata->p;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

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

// Hessian of the objective
ipc_ hess_diag( ipc_ n,
               ipc_ ne,
               const rpc_ x[],
               rpc_ hval[],
               const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    hval[0] = -mag * freq * freq * cos(freq*x[0]);
    hval[1] = 2.0;
    hval[2] = 2.0;
    return 0;
}

// Hessian-vector product
ipc_ hessprod_diag( ipc_ n,
                   const rpc_ x[],
                   rpc_ u[],
                   const rpc_ v[],
                   bool got_h,
                   const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    u[0] = u[0] + -mag * freq * freq * cos(freq*x[0]) * v[0];
    u[1] = u[1] + 2.0 * v[1];
    u[2] = u[2] + 2.0 * v[2];
    return 0;
}

// Sparse Hessian-vector product
ipc_ shessprod_diag( ipc_ n,
                    const rpc_ x[],
                    ipc_ nnz_v,
                    const ipc_ index_nz_v[],
                    const rpc_ v[],
                    ipc_ *nnz_u,
                    ipc_ index_nz_u[],
                    rpc_ u[],
                    bool got_h,
                    const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    rpc_ p[] = {0., 0., 0.};
    bool used[] = {false, false, false};
    for(ipc_ i = 0; i < nnz_v; i++){
        ipc_ j = index_nz_v[i];
        switch(j){
            case 0:
                p[0] = p[0] - mag * freq * freq * cos(freq*x[0]) * v[0];
                used[0] = true;
                break;
            case 1:
                p[1] = p[1] + 2.0 * v[1];
                used[1] = true;
                break;
            case 2:
                p[2] = p[2] + 2.0 * v[2];
                used[2] = true;
                break;
        }
    }
    *nnz_u = 0;
    for(ipc_ j = 0; j < 3; j++){
        if(used[j]){
        u[j] = p[j];
        *nnz_u = *nnz_u + 1;
        index_nz_u[*nnz_u-1] = j;
        }
    }
    return 0;
}

This is the same example, but now fortran-style indexing is used; the code is available in $GALAHAD/src/dgo/C/dgotf.c .

/* dgotf.c */
/* Full test for the DGO C interface using Fortran sparse matrix indexing */

#include <stdio.h>
#include <math.h>
#include <string.h>
#include "galahad_precision.h"
#include "galahad_cfunctions.h"
#include "galahad_dgo.h"

// Custom userdata struct
struct userdata_type {
   rpc_ p;
   rpc_ freq;
   rpc_ mag;
};

// Function prototypes
ipc_ fun( ipc_ n, const rpc_ x[], rpc_ *f, const void * );
ipc_ grad( ipc_ n, const rpc_ x[], rpc_ g[], const void * );
ipc_ hess( ipc_ n, ipc_ ne, const rpc_ x[], rpc_ hval[], const void * );
ipc_ hess_dense( ipc_ n, ipc_ ne, const rpc_ x[], rpc_ hval[],
                 const void * );
ipc_ hessprod( ipc_ n, const rpc_ x[], rpc_ u[], const rpc_ v[],
               bool got_h, const void * );
ipc_ shessprod( ipc_ n, const rpc_ x[], ipc_ nnz_v, const ipc_ index_nz_v[],
                const rpc_ v[], ipc_ *nnz_u, ipc_ index_nz_u[],
                rpc_ u[], bool got_h, const void * );
ipc_ prec( ipc_ n, const rpc_ x[], rpc_ u[], const rpc_ v[],
           const void * );
ipc_ fun_diag( ipc_ n, const rpc_ x[], rpc_ *f, const void * );
ipc_ grad_diag( ipc_ n, const rpc_ x[], rpc_ g[], const void * );
ipc_ hess_diag( ipc_ n, ipc_ ne, const rpc_ x[], rpc_ hval[],
               const void * );
ipc_ hessprod_diag( ipc_ n, const rpc_ x[], rpc_ u[],
                    const rpc_ v[], bool got_h, const void * );
ipc_ shessprod_diag( ipc_ n, const rpc_ x[], ipc_ nnz_v,
                     const ipc_ index_nz_v[],
                     const rpc_ v[], ipc_ *nnz_u, ipc_ index_nz_u[],
                     rpc_ u[], bool got_h, const void * );

int main(void) {

    // Derived types
    void *data;
    struct dgo_control_type control;
    struct dgo_inform_type inform;

    // Set user data
    struct userdata_type userdata;
    userdata.p = 4.0;
    userdata.freq = 10;
    userdata.mag = 1000;

    // Set problem data
    ipc_ n = 3; // dimension
    ipc_ ne = 5; // Hesssian elements
    rpc_ x_l[] = {-10,-10,-10};
    rpc_ x_u[] = {0.5,0.5,0.5};
    ipc_ H_row[] = {1, 2, 3, 3, 3}; // Hessian H
    ipc_ H_col[] = {1, 2, 1, 2, 3}; // NB lower triangle
    ipc_ H_ptr[] = {1, 2, 3, 6};    // row pointers

    // Set storage
    rpc_ g[n]; // gradient
    char st = ' ';
    ipc_ status;

    printf(" Fortran sparse matrix indexing\n\n");

    printf(" tests options for all-in-one storage format\n\n");

    for(ipc_ d=1; d <= 5; d++){

        // Initialize DGO
        dgo_initialize( &data, &control, &status );
        strcpy(control.trb_control.trs_control.symmetric_linear_solver,"sytr ");
        strcpy(control.trb_control.trs_control.definite_linear_solver,"potr ");
        strcpy(control.trb_control.psls_control.definite_linear_solver,"potr ");

        // Set user-defined control options
        control.f_indexing = true; // Fortran sparse matrix indexing
        control.maxit = 2500;
        // control.trb_control.maxit = 100;
        //control.print_level = 1;

        // Start from 0
        rpc_ x[] = {0,0,0};

        switch(d){
            case 1: // sparse co-ordinate storage
                st = 'C';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "coordinate", ne, H_row, H_col, NULL );
                dgo_solve_with_mat( &data, &userdata, &status, n, x, g,
                                    ne, fun, grad, hess, hessprod, prec );
                break;
            case 2: // sparse by rows
                st = 'R';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "sparse_by_rows", ne, NULL, H_col, H_ptr );
                dgo_solve_with_mat( &data, &userdata, &status, n, x, g,
                                    ne, fun, grad, hess, hessprod, prec );
                break;
            case 3: // dense
                st = 'D';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "dense", ne, NULL, NULL, NULL );
                dgo_solve_with_mat( &data, &userdata, &status, n, x, g,
                                    ne, fun, grad, hess_dense, hessprod, prec );
                break;
            case 4: // diagonal
                st = 'I';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "diagonal", ne, NULL, NULL, NULL );
                dgo_solve_with_mat( &data, &userdata, &status, n, x, g,
                                    ne, fun_diag, grad_diag, hess_diag,
                                    hessprod_diag, prec );
                break;
            case 5: // access by products
                st = 'P';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "absent", ne, NULL, NULL, NULL );
                dgo_solve_without_mat( &data, &userdata, &status, n, x, g,
                                       fun, grad, hessprod, shessprod, prec );
                break;
        }

        // Record solution information
        dgo_information( &data, &inform, &status );

        if(inform.status == 0){
            printf("%c:%6" i_ipc_ " evaluations. Optimal objective value = %5.2f"
              " status = %1" i_ipc_ "\n", st, inform.f_eval, inform.obj, inform.status);
        }else if(inform.status == -18){
            printf("%c:%6" i_ipc_ " evaluations. Best objective value = %5.2f"
              " status = %1" i_ipc_ "\n", st, inform.f_eval, inform.obj, inform.status);
        }else{
            printf("%c: DGO_solve exit status = %1" i_ipc_ "\n", st, inform.status);
        }
        //printf("x: ");
        //for(ipc_ i = 0; i < n; i++) printf("%f ", x[i]);
        //printf("\n");
        //printf("gradient: ");
        //for(ipc_ i = 0; i < n; i++) printf("%f ", g[i]);
        //printf("\n");

        // Delete internal workspace
        dgo_terminate( &data, &control, &inform );
    }

    printf("\n tests reverse-communication options\n\n");

    // reverse-communication input/output
    ipc_ eval_status, nnz_u, nnz_v;
    rpc_ f = 0.0;
    rpc_ u[n], v[n];
    ipc_ index_nz_u[n], index_nz_v[n];
    rpc_ H_val[ne], H_dense[n*(n+1)/2], H_diag[n];

    for(ipc_ d=1; d <= 5; d++){

        // Initialize DGO
        dgo_initialize( &data, &control, &status );
        strcpy(control.trb_control.trs_control.symmetric_linear_solver,"sytr ");
        strcpy(control.trb_control.trs_control.definite_linear_solver,"potr ");
        strcpy(control.trb_control.psls_control.definite_linear_solver,"potr ");

        // Set user-defined control options
        control.f_indexing = true; // Fortran sparse matrix indexing
        control.maxit = 2500;
        // control.trb_control.maxit = 100;
        //control.print_level = 1;

        // Start from 0
        rpc_ x[] = {0,0,0};
        switch(d){
            case 1: // sparse co-ordinate storage
                st = 'C';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "coordinate", ne, H_row, H_col, NULL );
                while(true){ // reverse-communication loop
                    dgo_solve_reverse_with_mat( &data, &status, &eval_status,
                                                n, x, f, g, ne, H_val, u, v );
                    if(status == 0){ // successful termination
                        break;
                    }else if(status < 0){ // error exit
                        break;
                    }else if(status == 2){ // evaluate f
                        eval_status = fun( n, x, &f, &userdata );
                    }else if(status == 3){ // evaluate g
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 4){ // evaluate H
                        eval_status = hess( n, ne, x, H_val, &userdata );
                    }else if(status == 5){ // evaluate Hv product
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 6){ // evaluate the product with P
                        eval_status = prec( n, x, u, v, &userdata );
                    }else if(status == 23){ // evaluate f and g
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 25){ // evaluate f and Hv product
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 35){ // evaluate g and Hv product
                        eval_status = grad( n, x, g, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(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 %1" i_ipc_ " of status should not occur\n",
                               status );
                        break;
                    }
                }
                break;
            case 2: // sparse by rows
                st = 'R';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "sparse_by_rows", ne, NULL, H_col, H_ptr );
                while(true){ // reverse-communication loop
                    dgo_solve_reverse_with_mat( &data, &status, &eval_status,
                                                n, x, f, g, ne, H_val, u, v );
                    if(status == 0){ // successful termination
                        break;
                    }else if(status < 0){ // error exit
                        break;
                    }else if(status == 2){ // evaluate f
                        eval_status = fun( n, x, &f, &userdata );
                    }else if(status == 3){ // evaluate g
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 4){ // evaluate H
                        eval_status = hess( n, ne, x, H_val, &userdata );
                    }else if(status == 5){ // evaluate Hv product
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 6){ // evaluate the product with P
                        eval_status = prec( n, x, u, v, &userdata );
                    }else if(status == 23){ // evaluate f and g
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 25){ // evaluate f and Hv product
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 35){ // evaluate g and Hv product
                        eval_status = grad( n, x, g, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(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 %1" i_ipc_ " of status should not occur\n",
                               status);
                        break;
                    }
                }
                break;
            case 3: // dense
                st = 'D';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "dense", ne, NULL, NULL, NULL );
                while(true){ // reverse-communication loop
                    dgo_solve_reverse_with_mat( &data, &status, &eval_status,
                                                n, x, f, g, n*(n+1)/2,
                                                H_dense, u, v );
                    if(status == 0){ // successful termination
                        break;
                    }else if(status < 0){ // error exit
                        break;
                    }else if(status == 2){ // evaluate f
                        eval_status = fun( n, x, &f, &userdata );
                    }else if(status == 3){ // evaluate g
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 4){ // evaluate H
                        eval_status = hess_dense( n, n*(n+1)/2, x, H_dense,
                                                  &userdata );
                    }else if(status == 5){ // evaluate Hv product
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 6){ // evaluate the product with P
                        eval_status = prec( n, x, u, v, &userdata );
                    }else if(status == 23){ // evaluate f and g
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 25){ // evaluate f and Hv product
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 35){ // evaluate g and Hv product
                        eval_status = grad( n, x, g, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(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 %1" i_ipc_ " of status should not occur\n",
                               status);
                        break;
                    }
                }
                break;
            case 4: // diagonal
                st = 'I';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "diagonal", ne, NULL, NULL, NULL );
                while(true){ // reverse-communication loop
                    dgo_solve_reverse_with_mat( &data, &status, &eval_status,
                                                n, x, f, g, n, H_diag, u, v );
                    if(status == 0){ // successful termination
                        break;
                    }else if(status < 0){ // error exit
                        break;
                    }else if(status == 2){ // evaluate f
                        eval_status = fun_diag( n, x, &f, &userdata );
                    }else if(status == 3){ // evaluate g
                        eval_status = grad_diag( n, x, g, &userdata );
                    }else if(status == 4){ // evaluate H
                        eval_status = hess_diag( n, n, x, H_diag, &userdata );
                    }else if(status == 5){ // evaluate Hv product
                        eval_status = hessprod_diag( n, x, u, v, false,
                                                     &userdata );
                    }else if(status == 6){ // evaluate the product with P
                        eval_status = prec( n, x, u, v, &userdata );
                    }else if(status == 23){ // evaluate f and g
                        eval_status = fun_diag( n, x, &f, &userdata );
                        eval_status = grad_diag( n, x, g, &userdata );
                    }else if(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 );
                    }else if(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 );
                    }else if(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 %1" i_ipc_ " of status should not occur\n",
                               status);
                        break;
                    }
                }
                break;
            case 5: // access by products
                st = 'P';
                dgo_import( &control, &data, &status, n, x_l, x_u,
                            "absent", ne, NULL, NULL, NULL );
                nnz_u = 0;
                while(true){ // reverse-communication loop
                    dgo_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
                        break;
                    }else if(status < 0){ // error exit
                        break;
                    }else if(status == 2){ // evaluate f
                        eval_status = fun( n, x, &f, &userdata );
                    }else if(status == 3){ // evaluate g
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 5){ // evaluate Hv product
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 6){ // evaluate the product with P
                        eval_status = prec( n, x, u, v, &userdata );
                    }else if(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 );
                    }else if(status == 23){ // evaluate f and g
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = grad( n, x, g, &userdata );
                    }else if(status == 25){ // evaluate f and Hv product
                        eval_status = fun( n, x, &f, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(status == 35){ // evaluate g and Hv product
                        eval_status = grad( n, x, g, &userdata );
                        eval_status = hessprod( n, x, u, v, false, &userdata );
                    }else if(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 %1" i_ipc_ " of status should not occur\n",
                               status);
                        break;
                    }
                }
                break;
        }

        // Record solution information
        dgo_information( &data, &inform, &status );

        if(inform.status == 0){
            printf("%c:%6" i_ipc_ " evaluations. Optimal objective value = %5.2f"
              " status = %1" i_ipc_ "\n", st, inform.f_eval, inform.obj, inform.status);
        }else if(inform.status == -18){
            printf("%c:%6" i_ipc_ " evaluations. Best objective value = %5.2f"
              " status = %1" i_ipc_ "\n", st, inform.f_eval, inform.obj, inform.status);
        }else{
            printf("%c: DGO_solve exit status = %1" i_ipc_ "\n", st, inform.status);
        }
        //printf("x: ");
        //for(ipc_ i = 0; i < n; i++) printf("%f ", x[i]);
        //printf("\n");
        //printf("gradient: ");
        //for(ipc_ i = 0; i < n; i++) printf("%f ", g[i]);
        //printf("\n");

        // Delete internal workspace
        dgo_terminate( &data, &control, &inform );
    }

}

// Objective function
ipc_ fun( ipc_ n,
         const rpc_ x[],
         rpc_ *f,
         const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ p = myuserdata->p;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    *f = pow(x[0] + x[2] + p, 2) + pow(x[1] + x[2], 2) + mag * cos(freq*x[0])
         + x[0] + x[1] + x[2];
    return 0;
}

// Gradient of the objective
ipc_ grad( ipc_ n,
          const rpc_ x[],
          rpc_ g[],
          const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ p = myuserdata->p;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

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

// Hessian of the objective
ipc_ hess( ipc_ n,
          ipc_ ne,
          const rpc_ x[],
          rpc_ hval[],
          const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    hval[0] = 2.0 - mag * freq * freq * cos(freq*x[0]);
    hval[1] = 2.0;
    hval[2] = 2.0;
    hval[3] = 2.0;
    hval[4] = 4.0;
    return 0;
}

// Dense Hessian
ipc_ hess_dense( ipc_ n,
                ipc_ ne,
                const rpc_ x[],
                rpc_ hval[],
                const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    hval[0] = 2.0 - mag * freq * freq * cos(freq*x[0]);
    hval[1] = 0.0;
    hval[2] = 2.0;
    hval[3] = 2.0;
    hval[4] = 2.0;
    hval[5] = 4.0;
    return 0;
}

// Hessian-vector product
ipc_ hessprod( ipc_ n,
              const rpc_ x[],
              rpc_ u[],
              const rpc_ v[],
              bool got_h,
              const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    u[0] = u[0] + 2.0 * ( v[0] + v[2] )
           - mag * freq * freq * cos(freq*x[0]) * v[0];
    u[1] = u[1] + 2.0 * ( v[1] + v[2] );
    u[2] = u[2] + 2.0 * ( v[0] + v[1] + 2.0 * v[2] );
    return 0;
}

// Sparse Hessian-vector product
ipc_ shessprod( ipc_ n,
               const rpc_ x[],
               ipc_ nnz_v,
               const ipc_ index_nz_v[],
               const rpc_ v[],
               ipc_ *nnz_u,
               ipc_ index_nz_u[],
               rpc_ u[],
               bool got_h,
               const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    rpc_ p[] = {0., 0., 0.};
    bool used[] = {false, false, false};
    for(ipc_ i = 0; i < nnz_v; i++){
        ipc_ j = index_nz_v[i];
        switch(j){
            case 1:
                p[0] = p[0] + 2.0 * v[0]
                     - mag * freq * freq * cos(freq*x[0]) * v[0];
                used[0] = true;
                p[2] = p[2] + 2.0 * v[0];
                used[2] = true;
                break;
            case 2:
                p[1] = p[1] + 2.0 * v[1];
                used[1] = true;
                p[2] = p[2] + 2.0 * v[1];
                used[2] = true;
                break;
            case 3:
                p[0] = p[0] + 2.0 * v[2];
                used[0] = true;
                p[1] = p[1] + 2.0 * v[2];
                used[1] = true;
                p[2] = p[2] + 4.0 * v[2];
                used[2] = true;
                break;
        }
    }
    *nnz_u = 0;
    for(ipc_ j = 0; j < 3; j++){
        if(used[j]){
        u[j] = p[j];
        *nnz_u = *nnz_u + 1;
        index_nz_u[*nnz_u-1] = j+1;
        }
    }
    return 0;
}

// Apply preconditioner
ipc_ prec( ipc_ n,
          const rpc_ x[],
          rpc_ u[],
          const rpc_ v[],
          const void *userdata ){
   u[0] = 0.5 * v[0];
   u[1] = 0.5 * v[1];
   u[2] = 0.25 * v[2];
   return 0;
}

// Objective function
ipc_ fun_diag( ipc_ n,
              const rpc_ x[],
              rpc_ *f,
              const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ p = myuserdata->p;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    *f = pow(x[2] + p, 2) + pow(x[1], 2) + mag * cos(freq*x[0])
         + x[0] + x[1] + x[2];
    return 0;
}

// Gradient of the objective
ipc_ grad_diag( ipc_ n,
               const rpc_ x[],
               rpc_ g[],
               const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ p = myuserdata->p;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

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

// Hessian of the objective
ipc_ hess_diag( ipc_ n,
               ipc_ ne,
               const rpc_ x[],
               rpc_ hval[],
               const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    hval[0] = -mag * freq * freq * cos(freq*x[0]);
    hval[1] = 2.0;
    hval[2] = 2.0;
    return 0;
}

// Hessian-vector product
ipc_ hessprod_diag( ipc_ n,
                   const rpc_ x[],
                   rpc_ u[],
                   const rpc_ v[],
                   bool got_h,
                   const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    u[0] = u[0] + -mag * freq * freq * cos(freq*x[0]) * v[0];
    u[1] = u[1] + 2.0 * v[1];
    u[2] = u[2] + 2.0 * v[2];
    return 0;
}

// Sparse Hessian-vector product
ipc_ shessprod_diag( ipc_ n,
                    const rpc_ x[],
                    ipc_ nnz_v,
                    const ipc_ index_nz_v[],
                    const rpc_ v[],
                    ipc_ *nnz_u,
                    ipc_ index_nz_u[],
                    rpc_ u[],
                    bool got_h,
                    const void *userdata ){
    struct userdata_type *myuserdata = (struct userdata_type *) userdata;
    rpc_ freq = myuserdata->freq;
    rpc_ mag = myuserdata->mag;

    rpc_ p[] = {0., 0., 0.};
    bool used[] = {false, false, false};
    for(ipc_ i = 0; i < nnz_v; i++){
        ipc_ j = index_nz_v[i];
        switch(j){
            case 1:
                p[0] = p[0] - mag * freq * freq * cos(freq*x[0]) * v[0];
                used[0] = true;
                break;
            case 2:
                p[1] = p[1] + 2.0 * v[1];
                used[1] = true;
                break;
            case 3:
                p[2] = p[2] + 2.0 * v[2];
                used[2] = true;
                break;
        }
    }
    *nnz_u = 0;
    for(ipc_ j = 0; j < 3; j++){
        if(used[j]){
        u[j] = p[j];
        *nnz_u = *nnz_u + 1;
        index_nz_u[*nnz_u-1] = j+1;
        }
    }
    return 0;
}