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.

LSPModule Class

class localsolver.modeler.LSPModule

A module is a collection of global variables. As LSP is a dynamically typed language, global variable can contain any value (including functions and other modules). Each LSP file is a module.

Global variables can be modified and accessed by their name specified as a string. Most of the functions below exist in multiple versions: values of the global variables can be manipulated through their native type (like int, float, String, LSPMap, LSPFunction…) or manipulated with an LSPValue which is a container that can hold any type of value. Using the native type is more convenient and results in less overhead most of the time. LSPValue should only be used when you don’t know the type of the manipulated value.

Summary

Methods
Run Starts the execution of the module using the solver passed in parameter.
RunMain Runs the module in main mode.
GetType Returns the type of the variable with the given name.
GetValue Returns the LSPValue with the given name.
GetInt Returns the integer variable with the given name.
GetDouble Returns the double variable with the given name.
GetBool Returns the boolean variable with the given name.
GetString Returns the string variable with the given name.
GetExpression Returns the LSExpression variable with the given name.
GetFunction Returns the LSPFunction variable with the given name.
GetModule Returns the LSPModule variable with the given name.
GetMap Returns the LSPMap variable with the given name.
SetValue Sets the value associated with the given name.
SetInt Sets the integer value associated with the given name.
SetDouble Sets the double value associated with the given name.
SetBool Sets the boolean value associated with the given name.
SetString Sets the string value associated with the given name.
SetExpression Sets the LSExpression associated with the given name.
SetFunction Sets the function associated with the given name.
SetModule Sets the module associated with the given name.
SetMap Sets the map associated with the given name.
SetNil Unsets the variable with the given name.
Unset Unsets the variable with the given name.
IsNil Returns true if no variable with this name exists in the module or if the variable holds a nil value.
IsInt Returns true if the variable with the given name exists and holds an integer value.
IsDouble Returns true if the variable with the given name exists and holds a double value.
IsBool Returns true if the variable with the given name exists and holds a boolean value.
IsString Returns true if the variable with the given name exists and holds a boolean value.
IsExpression Returns true if the variable with the given name exists and holds a LSExpression.
IsFunction Returns true if the variable with the given name exists and holds a LSPFunction.
IsModule Returns true if the variable with the given name exists and holds a LSPModule.
IsMap Returns true if the variable with the given name exists and holds a LSPMap.
Dispose Releases the reference.

Instance methods

void Run(LocalSolver solver)
void Run(LocalSolver solver, params string[] arguments)
void Run(LocalSolver solver, IEnumerable<string> arguments)

Starts the execution of the module using the solver passed in parameter. Such execution is defined by the following steps:

  • The input function is executed if it exists in the module.
  • The model function is executed. It must be declared in the module.
  • The LSModel is then closed.
  • The param function is executed if it exists in the module.
  • The function Solve is called on the corresponding solver instance. If the display function is defined, it will be called during the resolution process.
  • The output function is executed if it exists in the module.

Note that the solver used as a parameter must have been created by the LSPModeler.CreateSolver method.

If arguments are given, each of them must be of the form argName=argValue. Each argument is then parsed and exposed as a new global variable in the module with argName as the name of the variable and argValue as its value. The format followed by the arguments is exactly the same as in the LocalSolver command line. For more information about the format of the LocalSolver command line you can read Command line.

Arguments:
  • solver (localsolver.LocalSolver) – Instance on which modeling and optimization is performed.
  • arguments – List of arguments to parse and expose as global variables.
void RunMain(params string[] arguments)
void RunMain(IEnumerable<string> arguments)

Runs the module in main mode. In this mode, the modeler behaves like a classical programming language. Therefore, the call sequence is free: you only need to implement a main method which will be the only one invoked by the modeler.

Unlike the optimization mode, the main mode requires you to manually create the solver instances, close your models and launch the resolutions directly in your LSP files. For this you can rely on the builtin module localsolver. In return, you are free to run several successive resolutions or none at all if you just want to use LSP for its pure programming features.

The arguments specified when calling this method are copied as is (as strings, without parsing) into a LSP map that is passed as the first argument to the main function of your LSP file. If the main function of your LSP does not accept any arguments, the parameter arguments is simply ignored.

For more details on the differences between the optimization and the main mode, read Main mode.

Arguments:arguments – List of arguments to copy into a map and pass to the main function.
See:Main mode
See:LocalSolver module
LSPType GetType(string varName)

Returns the type of the variable with the given name. If the variable does not exist in the module, LSPType.Nil is returned.

Arguments:varName (string) – Name of the variable.
Returns:Type of the variable.
Return type:LSPType
LSPValue GetValue(string varName)

Returns the LSPValue with the given name. If the variable does not exist in the module, an LSPValue representing nil is returned.

Arguments:varName (string) – Name of the variable.
Returns:LSPValue associated with the given name.
Return type:LSPValue
long GetInt(string varName)

Returns the integer variable with the given name. The variable must exist and must hold an integer value.

Arguments:varName (string) – Name of the variable.
Returns:Integer value associated with the given name.
Return type:long
double GetDouble(string varName)

Returns the double variable with the given name. The variable must exist and must hold a double value.

Arguments:varName (string) – Name of the variable.
Returns:Double value associated with the given name.
Return type:double
bool GetBool(string varName)

