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

The creation of a modeler environment results in the creation of a dedicated LocalSolver environment as well. For more information on how to use the solver’s API, see the LocalSolver class.

Since:10.0

Summary

Attributes
solver Returns a reference to the LocalSolver environment associated with this modeler instance.
Methods
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.
get_solver Returns a reference to the LocalSolver environment associated with this modeler instance.
create_map Creates a LSPMap.
create_function Creates an external LSPFunction.
delete Deletes this modeler environment and all associated resources.

Instance methods

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.get_solver()

Returns a reference to the LocalSolver environment associated with this modeler instance.

Returns:The associated solver.
Return type:LocalSolver
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 a blackbox or 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 solver environment attached to the modeler (see LocalSolver.delete()). This method is automatically called when you use the LSPModeler in a ‘with’ statement.

Instance attributes

LSPModeler.solver

Returns a reference to the LocalSolver environment associated with this modeler instance. This is a shortcut for get_solver().