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:
- Build your model
LSModel
by creating some expressionsLSExpression
. - If desired, parameterize and add phases to the solver
LSParam
,LSPhase
. - Run the solver
LocalSolver
. - Retrieve the best solution found by the solver
LSSolution
. - Consult the statistics of the resolution
LSStatistics
.
Please consult
LSVersion
for copyright and version info.- Build your model
Summary¶
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. |
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. |
solve |
Solves the model. |
compute_inconsistency |
Computes an inconsistency core for this model. |
stop |
Aborts the resolution previously launched using solve(). |
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. |
add_callback |
Add a new callback for a specific event type. |
remove_callback |
Remove the callback for the given event type. |
__str__ |
Returns a string representation of this LocalSolver environment. |
Instance methods¶
-
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.
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.
compute_inconsistency
()¶ Computes an inconsistency core for this model. Only allowed in state
LSState.STOPPED
.
-
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 stateLSState.RUNNING
.
-
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
orLSState.STOPPED
.You can also use the shortcut collection
phases
.Parameters: phase_id ( int
) – Index of the phaseReturns: Phase Return type: LSPhase
-
LocalSolver.
get_nb_phases
()¶ Gets the number of phases. Only allowed in states
LSState.PAUSED
orLSState.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 (such a solution may be infeasible). Only allowed in states
LSState.PAUSED
orLSState.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
orLSState.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
orLSState.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.
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 4 main file formats : LSM, LSB, LP and MPS. The first two are preferable since they are the native format of LocalSolver. The other ones are classically used by linear programming solvers. LocalSolver supports only a subset of features exposed by these last formats. Please refer to the documentation for more details on this subject.
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.
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
orLSState.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
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 forget_phase()
andget_nb_phases()
methods. Please note, that you still have to usecreate_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