Simple examples of running discrete optimization solvers in Python
If needed, install uv. You can then run the examples with uv run manage.py.
You can open this repo in a Visual Studio Code Dev Container with all the required packages automatically installed. You will need to install Visual Studio Code, and a containerization platform like Docker or Podman (see Dev Containers tutorial).
Alternatively, you can create a DevPod Workspace if you want to use other IDEs like VSCodium.
You can open this repo in a container with command-line access using Docker Compose (included in Docker Desktop) or Podman Compose (setup from Podman Desktop), by running the following command in the repo's root directory:
docker compose run --rm devbox
Since the repo directory is mounted into the container as a volume, any changes to the files will be reflected immediately.
If you make any changes to the package requirements, you can rebuild the container with
docker compose build devbox
If needed, install a conda environment manager (recommend a local user installation of either miniforge or micromamba; but mamba, conda, or the Anaconda Distribution will also work).
Then, you can build and activate a conda environment / mamba environment with all the necessary packages by running the following commands in the repo's root directory:
# Replace `conda` with `micromamba` or `mamba` as needed.
conda create -n vscode-python-opt -f environment.yml
conda activate vscode-python-opt
If needed, install and setup a Python environment for your platform.
Then, you can build and activate a Python virtual environment with all the necessary packages by running the following commands in the repo's root directory:
# Windows PowerShell / cmd
# You may need to use `python` instead of `py` if you installed Python via the Microsoft Store app.
py -m venv .venv
.\.venv\Scripts\activate
pip install -r requirements.txt
# Linux / OSX
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Alternatively, if you are using uv, you can run uv sync to update the virtual environment, and/or run the examples with uv run manage.py.
To run the examples, you can use the manage.py script.
To run a specific example, specify the name of the solver and the name of the example:
python manage.py gurobi simple
You can also call manage.py with no arguments to get a CLI showing the possible options:
# Show available solvers
python manage.py
# Show available examples for the solver Gurobi
python manage.py gurobi
An error occurred while loading the CBC library: cannot load library ...
...
nameError: name 'cbclib' is not defined
For whatever reason, Python-MIP seems to be incompatible with OR-Tools, and sometimes fails to find the CBC binary if OR-Tools has also been imported. Two simple workarounds are
- Comment out all imports of OR-Tools when running COIN-OR examples.
- Revert to using an older version
python-mip<1.16(before the CBC binary was decoupled into a separate package).
ImportError: libortools.so.9: undefined symbol: _Z19setLocalOptionValueRK15HighsLogOptionsRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERS_RSt6vectorIP12OptionRecordSaISD_EES7_
OR-Tools currently has an incompatibility issue with HiGHS on certain platforms - see google/or-tools#5246 for the relevant discussion.
A simple workaround is to comment out all imports of OR-Tools or HiGHS depending on the example being run.
When updating package versions, remember to update:
- pyproject.toml
- requirements.txt
- environment.yml
- uv.lock
When updating Python version, remember to update:
- pyproject.toml
- environment.yml
- .python-version
- Dockerfile
- devcontainer.json
- uv.lock
When updating the Docker image version (e.g. 3-VARIANT), remember to update:
- Dockerfile