LSPModule Class¶
- class localsolver.modeler.LSPModule¶
A module is a collection of global variables. As LSP is a dynamically typed language, global variables 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.
Summary¶
Starts the execution of the module using the solver passed in parameter. |
|
Runs the module in main mode. |
Returns the value of the variable with the given name. |
|
Sets the value of the variable with the given name. |
|
Resets the value of the variable with the given name to nil if the variable exists in the module. |
|
Returns true if the variable with the given name exists in the module and if its value is not nil (None in python). |
Instance methods¶
- LSPModule.run(solver, *args)¶
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 thedisplay
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.create_solver()
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 withargName
as the name of the variable andargValue
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.- Parameters
solver (LocalSolver) – Instance on which modeling and optimization is performed.
args (
str
or list ofstr
) – List of arguments to parse and expose as global variables.
- LSPModule.run_main(*args)¶
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
args
is simply ignored.For more details on the differences between the optimization and the main mode, read Main mode.
- Parameters
args (
str
or list ofstr
) – List of arguments to copy into a map and pass to the main function.- See
- See
Special operators and methods¶
- LSPModule.__getitem__(var_name)¶
Returns the value of the variable with the given name. If the variable doesn’t exist in the module, None is returned. This method can return values of the following types:
NoneType
(nil in LSP)bool
int
float
str
LSPValue
This method is automatically called by python when using special constructs like
value = myModule[var_name]
.- Parameters
var_name (
str
) – Name of the variable.
- LSPModule.__setitem__(var_name, value)¶
Sets the value of the variable with the given name. The variable is automatically created if it doesn’t exist in the module. This method accepts the following types as input value:
NoneType
(nil in LSP)bool
int
float
str
LSPValue
Setting a variable to
None
has the same effect as calling__delitem__()
on the variable.This method is automatically called by python when using special constructs like
myModule[var_name] = value
.- Parameters
var_name (
str
) – Name of the variable.value – Value to set.
- LSPModule.__delitem__(var_name, value)¶
Resets the value of the variable with the given name to nil if the variable exists in the module. Do nothing if the variable was not declared or if the variable is already nil. This method is automatically called by python when using special constructs like
del myModule[var_name]
.After calling this method,
var_name in myModule
returns false andmyModule[var_name]
returnsNone
.
- LSPModule.__contains__(var_name)¶
Returns true if the variable with the given name exists in the module and if its value is not nil (None in python). Returns false otherwise. This method is automatically called by python when using special constructs like
var_name in myModule
.