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.

LocalSolver Class

class LocalSolver

This class contains the LocalSolver environment. It is the main class of the LocalSolver library. Here are described the basic steps for using LocalSolver:

  1. Build your model LSModel by creating some expressions LSExpression.
  2. If desired, parameterize and add phases to the solver LSParam, LSPhase.
  3. Run the solver LocalSolver.
  4. Retrieve the best solution found by the solver LSSolution.
  5. Consult the statistics of the resolution LSStatistics.

Please consult LSVersion for copyright and version info.

Summary

Attributes
state State of the solver.
model Model associated to this environment.
param Parameters of the model.
phases List of the phases of the model.
solution Best solution found.
statistics Statistics.
info Useful info about the search.
Methods
solve Solves the model.
stop Aborts the resolution previously launched using solve().
get_state Gets the state of this LocalSolver environment.
get_model Gets the model associated to this LocalSolver environment.
get_param Gets the parameters of this LocalSolver environment.
create_phase Adds a new phase to this LocalSolver environment.
get_phase Gets the phase with the given index.
get_nb_phases Gets the number of phases.
get_solution Gets the best solution found by the solver.
get_statistics Gets the statistics of this LocalSolver environment.
get_info Returns useful info about the search while running.
save_environment Exports the complete environment (model, parameters, solution, .
load_environment Import a complete environment or a model from a file.
compute_inconsistency Computes an inconsistency core for this model.
add_callback Add a new callback for a specific event type.
remove_callback Remove the callback for the given event type.
delete Delete the LocalSolver objects and release the licence token.
Special methods
__str__ Returns a string representation of this LocalSolver environment.

Instance methods

LocalSolver.solve()

Solves the model. This method returns only when the time limit, the iteration limit or the objective bounds are reached. The model must be closed to launch the resolution.

LocalSolver.stop()

Aborts the resolution previously launched using solve(). If no resolution was launched, this method does nothing. Called from another thread, this method enables users to stop the resolution properly. Solution and statistics remain valid. This method can be called in any state, notably in state LSState.RUNNING.

LocalSolver.get_state()

Gets the state of this LocalSolver environment. This method can be called in any state. In particular, this method can be called in state LSState.RUNNING.

You can also use the shortcut member state

Returns:State of LocalSolver.
Return type:LSState
LocalSolver.get_model()

Gets the model associated to this LocalSolver environment. Once the model is created and closed, the solver can be launched with solve().

You can also use the shortcut member model

Returns:Model
Return type:LSModel
LocalSolver.get_param()

Gets the parameters of this LocalSolver environment.

You can also use the shortcut member param

Returns:Parameters
Return type:LSParam
LocalSolver.create_phase()

Adds a new phase to this LocalSolver environment. Only allowed in state LSState.STOPPED.

Returns:Created phase.
Return type:LSPhase
LocalSolver.get_phase(phase_id)

Gets the phase with the given index. Only allowed in states LSState.PAUSED or LSState.STOPPED.

You can also use the shortcut collection phases.

Parameters:phase_id (int) – Index of the phase
Returns:Phase
Return type:LSPhase
LocalSolver.get_nb_phases()

Gets the number of phases. Only allowed in states LSState.PAUSED or LSState.STOPPED.

You can also use the shortcut collection phases.

Returns:Number of phases.
Return type:int
LocalSolver.get_solution()

Gets the best solution found by the solver. If the solver has not been started at least once, all the decision variables of the solution are set to 0, or to their closest bound if 0 is not part of their domain (such a solution may be infeasible). Only allowed in states LSState.PAUSED or LSState.STOPPED.

You can also use the shortcut member solution

Returns:The best solution.
Return type:LSSolution
LocalSolver.get_statistics()

Gets the statistics of this LocalSolver environment. Statistics are reset to zero before each resolution. Only allowed in states LSState.PAUSED or LSState.STOPPED. Note that for performance reasons, this function always returns the same object.

You can also use the shortcut member statistics

Returns:Statistics.
Return type:LSStatistics
LocalSolver.get_info()

Returns useful info about the search while running. Only allowed if the solver has been started at least once. Only allowed in states LSState.PAUSED or LSState.STOPPED. Useful for debugging or logging purposes. Here are some explanations about the output string:

  • “sec” stands for the number of seconds.
  • “itr” stands for the number of iterations.
  • “infeas” corresponds to the infeasibility score of the best solution found, if infeasible.
  • “obj” corresponds to the objective values of the best solution found, if feasible.
  • “mov” corresponds to the number of moves performed.
  • “inf” corresponds to the percentage of infeasible moves.
  • “acc” corresponds to the percentage of accepted moves.
  • “imp” corresponds to the number of improving moves.

You can also use the shortcut member info

Returns:Info about the search while running
Return type:str
LocalSolver.save_environment(filename)

Exports the complete environment (model, parameters, solution, ..) in a file. This method is useful to debug or replay a model in the same conditions.

Currently, this function supports 2 file formats : LSM and LSB. These two formats reproduce all the parameters and the features of this API. The main difference between LSM and LSB files is the way the fields are organized : LSB files are binary files. They are less readable but offer better performance and are more space-efficient than LSM files. LSB is the recommended format and the only one ensuring an exact export of floating point numbers.

The chosen file format is determined by the file suffix. An exception is thrown if the provided file suffix is not supported. The suffix may optionally be followed by .gz. In that case, this function produces a compressed result (using deflate algorithm).

Parameters:filename (str) – Name of the file.
Since:3.0
LocalSolver.load_environment(filename)

Import a complete environment or a model from a file. Only allowed in state LSState.MODELING. The current model must be empty.

Currently, this function supports 2 file formats : LSM and LSB.

The chosen file format is determined by the file suffix. An exception is thrown if the provided file suffix is not supported. The suffix may optionally be followed by .gz. In that case, this function uncompress the stream before reading.

Parameters:filename (str) – Name of the file.
Since:3.0
LocalSolver.compute_inconsistency()

Computes an inconsistency core for this model. Only allowed in state LSState.STOPPED.

Returns:Inconsistency core
Return type:LSInconsistency
Since:6.0
LocalSolver.add_callback(type, callback)

Add a new callback for a specific event type. The callback will be called each time the given event occurs. When a callback is called, the solver is paused. You can stop the resolution from a callback, retrieve the current solution, retrieve the statistics of the solver and, in general, call all the methods marked as “allowed in state LSState.PAUSED”.

The same callback can be used for different events. Only allowed in states LSState.STOPPED or LSState.MODELING.

The callback function must accept 2 parameters that are respectively the LocalSolver environment and the type of the event.

Parameters:
  • type (LSCallbackType) – Event to watch.
  • callback (function) – User callback. Cannot be null.
Since:

4.0

LocalSolver.remove_callback(type, callback)

Remove the callback for the given event type.

Parameters:
  • type (LSCallbackType) – Event
  • callback (function) – User callback to delete
Returns:

False if the callback was not added for the given event, True otherwise.

Return type:

bool

Since:

4.0

LocalSolver.delete()

Delete the LocalSolver objects and release the licence token. It deletes the native memory used by LocalSolver and makes the LocalSolver licence available for another LocalSolver model. This method is automatically called by the garbage collector of Python when it detects that this LocalSolver object is no longer needed or reachable. This method is also automatically called when you use LocalSolver in a ‘with’ statement.

Instance attributes

All get/set methods have their attribute counterpart. You can use them as shortcuts to improve the readability or your models and codes.

LocalSolver.state

State of the solver. This attribute is read-only. It is a shortcut for get_state().

LocalSolver.model

Model associated to this environment. This attribute is read-only. It is a shortcut for get_model().

LocalSolver.param

Parameters of the model. This attribute is read-only. It is a shortcut for get_param().

LocalSolver.phases

List of the phases of the model. This attribute is read-only. The returned object is iterable, supports the len function and can be indexed with integers. It is a shortcut for get_phase() and get_nb_phases() methods. Please note, that you still have to use create_phase() to add phases.

LocalSolver.solution

Best solution found. This attribute is read-only. It is a shortcut for get_solution().

LocalSolver.statistics

Statistics. This attribute is read-only. It is a shortcut for get_statistics().

LocalSolver.info

Useful info about the search. This attribute is read-only. It is a shortcut for get_info().

Special operators and methods

LocalSolver.__str__()

Returns a string representation of this LocalSolver environment. This representation provides useful info related to the model, the parameters, and the phases (if any). Useful for debugging or logging purposes.

Returns:String representation of this LocalSolver.
Return type:str