HxExternalFunction Interface

template<typename T>
class HxExternalFunction

External function interface. To use your own external functions within Hexaly Optimizer, you have to first define your external function in one of two ways:

  • Implement the HxExternalFunction interface with the call method. The call method must take a HxExternalArgumentValues as single argument and must return a hxint or hxdouble value. The argument values contain the values of the expressions passed to the function. A distinction is made between integer arguments (bool, int) and floating point arguments (double).

  • If your program is compiled with C++11, you can use a function, a functor or a lambda function instead. This function may take any number of hxint and hxdouble arguments, or alternatively a single HxExternalArgumentValues. It must return a hxint or hxdouble value.

Then:

  1. Instantiate the function as an HxExpression with HxModel::createExternalFunction() or the dedicated shortcut HxModel::externalFunction().

  2. Pass arguments to your function and call it using HxModel::call(). The first operand must the HxExpression returned by createExternalFunction. The other operands must be HxExpressions, whose values will be made accessible to your external function when it is called.

Note 1: Most of the time your external function will be called when the optimizer is in state S_Running. Do not attempt to call any method of the optimizer (to retrieve statistics, values of HxExpressions, etc.) in that state or an exception will be thrown.

Note 2: Your functions must be thread-safe. According to the “nbThreads” parameter, Hexaly Optimizer can be multi-threaded. In that case, your external functions must be thread safe. If you cannot guarantee the thread-safety of your code, we strongly recommend you to limit the search of Hexaly Optimizer to one thread with HxParam::setNbThreads().

Note 3: Hexaly Optimizer does not manage memory of objects created outside of its environment. You are responsible for the lifetime of your HxExternalFunction, which must last as long as the search is active.

Node 4: You can provide additional data for your function (such as lower and upper bounds) with the help of the HxExternalContext associated with your function (see HxExpression::getExternalContext()).

Since:

9.5

Summary

Functions

call

The function to call.

~HxExternalFunction

Default virtual destructor.

Functions

virtual T HxExternalFunction::call(const HxExternalArgumentValues &argumentValues) = 0

The function to call. The argument values contain the arguments to pass to your function.

Parameters:

argumentValues – Values of the arguments passed to the function.

Returns:

The value of the HxExpression.

virtual HxExternalFunction::~HxExternalFunction()

Default virtual destructor.