This page is for an old version of Hexaly Optimizer. We recommend that you update your version and read the documentation for the latest stable release.

Solving your first model in Python

LocalSolver is implemented in C++ language. Nevertheless, 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-and-run math programming solver: having instantiated the model, no additional code has to be written in order to run the solver.

Note

Once you have installed LocalSolver on your computer, the recommended way to link LocalSolver to your Python installation, is to type the following command in a command prompt or in the prompt of your Python environment (Anaconda for instance):

pip install localsolver -i https://pip.localsolver.com

Note that it only configures the integration of LocalSolver in Python. It is not a substitute for a proper installation of LocalSolver with a LocalSolver license. This command must be executed each time you install a new version of LocalSolver.

Below is given the Python code for solving the knapsack toy instance introduced during the Quick tour of LocalSolver’s modeler. The corresponding source file can be retrieved in /examples/toy.

A small example

########## toy.py ##########

import localsolver

with localsolver.LocalSolver() as ls:
    weights = [10, 60, 30, 40, 30, 20, 20, 2]
    values = [1, 10, 15, 40, 60, 90, 100, 15]
    knapsack_bound = 102

    #
    # Declares the optimization model
    # 
    model = ls.model

    # 0-1 decisions
    x = [model.bool() for i in range(8)]

    # weight constraint
    knapsack_weight = model.sum(weights[i]*x[i] for i in range(8))
    model.constraint(knapsack_weight <= knapsack_bound)

    # maximize value
    knapsack_value = model.sum(values[i]*x[i] for i in range(8))
    model.maximize(knapsack_value)

    model.close()

    #
    # Parameterizes the solver
    #
    ls.param.time_limit = 10

    ls.solve()

You can observe that the structure of the Python program follows the structure of corresponding LSP. First, we read input data. Then, we declare the knapsack model by creating some expressions. Once the model is closed, we can parameterize the search before launching the solver.

Running the Python program

A Python distribution must be installed on your computer. LocalSolver is compatible with Python 2.7 and Python >= 3.3.

On any system, if you applied the recommended pip install procedure described above, your Python program is direclty launched with:

python toy.py

Otherwise, executing your program will require setting the PYTHONPATH environment variable beforehand, and the lauching lines will depend on your system.

On Windows, the above program is launched using the following lines:

set PYTHONPATH=%LS_HOME%\bin\
python toy.py

Note that on Windows, in a PowerShell window you would use the following lines:

$env:PYTHONPATH="$env:LS_HOME\bin"
python toy.py

On Linux or Mac OS, the above program is compiled and launched using the following lines:

export PYTHONPATH=/opt/localsolver_9_5/bin/
python toy.py

If you have trouble compiling or launching the program, please have a look at the Installation & licensing instructions. We invite users willing to go further with APIs to consult the Python API Reference.