HxmModule Class

class hexaly.modeler.HxmModule

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

Global variables can be modified and accessed by their name specified as a string.

Summary

Methods

run

Starts the execution of the module using the optimizer passed in parameter.

run_main

Runs the module in main mode.

Special methods

__getitem__

Returns the value of the variable with the given name.

__setitem__

Sets the value of the variable with the given name.

__delitem__

Resets the value of the variable with the given name to nil if the variable exists in the module.

__contains__

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

HxmModule.run(optimizer, *args)

Starts the execution of the module using the optimizer 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 HxModel is then closed.

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

  • The function solve() is called on the corresponding optimizer 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 optimizer used as a parameter must have been created by the HexalyModeler.create_optimizer() 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 Hexaly Optimizer command line. For more information about the format of the Hexaly Optimizer command line you can read Command line.

Parameters:
  • optimizer (HexalyOptimizer) – Instance on which modeling and optimization is performed.

  • args (str or list of str) – List of arguments to parse and expose as global variables.

HxmModule.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 optimizer instances, close your models and launch the resolutions directly in your HXM files. For this you can rely on the builtin module hexaly. In return, you are free to run several successive resolutions or none at all if you just want to use Hexaly Modeler for its pure programming features.

The arguments specified when calling this method are copied as is (as strings, without parsing) into a map that is passed as the first argument to the main function of your HXM file. If the main function of your file 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 of str) – List of arguments to copy into a map and pass to the main function.

See:

Main mode

See:

Hexaly module

Special operators and methods

HxmModule.__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:

This method is automatically called by python when using special constructs like value = myModule[var_name].

Parameters:

var_name (str) – Name of the variable.

HxmModule.__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:

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.

HxmModule.__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 and myModule[var_name] returns None.

HxmModule.__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.