Localsolver  5.5
localsolver::LocalSolver Class Reference

Detailed Description

LocalSolver environment.

Main class of 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).

Note that this API is not thread safe. If multiple threads access and modify a same LocalSolver environment, it must be synchronized. The only methods that can be used without synchronization are getState() and stop().

Please consult LSVersion for copyright and version info.

Public Member Functions

 LocalSolver ()
 Constructs a complete LocalSolver environment and take a token license.
 ~LocalSolver ()
 Deletes this LocalSolver environment and all associated objects.
LSState getState () const
 Gets the state of this LocalSolver environment.
LSModel getModel () const
 Gets the model associated to this LocalSolver environment.
LSParam getParam () const
 Gets the parameters of this LocalSolver environment.
void solve ()
 Solves the model.
void stop ()
 Aborts the resolution previously launched using solve().
LSPhase createPhase ()
 Adds a new phase to this LocalSolver environment.
LSPhase getPhase (int phaseIndex) const
 Gets the phase with the given index.
int getNbPhases () const
 Gets the number of phases.
LSSolution getSolution () const
 Gets the best solution found by the solver.
LSStatistics getStatistics () const
 Gets the statistics of this LocalSolver environment.
std::string toString () const
 Returns a string representation of this LocalSolver environment.
std::string getInfo () const
 Returns useful info about the search while running.
void saveEnvironment (const std::string &filename) const
 Exports the complete environment (model, parameters, solution, ..) in a file.
void loadEnvironment (const std::string &filename)
 Import a complete environment or a model from a file.
void addCallback (LSCallbackType type, LSCallback callback, void *userdata)
 Add a new callback for a specific event type.
bool removeCallback (LSCallbackType type, LSCallback callback)
 Remove the callback for the given event type.
bool removeCallback (LSCallbackType type, LSCallback callback, void *userdata)
 Remove the callback for the given event type.

Constructor & Destructor Documentation

localsolver::LocalSolver::LocalSolver ( )
inline

Constructs a complete LocalSolver environment and take a token license.

If no token is available or if the token server is not accessible, an exception is thrown. The token license is released when the destructor of this LocalSolver environment is called.

localsolver::LocalSolver::~LocalSolver ( )
inline

Deletes this LocalSolver environment and all associated objects.

In particular, the model, its expressions, the phases, the statistics, and the solution are deleted. The token license is then released.

Member Function Documentation

LSState localsolver::LocalSolver::getState ( ) const
inline

Gets the state of this LocalSolver environment.

This method can be called in any state. In particular, this method can be called in state S_Running.

Returns
State of LocalSolver.
LSModel localsolver::LocalSolver::getModel ( ) const
inline

Gets the model associated to this LocalSolver environment.

Once the model is created and closed, the solver can be launched with solve().

Returns
Model.
LSParam localsolver::LocalSolver::getParam ( ) const
inline

Gets the parameters of this LocalSolver environment.

Returns
Parameters.
void localsolver::LocalSolver::solve ( )
inline

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.

void localsolver::LocalSolver::stop ( )
inline

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 S_Running.

LSPhase localsolver::LocalSolver::createPhase ( )
inline

Adds a new phase to this LocalSolver environment.

Only allowed in state S_Stopped.

Returns
Created phase.
LSPhase localsolver::LocalSolver::getPhase ( int  phaseIndex) const
inline

Gets the phase with the given index.

Only allowed in states S_Paused or S_Stopped.

Parameters
phaseIndexIndex of the phase.
Returns
Phase.
int localsolver::LocalSolver::getNbPhases ( ) const
inline

Gets the number of phases.

Only allowed in states S_Paused or S_Stopped.

Returns
Number of phases.
LSSolution localsolver::LocalSolver::getSolution ( ) const
inline

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 S_Paused or S_Stopped.

Returns
Best solution.
LSStatistics localsolver::LocalSolver::getStatistics ( ) const
inline

Gets the statistics of this LocalSolver environment.

Statistics are reset to zero before each resolution. Only allowed in states S_Paused or S_Stopped. Note that for performance reasons, this function always returns the same object.

Returns
Statistics.
See Also
LSStatistics
std::string localsolver::LocalSolver::toString ( ) const
inline

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.
std::string localsolver::LocalSolver::getInfo ( ) const
inline

Returns useful info about the search while running.

Only allowed if the solver has been started at least once. Only allowed in states S_Paused or S_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.
Returns
Info about the search while running.
void localsolver::LocalSolver::saveEnvironment ( const std::string &  filename) const
inline

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
filenameName of the file.
See Also
LocalSolver::loadEnvironment
Since
3.0
void localsolver::LocalSolver::loadEnvironment ( const std::string &  filename)
inline

Import a complete environment or a model from a file.

Only allowed in state S_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
filenameName of the file.
See Also
LocalSolver::saveEnvironment
Since
3.0
void localsolver::LocalSolver::addCallback ( LSCallbackType  type,
LSCallback  callback,
void *  userdata 
)
inline

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 {@link #S_Paused}".

The same callback can be used for different events.

Only allowed in states S_Stopped or S_Modeling.

Parameters
typeEvent to watch.
callbackUser callback. Cannot be null.
userdataA pointer to a user structure.
Since
4.0
bool localsolver::LocalSolver::removeCallback ( LSCallbackType  type,
LSCallback  callback 
)
inline

Remove the callback for the given event type.

Parameters
typeEvent.
callbackUser callback to delete.
Returns
false if the callback was not added for the given event, true otherwise.
Since
4.0
See Also
LocalSolver::addCallback
bool localsolver::LocalSolver::removeCallback ( LSCallbackType  type,
LSCallback  callback,
void *  userdata 
)
inline

Remove the callback for the given event type.

Parameters
typeEvent.
callbackUser callback to delete.
userdataOptional pointer to user data
Returns
false if the callback was not added for the given event, true otherwise.
Since
4.0
See Also
LocalSolver::addCallback