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.

LSPModeler Class

class LSPModeler

Modeler environment. Main class of the Modeler library which enables the creation and manipulation of a virtual machine that can load and execute programs written in the LSP language (see the language reference for more information).

Since

10.0

Summary

Methods

create_solver

Returns a new LocalSolver instance that can be used to launch a module with the method LSPModule.run().

load_module

Loads a program written in LSP format into a LSPModule whose name corresponds to the filename.

create_module

Creates an empty module with the given name.

create_map

Creates a LSPMap.

create_function

Creates an external LSPFunction.

delete

Deletes this modeler environment and all associated resources.

Instance methods

LSPModeler.create_solver()

Returns a new LocalSolver instance that can be used to launch a module with the method LSPModule.run(). The returned solver will be automatically disposed when the modeler is destroyed or when the current reference scope is released.

Returns

Solver.

Return type

LocalSolver

See

LocalSolver

LSPModeler.load_module(filepath)

Loads a program written in LSP format into a LSPModule whose name corresponds to the filename. The variables of the module can be manipulated through the returned module object.

Parameters

filepath (str) – Path to the file.

Returns

Module loaded.

Return type

LSPModule

LSPModeler.create_module(module_name)

Creates an empty module with the given name. The variables of the module can be manipulated through the return module object.

Parameters

module_name (str) – Name of the module.

Returns

Module created.

Return type

LSPModule

LSPModeler.create_map()

Creates a LSPMap. A map is a data structure mapping keys to values that can also be used as an array-like structure. The map can be assigned to any variable in a module with LSPModule.__setitem__() or can be part of another map with LSPMap.__setitem__().

Returns

Map created.

Return type

LSPMap

LSPModeler.create_function(name, function)
LSPModeler.create_function(function)

Creates an external LSPFunction. The provided function must take a modeler instance as first argument and the LSP function arguments as subsequent arguments. For instance, the following example creates a simple function that accepts two arguments and returns the sum of both values. The generated function is then exposed in an LSP module under the name “myCustomFunction”:

def my_custom_function(modeler, arg1, arg2):
    return arg1 + arg2

module["myCustomFunction"] = modeler.create_function(my_custom_function)

Note: This method should only be used to expose functions used during the modeling process. You should not use this method to create a function that will be used during the resolution as an external function. In this case, you should instead use the solver API directly (see LSModel.create_int_external_function() or LSModel.create_double_external_function() for example).

Parameters
  • function – A python function that accepts a modeler instance as first argument and the LSP function arguments as subsequent arguments. The function must returns a value whose type is supported by the modeler (None, int, float, str, LSPMap, LSPFunction, LSPModule, LSExpression)

  • name (str) – Name of the function (optional). The name is only used to identify the function in the generated stack trace when an exception occurs. Once created, the function can be associated with any variable in any module, regardless of its name.

Returns

Function created.

Return type

LSPFunction

LSPModeler.delete()

Deletes this modeler environment and all associated resources. This also deletes the solvers created using LSPModeler.create_solver(). This method is automatically called when you use the LSPModeler in a with statement.