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

See:

LocalSolver

See:

LSPModule

Since:

10.0

Summary

Methods

Dispose

Deletes this modeler environment.

CreateSolver

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

GetModule

Returns the module with the given name.

AddModuleLookupPath

Adds a new lookup path for modules.

ClearModuleLookupPaths

Deletes all paths used to search for modules.

LoadModule

Loads the module written in LSP at the indicated location.

CreateModule

Creates an empty module with the given name.

GetSolver

Returns the LocalSolver environment associated with this modeler instance.

CreateFunction

Creates an external LSPFunction.

CreateMap

Creates a LSPMap.

CreateNil

Creates a nil value.

CreateInt

Creates an integer value.

CreateDouble

Creates a double value.

CreateBool

Creates a boolean value.

CreateString

Creates a string value.

GetStdOut

Returns the writer used by the modeler for its standard output methods like Write or WriteLine.

SetStdOut

Sets the writer used by the modeler for its standard output methods like Write or WriteLine.

GetStdErr

Returns the writer used by the modeler for its standard error output.

SetStdErr

Sets the writer used by the modeler for its standard error output.

Instance methods

LSPModeler()

Constructs a complete modeler environment.

See:

LocalSolver

void Dispose()

Deletes this modeler environment. All references bound to this modeler will be automatically freed. This also includes solvers created using the LSPModeler.CreateSolver method.

LocalSolver CreateSolver()

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.

See:

localsolver.LocalSolver

LSPModule GetModule(string moduleName)

Returns the module with the given name. User modules can be accessed as well as builtin modules like the JSON module or the CSV module. If the module is not loaded, this method attempts to load it from the paths specified by AddModuleLookupPath.

The variables of the module can then be manipulated through the associated LSPModule object.

Arguments:

moduleName (string) – Module name.

Returns:

Module created.

Return type:

LSPModule

void AddModuleLookupPath(string path)

Adds a new lookup path for modules. The paths specified when calling this method are taken into account when the LSP virtual machine loads a module, either by a direct call to the GetModule method, or indirectly when loading a dependency to another module. By default, the lookup path contains at least the current runtime folder.

Arguments:

path (string) – New path to consider for modules.

void ClearModuleLookupPaths()

Deletes all paths used to search for modules. This method deletes all the paths previously added by AddModuleLookupPath method, as well as the default location(s) (including the current execution folder).

__Important note:__ after calling this method, you must at least add a path with AddModuleLookupPath. Without a path, you won’t be able to load any LSP modules other than the native ones supplied by the virtual machine.

LSPModule LoadModule(string moduleName, string filePath)

Loads the module written in LSP at the indicated location. The loaded module will take the name specified in parameter. Unlike GetModule, this method ignores all paths indicated by AddModuleLookupPath, and if a module with a similar name is already loaded, an exception will be thrown.

Once loaded, the variables of the module can be manipulated through the associated LSPModule` object.

Arguments:
  • moduleName (string) – Name taken by the module once loaded.

  • filePath (string) – Path to the module file.

Returns:

Module created.

Return type:

LSPModule

LSPModule CreateModule(string moduleName)

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

Arguments:

moduleName (string) – Name of the module.

Returns:

Module created.

Return type:

LSPModule

LocalSolver GetSolver()

Returns the LocalSolver environment associated with this modeler instance.

Returns:

Solver associated with this modeler.

Return type:

LocalSolver

LSPFunction CreateFunction(LSPFunctor functor)
LSPFunction CreateFunction(string name, LSPFunctor functor)

Creates an external LSPFunction. The argument must implement LSPFunctor. When the function is called, the modeler instance will be made accessible to the function, as well as the arguments in a list.

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”:

module.SetFunction("myCustomFunction", (modeler, arguments) => {
    return modeler.CreateDouble(arguments[0].AsDouble() + arguments[1].AsDouble());
});

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

Arguments:
  • functor – External function to call, passed as a delegate.

  • name (string) – 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.

See:

LSPFunctor

LSPMap CreateMap()

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 thanks to LSPModule.SetMap() or can be part of another map with LSPMap.SetMap().

Returns:

Map created.

See:

LSPMap

LSPMap CreateMap(IList<long> values)
LSPMap CreateMap(IList<double> values)
LSPMap CreateMap(IList<bool> values)
LSPMap CreateMap(IList<string> values)
LSPMap CreateMap(IList<LSPFunction> values)
LPSMap CreateMap(IList<LSPMap> values)
LSPMap CreateMap(IList<LSPModule> values)
LSPMap CreateMap(IList<LSPValue> values)

Creates a LSPMap and fills it with the provided values. The map can be assigned to any variable in a module with LSPModule.SetMap() or can be part of another map with LSPMap.SetMap().

Returns:

Map created.

See:

LSPMap

LSPValue CreateNil()

Creates a nil value.

Returns:

Nil value created.

See:

LSPValue

LSPValue CreateInt(long value)

Creates an integer value. The value can be assigned to any variable in a module with LSPModule.SetValue() or can be part of a map as key or value.

Returns:

Integer value created.

See:

LSPValue

LSPValue CreateDouble(double value)

Creates a double value. The value can be assigned to any variable in a module with LSPModule.SetValue() or can be part of a map as key or value.

Returns:

Double value created.

See:

LSPValue

LSPValue CreateBool(bool value)

Creates a boolean value. The value can be assigned to any variable in a module with LSPModule.SetValue() or can be part of a map as key or value.

Returns:

Boolean value created.

See:

LSPValue

LSPValue CreateString(string value)

Creates a string value. The value can be assigned to any variable in a module with LSPModule.SetValue() or can be part of a map as key or value.

Returns:

String value created.

See:

LSPValue

System.IO.TextWriter GetStdOut()

Returns the writer used by the modeler for its standard output methods like Write or WriteLine. The default writer used by the modeler corresponds to the C# standard output, retrieved with Console.Out.

Returns:

The writer used by the modeler for its standard output or null if the standard output is disabled.

void SetStdOut(System.IO.TextWriter writer)

Sets the writer used by the modeler for its standard output methods like Write or WriteLine. The default is to redirect all the modeler’s outputs to the C# standard output. If the given writer is null, the standard output of the modeler will be disabled and all calls to the Write/WriteLine related functions will do nothing.

Arguments:

writer – writer to use for the standard output or null to disable standard output.

System.IO.TextWriter GetStdErr()

Returns the writer used by the modeler for its standard error output. The default writer used by the modeler corresponds to the C# standard error output, retrieved with Console.Error.

Returns:

The writer used by the modeler for its standard error output or null if the standard error output is disabled.

void SetStdErr(System.IO.TextWriter writer)

Sets the writer used by the modeler for its standard error output. The default is to redirect the standard error output stream to the C# standard error output writer. If the given writer is null, the standard error output of the modeler will be disabled.

Arguments:

writer – writer to use for the standard error output or null to disable standard error output.