Python API Reference¶
Object-oriented APIs are provided for Python, allowing a full integration of LocalSolver in your Python business applications. LocalSolver’s APIs are lightweight, with only a few classes to manipulate. Note that LocalSolver is a model & run math programming solver: having instantiated the model, no additional code has to be written in order to run the solver.
Build your model
First, you have to create a
LocalSolver environment. It is the
main class of the LocalSolver library. Then, use the methods of the class
LSModel to build your model with expressions. Expressions are
a particularly important concept in LocalSolver. In fact, every aspect of a
model is an expression: variables, objectives and even constraints are
LSExpression. There are 3 different ways to create these
LSExpressions with the class LSModel:
- You can use the available shortcut methods like
- You can also use the more generic version of these operators with the
LSModel.create_expression(). It takes the type of the expression to add as first argument, then the list of the operands of the expression. It is also possible to add operands one-by-one with the method
LSOperatorfor the complete list of available operators.
- Finally, you can use the overloaded operators for common operations:
+, -, *, /, <=, >=, ==, !=, >, <, **, , &, ^, |, ().
Most of these methods accept LSExpressions as arguments but also integers or
double constants. If you prefer, you can also create constants explicitly
Solve your model
Once you have created your model, you have to close it with
LSModel.close() and call
LocalSolver.solve() to launch
the resolution. By default, the search will continue until an optimal
solution is found. To set a time limit or an iteration limit, create
set the according attributes.
Retrieve the solution
The solution is available through the attribute
LocalSolver.solution and carries the values of all expressions
in the model and the status of the solution. There are 4 different statuses:
INCONSISTENT: The solver was able to prove that the model admits no feasible solution.
INFEASIBLE: The solution is infeasible. Some constraints or expressions are violated.
FEASIBLE: The solution is feasible but the optimality was not proven.
OPTIMAL: The solution is optimal. All objective bounds are reached.
You can retrieve statistics of the search (number of iterations, % of
feasible moves, etc.) with the
LSStatistics object. Statistics
are provided for the global search or for each phase.
All classes and methods of the LocalSolver API can throw exceptions.
The exception type related to LocalSolver errors is
- LocalSolver Class
- LSArray Class
- LSBlackBoxArgumentValues Class
- LSBlackBoxContext Class
- LSCollection Class
- LSExpression Class
- LSExternalArgumentValues Class
- LSExternalContext Class
- LSInconsistency Class
- LSModel Class
- LSParam Class
- LSPhase Class
- LSSolution Class
- LSStatistics Class
- LSVersion Class
- LSError Class
- LSCallbackType Enumeration
- LSErrorCode Enumeration
- LSObjectiveDirection Enumeration
- LSOperator Enumeration
- LSSolutionStatus Enumeration
- LSState Enumeration