LSPModule Class

class 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, std::string, LSPMap, LSPFunction…) or manipulated through 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.

Since:

10.0

Summary

Functions

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.

getExpression

Returns the LSExpression with the given name.

getString

Returns the string with the given name.

getMap

Returns the LSPMap with the given name.

getFunction

Returns the LSPFunction with the given name.

getModule

Returns the LSPModule with the given name.

setValue

Sets the value associated with the variable with the given name.

setNil

Unsets the variable with the given name.

setInt

Sets the integer value associated with the variable with the given name.

setDouble

Sets the double value associated with the variable with the given name.

setBool

Sets the boolean value associated with the variable with the given name.

setExpression

Sets the LSExpression associated with the variable with the given name.

setString

Sets the string associated with the variable with the given name.

setMap

Sets the map associated with the variable with the given name.

setFunction

Sets the function associated with the variable with the given name.

setModule

Sets the module associated with 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.

isExpression

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

isString

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

isMap

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

isFunction

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

isModule

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

asValue

Returns the module as an LSPValue.

Overloaded operators

LSPValue

Returns the module as an LSPValue.

Functions

void LSPModule::run(LocalSolver &solver)
void LSPModule::run(LocalSolver &solver, const std::vector<std::string> &arguments)
void LSPModule::run(LocalSolver &solver, int argc, const char *const *argv)

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 localsolver::LSModel is then closed.

  • The param function is executed if it exists in the module.

  • The function localsolver::LocalSolver::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.

Parameters:
  • solver – Instance on which modeling and optimization is performed.

  • arguments – List of arguments to parse and expose as global variables.

  • argc – Number of arguments.

  • argv – Pointer to the first argument in the list.

void LSPModule::runMain(const std::vector<std::string> &arguments)
void LSPModule::runMain(int argc, const char *const *argv)

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 parameters arguments, argc, argv are simply ignored.

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

Parameters:
  • arguments – List of arguments to copy into a map and pass to the main function.

  • argc – Number of arguments.

  • argv – Pointer to the first argument in the list.

See:

Main mode

See:

LocalSolver module

LSPType LSPModule::getType(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

Returns:

Type of the variable.

See:

LSPType

LSPValue LSPModule::getValue(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

Returns:

LSPValue associated with the given name.

See:

LSPValue

lsint LSPModule::getInt(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

Returns:

Integer value associated with the given name.

lsdouble LSPModule::getDouble(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

Returns:

Double value associated with the given name.

bool LSPModule::getBool(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

Returns:

Boolean value associated with the given name.

LSExpression LSPModule::getExpression(const std::string &varName) const

Returns the LSExpression with the given name. The variable must exist and must hold a value of type LSP_Expression.

Parameters:

varName – Name of the variable.

Returns:

LSExpression associated with the given name.

std::string LSPModule::getString(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

Returns:

String associated with the given name.

LSPMap LSPModule::getMap(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

Returns:

LSPMap associated with the given name.

LSPFunction LSPModule::getFunction(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

Returns:

LSPFunction associated with the given name.

LSPModule LSPModule::getModule(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

Returns:

LSPModule associated with the given name.

void LSPModule::setValue(const std::string &varName, const LSPValue &value)

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

Parameters:
  • varName – Name of the variable.

  • value – Value of the variable.

void LSPModule::setNil(const std::string &varName)

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

Parameters:

varName – Name of the variable.

void LSPModule::setInt(const std::string &varName, lsint value)

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

Parameters:
  • varName – Name of the variable.

  • value – Value of the variable.

void LSPModule::setDouble(const std::string &varName, lsdouble value)

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

Parameters:
  • varName – Name of the variable.

  • value – Value of the variable.

void LSPModule::setBool(const std::string &varName, bool value)

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

Parameters:
  • varName – Name of the variable.

  • value – Value of the variable.

void LSPModule::setExpression(const std::string &varName, const LSExpression &expr)

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

Parameters:
  • varName – Name of the variable.

  • expr – Value of the variable.

void LSPModule::setString(const std::string &varName, const std::string &value)

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

Parameters:
  • varName – Name of the variable.

  • value – Value of the variable.

void LSPModule::setMap(const std::string &varName, const LSPMap &map)

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

Parameters:
  • varName – Name of the variable.

  • map – Value of the variable.

void LSPModule::setFunction(const std::string &varName, const LSPFunction &function)

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

Parameters:
  • varName – Name of the variable.

  • function – Value of the variable.

void LSPModule::setModule(const std::string &varName, const LSPModule &module)

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

Parameters:
  • varName – Name of the variable.

  • module – Value of the variable.

void LSPModule::unset(const std::string &varName)

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

Parameters:

varName – Name of the variable.

bool LSPModule::isNil(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

bool LSPModule::isInt(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

bool LSPModule::isDouble(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

bool LSPModule::isBool(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

bool LSPModule::isExpression(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

bool LSPModule::isString(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

bool LSPModule::isMap(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

bool LSPModule::isFunction(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

bool LSPModule::isModule(const std::string &varName) const

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

Parameters:

varName – Name of the variable.

LSPValue LSPModule::asValue() const

Returns the module as an LSPValue.

Overloaded operators

explicit operator LSPModule::LSPValue() const

Returns the module as an LSPValue.

See:

LSPValue