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. |
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 |
Export a complete environment or a model to a file. |
load_environment |
Import a complete environment 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. |
__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 or callback, 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
. Note that the effect of this method is not immediate. In particular when called within a callback, the resolution will not be stopped before the end of the callback.See: solve()
See: add_callback()
-
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
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, 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
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)¶ Export a complete environment or a model to a file.
Currently, this function supports 2 file formats :
- LSB : with this format, the complete environment (model, parameters, solution, ...) is exported. This format is useful to debug or replay a model in the same conditions. Since the produced file is binary, it offers good performance and space efficiency.
- LSP : with this format, only the model is exported in the LSP language. This format is useful to have a quick view of the optimization model or to perform some modifications. However, the file may be heavy for big models and the exact reproductibility is not guaranteed since the parameters from one execution to another may differ.
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 from a file. Only allowed in state
LSState.MODELING
. The current model must be empty.The only format supported is LSB. You can obtain a such file thanks to
save_environment()
.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
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
-
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 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