LocalSolver logo
is now
Hexaly logo

We're excited to share that we are moving forward. We're leaving behind the LocalSolver brand and transitioning to our new identity: Hexaly. This represents a leap forward in our mission to enable every organization to make better decisions faster when faced with operational and strategic challenges.

This page is for an old version of Hexaly Optimizer. We recommend that you update your version and read the documentation for the latest stable release.

LSNativeFunction Delegate

delegate double localsolver.LSNativeFunction(LSNativeContext context)

Native function delegate. To use your own functions with LocalSolver, you have to proceed in 3 steps:

  1. Implement the delegate in a method. The method must take the native context and must return a double value. This native context contains the values of the expressions passed to the function. A distinction is made between integer arguments (bool, int) and floating point arguments (double).
  2. Instanciate the function as an LSExpression with LSModel.CreateNativeFunction or the dedicated shortcut LSModel.Function.
  3. Pass arguments to your function and call it. For that, you have to create expressions of type LSOperator.Call. The first operand must be the LSExpression corresponding to your native function. The other operands must be LSExpressions that will be used as arguments. Their value will be made accessible to your native function through the native context.

Note 1: Most of the time your native function will be called when the solver is in state LSState.Running. Do not attempt to call any method of the solver (to retrieve statistics, values of LSExpressions or whatever) in that state or an exception will be thrown. The only accessible function is LocalSolver.Stop.

Note 2: Your functions must be thread-safe. According to the “nbThreads” parameter, LocalSolver can be multi-threaded. In that case, your native functions must be thread safe. If you cannot guarantee the thread-safety of your code, we strongly recommend you to limit the search of LocalSolver to one thread with LSParam.SetNbThreads.

See:LSModel.CreateNativeFunction
See:LSOperator.Call
See:LSOperator.NativeFunction