LSExternalContext Class

class localsolver.LSExternalContext

Context of an external function. External context can be used to query and set properties of the associated external function.

If you know the bounds of your external function or if you know it cannot return a NaN value, we recommend you to specify it as LocalSolver is then able to improve its search process. But beware! If the evaluation of the function returns an unexpected value (like a value outside of the bounds), an exception will be thrown during the search.

Since:

9.5

See:

LSExpression.get_external_context()

Summary

Attributes

lower_bound

Lower bound of the external function.

upper_bound

Upper bound of the external function.

nanable

True if the function can return a NaN value, false otherwise.

Methods

get_lower_bound

Gets the lower bound of the exernal function.

get_upper_bound

Gets the upper bound of the external function.

set_lower_bound

Sets the lower bound of the external function.

set_upper_bound

Sets the upper bound of the external function.

is_nanable

Returns true if the function can return a NaN value.

set_nanable

Sets whether or not the function can return a NaN value.

enable_surrogate_modeling

Enables the surrogate modeling on the associated external function.

Instance methods

LSExternalContext.get_lower_bound()

Gets the lower bound of the exernal function. By default, this property is set to a large integer number or math.inf.

Returns:

Lower bound of the external function.

Return type:

int or double

LSExternalContext.get_upper_bound()

Gets the upper bound of the external function. By default, this property is set to a large integer number or math.inf.

Returns:

Upper bound of the external function.

LSExternalContext.set_lower_bound(lower_bound)

Sets the lower bound of the external function. If it is an array function, the bound will apply to all elements of the array. Only allowed in state LSState.MODELING.

Parameters:

lower_bound – Lower bound of the function.

LSExternalContext.set_upper_bound(upper_bound)

Sets the upper bound of the external function. If it is an array function, the bound will apply to all elements of the array. Only allowed in state LSState.MODELING.

Parameters:

upper_bound – Upper bound of the function.

LSExternalContext.is_nanable()

Returns true if the function can return a NaN value. Only allowed if the external function is a double function. By default, this property is set to true.

Returns:

True if the function can return a NaN value.

LSExternalContext.set_nanable(nanable)

Sets whether or not the function can return a NaN value. Only allowed if the external function is a double function. If it is an array function, this parameter will apply to all elements of the array. Only allowed in state LSState.MODELING.

Parameters:

nanable – True if the function can return a NaN value, false otherwise.

LSExternalContext.enable_surrogate_modeling()

Enables the surrogate modeling on the associated external function. This method changes the internal behavior of the solver: the solver now considers that the function is computationally expensive and therefore uses surrogate modeling. This method returns the LSSurrogateParameters which are used to parameterize various aspects of surrogate modeling. Note: Once the surrogate modeling is enabled, it cannot be disabled. Only allowed in state MODELING.

For more information, please see the section dedicated to surrogate modeling among the modeling features.

Returns:

Surrogate parameters.

Return type:

LSSurrogateParameters

Instance attributes

All get/set methods have their attribute counterpart. You can use them as shortcuts to improve the readability of your models and codes.

LSExternalContext.lower_bound

Lower bound of the external function. It is a shortcut for get_lower_bound() and set_lower_bound().

LSExternalContext.upper_bound

Upper bound of the external function. It is a shortcut for get_upper_bound() and set_upper_bound().

LSExternalContext.nanable

True if the function can return a NaN value, false otherwise. It is a shortcut for is_nanable() and set_nanable().