LocalSolver Class

class localsolver::LocalSolver

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.

Summary

Functions
LocalSolver Constructs a complete LocalSolver environment and take a token license.
~LocalSolver Deletes this LocalSolver environment and all associated objects.
getState Gets the state of this LocalSolver environment.
getModel Gets the model associated to this LocalSolver environment.
getParam Gets the parameters of this LocalSolver environment.
solve Solves the model.
computeInconsistency Compute an inconsistency core for this model.
stop Aborts the resolution previously launched using solve().
createPhase Adds a new phase to this LocalSolver environment.
getPhase Gets the phase with the given index.
getNbPhases Gets the number of phases.
getSolution Gets the best solution found by the solver.
getStatistics Gets the statistics of this LocalSolver environment.
toString Returns a string representation of this LocalSolver environment.
getInfo Returns useful info about the search while running.
saveEnvironment Export a complete environment or a model to a file.
loadEnvironment Import a complete environment from a file.
addCallback Add a new callback for a specific event type.
removeCallback Remove the callback for the given event type.

Functions

LocalSolver()

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()

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.

LSState getState() const

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.

Return:State of LocalSolver.

LSModel getModel() const

Gets the model associated to this LocalSolver environment.

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

Return:Model.

LSParam getParam() const

Gets the parameters of this LocalSolver environment.

Return:Parameters.

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

LSInconsistency computeInconsistency() const

Compute an inconsistency core for this model.

Only allowed in state S_Stopped.

Return:The inconsistency core

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

LSPhase createPhase()

Adds a new phase to this LocalSolver environment.

Only allowed in state S_Stopped.

Return:Created phase.

LSPhase getPhase(int phaseIndex) const

Gets the phase with the given index.

Only allowed in states S_Paused or S_Stopped.

Return:Phase.
Parameters:phaseIndex - Index of the phase.

int getNbPhases() const

Gets the number of phases.

Only allowed in states S_Paused or S_Stopped.

Return:Number of phases.

LSSolution getSolution() const

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

Return:Best solution.

LSStatistics getStatistics() const

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.

Return:Statistics.
See:LSStatistics

std::string toString() const

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.

Return:String representation of this LocalSolver.

std::string getInfo() const

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. The displayed information depends on the selected verbosity.isDescent Here are some explanations about the output string for the highest verbosity (2):

  • “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.

Return:Info about the search while running.

void saveEnvironment(const std::string &filename) const

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

See:LocalSolver::loadEnvironment
Since:3.0
Parameters:filename - Name of the file.

void loadEnvironment(const std::string &filename)

Import a complete environment from a file.

Only allowed in state S_Modeling. The current model must be empty.

The only format supported is 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.

See:LocalSolver::saveEnvironment
Since:3.0
Parameters:filename - Name of the file.

void addCallback(LSCallbackType type, LSCallback *callback)

Add a new callback for a specific event type.

The callback will be called each time the given event occurs.

The same callback can be used for different events. Only allowed in states S_Stopped or S_Modeling.

Note 1: When a callback is called, the solver is paused. In that state, you can call all the methods marked as “allowed in state {@link #S_Paused}”. Calling any other method will throw an error. For example, you can stop the resolution from a callback, retrieve the current solution or retrieve the statistics of the solver but you can’t remove a constraint.

Note 2: Please note that LocalSolver does not manage memory of objects created outside of its environment. Thus, you have to explicitly remove and delete your LSCallback at the end of the search.

See:

LSCallback

Since:

4.0

Parameters:
  • type - Event to watch.
  • callback - Callback. Cannot be null.

bool removeCallback(LSCallbackType type, LSCallback *callback)

Remove the callback for the given event type.

Return:

false if the callback was not added for the given event, true otherwise.

See:

LocalSolver::addCallback

See:

LSCallback

Since:

4.0

Parameters:
  • type - Event.
  • callback - User callback to delete.