Returns the boolean variable with the given name. The variable must exist and must hold a boolean value.

Arguments:varName (string) – Name of the variable.
Returns:Boolean value associated with the given name.
Return type:bool
string GetString(string varName)

Returns the string variable with the given name. The variable must hold a string value.

Arguments:varName (string) – Name of the variable.
Returns:String value associated with the given name.
Return type:string
LSExpression GetExpression(string varName)

Returns the LSExpression variable with the given name. The variable must exist and must hold an expression.

Arguments:varName (string) – Name of the variable.
Returns:Expression associated with the given name.
Return type:LSExpression
LSPFunction GetFunction(string varName)

Returns the LSPFunction variable with the given name. The variable must exist and must hold a function.

Arguments:varName (string) – Name of the variable.
Returns:Function associated with the given name.
Return type:LSPFunction
LSPModule GetModule(string varName)

Returns the LSPModule variable with the given name. The variable must exist and must hold a module.

Arguments:varName (string) – Name of the variable.
Returns:Module associated with the given name.
Return type:LSPModule
LSPMap GetMap(string varName)

Returns the LSPMap variable with the given name. The variable must exist and must hold a map.

Arguments:varName (string) – Name of the variable.
Returns:Map associated with the given name.
Return type:LSPMap
void SetValue(string varName, LSPValue value)

Sets the value associated with the given name. The variable is automatically created if it doesn’t exist in the module.

Arguments:
  • varName (string) – Name of the variable.
  • value (LSPValue) – Value of the variable.
void SetInt(string varName, long value)

Sets the integer value associated with the given name. The variable is automatically created if it doesn’t exist in the module.

Arguments:
  • varName (string) – Name of the variable.
  • value (long) – Integer value of the variable.
void SetDouble(string varName, double value)

Sets the double value associated with the given name. The variable is automatically created if it doesn’t exist in the module.

Arguments:
  • varName (string) – Name of the variable.
  • value (double) – Double value of the variable.
void SetBool(string varName, bool value)

Sets the boolean value associated with the given name. The variable is automatically created if it doesn’t exist in the module.

Arguments:
  • varName (string) – Name of the variable.
  • value (bool) – Boolean value of the variable.
void SetString(string varName, string value)

Sets the string value associated with the given name. The variable is automatically created if it doesn’t exist in the module.

Arguments:
  • varName (string) – Name of the variable.
  • value (string) – String value of the variable.
void SetExpression(string varName, LSExpression expr)

Sets the LSExpression associated with the given name. The variable is automatically created if it doesn’t exist in the module.

Arguments:
  • varName (string) – Name of the variable.
  • expr (LSExpression) – Value of the variable.
void SetFunction(string varName, LSPFunction function)

Sets the function associated with the given name. The variable is automatically created if it doesn’t exist in the module.

Arguments:
  • varName (string) – Name of the variable.
  • function (LSPFunction) – Value of the variable.
void SetModule(string varName, LSPModule module)

Sets the module associated with the given name. The variable is automatically created if it doesn’t exist in the module.

Arguments:
  • varName (string) – Name of the variable.
  • module (LSPModule) – Value of the variable.
void SetMap(string varName, LSPMap map)

Sets the map associated with the given name. The variable is automatically created if it doesn’t exist in the module.

Arguments:
  • varName (string) – Name of the variable.
  • map (LSPMap) – Value of the variable.
void SetNil(string varName)

Unsets the variable with the given name. Do nothing it the variable does not exist.

Arguments:varName (string) – Name of the variable.
void Unset(string varName)

Unsets the variable with the given name. Do nothing it the variable does not exist.

Arguments:varName (string) – Name of the variable.
bool IsNil(string varName)

Returns true if no variable with this name exists in the module or if the variable holds a nil value.

Arguments:varName (string) – Name of the variable.
bool IsInt(string varName)

Returns true if the variable with the given name exists and holds an integer value.

Arguments:varName (string) – Name of the variable.
bool IsDouble(string varName)

Returns true if the variable with the given name exists and holds a double value.

Arguments:varName (string) – Name of the variable.
bool IsBool(string varName)

Returns true if the variable with the given name exists and holds a boolean value.

Arguments:varName (string) – Name of the variable.
bool IsString(string varName)

Returns true if the variable with the given name exists and holds a boolean value.

Arguments:varName (string) – Name of the variable.
bool IsExpression(string varName)

Returns true if the variable with the given name exists and holds a LSExpression.

Arguments:varName (string) – Name of the variable.
bool IsFunction(string varName)

Returns true if the variable with the given name exists and holds a LSPFunction.

Arguments:varName (string) – Name of the variable.
bool IsModule(string varName)

Returns true if the variable with the given name exists and holds a LSPModule.

Arguments:varName (string) – Name of the variable.
bool IsMap(string varName)

Returns true if the variable with the given name exists and holds a LSPMap.

Arguments:varName (string) – Name of the variable.
void Dispose()

Releases the reference. If this module was already released, returns immediately and does nothing. Invoking any method on this object after this operation will throw an exception.

Note: Releasing a reference does not necessarily imply that the underlying module object is destroyed. It is only destroyed if no more references point to it.

Since:11.5